/** * Machine Status Command * Show detailed machine status with live connectivity and resource usage */ import { detectMachineInfo, testSshConnection } from '../../services/machine-detector'; import { getMachineByHostname, getModulesOnMachine } from '../../services/machine-pool'; import { ManagedSshKey } from '../../services/ssh-key-manager'; import { celiloIntro } from '../prompts'; import type { CommandResult } from '../types'; /** * Handle machine status command * * @param args - Command arguments [hostname] * @param flags - Command flags */ export async function handleMachineStatus( args: string[], _flags: Record = {}, ): Promise { try { celiloIntro('Machine Status'); // Get hostname from args const hostname = args[0]; if (!hostname) { return { success: false, error: 'Hostname is required\n\nUsage: celilo machine status ', }; } // Verify machine exists const machine = await getMachineByHostname(hostname); if (!machine) { return { success: false, error: `Machine not found: ${hostname}`, }; } console.log('\nMachine Information'); console.log('─────────────────'); console.log(`Hostname: ${machine.hostname}`); console.log(`Zone: ${machine.zone}`); console.log(`IP: ${machine.ipAddress}`); console.log(`SSH User: ${machine.sshUser}`); console.log(''); console.log('Hardware Specifications'); console.log('──────────────────────'); console.log(`CPU: ${machine.hardware.cpu_cores} cores`); console.log(`Memory: ${machine.hardware.memory_mb} MB`); console.log(`Disk: ${machine.hardware.disk_gb} GB`); console.log(''); console.log('Assigned Modules'); console.log('───────────────'); const occupants = getModulesOnMachine(machine.id); if (occupants.length === 0) { console.log('None (available)'); } else { for (const moduleId of occupants) { console.log(` - ${moduleId}`); } // No "Resource Allocation" block (celilo#773). It printed // `0 / ` for every resource on every machine, because its source // returned hard-coded zeros behind a TODO — an operator reading it would // conclude a fully-committed box was entirely free. The machine's own // hardware is already printed above; that part is real. } console.log(''); console.log('Connectivity Status'); console.log('──────────────────'); console.log('Testing SSH connection...'); // Get SSH key and test connectivity const managedKey = new ManagedSshKey(machine.id); try { await managedKey.use(async (keyPath) => { const canConnect = await testSshConnection(machine.ipAddress, machine.sshUser, keyPath); if (canConnect) { console.log('✓ SSH connection: OK'); // Try to get live hardware info console.log('\nQuerying current resource usage...'); try { const liveInfo = await detectMachineInfo(machine.ipAddress, machine.sshUser, keyPath); console.log('✓ Live hardware info retrieved:'); console.log(` CPU: ${liveInfo.hardware.cpu_cores} cores`); console.log(` Memory: ${liveInfo.hardware.memory_mb} MB`); console.log(` Disk: ${liveInfo.hardware.disk_gb} GB`); console.log(` OS: ${liveInfo.osInfo}`); } catch (error) { console.log('✗ Could not retrieve live hardware info'); if (error instanceof Error) { console.log(` Error: ${error.message}`); } } } else { console.log('✗ SSH connection: FAILED'); console.log(' Machine may be offline or SSH key may have changed'); } }); } catch (error) { console.log('✗ SSH connection: ERROR'); if (error instanceof Error) { console.log(` Error: ${error.message}`); } } console.log(''); console.log(`Last updated: ${machine.updatedAt.toISOString()}\n`); return { success: true, message: `Retrieved status for machine: ${hostname}`, }; } catch (error) { return { success: false, error: `Failed to get machine status: ${error instanceof Error ? error.message : String(error)}`, }; } }