diff --git a/WatcherAgent/src/serverclientcomm.rs b/WatcherAgent/src/serverclientcomm.rs index e65469e..75ac14e 100644 --- a/WatcherAgent/src/serverclientcomm.rs +++ b/WatcherAgent/src/serverclientcomm.rs @@ -1,8 +1,7 @@ use crate::models::ServerMessage; use bollard::Docker; -use bollard::query_parameters::CreateImageOptions; -use bollard::query_parameters::RestartContainerOptions; +use bollard::query_parameters::{CreateImageOptions, RestartContainerOptions, InspectContainerOptions}; use futures_util::StreamExt; pub fn parse_message(raw: &str) -> ServerMessage { @@ -53,6 +52,46 @@ pub async fn update_docker_image(docker: &Docker, image: &str) { restart_container(docker).await; } +pub async fn get_current_image(docker: &Docker) -> Option { + // Get the current container ID from /proc/self/cgroup + let container_id = match std::fs::read_to_string("/proc/self/cgroup") { + Ok(content) => { + content + .lines() + .find_map(|line| { + if line.contains("docker") { + line.split('/').last().map(|s| s.trim().to_string()) + } else { + None + } + }) + } + Err(e) => { + eprintln!("Error reading cgroup file: {}", e); + return None; + } + }; + + let container_id = match container_id { + Some(id) => id, + None => { + eprintln!("Could not find container ID in cgroup"); + return None; + } + }; + + // Inspect the current container to get its image + match docker.inspect_container(&container_id, None::).await { + Ok(container_info) => { + container_info.config.map(|config| config.image.unwrap_or_else(|| "unknown".to_string())) + } + Err(e) => { + eprintln!("Error inspecting container: {}", e); + None + } + } +} + pub async fn restart_container(docker: &Docker) { if let Ok(container_id) = std::env::var("HOSTNAME") { println!("Restarting container {}", container_id);