blob: 9ca188ba83685c0a9eb5a7ba0a95bad15410ed38 [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
Sean McCulloughbaa2b592025-04-23 10:40:08 -070023 "sketch.dev/loop/server"
Earl Lee2e463fb2025-04-17 11:22:22 -070024 "sketch.dev/skribe"
Philip Zeyliger5d6af872025-04-23 19:48:34 -070025 "sketch.dev/webui"
Earl Lee2e463fb2025-04-17 11:22:22 -070026)
27
28// ContainerConfig holds all configuration for launching a container
29type ContainerConfig struct {
30 // SessionID is the unique identifier for this session
31 SessionID string
32
33 // LocalAddr is the initial address to use (though it may be overwritten later)
34 LocalAddr string
35
36 // SkabandAddr is the address of the skaband service if available
37 SkabandAddr string
38
39 // AntURL is the URL of the LLM service.
40 AntURL string
41
42 // AntAPIKey is the API key for LLM service.
43 AntAPIKey string
44
45 // Path is the local filesystem path to use
46 Path string
47
48 // GitUsername is the username to use for git operations
49 GitUsername string
50
51 // GitEmail is the email to use for git operations
52 GitEmail string
53
54 // OpenBrowser determines whether to open a browser automatically
55 OpenBrowser bool
56
57 // NoCleanup prevents container cleanup when set to true
58 NoCleanup bool
59
60 // ForceRebuild forces rebuilding of the Docker image even if it exists
61 ForceRebuild bool
62
63 // Host directory to copy container logs into, if not set to ""
64 ContainerLogDest string
65
66 // Path to pre-built linux sketch binary, or build a new one if set to ""
67 SketchBinaryLinux string
68
69 // Sketch client public key.
70 SketchPubKey string
Philip Zeyligerd1402952025-04-23 03:54:37 +000071
Sean McCulloughbaa2b592025-04-23 10:40:08 -070072 // Host port for the container's ssh server
73 SSHPort int
74
Philip Zeyliger18532b22025-04-23 21:11:46 +000075 // Outside information to pass to the container
76 OutsideHostname string
77 OutsideOS string
78 OutsideWorkingDir string
Philip Zeyligerb74c4f62025-04-25 19:18:49 -070079
Pokey Rule0dcebe12025-04-28 14:51:04 +010080 // If true, exit after the first turn
81 OneShot bool
82
83 // Initial prompt
84 Prompt string
Philip Zeyliger1b47aa22025-04-28 19:25:38 +000085
86 // Initial commit to use as starting point
87 InitialCommit string
Earl Lee2e463fb2025-04-17 11:22:22 -070088}
89
90// LaunchContainer creates a docker container for a project, installs sketch and opens a connection to it.
91// It writes status to stdout.
92func LaunchContainer(ctx context.Context, stdout, stderr io.Writer, config ContainerConfig) error {
93 if _, err := exec.LookPath("docker"); err != nil {
Philip Zeyliger5e227dd2025-04-21 15:55:29 -070094 if runtime.GOOS == "darwin" {
95 return fmt.Errorf("cannot find `docker` binary; run: brew install docker colima && colima start")
96 } else {
97 return fmt.Errorf("cannot find `docker` binary; install docker (e.g., apt-get install docker.io)")
98 }
Earl Lee2e463fb2025-04-17 11:22:22 -070099 }
100
101 if out, err := combinedOutput(ctx, "docker", "ps"); err != nil {
102 // `docker ps` provides a good error message here that can be
103 // easily chatgpt'ed by users, so send it to the user as-is:
104 // Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
105 return fmt.Errorf("docker ps: %s (%w)", out, err)
106 }
107
108 _, hostPort, err := net.SplitHostPort(config.LocalAddr)
109 if err != nil {
110 return err
111 }
112
113 gitRoot, err := findGitRoot(ctx, config.Path)
114 if err != nil {
115 return err
116 }
117
118 imgName, err := findOrBuildDockerImage(ctx, stdout, stderr, config.Path, gitRoot, config.AntURL, config.AntAPIKey, config.ForceRebuild)
119 if err != nil {
120 return err
121 }
122
123 linuxSketchBin := config.SketchBinaryLinux
124 if linuxSketchBin == "" {
125 linuxSketchBin, err = buildLinuxSketchBin(ctx, config.Path)
126 if err != nil {
127 return err
128 }
Josh Bleecher Snyder5544d142025-04-23 14:15:45 -0700129 defer os.Remove(linuxSketchBin) // in case of errors
Earl Lee2e463fb2025-04-17 11:22:22 -0700130 }
131
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000132 cntrName := "sketch-" + config.SessionID
Earl Lee2e463fb2025-04-17 11:22:22 -0700133 defer func() {
134 if config.NoCleanup {
135 return
136 }
137 if out, err := combinedOutput(ctx, "docker", "kill", cntrName); err != nil {
138 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
139 _ = out
140 }
141 if out, err := combinedOutput(ctx, "docker", "rm", cntrName); err != nil {
142 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
143 _ = out
144 }
145 }()
146
147 // errCh receives errors from operations that this function calls in separate goroutines.
148 errCh := make(chan error)
149
150 // Start the git server
151 gitSrv, err := newGitServer(gitRoot)
152 if err != nil {
153 return fmt.Errorf("failed to start git server: %w", err)
154 }
155 defer gitSrv.shutdown(ctx)
156
157 go func() {
158 errCh <- gitSrv.serve(ctx)
159 }()
160
161 // Get the current host git commit
162 var commit string
Philip Zeyliger1b47aa22025-04-28 19:25:38 +0000163 if out, err := combinedOutput(ctx, "git", "rev-parse", config.InitialCommit); err != nil {
164 return fmt.Errorf("git rev-parse %s: %w", config.InitialCommit, err)
Earl Lee2e463fb2025-04-17 11:22:22 -0700165 } else {
166 commit = strings.TrimSpace(string(out))
167 }
168 if out, err := combinedOutput(ctx, "git", "config", "http.receivepack", "true"); err != nil {
169 return fmt.Errorf("git config http.receivepack true: %s: %w", out, err)
170 }
171
172 relPath, err := filepath.Rel(gitRoot, config.Path)
173 if err != nil {
174 return err
175 }
176
177 // Create the sketch container
178 if err := createDockerContainer(ctx, cntrName, hostPort, relPath, imgName, config); err != nil {
179 return err
180 }
181
182 // Copy the sketch linux binary into the container
183 if out, err := combinedOutput(ctx, "docker", "cp", linuxSketchBin, cntrName+":/bin/sketch"); err != nil {
184 return fmt.Errorf("docker cp: %s, %w", out, err)
185 }
Josh Bleecher Snyder5544d142025-04-23 14:15:45 -0700186 os.Remove(linuxSketchBin) // in normal operations, the code below blocks, so actively delete now
Sean McCulloughf5bb3d32025-04-18 10:47:59 -0700187
188 // Make sure that the webui is built so we can copy the results to the container.
189 _, err = webui.Build()
190 if err != nil {
191 return fmt.Errorf("failed to build webui: %w", err)
192 }
193
David Crawshaw8bff16a2025-04-18 01:16:49 -0700194 webuiZipPath, err := webui.ZipPath()
195 if err != nil {
196 return err
197 }
198 if out, err := combinedOutput(ctx, "docker", "cp", webuiZipPath, cntrName+":/root/.cache/sketch/webui/"+filepath.Base(webuiZipPath)); err != nil {
199 return fmt.Errorf("docker cp: %s, %w", out, err)
200 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700201
David Crawshaw53786ef2025-04-24 12:52:51 -0700202 fmt.Printf("📦 running in container %s\n", cntrName)
Earl Lee2e463fb2025-04-17 11:22:22 -0700203
204 // Start the sketch container
205 if out, err := combinedOutput(ctx, "docker", "start", cntrName); err != nil {
206 return fmt.Errorf("docker start: %s, %w", out, err)
207 }
208
209 // Copies structured logs from the container to the host.
210 copyLogs := func() {
211 if config.ContainerLogDest == "" {
212 return
213 }
214 out, err := combinedOutput(ctx, "docker", "logs", cntrName)
215 if err != nil {
216 fmt.Fprintf(os.Stderr, "docker logs failed: %v\n", err)
217 return
218 }
Josh Bleecher Snyder7660e4e2025-04-24 10:34:17 -0700219 prefix := []byte("structured logs:")
220 for line := range bytes.Lines(out) {
221 rest, ok := bytes.CutPrefix(line, prefix)
222 if !ok {
Earl Lee2e463fb2025-04-17 11:22:22 -0700223 continue
224 }
Josh Bleecher Snyder7660e4e2025-04-24 10:34:17 -0700225 logFile := string(bytes.TrimSpace(rest))
Earl Lee2e463fb2025-04-17 11:22:22 -0700226 srcPath := fmt.Sprintf("%s:%s", cntrName, logFile)
227 logFileName := filepath.Base(logFile)
228 dstPath := filepath.Join(config.ContainerLogDest, logFileName)
229 _, err := combinedOutput(ctx, "docker", "cp", srcPath, dstPath)
230 if err != nil {
231 fmt.Fprintf(os.Stderr, "docker cp %s %s failed: %v\n", srcPath, dstPath, err)
232 }
233 fmt.Fprintf(os.Stderr, "\ncopied container log %s to %s\n", srcPath, dstPath)
234 }
235 }
236
237 // NOTE: we want to see what the internal sketch binary prints
238 // regardless of the setting of the verbosity flag on the external
239 // binary, so reading "docker logs", which is the stdout/stderr of
240 // the internal binary is not conditional on the verbose flag.
241 appendInternalErr := func(err error) error {
242 if err == nil {
243 return nil
244 }
245 out, logsErr := combinedOutput(ctx, "docker", "logs", cntrName)
Philip Zeyligerd1402952025-04-23 03:54:37 +0000246 if logsErr != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700247 return fmt.Errorf("%w; and docker logs failed: %s, %v", err, out, logsErr)
248 }
249 out = bytes.TrimSpace(out)
250 if len(out) > 0 {
251 return fmt.Errorf("docker logs: %s;\n%w", out, err)
252 }
253 return err
254 }
255
256 // Get the sketch server port from the container
Sean McCulloughae3480f2025-04-23 15:28:20 -0700257 localAddr, err := getContainerPort(ctx, cntrName, "80")
Earl Lee2e463fb2025-04-17 11:22:22 -0700258 if err != nil {
259 return appendInternalErr(err)
260 }
261
Sean McCulloughae3480f2025-04-23 15:28:20 -0700262 localSSHAddr, err := getContainerPort(ctx, cntrName, "22")
263 if err != nil {
264 return appendInternalErr(err)
265 }
266 sshHost, sshPort, err := net.SplitHostPort(localSSHAddr)
267 if err != nil {
Sean McCullough4854c652025-04-24 18:37:02 -0700268 return appendInternalErr(fmt.Errorf("Error splitting ssh host and port: %w", err))
Sean McCulloughae3480f2025-04-23 15:28:20 -0700269 }
Sean McCullough4854c652025-04-24 18:37:02 -0700270
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700271 var sshServerIdentity, sshUserIdentity []byte
Sean McCullough4854c652025-04-24 18:37:02 -0700272
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700273 if err := CheckForInclude(); err != nil {
274 fmt.Println(err.Error())
275 // continue - ssh config is not required for the rest of sketch to function locally.
276 } else {
277 cst, err := NewSSHTheather(cntrName, sshHost, sshPort)
278 if err != nil {
279 return appendInternalErr(fmt.Errorf("NewContainerSSHTheather: %w", err))
280 }
281
Sean McCulloughea3fc202025-04-28 12:53:37 -0700282 // Note: The vscode: link uses an undocumented request parameter that I really had to dig to find:
283 // https://github.com/microsoft/vscode/blob/2b9486161abaca59b5132ce3c59544f3cc7000f6/src/vs/code/electron-main/app.ts#L878
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700284 fmt.Printf(`Connect to this container via any of these methods:
Sean McCullough4854c652025-04-24 18:37:02 -0700285🖥️ ssh %s
286🖥️ code --remote ssh-remote+root@%s /app -n
Sean McCulloughea3fc202025-04-28 12:53:37 -0700287🔗 vscode://vscode-remote/ssh-remote+root@%s/app?windowId=_blank
Sean McCullough4854c652025-04-24 18:37:02 -0700288`, cntrName, cntrName, cntrName)
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700289 sshUserIdentity = cst.userIdentity
290 sshServerIdentity = cst.serverIdentity
291 defer func() {
292 if err := cst.Cleanup(); err != nil {
293 appendInternalErr(err)
294 }
295 }()
296 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700297
Earl Lee2e463fb2025-04-17 11:22:22 -0700298 // Tell the sketch container which git server port and commit to initialize with.
299 go func() {
300 // TODO: Why is this called in a goroutine? I have found that when I pull this out
301 // of the goroutine and call it inline, then the terminal UI clears itself and all
302 // the scrollback (which is not good, but also not fatal). I can't see why it does this
303 // though, since none of the calls in postContainerInitConfig obviously write to stdout
304 // or stderr.
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700305 if err := postContainerInitConfig(ctx, localAddr, commit, gitSrv.gitPort, gitSrv.pass, sshServerIdentity, sshUserIdentity); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700306 slog.ErrorContext(ctx, "LaunchContainer.postContainerInitConfig", slog.String("err", err.Error()))
307 errCh <- appendInternalErr(err)
308 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700309
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700310 // We open the browser after the init config because the above waits for the web server to be serving.
311 if config.OpenBrowser {
312 if config.SkabandAddr != "" {
313 OpenBrowser(ctx, fmt.Sprintf("%s/s/%s", config.SkabandAddr, config.SessionID))
314 } else {
315 OpenBrowser(ctx, "http://"+localAddr)
316 }
317 }
318 }()
Earl Lee2e463fb2025-04-17 11:22:22 -0700319
320 go func() {
321 cmd := exec.CommandContext(ctx, "docker", "attach", cntrName)
322 cmd.Stdin = os.Stdin
323 cmd.Stdout = os.Stdout
324 cmd.Stderr = os.Stderr
325 errCh <- run(ctx, "docker attach", cmd)
326 }()
327
328 defer copyLogs()
329
330 for {
331 select {
332 case <-ctx.Done():
333 return ctx.Err()
334 case err := <-errCh:
335 if err != nil {
336 return appendInternalErr(fmt.Errorf("container process: %w", err))
337 }
338 return nil
339 }
340 }
341}
342
343func combinedOutput(ctx context.Context, cmdName string, args ...string) ([]byte, error) {
344 cmd := exec.CommandContext(ctx, cmdName, args...)
345 // Really only needed for the "go build" command for the linux sketch binary
346 cmd.Env = append(os.Environ(), "GOOS=linux", "CGO_ENABLED=0")
347 start := time.Now()
348
349 out, err := cmd.CombinedOutput()
350 if err != nil {
351 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))))
352 } else {
353 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))))
354 }
355 return out, err
356}
357
358func run(ctx context.Context, cmdName string, cmd *exec.Cmd) error {
359 start := time.Now()
360 err := cmd.Run()
361 if err != nil {
362 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))))
363 } else {
364 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))))
365 }
366 return err
367}
368
369type gitServer struct {
370 gitLn net.Listener
371 gitPort string
372 srv *http.Server
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700373 pass string
Earl Lee2e463fb2025-04-17 11:22:22 -0700374}
375
376func (gs *gitServer) shutdown(ctx context.Context) {
377 gs.srv.Shutdown(ctx)
378 gs.gitLn.Close()
379}
380
381// Serve a git remote from the host for the container to fetch from and push to.
382func (gs *gitServer) serve(ctx context.Context) error {
383 slog.DebugContext(ctx, "starting git server", slog.String("git_remote_addr", "http://host.docker.internal:"+gs.gitPort+"/.git"))
384 return gs.srv.Serve(gs.gitLn)
385}
386
387func newGitServer(gitRoot string) (*gitServer, error) {
Josh Bleecher Snyder9f6a9982025-04-22 17:34:15 -0700388 ret := &gitServer{
389 pass: rand.Text(),
390 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700391
Earl Lee2e463fb2025-04-17 11:22:22 -0700392 gitLn, err := net.Listen("tcp4", ":0")
393 if err != nil {
394 return nil, fmt.Errorf("git listen: %w", err)
395 }
396 ret.gitLn = gitLn
397
398 srv := http.Server{
Josh Bleecher Snyder9f6a9982025-04-22 17:34:15 -0700399 Handler: &gitHTTP{gitRepoRoot: gitRoot, pass: []byte(ret.pass)},
Earl Lee2e463fb2025-04-17 11:22:22 -0700400 }
401 ret.srv = &srv
402
403 _, gitPort, err := net.SplitHostPort(gitLn.Addr().String())
404 if err != nil {
405 return nil, fmt.Errorf("git port: %w", err)
406 }
407 ret.gitPort = gitPort
408 return ret, nil
409}
410
411func createDockerContainer(ctx context.Context, cntrName, hostPort, relPath, imgName string, config ContainerConfig) error {
412 //, config.SessionID, config.GitUsername, config.GitEmail, config.SkabandAddr
413 // sessionID, gitUsername, gitEmail, skabandAddr string
David Crawshaw69c67312025-04-17 13:42:00 -0700414 cmdArgs := []string{
415 "create",
Earl Lee2e463fb2025-04-17 11:22:22 -0700416 "-it",
417 "--name", cntrName,
418 "-p", hostPort + ":80", // forward container port 80 to a host port
419 "-e", "ANTHROPIC_API_KEY=" + config.AntAPIKey,
420 }
421 if config.AntURL != "" {
422 cmdArgs = append(cmdArgs, "-e", "ANT_URL="+config.AntURL)
423 }
424 if config.SketchPubKey != "" {
425 cmdArgs = append(cmdArgs, "-e", "SKETCH_PUB_KEY="+config.SketchPubKey)
426 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700427 if config.SSHPort > 0 {
428 cmdArgs = append(cmdArgs, "-p", fmt.Sprintf("%d:22", config.SSHPort)) // forward container ssh port to host ssh port
429 } else {
430 cmdArgs = append(cmdArgs, "-p", "22") // use an ephemeral host port for ssh.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700431 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700432 if relPath != "." {
433 cmdArgs = append(cmdArgs, "-w", "/app/"+relPath)
434 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700435 // colima does this by default, but Linux docker seems to need this set explicitly
436 cmdArgs = append(cmdArgs, "--add-host", "host.docker.internal:host-gateway")
Earl Lee2e463fb2025-04-17 11:22:22 -0700437 cmdArgs = append(
438 cmdArgs,
439 imgName,
440 "/bin/sketch",
441 "-unsafe",
442 "-addr=:80",
443 "-session-id="+config.SessionID,
Philip Zeyligerd1402952025-04-23 03:54:37 +0000444 "-git-username="+config.GitUsername,
445 "-git-email="+config.GitEmail,
Philip Zeyliger18532b22025-04-23 21:11:46 +0000446 "-outside-hostname="+config.OutsideHostname,
447 "-outside-os="+config.OutsideOS,
448 "-outside-working-dir="+config.OutsideWorkingDir,
Josh Bleecher Snyder3cae7d92025-04-30 09:54:29 -0700449 "-open=false",
Earl Lee2e463fb2025-04-17 11:22:22 -0700450 )
451 if config.SkabandAddr != "" {
452 cmdArgs = append(cmdArgs, "-skaband-addr="+config.SkabandAddr)
453 }
Pokey Rule0dcebe12025-04-28 14:51:04 +0100454 if config.Prompt != "" {
455 cmdArgs = append(cmdArgs, "-prompt", config.Prompt)
456 }
457 if config.OneShot {
458 cmdArgs = append(cmdArgs, "-one-shot")
Philip Zeyligerb74c4f62025-04-25 19:18:49 -0700459 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700460 if out, err := combinedOutput(ctx, "docker", cmdArgs...); err != nil {
461 return fmt.Errorf("docker create: %s, %w", out, err)
462 }
463 return nil
464}
465
466func buildLinuxSketchBin(ctx context.Context, path string) (string, error) {
David Crawshaw8a617cb2025-04-18 01:28:43 -0700467 homeDir, err := os.UserHomeDir()
David Crawshaw69c67312025-04-17 13:42:00 -0700468 if err != nil {
469 return "", err
470 }
David Crawshaw8a617cb2025-04-18 01:28:43 -0700471 linuxGopath := filepath.Join(homeDir, ".cache", "sketch", "linuxgo")
472 if err := os.MkdirAll(linuxGopath, 0o777); err != nil {
473 return "", err
474 }
475
476 verToInstall := "@latest"
477 if out, err := exec.Command("go", "list", "-m").CombinedOutput(); err != nil {
478 return "", fmt.Errorf("failed to run go list -m: %s: %v", out, err)
479 } else {
480 if strings.TrimSpace(string(out)) == "sketch.dev" {
David Crawshaw094e4d22025-04-24 11:35:14 -0700481 slog.DebugContext(ctx, "built linux agent from currently checked out module")
David Crawshaw8a617cb2025-04-18 01:28:43 -0700482 verToInstall = ""
483 }
484 }
David Crawshaw69c67312025-04-17 13:42:00 -0700485
Earl Lee2e463fb2025-04-17 11:22:22 -0700486 start := time.Now()
David Crawshaw8a617cb2025-04-18 01:28:43 -0700487 cmd := exec.CommandContext(ctx, "go", "install", "sketch.dev/cmd/sketch"+verToInstall)
David Crawshawb9eaef52025-04-17 15:23:18 -0700488 cmd.Env = append(
489 os.Environ(),
490 "GOOS=linux",
491 "CGO_ENABLED=0",
492 "GOTOOLCHAIN=auto",
David Crawshaw8a617cb2025-04-18 01:28:43 -0700493 "GOPATH="+linuxGopath,
Josh Bleecher Snyderfae17572025-04-21 11:48:05 -0700494 "GOBIN=",
David Crawshawb9eaef52025-04-17 15:23:18 -0700495 )
Earl Lee2e463fb2025-04-17 11:22:22 -0700496
Earl Lee2e463fb2025-04-17 11:22:22 -0700497 out, err := cmd.CombinedOutput()
498 if err != nil {
499 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))))
500 return "", fmt.Errorf("failed to build linux sketch binary: %s: %w", out, err)
501 } else {
502 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))))
503 }
504
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700505 var src string
506 if runtime.GOOS != "linux" {
507 src = filepath.Join(linuxGopath, "bin", "linux_"+runtime.GOARCH, "sketch")
508 } else {
509 // If we are already on Linux, there's no extra platform name in the path
510 src = filepath.Join(linuxGopath, "bin", "sketch")
511 }
512
David Crawshaw69c67312025-04-17 13:42:00 -0700513 dst := filepath.Join(path, "tmp-sketch-binary-linux")
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700514 if err := moveFile(src, dst); err != nil {
David Crawshaw69c67312025-04-17 13:42:00 -0700515 return "", err
516 }
517
David Crawshaw69c67312025-04-17 13:42:00 -0700518 return dst, nil
Earl Lee2e463fb2025-04-17 11:22:22 -0700519}
520
Sean McCulloughae3480f2025-04-23 15:28:20 -0700521func getContainerPort(ctx context.Context, cntrName, cntrPort string) (string, error) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700522 localAddr := ""
Sean McCulloughae3480f2025-04-23 15:28:20 -0700523 if out, err := combinedOutput(ctx, "docker", "port", cntrName, cntrPort); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700524 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
525 } else {
526 v4, _, found := strings.Cut(string(out), "\n")
527 if !found {
528 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
529 }
530 localAddr = v4
531 if strings.HasPrefix(localAddr, "0.0.0.0") {
532 localAddr = "127.0.0.1" + strings.TrimPrefix(localAddr, "0.0.0.0")
533 }
534 }
535 return localAddr, nil
536}
537
538// Contact the container and configure it.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700539func postContainerInitConfig(ctx context.Context, localAddr, commit, gitPort, gitPass string, sshServerIdentity, sshAuthorizedKeys []byte) error {
Earl Lee2e463fb2025-04-17 11:22:22 -0700540 localURL := "http://" + localAddr
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700541
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000542 // Check if SSH is available by checking for the Include directive in ~/.ssh/config
543 sshAvailable := true
544 sshError := ""
545 if err := CheckForInclude(); err != nil {
546 sshAvailable = false
547 sshError = err.Error()
548 }
549
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700550 initMsg, err := json.Marshal(
551 server.InitRequest{
552 Commit: commit,
553 GitRemoteAddr: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s/.git", gitPass, gitPort),
554 HostAddr: localAddr,
555 SSHAuthorizedKeys: sshAuthorizedKeys,
556 SSHServerIdentity: sshServerIdentity,
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000557 SSHAvailable: sshAvailable,
558 SSHError: sshError,
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700559 })
Earl Lee2e463fb2025-04-17 11:22:22 -0700560 if err != nil {
561 return fmt.Errorf("init msg: %w", err)
562 }
563
Earl Lee2e463fb2025-04-17 11:22:22 -0700564 // Note: this /init POST is handled in loop/server/loophttp.go:
565 initMsgByteReader := bytes.NewReader(initMsg)
566 req, err := http.NewRequest("POST", localURL+"/init", initMsgByteReader)
567 if err != nil {
568 return err
569 }
570
571 var res *http.Response
572 for i := 0; ; i++ {
573 time.Sleep(100 * time.Millisecond)
574 // If you DON'T reset this byteReader, then subsequent retries may end up sending 0 bytes.
575 initMsgByteReader.Reset(initMsg)
576 res, err = http.DefaultClient.Do(req)
577 if err != nil {
578 // In addition to "connection refused", we also occasionally see "EOF" errors that can succeed on retries.
579 if i < 100 && (strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "EOF")) {
580 slog.DebugContext(ctx, "postContainerInitConfig retrying", slog.Int("retry", i), slog.String("err", err.Error()))
581 continue
582 }
583 return fmt.Errorf("failed to %s/init sketch in container, NOT retrying: err: %v", localURL, err)
584 }
585 break
586 }
587 resBytes, _ := io.ReadAll(res.Body)
588 if res.StatusCode != http.StatusOK {
589 return fmt.Errorf("failed to initialize sketch in container, response status code %d: %s", res.StatusCode, resBytes)
590 }
591 return nil
592}
593
594func findOrBuildDockerImage(ctx context.Context, stdout, stderr io.Writer, cwd, gitRoot, antURL, antAPIKey string, forceRebuild bool) (imgName string, err error) {
595 h := sha256.Sum256([]byte(gitRoot))
596 imgName = "sketch-" + hex.EncodeToString(h[:6])
597
598 var curImgInitFilesHash string
599 if out, err := combinedOutput(ctx, "docker", "inspect", "--format", "{{json .Config.Labels}}", imgName); err != nil {
600 if strings.Contains(string(out), "No such object") {
601 // Image does not exist, continue and build it.
602 curImgInitFilesHash = ""
603 } else {
604 return "", fmt.Errorf("docker inspect failed: %s, %v", out, err)
605 }
606 } else {
607 m := map[string]string{}
608 if err := json.Unmarshal(bytes.TrimSpace(out), &m); err != nil {
609 return "", fmt.Errorf("docker inspect output unparsable: %s, %v", out, err)
610 }
611 curImgInitFilesHash = m["sketch_context"]
612 }
613
614 candidates, err := findRepoDockerfiles(cwd, gitRoot)
615 if err != nil {
616 return "", fmt.Errorf("find dockerfile: %w", err)
617 }
618
619 var initFiles map[string]string
620 var dockerfilePath string
621
622 // TODO: prefer a "Dockerfile.sketch" so users can tailor any env to this tool.
623 if len(candidates) == 1 && strings.ToLower(filepath.Base(candidates[0])) == "dockerfile" {
624 dockerfilePath = candidates[0]
625 contents, err := os.ReadFile(dockerfilePath)
626 if err != nil {
627 return "", err
628 }
629 fmt.Printf("using %s as dev env\n", candidates[0])
630 if hashInitFiles(map[string]string{dockerfilePath: string(contents)}) == curImgInitFilesHash && !forceRebuild {
Earl Lee2e463fb2025-04-17 11:22:22 -0700631 return imgName, nil
632 }
633 } else {
634 initFiles, err = readInitFiles(os.DirFS(gitRoot))
635 if err != nil {
636 return "", err
637 }
638 subPathWorkingDir, err := filepath.Rel(gitRoot, cwd)
639 if err != nil {
640 return "", err
641 }
642 initFileHash := hashInitFiles(initFiles)
643 if curImgInitFilesHash == initFileHash && !forceRebuild {
Earl Lee2e463fb2025-04-17 11:22:22 -0700644 return imgName, nil
645 }
646
647 start := time.Now()
648 dockerfile, err := createDockerfile(ctx, http.DefaultClient, antURL, antAPIKey, initFiles, subPathWorkingDir)
649 if err != nil {
650 return "", fmt.Errorf("create dockerfile: %w", err)
651 }
652 dockerfilePath = filepath.Join(cwd, "tmp-sketch-dockerfile")
653 if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0o666); err != nil {
654 return "", err
655 }
656 defer os.Remove(dockerfilePath)
657
658 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))
659 }
660
661 var gitUserEmail, gitUserName string
662 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.email"); err != nil {
663 return "", fmt.Errorf("git config: %s: %v", out, err)
664 } else {
665 gitUserEmail = strings.TrimSpace(string(out))
666 }
667 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.name"); err != nil {
668 return "", fmt.Errorf("git config: %s: %v", out, err)
669 } else {
670 gitUserName = strings.TrimSpace(string(out))
671 }
672
673 start := time.Now()
674 cmd := exec.CommandContext(ctx,
675 "docker", "build",
676 "-t", imgName,
677 "-f", dockerfilePath,
678 "--build-arg", "GIT_USER_EMAIL="+gitUserEmail,
679 "--build-arg", "GIT_USER_NAME="+gitUserName,
680 ".",
681 )
682 cmd.Dir = gitRoot
683 cmd.Stdout = stdout
684 cmd.Stderr = stderr
Josh Bleecher Snyderdf2d3dc2025-04-25 12:31:35 -0700685 fmt.Printf("🏗️ building docker image %s... (use -verbose to see build output)\n", imgName)
Philip Zeyligere4fa0e32025-04-23 14:15:55 -0700686 dockerfileContent, err := os.ReadFile(dockerfilePath)
687 if err != nil {
688 return "", fmt.Errorf("failed to read Dockerfile: %w", err)
689 }
Philip Zeyliger5d6af872025-04-23 19:48:34 -0700690 fmt.Fprintf(stdout, "Dockerfile:\n%s\n", string(dockerfileContent))
Earl Lee2e463fb2025-04-17 11:22:22 -0700691
692 err = run(ctx, "docker build", cmd)
693 if err != nil {
694 return "", fmt.Errorf("docker build failed: %v", err)
695 }
696 fmt.Printf("built docker image %s in %s\n", imgName, time.Since(start).Round(time.Millisecond))
697 return imgName, nil
698}
699
700func findRepoDockerfiles(cwd, gitRoot string) ([]string, error) {
701 files, err := findDirDockerfiles(cwd)
702 if err != nil {
703 return nil, err
704 }
705 if len(files) > 0 {
706 return files, nil
707 }
708
709 path := cwd
710 for path != gitRoot {
711 path = filepath.Dir(path)
712 files, err := findDirDockerfiles(path)
713 if err != nil {
714 return nil, err
715 }
716 if len(files) > 0 {
717 return files, nil
718 }
719 }
720 return files, nil
721}
722
723// findDirDockerfiles finds all "Dockerfile*" files in a directory.
724func findDirDockerfiles(root string) (res []string, err error) {
725 err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
726 if err != nil {
727 return err
728 }
729 if info.IsDir() && root != path {
730 return filepath.SkipDir
731 }
732 name := strings.ToLower(info.Name())
733 if name == "dockerfile" || strings.HasPrefix(name, "dockerfile.") {
734 res = append(res, path)
735 }
736 return nil
737 })
738 if err != nil {
739 return nil, err
740 }
741 return res, nil
742}
743
744func findGitRoot(ctx context.Context, path string) (string, error) {
745 cmd := exec.CommandContext(ctx, "git", "rev-parse", "--git-common-dir")
746 cmd.Dir = path
747 out, err := cmd.CombinedOutput()
748 if err != nil {
749 if strings.Contains(string(out), "not a git repository") {
750 return "", fmt.Errorf(`sketch needs to run from within a git repo, but %s is not part of a git repo.
751Consider one of the following options:
752 - cd to a different dir that is already part of a git repo first, or
753 - to create a new git repo from this directory (%s), run this command:
754
755 git init . && git commit --allow-empty -m "initial commit"
756
757and try running sketch again.
758`, path, path)
759 }
760 return "", fmt.Errorf("git rev-parse --git-common-dir: %s: %w", out, err)
761 }
762 gitDir := strings.TrimSpace(string(out)) // location of .git dir, often as a relative path
763 absGitDir := filepath.Join(path, gitDir)
764 return filepath.Dir(absGitDir), err
765}
766
767func OpenBrowser(ctx context.Context, url string) {
768 var cmd *exec.Cmd
769 switch runtime.GOOS {
770 case "darwin":
771 cmd = exec.CommandContext(ctx, "open", url)
772 case "windows":
773 cmd = exec.CommandContext(ctx, "cmd", "/c", "start", url)
774 default: // Linux and other Unix-like systems
775 cmd = exec.CommandContext(ctx, "xdg-open", url)
776 }
777 if b, err := cmd.CombinedOutput(); err != nil {
778 fmt.Fprintf(os.Stderr, "failed to open browser: %v: %s\n", err, b)
779 }
780}
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700781
782// moveFile is like Python's shutil.move, in that it tries a rename, and, if that fails,
783// copies and deletes
784func moveFile(src, dst string) error {
785 if err := os.Rename(src, dst); err == nil {
786 return nil
787 }
788
789 stat, err := os.Stat(src)
790 if err != nil {
791 return err
792 }
793
794 sourceFile, err := os.Open(src)
795 if err != nil {
796 return err
797 }
798 defer sourceFile.Close()
799
800 destFile, err := os.Create(dst)
801 if err != nil {
802 return err
803 }
804 defer destFile.Close()
805
806 _, err = io.Copy(destFile, sourceFile)
807 if err != nil {
808 return err
809 }
810
811 sourceFile.Close()
812 destFile.Close()
813
814 os.Chmod(dst, stat.Mode())
815
816 return os.Remove(src)
817}