All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 5s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 4s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m7s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 2m56s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 3m40s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m11s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 2s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s
80 lines
2.8 KiB
Rust
80 lines
2.8 KiB
Rust
|
|
/// # 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>,
|
|
pub net_in_total: Option<f64>,
|
|
pub net_out_total: Option<f64>,
|
|
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 {
|
|
/*
|
|
/// 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...
|
|
}
|
|
*/
|
|
|
|
/// 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<String, Box<dyn Error + Send + Sync>> {
|
|
Ok(container.ID)
|
|
}
|
|
|
|
/// 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)
|
|
}
|
|
} |