|
|
|
@@ -16,38 +16,38 @@ use crate::process::CommandResult;
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<(), Box<EvalAltResult>>` - Ok if registration was successful, Err otherwise
|
|
|
|
|
pub fn register_buildah_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
pub fn register_bah_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
// Register types
|
|
|
|
|
register_buildah_types(engine)?;
|
|
|
|
|
register_bah_types(engine)?;
|
|
|
|
|
|
|
|
|
|
// Register container functions
|
|
|
|
|
engine.register_fn("buildah_from", from);
|
|
|
|
|
engine.register_fn("buildah_run", run);
|
|
|
|
|
engine.register_fn("buildah_run_with_isolation", run_with_isolation);
|
|
|
|
|
engine.register_fn("buildah_copy", copy);
|
|
|
|
|
engine.register_fn("buildah_add", add);
|
|
|
|
|
engine.register_fn("buildah_commit", commit);
|
|
|
|
|
engine.register_fn("buildah_remove", remove);
|
|
|
|
|
engine.register_fn("buildah_list", list);
|
|
|
|
|
engine.register_fn("buildah_build", build_with_options);
|
|
|
|
|
engine.register_fn("buildah_new_build_options", new_build_options);
|
|
|
|
|
engine.register_fn("bah_from", bah_from);
|
|
|
|
|
engine.register_fn("bah_run", bah_run);
|
|
|
|
|
engine.register_fn("bah_run_with_isolation", bah_run_with_isolation);
|
|
|
|
|
engine.register_fn("bah_copy", bah_copy);
|
|
|
|
|
engine.register_fn("bah_add", bah_add);
|
|
|
|
|
engine.register_fn("bah_commit", bah_commit);
|
|
|
|
|
engine.register_fn("bah_remove", bah_remove);
|
|
|
|
|
engine.register_fn("bah_list", bah_list);
|
|
|
|
|
engine.register_fn("bah_build", bah_build_with_options);
|
|
|
|
|
engine.register_fn("bah_new_build_options", new_build_options);
|
|
|
|
|
|
|
|
|
|
// Register image functions
|
|
|
|
|
engine.register_fn("buildah_images", images);
|
|
|
|
|
engine.register_fn("buildah_image_remove", image_remove);
|
|
|
|
|
engine.register_fn("buildah_image_push", image_push);
|
|
|
|
|
engine.register_fn("buildah_image_tag", image_tag);
|
|
|
|
|
engine.register_fn("buildah_image_pull", image_pull);
|
|
|
|
|
engine.register_fn("buildah_image_commit", image_commit_with_options);
|
|
|
|
|
engine.register_fn("buildah_new_commit_options", new_commit_options);
|
|
|
|
|
engine.register_fn("buildah_config", config_with_options);
|
|
|
|
|
engine.register_fn("buildah_new_config_options", new_config_options);
|
|
|
|
|
engine.register_fn("bah_images", images);
|
|
|
|
|
engine.register_fn("bah_image_remove", image_remove);
|
|
|
|
|
engine.register_fn("bah_image_push", image_push);
|
|
|
|
|
engine.register_fn("bah_image_tag", image_tag);
|
|
|
|
|
engine.register_fn("bah_image_pull", image_pull);
|
|
|
|
|
engine.register_fn("bah_image_commit", image_commit_with_options);
|
|
|
|
|
engine.register_fn("bah_new_commit_options", new_commit_options);
|
|
|
|
|
engine.register_fn("bah_config", config_with_options);
|
|
|
|
|
engine.register_fn("bah_new_config_options", new_config_options);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Register Buildah module types with the Rhai engine
|
|
|
|
|
fn register_buildah_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
fn register_bah_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
// Register Image type and methods
|
|
|
|
|
engine.register_type_with_name::<Image>("BuildahImage");
|
|
|
|
|
|
|
|
|
@@ -60,6 +60,14 @@ fn register_buildah_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>>
|
|
|
|
|
}
|
|
|
|
|
array
|
|
|
|
|
});
|
|
|
|
|
// Add a 'name' getter that returns the first name or a default
|
|
|
|
|
engine.register_get("name", |img: &mut Image| {
|
|
|
|
|
if img.names.is_empty() {
|
|
|
|
|
"<none>".to_string()
|
|
|
|
|
} else {
|
|
|
|
|
img.names[0].clone()
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
engine.register_get("size", |img: &mut Image| img.size.clone());
|
|
|
|
|
engine.register_get("created", |img: &mut Image| img.created.clone());
|
|
|
|
|
|
|
|
|
@@ -67,7 +75,7 @@ fn register_buildah_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper functions for error conversion
|
|
|
|
|
fn buildah_error_to_rhai_error<T>(result: Result<T, BuildahError>) -> Result<T, Box<EvalAltResult>> {
|
|
|
|
|
fn bah_error_to_rhai_error<T>(result: Result<T, BuildahError>) -> Result<T, Box<EvalAltResult>> {
|
|
|
|
|
result.map_err(|e| {
|
|
|
|
|
Box::new(EvalAltResult::ErrorRuntime(
|
|
|
|
|
format!("Buildah error: {}", e).into(),
|
|
|
|
@@ -107,57 +115,67 @@ pub fn new_config_options() -> Map {
|
|
|
|
|
/// Wrapper for buildah::from
|
|
|
|
|
///
|
|
|
|
|
/// Create a container from an image.
|
|
|
|
|
pub fn from(image: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
buildah_error_to_rhai_error(buildah::from(image))
|
|
|
|
|
pub fn bah_from(image: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
let result = bah_error_to_rhai_error(buildah::from(image))?;
|
|
|
|
|
|
|
|
|
|
// Create a new CommandResult with trimmed stdout
|
|
|
|
|
let trimmed_result = CommandResult {
|
|
|
|
|
stdout: result.stdout.trim().to_string(),
|
|
|
|
|
stderr: result.stderr.trim().to_string(),
|
|
|
|
|
success: result.success,
|
|
|
|
|
code: result.code,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(trimmed_result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wrapper for buildah::run
|
|
|
|
|
///
|
|
|
|
|
/// Run a command in a container.
|
|
|
|
|
pub fn run(container: &str, command: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
buildah_error_to_rhai_error(buildah::run(container, command))
|
|
|
|
|
pub fn bah_run(container: &str, command: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
bah_error_to_rhai_error(buildah::run(container, command))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wrapper for buildah::run_with_isolation
|
|
|
|
|
///
|
|
|
|
|
/// Run a command in a container with specified isolation.
|
|
|
|
|
pub fn run_with_isolation(container: &str, command: &str, isolation: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
buildah_error_to_rhai_error(buildah::run_with_isolation(container, command, isolation))
|
|
|
|
|
pub fn bah_run_with_isolation(container: &str, command: &str, isolation: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
bah_error_to_rhai_error(buildah::bah_run_with_isolation(container, command, isolation))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wrapper for buildah::copy
|
|
|
|
|
///
|
|
|
|
|
/// Copy files into a container.
|
|
|
|
|
pub fn copy(container: &str, source: &str, dest: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
buildah_error_to_rhai_error(buildah::copy(container, source, dest))
|
|
|
|
|
pub fn bah_copy(container: &str, source: &str, dest: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
bah_error_to_rhai_error(buildah::bah_copy(container, source, dest))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wrapper for buildah::add
|
|
|
|
|
///
|
|
|
|
|
/// Add files into a container.
|
|
|
|
|
pub fn add(container: &str, source: &str, dest: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
buildah_error_to_rhai_error(buildah::add(container, source, dest))
|
|
|
|
|
pub fn bah_add(container: &str, source: &str, dest: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
bah_error_to_rhai_error(buildah::bah_add(container, source, dest))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wrapper for buildah::commit
|
|
|
|
|
///
|
|
|
|
|
/// Commit a container to an image.
|
|
|
|
|
pub fn commit(container: &str, image_name: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
buildah_error_to_rhai_error(buildah::commit(container, image_name))
|
|
|
|
|
pub fn bah_commit(container: &str, image_name: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
bah_error_to_rhai_error(buildah::bah_commit(container, image_name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wrapper for buildah::remove
|
|
|
|
|
///
|
|
|
|
|
/// Remove a container.
|
|
|
|
|
pub fn remove(container: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
buildah_error_to_rhai_error(buildah::remove(container))
|
|
|
|
|
pub fn bah_remove(container: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
bah_error_to_rhai_error(buildah::bah_remove(container))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wrapper for buildah::list
|
|
|
|
|
///
|
|
|
|
|
/// List containers.
|
|
|
|
|
pub fn list() -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
buildah_error_to_rhai_error(buildah::list())
|
|
|
|
|
pub fn bah_list() -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
bah_error_to_rhai_error(buildah::bah_list())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Build an image with options specified in a Map
|
|
|
|
@@ -167,14 +185,14 @@ pub fn list() -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```rhai
|
|
|
|
|
/// let options = buildah_new_build_options();
|
|
|
|
|
/// let options = bah_new_build_options();
|
|
|
|
|
/// options.tag = "my-image:latest";
|
|
|
|
|
/// options.context_dir = ".";
|
|
|
|
|
/// options.file = "Dockerfile";
|
|
|
|
|
/// options.isolation = "chroot";
|
|
|
|
|
/// let result = buildah_build(options);
|
|
|
|
|
/// let result = bah_build(options);
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn build_with_options(options: Map) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
pub fn bah_build_with_options(options: Map) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
// Extract options from the map
|
|
|
|
|
let tag_option = match options.get("tag") {
|
|
|
|
|
Some(tag) => {
|
|
|
|
@@ -241,7 +259,7 @@ pub fn build_with_options(options: Map) -> Result<CommandResult, Box<EvalAltResu
|
|
|
|
|
let isolation_ref = isolation_option.as_deref();
|
|
|
|
|
|
|
|
|
|
// Call the buildah build function
|
|
|
|
|
buildah_error_to_rhai_error(buildah::build(tag_ref, &context_dir, &file, isolation_ref))
|
|
|
|
|
bah_error_to_rhai_error(buildah::bah_build(tag_ref, &context_dir, &file, isolation_ref))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
@@ -252,7 +270,7 @@ pub fn build_with_options(options: Map) -> Result<CommandResult, Box<EvalAltResu
|
|
|
|
|
///
|
|
|
|
|
/// List images in local storage.
|
|
|
|
|
pub fn images() -> Result<Array, Box<EvalAltResult>> {
|
|
|
|
|
let images = buildah_error_to_rhai_error(buildah::images())?;
|
|
|
|
|
let images = bah_error_to_rhai_error(buildah::images())?;
|
|
|
|
|
|
|
|
|
|
// Convert Vec<Image> to Rhai Array
|
|
|
|
|
let mut array = Array::new();
|
|
|
|
@@ -267,28 +285,28 @@ pub fn images() -> Result<Array, Box<EvalAltResult>> {
|
|
|
|
|
///
|
|
|
|
|
/// Remove one or more images.
|
|
|
|
|
pub fn image_remove(image: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
buildah_error_to_rhai_error(buildah::image_remove(image))
|
|
|
|
|
bah_error_to_rhai_error(buildah::image_remove(image))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wrapper for buildah::image_push
|
|
|
|
|
///
|
|
|
|
|
/// Push an image to a registry.
|
|
|
|
|
pub fn image_push(image: &str, destination: &str, tls_verify: bool) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
buildah_error_to_rhai_error(buildah::image_push(image, destination, tls_verify))
|
|
|
|
|
bah_error_to_rhai_error(buildah::image_push(image, destination, tls_verify))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wrapper for buildah::image_tag
|
|
|
|
|
///
|
|
|
|
|
/// Add an additional name to a local image.
|
|
|
|
|
pub fn image_tag(image: &str, new_name: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
buildah_error_to_rhai_error(buildah::image_tag(image, new_name))
|
|
|
|
|
bah_error_to_rhai_error(buildah::image_tag(image, new_name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wrapper for buildah::image_pull
|
|
|
|
|
///
|
|
|
|
|
/// Pull an image from a registry.
|
|
|
|
|
pub fn image_pull(image: &str, tls_verify: bool) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
buildah_error_to_rhai_error(buildah::image_pull(image, tls_verify))
|
|
|
|
|
bah_error_to_rhai_error(buildah::image_pull(image, tls_verify))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Commit a container to an image with options specified in a Map
|
|
|
|
@@ -298,11 +316,11 @@ pub fn image_pull(image: &str, tls_verify: bool) -> Result<CommandResult, Box<Ev
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```rhai
|
|
|
|
|
/// let options = buildah_new_commit_options();
|
|
|
|
|
/// let options = bah_new_commit_options();
|
|
|
|
|
/// options.format = "docker";
|
|
|
|
|
/// options.squash = true;
|
|
|
|
|
/// options.rm = true;
|
|
|
|
|
/// let result = buildah_image_commit("my-container", "my-image:latest", options);
|
|
|
|
|
/// let result = bah_image_commit("my-container", "my-image:latest", options);
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn image_commit_with_options(container: &str, image_name: &str, options: Map) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
// Extract options from the map
|
|
|
|
@@ -354,7 +372,7 @@ pub fn image_commit_with_options(container: &str, image_name: &str, options: Map
|
|
|
|
|
let format_ref = format_option.as_deref();
|
|
|
|
|
|
|
|
|
|
// Call the buildah image_commit function
|
|
|
|
|
buildah_error_to_rhai_error(buildah::image_commit(container, image_name, format_ref, squash, rm))
|
|
|
|
|
bah_error_to_rhai_error(buildah::image_commit(container, image_name, format_ref, squash, rm))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Configure a container with options specified in a Map
|
|
|
|
@@ -364,11 +382,11 @@ pub fn image_commit_with_options(container: &str, image_name: &str, options: Map
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```rhai
|
|
|
|
|
/// let options = buildah_new_config_options();
|
|
|
|
|
/// let options = bah_new_config_options();
|
|
|
|
|
/// options.author = "John Doe";
|
|
|
|
|
/// options.cmd = "echo Hello";
|
|
|
|
|
/// options.entrypoint = "/bin/sh -c";
|
|
|
|
|
/// let result = buildah_config("my-container", options);
|
|
|
|
|
/// let result = bah_config("my-container", options);
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn config_with_options(container: &str, options: Map) -> Result<CommandResult, Box<EvalAltResult>> {
|
|
|
|
|
// Convert Rhai Map to Rust HashMap
|
|
|
|
@@ -387,5 +405,5 @@ pub fn config_with_options(container: &str, options: Map) -> Result<CommandResul
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call the buildah config function
|
|
|
|
|
buildah_error_to_rhai_error(buildah::config(container, config_options))
|
|
|
|
|
bah_error_to_rhai_error(buildah::bah_config(container, config_options))
|
|
|
|
|
}
|