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
This commit is contained in:
Christoffer Martinsson 2025-10-23 23:07:52 +02:00
parent 99da289183
commit 967244064f
2 changed files with 32 additions and 6 deletions

View File

@ -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()

View File

@ -24,7 +24,12 @@ impl BackupCollector {
}
}
async fn read_backup_status(&self) -> Result<BackupStatusToml, CollectorError> {
async fn read_backup_status(&self) -> Result<Option<BackupStatusToml>, 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<Vec<Metric>, 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 {