diff --git a/examples/containers/nerdctl_install.rhai b/examples/containers/nerdctl_install.rhai index 24c8458..55c9bd1 100644 --- a/examples/containers/nerdctl_install.rhai +++ b/examples/containers/nerdctl_install.rhai @@ -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" diff --git a/installers/install_all.rhai b/installers/install_all.rhai new file mode 100644 index 0000000..72497b2 --- /dev/null +++ b/installers/install_all.rhai @@ -0,0 +1,4 @@ + + + +exec(`https://git.threefold.info/herocode/sal/raw/branch/main/examples/containers/nerdctl_install.rhai`); diff --git a/installers/nerctl.rhai b/installers/nerctl.rhai new file mode 100644 index 0000000..55c9bd1 --- /dev/null +++ b/installers/nerctl.rhai @@ -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" diff --git a/src/rhai/core.rs b/src/rhai/core.rs new file mode 100644 index 0000000..87f5d64 --- /dev/null +++ b/src/rhai/core.rs @@ -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>` - Ok if registration was successful, Err otherwise +pub fn register_core_module(engine: &mut Engine) -> Result<(), Box> { + 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>` - The result of the script execution +pub fn exec(context: NativeCallContext, source: &str) -> Result> { + 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) +} \ No newline at end of file diff --git a/src/rhai/mod.rs b/src/rhai/mod.rs index 801864c..7a172e3 100644 --- a/src/rhai/mod.rs +++ b/src/rhai/mod.rs @@ -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::("exist('some_file.txt')").unwrap(); /// ``` pub fn register(engine: &mut Engine) -> Result<(), Box> { + // Register Core module functions + core::register_core_module(engine)?; + // Register OS module functions os::register_os_module(engine)?;