sal/rhaiexamples/04_buildah_operations.rhai
2025-04-04 18:21:16 +02:00

112 lines
4.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 != "" {
// This try/catch block will handle any errors that might occur
// when trying to use buildah functions
try {
let images = buildah_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;
}
} catch(err) {
println(`Error accessing buildah: ${err}`);
}
} else {
println("Buildah is not installed. Skipping container image operations.");
}
// Remove the duplicate code that was causing the error
// The following operations are commented out as they require buildah to be installed
// and may need root privileges. Uncomment them if you want to try them out.
// Create a container from an image
// println("\nCreating a container from alpine image:");
// let container = from("alpine:latest");
// println(`Container ID: ${container.stdout.trim()}`);
// Run a command in the container
// println("\nRunning a command in the container:");
// let run_result = run(container.stdout.trim(), "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_command(`echo "Test content" > ${test_file}`);
// let add_result = add(container.stdout.trim(), 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 = commit(container.stdout.trim(), "my-custom-image:latest");
// println(`Commit result: ${commit_result.success}`);
// Remove the container
// println("\nRemoving the container:");
// let remove_result = remove(container.stdout.trim());
// println(`Remove result: ${remove_result.success}`);
// Clean up the test file
// delete(test_file);
// Only demonstrate buildah options if buildah is installed
if buildah_exists != "" {
try {
// Demonstrate build options
println("\nDemonstrating build options:");
let build_options = buildah_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 = buildah_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 = buildah_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}`);
} catch(err) {
println(`Error accessing buildah options: ${err}`);
}
} else {
println("\nSkipping buildah options demonstration since buildah is not installed.");
}
"Buildah operations script completed successfully!"