Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b1bff4857b | |||
| f8a061d496 | |||
| e61a845965 | |||
| ac5d2d4db5 | |||
| 69892a2d84 |
66
CLAUDE.md
66
CLAUDE.md
@@ -33,11 +33,25 @@ All keyboard navigation and service selection features successfully implemented:
|
||||
- Service selection cursor implemented with focus-aware highlighting ✅
|
||||
- Panel scrolling fixed for System, Services, and Backup panels ✅
|
||||
- Build display working: "Build: 25.05.20251004.3bcc93c" ✅
|
||||
- Agent version display working: "Agent: 3kvc03nd" ✅
|
||||
- Agent version display working: "Agent: v0.1.14" ✅
|
||||
- Cross-host version comparison implemented ✅
|
||||
- Automated binary release system working ✅
|
||||
- SMART data consolidated into disk collector ✅
|
||||
|
||||
**CRITICAL ISSUE - Remote Rebuild Functionality:**
|
||||
- ❌ **System Rebuild**: Agent crashes during nixos-rebuild operations
|
||||
- ❌ **Systemd Service**: cm-rebuild.service fails with exit status 1
|
||||
- ❌ **Output Streaming**: Terminal popup shows agent messages but not rebuild output
|
||||
- ⚠️ **Service Control**: Works correctly for start/stop/restart of services
|
||||
|
||||
**Problem Details:**
|
||||
- Implemented systemd service approach to prevent agent crashes
|
||||
- Terminal popup implemented with real-time streaming capability
|
||||
- Service produces empty journal lines then exits with status 1
|
||||
- Permission issues addressed by moving working directory to /tmp
|
||||
- Issue persists despite multiple troubleshooting attempts
|
||||
- Manual rebuilds work perfectly when done directly
|
||||
|
||||
**Current Layout:**
|
||||
```
|
||||
NixOS:
|
||||
@@ -92,6 +106,56 @@ Storage:
|
||||
- ✅ **Git Clone Approach**: Implemented for nixos-rebuild to avoid directory permissions
|
||||
- ✅ **Visual Feedback**: Directional arrows for service status (↑ starting, ↓ stopping, ↻ restarting)
|
||||
|
||||
### Terminal Popup for Real-time Output - IMPLEMENTED ✅
|
||||
|
||||
**Status (as of 2025-10-26):**
|
||||
- ✅ **Terminal Popup UI**: 80% screen coverage with terminal styling and color-coded output
|
||||
- ✅ **ZMQ Streaming Protocol**: CommandOutputMessage for real-time output transmission
|
||||
- ✅ **Keyboard Controls**: ESC/Q to close, ↑↓ to scroll, manual close (no auto-close)
|
||||
- ✅ **Real-time Display**: Live streaming of command output as it happens
|
||||
- ✅ **Version-based Agent Reporting**: Shows "Agent: v0.1.13" instead of nix store hash
|
||||
|
||||
**Current Implementation Issues:**
|
||||
- ❌ **Agent Process Crashes**: Agent dies during nixos-rebuild execution
|
||||
- ❌ **Inconsistent Output**: Different outputs each time 'R' is pressed
|
||||
- ❌ **Limited Output Visibility**: Not capturing all nixos-rebuild progress
|
||||
|
||||
**PLANNED SOLUTION - Systemd Service Approach:**
|
||||
|
||||
**Problem**: Direct nixos-rebuild execution in agent causes process crashes and inconsistent output.
|
||||
|
||||
**Solution**: Create dedicated systemd service for rebuild operations.
|
||||
|
||||
**Implementation Plan:**
|
||||
1. **NixOS Systemd Service**:
|
||||
```nix
|
||||
systemd.services.cm-rebuild = {
|
||||
description = "CM Dashboard NixOS Rebuild";
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
ExecStart = "${pkgs.nixos-rebuild}/bin/nixos-rebuild switch --flake . --option sandbox false";
|
||||
WorkingDirectory = "/var/lib/cm-dashboard/nixos-config";
|
||||
User = "root";
|
||||
StandardOutput = "journal";
|
||||
StandardError = "journal";
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
2. **Agent Modification**:
|
||||
- Replace direct nixos-rebuild execution with: `systemctl start cm-rebuild`
|
||||
- Stream output via: `journalctl -u cm-rebuild -f --no-pager`
|
||||
- Monitor service status for completion detection
|
||||
|
||||
3. **Benefits**:
|
||||
- **Process Isolation**: Service runs independently, won't crash agent
|
||||
- **Consistent Output**: Always same deterministic rebuild process
|
||||
- **Proper Logging**: systemd journal handles all output management
|
||||
- **Resource Management**: systemd manages cleanup and resource limits
|
||||
- **Status Tracking**: Can query service status (running/failed/success)
|
||||
|
||||
**Next Priority**: Implement systemd service approach for reliable rebuild operations.
|
||||
|
||||
**Keyboard Controls Status:**
|
||||
- **Services Panel**:
|
||||
- R (restart) ✅ Working
|
||||
|
||||
6
Cargo.lock
generated
6
Cargo.lock
generated
@@ -270,7 +270,7 @@ checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d"
|
||||
|
||||
[[package]]
|
||||
name = "cm-dashboard"
|
||||
version = "0.1.0"
|
||||
version = "0.1.16"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"chrono",
|
||||
@@ -291,7 +291,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "cm-dashboard-agent"
|
||||
version = "0.1.0"
|
||||
version = "0.1.16"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
@@ -314,7 +314,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "cm-dashboard-shared"
|
||||
version = "0.1.0"
|
||||
version = "0.1.16"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"serde",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "cm-dashboard-agent"
|
||||
version = "0.1.11"
|
||||
version = "0.1.17"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::config::AgentConfig;
|
||||
use crate::metrics::MetricCollectionManager;
|
||||
use crate::notifications::NotificationManager;
|
||||
use crate::status::HostStatusManager;
|
||||
use cm_dashboard_shared::{CommandOutputMessage, Metric, MetricMessage, MetricValue, Status};
|
||||
use cm_dashboard_shared::{Metric, MetricMessage, MetricValue, Status};
|
||||
|
||||
pub struct Agent {
|
||||
hostname: String,
|
||||
@@ -254,12 +254,6 @@ impl Agent {
|
||||
error!("Failed to execute service control: {}", e);
|
||||
}
|
||||
}
|
||||
AgentCommand::SystemRebuild { git_url, git_branch, working_dir, api_key_file } => {
|
||||
info!("Processing SystemRebuild command: {} @ {} -> {}", git_url, git_branch, working_dir);
|
||||
if let Err(e) = self.handle_system_rebuild(&git_url, &git_branch, &working_dir, api_key_file.as_deref()).await {
|
||||
error!("Failed to execute system rebuild: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -303,230 +297,4 @@ impl Agent {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Handle NixOS system rebuild commands with real-time output streaming
|
||||
async fn handle_system_rebuild(&self, git_url: &str, git_branch: &str, working_dir: &str, api_key_file: Option<&str>) -> Result<()> {
|
||||
info!("Starting NixOS system rebuild: {} @ {} -> {}", git_url, git_branch, working_dir);
|
||||
|
||||
let command_id = format!("rebuild_{}", chrono::Utc::now().timestamp());
|
||||
|
||||
// Send initial status
|
||||
self.send_command_output(&command_id, "SystemRebuild", "Starting NixOS system rebuild...").await?;
|
||||
|
||||
// Enable maintenance mode before rebuild
|
||||
let maintenance_file = "/tmp/cm-maintenance";
|
||||
if let Err(e) = tokio::fs::File::create(maintenance_file).await {
|
||||
self.send_command_output(&command_id, "SystemRebuild", &format!("Warning: Failed to create maintenance mode file: {}", e)).await?;
|
||||
} else {
|
||||
self.send_command_output(&command_id, "SystemRebuild", "Maintenance mode enabled").await?;
|
||||
}
|
||||
|
||||
// Clone or update repository
|
||||
self.send_command_output(&command_id, "SystemRebuild", "Cloning/updating git repository...").await?;
|
||||
let git_result = self.ensure_git_repository_with_output(&command_id, git_url, git_branch, working_dir, api_key_file).await;
|
||||
|
||||
if git_result.is_err() {
|
||||
self.send_command_output(&command_id, "SystemRebuild", &format!("Git operation failed: {:?}", git_result)).await?;
|
||||
self.send_command_output_complete(&command_id, "SystemRebuild").await?;
|
||||
return git_result;
|
||||
}
|
||||
|
||||
self.send_command_output(&command_id, "SystemRebuild", "Git repository ready, starting nixos-rebuild...").await?;
|
||||
|
||||
// Execute nixos-rebuild with real-time output streaming
|
||||
let rebuild_result = self.execute_nixos_rebuild_with_streaming(&command_id, working_dir).await;
|
||||
|
||||
// Always try to remove maintenance mode file
|
||||
if let Err(e) = tokio::fs::remove_file(maintenance_file).await {
|
||||
if e.kind() != std::io::ErrorKind::NotFound {
|
||||
self.send_command_output(&command_id, "SystemRebuild", &format!("Warning: Failed to remove maintenance mode file: {}", e)).await?;
|
||||
}
|
||||
} else {
|
||||
self.send_command_output(&command_id, "SystemRebuild", "Maintenance mode disabled").await?;
|
||||
}
|
||||
|
||||
// Handle rebuild result
|
||||
match rebuild_result {
|
||||
Ok(()) => {
|
||||
self.send_command_output(&command_id, "SystemRebuild", "✓ NixOS rebuild completed successfully!").await?;
|
||||
}
|
||||
Err(e) => {
|
||||
self.send_command_output(&command_id, "SystemRebuild", &format!("✗ NixOS rebuild failed: {}", e)).await?;
|
||||
}
|
||||
}
|
||||
|
||||
// Signal completion
|
||||
self.send_command_output_complete(&command_id, "SystemRebuild").await?;
|
||||
|
||||
info!("System rebuild streaming completed");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Send command output line to dashboard
|
||||
async fn send_command_output(&self, command_id: &str, command_type: &str, output_line: &str) -> Result<()> {
|
||||
let message = CommandOutputMessage::new(
|
||||
self.hostname.clone(),
|
||||
command_id.to_string(),
|
||||
command_type.to_string(),
|
||||
output_line.to_string(),
|
||||
false,
|
||||
);
|
||||
self.zmq_handler.publish_command_output(&message).await
|
||||
}
|
||||
|
||||
/// Send command completion signal to dashboard
|
||||
async fn send_command_output_complete(&self, command_id: &str, command_type: &str) -> Result<()> {
|
||||
let message = CommandOutputMessage::new(
|
||||
self.hostname.clone(),
|
||||
command_id.to_string(),
|
||||
command_type.to_string(),
|
||||
"Command completed".to_string(),
|
||||
true,
|
||||
);
|
||||
self.zmq_handler.publish_command_output(&message).await
|
||||
}
|
||||
|
||||
/// Execute nixos-rebuild with real-time output streaming
|
||||
async fn execute_nixos_rebuild_with_streaming(&self, command_id: &str, working_dir: &str) -> Result<()> {
|
||||
use tokio::io::{AsyncBufReadExt, BufReader};
|
||||
use tokio::process::Command;
|
||||
|
||||
let mut child = Command::new("sudo")
|
||||
.arg("/run/current-system/sw/bin/nixos-rebuild")
|
||||
.arg("switch")
|
||||
.arg("--option")
|
||||
.arg("sandbox")
|
||||
.arg("false")
|
||||
.arg("--flake")
|
||||
.arg(".")
|
||||
.current_dir(working_dir)
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::piped())
|
||||
.spawn()?;
|
||||
|
||||
// Get stdout and stderr handles
|
||||
let stdout = child.stdout.take().expect("Failed to get stdout");
|
||||
let stderr = child.stderr.take().expect("Failed to get stderr");
|
||||
|
||||
// Create readers for both streams
|
||||
let stdout_reader = BufReader::new(stdout);
|
||||
let stderr_reader = BufReader::new(stderr);
|
||||
|
||||
let mut stdout_lines = stdout_reader.lines();
|
||||
let mut stderr_lines = stderr_reader.lines();
|
||||
|
||||
// Stream output lines in real-time
|
||||
loop {
|
||||
tokio::select! {
|
||||
// Read from stdout
|
||||
line = stdout_lines.next_line() => {
|
||||
match line {
|
||||
Ok(Some(line)) => {
|
||||
self.send_command_output(command_id, "SystemRebuild", &line).await?;
|
||||
}
|
||||
Ok(None) => {
|
||||
// stdout closed
|
||||
}
|
||||
Err(e) => {
|
||||
self.send_command_output(command_id, "SystemRebuild", &format!("stdout error: {}", e)).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Read from stderr
|
||||
line = stderr_lines.next_line() => {
|
||||
match line {
|
||||
Ok(Some(line)) => {
|
||||
self.send_command_output(command_id, "SystemRebuild", &line).await?;
|
||||
}
|
||||
Ok(None) => {
|
||||
// stderr closed
|
||||
}
|
||||
Err(e) => {
|
||||
self.send_command_output(command_id, "SystemRebuild", &format!("stderr error: {}", e)).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Wait for process completion
|
||||
result = child.wait() => {
|
||||
let status = result?;
|
||||
if status.success() {
|
||||
return Ok(());
|
||||
} else {
|
||||
return Err(anyhow::anyhow!("nixos-rebuild exited with status: {}", status));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensure git repository with output streaming
|
||||
async fn ensure_git_repository_with_output(&self, command_id: &str, git_url: &str, git_branch: &str, working_dir: &str, api_key_file: Option<&str>) -> Result<()> {
|
||||
// This is a simplified version - we can enhance this later with git output streaming
|
||||
self.ensure_git_repository(git_url, git_branch, working_dir, api_key_file).await
|
||||
}
|
||||
|
||||
/// Ensure git repository is cloned and up to date with force clone approach
|
||||
async fn ensure_git_repository(&self, git_url: &str, git_branch: &str, working_dir: &str, api_key_file: Option<&str>) -> Result<()> {
|
||||
use std::path::Path;
|
||||
|
||||
// Read API key if provided
|
||||
let auth_url = if let Some(key_file) = api_key_file {
|
||||
match tokio::fs::read_to_string(key_file).await {
|
||||
Ok(api_key) => {
|
||||
let api_key = api_key.trim();
|
||||
if !api_key.is_empty() {
|
||||
// Convert https://gitea.cmtec.se/cm/nixosbox.git to https://token@gitea.cmtec.se/cm/nixosbox.git
|
||||
if git_url.starts_with("https://") {
|
||||
let url_without_protocol = &git_url[8..]; // Remove "https://"
|
||||
format!("https://{}@{}", api_key, url_without_protocol)
|
||||
} else {
|
||||
info!("API key provided but URL is not HTTPS, using original URL");
|
||||
git_url.to_string()
|
||||
}
|
||||
} else {
|
||||
info!("API key file is empty, using original URL");
|
||||
git_url.to_string()
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
info!("Could not read API key file {}: {}, using original URL", key_file, e);
|
||||
git_url.to_string()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
git_url.to_string()
|
||||
};
|
||||
|
||||
// Always remove existing directory and do fresh clone for consistent state
|
||||
let working_path = Path::new(working_dir);
|
||||
if working_path.exists() {
|
||||
info!("Removing existing repository directory: {}", working_dir);
|
||||
if let Err(e) = tokio::fs::remove_dir_all(working_path).await {
|
||||
error!("Failed to remove existing directory: {}", e);
|
||||
return Err(anyhow::anyhow!("Failed to remove existing directory: {}", e));
|
||||
}
|
||||
}
|
||||
|
||||
info!("Force cloning git repository from {} (branch: {})", git_url, git_branch);
|
||||
|
||||
// Force clone with depth 1 for efficiency (no history needed for deployment)
|
||||
let output = tokio::process::Command::new("git")
|
||||
.arg("clone")
|
||||
.arg("--depth")
|
||||
.arg("1")
|
||||
.arg("--branch")
|
||||
.arg(git_branch)
|
||||
.arg(&auth_url)
|
||||
.arg(working_dir)
|
||||
.output()
|
||||
.await?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
error!("Git clone failed: {}", stderr);
|
||||
return Err(anyhow::anyhow!("Git clone failed: {}", stderr));
|
||||
}
|
||||
|
||||
info!("Git repository cloned successfully with latest state");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
use anyhow::Result;
|
||||
use cm_dashboard_shared::{CommandOutputMessage, MessageEnvelope, MetricMessage};
|
||||
use cm_dashboard_shared::{MessageEnvelope, MetricMessage};
|
||||
use tracing::{debug, info};
|
||||
use zmq::{Context, Socket, SocketType};
|
||||
|
||||
@@ -65,23 +65,6 @@ impl ZmqHandler {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Publish command output message via ZMQ
|
||||
pub async fn publish_command_output(&self, message: &CommandOutputMessage) -> Result<()> {
|
||||
debug!(
|
||||
"Publishing command output for host {} (command: {}): {}",
|
||||
message.hostname,
|
||||
message.command_type,
|
||||
message.output_line
|
||||
);
|
||||
|
||||
let envelope = MessageEnvelope::command_output(message.clone())?;
|
||||
let serialized = serde_json::to_vec(&envelope)?;
|
||||
|
||||
self.publisher.send(&serialized, 0)?;
|
||||
|
||||
debug!("Command output published successfully");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Send heartbeat (placeholder for future use)
|
||||
|
||||
@@ -122,13 +105,6 @@ pub enum AgentCommand {
|
||||
service_name: String,
|
||||
action: ServiceAction,
|
||||
},
|
||||
/// Rebuild NixOS system
|
||||
SystemRebuild {
|
||||
git_url: String,
|
||||
git_branch: String,
|
||||
working_dir: String,
|
||||
api_key_file: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
/// Service control actions
|
||||
|
||||
@@ -141,6 +141,7 @@ pub struct NotificationConfig {
|
||||
pub rate_limit_minutes: u64,
|
||||
}
|
||||
|
||||
|
||||
impl AgentConfig {
|
||||
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
|
||||
loader::load_config(path)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "cm-dashboard"
|
||||
version = "0.1.11"
|
||||
version = "0.1.17"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -22,7 +22,7 @@ pub struct Dashboard {
|
||||
terminal: Option<Terminal<CrosstermBackend<io::Stdout>>>,
|
||||
headless: bool,
|
||||
initial_commands_sent: std::collections::HashSet<String>,
|
||||
config: DashboardConfig,
|
||||
_config: DashboardConfig,
|
||||
}
|
||||
|
||||
impl Dashboard {
|
||||
@@ -91,7 +91,7 @@ impl Dashboard {
|
||||
(None, None)
|
||||
} else {
|
||||
// Initialize TUI app
|
||||
let tui_app = TuiApp::new();
|
||||
let tui_app = TuiApp::new(config.clone());
|
||||
|
||||
// Setup terminal
|
||||
if let Err(e) = enable_raw_mode() {
|
||||
@@ -133,7 +133,7 @@ impl Dashboard {
|
||||
terminal,
|
||||
headless,
|
||||
initial_commands_sent: std::collections::HashSet::new(),
|
||||
config,
|
||||
_config: config,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -245,24 +245,10 @@ impl Dashboard {
|
||||
|
||||
// Update TUI with new hosts and metrics (only if not headless)
|
||||
if let Some(ref mut tui_app) = self.tui_app {
|
||||
let mut connected_hosts = self
|
||||
let connected_hosts = self
|
||||
.metric_store
|
||||
.get_connected_hosts(Duration::from_secs(30));
|
||||
|
||||
// Add hosts that are rebuilding but may be temporarily disconnected
|
||||
// Use extended timeout (5 minutes) for rebuilding hosts
|
||||
let rebuilding_hosts = self
|
||||
.metric_store
|
||||
.get_connected_hosts(Duration::from_secs(300));
|
||||
|
||||
for host in rebuilding_hosts {
|
||||
if !connected_hosts.contains(&host) {
|
||||
// Check if this host is rebuilding in the UI
|
||||
if tui_app.is_host_rebuilding(&host) {
|
||||
connected_hosts.push(host);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tui_app.update_hosts(connected_hosts);
|
||||
tui_app.update_metrics(&self.metric_store);
|
||||
@@ -290,14 +276,14 @@ impl Dashboard {
|
||||
|
||||
// Render TUI (only if not headless)
|
||||
if !self.headless {
|
||||
if let (Some(ref mut terminal), Some(ref mut tui_app)) =
|
||||
(&mut self.terminal, &mut self.tui_app)
|
||||
{
|
||||
if let Err(e) = terminal.draw(|frame| {
|
||||
tui_app.render(frame, &self.metric_store);
|
||||
}) {
|
||||
error!("Error rendering TUI: {}", e);
|
||||
break;
|
||||
if let Some(ref mut terminal) = self.terminal {
|
||||
if let Some(ref mut tui_app) = self.tui_app {
|
||||
if let Err(e) = terminal.draw(|frame| {
|
||||
tui_app.render(frame, &self.metric_store);
|
||||
}) {
|
||||
error!("Error rendering TUI: {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -337,16 +323,6 @@ impl Dashboard {
|
||||
};
|
||||
self.zmq_command_sender.send_command(&hostname, agent_command).await?;
|
||||
}
|
||||
UiCommand::SystemRebuild { hostname } => {
|
||||
info!("Sending system rebuild command to {}", hostname);
|
||||
let agent_command = AgentCommand::SystemRebuild {
|
||||
git_url: self.config.system.nixos_config_git_url.clone(),
|
||||
git_branch: self.config.system.nixos_config_branch.clone(),
|
||||
working_dir: self.config.system.nixos_config_working_dir.clone(),
|
||||
api_key_file: self.config.system.nixos_config_api_key_file.clone(),
|
||||
};
|
||||
self.zmq_command_sender.send_command(&hostname, agent_command).await?;
|
||||
}
|
||||
UiCommand::TriggerBackup { hostname } => {
|
||||
info!("Trigger backup requested for {}", hostname);
|
||||
// TODO: Implement backup trigger command
|
||||
|
||||
@@ -8,6 +8,7 @@ pub struct DashboardConfig {
|
||||
pub zmq: ZmqConfig,
|
||||
pub hosts: HostsConfig,
|
||||
pub system: SystemConfig,
|
||||
pub ssh: SshConfig,
|
||||
}
|
||||
|
||||
/// ZMQ consumer configuration
|
||||
@@ -31,6 +32,13 @@ pub struct SystemConfig {
|
||||
pub nixos_config_api_key_file: Option<String>,
|
||||
}
|
||||
|
||||
/// SSH configuration for rebuild operations
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SshConfig {
|
||||
pub rebuild_user: String,
|
||||
pub rebuild_alias: String,
|
||||
}
|
||||
|
||||
impl DashboardConfig {
|
||||
pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
|
||||
let path = path.as_ref();
|
||||
|
||||
@@ -13,6 +13,7 @@ use tracing::info;
|
||||
pub mod theme;
|
||||
pub mod widgets;
|
||||
|
||||
use crate::config::DashboardConfig;
|
||||
use crate::metrics::MetricStore;
|
||||
use cm_dashboard_shared::{Metric, Status};
|
||||
use theme::{Components, Layout as ThemeLayout, Theme, Typography};
|
||||
@@ -24,7 +25,6 @@ pub enum UiCommand {
|
||||
ServiceRestart { hostname: String, service_name: String },
|
||||
ServiceStart { hostname: String, service_name: String },
|
||||
ServiceStop { hostname: String, service_name: String },
|
||||
SystemRebuild { hostname: String },
|
||||
TriggerBackup { hostname: String },
|
||||
}
|
||||
|
||||
@@ -33,8 +33,6 @@ pub enum UiCommand {
|
||||
pub enum CommandStatus {
|
||||
/// Command is executing
|
||||
InProgress { command_type: CommandType, target: String, start_time: std::time::Instant },
|
||||
/// Command completed successfully
|
||||
Success { command_type: CommandType, completed_at: std::time::Instant },
|
||||
}
|
||||
|
||||
/// Types of commands for status tracking
|
||||
@@ -43,7 +41,6 @@ pub enum CommandType {
|
||||
ServiceRestart,
|
||||
ServiceStart,
|
||||
ServiceStop,
|
||||
SystemRebuild,
|
||||
BackupTrigger,
|
||||
}
|
||||
|
||||
@@ -98,7 +95,7 @@ pub struct TerminalPopup {
|
||||
/// Is the popup currently visible
|
||||
pub visible: bool,
|
||||
/// Command being executed
|
||||
pub command_type: CommandType,
|
||||
pub _command_type: CommandType,
|
||||
/// Target hostname
|
||||
pub hostname: String,
|
||||
/// Target service/operation name
|
||||
@@ -112,10 +109,10 @@ pub struct TerminalPopup {
|
||||
}
|
||||
|
||||
impl TerminalPopup {
|
||||
pub fn new(command_type: CommandType, hostname: String, target: String) -> Self {
|
||||
pub fn _new(command_type: CommandType, hostname: String, target: String) -> Self {
|
||||
Self {
|
||||
visible: true,
|
||||
command_type,
|
||||
_command_type: command_type,
|
||||
hostname,
|
||||
target,
|
||||
output_lines: Vec::new(),
|
||||
@@ -155,10 +152,12 @@ pub struct TuiApp {
|
||||
user_navigated_away: bool,
|
||||
/// Terminal popup for streaming command output
|
||||
terminal_popup: Option<TerminalPopup>,
|
||||
/// Dashboard configuration
|
||||
config: DashboardConfig,
|
||||
}
|
||||
|
||||
impl TuiApp {
|
||||
pub fn new() -> Self {
|
||||
pub fn new(config: DashboardConfig) -> Self {
|
||||
Self {
|
||||
host_widgets: HashMap::new(),
|
||||
current_host: None,
|
||||
@@ -168,6 +167,7 @@ impl TuiApp {
|
||||
should_quit: false,
|
||||
user_navigated_away: false,
|
||||
terminal_popup: None,
|
||||
config,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,7 +184,6 @@ impl TuiApp {
|
||||
self.check_command_timeouts();
|
||||
|
||||
// Check for rebuild completion by agent hash change
|
||||
self.check_rebuild_completion(metric_store);
|
||||
|
||||
if let Some(hostname) = self.current_host.clone() {
|
||||
// Only update widgets if we have metrics for this host
|
||||
@@ -257,9 +256,9 @@ impl TuiApp {
|
||||
// Sort hosts alphabetically
|
||||
let mut sorted_hosts = hosts.clone();
|
||||
|
||||
// Keep hosts that are undergoing SystemRebuild even if they're offline
|
||||
// Keep hosts that have ongoing commands even if they're offline
|
||||
for (hostname, host_widgets) in &self.host_widgets {
|
||||
if let Some(CommandStatus::InProgress { command_type: CommandType::SystemRebuild, .. }) = &host_widgets.command_status {
|
||||
if let Some(CommandStatus::InProgress { .. }) = &host_widgets.command_status {
|
||||
if !sorted_hosts.contains(hostname) {
|
||||
sorted_hosts.push(hostname.clone());
|
||||
}
|
||||
@@ -343,16 +342,20 @@ impl TuiApp {
|
||||
KeyCode::Char('r') => {
|
||||
match self.focused_panel {
|
||||
PanelType::System => {
|
||||
// System rebuild command
|
||||
// Simple tmux popup with SSH rebuild using configured user and alias
|
||||
if let Some(hostname) = self.current_host.clone() {
|
||||
self.start_command(&hostname, CommandType::SystemRebuild, hostname.clone());
|
||||
// Open terminal popup for real-time output
|
||||
self.terminal_popup = Some(TerminalPopup::new(
|
||||
CommandType::SystemRebuild,
|
||||
hostname.clone(),
|
||||
"NixOS Rebuild".to_string()
|
||||
));
|
||||
return Ok(Some(UiCommand::SystemRebuild { hostname }));
|
||||
// Launch tmux popup with SSH using config values
|
||||
let ssh_command = format!(
|
||||
"ssh -tt {}@{} 'bash -ic {}'",
|
||||
self.config.ssh.rebuild_user,
|
||||
hostname,
|
||||
self.config.ssh.rebuild_alias
|
||||
);
|
||||
std::process::Command::new("tmux")
|
||||
.arg("display-popup")
|
||||
.arg(&ssh_command)
|
||||
.spawn()
|
||||
.ok(); // Ignore errors, tmux will handle them
|
||||
}
|
||||
}
|
||||
PanelType::Services => {
|
||||
@@ -453,17 +456,6 @@ impl TuiApp {
|
||||
info!("Switched to host: {}", self.current_host.as_ref().unwrap());
|
||||
}
|
||||
|
||||
/// Check if a host is currently rebuilding
|
||||
pub fn is_host_rebuilding(&self, hostname: &str) -> bool {
|
||||
if let Some(host_widgets) = self.host_widgets.get(hostname) {
|
||||
matches!(
|
||||
&host_widgets.command_status,
|
||||
Some(CommandStatus::InProgress { command_type: CommandType::SystemRebuild, .. })
|
||||
)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Switch to next panel (Shift+Tab) - only cycles through visible panels
|
||||
pub fn next_panel(&mut self) {
|
||||
@@ -515,14 +507,10 @@ impl TuiApp {
|
||||
}
|
||||
|
||||
/// Mark command as completed successfully
|
||||
pub fn complete_command(&mut self, hostname: &str) {
|
||||
pub fn _complete_command(&mut self, hostname: &str) {
|
||||
if let Some(host_widgets) = self.host_widgets.get_mut(hostname) {
|
||||
if let Some(CommandStatus::InProgress { command_type, .. }) = &host_widgets.command_status {
|
||||
host_widgets.command_status = Some(CommandStatus::Success {
|
||||
command_type: command_type.clone(),
|
||||
completed_at: Instant::now(),
|
||||
});
|
||||
}
|
||||
// Simply clear the command status when completed
|
||||
host_widgets.command_status = None;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -533,22 +521,13 @@ impl TuiApp {
|
||||
let mut hosts_to_clear = Vec::new();
|
||||
|
||||
for (hostname, host_widgets) in &self.host_widgets {
|
||||
if let Some(CommandStatus::InProgress { command_type, start_time, .. }) = &host_widgets.command_status {
|
||||
let timeout_duration = match command_type {
|
||||
CommandType::SystemRebuild => Duration::from_secs(300), // 5 minutes for rebuilds
|
||||
_ => Duration::from_secs(30), // 30 seconds for service commands
|
||||
};
|
||||
if let Some(CommandStatus::InProgress { command_type: _, start_time, .. }) = &host_widgets.command_status {
|
||||
let timeout_duration = Duration::from_secs(30); // 30 seconds for service commands
|
||||
|
||||
if now.duration_since(*start_time) > timeout_duration {
|
||||
hosts_to_clear.push(hostname.clone());
|
||||
}
|
||||
}
|
||||
// Also clear success/failed status after display time
|
||||
else if let Some(CommandStatus::Success { completed_at, .. }) = &host_widgets.command_status {
|
||||
if now.duration_since(*completed_at) > Duration::from_secs(3) {
|
||||
hosts_to_clear.push(hostname.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear timed out commands
|
||||
@@ -569,7 +548,7 @@ impl TuiApp {
|
||||
}
|
||||
|
||||
/// Close terminal popup for a specific hostname
|
||||
pub fn close_terminal_popup(&mut self, hostname: &str) {
|
||||
pub fn _close_terminal_popup(&mut self, hostname: &str) {
|
||||
if let Some(ref mut popup) = self.terminal_popup {
|
||||
if popup.hostname == hostname {
|
||||
popup.close();
|
||||
@@ -578,32 +557,6 @@ impl TuiApp {
|
||||
}
|
||||
}
|
||||
|
||||
/// Check for rebuild completion by detecting agent hash changes
|
||||
pub fn check_rebuild_completion(&mut self, metric_store: &MetricStore) {
|
||||
let mut hosts_to_complete = Vec::new();
|
||||
|
||||
for (hostname, host_widgets) in &self.host_widgets {
|
||||
if let Some(CommandStatus::InProgress { command_type: CommandType::SystemRebuild, .. }) = &host_widgets.command_status {
|
||||
// Check if agent hash has changed (indicating successful rebuild)
|
||||
if let Some(agent_hash_metric) = metric_store.get_metric(hostname, "system_agent_hash") {
|
||||
if let cm_dashboard_shared::MetricValue::String(current_hash) = &agent_hash_metric.value {
|
||||
// Compare with stored hash (if we have one)
|
||||
if let Some(stored_hash) = host_widgets.system_widget.get_agent_hash() {
|
||||
if current_hash != stored_hash {
|
||||
// Agent hash changed - rebuild completed successfully
|
||||
hosts_to_complete.push(hostname.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mark rebuilds as completed
|
||||
for hostname in hosts_to_complete {
|
||||
self.complete_command(&hostname);
|
||||
}
|
||||
}
|
||||
|
||||
/// Scroll the focused panel up or down
|
||||
pub fn scroll_focused_panel(&mut self, direction: i32) {
|
||||
@@ -774,13 +727,9 @@ impl TuiApp {
|
||||
// Check if this host has a command status that affects the icon
|
||||
let (status_icon, status_color) = if let Some(host_widgets) = self.host_widgets.get(host) {
|
||||
match &host_widgets.command_status {
|
||||
Some(CommandStatus::InProgress { command_type: CommandType::SystemRebuild, .. }) => {
|
||||
// Show blue circular arrow during rebuild
|
||||
("↻", Theme::highlight())
|
||||
}
|
||||
Some(CommandStatus::Success { command_type: CommandType::SystemRebuild, .. }) => {
|
||||
// Show green checkmark for successful rebuild
|
||||
("✓", Theme::success())
|
||||
Some(CommandStatus::InProgress { .. }) => {
|
||||
// Show working indicator for in-progress commands
|
||||
("⏳", Theme::highlight())
|
||||
}
|
||||
_ => {
|
||||
// Normal status icon based on metrics
|
||||
@@ -950,7 +899,7 @@ impl TuiApp {
|
||||
/// Render terminal popup with streaming output
|
||||
fn render_terminal_popup(&self, frame: &mut Frame, area: Rect, popup: &TerminalPopup) {
|
||||
use ratatui::{
|
||||
style::{Color, Modifier, Style},
|
||||
style::{Color, Style},
|
||||
text::{Line, Span},
|
||||
widgets::{Block, Borders, Clear, Paragraph, Wrap},
|
||||
};
|
||||
|
||||
@@ -259,7 +259,12 @@ impl Widget for BackupWidget {
|
||||
services.sort_by(|a, b| a.name.cmp(&b.name));
|
||||
self.service_metrics = services;
|
||||
|
||||
self.has_data = !metrics.is_empty();
|
||||
// Only show backup panel if we have meaningful backup data
|
||||
self.has_data = !metrics.is_empty() && (
|
||||
self.last_run_timestamp.is_some() ||
|
||||
self.total_repo_size_gb.is_some() ||
|
||||
!self.service_metrics.is_empty()
|
||||
);
|
||||
|
||||
debug!(
|
||||
"Backup widget updated: status={:?}, services={}, total_size={:?}GB",
|
||||
|
||||
@@ -146,7 +146,6 @@ impl ServicesWidget {
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {} // Success/Failed states will show normal status
|
||||
}
|
||||
}
|
||||
|
||||
@@ -561,7 +560,6 @@ impl ServicesWidget {
|
||||
StatusIcons::create_status_spans(*line_status, line_text)
|
||||
}
|
||||
}
|
||||
_ => StatusIcons::create_status_spans(*line_status, line_text)
|
||||
}
|
||||
} else {
|
||||
StatusIcons::create_status_spans(*line_status, line_text)
|
||||
|
||||
@@ -129,7 +129,7 @@ impl SystemWidget {
|
||||
}
|
||||
|
||||
/// Get the current agent hash for rebuild completion detection
|
||||
pub fn get_agent_hash(&self) -> Option<&String> {
|
||||
pub fn _get_agent_hash(&self) -> Option<&String> {
|
||||
self.agent_hash.as_ref()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "cm-dashboard-shared"
|
||||
version = "0.1.11"
|
||||
version = "0.1.17"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
Reference in New Issue
Block a user