| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | "time" |
| 12 | |
| 13 | agentvm "github.com/sylvinus/agent-vm" |
| 14 | "github.com/sylvinus/agent-vm/internal/cage" |
| 15 | "github.com/sylvinus/agent-vm/internal/diagnostics" |
| 16 | "github.com/sylvinus/agent-vm/internal/env" |
| 17 | "github.com/sylvinus/agent-vm/internal/host" |
| 18 | "github.com/sylvinus/agent-vm/internal/hostimage" |
| 19 | "github.com/sylvinus/agent-vm/internal/state" |
| 20 | "github.com/sylvinus/agent-vm/internal/ui" |
| 21 | "github.com/sylvinus/agent-vm/internal/version" |
| 22 | "github.com/sylvinus/agent-vm/internal/vm" |
| 23 | "github.com/sylvinus/agent-vm/internal/vmname" |
| 24 | ) |
| 25 | |
| 26 | const setupUsage = "Usage: agent-vm setup [--disk GB] [--memory GB] [--cpus N] [--preinstall=LIST]" |
| 27 | |
| 28 | const setupHelp = `Usage: agent-vm setup [options] |
| 29 | |
| 30 | Create a base VM template with dev tools and agents pre-installed. Runs an |
| 31 | interactive wizard by default; the first prompt offers a "default install" |
| 32 | (everything except the opt-in Ruby, Rust, Go, Pi, Playwright MCP, code-server |
| 33 | and its extensions). Answer 'n' for per-component prompts. Pass |
| 34 | --preinstall=... to skip the wizard and pick a specific subset |
| 35 | non-interactively. When no terminal can be asked on (no controlling |
| 36 | terminal, or stderr redirected, as in CI), the wizard is skipped |
| 37 | automatically and the default set is installed. |
| 38 | |
| 39 | Options: |
| 40 | --disk GB VM disk size (default: 10) |
| 41 | --memory GB VM memory (default: 3, 4 with code-server in the wizard) |
| 42 | --cpus N Number of CPUs (default: 1) |
| 43 | --preinstall=LIST Comma-separated list of tools to preinstall in the base |
| 44 | VM image (skips the wizard). Anything not listed is |
| 45 | skipped. Use: |
| 46 | 'default' for the default set |
| 47 | (everything except Ruby, Rust, Go, Pi, |
| 48 | mcp-playwright, code-server, code-*), |
| 49 | 'all' for everything, |
| 50 | 'none' for nothing. |
| 51 | Available names: |
| 52 | python, node, ruby, rust, golang, docker, chromium, |
| 53 | gh, claude, opencode, codex, vibe, pi, mcp-chrome, |
| 54 | mcp-playwright, code-server, code-claude, |
| 55 | code-opencode, code-codex, code-vibe |
| 56 | 'code-server' is VS Code in the browser, for |
| 57 | 'agent-vm code', with GitHub Copilot turned off. |
| 58 | 'code-claude', 'code-opencode', 'code-codex' and |
| 59 | 'code-vibe' add that agent's extension, and code-server |
| 60 | with it. The Claude, Codex and Vibe extensions bring |
| 61 | their own copy of the agent: 'claude', 'codex' and |
| 62 | 'vibe' are the command-line ones, for 'agent-vm claude' |
| 63 | and the others. OpenCode's does not: 'code-opencode' |
| 64 | also installs 'opencode'. |
| 65 | Selecting codex or pi also installs node (npm). So does |
| 66 | mcp-chrome when chromium and an agent are selected |
| 67 | (npx). mcp-playwright does not: list node yourself. |
| 68 | The mcp-* names wire an MCP server into each installed |
| 69 | agent's config. Both 'mcp-chrome' (Chrome DevTools) and |
| 70 | 'mcp-playwright' drive the preinstalled Chromium, so |
| 71 | both need node and chromium and are skipped, with a |
| 72 | notice, without them. Pi has no MCP support, so they |
| 73 | are not wired into it. 'mcp-playwright' is opt-in and not |
| 74 | part of 'default': a second browser-driving server is |
| 75 | redundant for most users. Omit them to leave the agents' |
| 76 | MCP config untouched, for when MCP servers are |
| 77 | managed per project rather than baked into the image. |
| 78 | Examples: |
| 79 | --preinstall=default,rust # default set plus Rust |
| 80 | --preinstall=python,docker,claude |
| 81 | --preinstall=node,chromium,opencode # no chrome MCP |
| 82 | --preinstall=default,code-claude # plus the editor |
| 83 | --help Show this help |
| 84 | ` |
| 85 | |
| 86 | |
| 87 | |
| 88 | var software = []string{"python", "node", "ruby", "rust", "golang", "docker", "chromium", "gh", "claude", "opencode", "codex", "vibe", "pi", |
| 89 | "code-server", "code-claude", "code-opencode", "code-codex", "code-vibe", "mcp-chrome", "mcp-playwright"} |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | var defaultSoftware = []string{"python", "node", "docker", "chromium", "gh", "claude", "opencode", "codex", "vibe", "mcp-chrome"} |
| 96 | |
| 97 | |
| 98 | var skipPrereqs = false |
| 99 | |
| 100 | type install map[string]bool |
| 101 | |
| 102 | func (in install) set(names ...string) { |
| 103 | for _, n := range names { |
| 104 | in[n] = true |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | func (in install) nodeNeededBy() string { |
| 111 | switch { |
| 112 | case in["codex"]: |
| 113 | return "Codex CLI requires npm" |
| 114 | case in["pi"]: |
| 115 | return "Pi requires npm" |
| 116 | case in["chromium"] && in["mcp-chrome"] && (in["claude"] || in["opencode"] || in["vibe"] || in["code-claude"] || in["code-codex"] || in["code-vibe"]): |
| 117 | return "Chrome DevTools MCP uses npx" |
| 118 | } |
| 119 | return "" |
| 120 | } |
| 121 | |
| 122 | |
| 123 | func (in install) exports() string { |
| 124 | var b strings.Builder |
| 125 | for _, n := range software { |
| 126 | v := 0 |
| 127 | if in[n] { |
| 128 | v = 1 |
| 129 | } |
| 130 | fmt.Fprintf(&b, "export AGENT_VM_INSTALL_%s=%d\n", strings.ToUpper(strings.ReplaceAll(n, "-", "_")), v) |
| 131 | } |
| 132 | return b.String() |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | func (e *app) setupCmd(ctx context.Context, args []string) int { |
| 138 | disk, memory, cpus := 10, 3, 1 |
| 139 | memorySeen := false |
| 140 | in := install{} |
| 141 | in.set(defaultSoftware...) |
| 142 | preinstall, preinstallSeen := "", false |
| 143 | for len(args) > 0 { |
| 144 | o := newVMOpts() |
| 145 | n, err := o.take(args) |
| 146 | if err != nil { |
| 147 | fmt.Fprintf(e.io.Stderr, "Error: %v\n", err) |
| 148 | return exitUsage |
| 149 | } |
| 150 | if n > 0 { |
| 151 | switch { |
| 152 | case o.Disk > 0: |
| 153 | disk = o.Disk |
| 154 | case o.Memory > 0: |
| 155 | memory, memorySeen = o.Memory, true |
| 156 | case o.CPUs > 0: |
| 157 | cpus = o.CPUs |
| 158 | default: |
| 159 | name, _, _ := strings.Cut(args[0], "=") |
| 160 | fmt.Fprintf(e.io.Stderr, "Unknown option: %s\n%s\n", name, setupUsage) |
| 161 | return exitUsage |
| 162 | } |
| 163 | args = args[n:] |
| 164 | continue |
| 165 | } |
| 166 | switch a := args[0]; { |
| 167 | case a == "--help" || a == "-h": |
| 168 | fmt.Fprint(e.io.Stdout, setupHelp) |
| 169 | return 0 |
| 170 | case a == "--preinstall": |
| 171 | |
| 172 | |
| 173 | preinstallSeen = true |
| 174 | if len(args) > 1 && args[1] != "" && !strings.HasPrefix(args[1], "-") { |
| 175 | preinstall, args = args[1], args[2:] |
| 176 | } else { |
| 177 | args = args[1:] |
| 178 | } |
| 179 | case strings.HasPrefix(a, "--preinstall="): |
| 180 | preinstall, preinstallSeen, args = strings.TrimPrefix(a, "--preinstall="), true, args[1:] |
| 181 | default: |
| 182 | fmt.Fprintf(e.io.Stderr, "Unknown option: %s\n%s\n", a, setupUsage) |
| 183 | return exitUsage |
| 184 | } |
| 185 | } |
| 186 | if preinstallSeen { |
| 187 | in = install{} |
| 188 | if preinstall == "" { |
| 189 | preinstall = "default" |
| 190 | } |
| 191 | for _, f := range strings.Split(preinstall, ",") { |
| 192 | f = strings.TrimPrefix(strings.TrimSuffix(f, " "), " ") |
| 193 | switch { |
| 194 | case f == "": |
| 195 | case f == "all": |
| 196 | in.set(software...) |
| 197 | case f == "default": |
| 198 | in.set(defaultSoftware...) |
| 199 | case f == "none": |
| 200 | case f == "code-claude" || f == "code-codex" || f == "code-vibe": |
| 201 | |
| 202 | in.set(f, "code-server") |
| 203 | case f == "code-opencode": |
| 204 | |
| 205 | in.set(f, "code-server", "opencode") |
| 206 | case contains(software, f): |
| 207 | in.set(f) |
| 208 | default: |
| 209 | fmt.Fprintf(e.io.Stderr, "Unknown preinstall name: %s (names are lowercase)\n", f) |
| 210 | fmt.Fprintf(e.io.Stderr, "Valid: %s, default, all, none\n", strings.Join(software, ", ")) |
| 211 | return exitUsage |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | fmt.Fprintln(e.io.Stdout, "Starting agent-vm setup...") |
| 217 | if !skipPrereqs && (!host.LinuxPrereqs(e.io.Stderr) || !host.WindowsPrereqs(e.io.Stdout, e.io.Stderr)) { |
| 218 | return 1 |
| 219 | } |
| 220 | if !preinstallSeen && e.ui.CanAsk() { |
| 221 | disk, memory, cpus = e.wizard(in, disk, memory, cpus, memorySeen) |
| 222 | } |
| 223 | if r := in.nodeNeededBy(); r != "" && !in["node"] { |
| 224 | fmt.Fprintf(e.io.Stderr, "Enabling Node.js because %s.\n", r) |
| 225 | in["node"] = true |
| 226 | } |
| 227 | return e.buildBase(ctx, in, disk, memory, cpus) |
| 228 | } |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | func (e *app) swapBase(ctx context.Context, b vm.Backend) bool { |
| 236 | w := e.io.Stderr |
| 237 | unlock, err := e.lockVM(ctx, vmname.Template) |
| 238 | if err != nil { |
| 239 | fmt.Fprintf(w, "Error: could not lock the base template: %v. The new base is VM '%s'; 'agent-vm setup' again builds another.\n", err, building) |
| 240 | return false |
| 241 | } |
| 242 | defer unlock() |
| 243 | if d := filepath.Join(LimaHome(e.state), vmname.Template); isDirNoYAML(d) { |
| 244 | fmt.Fprintf(w, "Detected partial VM state at %s (no lima.yaml): cleaning up.\n", d) |
| 245 | if err := os.RemoveAll(d); err != nil { |
| 246 | fmt.Fprintf(w, "Error: could not remove %s: %v\n", d, err) |
| 247 | return false |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | |
| 252 | if !e.deleteVM(ctx, vmname.Template) { |
| 253 | fmt.Fprintf(w, "Error: could not replace the base VM. The new one is VM '%s'; 'agent-vm setup' again builds another.\n", building) |
| 254 | return false |
| 255 | } |
| 256 | if err := b.Clone(ctx, building, vmname.Template); err != nil { |
| 257 | fmt.Fprintf(w, "Error: could not copy the new base VM to '%s': %v\nThere is no base now. The new one is VM '%s'; 'agent-vm setup' again builds another.\n", vmname.Template, err, building) |
| 258 | return false |
| 259 | } |
| 260 | |
| 261 | if err := e.state.Write(state.BaseBuiltBy, []byte(version.Version+"\n")); err != nil { |
| 262 | fmt.Fprintf(w, "Error: could not record the version that built the base: %v\nThe base VM is not marked ready: 'agent-vm setup' again.\n", err) |
| 263 | return false |
| 264 | } |
| 265 | if err := e.state.Write(state.BaseVersion, []byte(strconv.FormatInt(time.Now().Unix(), 10)+"\n")); err != nil { |
| 266 | fmt.Fprintf(w, "Error: could not record when the base was built: %v\nThe base VM is not marked ready: 'agent-vm setup' again.\n", err) |
| 267 | return false |
| 268 | } |
| 269 | if !e.deleteVM(ctx, building) { |
| 270 | fmt.Fprintf(w, "Warning: the base is ready, but VM '%s' stays: 'agent-vm rm %s' frees its disk.\n", building, building) |
| 271 | } |
| 272 | return true |
| 273 | } |
| 274 | |
| 275 | func contains(s []string, v string) bool { |
| 276 | for _, x := range s { |
| 277 | if x == v { |
| 278 | return true |
| 279 | } |
| 280 | } |
| 281 | return false |
| 282 | } |
| 283 | |
| 284 | |
| 285 | func (e *app) wizard(in install, disk, memory, cpus int, memorySeen bool) (int, int, int) { |
| 286 | w := e.io.Stderr |
| 287 | u := e.ui |
| 288 | yn := func(q string, def bool) bool { return u.AskYN(q, def) } |
| 289 | fmt.Fprint(w, "\nagent-vm setup wizard\n─────────────────────\n\n"+ |
| 290 | "These settings apply to the base VM image. Every per-project VM is\n"+ |
| 291 | "cloned from it, so anything preinstalled here is available in all\n"+ |
| 292 | "future agent VMs. You can still install extra tools inside any\n"+ |
| 293 | "individual VM later (e.g. via `agent-vm shell`).\n\n"+ |
| 294 | "For more: https://www.agent-vm.org/\n\n"+ |
| 295 | "Software\n────────\n"+ |
| 296 | " Agents: Claude Code, OpenCode, Codex CLI, Mistral Vibe\n"+ |
| 297 | " Tools: Python, Node.js, Docker, Chromium, gh,\n"+ |
| 298 | " Chrome DevTools MCP\n"+ |
| 299 | " Skip: Pi, Ruby, Rust, Go, Playwright MCP, code-server\n\n") |
| 300 | if !yn("Use this default", true) { |
| 301 | fmt.Fprint(w, "\nAI coding agents\n────────────────\n") |
| 302 | in["claude"] = yn("Claude Code", true) |
| 303 | in["opencode"] = yn("OpenCode", true) |
| 304 | in["codex"] = yn("Codex CLI", true) |
| 305 | in["vibe"] = yn("Mistral Vibe", true) |
| 306 | in["pi"] = yn("Pi", false) |
| 307 | fmt.Fprint(w, "\nEditor\n──────\n") |
| 308 | in["code-server"] = yn("code-server (VS Code in the browser, for 'agent-vm code')", false) |
| 309 | e.askCodeExtensions(in) |
| 310 | fmt.Fprint(w, "\nSystem tools\n────────────\n") |
| 311 | in["docker"] = yn("Docker", true) |
| 312 | in["chromium"] = yn("Chromium (headless browser)", true) |
| 313 | in["gh"] = yn("GitHub CLI (gh)", true) |
| 314 | in["mcp-chrome"], in["mcp-playwright"] = false, false |
| 315 | if in["chromium"] { |
| 316 | in["mcp-chrome"] = yn("Chrome DevTools MCP (wired into each agent's config)", true) |
| 317 | in["mcp-playwright"] = yn("Playwright MCP (also drives that Chromium)", false) |
| 318 | } |
| 319 | reason := in.nodeNeededBy() |
| 320 | fmt.Fprint(w, "\nLanguages\n─────────\n") |
| 321 | in["python"] = yn("Python 3", true) |
| 322 | if reason != "" { |
| 323 | in["node"] = true |
| 324 | fmt.Fprintf(w, "Node.js 24: yes (%s)\n", reason) |
| 325 | } else { |
| 326 | in["node"] = yn("Node.js 24", true) |
| 327 | } |
| 328 | in["ruby"] = yn("Ruby", false) |
| 329 | in["rust"] = yn("Rust", false) |
| 330 | in["golang"] = yn("Go", false) |
| 331 | } |
| 332 | |
| 333 | if in["code-server"] && !memorySeen { |
| 334 | memory = 4 |
| 335 | } |
| 336 | fmt.Fprint(w, "\nDefault resources\n─────────────────\n(per-VM override with --disk / --memory / --cpus on any agent-vm command)\n\n") |
| 337 | fmt.Fprintf(w, " Disk %d GB\n Memory %d GB\n CPUs %d\n\n", disk, memory, cpus) |
| 338 | if !yn("Use these defaults", true) { |
| 339 | disk = u.AskInt("Disk size in GB", disk, 0) |
| 340 | memory = u.AskInt("Memory in GB", memory, 0) |
| 341 | cpus = u.AskInt("Number of CPUs", cpus, 0) |
| 342 | } |
| 343 | fmt.Fprintln(w) |
| 344 | return disk, memory, cpus |
| 345 | } |
| 346 | |
| 347 | |
| 348 | |
| 349 | func (e *app) askCodeExtensions(in install) { |
| 350 | if !in["code-server"] { |
| 351 | return |
| 352 | } |
| 353 | var names []string |
| 354 | for _, a := range []struct{ key, name string }{ |
| 355 | {"claude", "Claude Code"}, |
| 356 | {"opencode", "OpenCode"}, |
| 357 | {"codex", "Codex"}, |
| 358 | {"vibe", "Mistral Vibe"}, |
| 359 | } { |
| 360 | if in[a.key] { |
| 361 | names = append(names, a.name) |
| 362 | } |
| 363 | } |
| 364 | if len(names) == 0 { |
| 365 | return |
| 366 | } |
| 367 | fmt.Fprintf(e.io.Stderr, " %s in the editor:\n", strings.Join(names, ", ")) |
| 368 | if in["opencode"] { |
| 369 | fmt.Fprintln(e.io.Stderr, " (OpenCode's extension needs its command line: it stays installed.)") |
| 370 | } |
| 371 | choice := e.ui.AskChoice("Pick one", 1, "Extension and command line (agent-vm claude...)", "Extension only", "Command line only") |
| 372 | if choice == 3 { |
| 373 | return |
| 374 | } |
| 375 | in["code-claude"], in["code-opencode"], in["code-codex"], in["code-vibe"] = in["claude"], in["opencode"], in["codex"], in["vibe"] |
| 376 | if choice == 2 { |
| 377 | in["claude"], in["codex"], in["vibe"] = false, false, false |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | |
| 382 | |
| 383 | const building = vmname.Template + "-building" |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | func (e *app) buildBase(ctx context.Context, in install, disk, memory, cpus int) int { |
| 390 | b, err := e.backend() |
| 391 | if err != nil { |
| 392 | fmt.Fprintf(e.io.Stderr, "Error: %v\n", err) |
| 393 | return 1 |
| 394 | } |
| 395 | |
| 396 | unlockSetup, err := e.lockVM(ctx, vmname.Template+"-setup") |
| 397 | if err != nil { |
| 398 | fmt.Fprintf(e.io.Stderr, "Error: could not lock the base template's setup: %v\n", err) |
| 399 | return 1 |
| 400 | } |
| 401 | defer unlockSetup() |
| 402 | if !(&start{app: e, ctx: ctx}).limaOverridesOK() { |
| 403 | return 1 |
| 404 | } |
| 405 | if err := policiesErr(e.state); err != nil { |
| 406 | fmt.Fprintf(e.io.Stderr, "Error: %v\n", err) |
| 407 | return 1 |
| 408 | } |
| 409 | warnDomains(e.state, e.io.Stderr) |
| 410 | |
| 411 | if d := filepath.Join(LimaHome(e.state), building); isDirNoYAML(d) { |
| 412 | fmt.Fprintf(e.io.Stderr, "Detected partial VM state at %s (no lima.yaml): cleaning up.\n", d) |
| 413 | if err := os.RemoveAll(d); err != nil { |
| 414 | fmt.Fprintf(e.io.Stderr, "Error: could not remove %s: %v\n", d, err) |
| 415 | return 1 |
| 416 | } |
| 417 | } |
| 418 | resume := false |
| 419 | if os.Getenv("CLAUDE_CAGE_CONFIG") != "" { |
| 420 | previous, err := b.Get(ctx, building) |
| 421 | resume = err == nil && previous != nil |
| 422 | } |
| 423 | if !resume && !e.deleteVM(ctx, building) { |
| 424 | return 1 |
| 425 | } |
| 426 | |
| 427 | cpus = host.Cap("cpus", cpus, e.io.Stderr) |
| 428 | memory = host.Cap("memory", memory, e.io.Stderr) |
| 429 | host.WarnDiskSpace(LimaHome(e.state), disk, e.io.Stderr) |
| 430 | |
| 431 | if err := os.MkdirAll(string(e.state), 0o755); err != nil { |
| 432 | fmt.Fprintf(e.io.Stderr, "Error: %v\n", err) |
| 433 | return 1 |
| 434 | } |
| 435 | logPath := e.state.Path("setup.log") |
| 436 | var log io.WriteCloser |
| 437 | if configPath := os.Getenv(cage.ConfigEnv); configPath != "" { |
| 438 | logPath = filepath.Join(filepath.Dir(configPath), "logs") |
| 439 | log = diagnostics.New(filepath.Dir(configPath)).Stream("setup", "info") |
| 440 | } else { |
| 441 | log, err = os.Create(logPath) |
| 442 | } |
| 443 | if err != nil { |
| 444 | fmt.Fprintf(e.io.Stderr, "Error: %v\n", err) |
| 445 | return 1 |
| 446 | } |
| 447 | defer log.Close() |
| 448 | |
| 449 | |
| 450 | windowed := func(fn func(w io.Writer) error) bool { |
| 451 | win := e.ui.NewWindow(e.io.Stdout, log) |
| 452 | err := fn(win) |
| 453 | win.Close() |
| 454 | if err != nil { |
| 455 | fmt.Fprintf(log, "%v\n", err) |
| 456 | fmt.Fprintf(e.io.Stderr, "Error: %s\n", diagnostics.Redact(err.Error())) |
| 457 | if f, ok := e.io.Stdout.(*os.File); ok && ui.IsTerminal(f) && os.Getenv(cage.ConfigEnv) == "" { |
| 458 | fmt.Fprint(e.io.Stderr, ui.Tail(logPath, 20)) |
| 459 | } |
| 460 | return false |
| 461 | } |
| 462 | return true |
| 463 | } |
| 464 | stopBase := func() { _ = b.Stop(ctx, building) } |
| 465 | |
| 466 | fmt.Fprintln(e.io.Stdout, "Creating base VM...") |
| 467 | none := []vm.Mount{} |
| 468 | if !resume && !windowed(func(w io.Writer) error { |
| 469 | template, err := hostimage.LinuxTemplate(string(e.state)) |
| 470 | if err != nil { |
| 471 | return err |
| 472 | } |
| 473 | return b.Create(ctx, building, template, vm.Settings{CPUs: cpus, MemoryGiB: memory, DiskGiB: disk, Mounts: &none, NoContainerd: true}, w) |
| 474 | }) { |
| 475 | fmt.Fprintf(e.io.Stderr, "Error: Failed to create base VM. Full log: %s\n", logPath) |
| 476 | return 1 |
| 477 | } |
| 478 | (&start{app: e, ctx: ctx, name: building}).printResources() |
| 479 | |
| 480 | fmt.Fprintln(e.io.Stdout, "Starting the selected base system...") |
| 481 | if !windowed(func(w io.Writer) error { return e.startVM(ctx, b, building, w) }) { |
| 482 | ha := filepath.Join(LimaHome(e.state), building, "ha.stderr.log") |
| 483 | fmt.Fprintf(e.io.Stderr, "Error: Failed to start base VM. Full log: %s\nLima's own log: %s\n", logPath, ha) |
| 484 | host.WindowsStartHint(e.io.Stderr, logPath, ha) |
| 485 | return 1 |
| 486 | } |
| 487 | |
| 488 | |
| 489 | |
| 490 | fmt.Fprintln(e.io.Stdout, "Installing packages inside VM...") |
| 491 | if !windowed(func(w io.Writer) error { |
| 492 | code, err := b.Shell(ctx, building, vm.ShellOpts{Args: []string{"bash", "-l"}, |
| 493 | Stdin: strings.NewReader(cageInstallEnv() + in.exports() + agentvm.SetupScript), Stdout: w, Stderr: w}) |
| 494 | if err == nil && code != 0 { |
| 495 | err = fmt.Errorf("exit status %d", code) |
| 496 | } |
| 497 | return err |
| 498 | }) { |
| 499 | fmt.Fprintf(e.io.Stderr, "Error: Setup script failed. Full log: %s\n", logPath) |
| 500 | stopBase() |
| 501 | return 1 |
| 502 | } |
| 503 | |
| 504 | if user, ok := readRegular(e.state.Path("setup.sh")); ok { |
| 505 | fmt.Fprintf(e.io.Stdout, "Running custom setup from %s...\n", e.state.Path("setup.sh")) |
| 506 | shell := "zsh" |
| 507 | if os.Getenv(cage.ConfigEnv) != "" { |
| 508 | |
| 509 | |
| 510 | shell = "bash" |
| 511 | } |
| 512 | code, err := b.Shell(ctx, building, vm.ShellOpts{Args: []string{shell, "-l"}, |
| 513 | Stdin: strings.NewReader(env.StripCR(user)), Stdout: e.io.Stdout, Stderr: e.io.Stderr}) |
| 514 | if err != nil || code != 0 { |
| 515 | fmt.Fprintln(e.io.Stderr, "Error: Custom setup script failed.") |
| 516 | stopBase() |
| 517 | return 1 |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | |
| 522 | if !e.stopVM(ctx, building) { |
| 523 | fmt.Fprintf(e.io.Stderr, "Error: the new base VM is set up but did not stop: 'agent-vm stop %s', then 'agent-vm setup' again. The base from before is untouched.\n", building) |
| 524 | return 1 |
| 525 | } |
| 526 | if !e.swapBase(ctx, b) { |
| 527 | return 1 |
| 528 | } |
| 529 | |
| 530 | fmt.Fprint(e.io.Stdout, "\nBase VM ready. Try one of these in any project directory:\n agent-vm shell\n") |
| 531 | for _, a := range []string{"claude", "opencode", "codex", "vibe", "pi"} { |
| 532 | if in[a] { |
| 533 | fmt.Fprintf(e.io.Stdout, " agent-vm %s\n", a) |
| 534 | } |
| 535 | } |
| 536 | if in["code-server"] { |
| 537 | fmt.Fprintln(e.io.Stdout, " agent-vm code") |
| 538 | } |
| 539 | |
| 540 | if vms, err := e.agentVMs(ctx); err == nil && len(vms) > 1 { |
| 541 | fmt.Fprint(e.io.Stdout, "\nNote: Existing VMs were not updated. Use --reset to re-clone them from the new base.\n") |
| 542 | } |
| 543 | return 0 |
| 544 | } |
| 545 | |
| 546 | func cageInstallEnv() string { |
| 547 | if os.Getenv("CLAUDE_CAGE_CONFIG") != "" { |
| 548 | progress := "" |
| 549 | if os.Getenv("CAGE_SETUP_PROGRESS") == "1" { |
| 550 | progress = "export CAGE_SETUP_PROGRESS=1\n" |
| 551 | } |
| 552 | return "set -e\nexport CLAUDE_CAGE=1\n" + progress + cage.DebianPrepareScript + "\n" + cage.ToolInstallPrelude() |
| 553 | } |
| 554 | return "" |
| 555 | } |