This commit is contained in:
kristof 2025-06-15 22:40:28 +02:00
parent 8a8ead17cb
commit e7e8e7daf8
8 changed files with 86 additions and 51 deletions

View File

@ -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"

View File

@ -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();

View File

@ -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);

View File

@ -12,8 +12,10 @@
mod run;
mod mgmt;
mod screen;
#[cfg(test)]
mod tests;
pub use run::*;
pub use mgmt::*;
pub use mgmt::*;
pub use screen::{new as new_screen, kill as kill_screen};

48
src/process/screen.rs Normal file
View File

@ -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(())
}

View File

@ -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<rhai::EvalAltResult>> {
// 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 {

View File

@ -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;

26
src/rhai/screen.rs Normal file
View File

@ -0,0 +1,26 @@
use crate::process::{new_screen, kill_screen};
use rhai::{Engine, Module, EvalAltResult};
fn screen_error_to_rhai_error<T>(result: anyhow::Result<T>) -> Result<T, Box<EvalAltResult>> {
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());
}