Add shutdown machine feature with configurable command

Backend:
- New migration: add shutdown_command TEXT column to machines table
- Machine model updated with ShutdownCommand field (Create/GetAll/GetByID/Update)
- MachineRequest/MachineResponse DTOs updated with shutdown_command field
- New ShutdownResponse DTO
- New POST /api/machines/{id}/shutdown handler via SSH
- Refactor sshmanager/testconn.go: extract dialSSH() helper shared with shutdown.go
- New sshmanager/shutdown.go: RunRemoteCommand with 15s timeout

Frontend:
- New shutdownMachine() API helper and ShutdownResponse type in client.ts
- New shutdown_command field in MachineForm
- Power button (amber) in machines table actions
- Shutdown confirmation modal with WoL warning notice
- shutdown_command input field in machine edit/create form
- Machine interface updated with shutdown_command and last_seen_at fields
This commit is contained in:
2026-07-13 10:02:18 -04:00
parent bf9bcccde2
commit bc3bc44c4a
11 changed files with 282 additions and 32 deletions
+20 -16
View File
@@ -16,24 +16,24 @@ import (
)
type ConnResult struct {
Success bool
Output string
Error string
Success bool
Output string
Error string
Fingerprint string
}
func TestSSHConnection(ctx context.Context, host string, port int, user, privKeyPath, knownHostsPath string, strictHostKeyChecking bool) (*ConnResult, error) {
func dialSSH(ctx context.Context, host string, port int, user, privKeyPath, knownHostsPath string, strictHostKeyChecking bool) (*ssh.Client, string, error) {
addr := fmt.Sprintf("%s:%d", host, port)
auths := []ssh.AuthMethod{}
if privKeyPath != "" {
key, err := os.ReadFile(privKeyPath)
if err != nil {
return nil, fmt.Errorf("reading private key: %w", err)
return nil, "", fmt.Errorf("reading private key: %w", err)
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, fmt.Errorf("parsing private key: %w", err)
return nil, "", fmt.Errorf("parsing private key: %w", err)
}
auths = append(auths, ssh.PublicKeys(signer))
}
@@ -71,23 +71,27 @@ func TestSSHConnection(ctx context.Context, host string, port int, user, privKey
conn, err := ssh.Dial("tcp", addr, cfg)
if err != nil {
if strings.Contains(err.Error(), "known_hosts") || strings.Contains(err.Error(), "host key") {
return &ConnResult{
Success: false,
Error: fmt.Sprintf("host key verification failed: %v", err),
Fingerprint: capturedFingerprint,
}, nil
return nil, capturedFingerprint, fmt.Errorf("host key verification failed: %v", err)
}
return nil, capturedFingerprint, fmt.Errorf("connection failed: %v", err)
}
return conn, capturedFingerprint, nil
}
func TestSSHConnection(ctx context.Context, host string, port int, user, privKeyPath, knownHostsPath string, strictHostKeyChecking bool) (*ConnResult, error) {
conn, fingerprint, err := dialSSH(ctx, host, port, user, privKeyPath, knownHostsPath, strictHostKeyChecking)
if err != nil {
return &ConnResult{
Success: false,
Error: fmt.Sprintf("connection failed: %v", err),
Fingerprint: capturedFingerprint,
Error: err.Error(),
Fingerprint: fingerprint,
}, nil
}
defer conn.Close()
session, err := conn.NewSession()
if err != nil {
return &ConnResult{Success: false, Error: fmt.Sprintf("session: %v", err), Fingerprint: capturedFingerprint}, nil
return &ConnResult{Success: false, Error: fmt.Sprintf("session: %v", err), Fingerprint: fingerprint}, nil
}
defer session.Close()
@@ -99,13 +103,13 @@ func TestSSHConnection(ctx context.Context, host string, port int, user, privKey
return &ConnResult{
Success: false,
Error: fmt.Sprintf("exec: %v, stderr: %s", err, stderr.String()),
Fingerprint: capturedFingerprint,
Fingerprint: fingerprint,
}, nil
}
return &ConnResult{
Success: true,
Output: stdout.String(),
Fingerprint: capturedFingerprint,
Fingerprint: fingerprint,
}, nil
}