This commit is contained in:
2025-04-05 09:36:54 +02:00
parent 4c50d4b62c
commit 78db13d738
15 changed files with 1119 additions and 248 deletions

View File

@@ -1,11 +1,26 @@
// Basic buildah operations for container management
use std::process::Command;
use crate::process::CommandResult;
use super::BuildahError;
use super::{BuildahError, Builder};
/// Execute a buildah command and return the result
///
/// # Arguments
///
/// * `args` - The command arguments
///
/// # Returns
///
/// * `Result<CommandResult, BuildahError>` - Command result or error
pub fn execute_buildah_command(args: &[&str]) -> Result<CommandResult, BuildahError> {
// Get the current thread-local Builder instance if available
let debug = thread_local_debug();
if debug {
println!("Executing buildah command: buildah {}", args.join(" "));
}
let output = Command::new("buildah")
.args(args)
.output();
@@ -22,15 +37,93 @@ pub fn execute_buildah_command(args: &[&str]) -> Result<CommandResult, BuildahEr
code: output.status.code().unwrap_or(-1),
};
if debug {
if !result.stdout.is_empty() {
println!("Command stdout: {}", result.stdout);
}
if !result.stderr.is_empty() {
println!("Command stderr: {}", result.stderr);
}
}
if result.success {
if debug {
println!("Command succeeded with code {}", result.code);
}
Ok(result)
} else {
Err(BuildahError::CommandFailed(format!("Command failed with code {}: {}",
result.code, result.stderr.trim())))
let error_msg = format!("Command failed with code {}: {}",
result.code, result.stderr.trim());
if debug {
println!("Command failed: {}", error_msg);
}
Err(BuildahError::CommandFailed(error_msg))
}
},
Err(e) => {
if debug {
println!("Command execution failed: {}", e);
}
Err(BuildahError::CommandExecutionFailed(e))
}
}
}
// Thread-local storage for debug flag
thread_local! {
static DEBUG: std::cell::RefCell<bool> = std::cell::RefCell::new(false);
}
/// Set the debug flag for the current thread
pub fn set_thread_local_debug(debug: bool) {
DEBUG.with(|cell| {
*cell.borrow_mut() = debug;
});
}
/// Get the debug flag for the current thread
pub fn thread_local_debug() -> bool {
DEBUG.with(|cell| {
*cell.borrow()
})
}
/// Execute a buildah command with debug output
///
/// # Arguments
///
/// * `args` - The command arguments
/// * `builder` - Reference to a Builder instance for debug output
///
/// # Returns
///
/// * `Result<CommandResult, BuildahError>` - Command result or error
pub fn execute_buildah_command_with_debug(args: &[&str], builder: &Builder) -> Result<CommandResult, BuildahError> {
if builder.debug() {
println!("Executing buildah command: buildah {}", args.join(" "));
}
let result = execute_buildah_command(args);
if builder.debug() {
match &result {
Ok(cmd_result) => {
if !cmd_result.stdout.is_empty() {
println!("Command stdout: {}", cmd_result.stdout);
}
if !cmd_result.stderr.is_empty() {
println!("Command stderr: {}", cmd_result.stderr);
}
println!("Command succeeded with code {}", cmd_result.code);
},
Err(e) => {
println!("Command failed: {}", e);
}
}
}
result
}

View File

@@ -7,6 +7,7 @@ use super::health_check_script::prepare_health_check_command;
impl Container {
/// Reset the container configuration to defaults while keeping the name and image
/// If the container exists, it will be stopped and removed.
///
/// # Returns
///
@@ -14,12 +15,22 @@ impl Container {
pub fn reset(mut self) -> Self {
let name = self.name;
let image = self.image.clone();
let container_id = self.container_id.clone();
// Create a new container with just the name, image, and container_id
// If container exists, stop and remove it
if let Some(container_id) = &self.container_id {
println!("Container exists. Stopping and removing container '{}'...", name);
// Try to stop the container
let _ = execute_nerdctl_command(&["stop", container_id]);
// Try to remove the container
let _ = execute_nerdctl_command(&["rm", container_id]);
}
// Create a new container with just the name and image, but no container_id
Self {
name,
container_id,
container_id: None, // Reset container_id to None since we removed the container
image,
config: std::collections::HashMap::new(),
ports: Vec::new(),