This commit is contained in:
2025-04-02 08:55:54 +02:00
parent c7908cb6e4
commit 6cc05ad2eb
6 changed files with 39 additions and 764 deletions

220
src/virt/buildah/images.rs Normal file
View File

@@ -0,0 +1,220 @@
use std::process::Command;
use crate::virt::buildah::execute_buildah_command;
/// List images in local storage
///
/// # Returns
/// * Array of image details on success or error details
pub fn images() -> Dynamic {
let result = execute_buildah_command(&["images", "--format", "json"]);
let result_map = match result.clone().try_cast::<Map>() {
Some(map) => map,
None => {
return create_result("", "Failed to convert result to map", false, -1);
}
};
let success = match result_map.get("success") {
Some(val) => val.as_bool().unwrap_or(false),
None => false,
};
if success {
let output = match result_map.get("stdout") {
Some(val) => val.to_string(),
None => "".to_string(),
};
// Try to parse the JSON output
match serde_json::from_str::<serde_json::Value>(&output) {
Ok(json) => {
if let serde_json::Value::Array(images) = json {
let mut image_array = Array::new();
for image in images {
let mut image_map = Map::new();
if let Some(id) = image.get("id").and_then(|v| v.as_str()) {
image_map.insert("id".into(), Dynamic::from(id.to_string()));
}
if let Some(name) = image.get("names").and_then(|v| v.as_array()) {
let mut names_array = Array::new();
for name_value in name {
if let Some(name_str) = name_value.as_str() {
names_array.push(Dynamic::from(name_str.to_string()));
}
}
image_map.insert("names".into(), Dynamic::from(names_array));
}
if let Some(size) = image.get("size").and_then(|v| v.as_str()) {
image_map.insert("size".into(), Dynamic::from(size.to_string()));
}
if let Some(created) = image.get("created").and_then(|v| v.as_str()) {
image_map.insert("created".into(), Dynamic::from(created.to_string()));
}
image_array.push(Dynamic::from(image_map));
}
let mut result_map = match result.clone().try_cast::<Map>() {
Some(map) => map,
None => Map::new(),
};
result_map.insert("images".into(), Dynamic::from(image_array));
Dynamic::from(result_map)
} else {
create_result(
"",
"Failed to parse image list: Expected JSON array",
false,
-1
)
}
},
Err(e) => {
create_result(
"",
&format!("Failed to parse image list JSON: {}", e),
false,
-1
)
}
}
} else {
result
}
}
/// Remove one or more images
///
/// # Arguments
/// * `image` - Image ID or name
///
/// # Returns
/// * Success or error details
pub fn image_remove(image: &str) -> Dynamic {
execute_buildah_command(&["rmi", image])
}
/// Push an image to a registry
///
/// # Arguments
/// * `image` - Image name
/// * `destination` - Destination (e.g., "docker://registry.example.com/myimage:latest")
/// * `tls_verify` - Whether to verify TLS (default: true)
///
/// # Returns
/// * Success or error details
pub fn image_push(image: &str, destination: &str, tls_verify: bool) -> Dynamic {
let mut args = vec!["push"];
if !tls_verify {
args.push("--tls-verify=false");
}
args.push(image);
args.push(destination);
execute_buildah_command(&args)
}
/// Add an additional name to a local image
///
/// # Arguments
/// * `image` - Image ID or name
/// * `new_name` - New name for the image
///
/// # Returns
/// * Success or error details
pub fn image_tag(image: &str, new_name: &str) -> Dynamic {
execute_buildah_command(&["tag", image, new_name])
}
/// Pull an image from a registry
///
/// # Arguments
/// * `image` - Image name
/// * `tls_verify` - Whether to verify TLS (default: true)
///
/// # Returns
/// * Success or error details
pub fn image_pull(image: &str, tls_verify: bool) -> Dynamic {
let mut args = vec!["pull"];
if !tls_verify {
args.push("--tls-verify=false");
}
args.push(image);
execute_buildah_command(&args)
}
/// Commit a container to an image
///
/// # Arguments
/// * `container` - Container ID or name
/// * `image_name` - New name for the image
/// * `format` - Optional, format to use for the image (oci or docker)
/// * `squash` - Whether to squash layers
/// * `rm` - Whether to remove the container after commit
///
/// # Returns
/// * Success or error details
pub fn image_commit(container: &str, image_name: &str, format: Option<&str>, squash: bool, rm: bool) -> Dynamic {
let mut args = vec!["commit"];
if let Some(format_str) = format {
args.push("--format");
args.push(format_str);
}
if squash {
args.push("--squash");
}
if rm {
args.push("--rm");
}
args.push(container);
args.push(image_name);
execute_buildah_command(&args)
}
/// Container configuration options
///
/// # Arguments
/// * `container` - Container ID or name
/// * `options` - Map of configuration options
///
/// # Returns
/// * Success or error details
pub fn config(container: &str, options: Map) -> Dynamic {
let mut args_owned: Vec<String> = Vec::new();
args_owned.push("config".to_string());
// Process options map
for (key, value) in options.iter() {
let option_name = format!("--{}", key);
args_owned.push(option_name);
if !value.is_unit() {
args_owned.push(value.to_string());
}
}
args_owned.push(container.to_string());
// Convert Vec<String> to Vec<&str> for execute_buildah_command
let args: Vec<&str> = args_owned.iter().map(|s| s.as_str()).collect();
execute_buildah_command(&args)
}