...
Some checks are pending
Rhai Tests / Run Rhai Tests (push) Waiting to run

This commit is contained in:
kristof 2025-06-15 21:50:10 +02:00
parent 66d5c8588a
commit 3a7b323f9a
5 changed files with 110 additions and 1 deletions

View File

@ -33,10 +33,13 @@ fn ipfs_download(){
}
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();
// nerdctl_download();
// ipfs_download();
"done"

View File

@ -0,0 +1,4 @@
exec(`https://git.threefold.info/herocode/sal/raw/branch/main/examples/containers/nerdctl_install.rhai`);

45
installers/nerctl.rhai Normal file
View File

@ -0,0 +1,45 @@
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"

53
src/rhai/core.rs Normal file
View File

@ -0,0 +1,53 @@
//! Rhai wrappers for core engine functions
//!
//! This module provides Rhai wrappers for functions that interact with the Rhai engine itself.
use rhai::{Engine, EvalAltResult, NativeCallContext};
use crate::os;
use super::error::ToRhaiError;
/// Register core module functions with the Rhai engine
///
/// # Arguments
///
/// * `engine` - The Rhai engine to register the functions with
///
/// # Returns
///
/// * `Result<(), Box<EvalAltResult>>` - Ok if registration was successful, Err otherwise
pub fn register_core_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
engine.register_fn("exec", exec);
Ok(())
}
/// Execute a Rhai script from a URL, file, or string
///
/// # Arguments
///
/// * `context` - The native call context, used to access the Rhai engine
/// * `source` - The source of the script to execute. Can be a URL, a file path, or a string of code.
///
/// # Returns
///
/// * `Result<rhai::Dynamic, Box<EvalAltResult>>` - The result of the script execution
pub fn exec(context: NativeCallContext, source: &str) -> Result<rhai::Dynamic, Box<EvalAltResult>> {
let content = if source.starts_with("http://") || source.starts_with("https://") {
// If the source is a URL, download it to a temporary file
let temp_dir = std::env::temp_dir();
let file_name = source.split('/').last().unwrap_or("script.rhai");
let dest_path = temp_dir.join(format!("{}-{}", uuid::Uuid::new_v4(), file_name));
let dest_str = dest_path.to_str().unwrap();
os::download_file(source, dest_str, 0).to_rhai_error()?;
os::file_read(dest_str).to_rhai_error()?
} else if os::exist(source) {
// If the source is an existing file, read it
os::file_read(source).to_rhai_error()?
} else {
// Otherwise, treat the source as the script content itself
source.to_string()
};
// Execute the script content
context.engine().eval(&content)
}

View File

@ -4,6 +4,7 @@
//! allowing SAL functions to be called from Rhai scripts.
mod buildah;
mod core;
mod error;
mod git;
mod nerdctl;
@ -140,6 +141,9 @@ pub use os::copy as os_copy;
/// let result = engine.eval::<i64>("exist('some_file.txt')").unwrap();
/// ```
pub fn register(engine: &mut Engine) -> Result<(), Box<rhai::EvalAltResult>> {
// Register Core module functions
core::register_core_module(engine)?;
// Register OS module functions
os::register_os_module(engine)?;