lix-installer/src/actions/mod.rs

286 lines
13 KiB
Rust
Raw Normal View History

pub mod base;
pub mod meta;
2022-09-15 19:11:46 +00:00
use base::{
ConfigureNixDaemonService, ConfigureNixDaemonServiceError, CreateDirectory,
CreateDirectoryError, CreateFile, CreateFileError, CreateGroup, CreateGroupError,
CreateOrAppendFile, CreateOrAppendFileError, CreateUser, CreateUserError, FetchNix,
2022-09-28 20:32:54 +00:00
FetchNixError, MoveUnpackedNix, MoveUnpackedNixError, SetupDefaultProfile,
SetupDefaultProfileError, StartSystemdUnit, StartSystemdUnitError, SystemdSysextMerge,
SystemdSysextMergeError,
};
use meta::{
2022-10-17 23:00:16 +00:00
darwin::{CreateApfsVolume, CreateApfsVolumeError},
ConfigureNix, ConfigureNixError, ConfigureShellProfile, ConfigureShellProfileError,
CreateNixTree, CreateNixTreeError, CreateSystemdSysext, CreateSystemdSysextError,
CreateUsersAndGroup, CreateUsersAndGroupError, PlaceChannelConfiguration,
PlaceChannelConfigurationError, PlaceNixConfiguration, PlaceNixConfigurationError,
ProvisionNix, ProvisionNixError,
2022-09-15 19:11:46 +00:00
};
2022-09-26 21:07:53 +00:00
use serde::{de::DeserializeOwned, Deserialize, Serialize};
2022-09-15 19:11:46 +00:00
#[async_trait::async_trait]
pub trait Actionable: DeserializeOwned + Serialize + Into<Action> {
type Error: std::error::Error + std::fmt::Debug + Serialize + Into<ActionError>;
2022-09-27 19:05:24 +00:00
fn describe_execute(&self) -> Vec<ActionDescription>;
fn describe_revert(&self) -> Vec<ActionDescription>;
2022-09-26 21:07:53 +00:00
// They should also have an `async fn plan(args...) -> Result<ActionState<Self>, Self::Error>;`
async fn execute(&mut self) -> Result<(), Self::Error>;
async fn revert(&mut self) -> Result<(), Self::Error>;
2022-09-15 19:11:46 +00:00
}
2022-09-26 23:14:25 +00:00
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub enum ActionState {
Completed,
2022-09-28 20:20:11 +00:00
// Only applicable to meta-actions that start multiple sub-actions.
Progress,
2022-09-26 23:14:25 +00:00
Uncompleted,
2022-09-15 19:11:46 +00:00
}
2022-09-14 22:18:13 +00:00
2022-09-15 17:29:22 +00:00
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
pub struct ActionDescription {
pub description: String,
pub explanation: Vec<String>,
}
impl ActionDescription {
fn new(description: String, explanation: Vec<String>) -> Self {
2022-09-15 19:11:46 +00:00
Self {
description,
explanation,
}
2022-09-15 17:29:22 +00:00
}
}
2022-09-14 22:18:13 +00:00
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
pub enum Action {
2022-10-18 18:03:19 +00:00
DarwinBootstrapVolume(base::darwin::BootstrapVolume),
DarwinCreateSyntheticObjects(base::darwin::CreateSyntheticObjects),
DarwinCreateVolume(base::darwin::CreateVolume),
DarwinEnableOwnership(base::darwin::EnableOwnership),
DarwinEncryptVolume(base::darwin::EncryptVolume),
DarwinKickStartLaunchctlService(base::darwin::KickstartLaunchctlService),
2022-10-18 18:03:19 +00:00
DarwinUnmountVolume(base::darwin::UnmountVolume),
ConfigureNix(ConfigureNix),
2022-10-17 23:00:16 +00:00
ConfigureNixDaemonService(ConfigureNixDaemonService),
ConfigureShellProfile(ConfigureShellProfile),
2022-10-17 23:00:16 +00:00
CreateApfsVolume(CreateApfsVolume),
CreateDirectory(CreateDirectory),
CreateFile(CreateFile),
CreateGroup(CreateGroup),
CreateNixTree(CreateNixTree),
2022-10-17 23:00:16 +00:00
CreateOrAppendFile(CreateOrAppendFile),
CreateSystemdSysext(CreateSystemdSysext),
CreateUser(CreateUser),
CreateUsersAndGroup(CreateUsersAndGroup),
FetchNix(FetchNix),
MoveUnpackedNix(MoveUnpackedNix),
PlaceChannelConfiguration(PlaceChannelConfiguration),
PlaceNixConfiguration(PlaceNixConfiguration),
2022-10-17 23:00:16 +00:00
ProvisionNix(ProvisionNix),
SetupDefaultProfile(SetupDefaultProfile),
StartSystemdUnit(StartSystemdUnit),
SystemdSysextMerge(SystemdSysextMerge),
2022-09-14 22:18:13 +00:00
}
#[derive(Debug, thiserror::Error, serde::Serialize)]
pub enum ActionError {
2022-10-18 18:03:19 +00:00
#[error(transparent)]
DarwinBootstrapVolume(#[from] base::darwin::BootstrapVolumeError),
#[error(transparent)]
DarwinCreateSyntheticObjects(#[from] base::darwin::CreateSyntheticObjectsError),
#[error(transparent)]
DarwinCreateVolume(#[from] base::darwin::CreateVolumeError),
#[error(transparent)]
DarwinEnableOwnership(#[from] base::darwin::EnableOwnershipError),
#[error(transparent)]
DarwinEncryptVolume(#[from] base::darwin::EncryptVolumeError),
#[error(transparent)]
DarwinKickStartLaunchctlService(#[from] base::darwin::KickstartLaunchctlServiceError),
#[error(transparent)]
2022-10-18 18:03:19 +00:00
DarwinUnmountVolume(#[from] base::darwin::UnmountVolumeError),
#[error("Attempted to revert an unexecuted action")]
NotExecuted(Action),
#[error("Attempted to execute an already executed action")]
AlreadyExecuted(Action),
#[error("Attempted to revert an already reverted action")]
AlreadyReverted(Action),
#[error(transparent)]
2022-10-17 23:00:16 +00:00
CreateApfsVolume(#[from] CreateApfsVolumeError),
#[error(transparent)]
ConfigureNixDaemonService(#[from] ConfigureNixDaemonServiceError),
#[error(transparent)]
ConfigureNix(#[from] ConfigureNixError),
#[error(transparent)]
ConfigureShellProfile(#[from] ConfigureShellProfileError),
#[error(transparent)]
CreateDirectory(#[from] CreateDirectoryError),
#[error(transparent)]
CreateSystemdSysext(#[from] CreateSystemdSysextError),
#[error(transparent)]
CreateFile(#[from] CreateFileError),
#[error(transparent)]
CreateGroup(#[from] CreateGroupError),
#[error(transparent)]
CreateOrAppendFile(#[from] CreateOrAppendFileError),
#[error(transparent)]
CreateNixTree(#[from] CreateNixTreeError),
#[error(transparent)]
CreateUser(#[from] CreateUserError),
#[error(transparent)]
CreateUsersAndGroup(#[from] CreateUsersAndGroupError),
#[error(transparent)]
FetchNix(#[from] FetchNixError),
#[error(transparent)]
MoveUnpackedNix(#[from] MoveUnpackedNixError),
#[error(transparent)]
PlaceChannelConfiguration(#[from] PlaceChannelConfigurationError),
#[error(transparent)]
PlaceNixConfiguration(#[from] PlaceNixConfigurationError),
#[error(transparent)]
SetupDefaultProfile(#[from] SetupDefaultProfileError),
#[error(transparent)]
StartSystemdUnit(#[from] StartSystemdUnitError),
#[error(transparent)]
SystemdSysExtMerge(#[from] SystemdSysextMergeError),
#[error(transparent)]
ProvisionNix(#[from] ProvisionNixError),
2022-09-14 22:18:13 +00:00
}
#[async_trait::async_trait]
impl Actionable for Action {
type Error = ActionError;
2022-09-27 19:05:24 +00:00
fn describe_execute(&self) -> Vec<ActionDescription> {
match self {
2022-10-18 18:03:19 +00:00
Action::DarwinBootstrapVolume(i) => i.describe_execute(),
Action::DarwinCreateSyntheticObjects(i) => i.describe_execute(),
Action::DarwinCreateVolume(i) => i.describe_execute(),
Action::DarwinEnableOwnership(i) => i.describe_execute(),
Action::DarwinEncryptVolume(i) => i.describe_execute(),
Action::DarwinUnmountVolume(i) => i.describe_execute(),
2022-09-27 19:05:24 +00:00
Action::ConfigureNix(i) => i.describe_execute(),
2022-10-17 23:00:16 +00:00
Action::ConfigureNixDaemonService(i) => i.describe_execute(),
2022-09-27 19:05:24 +00:00
Action::ConfigureShellProfile(i) => i.describe_execute(),
2022-10-17 23:00:16 +00:00
Action::CreateApfsVolume(i) => i.describe_execute(),
2022-09-27 19:05:24 +00:00
Action::CreateDirectory(i) => i.describe_execute(),
Action::CreateFile(i) => i.describe_execute(),
Action::CreateGroup(i) => i.describe_execute(),
Action::CreateNixTree(i) => i.describe_execute(),
2022-10-17 23:00:16 +00:00
Action::CreateOrAppendFile(i) => i.describe_execute(),
Action::CreateSystemdSysext(i) => i.describe_execute(),
2022-09-27 19:05:24 +00:00
Action::CreateUser(i) => i.describe_execute(),
Action::CreateUsersAndGroup(i) => i.describe_execute(),
Action::FetchNix(i) => i.describe_execute(),
Action::MoveUnpackedNix(i) => i.describe_execute(),
Action::PlaceChannelConfiguration(i) => i.describe_execute(),
Action::PlaceNixConfiguration(i) => i.describe_execute(),
2022-10-17 23:00:16 +00:00
Action::ProvisionNix(i) => i.describe_execute(),
2022-09-27 19:05:24 +00:00
Action::SetupDefaultProfile(i) => i.describe_execute(),
Action::DarwinKickStartLaunchctlService(i) => i.describe_execute(),
2022-09-27 19:05:24 +00:00
Action::StartSystemdUnit(i) => i.describe_execute(),
Action::SystemdSysextMerge(i) => i.describe_execute(),
2022-09-14 22:18:13 +00:00
}
}
async fn execute(&mut self) -> Result<(), Self::Error> {
match self {
2022-10-18 18:03:19 +00:00
Action::DarwinBootstrapVolume(i) => i.execute().await?,
Action::DarwinCreateSyntheticObjects(i) => i.execute().await?,
Action::DarwinCreateVolume(i) => i.execute().await?,
Action::DarwinEnableOwnership(i) => i.execute().await?,
Action::DarwinEncryptVolume(i) => i.execute().await?,
Action::DarwinUnmountVolume(i) => i.execute().await?,
Action::ConfigureNix(i) => i.execute().await?,
2022-10-17 23:00:16 +00:00
Action::ConfigureNixDaemonService(i) => i.execute().await?,
Action::ConfigureShellProfile(i) => i.execute().await?,
2022-10-17 23:00:16 +00:00
Action::CreateApfsVolume(i) => i.execute().await?,
Action::CreateDirectory(i) => i.execute().await?,
Action::CreateFile(i) => i.execute().await?,
Action::CreateGroup(i) => i.execute().await?,
Action::CreateNixTree(i) => i.execute().await?,
2022-10-17 23:00:16 +00:00
Action::CreateOrAppendFile(i) => i.execute().await?,
Action::CreateSystemdSysext(i) => i.execute().await?,
Action::CreateUser(i) => i.execute().await?,
Action::CreateUsersAndGroup(i) => i.execute().await?,
Action::FetchNix(i) => i.execute().await?,
Action::MoveUnpackedNix(i) => i.execute().await?,
Action::PlaceChannelConfiguration(i) => i.execute().await?,
Action::PlaceNixConfiguration(i) => i.execute().await?,
2022-10-17 23:00:16 +00:00
Action::ProvisionNix(i) => i.execute().await?,
Action::SetupDefaultProfile(i) => i.execute().await?,
Action::DarwinKickStartLaunchctlService(i) => i.execute().await?,
Action::StartSystemdUnit(i) => i.execute().await?,
Action::SystemdSysextMerge(i) => i.execute().await?,
};
Ok(())
2022-09-14 22:18:13 +00:00
}
2022-09-27 19:05:24 +00:00
fn describe_revert(&self) -> Vec<ActionDescription> {
match self {
2022-10-18 18:03:19 +00:00
Action::DarwinBootstrapVolume(i) => i.describe_revert(),
Action::DarwinCreateSyntheticObjects(i) => i.describe_revert(),
Action::DarwinCreateVolume(i) => i.describe_revert(),
Action::DarwinEnableOwnership(i) => i.describe_revert(),
Action::DarwinEncryptVolume(i) => i.describe_revert(),
Action::DarwinUnmountVolume(i) => i.describe_revert(),
2022-09-27 19:05:24 +00:00
Action::ConfigureNix(i) => i.describe_revert(),
2022-10-17 23:00:16 +00:00
Action::ConfigureNixDaemonService(i) => i.describe_revert(),
2022-09-27 19:05:24 +00:00
Action::ConfigureShellProfile(i) => i.describe_revert(),
2022-10-17 23:00:16 +00:00
Action::CreateApfsVolume(i) => i.describe_revert(),
2022-09-27 19:05:24 +00:00
Action::CreateDirectory(i) => i.describe_revert(),
Action::CreateFile(i) => i.describe_revert(),
Action::CreateGroup(i) => i.describe_revert(),
Action::CreateNixTree(i) => i.describe_revert(),
2022-10-17 23:00:16 +00:00
Action::CreateOrAppendFile(i) => i.describe_revert(),
Action::CreateSystemdSysext(i) => i.describe_revert(),
2022-09-27 19:05:24 +00:00
Action::CreateUser(i) => i.describe_revert(),
Action::CreateUsersAndGroup(i) => i.describe_revert(),
Action::FetchNix(i) => i.describe_revert(),
Action::MoveUnpackedNix(i) => i.describe_revert(),
Action::PlaceChannelConfiguration(i) => i.describe_revert(),
Action::PlaceNixConfiguration(i) => i.describe_revert(),
2022-10-17 23:00:16 +00:00
Action::ProvisionNix(i) => i.describe_revert(),
2022-09-27 19:05:24 +00:00
Action::SetupDefaultProfile(i) => i.describe_revert(),
Action::DarwinKickStartLaunchctlService(i) => i.describe_revert(),
2022-09-27 19:05:24 +00:00
Action::StartSystemdUnit(i) => i.describe_revert(),
Action::SystemdSysextMerge(i) => i.describe_revert(),
2022-09-27 19:05:24 +00:00
}
}
async fn revert(&mut self) -> Result<(), Self::Error> {
match self {
2022-10-18 18:03:19 +00:00
Action::DarwinBootstrapVolume(i) => i.revert().await?,
Action::DarwinCreateSyntheticObjects(i) => i.revert().await?,
Action::DarwinCreateVolume(i) => i.revert().await?,
Action::DarwinEnableOwnership(i) => i.revert().await?,
Action::DarwinEncryptVolume(i) => i.revert().await?,
Action::DarwinUnmountVolume(i) => i.revert().await?,
Action::ConfigureNix(i) => i.revert().await?,
2022-10-17 23:00:16 +00:00
Action::ConfigureNixDaemonService(i) => i.revert().await?,
Action::ConfigureShellProfile(i) => i.revert().await?,
2022-10-17 23:00:16 +00:00
Action::CreateApfsVolume(i) => i.revert().await?,
Action::CreateDirectory(i) => i.revert().await?,
Action::CreateFile(i) => i.revert().await?,
Action::CreateGroup(i) => i.revert().await?,
Action::CreateNixTree(i) => i.revert().await?,
2022-10-17 23:00:16 +00:00
Action::CreateOrAppendFile(i) => i.revert().await?,
Action::CreateSystemdSysext(i) => i.revert().await?,
Action::CreateUser(i) => i.revert().await?,
Action::CreateUsersAndGroup(i) => i.revert().await?,
Action::FetchNix(i) => i.revert().await?,
Action::MoveUnpackedNix(i) => i.revert().await?,
Action::PlaceChannelConfiguration(i) => i.revert().await?,
Action::PlaceNixConfiguration(i) => i.revert().await?,
2022-10-17 23:00:16 +00:00
Action::ProvisionNix(i) => i.revert().await?,
Action::SetupDefaultProfile(i) => i.revert().await?,
Action::DarwinKickStartLaunchctlService(i) => i.revert().await?,
Action::StartSystemdUnit(i) => i.revert().await?,
Action::SystemdSysextMerge(i) => i.revert().await?,
2022-09-14 22:18:13 +00:00
}
Ok(())
2022-09-14 22:18:13 +00:00
}
}