Check if running as root during install and uninstall

This commit is contained in:
Ana Hobden 2022-11-09 09:56:28 -08:00
parent b123bbf285
commit 9c0dd087c3
4 changed files with 21 additions and 5 deletions

View file

@ -40,3 +40,7 @@ impl CommandExecute for HarmonicCli {
}
}
}
pub fn is_root() -> bool {
nix::unistd::getuid() == nix::unistd::Uid::from_raw(0)
}

View file

@ -1,6 +1,6 @@
use std::{path::PathBuf, process::ExitCode};
use crate::BuiltinPlanner;
use crate::{cli::is_root, BuiltinPlanner};
use clap::{ArgAction, Parser};
use eyre::{eyre, WrapErr};
@ -43,6 +43,12 @@ impl CommandExecute for Install {
explain,
} = self;
if !is_root() {
return Err(eyre!(
"`harmonic install` must be run as `root`, try `sudo harmonic install`"
));
}
let mut plan = match &plan {
Some(plan_path) => {
let install_plan_string = tokio::fs::read_to_string(&plan_path)

View file

@ -9,11 +9,11 @@ use crate::cli::CommandExecute;
/// Plan an install that can be repeated on an identical host later
#[derive(Debug, Parser)]
#[command(args_conflicts_with_subcommands = true, arg_required_else_help = true)]
#[command(arg_required_else_help = true)]
pub struct Plan {
#[clap(subcommand)]
pub planner: Option<BuiltinPlanner>,
#[clap(env = "HARMONIC_PLAN")]
#[clap(env = "HARMONIC_PLAN", default_value = "/dev/stdout")]
pub output: PathBuf,
}

View file

@ -1,8 +1,8 @@
use std::{path::PathBuf, process::ExitCode};
use crate::InstallPlan;
use crate::{cli::is_root, InstallPlan};
use clap::{ArgAction, Parser};
use eyre::WrapErr;
use eyre::{eyre, WrapErr};
use crate::{cli::CommandExecute, interaction};
@ -37,6 +37,12 @@ impl CommandExecute for Uninstall {
explain,
} = self;
if !is_root() {
return Err(eyre!(
"`harmonic install` must be run as `root`, try `sudo harmonic install`"
));
}
let install_receipt_string = tokio::fs::read_to_string(receipt)
.await
.wrap_err("Reading receipt")?;