Each subcommand is its own function. Opening the lifecycle service, parsing a VM target, printing JSON and reading the UI contract had many copies and now have one. The vm usage line also lists delete, resize and compact. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/vmstart"
|
|
)
|
|
|
|
func suspendVM(ctx context.Context, cfg *config.Config, driverID, name string) error {
|
|
svc, closeService, err := openCLILifecycle(cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer closeService()
|
|
_, vm, err := svc.Resolve(ctx, driverID, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Fprintf(os.Stderr, "Suspending %s...\n", vm.Name)
|
|
res, err := svc.Suspend(ctx, vm.Ref(), nil, os.Stdout, os.Stderr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("Suspended %s (%.2f GiB saved)\n", vm.Name, float64(res.VMStateSize)/float64(1<<30))
|
|
return nil
|
|
}
|
|
|
|
func resumeVM(ctx context.Context, cfg *config.Config, driverID, name string, viaWindow bool) error {
|
|
svc, closeService, err := openCLILifecycle(cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer closeService()
|
|
_, vm, err := svc.Resolve(ctx, driverID, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ref := vm.Ref()
|
|
if viaWindow && windowHandoffWorks {
|
|
return runInWindow(ctx, cfg, ref.DriverID, ref.Name, true)
|
|
}
|
|
if err := requireDirectNetwork(vm); err != nil {
|
|
return err
|
|
}
|
|
fmt.Fprintf(os.Stderr, "Resuming %s...\n", ref.Name)
|
|
startAt := time.Now()
|
|
if _, err := svc.Resume(ctx, ref, nil, os.Stdout, os.Stderr); err != nil {
|
|
return err
|
|
}
|
|
rep := host.Inspect(cfg.DataRoot)
|
|
if list, err := svc.List(ctx); err == nil {
|
|
for _, item := range list {
|
|
if item.Ref() != ref || item.State != "running" || item.PID == nil || *item.PID <= 0 {
|
|
continue
|
|
}
|
|
vmDir := item.Directory
|
|
if vmDir == "" {
|
|
vmDir = vm.Directory
|
|
}
|
|
if line, _ := vmstart.ApplyVMAffinity(cfg.Locale, rep, vmDir, cfg.AutoPinPCores, *item.PID, vm.CPUs, false); line != "" {
|
|
fmt.Fprintln(os.Stderr, line)
|
|
}
|
|
}
|
|
}
|
|
fmt.Printf("Resumed %s in %.1fs\n", ref.Name, time.Since(startAt).Seconds())
|
|
return nil
|
|
}
|
|
|
|
func discardSuspendVM(ctx context.Context, cfg *config.Config, driverID, name string) error {
|
|
svc, closeService, err := openCLILifecycle(cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer closeService()
|
|
if err := svc.DiscardSuspend(ctx, driver.VMRef{DriverID: driverID, Name: name}); err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("Discarded suspend state for %s\n", name)
|
|
return nil
|
|
}
|