* refactor: 收拢核心管线、强化错误处理、优化热路径与更新体积 * fix: preserve download recovery and bound PBZX decoding Keep VM diagnostics available during plugin initialization failures and verify settings through the unified driver lifecycle.
358 lines
9.8 KiB
Go
358 lines
9.8 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 {
|
|
return errors.New("driver lifecycle is not initialized")
|
|
}
|
|
_, 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 errors.New("driver lifecycle is not initialized")
|
|
}
|
|
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 {
|
|
return errors.New("driver lifecycle is not initialized")
|
|
}
|
|
_, 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 {
|
|
return errors.New("driver lifecycle is not initialized")
|
|
}
|
|
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 {
|
|
if driverID != "" && driver.CanonicalDriverID(driverID) != driver.BuiltinReimsMacOSDriverID {
|
|
return "", fmt.Errorf("driver %q is unavailable without the plugin manager", driverID)
|
|
}
|
|
c, err := a.client()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return c.ReadLogs(context.Background(), 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()
|
|
if svc == nil {
|
|
return errors.New("driver lifecycle is not initialized")
|
|
}
|
|
_, vm, err := svc.Resolve(context.Background(), driverID, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ref := vm.Ref()
|
|
if ref.DriverID != driver.BuiltinReimsMacOSDriverID {
|
|
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 {
|
|
return errors.New("driver lifecycle is not initialized")
|
|
}
|
|
d, vm, err := svc.ResolveForDelete(context.Background(), driverID, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ref := vm.Ref()
|
|
removedVM := false
|
|
err = a.withComputerUseVM(context.Background(), vm.Directory, func(ctx context.Context) error {
|
|
key := ref.Key()
|
|
if vm.State != "running" && ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
a.cancelRebootWatcher(ref.Name)
|
|
stopVMMonitor(ref.Name)
|
|
a.stopPacketBridge(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
|
|
}
|
|
removedVM = true
|
|
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
a.cancelRebootWatcher(ref.Name)
|
|
stopVMMonitor(ref.Name)
|
|
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()
|
|
a.stateTable().Delete(ref)
|
|
a.discardMaterializedSecretsForVM(key)
|
|
if gt != nil {
|
|
gt.UnregisterVM(key)
|
|
}
|
|
a.wakePoll()
|
|
a.emitStateChanged()
|
|
return deleteErr
|
|
})
|
|
if removedVM {
|
|
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
releaseVMTyping(ref.Name)
|
|
releaseVMThumbnail(ref.Name)
|
|
} else {
|
|
releaseVMTypingForDriver(ref.DriverID, ref.Name)
|
|
releaseVMThumbnailForDriver(ref.DriverID, ref.Name)
|
|
}
|
|
}
|
|
return err
|
|
}
|