Files
Reims-Go/internal/app/lifecycle_flow.go
T
Nwflower c1b33c9c4f refactor(app): isolate native macOS adapters
Route generic App and CLI code through lifecycle and driver models. Move native macOS detail, create, settings and legacy flows into explicit files, share progress and redaction through host, and enforce the boundary with a file-scoped architecture ratchet.
2026-11-30 11:18:09 -07:00

293 lines
8.4 KiB
Go

package app
import (
"context"
"errors"
"fmt"
"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"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
func (a *App) emitLifecycleStateChanged() {
if a.ctx != nil {
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
}
}
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
}
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: resolveVMDiskPath(detail), 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(name string) error { return a.StartVMWithDriver("", name) }
func (a *App) StartVMWithDriver(driverID, name string) error {
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)
}
return a.startVMLegacy(name)
}
_, vm, err := svc.Resolve(context.Background(), driverID, name)
if err != nil {
return err
}
ref := vm.Ref()
key := ref.Key()
detail := vm
a.mu.Lock()
a.transitions[key] = "starting"
delete(a.pendingChanges, key)
a.mu.Unlock()
a.emitLifecycleStateChanged()
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(context.Background(), 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.emitLifecycleStateChanged()
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.emitLifecycleStateChanged()
return nil
}
func (a *App) StopVM(name string, force bool) error { return a.StopVMWithDriver("", name, force) }
func (a *App) StopVMWithDriver(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.stopVMLegacy(name, force)
}
_, vm, err := svc.Resolve(context.Background(), driverID, name)
if err != nil {
return err
}
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.emitLifecycleStateChanged()
w := vmstart.LineWriter(func(line string) { a.emitVMLogForDriver(ref.DriverID, ref.Name, line) })
if err := svc.Stop(context.Background(), ref, force, w, w); err != nil {
a.mu.Lock()
delete(a.transitions, key)
a.mu.Unlock()
return err
}
a.mu.Lock()
delete(a.pendingChanges, key)
a.mu.Unlock()
a.wakePoll()
a.emitLifecycleStateChanged()
return nil
}
func (a *App) EjectInstaller(name string) error { return a.EjectInstallerWithDriver("", name) }
func (a *App) EjectInstallerWithDriver(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.ejectInstallerLegacy(name)
}
_, err := svc.InvokeAction(context.Background(), driver.VMRef{DriverID: driverID, Name: name}, "eject", nil)
if err == nil {
a.wakePoll()
a.emitLifecycleStateChanged()
}
return err
}
func (a *App) ReadVMLogsWithDriver(driverID, name string, serial bool) (string, error) {
a.mu.Lock()
svc := a.lifecycleSvc
a.mu.Unlock()
if svc == nil {
return a.readVMLogsLegacy(name, serial)
}
return svc.ReadLogs(context.Background(), driver.VMRef{DriverID: driverID, Name: name}, serial)
}
func (a *App) RestartVMWithDriver(driverID, name string) error {
a.mu.Lock()
svc := a.lifecycleSvc
a.mu.Unlock()
if svc != nil && driverID == "" {
if _, vm, err := svc.Resolve(context.Background(), driverID, name); err == nil && vm.DriverID != driver.BuiltinReimsMacOSDriverID {
if err := a.StopVMWithDriver(driverID, name, false); err != nil {
return err
}
return a.StartVMWithDriver(driverID, name)
}
}
if driver.CanonicalDriverID(driverID) != driver.BuiltinReimsMacOSDriverID {
if err := a.StopVMWithDriver(driverID, name, false); err != nil {
return err
}
return a.StartVMWithDriver(driverID, name)
}
_, err := a.restartVMLegacy(name)
return err
}
func (a *App) DeleteVM(name string, keepFiles bool) error {
return a.DeleteVMWithDriver("", name, keepFiles)
}
func (a *App) DeleteVMWithDriver(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.deleteVMLegacy(name, keepFiles)
}
_, vm, err := svc.Resolve(context.Background(), driverID, name)
if err != nil {
return err
}
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) })
if err := svc.Delete(context.Background(), ref, keepFiles, w, w); err != nil {
return err
}
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)
}
if gt != nil {
gt.UnregisterVM(key)
}
a.wakePoll()
a.emitLifecycleStateChanged()
return nil
}