333 lines
9.6 KiB
Go
333 lines
9.6 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
|
|
}
|
|
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: 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.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)
|
|
}
|
|
return a.startVMLegacy(name)
|
|
}
|
|
_, vm, err := svc.Resolve(context.Background(), driverID, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ref := vm.Ref()
|
|
if vm.State == "suspended" {
|
|
return a.ResumeVMWithDriver(ref.DriverID, ref.Name)
|
|
}
|
|
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 {
|
|
return a.stopVMWithDriverCtx(context.Background(), driverID, name, force)
|
|
}
|
|
|
|
func (a *App) stopVMWithDriverCtx(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.stopVMLegacyCtx(ctx, name, force)
|
|
}
|
|
_, vm, err := svc.Resolve(ctx, 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(ctx, 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()
|
|
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 err := a.StopVMWithDriver(ref.DriverID, ref.Name, false); err != nil {
|
|
return err
|
|
}
|
|
return a.StartVMWithDriver(ref.DriverID, ref.Name)
|
|
}
|
|
_, err := a.restartMacOSVM(ref.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 ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
removed, delErr := a.deleteVMWithGuestTools(ref.Name, func() error {
|
|
return svc.Delete(context.Background(), ref, keepFiles, w, w)
|
|
}, func() (bool, error) {
|
|
vms, lerr := a.listVMsInternal(context.Background())
|
|
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
|
|
}
|
|
err = delErr
|
|
} else {
|
|
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
|
|
}
|