| 1 | |
| 2 | |
| 3 | |
| 4 | package driverutil |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | "context" |
| 9 | "encoding/json" |
| 10 | "errors" |
| 11 | "fmt" |
| 12 | "os/exec" |
| 13 | "slices" |
| 14 | |
| 15 | "github.com/sirupsen/logrus" |
| 16 | |
| 17 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 18 | "github.com/lima-vm/lima/v2/pkg/registry" |
| 19 | ) |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | func ResolveVMType(y *limatype.LimaYAML) error { |
| 25 | if y.VMType == nil { |
| 26 | y.VMType = new(limatype.DefaultDriver()) |
| 27 | if y.Arch != nil && !limatype.IsNativeArch(*y.Arch) { |
| 28 | y.VMType = new(limatype.DefaultNonNativeArchDriver()) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | |
| 33 | if slices.Contains(limatype.VMTypes, *y.VMType) { |
| 34 | return nil |
| 35 | } |
| 36 | |
| 37 | |
| 38 | _, _, exists := registry.Get(*y.VMType) |
| 39 | if !exists { |
| 40 | return fmt.Errorf("vmType %#q is not a registered driver", *y.VMType) |
| 41 | } |
| 42 | |
| 43 | return nil |
| 44 | } |
| 45 | |
| 46 | func InspectStatus(ctx context.Context, inst *limatype.Instance) (string, error) { |
| 47 | if inst == nil || inst.Config == nil || inst.Config.VMType == nil { |
| 48 | return "", errors.New("instance or its configuration is not properly initialized") |
| 49 | } |
| 50 | |
| 51 | extDriver, intDriver, exists := registry.Get(*inst.Config.VMType) |
| 52 | if !exists { |
| 53 | return "", fmt.Errorf("unknown or unsupported VM type: %s", *inst.Config.VMType) |
| 54 | } |
| 55 | |
| 56 | if extDriver != nil { |
| 57 | status, err := handleInspectStatusAction(ctx, inst, extDriver.Path) |
| 58 | if err != nil { |
| 59 | extDriver.Logger.Errorf("Failed to inspect status for instance %#q: %v", inst.Name, err) |
| 60 | return "", err |
| 61 | } |
| 62 | extDriver.Logger.Debugf("Instance %#q inspected successfully with status: %s", inst.Name, inst.Status) |
| 63 | return status, nil |
| 64 | } |
| 65 | |
| 66 | return intDriver.InspectStatus(ctx, inst), nil |
| 67 | } |
| 68 | |
| 69 | func handleInspectStatusAction(ctx context.Context, inst *limatype.Instance, extDriverPath string) (string, error) { |
| 70 | cmd := exec.CommandContext(ctx, extDriverPath, "--inspect-status") |
| 71 | |
| 72 | var stderrBuf bytes.Buffer |
| 73 | cmd.Stderr = &stderrBuf |
| 74 | |
| 75 | stdin, err := cmd.StdinPipe() |
| 76 | if err != nil { |
| 77 | return "", err |
| 78 | } |
| 79 | stdout, err := cmd.StdoutPipe() |
| 80 | if err != nil { |
| 81 | return "", err |
| 82 | } |
| 83 | if err := cmd.Start(); err != nil { |
| 84 | return "", err |
| 85 | } |
| 86 | |
| 87 | encoder := json.NewEncoder(stdin) |
| 88 | payload, err := inst.MarshalJSON() |
| 89 | if err != nil { |
| 90 | return "", fmt.Errorf("failed to marshal instance config: %w", err) |
| 91 | } |
| 92 | if err := encoder.Encode(payload); err != nil { |
| 93 | return "", err |
| 94 | } |
| 95 | stdin.Close() |
| 96 | |
| 97 | decoder := json.NewDecoder(stdout) |
| 98 | var response []byte |
| 99 | if err := decoder.Decode(&response); err != nil { |
| 100 | return "", err |
| 101 | } |
| 102 | |
| 103 | var respInst limatype.Instance |
| 104 | if err := respInst.UnmarshalJSON(response); err != nil { |
| 105 | return "", fmt.Errorf("failed to unmarshal instance response: %w", err) |
| 106 | } |
| 107 | |
| 108 | if err := cmd.Wait(); err != nil { |
| 109 | if stderrBuf.Len() > 0 { |
| 110 | return "", fmt.Errorf("inspect status command failed: %w; stderr: %s", err, stderrBuf.String()) |
| 111 | } |
| 112 | return "", fmt.Errorf("inspect status command failed: %w", err) |
| 113 | } |
| 114 | |
| 115 | if stderrBuf.Len() > 0 { |
| 116 | logrus.Debugf("external driver stderr: %s", stderrBuf.String()) |
| 117 | } |
| 118 | |
| 119 | *inst = respInst |
| 120 | logrus.Debugf("Inspecting instance status action completed successfully for %#q", extDriverPath) |
| 121 | return inst.Status, nil |
| 122 | } |