get clientcontainer; container findable with id image name ahs to be implemented
This commit is contained in:
@@ -22,8 +22,9 @@ pub async fn get_available_container(docker: &Docker) -> Vec<DockerContainer> {
|
||||
container.id.as_ref()?; // Skip if no ID
|
||||
|
||||
let id = container.id?;
|
||||
let short_id = if id.len() > 12 { &id[..12] } else { &id };
|
||||
|
||||
let short_string_id = if id.len() > 12 { &id[..12] } else { &id };
|
||||
let short_id: u32 = short_string_id.trim().parse().unwrap();
|
||||
|
||||
let name = container.names
|
||||
.and_then(|names| names.into_iter().next())
|
||||
.map(|name| name.trim_start_matches('/').to_string())
|
||||
@@ -46,7 +47,7 @@ pub async fn get_available_container(docker: &Docker) -> Vec<DockerContainer> {
|
||||
println!(" - ID: {}, Image: {:?}, Name: {}", short_id, container.image, name);
|
||||
|
||||
Some(DockerContainer {
|
||||
ID: short_id.to_string(),
|
||||
ID: short_id,
|
||||
image,
|
||||
Name: name,
|
||||
Status: status,
|
||||
@@ -66,7 +67,7 @@ pub async fn get_available_container(docker: &Docker) -> Vec<DockerContainer> {
|
||||
containers_list
|
||||
}
|
||||
|
||||
pub fn extract_container_id(line: &str) -> Option<String> {
|
||||
/*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();
|
||||
@@ -94,4 +95,4 @@ pub fn extract_container_id(line: &str) -> Option<String> {
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}*/
|
@@ -16,4 +16,29 @@ impl DockerInfo {
|
||||
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?)");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}*/
|
||||
|
||||
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>> {
|
||||
Ok(container.image)
|
||||
}
|
||||
}
|
@@ -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);
|
||||
|
@@ -32,8 +32,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
.map_err(|e| format!("Failed to connect to Docker: {}", e))?;
|
||||
|
||||
// Get current image version
|
||||
let client_version = match docker::serverclientcomm::get_current_image(&docker).await {
|
||||
Ok(Some(version)) => version,
|
||||
let client_version = match docker::serverclientcomm::get_client_container(&docker).await {
|
||||
Ok(Some(version)) => version.image,
|
||||
Ok(None) => {
|
||||
eprintln!("Warning: No image version found");
|
||||
"unknown".to_string()
|
||||
|
@@ -102,7 +102,7 @@ pub struct Acknowledgment {
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct DockerContainer {
|
||||
pub ID: String,
|
||||
pub ID: u32,
|
||||
pub image: String,
|
||||
pub Name: String,
|
||||
pub Status: String, // "running";"stopped";others
|
||||
|
Reference in New Issue
Block a user