From 967244064f386cc0eabef3379d7016a867ec2f95 Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Thu, 23 Oct 2025 23:07:52 +0200 Subject: [PATCH] Fix command execution permissions and eliminate backup error spam - Add sudo permissions for systemctl and nixos-rebuild commands - Use sudo in agent command execution for proper privileges - Fix backup collector to handle missing status files gracefully - Eliminate backup error spam when no backup system is configured --- agent/src/agent.rs | 6 ++++-- agent/src/collectors/backup.rs | 32 ++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/agent/src/agent.rs b/agent/src/agent.rs index 7ef7a77..b5d957a 100644 --- a/agent/src/agent.rs +++ b/agent/src/agent.rs @@ -253,7 +253,8 @@ impl Agent { info!("Executing systemctl {} {}", action_str, service_name); - let output = tokio::process::Command::new("systemctl") + let output = tokio::process::Command::new("sudo") + .arg("systemctl") .arg(action_str) .arg(service_name) .output() @@ -293,7 +294,8 @@ impl Agent { } // Change to nixos directory and execute rebuild - let output = tokio::process::Command::new("nixos-rebuild") + let output = tokio::process::Command::new("sudo") + .arg("nixos-rebuild") .arg("switch") .current_dir(nixos_path) .output() diff --git a/agent/src/collectors/backup.rs b/agent/src/collectors/backup.rs index b2acd9e..ed3888a 100644 --- a/agent/src/collectors/backup.rs +++ b/agent/src/collectors/backup.rs @@ -24,7 +24,12 @@ impl BackupCollector { } } - async fn read_backup_status(&self) -> Result { + async fn read_backup_status(&self) -> Result, CollectorError> { + // Check if backup status file exists + if !std::path::Path::new(&self.backup_status_file).exists() { + return Ok(None); // File doesn't exist, but this is not an error + } + let content = fs::read_to_string(&self.backup_status_file) .await .map_err(|e| CollectorError::SystemRead { @@ -32,10 +37,12 @@ impl BackupCollector { error: e.to_string(), })?; - toml::from_str(&content).map_err(|e| CollectorError::Parse { + let backup_status = toml::from_str(&content).map_err(|e| CollectorError::Parse { value: "backup status TOML".to_string(), error: e.to_string(), - }) + })?; + + Ok(Some(backup_status)) } fn calculate_backup_status(&self, backup_status: &BackupStatusToml) -> Status { @@ -105,10 +112,27 @@ impl Collector for BackupCollector { } async fn collect(&self, _status_tracker: &mut StatusTracker) -> Result, CollectorError> { - let backup_status = self.read_backup_status().await?; + let backup_status_option = self.read_backup_status().await?; let mut metrics = Vec::new(); let timestamp = chrono::Utc::now().timestamp() as u64; + // If no backup status file exists, return minimal metrics indicating no backup system + let backup_status = match backup_status_option { + Some(status) => status, + None => { + // No backup system configured - return minimal "unknown" metrics + metrics.push(Metric { + name: "backup_overall_status".to_string(), + value: MetricValue::String("no_backup_system".to_string()), + status: Status::Unknown, + timestamp, + description: Some("No backup system configured (no status file found)".to_string()), + unit: None, + }); + return Ok(metrics); + } + }; + // Overall backup status let overall_status = self.calculate_backup_status(&backup_status); metrics.push(Metric {