| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "os" |
| 10 | "os/signal" |
| 11 | "path/filepath" |
| 12 | "runtime" |
| 13 | "syscall" |
| 14 | |
| 15 | "github.com/sylvinus/agent-vm/internal/cage" |
| 16 | "github.com/sylvinus/agent-vm/internal/containerenv" |
| 17 | "github.com/sylvinus/agent-vm/internal/managedproxy" |
| 18 | "github.com/sylvinus/agent-vm/internal/workbench" |
| 19 | ) |
| 20 | |
| 21 | func configureContainer(configPath string) error { |
| 22 | if runtime.GOOS != "linux" { |
| 23 | return errors.New("容器服务端需要 Linux;本机可使用轻量客户端连接") |
| 24 | } |
| 25 | profile := filepath.Dir(configPath) |
| 26 | entries, err := os.ReadDir(filepath.Join(profile, "state", "lima")) |
| 27 | if err != nil && !os.IsNotExist(err) { |
| 28 | return err |
| 29 | } |
| 30 | if len(entries) > 0 { |
| 31 | return errors.New("此配置已有虚拟机数据;请为云端容器使用独立配置,原有数据保持不变") |
| 32 | } |
| 33 | var config containerenv.Config |
| 34 | decoder := json.NewDecoder(io.LimitReader(os.Stdin, 64<<10)) |
| 35 | decoder.DisallowUnknownFields() |
| 36 | if err := decoder.Decode(&config); err != nil { |
| 37 | return errors.New("容器配置格式无效") |
| 38 | } |
| 39 | var extra any |
| 40 | if decoder.Decode(&extra) != io.EOF { |
| 41 | return errors.New("只接受一份容器配置") |
| 42 | } |
| 43 | if _, err := containerenv.Configure(profile, config); err != nil { |
| 44 | return err |
| 45 | } |
| 46 | return json.NewEncoder(os.Stdout).Encode(map[string]bool{"configured": true}) |
| 47 | } |
| 48 | |
| 49 | func containerOperation(configPath string, args []string, cfg cage.Config) (bool, error) { |
| 50 | profile := filepath.Dir(configPath) |
| 51 | if !containerenv.Configured(profile) { |
| 52 | return false, nil |
| 53 | } |
| 54 | switch args[0] { |
| 55 | case "setup", "up", "login", "stop", "list", "info", "doctor", "name", "run", "shell", "claude", "update-tools", "access": |
| 56 | default: |
| 57 | return false, nil |
| 58 | } |
| 59 | if runtime.GOOS != "linux" { |
| 60 | return true, errors.New("容器运行环境只能在 Linux 服务端使用") |
| 61 | } |
| 62 | if args[0] == "access" { |
| 63 | return true, errors.New("容器工作环境使用 Cage 工作台的受认证入口,不提供旧版独立查看器凭据") |
| 64 | } |
| 65 | if args[0] != "run" && args[0] != "claude" && len(args) != 1 { |
| 66 | return true, errors.New("该容器管理命令不接受额外参数") |
| 67 | } |
| 68 | environment, err := containerenv.Load(profile) |
| 69 | if err != nil { |
| 70 | return true, err |
| 71 | } |
| 72 | ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) |
| 73 | defer cancel() |
| 74 | switch args[0] { |
| 75 | case "stop": |
| 76 | if len(args) != 1 { |
| 77 | return true, errors.New("容器配置只管理自身的持久环境") |
| 78 | } |
| 79 | if err := environment.Stop(ctx); err != nil { |
| 80 | return true, err |
| 81 | } |
| 82 | return true, managedproxy.Stop(profile) |
| 83 | case "list", "info": |
| 84 | state, err := environment.State(ctx) |
| 85 | if err != nil { |
| 86 | return true, err |
| 87 | } |
| 88 | return true, json.NewEncoder(os.Stdout).Encode(state) |
| 89 | case "doctor": |
| 90 | inspection, err := environment.Inspect(ctx) |
| 91 | if err != nil { |
| 92 | return true, err |
| 93 | } |
| 94 | return true, json.NewEncoder(os.Stdout).Encode(inspection) |
| 95 | case "name": |
| 96 | fmt.Println(environment.Name()) |
| 97 | return true, nil |
| 98 | case "setup": |
| 99 | return true, environment.Prepare(ctx, os.Stdout) |
| 100 | } |
| 101 | if err := cfg.RequireManagedProxy(profile); err != nil { |
| 102 | return true, err |
| 103 | } |
| 104 | if err := cfg.CheckRunning(filepath.Join(profile, "state")); err != nil { |
| 105 | return true, err |
| 106 | } |
| 107 | if err := os.MkdirAll(cage.EnvironmentDir(profile), 0700); err != nil { |
| 108 | return true, err |
| 109 | } |
| 110 | if err := environment.Prepare(ctx, os.Stdout); err != nil { |
| 111 | return true, err |
| 112 | } |
| 113 | state, err := environment.State(ctx) |
| 114 | if err != nil { |
| 115 | return true, err |
| 116 | } |
| 117 | needsServices := !state.Ready || args[0] == "up" || args[0] == "login" |
| 118 | if err := environment.Start(ctx, configPath, os.Stdout); err != nil { |
| 119 | return true, err |
| 120 | } |
| 121 | if needsServices { |
| 122 | if err := workbench.StartGuestServices(ctx, configPath, cfg, os.Stdout, os.Stderr); err != nil { |
| 123 | return true, err |
| 124 | } |
| 125 | } |
| 126 | switch args[0] { |
| 127 | case "up": |
| 128 | fmt.Println("Cage 容器服务已启动;工作台完成网络检查后才会放行外网。") |
| 129 | return true, nil |
| 130 | case "update-tools": |
| 131 | return true, workbench.RunMaintenanceScript(ctx, configPath, cage.ToolsUpdateScript(), os.Stdout, os.Stderr) |
| 132 | case "login": |
| 133 | return true, workbench.RunEnvironmentCommand(ctx, configPath, []string{"claude", "auth", "login"}, os.Stdin, os.Stdout, os.Stderr, true) |
| 134 | case "shell": |
| 135 | return true, workbench.RunEnvironmentCommand(ctx, configPath, []string{"/bin/bash", "-l"}, os.Stdin, os.Stdout, os.Stderr, true) |
| 136 | case "claude": |
| 137 | return true, workbench.RunEnvironmentCommand(ctx, configPath, append([]string{"claude"}, args[1:]...), os.Stdin, os.Stdout, os.Stderr, true) |
| 138 | case "run": |
| 139 | if len(args) < 2 { |
| 140 | return true, errors.New("cage run 需要指定命令") |
| 141 | } |
| 142 | return true, workbench.RunEnvironmentCommand(ctx, configPath, args[1:], os.Stdin, os.Stdout, os.Stderr, false) |
| 143 | } |
| 144 | return true, nil |
| 145 | } |