Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
d7a58e00da | |||
d2efc64487 | |||
1f23c303c1 | |||
1cc85bfa14 |
@@ -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 })
|
||||
}
|
||||
}
|
152
WatcherAgent/src/docker/serverclientcomm.rs
Normal file
152
WatcherAgent/src/docker/serverclientcomm.rs
Normal file
@@ -0,0 +1,152 @@
|
||||
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};
|
||||
use futures_util::StreamExt;
|
||||
|
||||
pub async fn handle_server_message(docker: &Docker, msg: ServerMessage) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let msg = msg.clone();
|
||||
println!("Handling server message: {:?}", msg);
|
||||
|
||||
// Handle different message types
|
||||
match msg.message_type.as_str() {
|
||||
"update_image" => {
|
||||
if let Some(image_name) = msg.data.get("image").and_then(|v| v.as_str()) {
|
||||
println!("Received update command for image: {}", image_name);
|
||||
// Call your update_docker_image function here
|
||||
update_docker_image(docker, image_name).await?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err("Missing image name in update message".into())
|
||||
}
|
||||
}
|
||||
"restart_container" => {
|
||||
println!("Received restart container command");
|
||||
// Call your restart_container function here
|
||||
restart_container(docker).await?;
|
||||
Ok(())
|
||||
}
|
||||
"stop_agent" => {
|
||||
println!("Received stop agent command");
|
||||
// Implement graceful shutdown
|
||||
std::process::exit(0);
|
||||
}
|
||||
_ => {
|
||||
eprintln!("Unknown message type: {}", msg.message_type);
|
||||
Err(format!("Unknown message type: {}", msg.message_type).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_docker_image(docker: &Docker, image: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
println!("Updating to {}", image);
|
||||
|
||||
// 1. Pull new image
|
||||
let mut stream = docker.create_image(
|
||||
Some(CreateImageOptions {
|
||||
from_image: Some(image.to_string()),
|
||||
..Default::default()
|
||||
}),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
|
||||
// Use the stream with proper trait bounds
|
||||
while let Some(result) = StreamExt::next(&mut stream).await {
|
||||
match result {
|
||||
Ok(progress) => {
|
||||
if let Some(status) = progress.status {
|
||||
println!("Pull status: {}", status);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error pulling image: {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Restart the current container
|
||||
let _ = restart_container(docker).await;
|
||||
|
||||
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;
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
@@ -37,28 +37,12 @@ pub async fn get_disk_info() -> Result<DiskInfo, Box<dyn std::error::Error + Sen
|
||||
component_disk_label: String::new(),
|
||||
component_disk_temperature: 0.0,
|
||||
});
|
||||
|
||||
println!(
|
||||
"Disk_Name: {:?}:\n---- Disk_Kind: {},\n---- Total: {},\n---- Available: {},\n---- Used: {}, \n---- Mount_Point: {:?}",
|
||||
disk.name(),
|
||||
disk.kind(),
|
||||
disk.total_space(),
|
||||
disk.available_space(),
|
||||
disk_used,
|
||||
disk.mount_point()
|
||||
);
|
||||
}
|
||||
|
||||
// Get component temperatures
|
||||
let components = Components::new_with_refreshed_list();
|
||||
for component in &components {
|
||||
if let Some(temperature) = component.temperature() {
|
||||
println!(
|
||||
"Component_Label: {}, Temperature: {}°C",
|
||||
component.label(),
|
||||
temperature
|
||||
);
|
||||
|
||||
// Update detailed info with temperature data if it matches a disk component
|
||||
for disk_info in &mut detailed_info {
|
||||
if component.label().contains(&disk_info.disk_name) {
|
||||
|
@@ -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");
|
||||
|
@@ -99,3 +99,14 @@ pub struct Acknowledgment {
|
||||
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,
|
||||
}
|
@@ -1,227 +0,0 @@
|
||||
use crate::models::{ServerMessage};
|
||||
|
||||
use std::error::Error;
|
||||
|
||||
use bollard::Docker;
|
||||
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>> {
|
||||
let msg = msg.clone();
|
||||
println!("Handling server message: {:?}", msg);
|
||||
|
||||
// Handle different message types
|
||||
match msg.message_type.as_str() {
|
||||
"update_image" => {
|
||||
if let Some(image_name) = msg.data.get("image").and_then(|v| v.as_str()) {
|
||||
println!("Received update command for image: {}", image_name);
|
||||
// Call your update_docker_image function here
|
||||
update_docker_image(docker, image_name).await?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err("Missing image name in update message".into())
|
||||
}
|
||||
}
|
||||
"restart_container" => {
|
||||
println!("Received restart container command");
|
||||
// Call your restart_container function here
|
||||
restart_container(docker).await?;
|
||||
Ok(())
|
||||
}
|
||||
"stop_agent" => {
|
||||
println!("Received stop agent command");
|
||||
// Implement graceful shutdown
|
||||
std::process::exit(0);
|
||||
}
|
||||
_ => {
|
||||
eprintln!("Unknown message type: {}", msg.message_type);
|
||||
Err(format!("Unknown message type: {}", msg.message_type).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_docker_image(docker: &Docker, image: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
println!("Updating to {}", image);
|
||||
|
||||
// 1. Pull new image
|
||||
let mut stream = docker.create_image(
|
||||
Some(CreateImageOptions {
|
||||
from_image: Some(image.to_string()),
|
||||
..Default::default()
|
||||
}),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
|
||||
// Use the stream with proper trait bounds
|
||||
while let Some(result) = StreamExt::next(&mut stream).await {
|
||||
match result {
|
||||
Ok(progress) => {
|
||||
if let Some(status) = progress.status {
|
||||
println!("Pull status: {}", status);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error pulling image: {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Restart the current container
|
||||
let _ = restart_container(docker).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_current_image(docker: &Docker) -> Result<Option<String>, Box<dyn Error + Send + Sync>> {
|
||||
// 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| {
|
||||
// Look for any line that might contain container information
|
||||
if line.contains("docker") || line.contains("crio") || line.contains("containerd") || line.contains("kubepods") {
|
||||
// Extract the container ID from the line
|
||||
extract_container_id(line)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error reading cgroup file: {}", e);
|
||||
return Ok(None);
|
||||
}
|
||||
};
|
||||
|
||||
// Debug: Print what we found
|
||||
println!("Container ID search result: {:?}", container_id);
|
||||
|
||||
let container_id = match container_id {
|
||||
Some(id) if !id.is_empty() => {
|
||||
println!("Found container ID: '{}'", id);
|
||||
id
|
||||
}
|
||||
_ => {
|
||||
// Debug the cgroup file content
|
||||
if let Ok(content) = std::fs::read_to_string("/proc/self/cgroup") {
|
||||
eprintln!("Cgroup file content for debugging:");
|
||||
for (i, line) in content.lines().enumerate() {
|
||||
eprintln!("Line {}: {}", i, line);
|
||||
}
|
||||
}
|
||||
eprintln!("Could not find valid container ID in cgroup");
|
||||
return Ok(None);
|
||||
}
|
||||
};
|
||||
|
||||
// Clean up the container ID - remove any non-hex characters
|
||||
let clean_container_id: String = container_id
|
||||
.chars()
|
||||
.filter(|c| c.is_ascii_hexdigit())
|
||||
.collect();
|
||||
|
||||
// Validate the container ID (should be 64 characters for full ID, but short ID might work too)
|
||||
if clean_container_id.is_empty() {
|
||||
eprintln!("Container ID contains no hex characters: '{}'", container_id);
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
println!("Using container ID: '{}' (cleaned: '{}')", container_id, clean_container_id);
|
||||
|
||||
// Try with both the original and cleaned ID
|
||||
let ids_to_try = vec![&clean_container_id, &container_id];
|
||||
|
||||
for id in ids_to_try {
|
||||
println!("Attempting to inspect container with ID: '{}'", id);
|
||||
|
||||
match docker.inspect_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");
|
||||
return Ok(Some("unknown".to_string()));
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error inspecting container with ID '{}': {}", id, e);
|
||||
// Continue to try the next ID
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
eprintln!("All attempts to inspect container failed");
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn extract_container_id(line: &str) -> Option<String> {
|
||||
let parts: Vec<&str> = line.split('/').collect();
|
||||
|
||||
if let Some(last_part) = parts.last() {
|
||||
let last_part = last_part.trim();
|
||||
|
||||
println!("Processing cgroup line part: '{}'", last_part);
|
||||
|
||||
// Common patterns for container IDs in cgroups:
|
||||
|
||||
// Pattern 1: Full container ID (64-character hex)
|
||||
if last_part.len() == 64 && last_part.chars().all(|c| c.is_ascii_hexdigit()) {
|
||||
println!("Found full container ID: {}", last_part);
|
||||
return Some(last_part.to_string());
|
||||
}
|
||||
|
||||
// Pattern 2: docker-<short_id>.scope
|
||||
if last_part.starts_with("docker-") && last_part.ends_with(".scope") {
|
||||
let id = last_part
|
||||
.trim_start_matches("docker-")
|
||||
.trim_end_matches(".scope")
|
||||
.to_string();
|
||||
println!("Found docker scope ID: {}", id);
|
||||
return Some(id);
|
||||
}
|
||||
|
||||
// Pattern 3: <short_id> (12-character hex or similar)
|
||||
if last_part.chars().all(|c| c.is_ascii_hexdigit()) && last_part.len() >= 12 {
|
||||
println!("Found hex ID: {}", last_part);
|
||||
return Some(last_part.to_string());
|
||||
}
|
||||
|
||||
// Pattern 4: Try to extract ID from any string that looks like it contains a hex ID
|
||||
if let Some(caps) = regex::Regex::new(r"[a-fA-F0-9]{12,64}")
|
||||
.ok()
|
||||
.and_then(|re| re.find(last_part))
|
||||
{
|
||||
let id = caps.as_str().to_string();
|
||||
println!("Extracted ID via regex: {}", id);
|
||||
return Some(id);
|
||||
}
|
||||
|
||||
// Pattern 5: If nothing else matches, try the last part as-is
|
||||
if !last_part.is_empty() {
|
||||
println!("Using last part as ID: {}", last_part);
|
||||
return Some(last_part.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
Reference in New Issue
Block a user