From e7e8e7daf8b1d2fea2cb38ad52d652fa0825df80 Mon Sep 17 00:00:00 2001 From: kristof Date: Sun, 15 Jun 2025 22:40:28 +0200 Subject: [PATCH] ... --- examples/containers/nerdctl_install.rhai | 45 ---------------------- installers/base.rhai | 4 +- installers/nerdctl.rhai | 4 +- src/process/mod.rs | 4 +- src/process/screen.rs | 48 ++++++++++++++++++++++++ src/rhai/mod.rs | 4 ++ src/rhai/process.rs | 2 +- src/rhai/screen.rs | 26 +++++++++++++ 8 files changed, 86 insertions(+), 51 deletions(-) delete mode 100644 examples/containers/nerdctl_install.rhai create mode 100644 src/process/screen.rs create mode 100644 src/rhai/screen.rs diff --git a/examples/containers/nerdctl_install.rhai b/examples/containers/nerdctl_install.rhai deleted file mode 100644 index 55c9bd1..0000000 --- a/examples/containers/nerdctl_install.rhai +++ /dev/null @@ -1,45 +0,0 @@ - - - -fn nerdctl_download(){ - let name="nerdctl"; - let url="https://github.com/containerd/nerdctl/releases/download/v2.1.2/nerdctl-2.1.2-linux-amd64.tar.gz"; - download(url,`/tmp/${name}`,10000); - copy_bin(`/tmp/${name}/*`); - delete(`/tmp/${name}`); - - let name="containerd"; - let url="https://github.com/containerd/containerd/releases/download/v2.1.2/containerd-2.1.2-linux-amd64.tar.gz"; - download(url,`/tmp/${name}`,20000); - copy_bin(`/tmp/${name}/bin/*`); - delete(`/tmp/${name}`); - - run("apt-get -y install buildah runc"); - - // let url="https://github.com/threefoldtech/rfs/releases/download/v2.0.6/rfs"; - // download_file(url,`/tmp/rfs`,10000); - // chmod_exec("/tmp/rfs"); - // mv(`/tmp/rfs`,"/root/hero/bin/"); - -} - -fn ipfs_download(){ - let name="ipfs"; - let url="https://github.com/ipfs/kubo/releases/download/v0.34.1/kubo_v0.34.1_linux-amd64.tar.gz"; - download(url,`/tmp/${name}`,20); - copy(`/tmp/${name}/kubo/ipfs`,"/root/hero/bin/ipfs"); - // delete(`/tmp/${name}`); - - -} - -let d=download(`https://git.threefold.info/herocode/sal/raw/branch/main/examples/containers/nerdctl_install.rhai`,`/tmp/nerdctl_install.rhai`,0); - -print(`Repo path: ${d}`); - - - -// nerdctl_download(); -// ipfs_download(); - -"done" diff --git a/installers/base.rhai b/installers/base.rhai index 8f256a8..705ca20 100644 --- a/installers/base.rhai +++ b/installers/base.rhai @@ -19,9 +19,7 @@ fn zinit(){ download_file(url,`/tmp/${name}`,5000); copy_bin(`/tmp/${name}`); delete(`/tmp/${name}`); - - let name="containerd"; - + screen_new("zinit", "zinit init"); } platform_check_linux_x86(); diff --git a/installers/nerdctl.rhai b/installers/nerdctl.rhai index cae25dc..83d050d 100644 --- a/installers/nerdctl.rhai +++ b/installers/nerdctl.rhai @@ -14,7 +14,9 @@ fn nerdctl_download(){ copy_bin(`/tmp/${name}/bin/*`); delete(`/tmp/${name}`); - run("apt-get -y install buildah runc"); + package_install("buildah"); + package_install("runc"); + // let url="https://github.com/threefoldtech/rfs/releases/download/v2.0.6/rfs"; // download_file(url,`/tmp/rfs`,10000); diff --git a/src/process/mod.rs b/src/process/mod.rs index 6c48040..1563181 100644 --- a/src/process/mod.rs +++ b/src/process/mod.rs @@ -12,8 +12,10 @@ mod run; mod mgmt; +mod screen; #[cfg(test)] mod tests; pub use run::*; -pub use mgmt::*; \ No newline at end of file +pub use mgmt::*; +pub use screen::{new as new_screen, kill as kill_screen}; \ No newline at end of file diff --git a/src/process/screen.rs b/src/process/screen.rs new file mode 100644 index 0000000..b6a8de0 --- /dev/null +++ b/src/process/screen.rs @@ -0,0 +1,48 @@ +use crate::process::run_command; +use anyhow::Result; +use std::fs; + +/// Executes a command in a new screen session. +/// +/// # Arguments +/// +/// * `name` - The name of the screen session. +/// * `cmd` - The command to execute. +/// +/// # Returns +/// +/// * `Result<()>` - Ok if the command was executed successfully, otherwise an error. +pub fn new(name: &str, cmd: &str) -> Result<()> { + let script_path = format!("/tmp/cmd_{}.sh", name); + let mut script_content = String::new(); + + if !cmd.starts_with("#!") { + script_content.push_str("#!/bin/bash\n"); + } + + script_content.push_str("set -e\n"); + script_content.push_str(cmd); + + fs::write(&script_path, script_content)?; + fs::set_permissions(&script_path, std::os::unix::fs::PermissionsExt::from_mode(0o755))?; + + let screen_cmd = format!("screen -d -m -S {} {}", name, script_path); + run_command(&screen_cmd)?; + + Ok(()) +} + +/// Kills a screen session. +/// +/// # Arguments +/// +/// * `name` - The name of the screen session to kill. +/// +/// # Returns +/// +/// * `Result<()>` - Ok if the session was killed successfully, otherwise an error. +pub fn kill(name: &str) -> Result<()> { + let cmd = format!("screen -S {} -X quit", name); + run_command(&cmd)?; + Ok(()) +} \ No newline at end of file diff --git a/src/rhai/mod.rs b/src/rhai/mod.rs index 105340c..5a01592 100644 --- a/src/rhai/mod.rs +++ b/src/rhai/mod.rs @@ -14,6 +14,7 @@ mod postgresclient; mod process; mod redisclient; mod rfs; +mod screen; mod vault; mod text; mod zinit; @@ -185,6 +186,9 @@ pub fn register(engine: &mut Engine) -> Result<(), Box> { // Register Platform module functions platform::register(engine); + + // Register Screen module functions + screen::register(engine); // Register utility functions engine.register_fn("is_def_fn", |_name: &str| -> bool { diff --git a/src/rhai/process.rs b/src/rhai/process.rs index 258b0ec..7e25b23 100644 --- a/src/rhai/process.rs +++ b/src/rhai/process.rs @@ -2,7 +2,7 @@ //! //! This module provides Rhai wrappers for the functions in the Process module. -use crate::process::{self, CommandResult, ProcessError, ProcessInfo, RunError}; +use crate::process::{self, CommandResult, ProcessError, ProcessInfo, RunError }; use rhai::{Array, Dynamic, Engine, EvalAltResult, Map}; use std::clone::Clone; diff --git a/src/rhai/screen.rs b/src/rhai/screen.rs new file mode 100644 index 0000000..7b8107a --- /dev/null +++ b/src/rhai/screen.rs @@ -0,0 +1,26 @@ +use crate::process::{new_screen, kill_screen}; +use rhai::{Engine, Module, EvalAltResult}; + +fn screen_error_to_rhai_error(result: anyhow::Result) -> Result> { + result.map_err(|e| { + Box::new(EvalAltResult::ErrorRuntime( + format!("Screen error: {}", e).into(), + rhai::Position::NONE, + )) + }) +} + +#[allow(dead_code)] +pub fn register(engine: &mut Engine) { + let mut module = Module::new(); + + module.set_native_fn("screen_new", |name: &str, cmd: &str| { + screen_error_to_rhai_error(new_screen(name, cmd)) + }); + + module.set_native_fn("screen_kill", |name: &str| { + screen_error_to_rhai_error(kill_screen(name)) + }); + + engine.register_static_module("screen", module.into()); +} \ No newline at end of file