This commit is contained in:
despiegk 2025-04-05 11:30:20 +02:00
parent e9a73327e3
commit 768542afd3
3 changed files with 103 additions and 20 deletions

View File

@ -29,10 +29,14 @@ pub fn register_bah_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>
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 instance methods for entrypoint, cmd, and content operations
engine.register_fn("set_entrypoint", builder_set_entrypoint);
engine.register_fn("set_cmd", builder_set_cmd);
engine.register_fn("write_content", builder_write_content);
engine.register_fn("read_content", builder_read_content);
// Register Builder static methods
engine.register_fn("images", builder_images);
@ -41,7 +45,6 @@ pub fn register_bah_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>
engine.register_fn("image_push", builder_image_push);
engine.register_fn("image_tag", builder_image_tag);
engine.register_fn("build", builder_build);
engine.register_fn("write_content", builder_write_content);
engine.register_fn("read_content", builder_read_content);
Ok(())
@ -150,6 +153,16 @@ pub fn builder_config(builder: &mut Builder, options: Map) -> Result<CommandResu
bah_error_to_rhai_error(builder.config(config_options))
}
/// Set the entrypoint for the container
pub fn builder_set_entrypoint(builder: &mut Builder, entrypoint: &str) -> Result<CommandResult, Box<EvalAltResult>> {
bah_error_to_rhai_error(builder.set_entrypoint(entrypoint))
}
/// Set the default command for the container
pub fn builder_set_cmd(builder: &mut Builder, cmd: &str) -> Result<CommandResult, Box<EvalAltResult>> {
bah_error_to_rhai_error(builder.set_cmd(cmd))
}
/// Write content to a file in the container
pub fn builder_write_content(builder: &mut Builder, content: &str, dest_path: &str) -> Result<CommandResult, Box<EvalAltResult>> {
if let Some(container_id) = builder.container_id() {

View File

@ -94,6 +94,10 @@ nginx -g "daemon off;"
let startup_script_result = builder.write_content(startup_script, "/start.sh");
builder.run("chmod +x /start.sh");
// Set the entrypoint to the startup script
println("Setting entrypoint to /start.sh...");
builder.set_entrypoint("/start.sh");
// Read back the startup script to verify it was written correctly
let read_script = builder.read_content("/start.sh");
println("Startup script content verification:");
@ -103,27 +107,35 @@ println(read_script);
println(`Committing container to image '${final_image_name}'...`);
let commit_result = builder.commit(final_image_name);
// Clean up the buildah container
println("Cleaning up buildah container...");
builder.remove();
// // Clean up the buildah container
// println("Cleaning up buildah container...");
// builder.remove();
// Now use nerdctl to run a container from the new image
println("\nStarting container from the new image using nerdctl...");
// // Now use nerdctl to run a container from the new image
// println("\nStarting container from the new image using nerdctl...");
// Create a container using the builder pattern
let container = nerdctl_container_from_image("golang-nginx-demo", final_image_name)
.with_detach(true)
.with_port("8080:80") // Map port 80 in the container to 8080 on the host
.with_restart_policy("unless-stopped")
.build();
// // Create a container using the builder pattern
// // Use localhost/ prefix to ensure nerdctl uses the local image
// let local_image_name = "localhost/" + final_image_name;
// println(`Using local image: ${local_image_name}`);
// Start the container
let start_result = container.start();
// // Tag the image with the localhost prefix
// let tag_result = bah_image_tag(final_image_name, local_image_name);
// println(`Tagged image as ${local_image_name}`);
println("\nWorkflow completed successfully!");
println("The web server should be running at http://localhost:8080");
println("You can check container logs with: nerdctl logs golang-nginx-demo");
println("To stop the container: nerdctl stop golang-nginx-demo");
println("To remove the container: nerdctl rm golang-nginx-demo");
// let container = nerdctl_container_from_image("golang-nginx-demo", local_image_name)
// .with_detach(true)
// .with_port("8080:80") // Map port 80 in the container to 8080 on the host
// .with_restart_policy("unless-stopped")
// .build();
// // Start the container
// let start_result = container.start();
// println("\nWorkflow completed successfully!");
// println("The web server should be running at http://localhost:8080");
// println("You can check container logs with: nerdctl logs golang-nginx-demo");
// println("To stop the container: nerdctl stop golang-nginx-demo");
// println("To remove the container: nerdctl rm golang-nginx-demo");
"Buildah and nerdctl workflow completed successfully!"

View File

@ -355,6 +355,64 @@ impl Builder {
}
}
/// Set the entrypoint for the container
///
/// # Arguments
///
/// * `entrypoint` - The entrypoint command
///
/// # Returns
///
/// * `Result<CommandResult, BuildahError>` - Command result or error
pub fn set_entrypoint(&self, entrypoint: &str) -> Result<CommandResult, BuildahError> {
if let Some(container_id) = &self.container_id {
// Save the current debug flag
let previous_debug = thread_local_debug();
// Set the thread-local debug flag from the Builder's debug flag
set_thread_local_debug(self.debug);
// Execute the command
let result = execute_buildah_command(&["config", "--entrypoint", entrypoint, container_id]);
// Restore the previous debug flag
set_thread_local_debug(previous_debug);
result
} else {
Err(BuildahError::Other("No container ID available".to_string()))
}
}
/// Set the default command for the container
///
/// # Arguments
///
/// * `cmd` - The default command
///
/// # Returns
///
/// * `Result<CommandResult, BuildahError>` - Command result or error
pub fn set_cmd(&self, cmd: &str) -> Result<CommandResult, BuildahError> {
if let Some(container_id) = &self.container_id {
// Save the current debug flag
let previous_debug = thread_local_debug();
// Set the thread-local debug flag from the Builder's debug flag
set_thread_local_debug(self.debug);
// Execute the command
let result = execute_buildah_command(&["config", "--cmd", cmd, container_id]);
// Restore the previous debug flag
set_thread_local_debug(previous_debug);
result
} else {
Err(BuildahError::Other("No container ID available".to_string()))
}
}
/// List images in local storage
///
/// # Returns