96 lines
3.2 KiB
Plaintext
96 lines
3.2 KiB
Plaintext
// 04_buildah_operations.rhai
|
|
// Demonstrates container operations using SAL's buildah integration
|
|
// Note: This script requires buildah to be installed and may need root privileges
|
|
|
|
// Check if buildah is installed
|
|
let buildah_exists = which("buildah");
|
|
println(`Buildah exists: ${buildah_exists}`);
|
|
|
|
// List available images (only if buildah is installed)
|
|
println("Listing available container images:");
|
|
// if ! buildah_exists != "" {
|
|
// //EXIT
|
|
// }
|
|
let images = bah_images();
|
|
println(`Found ${images.len()} images`);
|
|
|
|
// Print image details (limited to 3)
|
|
let count = 0;
|
|
for img in images {
|
|
if count >= 3 {
|
|
break;
|
|
}
|
|
println(` - ID: ${img.id}, Name: ${img.name}, Created: ${img.created}`);
|
|
count += 1;
|
|
}
|
|
|
|
//Create a container from an image
|
|
println("\nCreating a container from alpine image:");
|
|
let container = bah_from("alpine:latest");
|
|
println(`Container result: success=${container.success}, code=${container.code}`);
|
|
println(`Container stdout: "${container.stdout}"`);
|
|
println(`Container stderr: "${container.stderr}"`);
|
|
let container_id = container.stdout;
|
|
println(`Container ID: ${container_id}`);
|
|
|
|
//Run a command in the container
|
|
println("\nRunning a command in the container:");
|
|
let run_result = bah_run(container_id, "echo 'Hello from container'");
|
|
println(`Command output: ${run_result.stdout}`);
|
|
|
|
//Add a file to the container
|
|
println("\nAdding a file to the container:");
|
|
let test_file = "test_file.txt";
|
|
run(`echo "Test content" > ${test_file}`);
|
|
let add_result = bah_add(container_id, test_file, "/");
|
|
println(`Add result: ${add_result.success}`);
|
|
|
|
//Commit the container to create a new image
|
|
println("\nCommitting the container to create a new image:");
|
|
let commit_result = bah_commit(container_id, "my-custom-image:latest");
|
|
println(`Commit result: ${commit_result.success}`);
|
|
|
|
//Remove the container
|
|
println("\nRemoving the container:");
|
|
let remove_result = bah_remove(container_id);
|
|
println(`Remove result: ${remove_result.success}`);
|
|
|
|
//Clean up the test file
|
|
delete(test_file);
|
|
|
|
// Demonstrate build options
|
|
println("\nDemonstrating build options:");
|
|
let build_options = bah_new_build_options();
|
|
build_options.tag = "example-image:latest";
|
|
build_options.context_dir = ".";
|
|
build_options.file = "example_Dockerfile";
|
|
|
|
println("Build options configured:");
|
|
println(` - Tag: ${build_options.tag}`);
|
|
println(` - Context: ${build_options.context_dir}`);
|
|
println(` - Dockerfile: ${build_options.file}`);
|
|
|
|
// Demonstrate commit options
|
|
println("\nDemonstrating commit options:");
|
|
let commit_options = bah_new_commit_options();
|
|
commit_options.format = "docker";
|
|
commit_options.squash = true;
|
|
commit_options.rm = true;
|
|
|
|
println("Commit options configured:");
|
|
println(` - Format: ${commit_options.format}`);
|
|
println(` - Squash: ${commit_options.squash}`);
|
|
println(` - Remove container: ${commit_options.rm}`);
|
|
|
|
// Demonstrate config options
|
|
println("\nDemonstrating config options:");
|
|
let config_options = bah_new_config_options();
|
|
config_options.author = "Rhai Example";
|
|
config_options.cmd = "/bin/sh -c 'echo Hello from Buildah'";
|
|
|
|
println("Config options configured:");
|
|
println(` - Author: ${config_options.author}`);
|
|
println(` - Command: ${config_options.cmd}`);
|
|
|
|
|
|
"Buildah operations script completed successfully!" |