Phase 4 of the daemon god-struct refactor. VM lifecycle, create-op
registry, handle cache, disk provisioning, stats polling, ports
query, and the per-VM lock set all move off *Daemon onto *VMService.
Daemon keeps thin forwarders only for FindVM / TouchVM (dispatch
surface) and is otherwise out of VM lifecycle. Lazy-init via
d.vmSvc() mirrors the earlier services so test literals like
\`&Daemon{store: db, runner: r}\` still get a functional service
without spelling one out.
Three small cleanups along the way:
* preflight helpers (validateStartPrereqs / addBaseStartPrereqs
/ addBaseStartCommandPrereqs / validateWorkDiskResizePrereqs)
move with the VM methods that call them.
* cleanupRuntime / rebuildDNS move to *VMService, with
HostNetwork primitives (findFirecrackerPID, cleanupDMSnapshot,
killVMProcess, releaseTap, waitForExit, sendCtrlAltDel)
reached through s.net instead of the hostNet() facade.
* vsockAgentBinary becomes a package-level function so both
*Daemon (doctor) and *VMService (preflight) call one entry
point instead of each owning a forwarder method.
WorkspaceService's peer deps switch from eager method values to
closures — vmSvc() constructs VMService with WorkspaceService as a
peer, so resolving d.vmSvc().FindVM at construction time recursed
through workspaceSvc() → vmSvc(). Closures defer the lookup to call
time.
Pure code motion: build + unit tests green, lint clean. No RPC
surface or lock-ordering changes.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
189 lines
4.4 KiB
Go
189 lines
4.4 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"banger/internal/api"
|
|
"banger/internal/model"
|
|
)
|
|
|
|
func (op *vmCreateOperationState) ID() string { return op.snapshot().ID }
|
|
func (op *vmCreateOperationState) IsDone() bool { return op.snapshot().Done }
|
|
func (op *vmCreateOperationState) UpdatedAt() time.Time { return op.snapshot().UpdatedAt }
|
|
func (op *vmCreateOperationState) Cancel() { op.cancelOperation() }
|
|
|
|
type vmCreateProgressKey struct{}
|
|
|
|
type vmCreateOperationState struct {
|
|
mu sync.Mutex
|
|
cancel context.CancelFunc
|
|
op api.VMCreateOperation
|
|
}
|
|
|
|
func newVMCreateOperationState() (*vmCreateOperationState, error) {
|
|
id, err := model.NewID()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
now := model.Now()
|
|
return &vmCreateOperationState{
|
|
op: api.VMCreateOperation{
|
|
ID: id,
|
|
Stage: "queued",
|
|
Detail: "waiting to start",
|
|
StartedAt: now,
|
|
UpdatedAt: now,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func withVMCreateProgress(ctx context.Context, op *vmCreateOperationState) context.Context {
|
|
if op == nil {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, vmCreateProgressKey{}, op)
|
|
}
|
|
|
|
func vmCreateProgressFromContext(ctx context.Context) *vmCreateOperationState {
|
|
if ctx == nil {
|
|
return nil
|
|
}
|
|
op, _ := ctx.Value(vmCreateProgressKey{}).(*vmCreateOperationState)
|
|
return op
|
|
}
|
|
|
|
func vmCreateStage(ctx context.Context, stage, detail string) {
|
|
if op := vmCreateProgressFromContext(ctx); op != nil {
|
|
op.stage(stage, detail)
|
|
}
|
|
}
|
|
|
|
func vmCreateBindVM(ctx context.Context, vm model.VMRecord) {
|
|
if op := vmCreateProgressFromContext(ctx); op != nil {
|
|
op.bindVM(vm)
|
|
}
|
|
}
|
|
|
|
func (op *vmCreateOperationState) setCancel(cancel context.CancelFunc) {
|
|
op.mu.Lock()
|
|
defer op.mu.Unlock()
|
|
op.cancel = cancel
|
|
}
|
|
|
|
func (op *vmCreateOperationState) bindVM(vm model.VMRecord) {
|
|
op.mu.Lock()
|
|
defer op.mu.Unlock()
|
|
op.op.VMID = vm.ID
|
|
op.op.VMName = vm.Name
|
|
}
|
|
|
|
func (op *vmCreateOperationState) stage(stage, detail string) {
|
|
op.mu.Lock()
|
|
defer op.mu.Unlock()
|
|
stage = strings.TrimSpace(stage)
|
|
detail = strings.TrimSpace(detail)
|
|
if stage == "" {
|
|
stage = op.op.Stage
|
|
}
|
|
if stage == op.op.Stage && detail == op.op.Detail {
|
|
return
|
|
}
|
|
op.op.Stage = stage
|
|
op.op.Detail = detail
|
|
op.op.UpdatedAt = model.Now()
|
|
}
|
|
|
|
func (op *vmCreateOperationState) done(vm model.VMRecord) {
|
|
op.mu.Lock()
|
|
defer op.mu.Unlock()
|
|
vmCopy := vm
|
|
op.op.VMID = vm.ID
|
|
op.op.VMName = vm.Name
|
|
op.op.Stage = "ready"
|
|
op.op.Detail = "vm is ready"
|
|
op.op.Done = true
|
|
op.op.Success = true
|
|
op.op.Error = ""
|
|
op.op.VM = &vmCopy
|
|
op.op.UpdatedAt = model.Now()
|
|
}
|
|
|
|
func (op *vmCreateOperationState) fail(err error) {
|
|
op.mu.Lock()
|
|
defer op.mu.Unlock()
|
|
op.op.Done = true
|
|
op.op.Success = false
|
|
if err != nil {
|
|
op.op.Error = err.Error()
|
|
}
|
|
if strings.TrimSpace(op.op.Detail) == "" {
|
|
op.op.Detail = "vm create failed"
|
|
}
|
|
op.op.UpdatedAt = model.Now()
|
|
}
|
|
|
|
func (op *vmCreateOperationState) snapshot() api.VMCreateOperation {
|
|
op.mu.Lock()
|
|
defer op.mu.Unlock()
|
|
snapshot := op.op
|
|
if snapshot.VM != nil {
|
|
vmCopy := *snapshot.VM
|
|
snapshot.VM = &vmCopy
|
|
}
|
|
return snapshot
|
|
}
|
|
|
|
func (op *vmCreateOperationState) cancelOperation() {
|
|
op.mu.Lock()
|
|
cancel := op.cancel
|
|
op.mu.Unlock()
|
|
if cancel != nil {
|
|
cancel()
|
|
}
|
|
}
|
|
|
|
func (s *VMService) BeginVMCreate(_ context.Context, params api.VMCreateParams) (api.VMCreateOperation, error) {
|
|
op, err := newVMCreateOperationState()
|
|
if err != nil {
|
|
return api.VMCreateOperation{}, err
|
|
}
|
|
createCtx, cancel := context.WithCancel(context.Background())
|
|
op.setCancel(cancel)
|
|
s.createOps.Insert(op)
|
|
go s.runVMCreateOperation(withVMCreateProgress(createCtx, op), op, params)
|
|
return op.snapshot(), nil
|
|
}
|
|
|
|
func (s *VMService) runVMCreateOperation(ctx context.Context, op *vmCreateOperationState, params api.VMCreateParams) {
|
|
vm, err := s.CreateVM(ctx, params)
|
|
if err != nil {
|
|
op.fail(err)
|
|
return
|
|
}
|
|
op.done(vm)
|
|
}
|
|
|
|
func (s *VMService) VMCreateStatus(_ context.Context, id string) (api.VMCreateOperation, error) {
|
|
op, ok := s.createOps.Get(strings.TrimSpace(id))
|
|
if !ok {
|
|
return api.VMCreateOperation{}, fmt.Errorf("vm create operation not found: %s", id)
|
|
}
|
|
return op.snapshot(), nil
|
|
}
|
|
|
|
func (s *VMService) CancelVMCreate(_ context.Context, id string) error {
|
|
op, ok := s.createOps.Get(strings.TrimSpace(id))
|
|
if !ok {
|
|
return fmt.Errorf("vm create operation not found: %s", id)
|
|
}
|
|
op.cancelOperation()
|
|
return nil
|
|
}
|
|
|
|
func (s *VMService) pruneVMCreateOperations(olderThan time.Time) {
|
|
s.createOps.Prune(olderThan)
|
|
}
|