Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
d7a58e00da | |||
d2efc64487 |
@@ -21,9 +21,6 @@ anyhow = "1.0.98"
|
||||
|
||||
regex = "1.11.3"
|
||||
|
||||
# Docker .env loading
|
||||
# config = "0.13"
|
||||
|
||||
# Docker API access
|
||||
bollard = "0.19"
|
||||
futures-util = "0.3"
|
||||
|
@@ -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};
|
||||
|
97
WatcherAgent/src/docker/container.rs
Normal file
97
WatcherAgent/src/docker/container.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
use crate::models::DockerContainer;
|
||||
|
||||
use bollard::query_parameters::{ListContainersOptions};
|
||||
use bollard::Docker;
|
||||
|
||||
|
||||
|
||||
|
||||
pub async fn get_available_container(docker: &Docker) -> Vec<DockerContainer> {
|
||||
println!("=== DOCKER CONTAINER LIST ===");
|
||||
|
||||
let options = Some(ListContainersOptions {
|
||||
all: true,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let containers_list = match docker.list_containers(options).await {
|
||||
Ok(containers) => {
|
||||
println!("Available containers ({}):", containers.len());
|
||||
containers.into_iter()
|
||||
.filter_map(|container| {
|
||||
container.id.as_ref()?; // Skip if no ID
|
||||
|
||||
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<String> {
|
||||
// Split by slashes and take the last part
|
||||
if let Some(last_part) = line.split('/').last() {
|
||||
let last_part = last_part.trim();
|
||||
|
||||
// Remove common suffixes
|
||||
let clean_id = last_part
|
||||
.trim_end_matches(".scope")
|
||||
.trim_start_matches("docker-")
|
||||
.trim_start_matches("crio-")
|
||||
.trim_start_matches("containerd-");
|
||||
|
||||
// Check if it looks like a container ID (hex characters)
|
||||
if clean_id.chars().all(|c| c.is_ascii_hexdigit()) && clean_id.len() >= 12 {
|
||||
return Some(clean_id.to_string());
|
||||
}
|
||||
|
||||
// If it's not pure hex, try to extract hex sequence
|
||||
let hex_part: String = clean_id.chars()
|
||||
.take_while(|c| c.is_ascii_hexdigit())
|
||||
.collect();
|
||||
|
||||
if hex_part.len() >= 12 {
|
||||
return Some(hex_part);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
19
WatcherAgent/src/docker/mod.rs
Normal file
19
WatcherAgent/src/docker/mod.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
pub mod container;
|
||||
pub mod serverclientcomm;
|
||||
|
||||
use std::error::Error;
|
||||
use crate::models::DockerContainer;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DockerInfo {
|
||||
pub number: Option<u16>,
|
||||
pub net_in_total: Option<f64>,
|
||||
pub net_out_total: Option<f64>,
|
||||
pub dockers: Option<Vec<DockerContainer>>,
|
||||
}
|
||||
|
||||
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 })
|
||||
}
|
||||
}
|
@@ -1,8 +1,9 @@
|
||||
use crate::models::{ServerMessage};
|
||||
use crate::models::ServerMessage;
|
||||
use crate::docker::container::{extract_container_id, get_available_container};
|
||||
|
||||
use std::error::Error;
|
||||
use bollard::Docker;
|
||||
use bollard::query_parameters::{CreateImageOptions, RestartContainerOptions, InspectContainerOptions, ListContainersOptions};
|
||||
use bollard::query_parameters::{CreateImageOptions, RestartContainerOptions, InspectContainerOptions};
|
||||
use futures_util::StreamExt;
|
||||
|
||||
pub async fn handle_server_message(docker: &Docker, msg: ServerMessage) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
@@ -75,7 +76,7 @@ pub async fn update_docker_image(docker: &Docker, image: &str) -> Result<(), Box
|
||||
|
||||
pub async fn get_current_image(docker: &Docker) -> Result<Option<String>, Box<dyn Error + Send + Sync>> {
|
||||
// First, let's debug the environment
|
||||
debug_docker_environment(docker).await;
|
||||
get_available_container(docker).await;
|
||||
|
||||
// Get the current container ID from /proc/self/cgroup
|
||||
let container_id = match std::fs::read_to_string("/proc/self/cgroup") {
|
||||
@@ -135,80 +136,6 @@ pub async fn get_current_image(docker: &Docker) -> Result<Option<String>, Box<dy
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_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();
|
||||
|
||||
// Remove common suffixes
|
||||
let clean_id = last_part
|
||||
.trim_end_matches(".scope")
|
||||
.trim_start_matches("docker-")
|
||||
.trim_start_matches("crio-")
|
||||
.trim_start_matches("containerd-");
|
||||
|
||||
// Check if it looks like a container ID (hex characters)
|
||||
if clean_id.chars().all(|c| c.is_ascii_hexdigit()) && clean_id.len() >= 12 {
|
||||
return Some(clean_id.to_string());
|
||||
}
|
||||
|
||||
// If it's not pure hex, try to extract hex sequence
|
||||
let hex_part: String = clean_id.chars()
|
||||
.take_while(|c| c.is_ascii_hexdigit())
|
||||
.collect();
|
||||
|
||||
if hex_part.len() >= 12 {
|
||||
return Some(hex_part);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
// Add this function to debug the Docker connection and environment
|
||||
async fn debug_docker_environment(docker: &Docker) {
|
||||
println!("=== DOCKER ENVIRONMENT DEBUG ===");
|
||||
|
||||
// List containers to see what's available - CORRECTED
|
||||
let options = Some(ListContainersOptions {
|
||||
all: true, // include stopped containers
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
// Also print the names for easier identification
|
||||
if let Some(names) = container.names {
|
||||
println!(" Names: {:?}", names);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to list containers: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we're actually running in a container
|
||||
if let Ok(content) = std::fs::read_to_string("/proc/self/cgroup") {
|
||||
println!("Cgroup contents:");
|
||||
println!("{}", content);
|
||||
}
|
||||
|
||||
// Check other container indicators
|
||||
if std::path::Path::new("/.dockerenv").exists() {
|
||||
println!("/.dockerenv exists - running in Docker container");
|
||||
} else {
|
||||
println!("/.dockerenv does not exist");
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
@@ -5,7 +5,7 @@ pub mod api;
|
||||
pub mod hardware;
|
||||
pub mod metrics;
|
||||
pub mod models;
|
||||
pub mod serverclientcomm;
|
||||
pub mod docker;
|
||||
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
@@ -15,8 +15,6 @@ use std::result::Result;
|
||||
use tokio::task::JoinHandle;
|
||||
use bollard::Docker;
|
||||
|
||||
use crate::serverclientcomm::{get_current_image};
|
||||
|
||||
async fn flatten<T>(
|
||||
handle: JoinHandle<Result<T, Box<dyn Error + Send + Sync>>>,
|
||||
) -> Result<T, Box<dyn Error + Send + Sync>> {
|
||||
@@ -34,7 +32,7 @@ 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 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");
|
||||
|
@@ -98,4 +98,15 @@ pub struct Acknowledgment {
|
||||
pub message_id: String,
|
||||
pub status: String, // "success" or "error"
|
||||
pub details: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct DockerContainer {
|
||||
pub ID: String,
|
||||
pub image: String,
|
||||
pub Name: String,
|
||||
pub Status: String, // "running";"stopped";others
|
||||
pub _net_in: f64,
|
||||
pub _net_out: f64,
|
||||
pub _cpu_load: f64,
|
||||
}
|
Reference in New Issue
Block a user