get clientcontainer; container findable with id image name ahs to be implemented

This commit is contained in:
2025-10-01 11:36:12 +02:00
parent d7a58e00da
commit d994be757e
5 changed files with 47 additions and 70 deletions

View File

@@ -1,5 +1,5 @@
use crate::models::ServerMessage;
use crate::docker::container::{extract_container_id, get_available_container};
use crate::models::{DockerContainer, ServerMessage};
use crate::docker::container::{get_available_container};
use std::error::Error;
use bollard::Docker;
@@ -74,72 +74,23 @@ pub async fn update_docker_image(docker: &Docker, image: &str) -> Result<(), Box
Ok(())
}
pub async fn get_current_image(docker: &Docker) -> Result<Option<String>, Box<dyn Error + Send + Sync>> {
// First, let's debug the environment
get_available_container(docker).await;
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";
// Get the current container ID from /proc/self/cgroup
let container_id = match std::fs::read_to_string("/proc/self/cgroup") {
Ok(content) => {
let mut found_id = None;
println!("Searching cgroup for container ID...");
for line in content.lines() {
println!("Checking line: {}", line);
// Look for container runtime indicators
if line.contains("docker") || line.contains("crio") || line.contains("containerd") {
if let Some(id) = extract_container_id(line) {
println!("Found potential container ID: {}", id);
found_id = Some(id);
break;
}
}
}
found_id
}
Err(e) => {
eprintln!("Error reading cgroup file: {}", e);
return Ok(None);
}
};
let container_id = match container_id {
Some(id) if !id.is_empty() => {
println!("Using container ID: '{}'", id);
id
}
_ => {
eprintln!("Could not find valid container ID in cgroup");
return Ok(None);
}
};
// Try to inspect the container
println!("Attempting to inspect container with ID: '{}'", container_id);
match docker.inspect_container(&container_id, None::<InspectContainerOptions>).await {
Ok(container_info) => {
if let Some(config) = container_info.config {
if let Some(image) = config.image {
println!("Successfully found image: {}", image);
return Ok(Some(image));
}
}
eprintln!("Container inspected but no image found in config");
Ok(Some("unknown".to_string()))
}
Err(e) => {
eprintln!("Error inspecting container: {}", e);
Ok(None)
}
// Find container with the specific image
if let Some(container) = containers.iter().find(|c| c.image == client_image) {
Ok(Some(container.clone()))
} else {
Ok(None)
}
}
pub async fn restart_container(docker: &Docker) -> Result<(), Box<dyn Error + Send + Sync>> {
if let Ok(container_id) = std::env::var("HOSTNAME") {
if let Ok(Some(container)) = get_client_container(docker).await {
let container_id = container.clone().ID;
println!("Restarting container {}", container_id);
if let Err(e) = docker.restart_container(&container_id, Some(RestartContainerOptions { signal: None, t: Some(0) }))
if let Err(e) = docker.restart_container(&container_id.to_string(), Some(RestartContainerOptions { signal: None, t: Some(0) }))
.await
{
eprintln!("Failed to restart container: {}", e);