跳到主要内容

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

浏览文件

template.go

262 行 · 6.9 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package cidata
5
6import (
7 "bytes"
8 "embed"
9 "errors"
10 "fmt"
11 "io/fs"
12 "path"
13 "path/filepath"
14 "strings"
15
16 "github.com/sethvargo/go-password/password"
17
18 "github.com/lima-vm/lima/v2/pkg/identifiers"
19 "github.com/lima-vm/lima/v2/pkg/iso9660util"
20 "github.com/lima-vm/lima/v2/pkg/limatype"
21 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
22 "github.com/lima-vm/lima/v2/pkg/textutil"
23)
24
25//go:embed cidata.TEMPLATE.d
26var templateFS embed.FS
27
28const templateFSRoot = "cidata.TEMPLATE.d"
29
30//go:embed wincidata.TEMPLATE.d
31var windowsTemplateFS embed.FS
32
33const windowsTemplateFSRoot = "wincidata.TEMPLATE.d"
34
35// This is for checking whether Windows OS is 11 or server 2025.
36// For example, Windows 11 x86-64's label is CCCOMA_X64FRE_EN-US_DV9.
37const windowsClientISOLabelPrefix = "CCCOMA_"
38
39type CACerts struct {
40 RemoveDefaults *bool
41 Trusted []Cert
42}
43
44type Cert struct {
45 Lines []string
46}
47
48type Containerd struct {
49 System bool
50 User bool
51 Archive string
52}
53type Network struct {
54 MACAddress string
55 Interface string
56 Metric uint32
57}
58type Mount struct {
59 Tag string
60 MountPoint string // abs path, accessible by the User
61 Type string
62 Options string
63}
64type BootCmds struct {
65 Lines []string
66}
67
68type DataFile struct {
69 FileName string
70 Overwrite string
71 Owner string
72 Path string
73 Permissions string
74}
75
76type YQProvision struct {
77 FileName string
78 Format string
79 Owner string
80 Path string
81 Permissions string
82}
83
84type Disk struct {
85 Name string
86 Device string
87 Format bool
88 FSType string
89 FSArgs []string
90}
91type TemplateArgs struct {
92 Debug bool
93 OS limatype.OS
94 Arch limatype.Arch
95 Name string // instance name
96 Hostname string // instance hostname
97 IID string // instance id
98 User string // user name
99 Comment string // user information
100 Home string // home directory
101 Shell string // login shell
102 UID uint32
103 PasswordlessSudo bool
104 SSHPubKeys []string
105 Mounts []Mount
106 MountType string
107 Disks []Disk
108 GuestInstallPrefix string
109 UpgradePackages bool
110 Containerd Containerd
111 Networks []Network
112 SlirpNICName string
113 SlirpGateway string
114 SlirpDNS string
115 SlirpIPAddress string
116 UDPDNSLocalPort int
117 TCPDNSLocalPort int
118 Env map[string]string
119 Param map[string]string
120 BootScripts bool
121 DataFiles []DataFile
122 YQProvisions []YQProvision
123 DNSAddresses []string
124 CACerts CACerts
125 HostHomeMountPoint string
126 BootCmds []BootCmds
127 RosettaEnabled bool
128 RosettaBinFmt bool
129 SkipDefaultDependencyResolution bool
130 VMType string
131 VSockPort int
132 VirtioPort string
133 Plain bool
134 TimeZone string
135 NoCloudInit bool
136 WindowsInitialPassword string
137 LegacyBIOS bool
138 IsWindowsServer bool
139 TPM bool
140 SuppressFirstLoginSetup bool
141 SuppressFirstLoginSetupPlist string // empty = use built-in plist
142}
143
144func (t *TemplateArgs) generateWindowsInitialPassword() error {
145 const pwLen = 16
146 // Avoid special characters to minimize potential keyboard layout issue.
147 pw, err := password.Generate(pwLen, pwLen/4, 0, false, false)
148 if err != nil {
149 return fmt.Errorf("failed to generate password: %w", err)
150 }
151
152 t.WindowsInitialPassword = pw
153 return nil
154}
155
156// checkWindowsVersion checks if a guest VM is Windows 11 (true) or Windows server 2025 (false).
157func (t *TemplateArgs) checkWindowsVersion(instDir string) error {
158 imagePath := filepath.Join(instDir, filenames.ISO)
159 label, err := iso9660util.Label(imagePath)
160 if err != nil {
161 return fmt.Errorf("failed to get ISO label: %w", err)
162 }
163
164 t.IsWindowsServer = !strings.HasPrefix(label, windowsClientISOLabelPrefix)
165
166 return nil
167}
168
169func ValidateTemplateArgs(args *TemplateArgs) error {
170 if err := identifiers.Validate(args.Name); err != nil {
171 return err
172 }
173 // args.User is intentionally not validated here; the user can override with any name they want
174 // limayaml.FillDefault will validate the default (local) username, but not an explicit setting
175 if args.User == "root" {
176 return errors.New("field User must not be `root`")
177 }
178 if args.UID == 0 {
179 return errors.New("field UID must not be 0")
180 }
181 if args.Home == "" {
182 return errors.New("field Home must be set")
183 }
184 if args.Shell == "" {
185 return errors.New("field Shell must be set")
186 }
187 if len(args.SSHPubKeys) == 0 {
188 return errors.New("field SSHPubKeys must be set")
189 }
190 for i, m := range args.Mounts {
191 f := m.MountPoint
192 if !path.IsAbs(f) {
193 return fmt.Errorf("field mounts[%d] must be absolute, got %#q", i, f)
194 }
195 }
196 return nil
197}
198
199func ExecuteTemplateCloudConfig(args *TemplateArgs) ([]byte, error) {
200 if err := ValidateTemplateArgs(args); err != nil {
201 return nil, err
202 }
203
204 userData, err := templateFS.ReadFile(path.Join(templateFSRoot, "user-data"))
205 if err != nil {
206 return nil, err
207 }
208
209 cloudConfigYaml := string(userData)
210 return textutil.ExecuteTemplate(cloudConfigYaml, args)
211}
212
213func executeTemplateWalkDir(args *TemplateArgs, fsys fs.FS, root string) ([]iso9660util.Entry, error) {
214 if err := ValidateTemplateArgs(args); err != nil {
215 return nil, err
216 }
217
218 fsys, err := fs.Sub(fsys, root)
219 if err != nil {
220 return nil, err
221 }
222
223 var layout []iso9660util.Entry
224 walkFn := func(path string, d fs.DirEntry, walkErr error) error {
225 if walkErr != nil {
226 return walkErr
227 }
228 if d.IsDir() {
229 return nil
230 }
231 if !d.Type().IsRegular() {
232 return fmt.Errorf("got non-regular file %#q", path)
233 }
234 templateB, err := fs.ReadFile(fsys, path)
235 if err != nil {
236 return err
237 }
238 b, err := textutil.ExecuteTemplate(string(templateB), args)
239 if err != nil {
240 return err
241 }
242 layout = append(layout, iso9660util.Entry{
243 Path: path,
244 Reader: bytes.NewReader(b),
245 })
246 return nil
247 }
248
249 if err := fs.WalkDir(fsys, ".", walkFn); err != nil {
250 return nil, err
251 }
252
253 return layout, nil
254}
255
256func ExecuteTemplateCIDataISO(args *TemplateArgs) ([]iso9660util.Entry, error) {
257 return executeTemplateWalkDir(args, templateFS, templateFSRoot)
258}
259
260func ExecuteTemplateWindowsISO(args *TemplateArgs) ([]iso9660util.Entry, error) {
261 return executeTemplateWalkDir(args, windowsTemplateFS, windowsTemplateFSRoot)
262}