This commit is contained in:
2025-04-04 21:51:31 +02:00
parent 5b006ff328
commit 9f33c94020
19 changed files with 1221 additions and 88 deletions

View File

@@ -44,11 +44,8 @@ println(appended_content);
// 4. Demonstrate multiple appends
println(`\n--- Demonstrating multiple appends ---`);
for i in range(1, 4) {
// Get timestamp and ensure it's properly trimmed
let result = run("date");
let timestamp = result.stdout.trim();
println(`Timestamp: ${timestamp}`);
let log_entry = `Log entry #${i} at ${timestamp}\n`;
// Use a simple counter instead of timestamp to avoid issues
let log_entry = `Log entry #${i} - appended at iteration ${i}\n`;
file_write_append(append_file, log_entry);
println(`Added log entry #${i}`);
}

View File

@@ -0,0 +1,100 @@
// 07_nerdctl_operations.rhai
// Demonstrates container operations using SAL's nerdctl integration
// Note: This script requires nerdctl to be installed and may need root privileges
// Check if nerdctl is installed
let nerdctl_exists = which("nerdctl");
println(`Nerdctl exists: ${nerdctl_exists}`);
// List available images (only if nerdctl is installed)
println("Listing available container images:");
if nerdctl_exists == "" {
println("Nerdctl is not installed. Please install it first.");
// You can use the install_nerdctl.rhai script to install nerdctl
// EXIT
}
// List images
let images_result = nerdctl_images();
println(`Images result: success=${images_result.success}, code=${images_result.code}`);
println(`Images output:\n${images_result.stdout}`);
// Pull an image if needed
println("\nPulling alpine:latest image:");
let pull_result = nerdctl_image_pull("alpine:latest");
println(`Pull result: success=${pull_result.success}, code=${pull_result.code}`);
println(`Pull output: ${pull_result.stdout}`);
// Create a container using the simple run function
println("\nCreating a container from alpine image:");
let container_name = "rhai-nerdctl-test";
let container = nerdctl_run("alpine:latest", container_name);
println(`Container result: success=${container.success}, code=${container.code}`);
println(`Container stdout: "${container.stdout}"`);
println(`Container stderr: "${container.stderr}"`);
// Run a command in the container
println("\nRunning a command in the container:");
let run_result = nerdctl_exec(container_name, "echo 'Hello from container'");
println(`Command output: ${run_result.stdout}`);
// Create a test file and copy it to the container
println("\nAdding a file to the container:");
let test_file = "test_file.txt";
run(`echo "Test content" > ${test_file}`);
let copy_result = nerdctl_copy(test_file, `${container_name}:/`);
println(`Copy result: ${copy_result.success}`);
// Commit the container to create a new image
println("\nCommitting the container to create a new image:");
let image_name = "my-custom-alpine:latest";
let commit_result = nerdctl_image_commit(container_name, image_name);
println(`Commit result: ${commit_result.success}`);
// Stop and remove the container
println("\nStopping the container:");
let stop_result = nerdctl_stop(container_name);
println(`Stop result: ${stop_result.success}`);
println("\nRemoving the container:");
let remove_result = nerdctl_remove(container_name);
println(`Remove result: ${remove_result.success}`);
// Clean up the test file
delete(test_file);
// Demonstrate run options
println("\nDemonstrating run options:");
let run_options = nerdctl_new_run_options();
run_options.name = "rhai-nerdctl-options-test";
run_options.detach = true;
run_options.ports = ["8080:80"];
run_options.snapshotter = "native";
println("Run options configured:");
println(` - Name: ${run_options.name}`);
println(` - Detach: ${run_options.detach}`);
println(` - Ports: ${run_options.ports}`);
println(` - Snapshotter: ${run_options.snapshotter}`);
// Create a container with options
println("\nCreating a container with options:");
let container_with_options = nerdctl_run_with_options("alpine:latest", run_options);
println(`Container with options result: ${container_with_options.success}`);
// Clean up the container with options
println("\nCleaning up the container with options:");
nerdctl_stop("rhai-nerdctl-options-test");
nerdctl_remove("rhai-nerdctl-options-test");
// List all containers (including stopped ones)
println("\nListing all containers:");
let list_result = nerdctl_list(true);
println(`List result: ${list_result.stdout}`);
// Remove the custom image
println("\nRemoving the custom image:");
let image_remove_result = nerdctl_image_remove(image_name);
println(`Image remove result: ${image_remove_result.success}`);
"Nerdctl operations script completed successfully!"

View File

