From d7a58e00da46c8a5541df5942015bab2a84bc56c Mon Sep 17 00:00:00 2001 From: donpat1to Date: Mon, 29 Sep 2025 14:54:03 +0200 Subject: [PATCH] added listing all running containers --- WatcherAgent/src/api.rs | 2 +- WatcherAgent/src/docker/container.rs | 67 ++++++++++++++----- WatcherAgent/src/docker/mod.rs | 17 +++-- .../src/{ => docker}/serverclientcomm.rs | 0 WatcherAgent/src/main.rs | 5 +- WatcherAgent/src/models.rs | 6 +- 6 files changed, 67 insertions(+), 30 deletions(-) rename WatcherAgent/src/{ => docker}/serverclientcomm.rs (100%) diff --git a/WatcherAgent/src/api.rs b/WatcherAgent/src/api.rs index a663041..6dd99ff 100644 --- a/WatcherAgent/src/api.rs +++ b/WatcherAgent/src/api.rs @@ -2,7 +2,7 @@ use std::time::Duration; use crate::hardware::HardwareInfo; use crate::models::{HeartbeatDto, IdResponse, MetricDto, RegistrationDto, ServerMessage, Acknowledgment}; -use crate::serverclientcomm::handle_server_message; +use crate::docker::serverclientcomm::handle_server_message; use anyhow::Result; use reqwest::{Client, StatusCode}; diff --git a/WatcherAgent/src/docker/container.rs b/WatcherAgent/src/docker/container.rs index 83d2734..de66eb6 100644 --- a/WatcherAgent/src/docker/container.rs +++ b/WatcherAgent/src/docker/container.rs @@ -1,36 +1,69 @@ +use crate::models::DockerContainer; + use bollard::query_parameters::{ListContainersOptions}; use bollard::Docker; -// Add this function to debug the Docker connection and environment -pub async fn get_available_container(docker: &Docker) { - println!("=== DOCKER ENVIRONMENT DEBUG ==="); + + +pub async fn get_available_container(docker: &Docker) -> Vec { + println!("=== DOCKER CONTAINER LIST ==="); - // List containers to see what's available - CORRECTED let options = Some(ListContainersOptions { - all: true, // include stopped containers + all: true, ..Default::default() }); - match docker.list_containers(options).await { + let containers_list = match docker.list_containers(options).await { Ok(containers) => { println!("Available containers ({}):", containers.len()); - for container in containers { - if let Some(id) = container.id { - let short_id = if id.len() > 12 { &id[..12] } else { &id }; - println!(" - ID: {}, Image: {:?}", short_id, container.image); + containers.into_iter() + .filter_map(|container| { + container.id.as_ref()?; // Skip if no ID - // Also print the names for easier identification - if let Some(names) = container.names { - println!(" Names: {:?}", names); - } - } - } + let id = container.id?; + let short_id = if id.len() > 12 { &id[..12] } else { &id }; + + let name = container.names + .and_then(|names| names.into_iter().next()) + .map(|name| name.trim_start_matches('/').to_string()) + .unwrap_or_else(|| "unknown".to_string()); + + let image = container.image + .as_ref() + .map(|img| img.to_string()) + .unwrap_or_else(|| "unknown".to_string()); + + let status = container.status + .as_ref() + .map(|s| match s.to_lowercase().as_str() { + s if s.contains("up") || s.contains("running") => "running".to_string(), + s if s.contains("exited") || s.contains("stopped") => "stopped".to_string(), + _ => s.to_string(), + }) + .unwrap_or_else(|| "unknown".to_string()); + + println!(" - ID: {}, Image: {:?}, Name: {}", short_id, container.image, name); + + Some(DockerContainer { + ID: short_id.to_string(), + image, + Name: name, + Status: status, + _net_in: 0.0, + _net_out: 0.0, + _cpu_load: 0.0, + }) + }) + .collect() } Err(e) => { eprintln!("Failed to list containers: {}", e); + Vec::new() } - } + }; + + containers_list } pub fn extract_container_id(line: &str) -> Option { diff --git a/WatcherAgent/src/docker/mod.rs b/WatcherAgent/src/docker/mod.rs index f65cc97..f91a5ff 100644 --- a/WatcherAgent/src/docker/mod.rs +++ b/WatcherAgent/src/docker/mod.rs @@ -1,12 +1,19 @@ pub mod container; +pub mod serverclientcomm; -//use std::error::Error; +use std::error::Error; use crate::models::DockerContainer; #[derive(Debug, Clone)] pub struct DockerInfo { - pub number: u16, - pub net_in_total: f64, - pub net_out_total: f64, - pub dockers: Vec, + pub number: Option, + pub net_in_total: Option, + pub net_out_total: Option, + pub dockers: Option>, +} + +impl DockerInfo { + pub async fn collect() -> Result> { + Ok(Self { number: None, net_in_total: None, net_out_total: None, dockers: None }) + } } \ No newline at end of file diff --git a/WatcherAgent/src/serverclientcomm.rs b/WatcherAgent/src/docker/serverclientcomm.rs similarity index 100% rename from WatcherAgent/src/serverclientcomm.rs rename to WatcherAgent/src/docker/serverclientcomm.rs diff --git a/WatcherAgent/src/main.rs b/WatcherAgent/src/main.rs index eb74e15..348f148 100644 --- a/WatcherAgent/src/main.rs +++ b/WatcherAgent/src/main.rs @@ -5,7 +5,6 @@ pub mod api; pub mod hardware; pub mod metrics; pub mod models; -pub mod serverclientcomm; pub mod docker; use std::env; @@ -16,8 +15,6 @@ use std::result::Result; use tokio::task::JoinHandle; use bollard::Docker; -use crate::serverclientcomm::{get_current_image}; - async fn flatten( handle: JoinHandle>>, ) -> Result> { @@ -35,7 +32,7 @@ async fn main() -> Result<(), Box> { .map_err(|e| format!("Failed to connect to Docker: {}", e))?; // Get current image version - let client_version = match get_current_image(&docker).await { + let client_version = match docker::serverclientcomm::get_current_image(&docker).await { Ok(Some(version)) => version, Ok(None) => { eprintln!("Warning: No image version found"); diff --git a/WatcherAgent/src/models.rs b/WatcherAgent/src/models.rs index b6549a0..53448bc 100644 --- a/WatcherAgent/src/models.rs +++ b/WatcherAgent/src/models.rs @@ -106,7 +106,7 @@ pub struct DockerContainer { pub image: String, pub Name: String, pub Status: String, // "running";"stopped";others - pub net_in: f64, - pub net_out: f64, - pub cpu_load: f64, + pub _net_in: f64, + pub _net_out: f64, + pub _cpu_load: f64, } \ No newline at end of file