...
This commit is contained in:
parent
e9a73327e3
commit
768542afd3
@ -29,10 +29,14 @@ pub fn register_bah_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>
|
|||||||
engine.register_fn("copy", builder_copy);
|
engine.register_fn("copy", builder_copy);
|
||||||
engine.register_fn("add", builder_add);
|
engine.register_fn("add", builder_add);
|
||||||
engine.register_fn("commit", builder_commit);
|
engine.register_fn("commit", builder_commit);
|
||||||
// Remove the line that's causing the error
|
|
||||||
engine.register_fn("remove", builder_remove);
|
engine.register_fn("remove", builder_remove);
|
||||||
engine.register_fn("reset", builder_reset);
|
engine.register_fn("reset", builder_reset);
|
||||||
engine.register_fn("config", builder_config);
|
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
|
// Register Builder static methods
|
||||||
engine.register_fn("images", builder_images);
|
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_push", builder_image_push);
|
||||||
engine.register_fn("image_tag", builder_image_tag);
|
engine.register_fn("image_tag", builder_image_tag);
|
||||||
engine.register_fn("build", builder_build);
|
engine.register_fn("build", builder_build);
|
||||||
engine.register_fn("write_content", builder_write_content);
|
|
||||||
engine.register_fn("read_content", builder_read_content);
|
engine.register_fn("read_content", builder_read_content);
|
||||||
|
|
||||||
Ok(())
|
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))
|
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
|
/// 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>> {
|
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() {
|
if let Some(container_id) = builder.container_id() {
|
||||||
|
@ -94,6 +94,10 @@ nginx -g "daemon off;"
|
|||||||
let startup_script_result = builder.write_content(startup_script, "/start.sh");
|
let startup_script_result = builder.write_content(startup_script, "/start.sh");
|
||||||
builder.run("chmod +x /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
|
// Read back the startup script to verify it was written correctly
|
||||||
let read_script = builder.read_content("/start.sh");
|
let read_script = builder.read_content("/start.sh");
|
||||||
println("Startup script content verification:");
|
println("Startup script content verification:");
|
||||||
@ -103,27 +107,35 @@ println(read_script);
|
|||||||
println(`Committing container to image '${final_image_name}'...`);
|
println(`Committing container to image '${final_image_name}'...`);
|
||||||
let commit_result = builder.commit(final_image_name);
|
let commit_result = builder.commit(final_image_name);
|
||||||
|
|
||||||
// Clean up the buildah container
|
// // Clean up the buildah container
|
||||||
println("Cleaning up buildah container...");
|
// println("Cleaning up buildah container...");
|
||||||
builder.remove();
|
// builder.remove();
|
||||||
|
|
||||||
// Now use nerdctl to run a container from the new image
|
// // Now use nerdctl to run a container from the new image
|
||||||
println("\nStarting container from the new image using nerdctl...");
|
// println("\nStarting container from the new image using nerdctl...");
|
||||||
|
|
||||||
// Create a container using the builder pattern
|
// // Create a container using the builder pattern
|
||||||
let container = nerdctl_container_from_image("golang-nginx-demo", final_image_name)
|
// // Use localhost/ prefix to ensure nerdctl uses the local image
|
||||||
.with_detach(true)
|
// let local_image_name = "localhost/" + final_image_name;
|
||||||
.with_port("8080:80") // Map port 80 in the container to 8080 on the host
|
// println(`Using local image: ${local_image_name}`);
|
||||||
.with_restart_policy("unless-stopped")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
// Start the container
|
// // Tag the image with the localhost prefix
|
||||||
let start_result = container.start();
|
// let tag_result = bah_image_tag(final_image_name, local_image_name);
|
||||||
|
// println(`Tagged image as ${local_image_name}`);
|
||||||
|
|
||||||
println("\nWorkflow completed successfully!");
|
// let container = nerdctl_container_from_image("golang-nginx-demo", local_image_name)
|
||||||
println("The web server should be running at http://localhost:8080");
|
// .with_detach(true)
|
||||||
println("You can check container logs with: nerdctl logs golang-nginx-demo");
|
// .with_port("8080:80") // Map port 80 in the container to 8080 on the host
|
||||||
println("To stop the container: nerdctl stop golang-nginx-demo");
|
// .with_restart_policy("unless-stopped")
|
||||||
println("To remove the container: nerdctl rm golang-nginx-demo");
|
// .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!"
|
"Buildah and nerdctl workflow completed successfully!"
|
||||||
|
@ -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
|
/// List images in local storage
|
||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
|
Loading…
Reference in New Issue
Block a user