blob: 722e3e652102b8d07f526577566aa52f631bb6c7 [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001// Package dockerimg
2package dockerimg
3
4import (
5 "bytes"
6 "context"
Philip Zeyliger5e227dd2025-04-21 15:55:29 -07007 "crypto/rand"
Earl Lee2e463fb2025-04-17 11:22:22 -07008 "crypto/sha256"
9 "encoding/hex"
10 "encoding/json"
11 "fmt"
12 "io"
13 "log/slog"
14 "net"
15 "net/http"
16 "os"
17 "os/exec"
18 "path/filepath"
19 "runtime"
20 "strings"
21 "time"
22
Josh Bleecher Snyder78707d62025-04-30 21:06:49 +000023 "sketch.dev/browser"
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070024 "sketch.dev/llm/ant"
Sean McCulloughbaa2b592025-04-23 10:40:08 -070025 "sketch.dev/loop/server"
Earl Lee2e463fb2025-04-17 11:22:22 -070026 "sketch.dev/skribe"
Philip Zeyliger5d6af872025-04-23 19:48:34 -070027 "sketch.dev/webui"
Earl Lee2e463fb2025-04-17 11:22:22 -070028)
29
30// ContainerConfig holds all configuration for launching a container
31type ContainerConfig struct {
32 // SessionID is the unique identifier for this session
33 SessionID string
34
35 // LocalAddr is the initial address to use (though it may be overwritten later)
36 LocalAddr string
37
38 // SkabandAddr is the address of the skaband service if available
39 SkabandAddr string
40
41 // AntURL is the URL of the LLM service.
42 AntURL string
43
44 // AntAPIKey is the API key for LLM service.
45 AntAPIKey string
46
47 // Path is the local filesystem path to use
48 Path string
49
50 // GitUsername is the username to use for git operations
51 GitUsername string
52
53 // GitEmail is the email to use for git operations
54 GitEmail string
55
56 // OpenBrowser determines whether to open a browser automatically
57 OpenBrowser bool
58
59 // NoCleanup prevents container cleanup when set to true
60 NoCleanup bool
61
62 // ForceRebuild forces rebuilding of the Docker image even if it exists
63 ForceRebuild bool
64
65 // Host directory to copy container logs into, if not set to ""
66 ContainerLogDest string
67
68 // Path to pre-built linux sketch binary, or build a new one if set to ""
69 SketchBinaryLinux string
70
71 // Sketch client public key.
72 SketchPubKey string
Philip Zeyligerd1402952025-04-23 03:54:37 +000073
Sean McCulloughbaa2b592025-04-23 10:40:08 -070074 // Host port for the container's ssh server
75 SSHPort int
76
Philip Zeyliger18532b22025-04-23 21:11:46 +000077 // Outside information to pass to the container
78 OutsideHostname string
79 OutsideOS string
80 OutsideWorkingDir string
Philip Zeyligerb74c4f62025-04-25 19:18:49 -070081
Pokey Rule0dcebe12025-04-28 14:51:04 +010082 // If true, exit after the first turn
83 OneShot bool
84
85 // Initial prompt
86 Prompt string
Philip Zeyliger1b47aa22025-04-28 19:25:38 +000087
88 // Initial commit to use as starting point
89 InitialCommit string
Earl Lee2e463fb2025-04-17 11:22:22 -070090}
91
92// LaunchContainer creates a docker container for a project, installs sketch and opens a connection to it.
93// It writes status to stdout.
94func LaunchContainer(ctx context.Context, stdout, stderr io.Writer, config ContainerConfig) error {
95 if _, err := exec.LookPath("docker"); err != nil {
Philip Zeyliger5e227dd2025-04-21 15:55:29 -070096 if runtime.GOOS == "darwin" {
97 return fmt.Errorf("cannot find `docker` binary; run: brew install docker colima && colima start")
98 } else {
99 return fmt.Errorf("cannot find `docker` binary; install docker (e.g., apt-get install docker.io)")
100 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700101 }
102
103 if out, err := combinedOutput(ctx, "docker", "ps"); err != nil {
104 // `docker ps` provides a good error message here that can be
105 // easily chatgpt'ed by users, so send it to the user as-is:
106 // Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
107 return fmt.Errorf("docker ps: %s (%w)", out, err)
108 }
109
110 _, hostPort, err := net.SplitHostPort(config.LocalAddr)
111 if err != nil {
112 return err
113 }
114
115 gitRoot, err := findGitRoot(ctx, config.Path)
116 if err != nil {
117 return err
118 }
119
120 imgName, err := findOrBuildDockerImage(ctx, stdout, stderr, config.Path, gitRoot, config.AntURL, config.AntAPIKey, config.ForceRebuild)
121 if err != nil {
122 return err
123 }
124
125 linuxSketchBin := config.SketchBinaryLinux
126 if linuxSketchBin == "" {
127 linuxSketchBin, err = buildLinuxSketchBin(ctx, config.Path)
128 if err != nil {
129 return err
130 }
Josh Bleecher Snyder5544d142025-04-23 14:15:45 -0700131 defer os.Remove(linuxSketchBin) // in case of errors
Earl Lee2e463fb2025-04-17 11:22:22 -0700132 }
133
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000134 cntrName := "sketch-" + config.SessionID
Earl Lee2e463fb2025-04-17 11:22:22 -0700135 defer func() {
136 if config.NoCleanup {
137 return
138 }
139 if out, err := combinedOutput(ctx, "docker", "kill", cntrName); err != nil {
140 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
141 _ = out
142 }
143 if out, err := combinedOutput(ctx, "docker", "rm", cntrName); err != nil {
144 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
145 _ = out
146 }
147 }()
148
149 // errCh receives errors from operations that this function calls in separate goroutines.
150 errCh := make(chan error)
151
152 // Start the git server
153 gitSrv, err := newGitServer(gitRoot)
154 if err != nil {
155 return fmt.Errorf("failed to start git server: %w", err)
156 }
157 defer gitSrv.shutdown(ctx)
158
159 go func() {
160 errCh <- gitSrv.serve(ctx)
161 }()
162
163 // Get the current host git commit
164 var commit string
Philip Zeyliger1b47aa22025-04-28 19:25:38 +0000165 if out, err := combinedOutput(ctx, "git", "rev-parse", config.InitialCommit); err != nil {
166 return fmt.Errorf("git rev-parse %s: %w", config.InitialCommit, err)
Earl Lee2e463fb2025-04-17 11:22:22 -0700167 } else {
168 commit = strings.TrimSpace(string(out))
169 }
170 if out, err := combinedOutput(ctx, "git", "config", "http.receivepack", "true"); err != nil {
171 return fmt.Errorf("git config http.receivepack true: %s: %w", out, err)
172 }
173
174 relPath, err := filepath.Rel(gitRoot, config.Path)
175 if err != nil {
176 return err
177 }
178
179 // Create the sketch container
180 if err := createDockerContainer(ctx, cntrName, hostPort, relPath, imgName, config); err != nil {
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000181 return fmt.Errorf("failed to create docker container: %w", err)
Earl Lee2e463fb2025-04-17 11:22:22 -0700182 }
183
184 // Copy the sketch linux binary into the container
185 if out, err := combinedOutput(ctx, "docker", "cp", linuxSketchBin, cntrName+":/bin/sketch"); err != nil {
186 return fmt.Errorf("docker cp: %s, %w", out, err)
187 }
Josh Bleecher Snyder5544d142025-04-23 14:15:45 -0700188 os.Remove(linuxSketchBin) // in normal operations, the code below blocks, so actively delete now
Sean McCulloughf5bb3d32025-04-18 10:47:59 -0700189
190 // Make sure that the webui is built so we can copy the results to the container.
191 _, err = webui.Build()
192 if err != nil {
193 return fmt.Errorf("failed to build webui: %w", err)
194 }
195
David Crawshaw8bff16a2025-04-18 01:16:49 -0700196 webuiZipPath, err := webui.ZipPath()
197 if err != nil {
198 return err
199 }
200 if out, err := combinedOutput(ctx, "docker", "cp", webuiZipPath, cntrName+":/root/.cache/sketch/webui/"+filepath.Base(webuiZipPath)); err != nil {
201 return fmt.Errorf("docker cp: %s, %w", out, err)
202 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700203
David Crawshaw53786ef2025-04-24 12:52:51 -0700204 fmt.Printf("📦 running in container %s\n", cntrName)
Earl Lee2e463fb2025-04-17 11:22:22 -0700205
206 // Start the sketch container
207 if out, err := combinedOutput(ctx, "docker", "start", cntrName); err != nil {
208 return fmt.Errorf("docker start: %s, %w", out, err)
209 }
210
211 // Copies structured logs from the container to the host.
212 copyLogs := func() {
213 if config.ContainerLogDest == "" {
214 return
215 }
216 out, err := combinedOutput(ctx, "docker", "logs", cntrName)
217 if err != nil {
218 fmt.Fprintf(os.Stderr, "docker logs failed: %v\n", err)
219 return
220 }
Josh Bleecher Snyder7660e4e2025-04-24 10:34:17 -0700221 prefix := []byte("structured logs:")
222 for line := range bytes.Lines(out) {
223 rest, ok := bytes.CutPrefix(line, prefix)
224 if !ok {
Earl Lee2e463fb2025-04-17 11:22:22 -0700225 continue
226 }
Josh Bleecher Snyder7660e4e2025-04-24 10:34:17 -0700227 logFile := string(bytes.TrimSpace(rest))
Earl Lee2e463fb2025-04-17 11:22:22 -0700228 srcPath := fmt.Sprintf("%s:%s", cntrName, logFile)
229 logFileName := filepath.Base(logFile)
230 dstPath := filepath.Join(config.ContainerLogDest, logFileName)
231 _, err := combinedOutput(ctx, "docker", "cp", srcPath, dstPath)
232 if err != nil {
233 fmt.Fprintf(os.Stderr, "docker cp %s %s failed: %v\n", srcPath, dstPath, err)
234 }
235 fmt.Fprintf(os.Stderr, "\ncopied container log %s to %s\n", srcPath, dstPath)
236 }
237 }
238
239 // NOTE: we want to see what the internal sketch binary prints
240 // regardless of the setting of the verbosity flag on the external
241 // binary, so reading "docker logs", which is the stdout/stderr of
242 // the internal binary is not conditional on the verbose flag.
243 appendInternalErr := func(err error) error {
244 if err == nil {
245 return nil
246 }
247 out, logsErr := combinedOutput(ctx, "docker", "logs", cntrName)
Philip Zeyligerd1402952025-04-23 03:54:37 +0000248 if logsErr != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700249 return fmt.Errorf("%w; and docker logs failed: %s, %v", err, out, logsErr)
250 }
251 out = bytes.TrimSpace(out)
252 if len(out) > 0 {
253 return fmt.Errorf("docker logs: %s;\n%w", out, err)
254 }
255 return err
256 }
257
258 // Get the sketch server port from the container
Sean McCulloughae3480f2025-04-23 15:28:20 -0700259 localAddr, err := getContainerPort(ctx, cntrName, "80")
Earl Lee2e463fb2025-04-17 11:22:22 -0700260 if err != nil {
261 return appendInternalErr(err)
262 }
263
Sean McCulloughae3480f2025-04-23 15:28:20 -0700264 localSSHAddr, err := getContainerPort(ctx, cntrName, "22")
265 if err != nil {
266 return appendInternalErr(err)
267 }
268 sshHost, sshPort, err := net.SplitHostPort(localSSHAddr)
269 if err != nil {
Sean McCullough4854c652025-04-24 18:37:02 -0700270 return appendInternalErr(fmt.Errorf("Error splitting ssh host and port: %w", err))
Sean McCulloughae3480f2025-04-23 15:28:20 -0700271 }
Sean McCullough4854c652025-04-24 18:37:02 -0700272
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700273 var sshServerIdentity, sshUserIdentity []byte
Sean McCullough4854c652025-04-24 18:37:02 -0700274
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700275 if err := CheckForInclude(); err != nil {
276 fmt.Println(err.Error())
277 // continue - ssh config is not required for the rest of sketch to function locally.
278 } else {
279 cst, err := NewSSHTheather(cntrName, sshHost, sshPort)
280 if err != nil {
281 return appendInternalErr(fmt.Errorf("NewContainerSSHTheather: %w", err))
282 }
283
Sean McCulloughea3fc202025-04-28 12:53:37 -0700284 // Note: The vscode: link uses an undocumented request parameter that I really had to dig to find:
285 // https://github.com/microsoft/vscode/blob/2b9486161abaca59b5132ce3c59544f3cc7000f6/src/vs/code/electron-main/app.ts#L878
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700286 fmt.Printf(`Connect to this container via any of these methods:
Sean McCullough4854c652025-04-24 18:37:02 -0700287🖥️ ssh %s
288🖥️ code --remote ssh-remote+root@%s /app -n
Sean McCulloughea3fc202025-04-28 12:53:37 -0700289🔗 vscode://vscode-remote/ssh-remote+root@%s/app?windowId=_blank
Sean McCullough4854c652025-04-24 18:37:02 -0700290`, cntrName, cntrName, cntrName)
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700291 sshUserIdentity = cst.userIdentity
292 sshServerIdentity = cst.serverIdentity
293 defer func() {
294 if err := cst.Cleanup(); err != nil {
295 appendInternalErr(err)
296 }
297 }()
298 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700299
Earl Lee2e463fb2025-04-17 11:22:22 -0700300 // Tell the sketch container which git server port and commit to initialize with.
301 go func() {
302 // TODO: Why is this called in a goroutine? I have found that when I pull this out
303 // of the goroutine and call it inline, then the terminal UI clears itself and all
304 // the scrollback (which is not good, but also not fatal). I can't see why it does this
305 // though, since none of the calls in postContainerInitConfig obviously write to stdout
306 // or stderr.
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700307 if err := postContainerInitConfig(ctx, localAddr, commit, gitSrv.gitPort, gitSrv.pass, sshServerIdentity, sshUserIdentity); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700308 slog.ErrorContext(ctx, "LaunchContainer.postContainerInitConfig", slog.String("err", err.Error()))
309 errCh <- appendInternalErr(err)
310 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700311
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700312 // We open the browser after the init config because the above waits for the web server to be serving.
313 if config.OpenBrowser {
314 if config.SkabandAddr != "" {
Josh Bleecher Snydere54b00a2025-04-30 16:48:02 -0700315 browser.Open(fmt.Sprintf("%s/s/%s", config.SkabandAddr, config.SessionID))
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700316 } else {
Josh Bleecher Snydere54b00a2025-04-30 16:48:02 -0700317 browser.Open("http://" + localAddr)
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700318 }
319 }
320 }()
Earl Lee2e463fb2025-04-17 11:22:22 -0700321
322 go func() {
323 cmd := exec.CommandContext(ctx, "docker", "attach", cntrName)
324 cmd.Stdin = os.Stdin
325 cmd.Stdout = os.Stdout
326 cmd.Stderr = os.Stderr
327 errCh <- run(ctx, "docker attach", cmd)
328 }()
329
330 defer copyLogs()
331
332 for {
333 select {
334 case <-ctx.Done():
335 return ctx.Err()
336 case err := <-errCh:
337 if err != nil {
338 return appendInternalErr(fmt.Errorf("container process: %w", err))
339 }
340 return nil
341 }
342 }
343}
344
345func combinedOutput(ctx context.Context, cmdName string, args ...string) ([]byte, error) {
346 cmd := exec.CommandContext(ctx, cmdName, args...)
347 // Really only needed for the "go build" command for the linux sketch binary
348 cmd.Env = append(os.Environ(), "GOOS=linux", "CGO_ENABLED=0")
349 start := time.Now()
350
351 out, err := cmd.CombinedOutput()
352 if err != nil {
353 slog.ErrorContext(ctx, cmdName, slog.Duration("elapsed", time.Now().Sub(start)), slog.String("err", err.Error()), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
354 } else {
355 slog.DebugContext(ctx, cmdName, slog.Duration("elapsed", time.Now().Sub(start)), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
356 }
357 return out, err
358}
359
360func run(ctx context.Context, cmdName string, cmd *exec.Cmd) error {
361 start := time.Now()
362 err := cmd.Run()
363 if err != nil {
364 slog.ErrorContext(ctx, cmdName, slog.Duration("elapsed", time.Now().Sub(start)), slog.String("err", err.Error()), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
365 } else {
366 slog.DebugContext(ctx, cmdName, slog.Duration("elapsed", time.Now().Sub(start)), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
367 }
368 return err
369}
370
371type gitServer struct {
372 gitLn net.Listener
373 gitPort string
374 srv *http.Server
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700375 pass string
Earl Lee2e463fb2025-04-17 11:22:22 -0700376}
377
378func (gs *gitServer) shutdown(ctx context.Context) {
379 gs.srv.Shutdown(ctx)
380 gs.gitLn.Close()
381}
382
383// Serve a git remote from the host for the container to fetch from and push to.
384func (gs *gitServer) serve(ctx context.Context) error {
385 slog.DebugContext(ctx, "starting git server", slog.String("git_remote_addr", "http://host.docker.internal:"+gs.gitPort+"/.git"))
386 return gs.srv.Serve(gs.gitLn)
387}
388
389func newGitServer(gitRoot string) (*gitServer, error) {
Josh Bleecher Snyder9f6a9982025-04-22 17:34:15 -0700390 ret := &gitServer{
391 pass: rand.Text(),
392 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700393
Earl Lee2e463fb2025-04-17 11:22:22 -0700394 gitLn, err := net.Listen("tcp4", ":0")
395 if err != nil {
396 return nil, fmt.Errorf("git listen: %w", err)
397 }
398 ret.gitLn = gitLn
399
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000400 browserC := make(chan string, 1) // channel of URLs to open in browser
401 go func() {
402 for url := range browserC {
403 browser.Open(url)
404 }
405 }()
406
407 srv := http.Server{Handler: &gitHTTP{gitRepoRoot: gitRoot, pass: []byte(ret.pass), browserC: browserC}}
Earl Lee2e463fb2025-04-17 11:22:22 -0700408 ret.srv = &srv
409
410 _, gitPort, err := net.SplitHostPort(gitLn.Addr().String())
411 if err != nil {
412 return nil, fmt.Errorf("git port: %w", err)
413 }
414 ret.gitPort = gitPort
415 return ret, nil
416}
417
418func createDockerContainer(ctx context.Context, cntrName, hostPort, relPath, imgName string, config ContainerConfig) error {
David Crawshaw69c67312025-04-17 13:42:00 -0700419 cmdArgs := []string{
420 "create",
Earl Lee2e463fb2025-04-17 11:22:22 -0700421 "-it",
422 "--name", cntrName,
423 "-p", hostPort + ":80", // forward container port 80 to a host port
Philip Zeyliger6234a8d2025-05-02 14:31:20 -0700424 "-e", "SKETCH_ANTHROPIC_API_KEY=" + config.AntAPIKey,
Earl Lee2e463fb2025-04-17 11:22:22 -0700425 }
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000426
427 for _, envVar := range getEnvForwardingFromGitConfig(ctx) {
428 cmdArgs = append(cmdArgs, "-e", envVar)
429 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700430 if config.AntURL != "" {
Philip Zeyliger6234a8d2025-05-02 14:31:20 -0700431 cmdArgs = append(cmdArgs, "-e", "SKETCH_ANT_URL="+config.AntURL)
Earl Lee2e463fb2025-04-17 11:22:22 -0700432 }
433 if config.SketchPubKey != "" {
434 cmdArgs = append(cmdArgs, "-e", "SKETCH_PUB_KEY="+config.SketchPubKey)
435 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700436 if config.SSHPort > 0 {
437 cmdArgs = append(cmdArgs, "-p", fmt.Sprintf("%d:22", config.SSHPort)) // forward container ssh port to host ssh port
438 } else {
439 cmdArgs = append(cmdArgs, "-p", "22") // use an ephemeral host port for ssh.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700440 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700441 if relPath != "." {
442 cmdArgs = append(cmdArgs, "-w", "/app/"+relPath)
443 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700444 // colima does this by default, but Linux docker seems to need this set explicitly
445 cmdArgs = append(cmdArgs, "--add-host", "host.docker.internal:host-gateway")
Earl Lee2e463fb2025-04-17 11:22:22 -0700446 cmdArgs = append(
447 cmdArgs,
448 imgName,
449 "/bin/sketch",
450 "-unsafe",
451 "-addr=:80",
452 "-session-id="+config.SessionID,
Philip Zeyligerd1402952025-04-23 03:54:37 +0000453 "-git-username="+config.GitUsername,
454 "-git-email="+config.GitEmail,
Philip Zeyliger18532b22025-04-23 21:11:46 +0000455 "-outside-hostname="+config.OutsideHostname,
456 "-outside-os="+config.OutsideOS,
457 "-outside-working-dir="+config.OutsideWorkingDir,
Josh Bleecher Snyder3cae7d92025-04-30 09:54:29 -0700458 "-open=false",
Earl Lee2e463fb2025-04-17 11:22:22 -0700459 )
460 if config.SkabandAddr != "" {
461 cmdArgs = append(cmdArgs, "-skaband-addr="+config.SkabandAddr)
462 }
Pokey Rule0dcebe12025-04-28 14:51:04 +0100463 if config.Prompt != "" {
464 cmdArgs = append(cmdArgs, "-prompt", config.Prompt)
465 }
466 if config.OneShot {
467 cmdArgs = append(cmdArgs, "-one-shot")
Philip Zeyligerb74c4f62025-04-25 19:18:49 -0700468 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700469 if out, err := combinedOutput(ctx, "docker", cmdArgs...); err != nil {
470 return fmt.Errorf("docker create: %s, %w", out, err)
471 }
472 return nil
473}
474
475func buildLinuxSketchBin(ctx context.Context, path string) (string, error) {
David Crawshaw8a617cb2025-04-18 01:28:43 -0700476 homeDir, err := os.UserHomeDir()
David Crawshaw69c67312025-04-17 13:42:00 -0700477 if err != nil {
478 return "", err
479 }
David Crawshaw8a617cb2025-04-18 01:28:43 -0700480 linuxGopath := filepath.Join(homeDir, ".cache", "sketch", "linuxgo")
481 if err := os.MkdirAll(linuxGopath, 0o777); err != nil {
482 return "", err
483 }
484
485 verToInstall := "@latest"
486 if out, err := exec.Command("go", "list", "-m").CombinedOutput(); err != nil {
487 return "", fmt.Errorf("failed to run go list -m: %s: %v", out, err)
488 } else {
489 if strings.TrimSpace(string(out)) == "sketch.dev" {
David Crawshaw094e4d22025-04-24 11:35:14 -0700490 slog.DebugContext(ctx, "built linux agent from currently checked out module")
David Crawshaw8a617cb2025-04-18 01:28:43 -0700491 verToInstall = ""
492 }
493 }
David Crawshaw69c67312025-04-17 13:42:00 -0700494
Earl Lee2e463fb2025-04-17 11:22:22 -0700495 start := time.Now()
David Crawshaw8a617cb2025-04-18 01:28:43 -0700496 cmd := exec.CommandContext(ctx, "go", "install", "sketch.dev/cmd/sketch"+verToInstall)
David Crawshawb9eaef52025-04-17 15:23:18 -0700497 cmd.Env = append(
498 os.Environ(),
499 "GOOS=linux",
500 "CGO_ENABLED=0",
501 "GOTOOLCHAIN=auto",
David Crawshaw8a617cb2025-04-18 01:28:43 -0700502 "GOPATH="+linuxGopath,
Josh Bleecher Snyderfae17572025-04-21 11:48:05 -0700503 "GOBIN=",
David Crawshawb9eaef52025-04-17 15:23:18 -0700504 )
Earl Lee2e463fb2025-04-17 11:22:22 -0700505
Earl Lee2e463fb2025-04-17 11:22:22 -0700506 out, err := cmd.CombinedOutput()
507 if err != nil {
508 slog.ErrorContext(ctx, "go", slog.Duration("elapsed", time.Now().Sub(start)), slog.String("err", err.Error()), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
509 return "", fmt.Errorf("failed to build linux sketch binary: %s: %w", out, err)
510 } else {
511 slog.DebugContext(ctx, "go", slog.Duration("elapsed", time.Now().Sub(start)), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
512 }
513
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700514 var src string
515 if runtime.GOOS != "linux" {
516 src = filepath.Join(linuxGopath, "bin", "linux_"+runtime.GOARCH, "sketch")
517 } else {
518 // If we are already on Linux, there's no extra platform name in the path
519 src = filepath.Join(linuxGopath, "bin", "sketch")
520 }
521
David Crawshaw69c67312025-04-17 13:42:00 -0700522 dst := filepath.Join(path, "tmp-sketch-binary-linux")
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700523 if err := moveFile(src, dst); err != nil {
David Crawshaw69c67312025-04-17 13:42:00 -0700524 return "", err
525 }
526
David Crawshaw69c67312025-04-17 13:42:00 -0700527 return dst, nil
Earl Lee2e463fb2025-04-17 11:22:22 -0700528}
529
Sean McCulloughae3480f2025-04-23 15:28:20 -0700530func getContainerPort(ctx context.Context, cntrName, cntrPort string) (string, error) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700531 localAddr := ""
Sean McCulloughae3480f2025-04-23 15:28:20 -0700532 if out, err := combinedOutput(ctx, "docker", "port", cntrName, cntrPort); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700533 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
534 } else {
535 v4, _, found := strings.Cut(string(out), "\n")
536 if !found {
537 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
538 }
539 localAddr = v4
540 if strings.HasPrefix(localAddr, "0.0.0.0") {
541 localAddr = "127.0.0.1" + strings.TrimPrefix(localAddr, "0.0.0.0")
542 }
543 }
544 return localAddr, nil
545}
546
547// Contact the container and configure it.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700548func postContainerInitConfig(ctx context.Context, localAddr, commit, gitPort, gitPass string, sshServerIdentity, sshAuthorizedKeys []byte) error {
Earl Lee2e463fb2025-04-17 11:22:22 -0700549 localURL := "http://" + localAddr
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700550
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000551 // Check if SSH is available by checking for the Include directive in ~/.ssh/config
552 sshAvailable := true
553 sshError := ""
554 if err := CheckForInclude(); err != nil {
555 sshAvailable = false
556 sshError = err.Error()
557 }
558
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700559 initMsg, err := json.Marshal(
560 server.InitRequest{
561 Commit: commit,
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000562 OutsideHTTP: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s", gitPass, gitPort),
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700563 GitRemoteAddr: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s/.git", gitPass, gitPort),
564 HostAddr: localAddr,
565 SSHAuthorizedKeys: sshAuthorizedKeys,
566 SSHServerIdentity: sshServerIdentity,
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000567 SSHAvailable: sshAvailable,
568 SSHError: sshError,
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700569 })
Earl Lee2e463fb2025-04-17 11:22:22 -0700570 if err != nil {
571 return fmt.Errorf("init msg: %w", err)
572 }
573
Earl Lee2e463fb2025-04-17 11:22:22 -0700574 // Note: this /init POST is handled in loop/server/loophttp.go:
575 initMsgByteReader := bytes.NewReader(initMsg)
576 req, err := http.NewRequest("POST", localURL+"/init", initMsgByteReader)
577 if err != nil {
578 return err
579 }
580
581 var res *http.Response
582 for i := 0; ; i++ {
583 time.Sleep(100 * time.Millisecond)
584 // If you DON'T reset this byteReader, then subsequent retries may end up sending 0 bytes.
585 initMsgByteReader.Reset(initMsg)
586 res, err = http.DefaultClient.Do(req)
587 if err != nil {
588 // In addition to "connection refused", we also occasionally see "EOF" errors that can succeed on retries.
589 if i < 100 && (strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "EOF")) {
590 slog.DebugContext(ctx, "postContainerInitConfig retrying", slog.Int("retry", i), slog.String("err", err.Error()))
591 continue
592 }
593 return fmt.Errorf("failed to %s/init sketch in container, NOT retrying: err: %v", localURL, err)
594 }
595 break
596 }
597 resBytes, _ := io.ReadAll(res.Body)
598 if res.StatusCode != http.StatusOK {
599 return fmt.Errorf("failed to initialize sketch in container, response status code %d: %s", res.StatusCode, resBytes)
600 }
601 return nil
602}
603
604func findOrBuildDockerImage(ctx context.Context, stdout, stderr io.Writer, cwd, gitRoot, antURL, antAPIKey string, forceRebuild bool) (imgName string, err error) {
605 h := sha256.Sum256([]byte(gitRoot))
606 imgName = "sketch-" + hex.EncodeToString(h[:6])
607
608 var curImgInitFilesHash string
609 if out, err := combinedOutput(ctx, "docker", "inspect", "--format", "{{json .Config.Labels}}", imgName); err != nil {
610 if strings.Contains(string(out), "No such object") {
611 // Image does not exist, continue and build it.
612 curImgInitFilesHash = ""
613 } else {
614 return "", fmt.Errorf("docker inspect failed: %s, %v", out, err)
615 }
616 } else {
617 m := map[string]string{}
618 if err := json.Unmarshal(bytes.TrimSpace(out), &m); err != nil {
619 return "", fmt.Errorf("docker inspect output unparsable: %s, %v", out, err)
620 }
621 curImgInitFilesHash = m["sketch_context"]
622 }
623
624 candidates, err := findRepoDockerfiles(cwd, gitRoot)
625 if err != nil {
626 return "", fmt.Errorf("find dockerfile: %w", err)
627 }
628
629 var initFiles map[string]string
630 var dockerfilePath string
631
632 // TODO: prefer a "Dockerfile.sketch" so users can tailor any env to this tool.
633 if len(candidates) == 1 && strings.ToLower(filepath.Base(candidates[0])) == "dockerfile" {
634 dockerfilePath = candidates[0]
635 contents, err := os.ReadFile(dockerfilePath)
636 if err != nil {
637 return "", err
638 }
639 fmt.Printf("using %s as dev env\n", candidates[0])
640 if hashInitFiles(map[string]string{dockerfilePath: string(contents)}) == curImgInitFilesHash && !forceRebuild {
Earl Lee2e463fb2025-04-17 11:22:22 -0700641 return imgName, nil
642 }
643 } else {
644 initFiles, err = readInitFiles(os.DirFS(gitRoot))
645 if err != nil {
646 return "", err
647 }
648 subPathWorkingDir, err := filepath.Rel(gitRoot, cwd)
649 if err != nil {
650 return "", err
651 }
652 initFileHash := hashInitFiles(initFiles)
653 if curImgInitFilesHash == initFileHash && !forceRebuild {
Earl Lee2e463fb2025-04-17 11:22:22 -0700654 return imgName, nil
655 }
656
657 start := time.Now()
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700658 srv := &ant.Service{
659 URL: antURL,
660 APIKey: antAPIKey,
661 HTTPC: http.DefaultClient,
662 }
663 dockerfile, err := createDockerfile(ctx, srv, initFiles, subPathWorkingDir)
Earl Lee2e463fb2025-04-17 11:22:22 -0700664 if err != nil {
665 return "", fmt.Errorf("create dockerfile: %w", err)
666 }
667 dockerfilePath = filepath.Join(cwd, "tmp-sketch-dockerfile")
668 if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0o666); err != nil {
669 return "", err
670 }
671 defer os.Remove(dockerfilePath)
672
673 fmt.Fprintf(stderr, "generated Dockerfile in %s:\n\t%s\n\n", time.Since(start).Round(time.Millisecond), strings.Replace(dockerfile, "\n", "\n\t", -1))
674 }
675
676 var gitUserEmail, gitUserName string
677 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.email"); err != nil {
678 return "", fmt.Errorf("git config: %s: %v", out, err)
679 } else {
680 gitUserEmail = strings.TrimSpace(string(out))
681 }
682 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.name"); err != nil {
683 return "", fmt.Errorf("git config: %s: %v", out, err)
684 } else {
685 gitUserName = strings.TrimSpace(string(out))
686 }
687
688 start := time.Now()
689 cmd := exec.CommandContext(ctx,
690 "docker", "build",
691 "-t", imgName,
692 "-f", dockerfilePath,
693 "--build-arg", "GIT_USER_EMAIL="+gitUserEmail,
694 "--build-arg", "GIT_USER_NAME="+gitUserName,
695 ".",
696 )
697 cmd.Dir = gitRoot
698 cmd.Stdout = stdout
699 cmd.Stderr = stderr
Josh Bleecher Snyderdf2d3dc2025-04-25 12:31:35 -0700700 fmt.Printf("🏗️ building docker image %s... (use -verbose to see build output)\n", imgName)
Philip Zeyligere4fa0e32025-04-23 14:15:55 -0700701 dockerfileContent, err := os.ReadFile(dockerfilePath)
702 if err != nil {
703 return "", fmt.Errorf("failed to read Dockerfile: %w", err)
704 }
David Crawshaw5228b582025-05-01 11:18:12 -0700705 // TODO: this is sometimes a repeat of earlier. Remove the earlier call?
Philip Zeyliger5d6af872025-04-23 19:48:34 -0700706 fmt.Fprintf(stdout, "Dockerfile:\n%s\n", string(dockerfileContent))
Earl Lee2e463fb2025-04-17 11:22:22 -0700707
708 err = run(ctx, "docker build", cmd)
709 if err != nil {
710 return "", fmt.Errorf("docker build failed: %v", err)
711 }
712 fmt.Printf("built docker image %s in %s\n", imgName, time.Since(start).Round(time.Millisecond))
713 return imgName, nil
714}
715
716func findRepoDockerfiles(cwd, gitRoot string) ([]string, error) {
717 files, err := findDirDockerfiles(cwd)
718 if err != nil {
719 return nil, err
720 }
721 if len(files) > 0 {
722 return files, nil
723 }
724
725 path := cwd
726 for path != gitRoot {
727 path = filepath.Dir(path)
728 files, err := findDirDockerfiles(path)
729 if err != nil {
730 return nil, err
731 }
732 if len(files) > 0 {
733 return files, nil
734 }
735 }
736 return files, nil
737}
738
739// findDirDockerfiles finds all "Dockerfile*" files in a directory.
740func findDirDockerfiles(root string) (res []string, err error) {
741 err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
742 if err != nil {
743 return err
744 }
745 if info.IsDir() && root != path {
746 return filepath.SkipDir
747 }
748 name := strings.ToLower(info.Name())
749 if name == "dockerfile" || strings.HasPrefix(name, "dockerfile.") {
750 res = append(res, path)
751 }
752 return nil
753 })
754 if err != nil {
755 return nil, err
756 }
757 return res, nil
758}
759
760func findGitRoot(ctx context.Context, path string) (string, error) {
761 cmd := exec.CommandContext(ctx, "git", "rev-parse", "--git-common-dir")
762 cmd.Dir = path
763 out, err := cmd.CombinedOutput()
764 if err != nil {
765 if strings.Contains(string(out), "not a git repository") {
766 return "", fmt.Errorf(`sketch needs to run from within a git repo, but %s is not part of a git repo.
767Consider one of the following options:
768 - cd to a different dir that is already part of a git repo first, or
769 - to create a new git repo from this directory (%s), run this command:
770
771 git init . && git commit --allow-empty -m "initial commit"
772
773and try running sketch again.
774`, path, path)
775 }
776 return "", fmt.Errorf("git rev-parse --git-common-dir: %s: %w", out, err)
777 }
778 gitDir := strings.TrimSpace(string(out)) // location of .git dir, often as a relative path
779 absGitDir := filepath.Join(path, gitDir)
780 return filepath.Dir(absGitDir), err
781}
782
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700783// moveFile is like Python's shutil.move, in that it tries a rename, and, if that fails,
784// copies and deletes
785func moveFile(src, dst string) error {
786 if err := os.Rename(src, dst); err == nil {
787 return nil
788 }
789
790 stat, err := os.Stat(src)
791 if err != nil {
792 return err
793 }
794
795 sourceFile, err := os.Open(src)
796 if err != nil {
797 return err
798 }
799 defer sourceFile.Close()
800
801 destFile, err := os.Create(dst)
802 if err != nil {
803 return err
804 }
805 defer destFile.Close()
806
807 _, err = io.Copy(destFile, sourceFile)
808 if err != nil {
809 return err
810 }
811
812 sourceFile.Close()
813 destFile.Close()
814
815 os.Chmod(dst, stat.Mode())
816
817 return os.Remove(src)
818}
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000819
820// getEnvForwardingFromGitConfig retrieves environment variables to pass through to Docker
821// from git config using the sketch.envfwd multi-valued key.
822func getEnvForwardingFromGitConfig(ctx context.Context) []string {
823 outb, err := exec.CommandContext(ctx, "git", "config", "--get-all", "sketch.envfwd").CombinedOutput()
824 out := string(outb)
825 if err != nil {
826 if strings.Contains(out, "key does not exist") {
827 return nil
828 }
829 slog.ErrorContext(ctx, "failed to get sketch.envfwd from git config", "err", err, "output", out)
830 return nil
831 }
832
833 var envVars []string
834 for envVar := range strings.Lines(out) {
835 envVar = strings.TrimSpace(envVar)
836 if envVar == "" {
837 continue
838 }
839 envVars = append(envVars, envVar+"="+os.Getenv(envVar))
840 }
841 return envVars
842}