2022-09-20 18:42:20 +00:00
|
|
|
pub mod base;
|
|
|
|
pub mod meta;
|
2022-09-15 19:11:46 +00:00
|
|
|
|
2022-09-23 19:26:59 +00:00
|
|
|
use std::{error::Error, fmt::Display};
|
|
|
|
|
2022-09-20 18:42:20 +00:00
|
|
|
use base::{
|
2022-09-23 19:26:59 +00:00
|
|
|
ConfigureNixDaemonService, ConfigureNixDaemonServiceError, CreateDirectory,
|
|
|
|
CreateDirectoryError, CreateFile, CreateFileError, CreateGroup, CreateGroupError,
|
|
|
|
CreateOrAppendFile, CreateOrAppendFileError, CreateUser, CreateUserError, FetchNix,
|
|
|
|
FetchNixError, MoveUnpackedNix, MoveUnpackedNixError, PlaceChannelConfiguration,
|
|
|
|
PlaceChannelConfigurationError, PlaceNixConfiguration, PlaceNixConfigurationError,
|
|
|
|
SetupDefaultProfile, SetupDefaultProfileError,
|
2022-09-20 18:42:20 +00:00
|
|
|
};
|
|
|
|
use meta::{
|
2022-09-23 19:26:59 +00:00
|
|
|
ConfigureNix, ConfigureNixError, ConfigureShellProfile, ConfigureShellProfileError,
|
|
|
|
CreateNixTree, CreateNixTreeError, CreateUsersAndGroup, CreateUsersAndGroupError,
|
|
|
|
ProvisionNix, ProvisionNixError, StartNixDaemon, StartNixDaemonError,
|
2022-09-15 19:11:46 +00:00
|
|
|
};
|
2022-09-23 19:26:59 +00:00
|
|
|
use serde::{Deserialize, de::DeserializeOwned, Serialize};
|
|
|
|
|
2022-09-20 18:42:20 +00:00
|
|
|
|
2022-09-23 19:26:59 +00:00
|
|
|
use self::base::{StartSystemdUnit, StartSystemdUnitError};
|
2022-09-15 19:11:46 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
2022-09-23 23:22:50 +00:00
|
|
|
pub trait Actionable: DeserializeOwned + Serialize + Into<Action> {
|
|
|
|
type Error: std::error::Error + std::fmt::Debug + Serialize + Into<ActionError>;
|
2022-09-23 19:26:59 +00:00
|
|
|
|
2022-09-15 19:11:46 +00:00
|
|
|
fn description(&self) -> Vec<ActionDescription>;
|
2022-09-23 19:26:59 +00:00
|
|
|
|
|
|
|
// They should also have an `async fn plan(args...) -> Result<ActionState<Self>, Self::Error>;`
|
2022-09-23 23:22:50 +00:00
|
|
|
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-23 23:22:50 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
pub enum ActionState {
|
|
|
|
Completed,
|
|
|
|
Planned,
|
|
|
|
Reverted,
|
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-09-23 23:22:50 +00:00
|
|
|
ConfigureNixDaemonService(ConfigureNixDaemonService),
|
|
|
|
ConfigureNix(ConfigureNix),
|
|
|
|
ConfigureShellProfile(ConfigureShellProfile),
|
|
|
|
CreateDirectory(CreateDirectory),
|
|
|
|
CreateFile(CreateFile),
|
|
|
|
CreateGroup(CreateGroup),
|
|
|
|
CreateOrAppendFile(CreateOrAppendFile),
|
|
|
|
CreateNixTree(CreateNixTree),
|
|
|
|
CreateUser(CreateUser),
|
|
|
|
CreateUsersAndGroup(CreateUsersAndGroup),
|
|
|
|
FetchNix(FetchNix),
|
|
|
|
MoveUnpackedNix(MoveUnpackedNix),
|
|
|
|
PlaceChannelConfiguration(PlaceChannelConfiguration),
|
|
|
|
PlaceNixConfiguration(PlaceNixConfiguration),
|
|
|
|
SetupDefaultProfile(SetupDefaultProfile),
|
|
|
|
StartNixDaemon(StartNixDaemon),
|
|
|
|
StartSystemdUnit(StartSystemdUnit),
|
|
|
|
ProvisionNix(ProvisionNix),
|
2022-09-14 22:18:13 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 19:26:59 +00:00
|
|
|
#[derive(Debug, thiserror::Error, serde::Serialize)]
|
|
|
|
pub enum ActionError {
|
|
|
|
#[error("Attempted to revert an unexecuted action")]
|
2022-09-23 23:22:50 +00:00
|
|
|
NotExecuted(Action),
|
2022-09-23 19:26:59 +00:00
|
|
|
#[error("Attempted to execute an already executed action")]
|
2022-09-23 23:22:50 +00:00
|
|
|
AlreadyExecuted(Action),
|
2022-09-23 19:26:59 +00:00
|
|
|
#[error("Attempted to revert an already reverted action")]
|
2022-09-23 23:22:50 +00:00
|
|
|
AlreadyReverted(Action),
|
2022-09-23 19:26:59 +00:00
|
|
|
#[error(transparent)]
|
|
|
|
ConfigureNixDaemonService(#[from] ConfigureNixDaemonServiceError),
|
|
|
|
#[error(transparent)]
|
|
|
|
ConfigureNix(#[from] ConfigureNixError),
|
|
|
|
#[error(transparent)]
|
|
|
|
ConfigureShellProfile(#[from] ConfigureShellProfileError),
|
|
|
|
#[error(transparent)]
|
|
|
|
CreateDirectory(#[from] CreateDirectoryError),
|
|
|
|
#[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)]
|
|
|
|
StartNixDaemon(#[from] StartNixDaemonError),
|
|
|
|
#[error(transparent)]
|
|
|
|
StartSystemdUnit(#[from] StartSystemdUnitError),
|
|
|
|
#[error(transparent)]
|
|
|
|
ProvisionNix(#[from] ProvisionNixError),
|
2022-09-14 22:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
2022-09-23 23:22:50 +00:00
|
|
|
impl Actionable for Action {
|
2022-09-23 19:26:59 +00:00
|
|
|
type Error = ActionError;
|
2022-09-15 17:29:22 +00:00
|
|
|
fn description(&self) -> Vec<ActionDescription> {
|
2022-09-23 23:22:50 +00:00
|
|
|
match self {
|
2022-09-15 19:11:46 +00:00
|
|
|
Action::ConfigureNixDaemonService(i) => i.description(),
|
|
|
|
Action::ConfigureNix(i) => i.description(),
|
|
|
|
Action::ConfigureShellProfile(i) => i.description(),
|
|
|
|
Action::CreateDirectory(i) => i.description(),
|
2022-09-21 20:26:45 +00:00
|
|
|
Action::CreateFile(i) => i.description(),
|
2022-09-15 19:11:46 +00:00
|
|
|
Action::CreateGroup(i) => i.description(),
|
2022-09-21 20:26:45 +00:00
|
|
|
Action::CreateOrAppendFile(i) => i.description(),
|
2022-09-15 19:11:46 +00:00
|
|
|
Action::CreateNixTree(i) => i.description(),
|
2022-09-14 22:18:13 +00:00
|
|
|
Action::CreateUser(i) => i.description(),
|
2022-09-20 18:42:20 +00:00
|
|
|
Action::CreateUsersAndGroup(i) => i.description(),
|
|
|
|
Action::FetchNix(i) => i.description(),
|
|
|
|
Action::MoveUnpackedNix(i) => i.description(),
|
2022-09-15 19:11:46 +00:00
|
|
|
Action::PlaceChannelConfiguration(i) => i.description(),
|
|
|
|
Action::PlaceNixConfiguration(i) => i.description(),
|
|
|
|
Action::SetupDefaultProfile(i) => i.description(),
|
|
|
|
Action::StartNixDaemon(i) => i.description(),
|
2022-09-23 19:26:59 +00:00
|
|
|
Action::StartSystemdUnit(i) => i.description(),
|
2022-09-20 18:42:20 +00:00
|
|
|
Action::ProvisionNix(i) => i.description(),
|
2022-09-14 22:18:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 19:26:59 +00:00
|
|
|
async fn execute(&mut self) -> Result<(), Self::Error> {
|
2022-09-23 23:22:50 +00:00
|
|
|
match self {
|
|
|
|
Action::ConfigureNixDaemonService(i) => i.execute().await?,
|
|
|
|
Action::ConfigureNix(i) => i.execute().await?,
|
|
|
|
Action::ConfigureShellProfile(i) => i.execute().await?,
|
|
|
|
Action::CreateDirectory(i) => i.execute().await?,
|
|
|
|
Action::CreateFile(i) => i.execute().await?,
|
|
|
|
Action::CreateGroup(i) => i.execute().await?,
|
|
|
|
Action::CreateOrAppendFile(i) => i.execute().await?,
|
|
|
|
Action::CreateNixTree(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?,
|
|
|
|
Action::SetupDefaultProfile(i) => i.execute().await?,
|
|
|
|
Action::StartNixDaemon(i) => i.execute().await?,
|
|
|
|
Action::StartSystemdUnit(i) => i.execute().await?,
|
|
|
|
Action::ProvisionNix(i) => i.execute().await?,
|
2022-09-23 19:26:59 +00:00
|
|
|
};
|
2022-09-23 23:22:50 +00:00
|
|
|
Ok(())
|
2022-09-14 22:18:13 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 19:26:59 +00:00
|
|
|
async fn revert(&mut self) -> Result<(), Self::Error> {
|
2022-09-23 23:22:50 +00:00
|
|
|
match self {
|
|
|
|
Action::ConfigureNixDaemonService(i) => i.revert().await?,
|
|
|
|
Action::ConfigureNix(i) => i.revert().await?,
|
|
|
|
Action::ConfigureShellProfile(i) => i.revert().await?,
|
|
|
|
Action::CreateDirectory(i) => i.revert().await?,
|
|
|
|
Action::CreateFile(i) => i.revert().await?,
|
|
|
|
Action::CreateGroup(i) => i.revert().await?,
|
|
|
|
Action::CreateOrAppendFile(i) => i.revert().await?,
|
|
|
|
Action::CreateNixTree(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?,
|
|
|
|
Action::SetupDefaultProfile(i) => i.revert().await?,
|
|
|
|
Action::StartNixDaemon(i) => i.revert().await?,
|
|
|
|
Action::StartSystemdUnit(i) => i.revert().await?,
|
|
|
|
Action::ProvisionNix(i) => i.revert().await?,
|
2022-09-14 22:18:13 +00:00
|
|
|
}
|
2022-09-23 23:22:50 +00:00
|
|
|
Ok(())
|
2022-09-14 22:18:13 +00:00
|
|
|
}
|
|
|
|
}
|