Tidy more logs

This commit is contained in:
Ana Hobden 2022-09-27 09:29:37 -07:00
parent 257d82b3b4
commit 47018ad00c
8 changed files with 118 additions and 37 deletions

View file

@ -66,7 +66,12 @@ impl Actionable for CreateOrAppendFile {
)]
}
#[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,
@ -118,7 +123,12 @@ impl Actionable for CreateOrAppendFile {
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,

View file

@ -39,7 +39,11 @@ impl Actionable for CreateUser {
)]
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
user = self.name,
uid = self.uid,
gid = self.gid,
))]
async fn execute(&mut self) -> Result<(), Self::Error> {
let Self {
name,
@ -80,7 +84,11 @@ impl Actionable for CreateUser {
Ok(())
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
user = self.name,
uid = self.uid,
gid = self.gid,
))]
async fn revert(&mut self) -> Result<(), Self::Error> {
let Self {
name,

View file

@ -10,19 +10,19 @@ use crate::actions::{Action, ActionDescription, ActionState, Actionable};
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
pub struct FetchNix {
url: Url,
destination: PathBuf,
dest: PathBuf,
action_state: ActionState,
}
impl FetchNix {
#[tracing::instrument(skip_all)]
pub async fn plan(url: Url, destination: PathBuf) -> Result<Self, FetchNixError> {
pub async fn plan(url: Url, dest: PathBuf) -> Result<Self, FetchNixError> {
// TODO(@hoverbear): Check URL exists?
// TODO(@hoverbear): Check tempdir exists
Ok(Self {
url,
destination,
dest,
action_state: ActionState::Uncompleted,
})
}
@ -34,23 +34,26 @@ impl Actionable for FetchNix {
fn description(&self) -> Vec<ActionDescription> {
let Self {
url,
destination,
dest,
action_state: _,
} = &self;
vec![ActionDescription::new(
format!("Fetch Nix from `{url}`"),
vec![format!(
"Unpack it to `{}` (moved later)",
destination.display()
dest.display()
)],
)]
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
url = %self.url,
dest = %self.dest.display(),
))]
async fn execute(&mut self) -> Result<(), Self::Error> {
let Self {
url,
destination,
dest,
action_state,
} = self;
if *action_state == ActionState::Completed {
@ -65,12 +68,12 @@ impl Actionable for FetchNix {
let bytes = res.bytes().await.map_err(Self::Error::Reqwest)?;
// TODO(@Hoverbear): Pick directory
tracing::trace!("Unpacking tar.xz");
let destination_clone = destination.clone();
let dest_clone = dest.clone();
let handle: Result<(), Self::Error> = spawn_blocking(move || {
let decoder = xz2::read::XzDecoder::new(bytes.reader());
let mut archive = tar::Archive::new(decoder);
archive.unpack(&destination_clone).map_err(Self::Error::Unarchive)?;
tracing::debug!(destination = %destination_clone.display(), "Downloaded & extracted Nix");
archive.unpack(&dest_clone).map_err(Self::Error::Unarchive)?;
tracing::debug!(dest = %dest_clone.display(), "Downloaded & extracted Nix");
Ok(())
})
.await?;
@ -82,11 +85,14 @@ impl Actionable for FetchNix {
Ok(())
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
url = %self.url,
dest = %self.dest.display(),
))]
async fn revert(&mut self) -> Result<(), Self::Error> {
let Self {
url: _,
destination: _,
dest: _,
action_state,
} = self;

View file

@ -4,18 +4,20 @@ use serde::Serialize;
use crate::actions::{Action, ActionDescription, ActionState, Actionable};
const DEST: &str = "/nix/store";
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
pub struct MoveUnpackedNix {
source: PathBuf,
src: PathBuf,
action_state: ActionState,
}
impl MoveUnpackedNix {
#[tracing::instrument(skip_all)]
pub async fn plan(source: PathBuf) -> Result<Self, MoveUnpackedNixError> {
// Note: Do NOT try to check for the source/dest since the installer creates those
pub async fn plan(src: PathBuf) -> Result<Self, MoveUnpackedNixError> {
// Note: Do NOT try to check for the src/dest since the installer creates those
Ok(Self {
source,
src,
action_state: ActionState::Uncompleted,
})
}
@ -26,22 +28,25 @@ impl Actionable for MoveUnpackedNix {
type Error = MoveUnpackedNixError;
fn description(&self) -> Vec<ActionDescription> {
let Self {
source,
src,
action_state: _,
} = &self;
vec![ActionDescription::new(
format!("Move the downloaded Nix into `/nix`"),
vec![format!(
"Nix is being downloaded to `{}` and should be in `nix`",
source.display(),
src.display(),
)],
)]
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
src = %self.src.display(),
dest = DEST,
))]
async fn execute(&mut self) -> Result<(), Self::Error> {
let Self {
source,
src,
action_state,
} = self;
if *action_state == ActionState::Completed {
@ -52,7 +57,7 @@ impl Actionable for MoveUnpackedNix {
// TODO(@Hoverbear): I would like to make this less awful
let found_nix_paths =
glob::glob(&format!("{}/nix-*", source.display()))?.collect::<Result<Vec<_>, _>>()?;
glob::glob(&format!("{}/nix-*", src.display()))?.collect::<Result<Vec<_>, _>>()?;
assert_eq!(
found_nix_paths.len(),
1,
@ -61,7 +66,7 @@ impl Actionable for MoveUnpackedNix {
let found_nix_path = found_nix_paths.into_iter().next().unwrap();
tracing::trace!("Renaming");
let src = found_nix_path.join("store");
let dest = Path::new("/nix/store");
let dest = Path::new(DEST);
tokio::fs::rename(src.clone(), dest)
.await
.map_err(|e| MoveUnpackedNixError::Rename(src, dest.to_owned(), e))?;
@ -71,10 +76,13 @@ impl Actionable for MoveUnpackedNix {
Ok(())
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
src = %self.src.display(),
dest = DEST,
))]
async fn revert(&mut self) -> Result<(), Self::Error> {
let Self {
source: _,
src: _,
action_state,
} = self;
if *action_state == ActionState::Uncompleted {

View file

@ -35,13 +35,19 @@ impl Actionable for SetupDefaultProfile {
)]
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
channels = %self.channels.join(","),
))]
async fn execute(&mut self) -> Result<(), Self::Error> {
let Self {
channels,
action_state,
} = self;
tracing::info!("Setting up default profile");
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Setting up default profile");
return Ok(());
}
tracing::debug!("Setting up default profile");
// Find an `nix` package
let nix_pkg_glob = "/nix/store/*-nix-*";
@ -119,19 +125,29 @@ impl Actionable for SetupDefaultProfile {
.await
.map_err(SetupDefaultProfileError::Command)?;
}
tracing::trace!("Set up default profile");
*action_state = ActionState::Completed;
Ok(())
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
channels = %self.channels.join(","),
))]
async fn revert(&mut self) -> Result<(), Self::Error> {
let Self {
channels: _,
action_state,
} = self;
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Unset default profile");
return Ok(());
}
tracing::debug!("Unsetting default profile (mostly noop)");
std::env::remove_var("NIX_SSL_CERT_FILE");
tracing::trace!("Unset default profile (mostly noop)");
*action_state = ActionState::Completed;
Ok(())
}

View file

@ -45,9 +45,16 @@ impl Actionable for StartSystemdUnit {
}
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
unit = %self.unit,
))]
async fn execute(&mut self) -> Result<(), Self::Error> {
let Self { unit, action_state } = self;
if *action_state == ActionState::Completed {
tracing::trace!("Already completed: Starting systemd unit");
return Ok(());
}
tracing::debug!("Starting systemd unit");
// TODO(@Hoverbear): Handle proxy vars
execute_command(
@ -59,19 +66,28 @@ impl Actionable for StartSystemdUnit {
.await
.map_err(StartSystemdUnitError::Command)?;
tracing::trace!("Started systemd unit");
*action_state = ActionState::Completed;
Ok(())
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
unit = %self.unit,
))]
async fn revert(&mut self) -> Result<(), Self::Error> {
let Self { unit, action_state } = self;
if *action_state == ActionState::Uncompleted {
tracing::trace!("Already reverted: Stopping systemd unit");
return Ok(());
}
tracing::debug!("Stopping systemd unit");
// TODO(@Hoverbear): Handle proxy vars
execute_command(Command::new("systemctl").arg("stop").arg(format!("{unit}")))
.await
.map_err(StartSystemdUnitError::Command)?;
tracing::trace!("Stopped systemd unit");
*action_state = ActionState::Completed;
Ok(())
}

