From 768542afd3f7428a6c0a728bc019985653d01aaf Mon Sep 17 00:00:00 2001 From: despiegk Date: Sat, 5 Apr 2025 11:30:20 +0200 Subject: [PATCH] ... --- src/rhai/buildah.rs | 17 ++++++++-- src/rhaiexamples/buildah.rhai | 48 ++++++++++++++++++----------- src/virt/buildah/builder.rs | 58 +++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 20 deletions(-) diff --git a/src/rhai/buildah.rs b/src/rhai/buildah.rs index b324e09..9eb1086 100644 --- a/src/rhai/buildah.rs +++ b/src/rhai/buildah.rs @@ -29,10 +29,14 @@ pub fn register_bah_module(engine: &mut Engine) -> Result<(), Box 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 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 Result> { + 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> { + 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> { if let Some(container_id) = builder.container_id() { diff --git a/src/rhaiexamples/buildah.rhai b/src/rhaiexamples/buildah.rhai index f188480..9956c1c 100644 --- a/src/rhaiexamples/buildah.rhai +++ b/src/rhaiexamples/buildah.rhai @@ -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!" diff --git a/src/virt/buildah/builder.rs b/src/virt/buildah/builder.rs index 4908190..4a690aa 100644 --- a/src/virt/buildah/builder.rs +++ b/src/virt/buildah/builder.rs @@ -355,6 +355,64 @@ impl Builder { } } + /// Set the entrypoint for the container + /// + /// # Arguments + /// + /// * `entrypoint` - The entrypoint command + /// + /// # Returns + /// + /// * `Result` - Command result or error + pub fn set_entrypoint(&self, entrypoint: &str) -> Result { + 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` - Command result or error + pub fn set_cmd(&self, cmd: &str) -> Result { + 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