@@ -0,0 +1,150 @@
// 08_nerdctl_web_server.rhai
// Demonstrates a complete workflow to set up a web server using nerdctl
// Note: This script requires nerdctl to be installed and may need root privileges
// Check if nerdctl is installed
let nerdctl_exists = which("nerdctl");
if nerdctl_exists == "" {
println("Nerdctl is not installed. Please install it first.");
// You can use the install_nerdctl.rhai script to install nerdctl
// EXIT
}
println("Starting nerdctl web server workflow...");
// Step 1: Pull the nginx image
println("\n=== Pulling nginx:latest image ===");
let pull_result = nerdctl_image_pull("nginx:latest");
if !pull_result.success {
println(`Failed to pull nginx image: ${pull_result.stderr}`);
// EXIT
}
println("Successfully pulled nginx:latest image");
// Step 2: Create a custom nginx configuration file
println("\n=== Creating custom nginx configuration ===");
let config_content = `
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
`;
let config_file = "custom-nginx.conf";
run(`echo "${config_content}" > ${config_file}`);
println("Created custom nginx configuration file");
// Step 3: Create a custom index.html file
println("\n=== Creating custom index.html ===");
let html_content = `
<!DOCTYPE html>
<html>
<head>
<title>Rhai Nerdctl Demo</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 40px;
line-height: 1.6;
color: #333;
}
h1 {
color: #0066cc;
}
</style>
</head>
<body>
<h1>Hello from Rhai Nerdctl!</h1>
<p>This page is served by an Nginx container created using the Rhai nerdctl wrapper.</p>
<p>Current time: ${now()}</p>
</body>
</html>
`;
let html_file = "index.html";
run(`echo "${html_content}" > ${html_file}`);
println("Created custom index.html file");
// Step 4: Create and run the nginx container with options
println("\n=== Creating nginx container ===");
let container_name = "rhai-nginx-demo";
// First, try to remove any existing container with the same name
let _ = nerdctl_remove(container_name);
let run_options = nerdctl_new_run_options();
run_options.name = container_name;
run_options.detach = true;
run_options.ports = ["8080:80"];
run_options.snapshotter = "native";
let container = nerdctl_run_with_options("nginx:latest", run_options);
if !container.success {
println(`Failed to create container: ${container.stderr}`);
// EXIT
}
println(`Successfully created container: ${container_name}`);
// Step 5: Copy the custom files to the container
println("\n=== Copying custom files to container ===");
let copy_config = nerdctl_copy(config_file, `${container_name}:/etc/nginx/conf.d/default.conf`);
if !copy_config.success {
println(`Failed to copy config file: ${copy_config.stderr}`);
}
let copy_html = nerdctl_copy(html_file, `${container_name}:/usr/share/nginx/html/index.html`);
if !copy_html.success {
println(`Failed to copy HTML file: ${copy_html.stderr}`);
}
println("Successfully copied custom files to container");
// Step 6: Restart the container to apply changes
println("\n=== Restarting container to apply changes ===");
let stop_result = nerdctl_stop(container_name);
if !stop_result.success {
println(`Failed to stop container: ${stop_result.stderr}`);
}
let start_result = nerdctl_exec(container_name, "nginx -s reload");
if !start_result.success {
println(`Failed to reload nginx: ${start_result.stderr}`);
}
println("Successfully restarted container");
// Step 7: Commit the container to create a custom image
println("\n=== Committing container to create custom image ===");
let image_name = "rhai-nginx-custom:latest";
let commit_result = nerdctl_image_commit(container_name, image_name);
if !commit_result.success {
println(`Failed to commit container: ${commit_result.stderr}`);
}
println(`Successfully created custom image: ${image_name}`);
// Step 8: Display information about the running container
println("\n=== Container Information ===");
println("The nginx web server is now running.");
println("You can access it at: http://localhost:8080");
println("Container name: " + container_name);
println("Custom image: " + image_name);
// Step 9: Clean up (commented out for demonstration purposes)
// println("\n=== Cleaning up ===");
// nerdctl_stop(container_name);
// nerdctl_remove(container_name);
// nerdctl_image_remove(image_name);
// delete(config_file);
// delete(html_file);
println("\nNerdctl web server workflow completed successfully!");
println("The web server is running at http://localhost:8080");
println("To clean up, run the following commands:");
println(` nerdctl stop ${container_name}`);
println(` nerdctl rm ${container_name}`);
println(` nerdctl rmi ${image_name}`);
"Nerdctl web server script completed successfully!"

View File

@@ -0,0 +1,66 @@
// nerdctl_test.rhai
// Tests the nerdctl wrapper functionality without requiring a running containerd daemon
// Check if nerdctl is installed
let nerdctl_exists = which("nerdctl");
println(`Nerdctl exists: ${nerdctl_exists}`);
// Test creating run options
println("\nTesting run options creation:");
let run_options = new_run_options();
println(`Default run options created: ${run_options}`);
println(`- name: ${run_options.name}`);
println(`- detach: ${run_options.detach}`);
println(`- ports: ${run_options.ports}`);
println(`- snapshotter: ${run_options.snapshotter}`);
// Modify run options
println("\nModifying run options:");
run_options.name = "test-container";
run_options.detach = false;
run_options.ports = ["8080:80", "8443:443"];
run_options.snapshotter = "overlayfs";
println(`Modified run options: ${run_options}`);
println(`- name: ${run_options.name}`);
println(`- detach: ${run_options.detach}`);
println(`- ports: ${run_options.ports}`);
println(`- snapshotter: ${run_options.snapshotter}`);
// Test function availability
println("\nTesting function availability:");
let functions = [
"nerdctl_run",
"nerdctl_run_with_name",
"nerdctl_run_with_port",
"nerdctl_exec",
"nerdctl_copy",
"nerdctl_stop",
"nerdctl_remove",
"nerdctl_list",
"nerdctl_images",
"nerdctl_image_remove",
"nerdctl_image_push",
"nerdctl_image_tag",
"nerdctl_image_pull",
"nerdctl_image_commit",
"nerdctl_image_build"
];
// Try to access each function (this will throw an error if the function doesn't exist)
for func in functions {
let exists = is_function_registered(func);
println(`Function ${func} registered: ${exists}`);
}
// Helper function to check if a function is registered
fn is_function_registered(name) {
try {
// This will throw an error if the function doesn't exist
eval(`${name}`);
return true;
} catch {
return false;
}
}
"Nerdctl wrapper test completed successfully!"