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:
@@ -0,0 +1,59 @@
|
||||
package sshmanager
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ShutdownResult struct {
|
||||
Success bool
|
||||
Output string
|
||||
Error string
|
||||
}
|
||||
|
||||
func RunRemoteCommand(ctx context.Context, host string, port int, user, privKeyPath, knownHostsPath string, strictHostKeyChecking bool, command string) (*ShutdownResult, error) {
|
||||
conn, _, err := dialSSH(ctx, host, port, user, privKeyPath, knownHostsPath, strictHostKeyChecking)
|
||||
if err != nil {
|
||||
return &ShutdownResult{Success: false, Error: err.Error()}, nil
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
session, err := conn.NewSession()
|
||||
if err != nil {
|
||||
return &ShutdownResult{Success: false, Error: fmt.Sprintf("session: %v", err)}, nil
|
||||
}
|
||||
defer session.Close()
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
session.Stdout = &stdout
|
||||
session.Stderr = &stderr
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
defer cancel()
|
||||
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- session.Run(command)
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
return &ShutdownResult{
|
||||
Success: false,
|
||||
Error: fmt.Sprintf("exec: %v, stderr: %s", err, stderr.String()),
|
||||
}, nil
|
||||
}
|
||||
return &ShutdownResult{
|
||||
Success: true,
|
||||
Output: stdout.String(),
|
||||
}, nil
|
||||
case <-ctx.Done():
|
||||
return &ShutdownResult{
|
||||
Success: false,
|
||||
Error: "command timed out after 15 seconds (machine may be shutting down)",
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user