sal/examples/buildah.rs
Maxime Van Hees 3f461334ec added example
2025-04-02 14:41:50 +02:00

129 lines
4.9 KiB
Rust

//! Example usage of the buildah module
//!
//! This file demonstrates how to use the buildah module to perform
//! common container operations like creating containers, running commands,
//! and managing images.
use sal::virt::buildah::{self, BuildahError};
use std::collections::HashMap;
/// Run a complete buildah workflow example
pub fn run_buildah_example() -> Result<(), BuildahError> {
println!("Starting buildah example workflow...");
// Step 1: Create a container from an image
println!("\n=== Creating container from fedora:latest ===");
let result = buildah::from("fedora:latest")?;
let container_id = result.stdout.trim();
println!("Created container: {}", container_id);
// Step 2: Run a command in the container
println!("\n=== Installing nginx in container ===");
let install_result = buildah::run(container_id, "dnf install -y nginx")?;
println!("Installation output: {}", install_result.stdout);
// Step 3: Copy a file into the container
println!("\n=== Copying configuration file to container ===");
// Note: This would require an actual file to exist
// buildah::copy(container_id, "./nginx.conf", "/etc/nginx/nginx.conf")?;
println!("For a real example, you would copy a configuration file here");
// Step 4: Configure container metadata
println!("\n=== Configuring container metadata ===");
let mut config_options = HashMap::new();
config_options.insert("port".to_string(), "80".to_string());
config_options.insert("label".to_string(), "maintainer=example@example.com".to_string());
config_options.insert("entrypoint".to_string(), "/usr/sbin/nginx".to_string());
buildah::config(container_id, config_options)?;
println!("Container configured");
// Step 5: Commit the container to create a new image
println!("\n=== Committing container to create image ===");
let image_name = "my-nginx:latest";
buildah::image_commit(container_id, image_name, Some("docker"), true, true)?;
println!("Created image: {}", image_name);
// Step 6: List images to verify our new image exists
println!("\n=== Listing images ===");
let images = buildah::images()?;
println!("Found {} images:", images.len());
for image in images {
println!(" ID: {}", image.id);
println!(" Names: {}", image.names.join(", "));
println!(" Size: {}", image.size);
println!(" Created: {}", image.created);
println!();
}
// Step 7: Clean up (optional in a real workflow)
println!("\n=== Cleaning up ===");
println!("In a real workflow, you might want to keep the image");
println!("To remove the image, you would run:");
println!("buildah::image_remove(\"{}\")", image_name);
println!("\nBuildah example workflow completed successfully!");
Ok(())
}
/// Demonstrate how to build an image from a Containerfile/Dockerfile
pub fn build_image_example() -> Result<(), BuildahError> {
println!("Building an image from a Containerfile...");
// This would typically use a command like:
// buildah build -t my-app:latest .
// For our example, we'll just show the command structure
let build_args = &["build", "-t", "my-app:latest", "."];
let result = buildah::execute_buildah_command(build_args)?;
println!("Build output: {}", result.stdout);
println!("Image built successfully!");
Ok(())
}
/// Example of pulling and pushing images
pub fn registry_operations_example() -> Result<(), BuildahError> {
println!("Demonstrating registry operations...");
// Pull an image
println!("\n=== Pulling an image ===");
buildah::image_pull("docker.io/library/alpine:latest", true)?;
println!("Image pulled successfully");
// Tag the image
println!("\n=== Tagging the image ===");
buildah::image_tag("alpine:latest", "my-alpine:v1.0")?;
println!("Image tagged successfully");
// Push an image (this would typically go to a real registry)
println!("\n=== Pushing an image (example only) ===");
println!("In a real scenario, you would push to a registry with:");
println!("buildah::image_push(\"my-alpine:v1.0\", \"docker://registry.example.com/my-alpine:v1.0\", true)");
Ok(())
}
/// Main function to run all examples
pub fn run_all_examples() -> Result<(), BuildahError> {
println!("=== BUILDAH MODULE EXAMPLES ===\n");
// Note: In a real application, you might want to run these
// examples individually or have proper error handling
// Uncomment these to run the examples
run_buildah_example()?;
// build_image_example()?;
// registry_operations_example()?;
println!("\nTo run these examples, uncomment the function calls in run_all_examples()");
println!("Note that these examples require buildah to be installed on your system");
println!("and may require root/sudo privileges depending on your setup.");
Ok(())
}
fn main() {
run_all_examples();
}