blob: a92fb92498a0f7ad6f5bf8a220974459e90ab4b3 [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
Earl Lee2e463fb2025-04-17 11:22:22 -070079}
80
81// LaunchContainer creates a docker container for a project, installs sketch and opens a connection to it.
82// It writes status to stdout.
83func LaunchContainer(ctx context.Context, stdout, stderr io.Writer, config ContainerConfig) error {
84 if _, err := exec.LookPath("docker"); err != nil {
Philip Zeyliger5e227dd2025-04-21 15:55:29 -070085 if runtime.GOOS == "darwin" {
86 return fmt.Errorf("cannot find `docker` binary; run: brew install docker colima && colima start")
87 } else {
88 return fmt.Errorf("cannot find `docker` binary; install docker (e.g., apt-get install docker.io)")
89 }
Earl Lee2e463fb2025-04-17 11:22:22 -070090 }
91
92 if out, err := combinedOutput(ctx, "docker", "ps"); err != nil {
93 // `docker ps` provides a good error message here that can be
94 // easily chatgpt'ed by users, so send it to the user as-is:
95 // Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
96 return fmt.Errorf("docker ps: %s (%w)", out, err)
97 }
98
99 _, hostPort, err := net.SplitHostPort(config.LocalAddr)
100 if err != nil {
101 return err
102 }
103
104 gitRoot, err := findGitRoot(ctx, config.Path)
105 if err != nil {
106 return err
107 }
108
109 imgName, err := findOrBuildDockerImage(ctx, stdout, stderr, config.Path, gitRoot, config.AntURL, config.AntAPIKey, config.ForceRebuild)
110 if err != nil {
111 return err
112 }
113
114 linuxSketchBin := config.SketchBinaryLinux
115 if linuxSketchBin == "" {
116 linuxSketchBin, err = buildLinuxSketchBin(ctx, config.Path)
117 if err != nil {
118 return err
119 }
Josh Bleecher Snyder5544d142025-04-23 14:15:45 -0700120 defer os.Remove(linuxSketchBin) // in case of errors
Earl Lee2e463fb2025-04-17 11:22:22 -0700121 }
122
123 cntrName := imgName + "-" + config.SessionID
124 defer func() {
125 if config.NoCleanup {
126 return
127 }
128 if out, err := combinedOutput(ctx, "docker", "kill", cntrName); err != nil {
129 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
130 _ = out
131 }
132 if out, err := combinedOutput(ctx, "docker", "rm", cntrName); err != nil {
133 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
134 _ = out
135 }
136 }()
137
138 // errCh receives errors from operations that this function calls in separate goroutines.
139 errCh := make(chan error)
140
141 // Start the git server
142 gitSrv, err := newGitServer(gitRoot)
143 if err != nil {
144 return fmt.Errorf("failed to start git server: %w", err)
145 }
146 defer gitSrv.shutdown(ctx)
147
148 go func() {
149 errCh <- gitSrv.serve(ctx)
150 }()
151
152 // Get the current host git commit
153 var commit string
154 if out, err := combinedOutput(ctx, "git", "rev-parse", "HEAD"); err != nil {
155 return fmt.Errorf("git rev-parse HEAD: %w", err)
156 } else {
157 commit = strings.TrimSpace(string(out))
158 }
159 if out, err := combinedOutput(ctx, "git", "config", "http.receivepack", "true"); err != nil {
160 return fmt.Errorf("git config http.receivepack true: %s: %w", out, err)
161 }
162
163 relPath, err := filepath.Rel(gitRoot, config.Path)
164 if err != nil {
165 return err
166 }
167
168 // Create the sketch container
169 if err := createDockerContainer(ctx, cntrName, hostPort, relPath, imgName, config); err != nil {
170 return err
171 }
172
173 // Copy the sketch linux binary into the container
174 if out, err := combinedOutput(ctx, "docker", "cp", linuxSketchBin, cntrName+":/bin/sketch"); err != nil {
175 return fmt.Errorf("docker cp: %s, %w", out, err)
176 }
Josh Bleecher Snyder5544d142025-04-23 14:15:45 -0700177 os.Remove(linuxSketchBin) // in normal operations, the code below blocks, so actively delete now
Sean McCulloughf5bb3d32025-04-18 10:47:59 -0700178
179 // Make sure that the webui is built so we can copy the results to the container.
180 _, err = webui.Build()
181 if err != nil {
182 return fmt.Errorf("failed to build webui: %w", err)
183 }
184
David Crawshaw8bff16a2025-04-18 01:16:49 -0700185 webuiZipPath, err := webui.ZipPath()
186 if err != nil {
187 return err
188 }
189 if out, err := combinedOutput(ctx, "docker", "cp", webuiZipPath, cntrName+":/root/.cache/sketch/webui/"+filepath.Base(webuiZipPath)); err != nil {
190 return fmt.Errorf("docker cp: %s, %w", out, err)
191 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700192
David Crawshaw53786ef2025-04-24 12:52:51 -0700193 fmt.Printf("📦 running in container %s\n", cntrName)
Earl Lee2e463fb2025-04-17 11:22:22 -0700194
195 // Start the sketch container
196 if out, err := combinedOutput(ctx, "docker", "start", cntrName); err != nil {
197 return fmt.Errorf("docker start: %s, %w", out, err)
198 }
199
200 // Copies structured logs from the container to the host.
201 copyLogs := func() {
202 if config.ContainerLogDest == "" {
203 return
204 }
205 out, err := combinedOutput(ctx, "docker", "logs", cntrName)
206 if err != nil {
207 fmt.Fprintf(os.Stderr, "docker logs failed: %v\n", err)
208 return
209 }
Josh Bleecher Snyder7660e4e2025-04-24 10:34:17 -0700210 prefix := []byte("structured logs:")
211 for line := range bytes.Lines(out) {
212 rest, ok := bytes.CutPrefix(line, prefix)
213 if !ok {
Earl Lee2e463fb2025-04-17 11:22:22 -0700214 continue
215 }
Josh Bleecher Snyder7660e4e2025-04-24 10:34:17 -0700216 logFile := string(bytes.TrimSpace(rest))
Earl Lee2e463fb2025-04-17 11:22:22 -0700217 srcPath := fmt.Sprintf("%s:%s", cntrName, logFile)
218 logFileName := filepath.Base(logFile)
219 dstPath := filepath.Join(config.ContainerLogDest, logFileName)
220 _, err := combinedOutput(ctx, "docker", "cp", srcPath, dstPath)
221 if err != nil {
222 fmt.Fprintf(os.Stderr, "docker cp %s %s failed: %v\n", srcPath, dstPath, err)
223 }
224 fmt.Fprintf(os.Stderr, "\ncopied container log %s to %s\n", srcPath, dstPath)
225 }
226 }
227
228 // NOTE: we want to see what the internal sketch binary prints
229 // regardless of the setting of the verbosity flag on the external
230 // binary, so reading "docker logs", which is the stdout/stderr of
231 // the internal binary is not conditional on the verbose flag.
232 appendInternalErr := func(err error) error {
233 if err == nil {
234 return nil
235 }
236 out, logsErr := combinedOutput(ctx, "docker", "logs", cntrName)
Philip Zeyligerd1402952025-04-23 03:54:37 +0000237 if logsErr != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700238 return fmt.Errorf("%w; and docker logs failed: %s, %v", err, out, logsErr)
239 }
240 out = bytes.TrimSpace(out)
241 if len(out) > 0 {
242 return fmt.Errorf("docker logs: %s;\n%w", out, err)
243 }
244 return err
245 }
246
247 // Get the sketch server port from the container
Sean McCulloughae3480f2025-04-23 15:28:20 -0700248 localAddr, err := getContainerPort(ctx, cntrName, "80")
Earl Lee2e463fb2025-04-17 11:22:22 -0700249 if err != nil {
250 return appendInternalErr(err)
251 }
252
Sean McCulloughae3480f2025-04-23 15:28:20 -0700253 localSSHAddr, err := getContainerPort(ctx, cntrName, "22")
254 if err != nil {
255 return appendInternalErr(err)
256 }
257 sshHost, sshPort, err := net.SplitHostPort(localSSHAddr)
258 if err != nil {
Sean McCullough4854c652025-04-24 18:37:02 -0700259 return appendInternalErr(fmt.Errorf("Error splitting ssh host and port: %w", err))
Sean McCulloughae3480f2025-04-23 15:28:20 -0700260 }
Sean McCullough4854c652025-04-24 18:37:02 -0700261
262 cst, err := NewSSHTheather(cntrName, sshHost, sshPort)
263 if err != nil {
264 return appendInternalErr(fmt.Errorf("NewContainerSSHTheather: %w", err))
265 }
266
267 fmt.Printf(`Connect to this container via any of these methods:
268🖥️ ssh %s
269🖥️ code --remote ssh-remote+root@%s /app -n
270🔗 vscode://vscode-remote/ssh-remote+root@%s/app?n=true
271`, cntrName, cntrName, cntrName)
272
273 defer func() {
274 if err := cst.Cleanup(); err != nil {
275 appendInternalErr(err)
276 }
277 }()
Sean McCulloughae3480f2025-04-23 15:28:20 -0700278
Earl Lee2e463fb2025-04-17 11:22:22 -0700279 // Tell the sketch container which git server port and commit to initialize with.
280 go func() {
281 // TODO: Why is this called in a goroutine? I have found that when I pull this out
282 // of the goroutine and call it inline, then the terminal UI clears itself and all
283 // the scrollback (which is not good, but also not fatal). I can't see why it does this
284 // though, since none of the calls in postContainerInitConfig obviously write to stdout
285 // or stderr.
Sean McCullough4854c652025-04-24 18:37:02 -0700286 if err := postContainerInitConfig(ctx, localAddr, commit, gitSrv.gitPort, gitSrv.pass, cst.serverIdentity, cst.userIdentity); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700287 slog.ErrorContext(ctx, "LaunchContainer.postContainerInitConfig", slog.String("err", err.Error()))
288 errCh <- appendInternalErr(err)
289 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700290
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700291 // We open the browser after the init config because the above waits for the web server to be serving.
292 if config.OpenBrowser {
293 if config.SkabandAddr != "" {
294 OpenBrowser(ctx, fmt.Sprintf("%s/s/%s", config.SkabandAddr, config.SessionID))
295 } else {
296 OpenBrowser(ctx, "http://"+localAddr)
297 }
298 }
299 }()
Earl Lee2e463fb2025-04-17 11:22:22 -0700300
301 go func() {
302 cmd := exec.CommandContext(ctx, "docker", "attach", cntrName)
303 cmd.Stdin = os.Stdin
304 cmd.Stdout = os.Stdout
305 cmd.Stderr = os.Stderr
306 errCh <- run(ctx, "docker attach", cmd)
307 }()
308
309 defer copyLogs()
310
311 for {
312 select {
313 case <-ctx.Done():
314 return ctx.Err()
315 case err := <-errCh:
316 if err != nil {
317 return appendInternalErr(fmt.Errorf("container process: %w", err))
318 }
319 return nil
320 }
321 }
322}
323
324func combinedOutput(ctx context.Context, cmdName string, args ...string) ([]byte, error) {
325 cmd := exec.CommandContext(ctx, cmdName, args...)
326 // Really only needed for the "go build" command for the linux sketch binary
327 cmd.Env = append(os.Environ(), "GOOS=linux", "CGO_ENABLED=0")
328 start := time.Now()
329
330 out, err := cmd.CombinedOutput()
331 if err != nil {
332 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))))
333 } else {
334 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))))
335 }
336 return out, err
337}
338
339func run(ctx context.Context, cmdName string, cmd *exec.Cmd) error {
340 start := time.Now()
341 err := cmd.Run()
342 if err != nil {
343 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))))
344 } else {
345 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))))
346 }
347 return err
348}
349
350type gitServer struct {
351 gitLn net.Listener
352 gitPort string
353 srv *http.Server
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700354 pass string
Earl Lee2e463fb2025-04-17 11:22:22 -0700355}
356
357func (gs *gitServer) shutdown(ctx context.Context) {
358 gs.srv.Shutdown(ctx)
359 gs.gitLn.Close()
360}
361
362// Serve a git remote from the host for the container to fetch from and push to.
363func (gs *gitServer) serve(ctx context.Context) error {
364 slog.DebugContext(ctx, "starting git server", slog.String("git_remote_addr", "http://host.docker.internal:"+gs.gitPort+"/.git"))
365 return gs.srv.Serve(gs.gitLn)
366}
367
368func newGitServer(gitRoot string) (*gitServer, error) {
Josh Bleecher Snyder9f6a9982025-04-22 17:34:15 -0700369 ret := &gitServer{
370 pass: rand.Text(),
371 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700372
Earl Lee2e463fb2025-04-17 11:22:22 -0700373 gitLn, err := net.Listen("tcp4", ":0")
374 if err != nil {
375 return nil, fmt.Errorf("git listen: %w", err)
376 }
377 ret.gitLn = gitLn
378
379 srv := http.Server{
Josh Bleecher Snyder9f6a9982025-04-22 17:34:15 -0700380 Handler: &gitHTTP{gitRepoRoot: gitRoot, pass: []byte(ret.pass)},
Earl Lee2e463fb2025-04-17 11:22:22 -0700381 }
382 ret.srv = &srv
383
384 _, gitPort, err := net.SplitHostPort(gitLn.Addr().String())
385 if err != nil {
386 return nil, fmt.Errorf("git port: %w", err)
387 }
388 ret.gitPort = gitPort
389 return ret, nil
390}
391
392func createDockerContainer(ctx context.Context, cntrName, hostPort, relPath, imgName string, config ContainerConfig) error {
393 //, config.SessionID, config.GitUsername, config.GitEmail, config.SkabandAddr
394 // sessionID, gitUsername, gitEmail, skabandAddr string
David Crawshaw69c67312025-04-17 13:42:00 -0700395 cmdArgs := []string{
396 "create",
Earl Lee2e463fb2025-04-17 11:22:22 -0700397 "-it",
398 "--name", cntrName,
399 "-p", hostPort + ":80", // forward container port 80 to a host port
400 "-e", "ANTHROPIC_API_KEY=" + config.AntAPIKey,
401 }
402 if config.AntURL != "" {
403 cmdArgs = append(cmdArgs, "-e", "ANT_URL="+config.AntURL)
404 }
405 if config.SketchPubKey != "" {
406 cmdArgs = append(cmdArgs, "-e", "SKETCH_PUB_KEY="+config.SketchPubKey)
407 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700408 if config.SSHPort > 0 {
409 cmdArgs = append(cmdArgs, "-p", fmt.Sprintf("%d:22", config.SSHPort)) // forward container ssh port to host ssh port
410 } else {
411 cmdArgs = append(cmdArgs, "-p", "22") // use an ephemeral host port for ssh.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700412 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700413 if relPath != "." {
414 cmdArgs = append(cmdArgs, "-w", "/app/"+relPath)
415 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700416 // colima does this by default, but Linux docker seems to need this set explicitly
417 cmdArgs = append(cmdArgs, "--add-host", "host.docker.internal:host-gateway")
Earl Lee2e463fb2025-04-17 11:22:22 -0700418 cmdArgs = append(
419 cmdArgs,
420 imgName,
421 "/bin/sketch",
422 "-unsafe",
423 "-addr=:80",
424 "-session-id="+config.SessionID,
Philip Zeyligerd1402952025-04-23 03:54:37 +0000425 "-git-username="+config.GitUsername,
426 "-git-email="+config.GitEmail,
Philip Zeyliger18532b22025-04-23 21:11:46 +0000427 "-outside-hostname="+config.OutsideHostname,
428 "-outside-os="+config.OutsideOS,
429 "-outside-working-dir="+config.OutsideWorkingDir,
Earl Lee2e463fb2025-04-17 11:22:22 -0700430 )
431 if config.SkabandAddr != "" {
432 cmdArgs = append(cmdArgs, "-skaband-addr="+config.SkabandAddr)
433 }
434 if out, err := combinedOutput(ctx, "docker", cmdArgs...); err != nil {
435 return fmt.Errorf("docker create: %s, %w", out, err)
436 }
437 return nil
438}
439
440func buildLinuxSketchBin(ctx context.Context, path string) (string, error) {
David Crawshaw8a617cb2025-04-18 01:28:43 -0700441 homeDir, err := os.UserHomeDir()
David Crawshaw69c67312025-04-17 13:42:00 -0700442 if err != nil {
443 return "", err
444 }
David Crawshaw8a617cb2025-04-18 01:28:43 -0700445 linuxGopath := filepath.Join(homeDir, ".cache", "sketch", "linuxgo")
446 if err := os.MkdirAll(linuxGopath, 0o777); err != nil {
447 return "", err
448 }
449
450 verToInstall := "@latest"
451 if out, err := exec.Command("go", "list", "-m").CombinedOutput(); err != nil {
452 return "", fmt.Errorf("failed to run go list -m: %s: %v", out, err)
453 } else {
454 if strings.TrimSpace(string(out)) == "sketch.dev" {
David Crawshaw094e4d22025-04-24 11:35:14 -0700455 slog.DebugContext(ctx, "built linux agent from currently checked out module")
David Crawshaw8a617cb2025-04-18 01:28:43 -0700456 verToInstall = ""
457 }
458 }
David Crawshaw69c67312025-04-17 13:42:00 -0700459
Earl Lee2e463fb2025-04-17 11:22:22 -0700460 start := time.Now()
David Crawshaw8a617cb2025-04-18 01:28:43 -0700461 cmd := exec.CommandContext(ctx, "go", "install", "sketch.dev/cmd/sketch"+verToInstall)
David Crawshawb9eaef52025-04-17 15:23:18 -0700462 cmd.Env = append(
463 os.Environ(),
464 "GOOS=linux",
465 "CGO_ENABLED=0",
466 "GOTOOLCHAIN=auto",
David Crawshaw8a617cb2025-04-18 01:28:43 -0700467 "GOPATH="+linuxGopath,
Josh Bleecher Snyderfae17572025-04-21 11:48:05 -0700468 "GOBIN=",
David Crawshawb9eaef52025-04-17 15:23:18 -0700469 )
Earl Lee2e463fb2025-04-17 11:22:22 -0700470
Earl Lee2e463fb2025-04-17 11:22:22 -0700471 out, err := cmd.CombinedOutput()
472 if err != nil {
473 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))))
474 return "", fmt.Errorf("failed to build linux sketch binary: %s: %w", out, err)
475 } else {
476 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))))
477 }
478
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700479 var src string
480 if runtime.GOOS != "linux" {
481 src = filepath.Join(linuxGopath, "bin", "linux_"+runtime.GOARCH, "sketch")
482 } else {
483 // If we are already on Linux, there's no extra platform name in the path
484 src = filepath.Join(linuxGopath, "bin", "sketch")
485 }
486
David Crawshaw69c67312025-04-17 13:42:00 -0700487 dst := filepath.Join(path, "tmp-sketch-binary-linux")
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700488 if err := moveFile(src, dst); err != nil {
David Crawshaw69c67312025-04-17 13:42:00 -0700489 return "", err
490 }
491
David Crawshaw69c67312025-04-17 13:42:00 -0700492 return dst, nil
Earl Lee2e463fb2025-04-17 11:22:22 -0700493}
494
Sean McCulloughae3480f2025-04-23 15:28:20 -0700495func getContainerPort(ctx context.Context, cntrName, cntrPort string) (string, error) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700496 localAddr := ""
Sean McCulloughae3480f2025-04-23 15:28:20 -0700497 if out, err := combinedOutput(ctx, "docker", "port", cntrName, cntrPort); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700498 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
499 } else {
500 v4, _, found := strings.Cut(string(out), "\n")
501 if !found {
502 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
503 }
504 localAddr = v4
505 if strings.HasPrefix(localAddr, "0.0.0.0") {
506 localAddr = "127.0.0.1" + strings.TrimPrefix(localAddr, "0.0.0.0")
507 }
508 }
509 return localAddr, nil
510}
511
512// Contact the container and configure it.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700513func postContainerInitConfig(ctx context.Context, localAddr, commit, gitPort, gitPass string, sshServerIdentity, sshAuthorizedKeys []byte) error {
Earl Lee2e463fb2025-04-17 11:22:22 -0700514 localURL := "http://" + localAddr
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700515
516 initMsg, err := json.Marshal(
517 server.InitRequest{
518 Commit: commit,
519 GitRemoteAddr: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s/.git", gitPass, gitPort),
520 HostAddr: localAddr,
521 SSHAuthorizedKeys: sshAuthorizedKeys,
522 SSHServerIdentity: sshServerIdentity,
523 })
Earl Lee2e463fb2025-04-17 11:22:22 -0700524 if err != nil {
525 return fmt.Errorf("init msg: %w", err)
526 }
527
Earl Lee2e463fb2025-04-17 11:22:22 -0700528 // Note: this /init POST is handled in loop/server/loophttp.go:
529 initMsgByteReader := bytes.NewReader(initMsg)
530 req, err := http.NewRequest("POST", localURL+"/init", initMsgByteReader)
531 if err != nil {
532 return err
533 }
534
535 var res *http.Response
536 for i := 0; ; i++ {
537 time.Sleep(100 * time.Millisecond)
538 // If you DON'T reset this byteReader, then subsequent retries may end up sending 0 bytes.
539 initMsgByteReader.Reset(initMsg)
540 res, err = http.DefaultClient.Do(req)
541 if err != nil {
542 // In addition to "connection refused", we also occasionally see "EOF" errors that can succeed on retries.
543 if i < 100 && (strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "EOF")) {
544 slog.DebugContext(ctx, "postContainerInitConfig retrying", slog.Int("retry", i), slog.String("err", err.Error()))
545 continue
546 }
547 return fmt.Errorf("failed to %s/init sketch in container, NOT retrying: err: %v", localURL, err)
548 }
549 break
550 }
551 resBytes, _ := io.ReadAll(res.Body)
552 if res.StatusCode != http.StatusOK {
553 return fmt.Errorf("failed to initialize sketch in container, response status code %d: %s", res.StatusCode, resBytes)
554 }
555 return nil
556}
557
558func findOrBuildDockerImage(ctx context.Context, stdout, stderr io.Writer, cwd, gitRoot, antURL, antAPIKey string, forceRebuild bool) (imgName string, err error) {
559 h := sha256.Sum256([]byte(gitRoot))
560 imgName = "sketch-" + hex.EncodeToString(h[:6])
561
562 var curImgInitFilesHash string
563 if out, err := combinedOutput(ctx, "docker", "inspect", "--format", "{{json .Config.Labels}}", imgName); err != nil {
564 if strings.Contains(string(out), "No such object") {
565 // Image does not exist, continue and build it.
566 curImgInitFilesHash = ""
567 } else {
568 return "", fmt.Errorf("docker inspect failed: %s, %v", out, err)
569 }
570 } else {
571 m := map[string]string{}
572 if err := json.Unmarshal(bytes.TrimSpace(out), &m); err != nil {
573 return "", fmt.Errorf("docker inspect output unparsable: %s, %v", out, err)
574 }
575 curImgInitFilesHash = m["sketch_context"]
576 }
577
578 candidates, err := findRepoDockerfiles(cwd, gitRoot)
579 if err != nil {
580 return "", fmt.Errorf("find dockerfile: %w", err)
581 }
582
583 var initFiles map[string]string
584 var dockerfilePath string
585
586 // TODO: prefer a "Dockerfile.sketch" so users can tailor any env to this tool.
587 if len(candidates) == 1 && strings.ToLower(filepath.Base(candidates[0])) == "dockerfile" {
588 dockerfilePath = candidates[0]
589 contents, err := os.ReadFile(dockerfilePath)
590 if err != nil {
591 return "", err
592 }
593 fmt.Printf("using %s as dev env\n", candidates[0])
594 if hashInitFiles(map[string]string{dockerfilePath: string(contents)}) == curImgInitFilesHash && !forceRebuild {
Earl Lee2e463fb2025-04-17 11:22:22 -0700595 return imgName, nil
596 }
597 } else {
598 initFiles, err = readInitFiles(os.DirFS(gitRoot))
599 if err != nil {
600 return "", err
601 }
602 subPathWorkingDir, err := filepath.Rel(gitRoot, cwd)
603 if err != nil {
604 return "", err
605 }
606 initFileHash := hashInitFiles(initFiles)
607 if curImgInitFilesHash == initFileHash && !forceRebuild {
Earl Lee2e463fb2025-04-17 11:22:22 -0700608 return imgName, nil
609 }
610
611 start := time.Now()
612 dockerfile, err := createDockerfile(ctx, http.DefaultClient, antURL, antAPIKey, initFiles, subPathWorkingDir)
613 if err != nil {
614 return "", fmt.Errorf("create dockerfile: %w", err)
615 }
616 dockerfilePath = filepath.Join(cwd, "tmp-sketch-dockerfile")
617 if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0o666); err != nil {
618 return "", err
619 }
620 defer os.Remove(dockerfilePath)
621
622 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))
623 }
624
625 var gitUserEmail, gitUserName string
626 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.email"); err != nil {
627 return "", fmt.Errorf("git config: %s: %v", out, err)
628 } else {
629 gitUserEmail = strings.TrimSpace(string(out))
630 }
631 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.name"); err != nil {
632 return "", fmt.Errorf("git config: %s: %v", out, err)
633 } else {
634 gitUserName = strings.TrimSpace(string(out))
635 }
636
637 start := time.Now()
638 cmd := exec.CommandContext(ctx,
639 "docker", "build",
640 "-t", imgName,
641 "-f", dockerfilePath,
642 "--build-arg", "GIT_USER_EMAIL="+gitUserEmail,
643 "--build-arg", "GIT_USER_NAME="+gitUserName,
644 ".",
645 )
646 cmd.Dir = gitRoot
647 cmd.Stdout = stdout
648 cmd.Stderr = stderr
Philip Zeyligercc3ba222025-04-23 14:52:21 -0700649 fmt.Printf("building docker image %s... (use -verbose to see build output)\n", imgName)
Philip Zeyligere4fa0e32025-04-23 14:15:55 -0700650 dockerfileContent, err := os.ReadFile(dockerfilePath)
651 if err != nil {
652 return "", fmt.Errorf("failed to read Dockerfile: %w", err)
653 }
Philip Zeyliger5d6af872025-04-23 19:48:34 -0700654 fmt.Fprintf(stdout, "Dockerfile:\n%s\n", string(dockerfileContent))
Earl Lee2e463fb2025-04-17 11:22:22 -0700655
656 err = run(ctx, "docker build", cmd)
657 if err != nil {
658 return "", fmt.Errorf("docker build failed: %v", err)
659 }
660 fmt.Printf("built docker image %s in %s\n", imgName, time.Since(start).Round(time.Millisecond))
661 return imgName, nil
662}
663
664func findRepoDockerfiles(cwd, gitRoot string) ([]string, error) {
665 files, err := findDirDockerfiles(cwd)
666 if err != nil {
667 return nil, err
668 }
669 if len(files) > 0 {
670 return files, nil
671 }
672
673 path := cwd
674 for path != gitRoot {
675 path = filepath.Dir(path)
676 files, err := findDirDockerfiles(path)
677 if err != nil {
678 return nil, err
679 }
680 if len(files) > 0 {
681 return files, nil
682 }
683 }
684 return files, nil
685}
686
687// findDirDockerfiles finds all "Dockerfile*" files in a directory.
688func findDirDockerfiles(root string) (res []string, err error) {
689 err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
690 if err != nil {
691 return err
692 }
693 if info.IsDir() && root != path {
694 return filepath.SkipDir
695 }
696 name := strings.ToLower(info.Name())
697 if name == "dockerfile" || strings.HasPrefix(name, "dockerfile.") {
698 res = append(res, path)
699 }
700 return nil
701 })
702 if err != nil {
703 return nil, err
704 }
705 return res, nil
706}
707
708func findGitRoot(ctx context.Context, path string) (string, error) {
709 cmd := exec.CommandContext(ctx, "git", "rev-parse", "--git-common-dir")
710 cmd.Dir = path
711 out, err := cmd.CombinedOutput()
712 if err != nil {
713 if strings.Contains(string(out), "not a git repository") {
714 return "", fmt.Errorf(`sketch needs to run from within a git repo, but %s is not part of a git repo.
715Consider one of the following options:
716 - cd to a different dir that is already part of a git repo first, or
717 - to create a new git repo from this directory (%s), run this command:
718
719 git init . && git commit --allow-empty -m "initial commit"
720
721and try running sketch again.
722`, path, path)
723 }
724 return "", fmt.Errorf("git rev-parse --git-common-dir: %s: %w", out, err)
725 }
726 gitDir := strings.TrimSpace(string(out)) // location of .git dir, often as a relative path
727 absGitDir := filepath.Join(path, gitDir)
728 return filepath.Dir(absGitDir), err
729}
730
731func OpenBrowser(ctx context.Context, url string) {
732 var cmd *exec.Cmd
733 switch runtime.GOOS {
734 case "darwin":
735 cmd = exec.CommandContext(ctx, "open", url)
736 case "windows":
737 cmd = exec.CommandContext(ctx, "cmd", "/c", "start", url)
738 default: // Linux and other Unix-like systems
739 cmd = exec.CommandContext(ctx, "xdg-open", url)
740 }
741 if b, err := cmd.CombinedOutput(); err != nil {
742 fmt.Fprintf(os.Stderr, "failed to open browser: %v: %s\n", err, b)
743 }
744}
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700745
746// moveFile is like Python's shutil.move, in that it tries a rename, and, if that fails,
747// copies and deletes
748func moveFile(src, dst string) error {
749 if err := os.Rename(src, dst); err == nil {
750 return nil
751 }
752
753 stat, err := os.Stat(src)
754 if err != nil {
755 return err
756 }
757
758 sourceFile, err := os.Open(src)
759 if err != nil {
760 return err
761 }
762 defer sourceFile.Close()
763
764 destFile, err := os.Create(dst)
765 if err != nil {
766 return err
767 }
768 defer destFile.Close()
769
770 _, err = io.Copy(destFile, sourceFile)
771 if err != nil {
772 return err
773 }
774
775 sourceFile.Close()
776 destFile.Close()
777
778 os.Chmod(dst, stat.Mode())
779
780 return os.Remove(src)
781}