...
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
use rhai::{Engine, EvalAltResult, Array, Dynamic, Map};
|
||||
use std::collections::HashMap;
|
||||
use crate::virt::buildah::{self, BuildahError, Image};
|
||||
use crate::virt::buildah::{self, BuildahError, Image, Builder};
|
||||
use crate::process::CommandResult;
|
||||
|
||||
/// Register Buildah module functions with the Rhai engine
|
||||
@@ -20,35 +20,42 @@ pub fn register_bah_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>
|
||||
// Register types
|
||||
register_bah_types(engine)?;
|
||||
|
||||
// Register container functions
|
||||
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 Builder constructor
|
||||
engine.register_fn("bah_new", bah_new);
|
||||
|
||||
// Register image functions
|
||||
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);
|
||||
// Register Builder instance methods
|
||||
engine.register_fn("run", builder_run);
|
||||
engine.register_fn("run_with_isolation", builder_run_with_isolation);
|
||||
engine.register_fn("copy", builder_copy);
|
||||
engine.register_fn("add", builder_add);
|
||||
engine.register_fn("commit", builder_commit);
|
||||
// Remove the line that's causing the error
|
||||
engine.register_fn("remove", builder_remove);
|
||||
engine.register_fn("reset", builder_reset);
|
||||
engine.register_fn("config", builder_config);
|
||||
|
||||
// Register Builder static methods
|
||||
engine.register_fn("images", builder_images);
|
||||
engine.register_fn("image_remove", builder_image_remove);
|
||||
engine.register_fn("image_pull", builder_image_pull);
|
||||
engine.register_fn("image_push", builder_image_push);
|
||||
engine.register_fn("image_tag", builder_image_tag);
|
||||
engine.register_fn("build", builder_build);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Register Buildah module types with the Rhai engine
|
||||
fn register_bah_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
|
||||
// Register Image type and methods
|
||||
// Register Builder type
|
||||
engine.register_type_with_name::<Builder>("BuildahBuilder");
|
||||
|
||||
// Register getters for Builder properties
|
||||
engine.register_get("container_id", get_builder_container_id);
|
||||
engine.register_get("name", get_builder_name);
|
||||
engine.register_get("image", get_builder_image);
|
||||
|
||||
// Register Image type and methods (same as before)
|
||||
engine.register_type_with_name::<Image>("BuildahImage");
|
||||
|
||||
// Register getters for Image properties
|
||||
@@ -84,312 +91,8 @@ fn bah_error_to_rhai_error<T>(result: Result<T, BuildahError>) -> Result<T, Box<
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a new Map with default build options
|
||||
pub fn new_build_options() -> Map {
|
||||
let mut map = Map::new();
|
||||
map.insert("tag".into(), Dynamic::UNIT);
|
||||
map.insert("context_dir".into(), Dynamic::from("."));
|
||||
map.insert("file".into(), Dynamic::from("Dockerfile"));
|
||||
map.insert("isolation".into(), Dynamic::UNIT);
|
||||
map
|
||||
}
|
||||
|
||||
/// Create a new Map with default commit options
|
||||
pub fn new_commit_options() -> Map {
|
||||
let mut map = Map::new();
|
||||
map.insert("format".into(), Dynamic::UNIT);
|
||||
map.insert("squash".into(), Dynamic::from(false));
|
||||
map.insert("rm".into(), Dynamic::from(false));
|
||||
map
|
||||
}
|
||||
|
||||
/// Create a new Map for config options
|
||||
pub fn new_config_options() -> Map {
|
||||
Map::new()
|
||||
}
|
||||
|
||||
//
|
||||
// Container Function Wrappers
|
||||
//
|
||||
|
||||
/// Wrapper for buildah::from
|
||||
///
|
||||
/// Create a container from an 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 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 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 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 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 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 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 bah_list() -> Result<CommandResult, Box<EvalAltResult>> {
|
||||
bah_error_to_rhai_error(buildah::bah_list())
|
||||
}
|
||||
|
||||
/// Build an image with options specified in a Map
|
||||
///
|
||||
/// This provides a builder-style interface for Rhai scripts.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rhai
|
||||
/// let options = bah_new_build_options();
|
||||
/// options.tag = "my-image:latest";
|
||||
/// options.context_dir = ".";
|
||||
/// options.file = "Dockerfile";
|
||||
/// options.isolation = "chroot";
|
||||
/// let result = bah_build(options);
|
||||
/// ```
|
||||
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) => {
|
||||
if tag.is_unit() {
|
||||
None
|
||||
} else if let Ok(tag_str) = tag.clone().into_string() {
|
||||
Some(tag_str)
|
||||
} else {
|
||||
return Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
"tag must be a string".into(),
|
||||
rhai::Position::NONE
|
||||
)));
|
||||
}
|
||||
},
|
||||
None => None
|
||||
};
|
||||
|
||||
let context_dir = match options.get("context_dir") {
|
||||
Some(dir) => {
|
||||
if let Ok(dir_str) = dir.clone().into_string() {
|
||||
dir_str
|
||||
} else {
|
||||
return Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
"context_dir must be a string".into(),
|
||||
rhai::Position::NONE
|
||||
)));
|
||||
}
|
||||
},
|
||||
None => String::from(".")
|
||||
};
|
||||
|
||||
let file = match options.get("file") {
|
||||
Some(file) => {
|
||||
if let Ok(file_str) = file.clone().into_string() {
|
||||
file_str
|
||||
} else {
|
||||
return Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
"file must be a string".into(),
|
||||
rhai::Position::NONE
|
||||
)));
|
||||
}
|
||||
},
|
||||
None => String::from("Dockerfile")
|
||||
};
|
||||
|
||||
let isolation_option = match options.get("isolation") {
|
||||
Some(isolation) => {
|
||||
if isolation.is_unit() {
|
||||
None
|
||||
} else if let Ok(isolation_str) = isolation.clone().into_string() {
|
||||
Some(isolation_str)
|
||||
} else {
|
||||
return Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
"isolation must be a string".into(),
|
||||
rhai::Position::NONE
|
||||
)));
|
||||
}
|
||||
},
|
||||
None => None
|
||||
};
|
||||
|
||||
// Convert String to &str for the function call
|
||||
let tag_ref = tag_option.as_deref();
|
||||
let isolation_ref = isolation_option.as_deref();
|
||||
|
||||
// Call the buildah build function
|
||||
bah_error_to_rhai_error(buildah::bah_build(tag_ref, &context_dir, &file, isolation_ref))
|
||||
}
|
||||
|
||||
//
|
||||
// Image Function Wrappers
|
||||
//
|
||||
|
||||
/// Wrapper for buildah::images
|
||||
///
|
||||
/// List images in local storage.
|
||||
pub fn images() -> Result<Array, Box<EvalAltResult>> {
|
||||
let images = bah_error_to_rhai_error(buildah::images())?;
|
||||
|
||||
// Convert Vec<Image> to Rhai Array
|
||||
let mut array = Array::new();
|
||||
for image in images {
|
||||
array.push(Dynamic::from(image));
|
||||
}
|
||||
|
||||
Ok(array)
|
||||
}
|
||||
|
||||
/// Wrapper for buildah::image_remove
|
||||
///
|
||||
/// Remove one or more images.
|
||||
pub fn image_remove(image: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
||||
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>> {
|
||||
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>> {
|
||||
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>> {
|
||||
bah_error_to_rhai_error(buildah::image_pull(image, tls_verify))
|
||||
}
|
||||
|
||||
/// Commit a container to an image with options specified in a Map
|
||||
///
|
||||
/// This provides a builder-style interface for Rhai scripts.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rhai
|
||||
/// let options = bah_new_commit_options();
|
||||
/// options.format = "docker";
|
||||
/// options.squash = true;
|
||||
/// options.rm = true;
|
||||
/// 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
|
||||
let format_option = match options.get("format") {
|
||||
Some(format) => {
|
||||
if format.is_unit() {
|
||||
None
|
||||
} else if let Ok(format_str) = format.clone().into_string() {
|
||||
Some(format_str)
|
||||
} else {
|
||||
return Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
"format must be a string".into(),
|
||||
rhai::Position::NONE
|
||||
)));
|
||||
}
|
||||
},
|
||||
None => None
|
||||
};
|
||||
|
||||
let squash = match options.get("squash") {
|
||||
Some(squash) => {
|
||||
if let Ok(squash_val) = squash.clone().as_bool() {
|
||||
squash_val
|
||||
} else {
|
||||
return Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
"squash must be a boolean".into(),
|
||||
rhai::Position::NONE
|
||||
)));
|
||||
}
|
||||
},
|
||||
None => false
|
||||
};
|
||||
|
||||
let rm = match options.get("rm") {
|
||||
Some(rm) => {
|
||||
if let Ok(rm_val) = rm.clone().as_bool() {
|
||||
rm_val
|
||||
} else {
|
||||
return Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
"rm must be a boolean".into(),
|
||||
rhai::Position::NONE
|
||||
)));
|
||||
}
|
||||
},
|
||||
None => false
|
||||
};
|
||||
|
||||
// Convert String to &str for the function call
|
||||
let format_ref = format_option.as_deref();
|
||||
|
||||
// Call the buildah image_commit function
|
||||
bah_error_to_rhai_error(buildah::image_commit(container, image_name, format_ref, squash, rm))
|
||||
}
|
||||
|
||||
/// Configure a container with options specified in a Map
|
||||
///
|
||||
/// This provides a builder-style interface for Rhai scripts.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rhai
|
||||
/// let options = bah_new_config_options();
|
||||
/// options.author = "John Doe";
|
||||
/// options.cmd = "echo Hello";
|
||||
/// options.entrypoint = "/bin/sh -c";
|
||||
/// 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
|
||||
// Helper function to convert Rhai Map to Rust HashMap
|
||||
fn convert_map_to_hashmap(options: Map) -> Result<HashMap<String, String>, Box<EvalAltResult>> {
|
||||
let mut config_options = HashMap::<String, String>::new();
|
||||
|
||||
for (key, value) in options.iter() {
|
||||
@@ -404,6 +107,96 @@ pub fn config_with_options(container: &str, options: Map) -> Result<CommandResul
|
||||
}
|
||||
}
|
||||
|
||||
// Call the buildah config function
|
||||
bah_error_to_rhai_error(buildah::bah_config(container, config_options))
|
||||
Ok(config_options)
|
||||
}
|
||||
|
||||
/// Create a new Builder
|
||||
pub fn bah_new(name: &str, image: &str) -> Result<Builder, Box<EvalAltResult>> {
|
||||
bah_error_to_rhai_error(Builder::new(name, image))
|
||||
}
|
||||
|
||||
// Builder instance methods
|
||||
pub fn builder_run(builder: &mut Builder, command: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
||||
bah_error_to_rhai_error(builder.run(command))
|
||||
}
|
||||
|
||||
pub fn builder_run_with_isolation(builder: &mut Builder, command: &str, isolation: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
||||
bah_error_to_rhai_error(builder.run_with_isolation(command, isolation))
|
||||
}
|
||||
|
||||
pub fn builder_copy(builder: &mut Builder, source: &str, dest: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
||||
bah_error_to_rhai_error(builder.copy(source, dest))
|
||||
}
|
||||
|
||||
pub fn builder_add(builder: &mut Builder, source: &str, dest: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
||||
bah_error_to_rhai_error(builder.add(source, dest))
|
||||
}
|
||||
|
||||
pub fn builder_commit(builder: &mut Builder, image_name: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
||||
bah_error_to_rhai_error(builder.commit(image_name))
|
||||
}
|
||||
|
||||
pub fn builder_remove(builder: &mut Builder) -> Result<CommandResult, Box<EvalAltResult>> {
|
||||
bah_error_to_rhai_error(builder.remove())
|
||||
}
|
||||
|
||||
pub fn builder_config(builder: &mut Builder, options: Map) -> Result<CommandResult, Box<EvalAltResult>> {
|
||||
// Convert Rhai Map to Rust HashMap
|
||||
let config_options = convert_map_to_hashmap(options)?;
|
||||
bah_error_to_rhai_error(builder.config(config_options))
|
||||
}
|
||||
|
||||
// Builder static methods
|
||||
pub fn builder_images(_builder: &mut Builder) -> Result<Array, Box<EvalAltResult>> {
|
||||
let images = bah_error_to_rhai_error(Builder::images())?;
|
||||
|
||||
// Convert Vec<Image> to Rhai Array
|
||||
let mut array = Array::new();
|
||||
for image in images {
|
||||
array.push(Dynamic::from(image));
|
||||
}
|
||||
|
||||
Ok(array)
|
||||
}
|
||||
|
||||
pub fn builder_image_remove(_builder: &mut Builder, image: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
||||
bah_error_to_rhai_error(Builder::image_remove(image))
|
||||
}
|
||||
|
||||
pub fn builder_image_pull(_builder: &mut Builder, image: &str, tls_verify: bool) -> Result<CommandResult, Box<EvalAltResult>> {
|
||||
bah_error_to_rhai_error(Builder::image_pull(image, tls_verify))
|
||||
}
|
||||
|
||||
pub fn builder_image_push(_builder: &mut Builder, image: &str, destination: &str, tls_verify: bool) -> Result<CommandResult, Box<EvalAltResult>> {
|
||||
bah_error_to_rhai_error(Builder::image_push(image, destination, tls_verify))
|
||||
}
|
||||
|
||||
pub fn builder_image_tag(_builder: &mut Builder, image: &str, new_name: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
||||
bah_error_to_rhai_error(Builder::image_tag(image, new_name))
|
||||
}
|
||||
|
||||
// Getter functions for Builder properties
|
||||
pub fn get_builder_container_id(builder: &mut Builder) -> String {
|
||||
match builder.container_id() {
|
||||
Some(id) => id.clone(),
|
||||
None => "".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_builder_name(builder: &mut Builder) -> String {
|
||||
builder.name().to_string()
|
||||
}
|
||||
|
||||
pub fn get_builder_image(builder: &mut Builder) -> String {
|
||||
builder.image().to_string()
|
||||
}
|
||||
|
||||
// Reset function for Builder
|
||||
pub fn builder_reset(builder: &mut Builder) -> Result<(), Box<EvalAltResult>> {
|
||||
bah_error_to_rhai_error(builder.reset())
|
||||
}
|
||||
|
||||
// Build function for Builder
|
||||
pub fn builder_build(_builder: &mut Builder, tag: &str, context_dir: &str, file: &str, isolation: &str) -> Result<CommandResult, Box<EvalAltResult>> {
|
||||
bah_error_to_rhai_error(Builder::build(Some(tag), context_dir, file, Some(isolation)))
|
||||
}
|
Reference in New Issue
Block a user