From 9df6106bf584ade997dfd44376f868b71c244c68 Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Thu, 23 Oct 2025 23:19:43 +0200 Subject: [PATCH] Fix service start/stop toggle and nixos-rebuild permissions - Implement proper service status checking for start/stop toggle - Space key now checks current service status and toggles appropriately - Active services get stopped, inactive services get started - Fix nixos-rebuild sudo permissions (remove invalid package reference) - Use only /run/current-system/sw/bin/nixos-rebuild path --- dashboard/src/app.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/dashboard/src/app.rs b/dashboard/src/app.rs index a5dab19..d062603 100644 --- a/dashboard/src/app.rs +++ b/dashboard/src/app.rs @@ -280,12 +280,22 @@ impl Dashboard { self.zmq_command_sender.send_command(&hostname, agent_command).await?; } UiCommand::ServiceStartStop { hostname, service_name } => { - // For now, we'll implement this as a start command - // TODO: Check current service status and toggle appropriately - info!("Sending start command for service {} on {}", service_name, hostname); + // Check current service status and toggle appropriately + let current_status = self.get_service_status(&hostname, &service_name); + let action = match current_status { + Some(status) if status.contains("active") || status.contains("running") => { + info!("Sending stop command for active service {} on {}", service_name, hostname); + ServiceAction::Stop + } + _ => { + info!("Sending start command for inactive service {} on {}", service_name, hostname); + ServiceAction::Start + } + }; + let agent_command = AgentCommand::ServiceControl { service_name, - action: ServiceAction::Start, + action, }; self.zmq_command_sender.send_command(&hostname, agent_command).await?; } @@ -304,6 +314,22 @@ impl Dashboard { } Ok(()) } + + /// Get current service status from metrics to determine start/stop action + fn get_service_status(&self, hostname: &str, service_name: &str) -> Option { + let metrics = self.metric_store.get_metrics_for_host(hostname); + + // Look for systemd service status metric + for metric in metrics { + if metric.name == format!("systemd_{}_status", service_name) { + if let cm_dashboard_shared::MetricValue::String(status) = &metric.value { + return Some(status.clone()); + } + } + } + + None + } } impl Drop for Dashboard {