added commentation

This commit is contained in:
2025-10-01 12:07:53 +02:00
parent d994be757e
commit 8c49a63a50
13 changed files with 570 additions and 56 deletions

View File

@@ -1,3 +1,8 @@
//! Docker container utilities for WatcherAgent
//!
//! Provides functions to list and process Docker containers using the Bollard library.
//!
use crate::models::DockerContainer;
use bollard::query_parameters::{ListContainersOptions};
@@ -6,6 +11,13 @@ use bollard::Docker;
/// Returns a list of available Docker containers.
///
/// # Arguments
/// * `docker` - Reference to a Bollard Docker client.
///
/// # Returns
/// * `Vec<DockerContainer>` - Vector of Docker container info.
pub async fn get_available_container(docker: &Docker) -> Vec<DockerContainer> {
println!("=== DOCKER CONTAINER LIST ===");
@@ -67,32 +79,15 @@ pub async fn get_available_container(docker: &Docker) -> Vec<DockerContainer> {
containers_list
}
/*pub fn extract_client_container_id(line: &str) -> Option<String> {
// Split by slashes and take the last part
if let Some(last_part) = line.split('/').last() {
let last_part = last_part.trim();
// Remove common suffixes
let clean_id = last_part
.trim_end_matches(".scope")
.trim_start_matches("docker-")
.trim_start_matches("crio-")
.trim_start_matches("containerd-");
// Check if it looks like a container ID (hex characters)
if clean_id.chars().all(|c| c.is_ascii_hexdigit()) && clean_id.len() >= 12 {
return Some(clean_id.to_string());
}
// If it's not pure hex, try to extract hex sequence
let hex_part: String = clean_id.chars()
.take_while(|c| c.is_ascii_hexdigit())
.collect();
if hex_part.len() >= 12 {
return Some(hex_part);
}
}
None
}*/
/*
/// Extracts a Docker container ID from a string line.
///
/// # Arguments
/// * `line` - The input string containing a container ID or related info.
///
/// # Returns
/// * `Option<String>` - The extracted container ID if found.
pub fn extract_client_container_id(line: &str) -> Option<String> {
// ...existing code...
}
*/

View File

@@ -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)
}
}

View File

@@ -1,3 +1,8 @@
//! Server-client communication utilities for WatcherAgent
//!
//! Handles server commands, Docker image updates, and container management using the Bollard library.
//!
use crate::models::{DockerContainer, ServerMessage};
use crate::docker::container::{get_available_container};
@@ -6,6 +11,14 @@ use bollard::Docker;
use bollard::query_parameters::{CreateImageOptions, RestartContainerOptions, InspectContainerOptions};
use futures_util::StreamExt;
/// Handles a message from the backend server and dispatches the appropriate action.
///
/// # Arguments
/// * `docker` - Reference to a Bollard Docker client.
/// * `msg` - The server message to handle.
///
/// # Returns
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if handled successfully, error otherwise.
pub async fn handle_server_message(docker: &Docker, msg: ServerMessage) -> Result<(), Box<dyn Error + Send + Sync>> {
let msg = msg.clone();
println!("Handling server message: {:?}", msg);
@@ -40,6 +53,14 @@ pub async fn handle_server_message(docker: &Docker, msg: ServerMessage) -> Resul
}
}
/// Pulls a new Docker image and restarts the current container.
///
/// # Arguments
/// * `docker` - Reference to a Bollard Docker client.
/// * `image` - The name of the Docker image to pull.
///
/// # Returns
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if updated successfully, error otherwise.
pub async fn update_docker_image(docker: &Docker, image: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
println!("Updating to {}", image);
@@ -74,6 +95,13 @@ pub async fn update_docker_image(docker: &Docker, image: &str) -> Result<(), Box
Ok(())
}
/// Finds the Docker container running the agent by image name.
///
/// # Arguments
/// * `docker` - Reference to a Bollard Docker client.
///
/// # Returns
/// * `Result<Option<DockerContainer>, Box<dyn Error + Send + Sync>>` - The agent's container info if found.
pub async fn get_client_container(docker: &Docker) -> Result<Option<DockerContainer>, Box<dyn Error + Send + Sync>> {
let containers = get_available_container(docker).await;
let client_image = "watcher-agent";
@@ -86,6 +114,13 @@ pub async fn get_client_container(docker: &Docker) -> Result<Option<DockerContai
}
}
/// Restarts the agent's own Docker container.
///
/// # 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>> {
if let Ok(Some(container)) = get_client_container(docker).await {
let container_id = container.clone().ID;