added docker management
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 4s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 4s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m2s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 2m18s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 3m25s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m0s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 2s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s

This commit is contained in:
2025-10-03 20:42:35 +02:00
parent 2f5e2391f7
commit bfeb43f38a
8 changed files with 668 additions and 255 deletions

View File

@@ -1,4 +1,3 @@
/// # WatcherAgent
///
/// **WatcherAgent** is a cross-platform system monitoring agent written in Rust.
@@ -26,18 +25,16 @@
/// ```
///
/// The agent will register itself, start collecting metrics, and listen for remote commands.
pub mod api;
pub mod docker;
pub mod hardware;
pub mod metrics;
pub mod models;
pub mod docker;
use tokio::task::JoinHandle;
use bollard::Docker;
use std::env;
use std::error::Error;
use tokio::task::JoinHandle;
/// Awaits a spawned asynchronous task and flattens its nested `Result` type.
///
@@ -82,26 +79,8 @@ async fn flatten<T>(
/// Returns an error if registration or any background task fails, or if required arguments are missing.
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// Initialize Docker client
let docker = Docker::connect_with_local_defaults()
.map_err(|e| format!("Failed to connect to Docker: {}", e))?;
// Get current image 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()
}
Err(e) => {
eprintln!("Warning: Could not get current image version: {}", e);
"unknown".to_string()
}
};
println!("Client Version: {}", client_version);
// Parse command-line arguments
let args: Vec<String> = env::args().collect();
// args[0] is the binary name, args[1] is the first actual argument
if args.len() < 2 {
eprintln!("Usage: {} <server-url>", args[0]);
return Err("Missing server URL argument".into());
@@ -118,13 +97,29 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
}
};
// Initialize Docker (optional - agent can run without Docker)
let docker_manager = docker::DockerManager::new_optional();
// Get current image version
let client_version = if let Some(ref docker_manager) = docker_manager {
docker_manager.get_client_version().await
} else {
"unknown".to_string()
};
println!("Client Version: {}", client_version);
// Start background tasks
// Start server listening for commands
let listening_handle = tokio::spawn({
let docker = docker.clone();
let server_url = server_url.to_string();
async move { api::listening_to_server(&docker, &server_url).await }
});
// Start server listening for commands (only if Docker is available)
let listening_handle = if let Some(docker_manager) = docker_manager {
tokio::spawn({
let docker = docker_manager.docker.clone();
let server_url = server_url.to_string();
async move { api::listening_to_server(&docker, &server_url).await }
})
} else {
println!("Docker not available, skipping server command listener.");
tokio::spawn(async { Ok(()) }) // Dummy task
};
// Start heartbeat in background
let heartbeat_handle = tokio::spawn({