added commentation
This commit is contained in:
@@ -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;
|
||||
|
Reference in New Issue
Block a user