This commit is contained in:
2025-04-05 04:45:56 +02:00
parent 9f33c94020
commit e48063a79c
22 changed files with 2661 additions and 1384 deletions

View File

@@ -4,29 +4,36 @@
//! common container operations like creating containers, running commands,
//! and managing images.
use sal::virt::buildah::{self, BuildahError};
use sal::virt::buildah::{BuildahError, Builder};
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
// Step 1: Create a container from an image using the Builder
println!("\n=== Creating container from fedora:latest ===");
let result = buildah::from("fedora:latest")?;
let container_id = result.stdout.trim();
println!("Created container: {}", container_id);
let mut builder = Builder::new("my-fedora-container", "fedora:latest")?;
// Reset the builder to remove any existing container
println!("\n=== Resetting the builder to start fresh ===");
builder.reset()?;
// Create a new container (or continue with existing one)
println!("\n=== Creating container from fedora:latest ===");
builder = Builder::new("my-fedora-container", "fedora:latest")?;
println!("Created container: {}", builder.container_id().unwrap_or(&"unknown".to_string()));
// Step 2: Run a command in the container
println!("\n=== Installing nginx in container ===");
// Use chroot isolation to avoid BPF issues
let install_result = buildah::bah_run_with_isolation(container_id, "dnf install -y nginx", "chroot")?;
let install_result = builder.run_with_isolation("dnf install -y nginx", "chroot")?;
println!("{:#?}", install_result);
println!("Installation output: {}", install_result.stdout);
// Step 3: Copy a file into the container
println!("\n=== Copying configuration file to container ===");
buildah::bah_copy(container_id, "./example.conf", "/etc/example.conf").unwrap();
builder.copy("./example.conf", "/etc/example.conf")?;
// Step 4: Configure container metadata
println!("\n=== Configuring container metadata ===");
@@ -34,19 +41,18 @@ pub fn run_buildah_example() -> Result<(), BuildahError> {
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::bah_config(container_id, config_options)?;
buildah::config(container_id, config_options)?;
builder.config(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)?;
builder.commit(image_name)?;
println!("Created image: {}", image_name);
// Step 6: List images to verify our new image exists
println!("\n=== Listing images ===");
let images = buildah::images()?;
let images = Builder::images()?;
println!("Found {} images:", images.len());
for image in images {
println!(" ID: {}", image.id);
@@ -56,9 +62,10 @@ pub fn run_buildah_example() -> Result<(), BuildahError> {
println!();
}
// // Step 7: Clean up (optional in a real workflow)
// Step 7: Clean up (optional in a real workflow)
println!("\n=== Cleaning up ===");
buildah::image_remove(image_name).unwrap();
Builder::image_remove(image_name)?;
builder.remove()?;
println!("\nBuildah example workflow completed successfully!");
Ok(())
@@ -69,7 +76,7 @@ pub fn build_image_example() -> Result<(), BuildahError> {
println!("Building an image from a Containerfile...");
// Use the build function with tag, context directory, and isolation to avoid BPF issues
let result = buildah::bah_build(Some("my-app:latest"), ".", "example_Dockerfile", Some("chroot"))?;
let result = Builder::build(Some("my-app:latest"), ".", "example_Dockerfile", Some("chroot"))?;
println!("Build output: {}", result.stdout);
println!("Image built successfully!");
@@ -83,18 +90,18 @@ pub fn registry_operations_example() -> Result<(), BuildahError> {
// Pull an image
println!("\n=== Pulling an image ===");
buildah::image_pull("docker.io/library/alpine:latest", true)?;
Builder::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")?;
Builder::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)");
// println!("Builder::image_push(\"my-alpine:v1.0\", \"docker://registry.example.com/my-alpine:v1.0\", true)");
Ok(())
}

View File

@@ -0,0 +1,66 @@
//! Example of using the Git module with Rhai
//!
//! This example demonstrates how to use the Git module functions
//! through the Rhai scripting language.
use sal::rhai::{self, Engine};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new Rhai engine
let mut engine = Engine::new();
// Register SAL functions with the engine
rhai::register(&mut engine)?;
// Run a Rhai script that uses Git functions
let script = r#"
// Print a header
print("=== Testing Git Module Functions ===\n");
// Test git_list function
print("Listing git repositories...");
let repos = git_list();
print(`Found ${repos.len()} repositories`);
// Print the first few repositories
if repos.len() > 0 {
print("First few repositories:");
let count = if repos.len() > 3 { 3 } else { repos.len() };
for i in range(0, count) {
print(` - ${repos[i]}`);
}
}
// Test find_matching_repos function
if repos.len() > 0 {
print("\nTesting repository search...");
// Extract a part of the first repo name to search for
let repo_path = repos[0];
let parts = repo_path.split("/");
let repo_name = parts[parts.len() - 1];
print(`Searching for repositories containing "${repo_name}"`);
let matching = find_matching_repos(repo_name);
print(`Found ${matching.len()} matching repositories`);
for repo in matching {
print(` - ${repo}`);
}
// Check if a repository has changes
print("\nChecking for changes in repository...");
let has_changes = git_has_changes(repo_path);
print(`Repository ${repo_path} has changes: ${has_changes}`);
}
print("\n=== Git Module Test Complete ===");
"#;
// Evaluate the script
match engine.eval::<()>(script) {
Ok(_) => println!("Script executed successfully"),
Err(e) => eprintln!("Script execution error: {}", e),
}
Ok(())
}