Add more instrumentation
This commit is contained in:
parent
fddc0dd302
commit
4082260d5a
1 changed files with 20 additions and 1 deletions
21
src/lib.rs
21
src/lib.rs
|
@ -62,6 +62,7 @@ impl Harmonic {
|
||||||
impl Harmonic {
|
impl Harmonic {
|
||||||
#[tracing::instrument(skip_all)]
|
#[tracing::instrument(skip_all)]
|
||||||
pub async fn fetch_nix(&self) -> Result<(), HarmonicError> {
|
pub async fn fetch_nix(&self) -> Result<(), HarmonicError> {
|
||||||
|
tracing::debug!("Fetching nix");
|
||||||
// TODO(@hoverbear): architecture specific download
|
// TODO(@hoverbear): architecture specific download
|
||||||
// TODO(@hoverbear): hash check
|
// TODO(@hoverbear): hash check
|
||||||
// TODO(@hoverbear): custom url
|
// TODO(@hoverbear): custom url
|
||||||
|
@ -93,6 +94,7 @@ impl Harmonic {
|
||||||
|
|
||||||
#[tracing::instrument(skip_all)]
|
#[tracing::instrument(skip_all)]
|
||||||
pub async fn create_group(&self) -> Result<(), HarmonicError> {
|
pub async fn create_group(&self) -> Result<(), HarmonicError> {
|
||||||
|
tracing::debug!("Creating group");
|
||||||
execute_command(
|
execute_command(
|
||||||
Command::new("groupadd")
|
Command::new("groupadd")
|
||||||
.arg("-g")
|
.arg("-g")
|
||||||
|
@ -107,6 +109,7 @@ impl Harmonic {
|
||||||
|
|
||||||
#[tracing::instrument(skip_all)]
|
#[tracing::instrument(skip_all)]
|
||||||
pub async fn create_users(&self) -> Result<(), HarmonicError> {
|
pub async fn create_users(&self) -> Result<(), HarmonicError> {
|
||||||
|
tracing::debug!("Creating users");
|
||||||
for index in 1..=self.daemon_user_count {
|
for index in 1..=self.daemon_user_count {
|
||||||
let user_name = format!("{}{index}", self.nix_build_user_prefix);
|
let user_name = format!("{}{index}", self.nix_build_user_prefix);
|
||||||
let user_id = self.nix_build_user_id_base + index;
|
let user_id = self.nix_build_user_id_base + index;
|
||||||
|
@ -139,6 +142,7 @@ impl Harmonic {
|
||||||
|
|
||||||
#[tracing::instrument(skip_all)]
|
#[tracing::instrument(skip_all)]
|
||||||
pub async fn create_directories(&self) -> Result<(), HarmonicError> {
|
pub async fn create_directories(&self) -> Result<(), HarmonicError> {
|
||||||
|
tracing::debug!("Creating directories");
|
||||||
create_directory("/nix", self.dry_run).await?;
|
create_directory("/nix", self.dry_run).await?;
|
||||||
set_permissions(
|
set_permissions(
|
||||||
"/nix",
|
"/nix",
|
||||||
|
@ -194,6 +198,7 @@ impl Harmonic {
|
||||||
|
|
||||||
#[tracing::instrument(skip_all)]
|
#[tracing::instrument(skip_all)]
|
||||||
pub async fn place_channel_configuration(&self) -> Result<(), HarmonicError> {
|
pub async fn place_channel_configuration(&self) -> Result<(), HarmonicError> {
|
||||||
|
tracing::debug!("Placing channel configuration");
|
||||||
let buf = self
|
let buf = self
|
||||||
.channels
|
.channels
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -214,6 +219,7 @@ impl Harmonic {
|
||||||
|
|
||||||
#[tracing::instrument(skip_all)]
|
#[tracing::instrument(skip_all)]
|
||||||
pub async fn configure_shell_profile(&self) -> Result<(), HarmonicError> {
|
pub async fn configure_shell_profile(&self) -> Result<(), HarmonicError> {
|
||||||
|
tracing::debug!("Configuring shell profile");
|
||||||
const PROFILE_TARGETS: &[&str] = &[
|
const PROFILE_TARGETS: &[&str] = &[
|
||||||
"/etc/bashrc",
|
"/etc/bashrc",
|
||||||
"/etc/profile.d/nix.sh",
|
"/etc/profile.d/nix.sh",
|
||||||
|
@ -240,6 +246,7 @@ impl Harmonic {
|
||||||
|
|
||||||
#[tracing::instrument(skip_all)]
|
#[tracing::instrument(skip_all)]
|
||||||
pub async fn setup_default_profile(&self) -> Result<(), HarmonicError> {
|
pub async fn setup_default_profile(&self) -> Result<(), HarmonicError> {
|
||||||
|
tracing::debug!("Setting up default profile");
|
||||||
// Find an `nix` package
|
// Find an `nix` package
|
||||||
let nix_pkg_glob = "/nix/store/*-nix-*";
|
let nix_pkg_glob = "/nix/store/*-nix-*";
|
||||||
let found_nix_pkg = if !self.dry_run {
|
let found_nix_pkg = if !self.dry_run {
|
||||||
|
@ -328,6 +335,7 @@ impl Harmonic {
|
||||||
|
|
||||||
#[tracing::instrument(skip_all)]
|
#[tracing::instrument(skip_all)]
|
||||||
pub async fn place_nix_configuration(&self) -> Result<(), HarmonicError> {
|
pub async fn place_nix_configuration(&self) -> Result<(), HarmonicError> {
|
||||||
|
tracing::debug!("Placing nix configuration");
|
||||||
let buf = format!(
|
let buf = format!(
|
||||||
"\
|
"\
|
||||||
{extra_conf}\n\
|
{extra_conf}\n\
|
||||||
|
@ -341,6 +349,7 @@ impl Harmonic {
|
||||||
|
|
||||||
#[tracing::instrument(skip_all)]
|
#[tracing::instrument(skip_all)]
|
||||||
pub async fn configure_nix_daemon_service(&self) -> Result<(), HarmonicError> {
|
pub async fn configure_nix_daemon_service(&self) -> Result<(), HarmonicError> {
|
||||||
|
tracing::debug!("Configuring nix daemon service");
|
||||||
if Path::new("/run/systemd/system").exists() {
|
if Path::new("/run/systemd/system").exists() {
|
||||||
const SERVICE_SRC: &str =
|
const SERVICE_SRC: &str =
|
||||||
"/nix/var/nix/profiles/default/lib/systemd/system/nix-daemon.service";
|
"/nix/var/nix/profiles/default/lib/systemd/system/nix-daemon.service";
|
||||||
|
@ -429,6 +438,7 @@ async fn set_permissions(
|
||||||
use nix::unistd::{chown, Group, User};
|
use nix::unistd::{chown, Group, User};
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
if !dry_run {
|
if !dry_run {
|
||||||
|
tracing::trace!("Setting permissions");
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
let uid = if let Some(owner) = owner {
|
let uid = if let Some(owner) = owner {
|
||||||
let uid = User::from_name(owner.as_str())
|
let uid = User::from_name(owner.as_str())
|
||||||
|
@ -471,6 +481,7 @@ async fn set_permissions(
|
||||||
async fn create_directory(path: impl AsRef<Path>, dry_run: bool) -> Result<(), HarmonicError> {
|
async fn create_directory(path: impl AsRef<Path>, dry_run: bool) -> Result<(), HarmonicError> {
|
||||||
use tokio::fs::create_dir;
|
use tokio::fs::create_dir;
|
||||||
if !dry_run {
|
if !dry_run {
|
||||||
|
tracing::trace!("Creating directory");
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
create_dir(path)
|
create_dir(path)
|
||||||
.await
|
.await
|
||||||
|
@ -488,6 +499,7 @@ async fn execute_command(
|
||||||
dry_run: bool,
|
dry_run: bool,
|
||||||
) -> Result<ExitStatus, HarmonicError> {
|
) -> Result<ExitStatus, HarmonicError> {
|
||||||
if !dry_run {
|
if !dry_run {
|
||||||
|
tracing::trace!("Executing");
|
||||||
let command_str = format!("{:?}", command.as_std());
|
let command_str = format!("{:?}", command.as_std());
|
||||||
let status = command
|
let status = command
|
||||||
.status()
|
.status()
|
||||||
|
@ -521,6 +533,7 @@ async fn create_or_append_file(
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
let buf = buf.as_ref();
|
let buf = buf.as_ref();
|
||||||
if !dry_run {
|
if !dry_run {
|
||||||
|
tracing::trace!("Creating or appending");
|
||||||
if let Some(parent) = path.parent() {
|
if let Some(parent) = path.parent() {
|
||||||
create_dir_all(parent)
|
create_dir_all(parent)
|
||||||
.await
|
.await
|
||||||
|
@ -548,7 +561,7 @@ async fn create_or_append_file(
|
||||||
|
|
||||||
#[tracing::instrument(skip_all, fields(
|
#[tracing::instrument(skip_all, fields(
|
||||||
path = %path.as_ref().display(),
|
path = %path.as_ref().display(),
|
||||||
buf = %format!("`{}`", buf.as_ref()),
|
buf = %format!("```{}```", buf.as_ref()),
|
||||||
))]
|
))]
|
||||||
async fn create_file_if_not_exists(
|
async fn create_file_if_not_exists(
|
||||||
path: impl AsRef<Path>,
|
path: impl AsRef<Path>,
|
||||||
|
@ -559,6 +572,7 @@ async fn create_file_if_not_exists(
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
let buf = buf.as_ref();
|
let buf = buf.as_ref();
|
||||||
if !dry_run {
|
if !dry_run {
|
||||||
|
tracing::trace!("Creating if not exists");
|
||||||
if let Some(parent) = path.parent() {
|
if let Some(parent) = path.parent() {
|
||||||
create_dir_all(parent)
|
create_dir_all(parent)
|
||||||
.await
|
.await
|
||||||
|
@ -593,6 +607,7 @@ async fn symlink(
|
||||||
let src = src.as_ref();
|
let src = src.as_ref();
|
||||||
let dest = dest.as_ref();
|
let dest = dest.as_ref();
|
||||||
if !dry_run {
|
if !dry_run {
|
||||||
|
tracing::trace!("Symlinking");
|
||||||
tokio::fs::symlink(src, dest)
|
tokio::fs::symlink(src, dest)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| HarmonicError::Symlink(src.to_owned(), dest.to_owned(), e))?;
|
.map_err(|e| HarmonicError::Symlink(src.to_owned(), dest.to_owned(), e))?;
|
||||||
|
@ -614,6 +629,7 @@ async fn rename(
|
||||||
let src = src.as_ref();
|
let src = src.as_ref();
|
||||||
let dest = dest.as_ref();
|
let dest = dest.as_ref();
|
||||||
if !dry_run {
|
if !dry_run {
|
||||||
|
tracing::trace!("Renaming");
|
||||||
tokio::fs::rename(src, dest)
|
tokio::fs::rename(src, dest)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| HarmonicError::Rename(src.to_owned(), dest.to_owned(), e))?;
|
.map_err(|e| HarmonicError::Rename(src.to_owned(), dest.to_owned(), e))?;
|
||||||
|
@ -635,9 +651,11 @@ async fn fetch_url_and_unpack_xz(
|
||||||
let url = url.as_ref();
|
let url = url.as_ref();
|
||||||
let dest = dest.as_ref().to_owned();
|
let dest = dest.as_ref().to_owned();
|
||||||
if !dry_run {
|
if !dry_run {
|
||||||
|
tracing::trace!("Fetching url");
|
||||||
let res = reqwest::get(url).await.map_err(HarmonicError::Reqwest)?;
|
let res = reqwest::get(url).await.map_err(HarmonicError::Reqwest)?;
|
||||||
let bytes = res.bytes().await.map_err(HarmonicError::Reqwest)?;
|
let bytes = res.bytes().await.map_err(HarmonicError::Reqwest)?;
|
||||||
// TODO(@Hoverbear): Pick directory
|
// TODO(@Hoverbear): Pick directory
|
||||||
|
tracing::trace!("Unpacking tar.xz");
|
||||||
let handle: Result<(), HarmonicError> = spawn_blocking(move || {
|
let handle: Result<(), HarmonicError> = spawn_blocking(move || {
|
||||||
let decoder = xz2::read::XzDecoder::new(bytes.reader());
|
let decoder = xz2::read::XzDecoder::new(bytes.reader());
|
||||||
let mut archive = tar::Archive::new(decoder);
|
let mut archive = tar::Archive::new(decoder);
|
||||||
|
@ -661,6 +679,7 @@ async fn fetch_url_and_unpack_xz(
|
||||||
))]
|
))]
|
||||||
fn set_env(k: impl AsRef<OsStr>, v: impl AsRef<OsStr>, dry_run: bool) {
|
fn set_env(k: impl AsRef<OsStr>, v: impl AsRef<OsStr>, dry_run: bool) {
|
||||||
if !dry_run {
|
if !dry_run {
|
||||||
|
tracing::trace!("Setting env");
|
||||||
std::env::set_var(k.as_ref(), v.as_ref());
|
std::env::set_var(k.as_ref(), v.as_ref());
|
||||||
} else {
|
} else {
|
||||||
tracing::info!("Dry run: Would set env");
|
tracing::info!("Dry run: Would set env");
|
||||||
|
|
Loading…
Reference in a new issue