View file

@ -76,7 +76,13 @@ impl Actionable for CreateUsersAndGroup {
]
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
daemon_user_count = self.daemon_user_count,
nix_build_group_name = self.nix_build_group_name,
nix_build_group_id = self.nix_build_group_id,
nix_build_user_prefix = self.nix_build_user_prefix,
nix_build_user_id_base = self.nix_build_user_id_base,
))]
async fn execute(&mut self) -> Result<(), Self::Error> {
let Self {
create_users,
@ -132,7 +138,14 @@ impl Actionable for CreateUsersAndGroup {
Ok(())
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
daemon_user_count = self.daemon_user_count,
nix_build_group_name = self.nix_build_group_name,
nix_build_group_id = self.nix_build_group_id,
nix_build_user_prefix = self.nix_build_user_prefix,
nix_build_user_id_base = self.nix_build_user_id_base,
))]
async fn revert(&mut self) -> Result<(), Self::Error> {
let Self {
create_users,

View file

@ -57,7 +57,9 @@ impl Actionable for PlaceChannelConfiguration {
)]
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
channels = self.channels.iter().map(|(c, u)| format!("{c}={u}")).collect::<Vec<_>>().join(", "),
))]
async fn execute(&mut self) -> Result<(), Self::Error> {
let Self {
create_file,
@ -77,7 +79,9 @@ impl Actionable for PlaceChannelConfiguration {
Ok(())
}
#[tracing::instrument(skip_all)]
#[tracing::instrument(skip_all, fields(
channels = self.channels.iter().map(|(c, u)| format!("{c}={u}")).collect::<Vec<_>>().join(", "),
))]
async fn revert(&mut self) -> Result<(), Self::Error> {
let Self {
create_file,