跳到主要内容

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

浏览文件

ansible.go

75 行 · 1.9 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package instance
5
6import (
7 "context"
8 "fmt"
9 "os"
10 "os/exec"
11 "path/filepath"
12
13 "github.com/goccy/go-yaml"
14 "github.com/sirupsen/logrus"
15
16 "github.com/lima-vm/lima/v2/pkg/limatype"
17 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
18)
19
20func runAnsibleProvision(ctx context.Context, inst *limatype.Instance) error {
21 for _, f := range inst.Config.Provision {
22 if f.Mode == limatype.ProvisionModeAnsible {
23 logrus.Infof("Waiting for ansible playbook %#q", f.Playbook)
24 if err := runAnsiblePlaybook(ctx, inst, f.Playbook); err != nil {
25 return err
26 }
27 }
28 }
29 return nil
30}
31
32func runAnsiblePlaybook(ctx context.Context, inst *limatype.Instance, playbook string) error {
33 inventory, err := createAnsibleInventory(inst)
34 if err != nil {
35 return err
36 }
37 logrus.Debugf("ansible-playbook -i %#q %#q", inventory, playbook)
38 args := []string{"-i", inventory, playbook}
39 cmd := exec.CommandContext(ctx, "ansible-playbook", args...)
40 cmd.Env = getAnsibleEnvironment(inst)
41 cmd.Stdout = os.Stdout
42 cmd.Stderr = os.Stderr
43 return cmd.Run()
44}
45
46func createAnsibleInventory(inst *limatype.Instance) (string, error) {
47 vars := map[string]any{
48 "ansible_connection": "ssh",
49 "ansible_host": inst.Hostname,
50 "ansible_ssh_common_args": "-F " + inst.SSHConfigFile,
51 }
52 hosts := map[string]any{
53 inst.Name: vars,
54 }
55 group := "lima"
56 data := map[string]any{
57 group: map[string]any{
58 "hosts": hosts,
59 },
60 }
61 bytes, err := yaml.Marshal(data)
62 if err != nil {
63 return "", err
64 }
65 inventory := filepath.Join(inst.Dir, filenames.AnsibleInventoryYAML)
66 return inventory, os.WriteFile(inventory, bytes, 0o644)
67}
68
69func getAnsibleEnvironment(inst *limatype.Instance) []string {
70 env := os.Environ()
71 for key, val := range inst.Config.Param {
72 env = append(env, fmt.Sprintf("PARAM_%s=%s", key, val))
73 }
74 return env
75}