Every VM action had two bindings: one by name and one by driver and name. The window used only the second, so the first is gone and the WithDriver suffix with it. Unused bindings and frontend exports are removed too. Tests now reject bindings the window never calls, stale generated bindings and unused frontend exports. TypeScript rejects unused locals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
368 lines
11 KiB
Go
368 lines
11 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
"github.com/Nwflower/reims-go/internal/plugin/builtin"
|
|
"github.com/Nwflower/reims-go/internal/vmstart"
|
|
)
|
|
|
|
func (a *App) emitVMLogForDriver(driverID, name, line string) {
|
|
a.emitLog(vmKeyForDriver(driverID, name), line)
|
|
}
|
|
|
|
func (a *App) startHookPayload(_ context.Context, d driver.Driver, vm driver.VMInfo, base plugin.BeforeStartPayload) (plugin.BeforeStartPayload, error) {
|
|
if d.ID() != builtin.IDDriverReimsMacOS {
|
|
return base, nil
|
|
}
|
|
prep, err := a.prep(vm.Name)
|
|
if err != nil {
|
|
return base, err
|
|
}
|
|
caps := prep.Runtime.Manifest.EffectiveCapabilities()
|
|
rep := host.Inspect(a.currentDataRoot())
|
|
base.RuntimeKind = prep.Runtime.Manifest.EffectiveKind()
|
|
base.RuntimeCaps = caps
|
|
base.HostFacts = buildHostFacts(rep, caps)
|
|
return base, nil
|
|
}
|
|
|
|
func (a *App) currentDataRoot() string {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
return a.cfg.DataRoot
|
|
}
|
|
|
|
func (a *App) evaluateOptimizations(rep host.Report) []host.Optimization {
|
|
core := host.EvaluateCoreOptimizations(rep)
|
|
a.mu.Lock()
|
|
router := a.driverRouter
|
|
a.mu.Unlock()
|
|
var extra []host.Optimization
|
|
if router != nil {
|
|
for _, drv := range router.Drivers() {
|
|
if advisor, ok := drv.(driver.OptimizationAdvisor); ok {
|
|
extra = append(extra, advisor.EvaluateOptimizations(context.Background(), rep)...)
|
|
}
|
|
}
|
|
}
|
|
if len(core) == 0 {
|
|
return extra
|
|
}
|
|
out := append([]host.Optimization{core[0]}, extra...)
|
|
return append(out, core[1:]...)
|
|
}
|
|
|
|
func (a *App) resolveVMInfo(driverID, name string) (driver.VMInfo, error) {
|
|
a.mu.Lock()
|
|
svc := a.lifecycleSvc
|
|
a.mu.Unlock()
|
|
if svc != nil {
|
|
_, vm, err := svc.Resolve(context.Background(), driverID, name)
|
|
return vm, err
|
|
}
|
|
if driverID != "" && driver.CanonicalDriverID(driverID) != driver.BuiltinReimsMacOSDriverID {
|
|
return driver.VMInfo{}, fmt.Errorf("driver %q is unavailable without the plugin manager", driverID)
|
|
}
|
|
detail, err := a.GetVMDetail(name)
|
|
if err != nil {
|
|
return driver.VMInfo{}, err
|
|
}
|
|
return driver.VMInfo{Name: detail.Name, DriverID: driver.BuiltinReimsMacOSDriverID, State: detail.State, CPUs: detail.CPUs, Memory: detail.Memory, DiskSummary: detail.Disk, DiskPath: detail.DiskPath(), Directory: detail.Directory, Network: detail.Network, SSHPort: detail.SSHPort}, nil
|
|
}
|
|
|
|
func (a *App) GetVMInfo(driverID, name string) (driver.VMInfo, error) {
|
|
a.mu.Lock()
|
|
svc := a.lifecycleSvc
|
|
a.mu.Unlock()
|
|
if svc == nil {
|
|
return driver.VMInfo{}, errors.New("driver lifecycle is not initialized")
|
|
}
|
|
_, vm, err := svc.Resolve(context.Background(), driverID, name)
|
|
return vm, err
|
|
}
|
|
|
|
func (a *App) StartVM(driverID, name string) error {
|
|
if err := a.beginVMStart(); err != nil {
|
|
return err
|
|
}
|
|
defer a.vmStarts.Done()
|
|
if err := a.requireWritableDataRoot(); err != nil {
|
|
return err
|
|
}
|
|
a.mu.Lock()
|
|
svc := a.lifecycleSvc
|
|
a.mu.Unlock()
|
|
if svc == nil {
|
|
if driverID != "" && driver.CanonicalDriverID(driverID) != driver.BuiltinReimsMacOSDriverID {
|
|
return fmt.Errorf("driver %q is unavailable without the plugin manager", driverID)
|
|
}
|
|
vm, err := a.resolveVMInfo(driver.BuiltinReimsMacOSDriverID, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return a.withComputerUseVM(context.Background(), vm.Directory, func(context.Context) error {
|
|
return a.startMacOSVM(vm.Name)
|
|
})
|
|
}
|
|
_, vm, err := svc.Resolve(context.Background(), driverID, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if vm.State == "suspended" {
|
|
return a.ResumeVM(vm.DriverID, vm.Name)
|
|
}
|
|
return a.withComputerUseVM(context.Background(), vm.Directory, func(ctx context.Context) error {
|
|
return a.startResolvedVMLocked(ctx, svc, vm)
|
|
})
|
|
}
|
|
|
|
func (a *App) startResolvedVMLocked(ctx context.Context, svc interface {
|
|
Start(context.Context, driver.VMRef, io.Writer, io.Writer) (driver.StartResult, error)
|
|
}, vm driver.VMInfo) error {
|
|
if svc == nil {
|
|
return a.startMacOSVM(vm.Name)
|
|
}
|
|
ref := vm.Ref()
|
|
key := ref.Key()
|
|
detail := vm
|
|
a.mu.Lock()
|
|
a.transitions[key] = "starting"
|
|
delete(a.pendingChanges, key)
|
|
a.mu.Unlock()
|
|
a.emitStateChanged()
|
|
startAt := time.Now()
|
|
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
rep := host.Inspect(a.currentDataRoot())
|
|
a.emitStartSummary(ref.Name, detail, rep, "")
|
|
}
|
|
w := vmstart.LineWriter(func(line string) { a.emitVMLogForDriver(ref.DriverID, ref.Name, line) })
|
|
a.emitVMLogForDriver(ref.DriverID, ref.Name, "Starting "+ref.Name+"...")
|
|
result, err := svc.Start(ctx, ref, w, w)
|
|
if err != nil {
|
|
a.mu.Lock()
|
|
delete(a.transitions, key)
|
|
a.mu.Unlock()
|
|
a.emitVMLogForDriver(ref.DriverID, ref.Name, "Start error: "+err.Error())
|
|
a.emitStateChanged()
|
|
return err
|
|
}
|
|
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
for _, action := range result.PostStart {
|
|
if action.Kind != "qmp.watch_cold_reboot" {
|
|
continue
|
|
}
|
|
if addr, _ := action.Params["qmp_addr"].(string); addr != "" {
|
|
a.watchWin10ColdReboot(ref.Name, addr)
|
|
}
|
|
}
|
|
a.startVMMonitor(ref.Name, detail, startAt)
|
|
}
|
|
a.wakePoll()
|
|
a.emitStateChanged()
|
|
return nil
|
|
}
|
|
|
|
func (a *App) StopVM(driverID, name string, force bool) error {
|
|
return a.stopVM(context.Background(), driverID, name, force)
|
|
}
|
|
|
|
func (a *App) stopVM(ctx context.Context, driverID, name string, force bool) error {
|
|
a.mu.Lock()
|
|
svc := a.lifecycleSvc
|
|
a.mu.Unlock()
|
|
if svc == nil {
|
|
if driverID != "" && driver.CanonicalDriverID(driverID) != driver.BuiltinReimsMacOSDriverID {
|
|
return errors.New("driver is unavailable without the plugin manager")
|
|
}
|
|
return a.stopMacOSVM(ctx, name, force)
|
|
}
|
|
_, vm, err := svc.Resolve(ctx, driverID, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return a.withComputerUseVM(ctx, vm.Directory, func(operationCtx context.Context) error {
|
|
ref := vm.Ref()
|
|
key := ref.Key()
|
|
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
a.cancelRebootWatcher(ref.Name)
|
|
stopVMMonitor(ref.Name)
|
|
}
|
|
a.mu.Lock()
|
|
a.transitions[key] = "stopping"
|
|
a.mu.Unlock()
|
|
a.emitStateChanged()
|
|
w := vmstart.LineWriter(func(line string) { a.emitVMLogForDriver(ref.DriverID, ref.Name, line) })
|
|
if err := svc.Stop(operationCtx, ref, force, w, w); err != nil {
|
|
a.mu.Lock()
|
|
delete(a.transitions, key)
|
|
a.mu.Unlock()
|
|
return err
|
|
}
|
|
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
a.stopPacketBridge(ref.Name)
|
|
}
|
|
a.mu.Lock()
|
|
delete(a.pendingChanges, key)
|
|
a.mu.Unlock()
|
|
a.wakePoll()
|
|
a.emitStateChanged()
|
|
return nil
|
|
})
|
|
}
|
|
func (a *App) EjectInstaller(driverID, name string) error {
|
|
a.mu.Lock()
|
|
svc := a.lifecycleSvc
|
|
a.mu.Unlock()
|
|
if svc == nil {
|
|
if driverID != "" && driver.CanonicalDriverID(driverID) != driver.BuiltinReimsMacOSDriverID {
|
|
return errors.New("driver is unavailable without the plugin manager")
|
|
}
|
|
return a.ejectMacOSInstaller(name)
|
|
}
|
|
vm, err := a.resolveVMInfo(driverID, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = a.withComputerUseVM(context.Background(), vm.Directory, func(ctx context.Context) error {
|
|
_, actionErr := svc.InvokeAction(ctx, vm.Ref(), "eject", nil)
|
|
return actionErr
|
|
})
|
|
if err == nil {
|
|
a.wakePoll()
|
|
a.emitStateChanged()
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (a *App) ReadVMLogs(driverID, name string, serial bool) (string, error) {
|
|
a.mu.Lock()
|
|
svc := a.lifecycleSvc
|
|
a.mu.Unlock()
|
|
if svc == nil {
|
|
return a.readMacOSVMLogs(name, serial)
|
|
}
|
|
return svc.ReadLogs(context.Background(), driver.VMRef{DriverID: driverID, Name: name}, serial)
|
|
}
|
|
|
|
func (a *App) RestartVM(driverID, name string) error {
|
|
if err := a.beginVMStart(); err != nil {
|
|
return err
|
|
}
|
|
defer a.vmStarts.Done()
|
|
|
|
a.mu.Lock()
|
|
svc := a.lifecycleSvc
|
|
a.mu.Unlock()
|
|
var ref driver.VMRef
|
|
if svc != nil {
|
|
_, vm, err := svc.Resolve(context.Background(), driverID, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ref = vm.Ref()
|
|
} else {
|
|
if driverID != "" && driver.CanonicalDriverID(driverID) != driver.BuiltinReimsMacOSDriverID {
|
|
return fmt.Errorf("driver %q is unavailable without the plugin manager", driverID)
|
|
}
|
|
ref = driver.NewVMRef(driver.BuiltinReimsMacOSDriverID, name)
|
|
}
|
|
if ref.DriverID != driver.BuiltinReimsMacOSDriverID {
|
|
if svc == nil {
|
|
return errors.New("driver lifecycle is not initialized")
|
|
}
|
|
_, vm, err := svc.Resolve(a.baseCtx(), ref.DriverID, ref.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return a.withComputerUseVM(a.baseCtx(), vm.Directory, func(ctx context.Context) error {
|
|
if err := a.stopVM(ctx, ref.DriverID, ref.Name, false); err != nil {
|
|
return err
|
|
}
|
|
return a.startResolvedVMLocked(ctx, svc, vm)
|
|
})
|
|
}
|
|
_, err := a.restartMacOSVM(a.baseCtx(), ref.Name)
|
|
return err
|
|
}
|
|
|
|
func (a *App) DeleteVM(driverID, name string, keepFiles bool) error {
|
|
a.mu.Lock()
|
|
svc := a.lifecycleSvc
|
|
a.mu.Unlock()
|
|
if svc == nil {
|
|
if driverID != "" && driver.CanonicalDriverID(driverID) != driver.BuiltinReimsMacOSDriverID {
|
|
return errors.New("driver is unavailable without the plugin manager")
|
|
}
|
|
return a.deleteMacOSVM(name, keepFiles)
|
|
}
|
|
d, vm, err := svc.ResolveForDelete(context.Background(), driverID, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return a.withComputerUseVM(context.Background(), vm.Directory, func(ctx context.Context) error {
|
|
ref := vm.Ref()
|
|
key := ref.Key()
|
|
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
a.cancelRebootWatcher(ref.Name)
|
|
stopVMMonitor(ref.Name)
|
|
}
|
|
w := vmstart.LineWriter(func(line string) { a.emitVMLogForDriver(ref.DriverID, ref.Name, line) })
|
|
var deleteErr error
|
|
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
removed, delErr := a.deleteVMWithGuestTools(ref.Name, func() error {
|
|
return svc.DeleteResolved(ctx, d, vm, keepFiles, w, w)
|
|
}, func() (bool, error) {
|
|
vms, lerr := a.listVMsInternal(ctx)
|
|
if lerr != nil {
|
|
return false, lerr
|
|
}
|
|
for _, item := range vms {
|
|
if item.Name == ref.Name && driver.CanonicalDriverID(item.DriverID) == ref.DriverID {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
})
|
|
if !removed {
|
|
return delErr
|
|
}
|
|
deleteErr = delErr
|
|
} else if err := svc.DeleteResolved(ctx, d, vm, keepFiles, w, w); err != nil {
|
|
return err
|
|
}
|
|
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
a.stopPacketBridge(ref.Name)
|
|
}
|
|
a.mu.Lock()
|
|
delete(a.pendingChanges, key)
|
|
delete(a.transitions, key)
|
|
delete(a.diskPaths, key)
|
|
delete(a.procSamples, key)
|
|
gt := a.guestTools
|
|
a.mu.Unlock()
|
|
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
releaseVMTyping(ref.Name)
|
|
releaseVMThumbnail(ref.Name)
|
|
} else {
|
|
releaseVMTypingForDriver(ref.DriverID, ref.Name)
|
|
releaseVMThumbnailForDriver(ref.DriverID, ref.Name)
|
|
}
|
|
a.stateTable().Delete(ref)
|
|
a.discardMaterializedSecretsForVM(key)
|
|
if gt != nil {
|
|
gt.UnregisterVM(key)
|
|
}
|
|
a.wakePoll()
|
|
a.emitStateChanged()
|
|
return deleteErr
|
|
})
|
|
}
|