This commit is contained in:
2025-04-05 07:40:32 +02:00
parent c177ac5efb
commit 4c50d4b62c
6 changed files with 285 additions and 6 deletions

View File

@@ -6,6 +6,38 @@ use super::container_types::{Container, HealthCheck};
use super::health_check_script::prepare_health_check_command;
impl Container {
/// Reset the container configuration to defaults while keeping the name and image
///
/// # Returns
///
/// * `Self` - The container instance for method chaining
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
Self {
name,
container_id,
image,
config: std::collections::HashMap::new(),
ports: Vec::new(),
volumes: Vec::new(),
env_vars: std::collections::HashMap::new(),
network: None,
network_aliases: Vec::new(),
cpu_limit: None,
memory_limit: None,
memory_swap_limit: None,
cpu_shares: None,
restart_policy: None,
health_check: None,
detach: false,
snapshotter: None,
}
}
/// Add a port mapping
///
/// # Arguments

View File

@@ -7,12 +7,54 @@ use serde_json;
impl Container {
/// Start the container and verify it's running
/// If the container hasn't been created yet, it will be created automatically.
///
/// # Returns
///
/// * `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 {
// If container_id is None, we need to create the container first
let container = if self.container_id.is_none() {
// Check if we have an image specified
if self.image.is_none() {
return Err(NerdctlError::Other("No image specified for container creation".to_string()));
}
// Clone self and create the container
println!("Container not created yet. Creating container from image...");
// First, try to pull the image if it doesn't exist locally
let image = self.image.as_ref().unwrap();
match execute_nerdctl_command(&["image", "inspect", image]) {
Err(_) => {
println!("Image '{}' not found locally. Pulling image...", image);
if let Err(e) = execute_nerdctl_command(&["pull", image]) {
return Err(NerdctlError::CommandFailed(
format!("Failed to pull image '{}': {}", image, e)
));
}
println!("Image '{}' pulled successfully.", image);
},
Ok(_) => {
println!("Image '{}' found locally.", image);
}
}
// Now create the container
match self.clone().build() {
Ok(built) => built,
Err(e) => {
return Err(NerdctlError::CommandFailed(
format!("Failed to create container from image '{}': {}", image, e)
));
}
}
} else {
// Container already has an ID, use it as is
self.clone()
};
if let Some(container_id) = &container.container_id {
// First, try to start the container
let start_result = execute_nerdctl_command(&["start", container_id]);
@@ -24,14 +66,14 @@ impl Container {
}
// Verify the container is actually running
match self.verify_running() {
match container.verify_running() {
Ok(true) => start_result,
Ok(false) => {
// Container started but isn't running - get detailed information
let mut error_message = format!("Container {} started but is not running.", container_id);
// Get container status
if let Ok(status) = self.status() {
if let Ok(status) = container.status() {
error_message.push_str(&format!("\nStatus: {}, State: {}, Health: {}",
status.status,
status.state,
@@ -69,7 +111,7 @@ impl Container {
}
}
} else {
Err(NerdctlError::Other("No container ID available".to_string()))
Err(NerdctlError::Other("Failed to create container. No container ID available.".to_string()))
}
}