Cleanup logging

This commit is contained in:
Ana Hobden 2022-09-26 16:14:25 -07:00
parent 625404665b
commit 257d82b3b4
21 changed files with 282 additions and 90 deletions

View file

@ -25,7 +25,7 @@ impl ConfigureNixDaemonService {
return Err(ConfigureNixDaemonServiceError::InitNotSupported);
}
Ok(Self {
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -48,7 +48,11 @@ impl Actionable for ConfigureNixDaemonService {
#[tracing::instrument(skip_all)]
async fn execute(&mut self) -> Result<(), Self::Error> {
let Self { action_state } = self;
tracing::info!("Configuring nix daemon service");
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Configuring nix daemon service");
return Ok(());
}
tracing::debug!("Configuring nix daemon service");
tracing::trace!(src = TMPFILES_SRC, dest = TMPFILES_DEST, "Symlinking");
tokio::fs::symlink(TMPFILES_SRC, TMPFILES_DEST)
@ -77,6 +81,7 @@ impl Actionable for ConfigureNixDaemonService {
.await
.map_err(Self::Error::CommandFailed)?;
tracing::trace!("Configured nix daemon service");
*action_state = ActionState::Completed;
Ok(())
}
@ -84,7 +89,11 @@ impl Actionable for ConfigureNixDaemonService {
#[tracing::instrument(skip_all)]
async fn revert(&mut self) -> Result<(), Self::Error> {
let Self { action_state } = self;
tracing::info!("Unconfiguring nix daemon service");
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Unconfiguring nix daemon service");
return Ok(());
}
tracing::debug!("Unconfiguring nix daemon service");
// We don't need to do this! Systemd does it for us! (In fact, it's an error if we try to do this...)
execute_command(Command::new("systemctl").args(["disable", SOCKET_SRC]))
@ -111,7 +120,8 @@ impl Actionable for ConfigureNixDaemonService {
.await
.map_err(Self::Error::CommandFailed)?;
*action_state = ActionState::Reverted;
tracing::trace!("Unconfigured nix daemon service");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -42,7 +42,7 @@ impl CreateDirectory {
user,
group,
mode,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -67,7 +67,12 @@ impl Actionable for CreateDirectory {
)]
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
path = %self.path.display(),
user = self.user,
group = self.group,
mode = format!("{:#o}", self.mode),
))]
async fn execute(&mut self) -> Result<(), Self::Error> {
let Self {
path,
@ -76,6 +81,11 @@ impl Actionable for CreateDirectory {
mode,
action_state,
} = self;
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Creating directory");
return Ok(());
}
tracing::debug!("Creating directory");
let gid = Group::from_name(group.as_str())
.map_err(|e| Self::Error::GroupId(group.clone(), e))?
@ -86,22 +96,26 @@ impl Actionable for CreateDirectory {
.ok_or(Self::Error::NoUser(user.clone()))?
.uid;
tracing::trace!(path = %path.display(), "Creating directory");
create_dir(path.clone())
.await
.map_err(|e| Self::Error::Creating(path.clone(), e))?;
chown(path, Some(uid), Some(gid)).map_err(|e| Self::Error::Chown(path.clone(), e))?;
tracing::trace!(path = %path.display(), "Changing permissions on directory");
tokio::fs::set_permissions(&path, PermissionsExt::from_mode(*mode))
.await
.map_err(|e| Self::Error::SetPermissions(*mode, path.to_owned(), e))?;
tracing::trace!("Created directory");
*action_state = ActionState::Completed;
Ok(())
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
path = %self.path.display(),
user = self.user,
group = self.group,
mode = format!("{:#o}", self.mode),
))]
async fn revert(&mut self) -> Result<(), Self::Error> {
let Self {
path,
@ -110,13 +124,19 @@ impl Actionable for CreateDirectory {
mode: _,
action_state,
} = self;
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Removing directory");
return Ok(());
}
tracing::debug!("Removing directory");
tracing::trace!(path = %path.display(), "Removing directory");
remove_dir_all(path.clone())
.await
.map_err(|e| Self::Error::Removing(path.clone(), e))?;
*action_state = ActionState::Reverted;
tracing::trace!("Removed directory");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -44,7 +44,7 @@ impl CreateFile {
mode,
buf,
force,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -70,7 +70,12 @@ impl Actionable for CreateFile {
)]
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
path = %self.path.display(),
user = self.user,
group = self.group,
mode = format!("{:#o}", self.mode),
))]
async fn execute(&mut self) -> Result<(), Self::Error> {
let Self {
path,
@ -81,7 +86,12 @@ impl Actionable for CreateFile {
force: _,
action_state,
} = self;
tracing::trace!(path = %path.display(), "Creating file");
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Creating file");
return Ok(());
}
tracing::debug!("Creating file");
let mut file = OpenOptions::new()
.create_new(true)
.mode(*mode)
@ -104,14 +114,19 @@ impl Actionable for CreateFile {
.ok_or(Self::Error::NoUser(user.clone()))?
.uid;
tracing::trace!(path = %path.display(), "Chowning file");
chown(path, Some(uid), Some(gid)).map_err(|e| Self::Error::Chown(path.clone(), e))?;
tracing::trace!("Created file");
*action_state = ActionState::Completed;
Ok(())
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
path = %self.path.display(),
user = self.user,
group = self.group,
mode = format!("{:#o}", self.mode),
))]
async fn revert(&mut self) -> Result<(), Self::Error> {
let Self {
path,
@ -122,14 +137,18 @@ impl Actionable for CreateFile {
force: _,
action_state,
} = self;
tracing::trace!(path = %path.display(), "Deleting file");
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Deleting file");
return Ok(());
}
tracing::debug!("Deleting file");
remove_file(&path)
.await
.map_err(|e| Self::Error::RemoveFile(path.to_owned(), e))?;
*action_state = ActionState::Reverted;
tracing::trace!("Deleted file");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -18,7 +18,7 @@ impl CreateGroup {
Self {
name,
gid,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
}
}
}
@ -40,35 +40,53 @@ impl Actionable for CreateGroup {
)]
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
user = self.name,
gid = self.gid,
))]
async fn execute(&mut self) -> Result<(), Self::Error> {
let Self {
name,
gid,
action_state,
} = self;
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Creating group");
return Ok(());
}
tracing::debug!("Creating group");
execute_command(Command::new("groupadd").args(["-g", &gid.to_string(), "--system", &name]))
.await
.map_err(CreateGroupError::Command)?;
tracing::trace!("Created group");
*action_state = ActionState::Completed;
Ok(())
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
user = self.name,
gid = self.gid,
))]
async fn revert(&mut self) -> Result<(), Self::Error> {
let Self {
name,
gid: _,
action_state,
} = self;
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Deleting group");
return Ok(());
}
tracing::debug!("Deleting group");
execute_command(Command::new("groupdel").arg(&name))
.await
.map_err(CreateGroupError::Command)?;
*action_state = ActionState::Reverted;
tracing::trace!("Deleted group");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -41,7 +41,7 @@ impl CreateOrAppendFile {
group,
mode,
buf,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -76,8 +76,12 @@ impl Actionable for CreateOrAppendFile {
buf,
action_state,
} = self;
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Creating or appending fragment to file");
return Ok(());
}
tracing::debug!("Creating or appending fragment to file");
tracing::trace!(path = %path.display(), "Creating or appending");
let mut file = OpenOptions::new()
.create(true)
.write(true)
@ -103,14 +107,13 @@ impl Actionable for CreateOrAppendFile {
.ok_or(Self::Error::NoUser(user.clone()))?
.uid;
tracing::trace!(path = %path.display(), "Changing permissions on file");
tokio::fs::set_permissions(&path, PermissionsExt::from_mode(*mode))
.await
.map_err(|e| Self::Error::SetPermissions(*mode, path.to_owned(), e))?;
tracing::trace!(path = %path.display(), "Chowning");
chown(path, Some(uid), Some(gid)).map_err(|e| Self::Error::Chown(path.clone(), e))?;
tracing::trace!("Created or appended fragment to file");
*action_state = ActionState::Completed;
Ok(())
}
@ -125,7 +128,11 @@ impl Actionable for CreateOrAppendFile {
buf,
action_state,
} = self;
tracing::trace!(path = %path.display(), "Deleting or trimming content from file");
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already completed: Removing fragment from file (and deleting it if it becomes empty)");
return Ok(());
}
tracing::debug!("Removing fragment from file (and deleting it if it becomes empty)");
let mut file = OpenOptions::new()
.create(false)
@ -149,6 +156,8 @@ impl Actionable for CreateOrAppendFile {
remove_file(&path)
.await
.map_err(|e| Self::Error::RemoveFile(path.to_owned(), e))?;
tracing::trace!("Removed file (since all content was removed)");
} else {
file.seek(SeekFrom::Start(0))
.await
@ -156,9 +165,10 @@ impl Actionable for CreateOrAppendFile {
file.write_all(file_contents.as_bytes())
.await
.map_err(|e| Self::Error::WriteFile(path.to_owned(), e))?;
}
*action_state = ActionState::Reverted;
tracing::trace!("Removed fragment from from file");
}
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -20,7 +20,7 @@ impl CreateUser {
name,
uid,
gid,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
}
}
}
@ -47,6 +47,11 @@ impl Actionable for CreateUser {
gid,
action_state,
} = self;
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Creating user");
return Ok(());
}
tracing::debug!("Creating user");
execute_command(Command::new("useradd").args([
"--home-dir",
@ -70,6 +75,7 @@ impl Actionable for CreateUser {
.await
.map_err(Self::Error::Command)?;
tracing::trace!("Created user");
*action_state = ActionState::Completed;
Ok(())
}
@ -82,12 +88,18 @@ impl Actionable for CreateUser {
gid: _,
action_state,
} = self;
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already completed: Deleting user");
return Ok(());
}
tracing::debug!("Deleting user");
execute_command(Command::new("userdel").args([&name.to_string()]))
.await
.map_err(Self::Error::Command)?;
*action_state = ActionState::Completed;
tracing::trace!("Deleted user");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -23,7 +23,7 @@ impl FetchNix {
Ok(Self {
url,
destination,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -53,8 +53,12 @@ impl Actionable for FetchNix {
destination,
action_state,
} = self;
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Fetching Nix");
return Ok(());
}
tracing::debug!("Fetching Nix");
tracing::trace!(%url, "Fetching url");
let res = reqwest::get(url.clone())
.await
.map_err(Self::Error::Reqwest)?;
@ -73,6 +77,7 @@ impl Actionable for FetchNix {
handle?;
tracing::trace!("Fetched Nix");
*action_state = ActionState::Completed;
Ok(())
}
@ -85,9 +90,12 @@ impl Actionable for FetchNix {
action_state,
} = self;
tracing::trace!("Nothing to do for `FetchNix` revert");
*action_state = ActionState::Reverted;
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Unfetch Nix (noop)");
return Ok(());
}
tracing::debug!("Unfetch Nix (noop)");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -8,8 +8,6 @@ mod create_or_append_file;
mod create_user;
mod fetch_nix;
mod move_unpacked_nix;
mod place_channel_configuration;
mod place_nix_configuration;
mod setup_default_profile;
mod start_systemd_unit;
@ -21,7 +19,5 @@ pub use create_or_append_file::{CreateOrAppendFile, CreateOrAppendFileError};
pub use create_user::{CreateUser, CreateUserError};
pub use fetch_nix::{FetchNix, FetchNixError};
pub use move_unpacked_nix::{MoveUnpackedNix, MoveUnpackedNixError};
pub use place_channel_configuration::{PlaceChannelConfiguration, PlaceChannelConfigurationError};
pub use place_nix_configuration::{PlaceNixConfiguration, PlaceNixConfigurationError};
pub use setup_default_profile::{SetupDefaultProfile, SetupDefaultProfileError};
pub use start_systemd_unit::{StartSystemdUnit, StartSystemdUnitError};

View file

@ -16,7 +16,7 @@ impl MoveUnpackedNix {
// Note: Do NOT try to check for the source/dest since the installer creates those
Ok(Self {
source,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -44,6 +44,11 @@ impl Actionable for MoveUnpackedNix {
source,
action_state,
} = self;
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Moving Nix");
return Ok(());
}
tracing::debug!("Moving Nix");
// TODO(@Hoverbear): I would like to make this less awful
let found_nix_paths =
@ -61,6 +66,7 @@ impl Actionable for MoveUnpackedNix {
.await
.map_err(|e| MoveUnpackedNixError::Rename(src, dest.to_owned(), e))?;
tracing::trace!("Moved Nix");
*action_state = ActionState::Completed;
Ok(())
}
@ -71,10 +77,12 @@ impl Actionable for MoveUnpackedNix {
source: _,
action_state,
} = self;
tracing::trace!("Nothing to do for `MoveUnpackedNix` revert");
*action_state = ActionState::Reverted;
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Unmove Nix (noop)");
return Ok(());
}
tracing::debug!("Unmove Nix (noop)");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -20,7 +20,7 @@ impl SetupDefaultProfile {
pub async fn plan(channels: Vec<String>) -> Result<Self, SetupDefaultProfileError> {
Ok(Self {
channels,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -132,7 +132,7 @@ impl Actionable for SetupDefaultProfile {
std::env::remove_var("NIX_SSL_CERT_FILE");
*action_state = ActionState::Reverted;
*action_state = ActionState::Completed;
Ok(())
}
}

View file

@ -16,7 +16,7 @@ impl StartSystemdUnit {
pub async fn plan(unit: String) -> Result<Self, StartSystemdUnitError> {
Ok(Self {
unit,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -26,7 +26,7 @@ impl Actionable for StartSystemdUnit {
type Error = StartSystemdUnitError;
fn description(&self) -> Vec<ActionDescription> {
match self.action_state {
ActionState::Planned => vec![
ActionState::Uncompleted => vec![
ActionDescription::new(
"Start the systemd Nix service and socket".to_string(),
vec![
@ -42,14 +42,6 @@ impl Actionable for StartSystemdUnit {
]
),
],
ActionState::Reverted => vec![
ActionDescription::new(
"Stopped the systemd Nix service and socket".to_string(),
vec![
"The `nix` command line tool communicates with a running Nix daemon managed by your init system".to_string()
]
),
],
}
}
@ -80,7 +72,7 @@ impl Actionable for StartSystemdUnit {
.await
.map_err(StartSystemdUnitError::Command)?;
*action_state = ActionState::Reverted;
*action_state = ActionState::Completed;
Ok(())
}
}

View file

@ -2,11 +2,14 @@ use serde::Serialize;
use crate::actions::{
base::{
ConfigureNixDaemonService, ConfigureNixDaemonServiceError, PlaceChannelConfiguration,
PlaceChannelConfigurationError, PlaceNixConfiguration, PlaceNixConfigurationError,
ConfigureNixDaemonService, ConfigureNixDaemonServiceError,
SetupDefaultProfile, SetupDefaultProfileError,
},
meta::{ConfigureShellProfile, ConfigureShellProfileError},
meta::{
ConfigureShellProfile, ConfigureShellProfileError,
PlaceChannelConfiguration, PlaceChannelConfigurationError,
PlaceNixConfiguration, PlaceNixConfigurationError,
},
Action, ActionState,
};
use crate::InstallSettings;
@ -55,7 +58,7 @@ impl ConfigureNix {
setup_default_profile,
configure_nix_daemon_service,
configure_shell_profile,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -94,6 +97,11 @@ impl Actionable for ConfigureNix {
configure_shell_profile,
action_state,
} = self;
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Configuring nix");
return Ok(());
}
tracing::debug!("Configuring nix");
if let Some(configure_shell_profile) = configure_shell_profile {
tokio::try_join!(
@ -146,6 +154,7 @@ impl Actionable for ConfigureNix {
};
configure_nix_daemon_service.execute().await?;
tracing::trace!("Configured nix");
*action_state = ActionState::Completed;
Ok(())
}
@ -160,6 +169,11 @@ impl Actionable for ConfigureNix {
configure_shell_profile,
action_state,
} = self;
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Unconfiguring nix");
return Ok(());
}
tracing::debug!("Unconfiguring nix");
configure_nix_daemon_service.revert().await?;
if let Some(configure_shell_profile) = configure_shell_profile {
@ -169,7 +183,8 @@ impl Actionable for ConfigureNix {
place_nix_configuration.revert().await?;
setup_default_profile.revert().await?;
*action_state = ActionState::Reverted;
tracing::trace!("Unconfigured nix");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -49,7 +49,7 @@ impl ConfigureShellProfile {
Ok(Self {
create_or_append_files,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -70,7 +70,11 @@ impl Actionable for ConfigureShellProfile {
create_or_append_files,
action_state,
} = self;
tracing::info!("Configuring shell profile");
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Configuring shell profile");
return Ok(());
}
tracing::debug!("Configuring shell profile");
let mut set = JoinSet::new();
let mut errors = Vec::default();
@ -103,6 +107,7 @@ impl Actionable for ConfigureShellProfile {
}
}
tracing::trace!("Configured shell profile");
*action_state = ActionState::Completed;
Ok(())
}
@ -113,7 +118,11 @@ impl Actionable for ConfigureShellProfile {
create_or_append_files,
action_state,
} = self;
tracing::info!("Configuring shell profile");
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Unconfiguring shell profile");
return Ok(());
}
tracing::debug!("Unconfiguring shell profile");
let mut set = JoinSet::new();
let mut errors = Vec::default();
@ -146,7 +155,8 @@ impl Actionable for ConfigureShellProfile {
}
}
*action_state = ActionState::Reverted;
tracing::trace!("Unconfigured shell profile");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -39,7 +39,7 @@ impl CreateNixTree {
Ok(Self {
create_directories,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -72,12 +72,18 @@ impl Actionable for CreateNixTree {
create_directories,
action_state,
} = self;
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Creating nix tree");
return Ok(());
}
tracing::debug!("Creating nix tree");
// Just do sequential since parallizing this will have little benefit
for create_directory in create_directories {
create_directory.execute().await?
}
tracing::trace!("Created nix tree");
*action_state = ActionState::Completed;
Ok(())
}
@ -88,13 +94,19 @@ impl Actionable for CreateNixTree {
create_directories,
action_state,
} = self;
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Deleting nix tree");
return Ok(());
}
tracing::debug!("Deleting nix tree");
// Just do sequential since parallizing this will have little benefit
for create_directory in create_directories.iter_mut().rev() {
create_directory.revert().await?
}
*action_state = ActionState::Reverted;
tracing::trace!("Deleted nix tree");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -44,7 +44,7 @@ impl CreateUsersAndGroup {
nix_build_user_id_base: settings.nix_build_user_id_base,
create_group,
create_users,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -88,6 +88,11 @@ impl Actionable for CreateUsersAndGroup {
nix_build_user_id_base: _,
action_state,
} = self;
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Creating users and groups");
return Ok(());
}
tracing::debug!("Creating users and groups");
// Create group
create_group.execute().await?;
@ -122,6 +127,7 @@ impl Actionable for CreateUsersAndGroup {
}
}
tracing::trace!("Created users and groups");
*action_state = ActionState::Completed;
Ok(())
}
@ -138,9 +144,12 @@ impl Actionable for CreateUsersAndGroup {
nix_build_user_id_base: _,
action_state,
} = self;
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Delete users and groups");
return Ok(());
}
tracing::debug!("Delete users and groups");
// Create users
// TODO(@hoverbear): Abstract this, it will be common
let mut set = JoinSet::new();
let mut errors = Vec::default();
@ -172,7 +181,8 @@ impl Actionable for CreateUsersAndGroup {
// Create group
create_group.revert().await?;
*action_state = ActionState::Reverted;
tracing::trace!("Deleted users and groups");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -4,6 +4,8 @@ mod configure_nix;
mod configure_shell_profile;
mod create_nix_tree;
mod create_users_and_group;
mod place_channel_configuration;
mod place_nix_configuration;
mod provision_nix;
mod start_nix_daemon;
@ -11,5 +13,7 @@ pub use configure_nix::{ConfigureNix, ConfigureNixError};
pub use configure_shell_profile::{ConfigureShellProfile, ConfigureShellProfileError};
pub use create_nix_tree::{CreateNixTree, CreateNixTreeError};
pub use create_users_and_group::{CreateUsersAndGroup, CreateUsersAndGroupError};
pub use place_channel_configuration::{PlaceChannelConfiguration, PlaceChannelConfigurationError};
pub use place_nix_configuration::{PlaceNixConfiguration, PlaceNixConfigurationError};
pub use provision_nix::{ProvisionNix, ProvisionNixError};
pub use start_nix_daemon::{StartNixDaemon, StartNixDaemonError};

View file

@ -3,7 +3,7 @@ use serde::Serialize;
use crate::actions::{Action, ActionDescription, ActionState, Actionable};
use super::{CreateFile, CreateFileError};
use crate::actions::base::{CreateFile, CreateFileError};
const NIX_CHANNELS_PATH: &str = "/root/.nix-channels";
@ -37,7 +37,7 @@ impl PlaceChannelConfiguration {
Ok(Self {
create_file,
channels,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -64,9 +64,15 @@ impl Actionable for PlaceChannelConfiguration {
channels: _,
action_state,
} = self;
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Placing channel configuration");
return Ok(());
}
tracing::debug!("Placing channel configuration");
create_file.execute().await?;
tracing::trace!("Placed channel configuration");
*action_state = ActionState::Completed;
Ok(())
}
@ -78,10 +84,16 @@ impl Actionable for PlaceChannelConfiguration {
channels: _,
action_state,
} = self;
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Removing channel configuration");
return Ok(());
}
tracing::debug!("Removing channel configuration");
create_file.revert().await?;
*action_state = ActionState::Reverted;
tracing::debug!("Removed channel configuration");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -2,7 +2,7 @@ use serde::Serialize;
use crate::actions::{Action, ActionDescription, ActionState, Actionable};
use super::{CreateDirectory, CreateDirectoryError, CreateFile, CreateFileError};
use crate::actions::base::{CreateDirectory, CreateDirectoryError, CreateFile, CreateFileError};
const NIX_CONF_FOLDER: &str = "/etc/nix";
const NIX_CONF: &str = "/etc/nix/nix.conf";
@ -40,7 +40,7 @@ impl PlaceNixConfiguration {
Ok(Self {
create_directory,
create_file,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -66,10 +66,16 @@ impl Actionable for PlaceNixConfiguration {
create_directory,
action_state,
} = self;
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Placing Nix configuration");
return Ok(());
}
tracing::debug!("Placing Nix configuration");
create_directory.execute().await?;
create_file.execute().await?;
tracing::trace!("Placed Nix configuration");
*action_state = ActionState::Completed;
Ok(())
}
@ -81,11 +87,17 @@ impl Actionable for PlaceNixConfiguration {
create_directory,
action_state,
} = self;
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Remove nix configuration");
return Ok(());
}
tracing::debug!("Remove nix configuration");
create_file.revert().await?;
create_directory.revert().await?;
*action_state = ActionState::Reverted;
tracing::trace!("Removed nix configuration");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -36,7 +36,7 @@ impl ProvisionNix {
create_users_and_group,
create_nix_tree,
move_unpacked_nix,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -70,6 +70,11 @@ impl Actionable for ProvisionNix {
move_unpacked_nix,
action_state,
} = self;
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Provisioning Nix");
return Ok(());
}
tracing::debug!("Provisioning Nix");
// We fetch nix while doing the rest, then move it over.
let mut fetch_nix_clone = fetch_nix.clone();
@ -87,6 +92,7 @@ impl Actionable for ProvisionNix {
*fetch_nix = fetch_nix_handle.await.map_err(ProvisionNixError::from)??;
move_unpacked_nix.execute().await?;
tracing::trace!("Provisioned Nix");
*action_state = ActionState::Completed;
Ok(())
}
@ -100,6 +106,11 @@ impl Actionable for ProvisionNix {
move_unpacked_nix,
action_state,
} = self;
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Unprovisioning nix");
return Ok(());
}
tracing::debug!("Unprovisioning nix");
// We fetch nix while doing the rest, then move it over.
let mut fetch_nix_clone = fetch_nix.clone();
@ -117,7 +128,8 @@ impl Actionable for ProvisionNix {
*fetch_nix = fetch_nix_handle.await.map_err(ProvisionNixError::from)??;
move_unpacked_nix.revert().await?;
*action_state = ActionState::Completed;
tracing::trace!("Unprovisioned Nix");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -17,7 +17,7 @@ impl StartNixDaemon {
let start_systemd_socket = StartSystemdUnit::plan("nix-daemon.socket".into()).await?;
Ok(Self {
start_systemd_socket,
action_state: ActionState::Planned,
action_state: ActionState::Uncompleted,
})
}
}
@ -36,9 +36,15 @@ impl Actionable for StartNixDaemon {
start_systemd_socket,
action_state,
} = self;
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Starting the nix daemon");
return Ok(());
}
tracing::debug!("Starting the nix daemon");
start_systemd_socket.execute().await?;
tracing::trace!("Started the nix daemon");
*action_state = ActionState::Completed;
Ok(())
}
@ -50,10 +56,16 @@ impl Actionable for StartNixDaemon {
action_state,
..
} = self;
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Stop the nix daemon");
return Ok(());
}
tracing::debug!("Stop the nix daemon");
start_systemd_socket.revert().await?;
*action_state = ActionState::Reverted;
tracing::trace!("Stopped the nix daemon");
*action_state = ActionState::Uncompleted;
Ok(())
}
}

View file

@ -5,13 +5,14 @@ use base::{
ConfigureNixDaemonService, ConfigureNixDaemonServiceError, CreateDirectory,
CreateDirectoryError, CreateFile, CreateFileError, CreateGroup, CreateGroupError,
CreateOrAppendFile, CreateOrAppendFileError, CreateUser, CreateUserError, FetchNix,
FetchNixError, MoveUnpackedNix, MoveUnpackedNixError, PlaceChannelConfiguration,
PlaceChannelConfigurationError, PlaceNixConfiguration, PlaceNixConfigurationError,
FetchNixError, MoveUnpackedNix, MoveUnpackedNixError,
SetupDefaultProfile, SetupDefaultProfileError,
};
use meta::{
ConfigureNix, ConfigureNixError, ConfigureShellProfile, ConfigureShellProfileError,
CreateNixTree, CreateNixTreeError, CreateUsersAndGroup, CreateUsersAndGroupError, ProvisionNix,
CreateNixTree, CreateNixTreeError, CreateUsersAndGroup, PlaceChannelConfiguration,
PlaceNixConfiguration, PlaceNixConfigurationError,
PlaceChannelConfigurationError, CreateUsersAndGroupError, ProvisionNix,
ProvisionNixError, StartNixDaemon, StartNixDaemonError,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
@ -29,11 +30,10 @@ pub trait Actionable: DeserializeOwned + Serialize + Into<Action> {
async fn revert(&mut self) -> Result<(), Self::Error>;
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub enum ActionState {
Completed,
Planned,
Reverted,
Uncompleted,
}
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]