added commentation
This commit is contained in:
@@ -1,9 +1,27 @@
|
||||
|
||||
//! # Docker Module
|
||||
//!
|
||||
//! This module provides Docker integration for WatcherAgent, including container enumeration, statistics, and lifecycle management.
|
||||
//!
|
||||
//! ## Responsibilities
|
||||
//! - **Container Management:** Lists, inspects, and manages Docker containers relevant to the agent.
|
||||
//! - **Statistics Aggregation:** Collects network and CPU statistics for all managed containers.
|
||||
//! - **Lifecycle Operations:** Supports container restart and ID lookup for agent self-management.
|
||||
//!
|
||||
pub mod container;
|
||||
pub mod serverclientcomm;
|
||||
|
||||
use std::error::Error;
|
||||
use crate::models::DockerContainer;
|
||||
|
||||
|
||||
/// Aggregated Docker statistics for all managed containers.
|
||||
///
|
||||
/// # Fields
|
||||
/// - `number`: Number of running containers (optional)
|
||||
/// - `net_in_total`: Total network receive rate in **bytes per second (B/s)** (optional)
|
||||
/// - `net_out_total`: Total network transmit rate in **bytes per second (B/s)** (optional)
|
||||
/// - `dockers`: List of [`DockerContainer`] statistics (optional)
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DockerInfo {
|
||||
pub number: Option<u16>,
|
||||
@@ -12,33 +30,51 @@ pub struct DockerInfo {
|
||||
pub dockers: Option<Vec<DockerContainer>>,
|
||||
}
|
||||
|
||||
|
||||
impl DockerInfo {
|
||||
/// Collects Docker statistics for all managed containers.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<DockerInfo, Box<dyn Error + Send + Sync>>` - Aggregated Docker statistics or error if collection fails.
|
||||
pub async fn collect() -> Result<Self, Box<dyn Error + Send + Sync>> {
|
||||
Ok(Self { number: None, net_in_total: None, net_out_total: None, dockers: None })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl DockerContainer {
|
||||
/*pub async fn restart_container(docker: &Docker) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
if let Ok(container_id) = std::env::var("HOSTNAME") {
|
||||
println!("Restarting container {}", container_id);
|
||||
if let Err(e) = docker.restart_container(&container_id, Some(RestartContainerOptions { signal: None, t: Some(0) }))
|
||||
.await
|
||||
{
|
||||
eprintln!("Failed to restart container: {}", e);
|
||||
}
|
||||
} else {
|
||||
eprintln!("No container ID found (HOSTNAME not set?)");
|
||||
}
|
||||
/*
|
||||
/// Restarts the specified Docker container by ID.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `docker` - Reference to a Bollard Docker client
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if restarted successfully, error otherwise.
|
||||
pub async fn restart_container(docker: &Docker) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
// ...existing code...
|
||||
}
|
||||
*/
|
||||
|
||||
Ok(())
|
||||
}*/
|
||||
|
||||
pub async fn get_docker_container_id (container: DockerContainer) -> Result<u32, Box<dyn Error + Send + Sync>> {
|
||||
/// Returns the container ID for a given [`DockerContainer`].
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `container` - Reference to a [`DockerContainer`]
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<u32, Box<dyn Error + Send + Sync>>` - Container ID as integer.
|
||||
pub async fn get_docker_container_id(container: DockerContainer) -> Result<u32, Box<dyn Error + Send + Sync>> {
|
||||
Ok(container.ID)
|
||||
}
|
||||
|
||||
pub async fn get_docker_container_image (container: DockerContainer) -> Result<String, Box<dyn Error + Send + Sync>> {
|
||||
/// Returns the image name for a given [`DockerContainer`].
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `container` - Reference to a [`DockerContainer`]
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<String, Box<dyn Error + Send + Sync>>` - Image name as string.
|
||||
pub async fn get_docker_container_image(container: DockerContainer) -> Result<String, Box<dyn Error + Send + Sync>> {
|
||||
Ok(container.image)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user