跳到主要内容

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

浏览文件

autostart_darwin.go

183 行 · 5.7 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package main
5
6import (
7 "context"
8 "errors"
9 "fmt"
10 "os"
11 "os/exec"
12
13 "github.com/sirupsen/logrus"
14 "github.com/spf13/cobra"
15
16 "github.com/lima-vm/lima/v2/pkg/autostart"
17 "github.com/lima-vm/lima/v2/pkg/autostart/launchd"
18 "github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
19 "github.com/lima-vm/lima/v2/pkg/store"
20 "github.com/lima-vm/lima/v2/pkg/textutil"
21)
22
23func autostartEnableAction(cmd *cobra.Command, args []string) error {
24 flags := cmd.Flags()
25 condition, err := flags.GetString("condition")
26 if err != nil {
27 return err
28 }
29 keepAlive, err := flags.GetBool("keep-alive")
30 if err != nil {
31 return err
32 }
33
34 ctx := cmd.Context()
35 inst, err := store.Inspect(ctx, args[0])
36 if err != nil {
37 if errors.Is(err, os.ErrNotExist) {
38 return fmt.Errorf("instance %q not found", args[0])
39 }
40 return err
41 }
42
43 switch condition {
44 case "login":
45 mgr := autostart.ManagerWith(keepAlive)
46 if err := mgr.RegisterToStartAtLogin(ctx, inst); err != nil {
47 return fmt.Errorf("failed to register instance %#q to start at login: %w", inst.Name, err)
48 }
49 logrus.Infof("Instance %#q registered to start at login", inst.Name)
50 case "boot":
51 userName, err := flags.GetString("user")
52 if err != nil {
53 return err
54 }
55 if userName == "" {
56 userName = os.Getenv("USER")
57 }
58 if userName == "" {
59 return errors.New("could not determine user; pass --user")
60 }
61 return daemonInstall(ctx, inst.Name, inst.Dir, userName, keepAlive)
62 default:
63 return fmt.Errorf("unknown condition %q: must be \"login\" or \"boot\"", condition)
64 }
65 return nil
66}
67
68func autostartDisableAction(cmd *cobra.Command, args []string) error {
69 ctx := cmd.Context()
70 inst, err := store.Inspect(ctx, args[0])
71 if err != nil {
72 if errors.Is(err, os.ErrNotExist) {
73 return fmt.Errorf("instance %q not found", args[0])
74 }
75 return err
76 }
77
78 // Check for a LaunchAgent (login) registration first.
79 if registered, err := autostart.IsRegistered(ctx, inst); err != nil {
80 return err
81 } else if registered {
82 if err := autostart.UnregisterFromStartAtLogin(ctx, inst); err != nil {
83 return fmt.Errorf("failed to unregister instance %#q from start at login: %w", inst.Name, err)
84 }
85 logrus.Infof("Instance %#q unregistered from start at login", inst.Name)
86 return nil
87 }
88
89 // Check for a LaunchDaemon (boot) installation.
90 daemonPath := launchd.GetDaemonPlistPath(inst.Name)
91 if _, err := os.Stat(daemonPath); err == nil {
92 return daemonUninstall(ctx, inst.Name)
93 }
94
95 logrus.Infof("Instance %#q is not registered for automatic startup", inst.Name)
96 return nil
97}
98
99// renderDaemonPlist renders the system LaunchDaemon plist for an instance. Every key the
100// template references must be supplied here: a missing one renders as the literal string
101// "<no value>" rather than failing, which would be written into the installed plist.
102func renderDaemonPlist(binary, instName, workDir, userName string, keepAlive bool) ([]byte, error) {
103 // The daemon inherits no environment from the shell that registers it, so LIMA_HOME
104 // has to be recorded in the plist for the instance to be found at boot.
105 limaHome, err := dirnames.LimaDir()
106 if err != nil {
107 return nil, err
108 }
109 vars := map[string]string{
110 "Binary": binary,
111 "Instance": instName,
112 "LimaHome": limaHome,
113 "WorkDir": workDir,
114 "UserName": userName,
115 }
116 if keepAlive {
117 vars["KeepAlive"] = "true"
118 }
119 return textutil.ExecuteTemplate(launchd.DaemonTemplate, vars)
120}
121
122func daemonInstall(ctx context.Context, instName, workDir, userName string, keepAlive bool) error {
123 selfExe, err := os.Executable()
124 if err != nil {
125 return fmt.Errorf("could not determine limactl path: %w", err)
126 }
127
128 content, err := renderDaemonPlist(selfExe, instName, workDir, userName, keepAlive)
129 if err != nil {
130 return fmt.Errorf("failed to render daemon plist: %w", err)
131 }
132
133 tmp, err := os.CreateTemp("", "io.lima-vm.daemon.*.plist")
134 if err != nil {
135 return fmt.Errorf("failed to create temp file: %w", err)
136 }
137 defer os.Remove(tmp.Name())
138 if _, err := tmp.Write(content); err != nil {
139 return fmt.Errorf("failed to write temp plist: %w", err)
140 }
141 tmp.Close()
142
143 destPath := launchd.GetDaemonPlistPath(instName)
144 svcTarget := "system/" + launchd.DaemonServiceNameFrom(instName)
145
146 _ = runSudo(ctx, "launchctl", "bootout", svcTarget)
147 if err := runSudo(ctx, "install", "-m", "644", tmp.Name(), destPath); err != nil {
148 return fmt.Errorf("failed to install plist to %s: %w", destPath, err)
149 }
150 if err := runSudo(ctx, "launchctl", "enable", svcTarget); err != nil {
151 return fmt.Errorf("failed to enable LaunchDaemon: %w", err)
152 }
153 if err := runSudo(ctx, "launchctl", "bootstrap", "system", destPath); err != nil {
154 return fmt.Errorf("failed to bootstrap LaunchDaemon: %w", err)
155 }
156
157 logrus.Infof("LaunchDaemon installed for instance %q (runs as %q at boot)", instName, userName)
158 logrus.Infof("Plist: %s", destPath)
159 return nil
160}
161
162func daemonUninstall(ctx context.Context, instName string) error {
163 svcTarget := "system/" + launchd.DaemonServiceNameFrom(instName)
164 destPath := launchd.GetDaemonPlistPath(instName)
165
166 _ = runSudo(ctx, "launchctl", "bootout", svcTarget)
167 _ = runSudo(ctx, "launchctl", "disable", svcTarget)
168 if err := runSudo(ctx, "rm", "-f", destPath); err != nil {
169 return fmt.Errorf("failed to remove %s: %w", destPath, err)
170 }
171 logrus.Infof("LaunchDaemon uninstalled for instance %q", instName)
172 return nil
173}
174
175// runSudo executes a command under sudo, inheriting the terminal so password prompts work.
176func runSudo(ctx context.Context, args ...string) error {
177 cmd := exec.CommandContext(ctx, "sudo", args...) //nolint:gosec // args are constructed internally, not from user input
178 cmd.Stdout = os.Stdout
179 cmd.Stderr = os.Stderr
180 cmd.Stdin = os.Stdin
181 logrus.Infof("running: sudo %v", args)
182 return cmd.Run()
183}