Add indicator for scripts if they should be run in a different root

chrooting
trivernis 2 years ago
parent 923549e843
commit 9a5a4bca81
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

736
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -16,17 +16,17 @@ path = "src/main.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
clap = { version = "4.0.17", features = ["derive"] } clap = { version = "4.0.24", features = ["derive"] }
color-eyre = "0.6.2" color-eyre = "0.6.2"
dotenv = "0.15.0" dotenv = "0.15.0"
embed-nu = "0.3.3" embed-nu = "0.3.5"
lazy_static = "1.4.0" lazy_static = "1.4.0"
libc = "0.2.135" libc = "0.2.137"
paste = "1.0.9" paste = "1.0.9"
rusty-value = { version = "0.6.0", features = ["derive", "json"] } rusty-value = { version = "0.6.0", features = ["derive", "json"] }
serde = { version = "1.0.145", features = ["derive"] } serde = { version = "1.0.147", features = ["derive"] }
serde_json = "1.0.86" serde_json = "1.0.87"
sys-mount = "1.5.1" sys-mount = "2.0.1"
thiserror = "1.0.37" thiserror = "1.0.37"
tokio = { version = "1.21.2", features = ["rt", "io-std", "io-util", "process", "time", "macros", "tracing", "fs"] } tokio = { version = "1.21.2", features = ["rt", "io-std", "io-util", "process", "time", "macros", "tracing", "fs"] }
toml = "0.5.9" toml = "0.5.9"
@ -36,4 +36,4 @@ valico = "3.6.1"
[build-dependencies] [build-dependencies]
cargo_toml = "0.13.0" cargo_toml = "0.13.0"
serde = { version = "1.0.145", features = ["derive"] } serde = { version = "1.0.147", features = ["derive"] }

@ -7,10 +7,8 @@ use crate::{distro::OSConfig, error::ScriptError, utils::CFG_PATH};
#[derive(Clone)] #[derive(Clone)]
pub struct ExecBuilder { pub struct ExecBuilder {
script: PathBuf,
script_contents: String,
os_config: OSConfig,
task_config: embed_nu::Value, task_config: embed_nu::Value,
ctx: Context,
} }
impl ExecBuilder { impl ExecBuilder {
@ -21,36 +19,38 @@ impl ExecBuilder {
) -> Result<Self, ScriptError> { ) -> Result<Self, ScriptError> {
let script_contents = Self::get_script_contents(&script)?; let script_contents = Self::get_script_contents(&script)?;
Ok(Self {
script,
script_contents,
os_config,
task_config,
})
}
#[tracing::instrument(level = "trace", skip_all)]
pub fn exec(self) -> Result<(), ScriptError> {
let mut ctx = Context::builder() let mut ctx = Context::builder()
.with_command_groups(CommandGroupConfig::default().all_groups(true))? .with_command_groups(CommandGroupConfig::default().all_groups(true))?
.add_parent_env_vars() .add_parent_env_vars()
.add_var("TRM_CONFIG", self.os_config)? .add_var("TRM_CONFIG", os_config)?
.add_script(self.script_contents)? .add_script(script_contents)?
.build()?; .build()?;
if !ctx.has_fn("main") {
if ctx.has_fn("main") { Err(ScriptError::MissingMain(script))
let pipeline = ctx.call_fn(
"main",
vec![Argument::Positional(self.task_config.into_expression())],
)?;
ctx.print_pipeline_stderr(pipeline)?;
Ok(())
} else { } else {
Err(ScriptError::MissingMain(self.script)) Ok(Self { ctx, task_config })
} }
} }
#[tracing::instrument(level = "trace", skip_all)]
pub fn exec(mut self) -> Result<(), ScriptError> {
let pipeline = self.ctx.call_fn(
"main",
vec![Argument::Positional(self.task_config.into_expression())],
)?;
self.ctx.print_pipeline_stderr(pipeline)?;
Ok(())
}
/// Returns if the script needs to be run inside the new root
pub fn requires_chroot(&self) -> bool {
self.ctx
.get_var("run_in_chroot")
.and_then(|v| v.as_bool().ok())
.unwrap_or(false)
}
#[tracing::instrument(level = "trace", skip_all)] #[tracing::instrument(level = "trace", skip_all)]
fn get_script_contents(path: &Path) -> Result<String, ScriptError> { fn get_script_contents(path: &Path) -> Result<String, ScriptError> {
let path = CFG_PATH.join(path); let path = CFG_PATH.join(path);

@ -1,5 +1,3 @@
use std::process::Command;
use crate::{ use crate::{
distro::{distro_config::DistroConfig, OSConfig}, distro::{distro_config::DistroConfig, OSConfig},
error::AppResult, error::AppResult,
@ -66,22 +64,14 @@ impl TaskExecutor {
pub async fn execute(&mut self) -> AppResult<()> { pub async fn execute(&mut self) -> AppResult<()> {
self.tasks.sort_by(Task::compare); self.tasks.sort_by(Task::compare);
let chroot = Chroot::create(&*ROOT_MNT).await?; let chroot = Chroot::create(&*ROOT_MNT).await?;
chroot
.run(move || {
Command::new("pacman")
.args(["-S", "--noconfirm", "neofetch"])
.spawn()
.unwrap()
.wait()
.unwrap();
Command::new("neofetch").spawn().unwrap().wait().unwrap();
})
.await
.unwrap()?;
for task in &self.tasks { for task in &self.tasks {
if let Some(up_task) = task.up(&self.os_config)? { if let Some(up_task) = task.up(&self.os_config)? {
up_task.exec()?; if up_task.requires_chroot() {
chroot.run(|| up_task.exec()).await.unwrap()??;
} else {
up_task.exec()?;
}
} }
} }

Loading…
Cancel
Save