231 lines
6.3 KiB
Go
231 lines
6.3 KiB
Go
package macosdriver
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
|
|
"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/macos/reims"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
)
|
|
|
|
// ReimsMacOSCapabilities lists all capabilities supported by builtin.driver.reims-macos.
|
|
var ReimsMacOSCapabilities = []string{
|
|
driver.CapSnapshot,
|
|
driver.CapCowClone,
|
|
driver.CapResize,
|
|
driver.CapQMP,
|
|
driver.CapSSH,
|
|
driver.CapGuestTools,
|
|
driver.CapAffinity,
|
|
driver.CapOpenCore,
|
|
driver.CapRecovery,
|
|
}
|
|
|
|
// ReimsMacOSDriver adapts internal/macos/reims into the generic driver.Driver interface
|
|
// as the compiled-in `builtin.driver.reims-macos` extension (§3.2, §5.3).
|
|
type ReimsMacOSDriver struct {
|
|
getConfig func() config.Config
|
|
}
|
|
|
|
// NewReimsMacOSDriver creates the built-in macOS driver adapter.
|
|
func NewReimsMacOSDriver(getConfig func() config.Config) *ReimsMacOSDriver {
|
|
return &ReimsMacOSDriver{getConfig: getConfig}
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) ID() string { return driver.BuiltinReimsMacOSDriverID }
|
|
|
|
func (d *ReimsMacOSDriver) DisplayName() plugin.LocalizedText {
|
|
return plugin.LocalizedText{ZhCN: "macOS (Reims vGPU)", EnUS: "macOS (Reims vGPU)"}
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) RuntimeKind() string { return rt.DefaultKind }
|
|
func (d *ReimsMacOSDriver) RuntimeAPIRange() string { return "^1.0.0" }
|
|
func (d *ReimsMacOSDriver) Capabilities() []string { return append([]string(nil), ReimsMacOSCapabilities...) }
|
|
|
|
func (d *ReimsMacOSDriver) CreateSchema() *plugin.SettingsSchema {
|
|
return nil // Uses the native macOS CreateWizard UI
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) SettingsSchema() *plugin.SettingsSchema {
|
|
return nil // Uses the native macOS SettingsModal UI
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) client() (*reims.Client, error) {
|
|
cfg := d.getConfig()
|
|
active, err := rt.ResolveForKind(&cfg, d.RuntimeKind())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rt.CheckCompatibility(d.RuntimeKind(), d.RuntimeAPIRange(), *active); err != nil {
|
|
return nil, err
|
|
}
|
|
return reims.NewClient(&cfg, *active), nil
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) List(ctx context.Context) ([]driver.VMInfo, error) {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items, err := c.List(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]driver.VMInfo, 0, len(items))
|
|
for _, it := range items {
|
|
out = append(out, driver.VMInfo{
|
|
Name: it.Name,
|
|
DriverID: d.ID(),
|
|
DriverName: "macOS (Reims vGPU)",
|
|
State: it.State,
|
|
PID: it.PID,
|
|
Capabilities: d.Capabilities(),
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) Show(ctx context.Context, name string) (driver.VMInfo, error) {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return driver.VMInfo{}, err
|
|
}
|
|
det, err := c.Show(ctx, name)
|
|
if err != nil {
|
|
return driver.VMInfo{}, err
|
|
}
|
|
rawBytes, _ := json.Marshal(det)
|
|
var data map[string]any
|
|
_ = json.Unmarshal(rawBytes, &data)
|
|
return driver.VMInfo{
|
|
Name: det.Name,
|
|
DriverID: d.ID(),
|
|
DriverName: "macOS (Reims vGPU)",
|
|
State: det.State,
|
|
CPUs: det.CPUs,
|
|
Memory: det.Memory,
|
|
DiskSummary: det.Disk,
|
|
Directory: det.Directory,
|
|
Capabilities: d.Capabilities(),
|
|
DriverData: data,
|
|
}, nil
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) ValidateCreate(_ context.Context, req driver.CreateRequest) []driver.ValidationIssue {
|
|
var opts reims.CreateOpts
|
|
if err := decodeMap(req.Options, &opts); err != nil {
|
|
return []driver.ValidationIssue{{Field: "options", Message: err.Error()}}
|
|
}
|
|
if opts.Name == "" {
|
|
opts.Name = req.Name
|
|
}
|
|
if opts.Dir == "" {
|
|
opts.Dir = req.Dir
|
|
}
|
|
cfg := d.getConfig()
|
|
rep := host.Inspect(cfg.DataRoot)
|
|
issues := reims.ValidateCreateLocalized(opts, rep, cfg.Locale)
|
|
out := make([]driver.ValidationIssue, 0, len(issues))
|
|
for _, is := range issues {
|
|
out = append(out, driver.ValidationIssue{Field: is.Field, Message: is.Message})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) Create(ctx context.Context, req driver.CreateRequest, stdout, stderr io.Writer) error {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var opts reims.CreateOpts
|
|
if err := decodeMap(req.Options, &opts); err != nil {
|
|
return err
|
|
}
|
|
if opts.Name == "" {
|
|
opts.Name = req.Name
|
|
}
|
|
if opts.Dir == "" {
|
|
opts.Dir = req.Dir
|
|
}
|
|
return c.Create(ctx, opts, stdout, stderr)
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) ValidateSet(ctx context.Context, req driver.SetRequest) []driver.ValidationIssue {
|
|
var opts reims.SetOpts
|
|
if err := decodeMap(req.Changes, &opts); err != nil {
|
|
return []driver.ValidationIssue{{Field: "changes", Message: err.Error()}}
|
|
}
|
|
opts.Name = req.Name
|
|
var cur reims.VMDetail
|
|
if c, err := d.client(); err == nil {
|
|
if det, err := c.Show(ctx, req.Name); err == nil {
|
|
cur = det
|
|
}
|
|
}
|
|
cfg := d.getConfig()
|
|
rep := host.Inspect(cfg.DataRoot)
|
|
issues := reims.ValidateSetLocalized(opts, cur, rep, cfg.Locale)
|
|
out := make([]driver.ValidationIssue, 0, len(issues))
|
|
for _, is := range issues {
|
|
out = append(out, driver.ValidationIssue{Field: is.Field, Message: is.Message})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) Set(ctx context.Context, req driver.SetRequest, stdout, stderr io.Writer) error {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var opts reims.SetOpts
|
|
if err := decodeMap(req.Changes, &opts); err != nil {
|
|
return err
|
|
}
|
|
opts.Name = req.Name
|
|
return c.Set(ctx, opts, stdout, stderr)
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) Start(ctx context.Context, name string, stdout, stderr io.Writer) error {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.StartWithCompat(ctx, name, stdout, stderr, nil)
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) Stop(ctx context.Context, name string, force bool, stdout, stderr io.Writer) error {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if force {
|
|
return c.StopForce(ctx, name, stdout, stderr)
|
|
}
|
|
return c.Stop(ctx, name, stdout, stderr)
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) Delete(ctx context.Context, name string, keepFiles bool, stdout, stderr io.Writer) error {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.Delete(ctx, name, keepFiles, stdout, stderr)
|
|
}
|
|
|
|
func decodeMap(m map[string]any, dst any) error {
|
|
if len(m) == 0 {
|
|
return nil
|
|
}
|
|
raw, err := json.Marshal(m)
|
|
if err != nil {
|
|
return fmt.Errorf("marshal options: %w", err)
|
|
}
|
|
return json.Unmarshal(raw, dst)
|
|
}
|