// 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}`); // Create a builder object println("\nCreating a builder object:"); let container_name = "my-container-example"; // Create a new builder let builder = bah_new(container_name, "alpine:latest"); // Reset the builder to remove any existing container println("\nResetting the builder to start fresh:"); let reset_result = builder.reset(); println(`Reset result: ${reset_result}`); // Create a new container after reset println("\nCreating a new container after reset:"); builder = bah_new(container_name, "alpine:latest"); println(`Container created with ID: ${builder.container_id}`); println(`Builder created with name: ${builder.name}, image: ${builder.image}`); // List available images (only if buildah is installed) println("\nListing available container images:"); // if ! buildah_exists != "" { // //EXIT // } let images = builder.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; } //Run a command in the container println("\nRunning a command in the container:"); let run_result = builder.run("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"; // Create the test file using Rhai's file_write function file_write(test_file, "Test content"); println(`Created test file: ${test_file}`); println(`Created test file: ${test_file}`); let add_result = builder.add(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 = builder.commit("my-custom-image:latest"); println(`Commit result: ${commit_result.success}`); //Remove the container println("\nRemoving the container:"); let remove_result = builder.remove(); println(`Remove result: ${remove_result.success}`); //Clean up the test file delete(test_file); // Demonstrate static methods println("\nDemonstrating static methods:"); println("Building an image from a Dockerfile:"); let build_result = builder.build("example-image:latest", ".", "example_Dockerfile", "chroot"); println(`Build result: ${build_result.success}`); // Pull an image println("\nPulling an image:"); let pull_result = builder.image_pull("alpine:latest", true); println(`Pull result: ${pull_result.success}`); // Skip commit options demonstration since we removed the legacy functions println("\nSkipping commit options demonstration (legacy functions removed)"); // Demonstrate config method println("\nDemonstrating config method:"); // Create a new container for config demonstration println("Creating a new container for config demonstration:"); builder = bah_new("config-demo-container", "alpine:latest"); println(`Container created with ID: ${builder.container_id}`); let config_options = #{ "author": "Rhai Example", "cmd": "/bin/sh -c 'echo Hello from Buildah'" }; let config_result = builder.config(config_options); println(`Config result: ${config_result.success}`); // Clean up the container println("Removing the config demo container:"); builder.remove(); "Buildah operations script completed successfully!"