From 257d82b3b4cfedaa85684e41914fc4be03c13417 Mon Sep 17 00:00:00 2001 From: Ana Hobden Date: Mon, 26 Sep 2022 16:14:25 -0700 Subject: [PATCH] Cleanup logging --- .../base/configure_nix_daemon_service.rs | 18 +++++++--- src/actions/base/create_directory.rs | 32 +++++++++++++---- src/actions/base/create_file.rs | 35 ++++++++++++++----- src/actions/base/create_group.rs | 26 +++++++++++--- src/actions/base/create_or_append_file.rs | 24 +++++++++---- src/actions/base/create_user.rs | 16 +++++++-- src/actions/base/fetch_nix.rs | 18 +++++++--- src/actions/base/mod.rs | 4 --- src/actions/base/move_unpacked_nix.rs | 18 +++++++--- src/actions/base/setup_default_profile.rs | 4 +-- src/actions/base/start_systemd_unit.rs | 14 ++------ src/actions/meta/configure_nix.rs | 25 ++++++++++--- src/actions/meta/configure_shell_profile.rs | 18 +++++++--- src/actions/meta/create_nix_tree.rs | 16 +++++++-- src/actions/meta/create_users_and_group.rs | 18 +++++++--- src/actions/meta/mod.rs | 4 +++ .../place_channel_configuration.rs | 20 ++++++++--- .../{base => meta}/place_nix_configuration.rs | 18 ++++++++-- src/actions/meta/provision_nix.rs | 16 +++++++-- src/actions/meta/start_nix_daemon.rs | 16 +++++++-- src/actions/mod.rs | 12 +++---- 21 files changed, 282 insertions(+), 90 deletions(-) rename src/actions/{base => meta}/place_channel_configuration.rs (76%) rename src/actions/{base => meta}/place_nix_configuration.rs (79%) diff --git a/src/actions/base/configure_nix_daemon_service.rs b/src/actions/base/configure_nix_daemon_service.rs index 249a1b3..623c601 100644 --- a/src/actions/base/configure_nix_daemon_service.rs +++ b/src/actions/base/configure_nix_daemon_service.rs @@ -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(()) } } diff --git a/src/actions/base/create_directory.rs b/src/actions/base/create_directory.rs index 27671eb..748c0c0 100644 --- a/src/actions/base/create_directory.rs +++ b/src/actions/base/create_directory.rs @@ -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(()) } } diff --git a/src/actions/base/create_file.rs b/src/actions/base/create_file.rs index dc548a1..883cdfa 100644 --- a/src/actions/base/create_file.rs +++ b/src/actions/base/create_file.rs @@ -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(()) } } diff --git a/src/actions/base/create_group.rs b/src/actions/base/create_group.rs index 6f60c7e..bcbf13c 100644 --- a/src/actions/base/create_group.rs +++ b/src/actions/base/create_group.rs @@ -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(()) } } diff --git a/src/actions/base/create_or_append_file.rs b/src/actions/base/create_or_append_file.rs index 3ebc751..c133e91 100644 --- a/src/actions/base/create_or_append_file.rs +++ b/src/actions/base/create_or_append_file.rs @@ -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(()) } } diff --git a/src/actions/base/create_user.rs b/src/actions/base/create_user.rs index a4b5ac5..e20cbc0 100644 --- a/src/actions/base/create_user.rs +++ b/src/actions/base/create_user.rs @@ -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(()) } } diff --git a/src/actions/base/fetch_nix.rs b/src/actions/base/fetch_nix.rs index c8a5e29..e915476 100644 --- a/src/actions/base/fetch_nix.rs +++ b/src/actions/base/fetch_nix.rs @@ -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(()) } } diff --git a/src/actions/base/mod.rs b/src/actions/base/mod.rs index 0a9c00d..123bc43 100644 --- a/src/actions/base/mod.rs +++ b/src/actions/base/mod.rs @@ -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}; diff --git a/src/actions/base/move_unpacked_nix.rs b/src/actions/base/move_unpacked_nix.rs index 9ddd148..f923810 100644 --- a/src/actions/base/move_unpacked_nix.rs +++ b/src/actions/base/move_unpacked_nix.rs @@ -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(()) } } diff --git a/src/actions/base/setup_default_profile.rs b/src/actions/base/setup_default_profile.rs index c8ef4fb..63573f7 100644 --- a/src/actions/base/setup_default_profile.rs +++ b/src/actions/base/setup_default_profile.rs @@ -20,7 +20,7 @@ impl SetupDefaultProfile { pub async fn plan(channels: Vec) -> Result { 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(()) } } diff --git a/src/actions/base/start_systemd_unit.rs b/src/actions/base/start_systemd_unit.rs index 9532d96..ad43c01 100644 --- a/src/actions/base/start_systemd_unit.rs +++ b/src/actions/base/start_systemd_unit.rs @@ -16,7 +16,7 @@ impl StartSystemdUnit { pub async fn plan(unit: String) -> Result { 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 { 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(()) } } diff --git a/src/actions/meta/configure_nix.rs b/src/actions/meta/configure_nix.rs index bcc7175..d4305ab 100644 --- a/src/actions/meta/configure_nix.rs +++ b/src/actions/meta/configure_nix.rs @@ -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(()) } } diff --git a/src/actions/meta/configure_shell_profile.rs b/src/actions/meta/configure_shell_profile.rs index a3152df..340b2fd 100644 --- a/src/actions/meta/configure_shell_profile.rs +++ b/src/actions/meta/configure_shell_profile.rs @@ -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(()) } } diff --git a/src/actions/meta/create_nix_tree.rs b/src/actions/meta/create_nix_tree.rs index 3d083ea..0c3919c 100644 --- a/src/actions/meta/create_nix_tree.rs +++ b/src/actions/meta/create_nix_tree.rs @@ -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(()) } } diff --git a/src/actions/meta/create_users_and_group.rs b/src/actions/meta/create_users_and_group.rs index 782b659..7cc22c0 100644 --- a/src/actions/meta/create_users_and_group.rs +++ b/src/actions/meta/create_users_and_group.rs @@ -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(()) } } diff --git a/src/actions/meta/mod.rs b/src/actions/meta/mod.rs index 75d1a76..3d64346 100644 --- a/src/actions/meta/mod.rs +++ b/src/actions/meta/mod.rs @@ -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}; diff --git a/src/actions/base/place_channel_configuration.rs b/src/actions/meta/place_channel_configuration.rs similarity index 76% rename from src/actions/base/place_channel_configuration.rs rename to src/actions/meta/place_channel_configuration.rs index cb988c6..095ed83 100644 --- a/src/actions/base/place_channel_configuration.rs +++ b/src/actions/meta/place_channel_configuration.rs @@ -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(()) } } diff --git a/src/actions/base/place_nix_configuration.rs b/src/actions/meta/place_nix_configuration.rs similarity index 79% rename from src/actions/base/place_nix_configuration.rs rename to src/actions/meta/place_nix_configuration.rs index 47576ac..6550cd2 100644 --- a/src/actions/base/place_nix_configuration.rs +++ b/src/actions/meta/place_nix_configuration.rs @@ -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(()) } } diff --git a/src/actions/meta/provision_nix.rs b/src/actions/meta/provision_nix.rs index 6480d65..8cf9cb1 100644 --- a/src/actions/meta/provision_nix.rs +++ b/src/actions/meta/provision_nix.rs @@ -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(()) } } diff --git a/src/actions/meta/start_nix_daemon.rs b/src/actions/meta/start_nix_daemon.rs index b9460d0..0f7886c 100644 --- a/src/actions/meta/start_nix_daemon.rs +++ b/src/actions/meta/start_nix_daemon.rs @@ -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(()) } } diff --git a/src/actions/mod.rs b/src/actions/mod.rs index d9e3c15..00bb2b0 100644 --- a/src/actions/mod.rs +++ b/src/actions/mod.rs @@ -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 { 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)]