637 lines
24 KiB
Go
637 lines
24 KiB
Go
package app
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/Nwflower/reims-go/internal/driver"
|
||
"github.com/Nwflower/reims-go/internal/plugin"
|
||
)
|
||
|
||
// AutoInstallerProviderView describes one enabled built-in or Sidecar plugin
|
||
// that provides unattended guest installation for a guest OS family.
|
||
type AutoInstallerProviderView struct {
|
||
PluginID string `json:"plugin_id"`
|
||
ID string `json:"id"`
|
||
Title string `json:"title"`
|
||
Description string `json:"description,omitempty"`
|
||
GuestFamilies []string `json:"guest_families"`
|
||
RequiresOfflineInstaller bool `json:"requires_offline_installer,omitempty"`
|
||
SecretSetting string `json:"secret_setting"`
|
||
}
|
||
|
||
// AutoInstallStartRequest carries Stage 0 preflight parameters submitted by the
|
||
// host Create Wizard or VM Detail Page. Password is encrypted directly into
|
||
// host DPAPI storage and never sent to the plugin or written to logs.
|
||
type AutoInstallStartRequest struct {
|
||
PluginID string `json:"plugin_id,omitempty"`
|
||
DriverID string `json:"driver_id,omitempty"`
|
||
VMName string `json:"vm_name"`
|
||
Mode string `json:"mode,omitempty"` // "recovery_only" | "full"
|
||
GuestLang string `json:"guest_lang,omitempty"`
|
||
FullName string `json:"full_name"`
|
||
AccountName string `json:"account_name"`
|
||
Password string `json:"password"`
|
||
Region string `json:"region,omitempty"`
|
||
Appearance string `json:"appearance,omitempty"`
|
||
AgeRange string `json:"age_range,omitempty"`
|
||
SLAAccepted bool `json:"sla_accepted"`
|
||
SaveUnlockCredential bool `json:"save_unlock_credential,omitempty"`
|
||
InstallGuestTools *bool `json:"install_guest_tools,omitempty"`
|
||
OCRBackend string `json:"ocr_backend,omitempty"`
|
||
CustomOCRMirror string `json:"custom_ocr_mirror,omitempty"`
|
||
StartVMIfStopped bool `json:"start_vm_if_stopped,omitempty"`
|
||
}
|
||
|
||
// ListAutoInstallers returns enabled auto-installer plugins matching guestFamily
|
||
// (or all enabled providers when guestFamily is empty). Disabling a plugin
|
||
// immediately removes it from this list so the UI leaves zero residue.
|
||
func (a *App) ListAutoInstallers(guestFamily string) []AutoInstallerProviderView {
|
||
a.mu.Lock()
|
||
mgr := a.pluginMgr
|
||
a.mu.Unlock()
|
||
if mgr == nil || mgr.Registry() == nil {
|
||
return nil
|
||
}
|
||
loc := string(a.computerUseLocale())
|
||
items := mgr.Registry().AutoInstallersForFamily(guestFamily)
|
||
out := make([]AutoInstallerProviderView, 0, len(items))
|
||
for _, item := range items {
|
||
secretKey := strings.TrimSpace(item.SecretSetting)
|
||
out = append(out, AutoInstallerProviderView{
|
||
PluginID: item.PluginID,
|
||
ID: item.ID,
|
||
Title: item.Title.Get(loc),
|
||
Description: item.Description.Get(loc),
|
||
GuestFamilies: append([]string(nil), item.GuestFamilies...),
|
||
RequiresOfflineInstaller: item.RequiresOfflineInstaller,
|
||
SecretSetting: secretKey,
|
||
})
|
||
}
|
||
return out
|
||
}
|
||
|
||
func (a *App) resolveAutoInstallerProvider(pluginID, guestFamily string) (*plugin.Manager, AutoInstallerProviderView, error) {
|
||
a.mu.Lock()
|
||
mgr := a.pluginMgr
|
||
a.mu.Unlock()
|
||
if mgr == nil || mgr.Registry() == nil {
|
||
return nil, AutoInstallerProviderView{}, errors.New("plugin manager is unavailable")
|
||
}
|
||
providers := a.ListAutoInstallers(guestFamily)
|
||
if len(providers) == 0 && guestFamily != "" {
|
||
providers = a.ListAutoInstallers("")
|
||
}
|
||
if len(providers) == 0 {
|
||
return nil, AutoInstallerProviderView{}, errors.New("no enabled auto-installer plugin is available")
|
||
}
|
||
pid := strings.TrimSpace(pluginID)
|
||
if pid == "" {
|
||
return mgr, providers[0], nil
|
||
}
|
||
for _, p := range providers {
|
||
if p.PluginID == pid {
|
||
return mgr, p, nil
|
||
}
|
||
}
|
||
return nil, AutoInstallerProviderView{}, fmt.Errorf("auto-installer plugin %q is not enabled", pid)
|
||
}
|
||
|
||
func validatePluginSecretSetting(mgr *plugin.Manager, pluginID, settingKey string) error {
|
||
settingKey = strings.TrimSpace(settingKey)
|
||
if settingKey == "" {
|
||
return fmt.Errorf("auto-installer plugin %q does not declare a secret_setting for password storage", pluginID)
|
||
}
|
||
if mgr == nil || mgr.Registry() == nil {
|
||
return errors.New("plugin manager is unavailable")
|
||
}
|
||
info, ok := mgr.Registry().Info(pluginID)
|
||
if !ok {
|
||
return fmt.Errorf("auto-installer plugin %q is not registered", pluginID)
|
||
}
|
||
if info.Settings == nil {
|
||
return fmt.Errorf("auto-installer plugin %q does not declare secret setting %q in its manifest", pluginID, settingKey)
|
||
}
|
||
prop, exists := info.Settings.Properties[settingKey]
|
||
if !exists || !prop.Secret {
|
||
return fmt.Errorf("auto-installer plugin %q setting %q is not declared as secret", pluginID, settingKey)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// Stambha can boot macOS, but cannot supply the guest console or installation phase.
|
||
func validateAutoInstallDriver(driverID string) error {
|
||
if driver.CanonicalDriverID(strings.TrimSpace(driverID)) == driver.BuiltinStambhaDriverID {
|
||
return errors.New("automatic installation is unavailable for Stambha VMs")
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func isPrintableASCIIPassword(s string) bool {
|
||
if len(s) < 4 || len(s) > 256 {
|
||
return false
|
||
}
|
||
for _, r := range s {
|
||
if r < 0x20 || r > 0x7E {
|
||
return false
|
||
}
|
||
}
|
||
return true
|
||
}
|
||
|
||
// PreflightAutoInstall queries the enabled auto-installer plugin for local OCR
|
||
// readiness and resumable checkpoint status.
|
||
func (a *App) PreflightAutoInstall(pluginID, driverID, vmName, guestLang string) (map[string]any, error) {
|
||
if err := validateAutoInstallDriver(driverID); err != nil {
|
||
return nil, err
|
||
}
|
||
mgr, provider, err := a.resolveAutoInstallerProvider(pluginID, "macos")
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
vmKeyStr := ""
|
||
if strings.TrimSpace(vmName) != "" {
|
||
vmKeyStr = vmKeyForDriver(driverID, vmName)
|
||
}
|
||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||
defer cancel()
|
||
var resp map[string]any
|
||
err = mgr.CallSidecar(ctx, provider.PluginID, "autoinstall/preflight", map[string]any{
|
||
"vm": vmKeyStr,
|
||
"guest_lang": guestLang,
|
||
}, &resp)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if resp == nil {
|
||
resp = map[string]any{}
|
||
}
|
||
resp["plugin_id"] = provider.PluginID
|
||
return resp, nil
|
||
}
|
||
|
||
// StartAutoInstall configures host session & authorization (plus optional DPAPI
|
||
// account credentials and REIMSPROV provisioning in "full" mode), starts the VM
|
||
// if requested, and launches the auto-installer's state machine with screen input lock.
|
||
func (a *App) StartAutoInstall(req AutoInstallStartRequest) (map[string]any, error) {
|
||
password := req.Password
|
||
req.Password = ""
|
||
defer func() { password = "" }()
|
||
|
||
mode := strings.ToLower(strings.TrimSpace(req.Mode))
|
||
if mode == "" {
|
||
if strings.TrimSpace(req.FullName) == "" && strings.TrimSpace(req.AccountName) == "" && password == "" {
|
||
mode = "recovery_only"
|
||
} else {
|
||
mode = "full"
|
||
}
|
||
}
|
||
if mode != "recovery_only" && mode != "full" {
|
||
return nil, fmt.Errorf("unsupported auto-install mode %q", mode)
|
||
}
|
||
if !req.SLAAccepted {
|
||
return nil, errors.New("explicit Apple SLA confirmation is required before starting auto-install")
|
||
}
|
||
if mode == "full" && !isPrintableASCIIPassword(password) {
|
||
return nil, errors.New("password must be 4–256 printable ASCII characters")
|
||
}
|
||
vm, err := a.resolveVMInfo(req.DriverID, req.VMName)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if err := validateAutoInstallDriver(vm.DriverID); err != nil {
|
||
return nil, err
|
||
}
|
||
family := "macos"
|
||
if vm.GuestOS != nil && vm.GuestOS.Family != "" {
|
||
family = vm.GuestOS.Family
|
||
}
|
||
mgr, provider, err := a.resolveAutoInstallerProvider(req.PluginID, family)
|
||
if err != nil {
|
||
if vm.State == "running" {
|
||
a.restoreManualInstallGuideFallback(vm.DriverID, vm.Name)
|
||
}
|
||
return nil, err
|
||
}
|
||
if broker := mgr.Broker(); broker != nil {
|
||
for _, perm := range []string{"vm:read", "vm:screen", "qmp:write", "vm:control"} {
|
||
if perr := broker.Authorize(provider.PluginID, perm); perr != nil {
|
||
if vm.State == "running" {
|
||
a.restoreManualInstallGuideFallback(vm.DriverID, vm.Name)
|
||
}
|
||
return nil, perr
|
||
}
|
||
}
|
||
}
|
||
vmKeyStr := vm.Ref().Key()
|
||
secretSetting := ""
|
||
if mode == "full" {
|
||
secretSetting = strings.TrimSpace(provider.SecretSetting)
|
||
if err := validatePluginSecretSetting(mgr, provider.PluginID, secretSetting); err != nil {
|
||
return nil, err
|
||
}
|
||
// Encrypt password straight into host DPAPI storage (never passed in RPC payload).
|
||
if err := mgr.SetSettings(provider.PluginID, vmKeyStr, map[string]any{
|
||
secretSetting: password,
|
||
}); err != nil {
|
||
return nil, fmt.Errorf("store DPAPI password: %w", err)
|
||
}
|
||
}
|
||
|
||
// Grant ephemeral per-VM console access and #105 protected-phase authorization
|
||
// backed by the user's explicit host preflight confirmation, and lock VM input
|
||
// against accidental physical mouse/keyboard interference while running.
|
||
a.grantEphemeralVMAccess(provider.PluginID, vmKeyStr)
|
||
phase := a.effectiveVMPhase(vm)
|
||
if phase == driver.PhaseNormal {
|
||
phase = driver.PhaseInstaller
|
||
}
|
||
a.grantProtectedPhase(provider.PluginID, vmKeyStr, phase, 0, true)
|
||
a.beginAgentSession(mgr, provider.PluginID, vm, provider.Title, "stage0_preflight", "lock", true)
|
||
a.setAgentSessionAutoInstallConfig(vmKeyStr, provider.PluginID, mode, secretSetting, mode == "full" && req.SaveUnlockCredential)
|
||
|
||
// Stage (or rebuild) per-VM REIMSPROV (hash-only offline provisioning disk)
|
||
// before cold start in full mode so offline install always matches the latest
|
||
// requested credentials and skips Setup Assistant & Keyboard Setup Assistant.
|
||
if mode == "full" && family == "macos" && vm.Directory != "" && vm.State != "running" &&
|
||
strings.TrimSpace(req.FullName) != "" && strings.TrimSpace(req.AccountName) != "" {
|
||
if perr := a.prepareVMProvisionForDir(vm.Name, vm.Directory, ProvisionRequest{
|
||
FullName: strings.TrimSpace(req.FullName),
|
||
AccountName: strings.TrimSpace(req.AccountName),
|
||
Password: password,
|
||
Appearance: strings.TrimSpace(req.Appearance),
|
||
SLAAccepted: req.SLAAccepted,
|
||
InstallGuestTools: req.InstallGuestTools,
|
||
}); perr != nil {
|
||
a.emitVMLog(vm.Name, "Warning: REIMSPROV provisioning skipped (continuing with OCR fallback): "+perr.Error())
|
||
}
|
||
}
|
||
|
||
if req.StartVMIfStopped && vm.State != "running" {
|
||
if err := a.StartVM(vm.DriverID, vm.Name); err != nil {
|
||
if secretSetting != "" {
|
||
_ = mgr.SetSettings(provider.PluginID, vmKeyStr, map[string]any{secretSetting: ""})
|
||
}
|
||
_ = a.StopAgentSession(vm.DriverID, vm.Name)
|
||
return nil, fmt.Errorf("start VM for auto-install: %w", err)
|
||
}
|
||
// Ensure the protected-phase grant stays active after cold start even if
|
||
// an early poll tick observed the VM stopped before QEMU launched.
|
||
a.grantProtectedPhase(provider.PluginID, vmKeyStr, phase, 0, true)
|
||
// Immediately bind the newly launched VM PID to the floating island and input shield.
|
||
a.refreshActiveAgentSessionAfterStart(vmKeyStr)
|
||
}
|
||
|
||
guestLang := strings.TrimSpace(req.GuestLang)
|
||
if guestLang == "" {
|
||
guestLang = vmInfoString(vm, "guest_lang")
|
||
}
|
||
if guestLang == "" {
|
||
guestLang = "zh-Hans:0"
|
||
}
|
||
|
||
payload := map[string]any{
|
||
"vm": vmKeyStr,
|
||
"mode": mode,
|
||
"guest_lang": guestLang,
|
||
"full_name": strings.TrimSpace(req.FullName),
|
||
"account_name": strings.TrimSpace(req.AccountName),
|
||
"secret_setting": secretSetting,
|
||
"region": strings.TrimSpace(req.Region),
|
||
"appearance": strings.TrimSpace(req.Appearance),
|
||
"age_range": strings.TrimSpace(req.AgeRange),
|
||
"sla_accepted": req.SLAAccepted,
|
||
"save_unlock_credential": mode == "full" && req.SaveUnlockCredential,
|
||
"ocr_backend": strings.TrimSpace(req.OCRBackend),
|
||
"custom_ocr_mirror": strings.TrimSpace(req.CustomOCRMirror),
|
||
"has_provision_disk": mode == "full" && a.hasVMProvisionDir(vm.Directory),
|
||
}
|
||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||
defer cancel()
|
||
var resp map[string]any
|
||
if err := mgr.CallSidecar(ctx, provider.PluginID, "autoinstall/start", payload, &resp); err != nil {
|
||
if secretSetting != "" {
|
||
_ = mgr.SetSettings(provider.PluginID, vmKeyStr, map[string]any{secretSetting: ""})
|
||
}
|
||
_ = a.StopAgentSession(vm.DriverID, vm.Name)
|
||
return nil, err
|
||
}
|
||
if watchCtx, watchGen, ok := a.startAutoInstallSessionWatch(vmKeyStr, provider.PluginID); ok {
|
||
go a.watchAutoInstallSession(watchCtx, watchGen, mgr, provider.PluginID, vmKeyStr, secretSetting, secretSetting != "" && !req.SaveUnlockCredential)
|
||
}
|
||
if resp == nil {
|
||
resp = map[string]any{}
|
||
}
|
||
resp["plugin_id"] = provider.PluginID
|
||
return resp, nil
|
||
}
|
||
|
||
var autoInstallWatchInterval = 3 * time.Second
|
||
|
||
// watchAutoInstallSession monitors an auto-install run so temporary DPAPI
|
||
// credentials are cleared and any lingering host session (for example if an
|
||
// external plugin exits or fails after permission denial without calling
|
||
// host/session.end) is ended cleanly to restore the manual installation guide.
|
||
func (a *App) watchAutoInstallSession(watchCtx context.Context, watchGen uint64, mgr *plugin.Manager, pluginID, vmKeyStr, secretSetting string, clearSecret bool) {
|
||
ticker := time.NewTicker(autoInstallWatchInterval)
|
||
defer ticker.Stop()
|
||
timeout := time.NewTimer(2 * time.Hour)
|
||
defer timeout.Stop()
|
||
var watchDone <-chan struct{}
|
||
if watchCtx != nil {
|
||
watchDone = watchCtx.Done()
|
||
}
|
||
cleanupSecret := func(completed bool) {
|
||
if secretSetting != "" && (clearSecret || !completed) {
|
||
_ = mgr.SetSettings(pluginID, vmKeyStr, map[string]any{secretSetting: ""})
|
||
}
|
||
}
|
||
consecutiveErrs := 0
|
||
for {
|
||
select {
|
||
case <-watchDone:
|
||
return
|
||
case <-timeout.C:
|
||
if !a.isCurrentAutoInstallSessionWatch(vmKeyStr, pluginID, watchGen) {
|
||
return
|
||
}
|
||
cleanupSecret(false)
|
||
a.revokeEphemeralVMAccess(pluginID, vmKeyStr)
|
||
if a.isCurrentAutoInstallSessionWatch(vmKeyStr, pluginID, watchGen) {
|
||
a.endAgentSession(mgr, pluginID, vmKeyStr, "timeout")
|
||
}
|
||
return
|
||
case <-ticker.C:
|
||
if !a.isCurrentAutoInstallSessionWatch(vmKeyStr, pluginID, watchGen) {
|
||
return
|
||
}
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||
var st struct {
|
||
Status string `json:"status"`
|
||
Stage string `json:"stage"`
|
||
Message string `json:"message"`
|
||
}
|
||
err := mgr.CallSidecar(ctx, pluginID, "autoinstall/status", map[string]any{"vm": vmKeyStr}, &st)
|
||
cancel()
|
||
if !a.isCurrentAutoInstallSessionWatch(vmKeyStr, pluginID, watchGen) {
|
||
return
|
||
}
|
||
if err != nil {
|
||
consecutiveErrs++
|
||
if consecutiveErrs < 3 {
|
||
continue
|
||
}
|
||
} else {
|
||
consecutiveErrs = 0
|
||
if st.Stage != "" || st.Message != "" {
|
||
a.syncAutoInstallSessionProgress(vmKeyStr, pluginID, st.Stage, st.Message)
|
||
}
|
||
}
|
||
if err != nil || st.Status == "completed" || st.Status == "stopped" || st.Status == "failed" || st.Status == "idle" {
|
||
if !a.isCurrentAutoInstallSessionWatch(vmKeyStr, pluginID, watchGen) {
|
||
return
|
||
}
|
||
completed := err == nil && st.Status == "completed"
|
||
cleanupSecret(completed)
|
||
a.revokeEphemeralVMAccess(pluginID, vmKeyStr)
|
||
if a.isCurrentAutoInstallSessionWatch(vmKeyStr, pluginID, watchGen) {
|
||
status := strings.TrimSpace(st.Status)
|
||
if err != nil || status == "" || status == "idle" {
|
||
status = "failed"
|
||
}
|
||
a.sessionMu.Lock()
|
||
if sess := a.agentSessions[vmKeyStr]; sess != nil && sess.pluginID == pluginID && sess.watchGen == watchGen {
|
||
sess.status = status
|
||
if strings.TrimSpace(st.Message) != "" {
|
||
sess.message = strings.TrimSpace(st.Message)
|
||
}
|
||
}
|
||
a.sessionMu.Unlock()
|
||
a.endAgentSession(mgr, pluginID, vmKeyStr, "autoinstall_"+status)
|
||
}
|
||
return
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// listAutoInstallStatuses returns all live and persisted auto-install runs for
|
||
// the selected provider (used by CLI control-pipe status queries without a VM).
|
||
func (a *App) listAutoInstallStatuses(pluginID string) (map[string]any, error) {
|
||
mgr, provider, err := a.resolveAutoInstallerProvider(pluginID, "macos")
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||
defer cancel()
|
||
var resp map[string]any
|
||
if err := mgr.CallSidecar(ctx, provider.PluginID, "autoinstall/status", map[string]any{"vm": ""}, &resp); err != nil {
|
||
return nil, err
|
||
}
|
||
if resp == nil {
|
||
resp = map[string]any{"runs": []any{}}
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
// GetAutoInstallStatus queries the auto-installer plugin for one VM's progress.
|
||
func (a *App) GetAutoInstallStatus(pluginID, driverID, vmName string) (map[string]any, error) {
|
||
mgr, provider, err := a.resolveAutoInstallerProvider(pluginID, "macos")
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||
defer cancel()
|
||
vmKeyStr := vmKeyForDriver(driverID, vmName)
|
||
var resp map[string]any
|
||
err = mgr.CallSidecar(ctx, provider.PluginID, "autoinstall/status", map[string]any{
|
||
"vm": vmKeyStr,
|
||
}, &resp)
|
||
if err == nil && resp != nil {
|
||
stage, _ := resp["stage"].(string)
|
||
msg, _ := resp["message"].(string)
|
||
if stage != "" {
|
||
if existing, _ := resp["stage_label"].(string); existing == "" {
|
||
if label := stageKnownName(a.computerUseLocale(), stage); label != "" {
|
||
resp["stage_label"] = label
|
||
}
|
||
}
|
||
}
|
||
a.syncAutoInstallSessionProgress(vmKeyStr, provider.PluginID, stage, msg)
|
||
}
|
||
return resp, err
|
||
}
|
||
|
||
func (a *App) autoInstallResumeVM(driverID, vmName string) (driver.VMInfo, error) {
|
||
vm, err := a.resolveVMInfo(driverID, vmName)
|
||
if err != nil {
|
||
// An explicit driver can resume from its saved checkpoint. A bare
|
||
// name must resolve before granting access to any particular VM.
|
||
if strings.TrimSpace(driverID) == "" {
|
||
return driver.VMInfo{}, err
|
||
}
|
||
ref := driver.NewVMRef(driverID, vmName)
|
||
vm = driver.VMInfo{DriverID: ref.DriverID, Name: ref.Name}
|
||
}
|
||
return vm, validateAutoInstallDriver(vm.DriverID)
|
||
}
|
||
|
||
// resumeAutoInstallFromControl handles `resume` requests arriving over the
|
||
// local named-pipe control channel (e.g. `reims-go vm autoinstall resume`).
|
||
// Because any same-user process (including an L3 sidecar plugin) can connect to
|
||
// the named pipe, this path requires explicit desktop UI confirmation before
|
||
// granting ephemeral VM access, protected-phase authorization, or clearing
|
||
// host-initiated pauses.
|
||
func (a *App) resumeAutoInstallFromControl(ctx context.Context, pluginID, driverID, vmName string) (map[string]any, error) {
|
||
mgr, provider, err := a.resolveAutoInstallerProvider(pluginID, "macos")
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if broker := mgr.Broker(); broker != nil {
|
||
for _, perm := range []string{"vm:read", "vm:screen", "qmp:write", "vm:control"} {
|
||
if perr := broker.Authorize(provider.PluginID, perm); perr != nil {
|
||
return nil, perr
|
||
}
|
||
}
|
||
}
|
||
vmInfo, err := a.autoInstallResumeVM(driverID, vmName)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
phase := a.effectiveVMPhase(vmInfo)
|
||
if phase != driver.PhaseInstaller && phase != driver.PhaseRecovery {
|
||
phase = driver.PhaseInstaller
|
||
}
|
||
prompt := a.buildProtectedPhasePrompt(provider.PluginID, vmInfo, phase, true)
|
||
res, err := a.requestHostConfirm(ctx, mgr.Broker(), mgr.Registry(), provider.PluginID, prompt)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if !res.Approved {
|
||
return nil, errors.New("confirmation_denied: user declined auto-install resume")
|
||
}
|
||
return a.ResumeAutoInstall(provider.PluginID, vmInfo.DriverID, vmInfo.Name)
|
||
}
|
||
|
||
// ResumeAutoInstall resumes a paused_for_user or takeover-paused installation.
|
||
func (a *App) ResumeAutoInstall(pluginID, driverID, vmName string) (map[string]any, error) {
|
||
mgr, provider, err := a.resolveAutoInstallerProvider(pluginID, "macos")
|
||
if err != nil {
|
||
a.restoreManualInstallGuideFallback(driverID, vmName)
|
||
return nil, err
|
||
}
|
||
resumeVM, err := a.autoInstallResumeVM(driverID, vmName)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
driverID, vmName = resumeVM.DriverID, resumeVM.Name
|
||
vmKeyStr := resumeVM.Ref().Key()
|
||
if broker := mgr.Broker(); broker != nil {
|
||
for _, perm := range []string{"vm:read", "vm:screen", "qmp:write", "vm:control"} {
|
||
if perr := broker.Authorize(provider.PluginID, perm); perr != nil {
|
||
a.handlePluginPermissionDeniedForVM(mgr, provider.PluginID, vmKeyStr, vmName, "permission_denied")
|
||
return nil, perr
|
||
}
|
||
}
|
||
}
|
||
hadSession := a.hasActiveAgentSessionOwnedBy(vmKeyStr, provider.PluginID)
|
||
a.grantEphemeralVMAccess(provider.PluginID, vmKeyStr)
|
||
if !hadSession {
|
||
phase := a.effectiveVMPhase(resumeVM)
|
||
if phase == driver.PhaseNormal {
|
||
phase = driver.PhaseInstaller
|
||
}
|
||
a.grantProtectedPhase(provider.PluginID, vmKeyStr, phase, 0, true)
|
||
}
|
||
_ = a.ResumeAgentSession(driverID, vmName)
|
||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||
defer cancel()
|
||
var resp map[string]any
|
||
err = mgr.CallSidecar(ctx, provider.PluginID, "autoinstall/resume", map[string]any{
|
||
"vm": vmKeyStr,
|
||
}, &resp)
|
||
if err != nil {
|
||
if !hadSession {
|
||
a.revokeEphemeralVMAccess(provider.PluginID, vmKeyStr)
|
||
a.revokeProtectedPhaseForPluginVM(provider.PluginID, vmKeyStr, "resume_failed")
|
||
}
|
||
if !a.hasActiveAgentSession(vmKeyStr) {
|
||
a.restoreManualInstallGuideFallback(driverID, vmName)
|
||
}
|
||
return resp, err
|
||
}
|
||
// If resuming restarted a runner from a persisted checkpoint (no prior live
|
||
// session in memory), establish the host session with input-lock policy and
|
||
// start the session watcher so temporary credentials and grants are always
|
||
// cleaned up on completion.
|
||
if !hadSession {
|
||
saveUnlock, _ := resp["save_unlock_credential"].(bool)
|
||
stage, _ := resp["stage"].(string)
|
||
secretKey := strings.TrimSpace(provider.SecretSetting)
|
||
if validatePluginSecretSetting(mgr, provider.PluginID, secretKey) != nil {
|
||
secretKey = ""
|
||
}
|
||
if !a.hasActiveAgentSessionOwnedBy(vmKeyStr, provider.PluginID) {
|
||
a.beginAgentSession(mgr, provider.PluginID, resumeVM, provider.Title, stage, "lock", true)
|
||
}
|
||
a.setAgentSessionAutoInstallConfig(vmKeyStr, provider.PluginID, "", secretKey, saveUnlock)
|
||
if watchCtx, watchGen, ok := a.startAutoInstallSessionWatch(vmKeyStr, provider.PluginID); ok {
|
||
go a.watchAutoInstallSession(watchCtx, watchGen, mgr, provider.PluginID, vmKeyStr, secretKey, !saveUnlock)
|
||
}
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
// StopAutoInstall aborts the active auto-install session, revokes authorization,
|
||
// and clears the temporary DPAPI password setting.
|
||
func (a *App) StopAutoInstall(pluginID, driverID, vmName string) error {
|
||
_ = a.StopAgentSession(driverID, vmName)
|
||
mgr, provider, err := a.resolveAutoInstallerProvider(pluginID, "macos")
|
||
if err != nil {
|
||
return nil
|
||
}
|
||
vmKeyStr := vmKeyForDriver(driverID, vmName)
|
||
secretSetting := strings.TrimSpace(provider.SecretSetting)
|
||
if validatePluginSecretSetting(mgr, provider.PluginID, secretSetting) == nil {
|
||
_ = mgr.SetSettings(provider.PluginID, vmKeyStr, map[string]any{secretSetting: ""})
|
||
}
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||
defer cancel()
|
||
_ = mgr.CallSidecar(ctx, provider.PluginID, "autoinstall/stop", map[string]any{"vm": vmKeyStr}, nil)
|
||
return nil
|
||
}
|
||
|
||
// GetAutoInstallDiagnosticSummary returns a redacted unknown-page/error summary
|
||
// safe for the user to copy and share without exposing names, passwords, or images.
|
||
func (a *App) GetAutoInstallDiagnosticSummary(pluginID, driverID, vmName string) (map[string]any, error) {
|
||
mgr, provider, err := a.resolveAutoInstallerProvider(pluginID, "macos")
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||
defer cancel()
|
||
var resp map[string]any
|
||
err = mgr.CallSidecar(ctx, provider.PluginID, "autoinstall/diagnosticSummary", map[string]any{
|
||
"vm": vmKeyForDriver(driverID, vmName),
|
||
}, &resp)
|
||
return resp, err
|
||
}
|
||
|
||
// DownloadAutoInstallOCR asks the auto-installer plugin to download RapidOCR ONNX fallback assets.
|
||
func (a *App) DownloadAutoInstallOCR(pluginID, customMirror string) (map[string]any, error) {
|
||
mgr, provider, err := a.resolveAutoInstallerProvider(pluginID, "macos")
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||
defer cancel()
|
||
var resp map[string]any
|
||
err = mgr.CallSidecar(ctx, provider.PluginID, "autoinstall/downloadOCR", map[string]any{
|
||
"custom_mirror": strings.TrimSpace(customMirror),
|
||
}, &resp)
|
||
return resp, err
|
||
}
|