This commit is contained in:
2025-04-05 06:21:50 +02:00
parent d336153247
commit 245aee12bf
9 changed files with 135 additions and 1306 deletions

View File

@@ -6,14 +6,76 @@ use super::container_types::{Container, ContainerStatus, ResourceUsage};
use serde_json;
impl Container {
/// Start the container
/// Start the container and verify it's running
///
/// # Returns
///
/// * `Result<CommandResult, NerdctlError>` - Command result or error
/// * `Result<CommandResult, NerdctlError>` - Command result or error with detailed information
pub fn start(&self) -> Result<CommandResult, NerdctlError> {
if let Some(container_id) = &self.container_id {
execute_nerdctl_command(&["start", container_id])
// First, try to start the container
let start_result = execute_nerdctl_command(&["start", container_id]);
// If the start command failed, return the error with details
if let Err(err) = &start_result {
return Err(NerdctlError::CommandFailed(
format!("Failed to start container {}: {}", container_id, err)
));
}
// Verify the container is actually running
match self.verify_running() {
Ok(true) => start_result,
Ok(false) => {
// Container started but isn't running - try to get more details
if let Ok(status) = self.status() {
Err(NerdctlError::CommandFailed(
format!("Container {} started but is not running. Status: {}, State: {}, Health: {}",
container_id,
status.status,
status.state,
status.health_status.unwrap_or_else(|| "N/A".to_string())
)
))
} else {
Err(NerdctlError::CommandFailed(
format!("Container {} started but is not running. Unable to get status details.",
container_id
)
))
}
},
Err(err) => {
// Failed to verify if container is running
Err(NerdctlError::CommandFailed(
format!("Container {} may have started, but verification failed: {}",
container_id, err
)
))
}
}
} else {
Err(NerdctlError::Other("No container ID available".to_string()))
}
}
/// Verify if the container is running
///
/// # Returns
///
/// * `Result<bool, NerdctlError>` - True if running, false if not running, error if verification failed
fn verify_running(&self) -> Result<bool, NerdctlError> {
if let Some(container_id) = &self.container_id {
// Use inspect to check if the container is running
let inspect_result = execute_nerdctl_command(&["inspect", "--format", "{{.State.Running}}", container_id]);
match inspect_result {
Ok(result) => {
let running = result.stdout.trim().to_lowercase() == "true";
Ok(running)
},
Err(err) => Err(err)
}
} else {
Err(NerdctlError::Other("No container ID available".to_string()))
}