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
This commit is contained in:
Christoffer Martinsson 2025-10-23 23:19:43 +02:00
parent 967244064f
commit 9df6106bf5

View File

@ -280,12 +280,22 @@ impl Dashboard {
self.zmq_command_sender.send_command(&hostname, agent_command).await?; self.zmq_command_sender.send_command(&hostname, agent_command).await?;
} }
UiCommand::ServiceStartStop { hostname, service_name } => { UiCommand::ServiceStartStop { hostname, service_name } => {
// For now, we'll implement this as a start command // Check current service status and toggle appropriately
// TODO: Check current service status and toggle appropriately let current_status = self.get_service_status(&hostname, &service_name);
info!("Sending start command for service {} on {}", service_name, hostname); 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 { let agent_command = AgentCommand::ServiceControl {
service_name, service_name,
action: ServiceAction::Start, action,
}; };
self.zmq_command_sender.send_command(&hostname, agent_command).await?; self.zmq_command_sender.send_command(&hostname, agent_command).await?;
} }
@ -304,6 +314,22 @@ impl Dashboard {
} }
Ok(()) Ok(())
} }
/// Get current service status from metrics to determine start/stop action
fn get_service_status(&self, hostname: &str, service_name: &str) -> Option<String> {
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 { impl Drop for Dashboard {