194 lines
5.9 KiB
Go
194 lines
5.9 KiB
Go
package driver
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
// SidecarDriver adapts an L3 Sidecar guest driver plugin into the unified
|
|
// driver.Driver + driver.ActionDriver interfaces over JSON-RPC 2.0 (§5.3, §8).
|
|
// Because VM processes are spawned via the reverse Host API (`host/vm.spawn`),
|
|
// a Sidecar crash or restart never terminates running VMs.
|
|
type SidecarDriver struct {
|
|
contrib plugin.GuestDriverContribution
|
|
sup *plugin.SidecarSupervisor
|
|
}
|
|
|
|
// NewSidecarDriver constructs a JSON-RPC proxy driver backed by sup.
|
|
func NewSidecarDriver(contrib plugin.GuestDriverContribution, sup *plugin.SidecarSupervisor) *SidecarDriver {
|
|
return &SidecarDriver{
|
|
contrib: contrib,
|
|
sup: sup,
|
|
}
|
|
}
|
|
|
|
func (d *SidecarDriver) ID() string { return d.contrib.ID }
|
|
func (d *SidecarDriver) DisplayName() plugin.LocalizedText { return d.contrib.Name }
|
|
func (d *SidecarDriver) RuntimeKind() string { return d.contrib.RuntimeKind }
|
|
func (d *SidecarDriver) RuntimeAPIRange() string { return d.contrib.RuntimeAPIRange }
|
|
func (d *SidecarDriver) Capabilities() []string { return append([]string(nil), d.contrib.Capabilities...) }
|
|
func (d *SidecarDriver) CreateSchema() *plugin.SettingsSchema { return d.contrib.CreateSchema }
|
|
func (d *SidecarDriver) SettingsSchema() *plugin.SettingsSchema { return d.contrib.SettingsSchema }
|
|
|
|
func (d *SidecarDriver) List(ctx context.Context) ([]VMInfo, error) {
|
|
var resp struct {
|
|
VMs []VMInfo `json:"vms"`
|
|
}
|
|
if err := d.sup.Call(ctx, "driver/list", nil, &resp); err != nil {
|
|
return nil, err
|
|
}
|
|
for i := range resp.VMs {
|
|
resp.VMs[i].DriverID = d.contrib.ID
|
|
if len(resp.VMs[i].Capabilities) == 0 {
|
|
resp.VMs[i].Capabilities = d.Capabilities()
|
|
}
|
|
}
|
|
return resp.VMs, nil
|
|
}
|
|
|
|
func (d *SidecarDriver) Show(ctx context.Context, name string) (VMInfo, error) {
|
|
var info VMInfo
|
|
if err := d.sup.Call(ctx, "driver/show", map[string]any{"name": name}, &info); err != nil {
|
|
return VMInfo{}, err
|
|
}
|
|
info.DriverID = d.contrib.ID
|
|
if len(info.Capabilities) == 0 {
|
|
info.Capabilities = d.Capabilities()
|
|
}
|
|
return info, nil
|
|
}
|
|
|
|
func (d *SidecarDriver) ValidateCreate(ctx context.Context, req CreateRequest) []ValidationIssue {
|
|
var resp struct {
|
|
Issues []ValidationIssue `json:"issues"`
|
|
}
|
|
if err := d.sup.Call(ctx, "driver/validateCreate", req, &resp); err != nil {
|
|
return []ValidationIssue{{Field: "driver", Message: err.Error()}}
|
|
}
|
|
return resp.Issues
|
|
}
|
|
|
|
func (d *SidecarDriver) Create(ctx context.Context, req CreateRequest, stdout, _ io.Writer) error {
|
|
var resp struct {
|
|
Directory string `json:"directory"`
|
|
Logs []string `json:"logs"`
|
|
}
|
|
if err := d.sup.Call(ctx, "driver/create", req, &resp); err != nil {
|
|
return err
|
|
}
|
|
for _, l := range resp.Logs {
|
|
if stdout != nil {
|
|
_, _ = fmt.Fprintln(stdout, l)
|
|
}
|
|
}
|
|
if resp.Directory != "" {
|
|
cleanDir := filepath.Clean(resp.Directory)
|
|
// Only stamp ownership inside an existing directory returned for this VM,
|
|
// and refuse to hijack a directory already owned by a different driver.
|
|
if req.Dir != "" {
|
|
cleanReq := filepath.Clean(req.Dir)
|
|
if !strings.EqualFold(cleanDir, cleanReq) && !strings.HasPrefix(strings.ToLower(cleanDir), strings.ToLower(cleanReq+string(os.PathSeparator))) {
|
|
return fmt.Errorf("sidecar driver returned directory %q outside requested %q", resp.Directory, req.Dir)
|
|
}
|
|
}
|
|
if st, err := os.Stat(cleanDir); err == nil && st.IsDir() {
|
|
markerPath := filepath.Join(cleanDir, OwnerMarkerFile)
|
|
if _, mErr := os.Stat(markerPath); mErr == nil {
|
|
if existing := ReadOwnerDriver(cleanDir); existing != d.contrib.ID {
|
|
return fmt.Errorf("refusing to overwrite VM owner marker %q with %q", existing, d.contrib.ID)
|
|
}
|
|
}
|
|
if err := WriteOwnerDriver(cleanDir, d.contrib.ID); err != nil {
|
|
return fmt.Errorf("write driver owner marker: %w", err)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *SidecarDriver) ValidateSet(ctx context.Context, req SetRequest) []ValidationIssue {
|
|
var resp struct {
|
|
Issues []ValidationIssue `json:"issues"`
|
|
}
|
|
if err := d.sup.Call(ctx, "driver/validateSet", req, &resp); err != nil {
|
|
return []ValidationIssue{{Field: "driver", Message: err.Error()}}
|
|
}
|
|
return resp.Issues
|
|
}
|
|
|
|
func (d *SidecarDriver) Set(ctx context.Context, req SetRequest, stdout, _ io.Writer) error {
|
|
var resp struct {
|
|
Logs []string `json:"logs"`
|
|
}
|
|
if err := d.sup.Call(ctx, "driver/set", req, &resp); err != nil {
|
|
return err
|
|
}
|
|
for _, l := range resp.Logs {
|
|
if stdout != nil {
|
|
_, _ = fmt.Fprintln(stdout, l)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *SidecarDriver) Start(ctx context.Context, name string, stdout, _ io.Writer) error {
|
|
var resp struct {
|
|
Logs []string `json:"logs"`
|
|
}
|
|
if err := d.sup.Call(ctx, "driver/start", map[string]any{"name": name}, &resp); err != nil {
|
|
return err
|
|
}
|
|
for _, l := range resp.Logs {
|
|
if stdout != nil {
|
|
_, _ = fmt.Fprintln(stdout, l)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *SidecarDriver) Stop(ctx context.Context, name string, force bool, stdout, _ io.Writer) error {
|
|
var resp struct {
|
|
Logs []string `json:"logs"`
|
|
}
|
|
if err := d.sup.Call(ctx, "driver/stop", map[string]any{"name": name, "force": force}, &resp); err != nil {
|
|
return err
|
|
}
|
|
for _, l := range resp.Logs {
|
|
if stdout != nil {
|
|
_, _ = fmt.Fprintln(stdout, l)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *SidecarDriver) Delete(ctx context.Context, name string, keepFiles bool, stdout, _ io.Writer) error {
|
|
var resp struct {
|
|
Logs []string `json:"logs"`
|
|
}
|
|
if err := d.sup.Call(ctx, "driver/delete", map[string]any{"name": name, "keep_files": keepFiles}, &resp); err != nil {
|
|
return err
|
|
}
|
|
for _, l := range resp.Logs {
|
|
if stdout != nil {
|
|
_, _ = fmt.Fprintln(stdout, l)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *SidecarDriver) InvokeAction(ctx context.Context, vmName, action string, params map[string]any) (any, error) {
|
|
var resp any
|
|
err := d.sup.Call(ctx, "driver/invokeAction", map[string]any{
|
|
"vm_name": vmName,
|
|
"action": action,
|
|
"params": params,
|
|
}, &resp)
|
|
return resp, err
|
|
}
|