跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

vm.go

122 行 · 3.3 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package driverutil
5
6import (
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// ResolveVMType sets y.VMType to the appropriate default if it is nil.
22// It validates the VMType is a known type (built-in or discovered external driver)
23// but does NOT require the driver to be available on the current platform.
24func 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 // Accept built-in VMType regardless of current platform.
33 if slices.Contains(limatype.VMTypes, *y.VMType) {
34 return nil
35 }
36
37 // Also accept external drivers discovered in the registry.
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
46func 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
69func 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}