blob: f934c2f1ba5a80b4beda1b79308dfacd9b4543da [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001// Package dockerimg
2package dockerimg
3
4import (
Philip Zeyligerd0131312025-04-23 21:14:01 +00005 "bufio"
Earl Lee2e463fb2025-04-17 11:22:22 -07006 "bytes"
7 "context"
Philip Zeyliger5e227dd2025-04-21 15:55:29 -07008 "crypto/rand"
Earl Lee2e463fb2025-04-17 11:22:22 -07009 "crypto/sha256"
10 "encoding/hex"
11 "encoding/json"
12 "fmt"
13 "io"
14 "log/slog"
15 "net"
16 "net/http"
17 "os"
18 "os/exec"
19 "path/filepath"
20 "runtime"
21 "strings"
22 "time"
23
Sean McCulloughbaa2b592025-04-23 10:40:08 -070024 "sketch.dev/loop/server"
Earl Lee2e463fb2025-04-17 11:22:22 -070025 "sketch.dev/skribe"
Philip Zeyliger5d6af872025-04-23 19:48:34 -070026 "sketch.dev/webui"
Earl Lee2e463fb2025-04-17 11:22:22 -070027)
28
29// ContainerConfig holds all configuration for launching a container
30type ContainerConfig struct {
31 // SessionID is the unique identifier for this session
32 SessionID string
33
34 // LocalAddr is the initial address to use (though it may be overwritten later)
35 LocalAddr string
36
37 // SkabandAddr is the address of the skaband service if available
38 SkabandAddr string
39
40 // AntURL is the URL of the LLM service.
41 AntURL string
42
43 // AntAPIKey is the API key for LLM service.
44 AntAPIKey string
45
46 // Path is the local filesystem path to use
47 Path string
48
49 // GitUsername is the username to use for git operations
50 GitUsername string
51
52 // GitEmail is the email to use for git operations
53 GitEmail string
54
55 // OpenBrowser determines whether to open a browser automatically
56 OpenBrowser bool
57
58 // NoCleanup prevents container cleanup when set to true
59 NoCleanup bool
60
61 // ForceRebuild forces rebuilding of the Docker image even if it exists
62 ForceRebuild bool
63
64 // Host directory to copy container logs into, if not set to ""
65 ContainerLogDest string
66
67 // Path to pre-built linux sketch binary, or build a new one if set to ""
68 SketchBinaryLinux string
69
70 // Sketch client public key.
71 SketchPubKey string
Philip Zeyligerd1402952025-04-23 03:54:37 +000072
Sean McCulloughbaa2b592025-04-23 10:40:08 -070073 // Host port for the container's ssh server
74 SSHPort int
75
76 // Public keys authorized to connect to the container's ssh server
77 SSHAuthorizedKeys []byte
78
79 // Private key used to identify the container's ssh server
80 SSHServerIdentity []byte
81
Philip Zeyliger18532b22025-04-23 21:11:46 +000082 // Outside information to pass to the container
83 OutsideHostname string
84 OutsideOS string
85 OutsideWorkingDir string
Earl Lee2e463fb2025-04-17 11:22:22 -070086}
87
88// LaunchContainer creates a docker container for a project, installs sketch and opens a connection to it.
89// It writes status to stdout.
90func LaunchContainer(ctx context.Context, stdout, stderr io.Writer, config ContainerConfig) error {
91 if _, err := exec.LookPath("docker"); err != nil {
Philip Zeyliger5e227dd2025-04-21 15:55:29 -070092 if runtime.GOOS == "darwin" {
93 return fmt.Errorf("cannot find `docker` binary; run: brew install docker colima && colima start")
94 } else {
95 return fmt.Errorf("cannot find `docker` binary; install docker (e.g., apt-get install docker.io)")
96 }
Earl Lee2e463fb2025-04-17 11:22:22 -070097 }
98
99 if out, err := combinedOutput(ctx, "docker", "ps"); err != nil {
100 // `docker ps` provides a good error message here that can be
101 // easily chatgpt'ed by users, so send it to the user as-is:
102 // Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
103 return fmt.Errorf("docker ps: %s (%w)", out, err)
104 }
105
106 _, hostPort, err := net.SplitHostPort(config.LocalAddr)
107 if err != nil {
108 return err
109 }
110
111 gitRoot, err := findGitRoot(ctx, config.Path)
112 if err != nil {
113 return err
114 }
115
116 imgName, err := findOrBuildDockerImage(ctx, stdout, stderr, config.Path, gitRoot, config.AntURL, config.AntAPIKey, config.ForceRebuild)
117 if err != nil {
118 return err
119 }
120
121 linuxSketchBin := config.SketchBinaryLinux
122 if linuxSketchBin == "" {
123 linuxSketchBin, err = buildLinuxSketchBin(ctx, config.Path)
124 if err != nil {
125 return err
126 }
Josh Bleecher Snyder5544d142025-04-23 14:15:45 -0700127 defer os.Remove(linuxSketchBin) // in case of errors
Earl Lee2e463fb2025-04-17 11:22:22 -0700128 }
129
130 cntrName := imgName + "-" + config.SessionID
131 defer func() {
132 if config.NoCleanup {
133 return
134 }
135 if out, err := combinedOutput(ctx, "docker", "kill", cntrName); err != nil {
136 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
137 _ = out
138 }
139 if out, err := combinedOutput(ctx, "docker", "rm", cntrName); err != nil {
140 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
141 _ = out
142 }
143 }()
144
145 // errCh receives errors from operations that this function calls in separate goroutines.
146 errCh := make(chan error)
147
148 // Start the git server
149 gitSrv, err := newGitServer(gitRoot)
150 if err != nil {
151 return fmt.Errorf("failed to start git server: %w", err)
152 }
153 defer gitSrv.shutdown(ctx)
154
155 go func() {
156 errCh <- gitSrv.serve(ctx)
157 }()
158
159 // Get the current host git commit
160 var commit string
161 if out, err := combinedOutput(ctx, "git", "rev-parse", "HEAD"); err != nil {
162 return fmt.Errorf("git rev-parse HEAD: %w", err)
163 } else {
164 commit = strings.TrimSpace(string(out))
165 }
166 if out, err := combinedOutput(ctx, "git", "config", "http.receivepack", "true"); err != nil {
167 return fmt.Errorf("git config http.receivepack true: %s: %w", out, err)
168 }
169
170 relPath, err := filepath.Rel(gitRoot, config.Path)
171 if err != nil {
172 return err
173 }
174
175 // Create the sketch container
176 if err := createDockerContainer(ctx, cntrName, hostPort, relPath, imgName, config); err != nil {
177 return err
178 }
179
180 // Copy the sketch linux binary into the container
181 if out, err := combinedOutput(ctx, "docker", "cp", linuxSketchBin, cntrName+":/bin/sketch"); err != nil {
182 return fmt.Errorf("docker cp: %s, %w", out, err)
183 }
Josh Bleecher Snyder5544d142025-04-23 14:15:45 -0700184 os.Remove(linuxSketchBin) // in normal operations, the code below blocks, so actively delete now
Sean McCulloughf5bb3d32025-04-18 10:47:59 -0700185
186 // Make sure that the webui is built so we can copy the results to the container.
187 _, err = webui.Build()
188 if err != nil {
189 return fmt.Errorf("failed to build webui: %w", err)
190 }
191
David Crawshaw8bff16a2025-04-18 01:16:49 -0700192 webuiZipPath, err := webui.ZipPath()
193 if err != nil {
194 return err
195 }
196 if out, err := combinedOutput(ctx, "docker", "cp", webuiZipPath, cntrName+":/root/.cache/sketch/webui/"+filepath.Base(webuiZipPath)); err != nil {
197 return fmt.Errorf("docker cp: %s, %w", out, err)
198 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700199
200 fmt.Printf("starting container %s\ncommits made by the agent will be pushed to \033[1msketch/*\033[0m\n", cntrName)
201
202 // Start the sketch container
203 if out, err := combinedOutput(ctx, "docker", "start", cntrName); err != nil {
204 return fmt.Errorf("docker start: %s, %w", out, err)
205 }
206
207 // Copies structured logs from the container to the host.
208 copyLogs := func() {
209 if config.ContainerLogDest == "" {
210 return
211 }
212 out, err := combinedOutput(ctx, "docker", "logs", cntrName)
213 if err != nil {
214 fmt.Fprintf(os.Stderr, "docker logs failed: %v\n", err)
215 return
216 }
Philip Zeyligerd0131312025-04-23 21:14:01 +0000217 scanner := bufio.NewScanner(bytes.NewReader(out))
218 for scanner.Scan() {
219 logLine := scanner.Text()
Earl Lee2e463fb2025-04-17 11:22:22 -0700220 if !strings.HasPrefix(logLine, "structured logs:") {
221 continue
222 }
223 logFile := strings.TrimSpace(strings.TrimPrefix(logLine, "structured logs:"))
224 srcPath := fmt.Sprintf("%s:%s", cntrName, logFile)
225 logFileName := filepath.Base(logFile)
226 dstPath := filepath.Join(config.ContainerLogDest, logFileName)
227 _, err := combinedOutput(ctx, "docker", "cp", srcPath, dstPath)
228 if err != nil {
229 fmt.Fprintf(os.Stderr, "docker cp %s %s failed: %v\n", srcPath, dstPath, err)
230 }
231 fmt.Fprintf(os.Stderr, "\ncopied container log %s to %s\n", srcPath, dstPath)
232 }
233 }
234
235 // NOTE: we want to see what the internal sketch binary prints
236 // regardless of the setting of the verbosity flag on the external
237 // binary, so reading "docker logs", which is the stdout/stderr of
238 // the internal binary is not conditional on the verbose flag.
239 appendInternalErr := func(err error) error {
240 if err == nil {
241 return nil
242 }
243 out, logsErr := combinedOutput(ctx, "docker", "logs", cntrName)
Philip Zeyligerd1402952025-04-23 03:54:37 +0000244 if logsErr != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700245 return fmt.Errorf("%w; and docker logs failed: %s, %v", err, out, logsErr)
246 }
247 out = bytes.TrimSpace(out)
248 if len(out) > 0 {
249 return fmt.Errorf("docker logs: %s;\n%w", out, err)
250 }
251 return err
252 }
253
254 // Get the sketch server port from the container
Sean McCulloughae3480f2025-04-23 15:28:20 -0700255 localAddr, err := getContainerPort(ctx, cntrName, "80")
Earl Lee2e463fb2025-04-17 11:22:22 -0700256 if err != nil {
257 return appendInternalErr(err)
258 }
259
Sean McCulloughae3480f2025-04-23 15:28:20 -0700260 localSSHAddr, err := getContainerPort(ctx, cntrName, "22")
261 if err != nil {
262 return appendInternalErr(err)
263 }
264 sshHost, sshPort, err := net.SplitHostPort(localSSHAddr)
265 if err != nil {
266 fmt.Println("Error splitting ssh host and port:", err)
267 }
268 fmt.Printf("ssh into this container with: ssh root@%s -p %s\n", sshHost, sshPort)
269
Earl Lee2e463fb2025-04-17 11:22:22 -0700270 // Tell the sketch container which git server port and commit to initialize with.
271 go func() {
272 // TODO: Why is this called in a goroutine? I have found that when I pull this out
273 // of the goroutine and call it inline, then the terminal UI clears itself and all
274 // the scrollback (which is not good, but also not fatal). I can't see why it does this
275 // though, since none of the calls in postContainerInitConfig obviously write to stdout
276 // or stderr.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700277 if err := postContainerInitConfig(ctx, localAddr, commit, gitSrv.gitPort, gitSrv.pass, config.SSHServerIdentity, config.SSHAuthorizedKeys); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700278 slog.ErrorContext(ctx, "LaunchContainer.postContainerInitConfig", slog.String("err", err.Error()))
279 errCh <- appendInternalErr(err)
280 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700281
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700282 // We open the browser after the init config because the above waits for the web server to be serving.
283 if config.OpenBrowser {
284 if config.SkabandAddr != "" {
285 OpenBrowser(ctx, fmt.Sprintf("%s/s/%s", config.SkabandAddr, config.SessionID))
286 } else {
287 OpenBrowser(ctx, "http://"+localAddr)
288 }
289 }
290 }()
Earl Lee2e463fb2025-04-17 11:22:22 -0700291
292 go func() {
293 cmd := exec.CommandContext(ctx, "docker", "attach", cntrName)
294 cmd.Stdin = os.Stdin
295 cmd.Stdout = os.Stdout
296 cmd.Stderr = os.Stderr
297 errCh <- run(ctx, "docker attach", cmd)
298 }()
299
300 defer copyLogs()
301
302 for {
303 select {
304 case <-ctx.Done():
305 return ctx.Err()
306 case err := <-errCh:
307 if err != nil {
308 return appendInternalErr(fmt.Errorf("container process: %w", err))
309 }
310 return nil
311 }
312 }
313}
314
315func combinedOutput(ctx context.Context, cmdName string, args ...string) ([]byte, error) {
316 cmd := exec.CommandContext(ctx, cmdName, args...)
317 // Really only needed for the "go build" command for the linux sketch binary
318 cmd.Env = append(os.Environ(), "GOOS=linux", "CGO_ENABLED=0")
319 start := time.Now()
320
321 out, err := cmd.CombinedOutput()
322 if err != nil {
323 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))))
324 } else {
325 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))))
326 }
327 return out, err
328}
329
330func run(ctx context.Context, cmdName string, cmd *exec.Cmd) error {
331 start := time.Now()
332 err := cmd.Run()
333 if err != nil {
334 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))))
335 } else {
336 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))))
337 }
338 return err
339}
340
341type gitServer struct {
342 gitLn net.Listener
343 gitPort string
344 srv *http.Server
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700345 pass string
Earl Lee2e463fb2025-04-17 11:22:22 -0700346}
347
348func (gs *gitServer) shutdown(ctx context.Context) {
349 gs.srv.Shutdown(ctx)
350 gs.gitLn.Close()
351}
352
353// Serve a git remote from the host for the container to fetch from and push to.
354func (gs *gitServer) serve(ctx context.Context) error {
355 slog.DebugContext(ctx, "starting git server", slog.String("git_remote_addr", "http://host.docker.internal:"+gs.gitPort+"/.git"))
356 return gs.srv.Serve(gs.gitLn)
357}
358
359func newGitServer(gitRoot string) (*gitServer, error) {
Josh Bleecher Snyder9f6a9982025-04-22 17:34:15 -0700360 ret := &gitServer{
361 pass: rand.Text(),
362 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700363
Earl Lee2e463fb2025-04-17 11:22:22 -0700364 gitLn, err := net.Listen("tcp4", ":0")
365 if err != nil {
366 return nil, fmt.Errorf("git listen: %w", err)
367 }
368 ret.gitLn = gitLn
369
370 srv := http.Server{
Josh Bleecher Snyder9f6a9982025-04-22 17:34:15 -0700371 Handler: &gitHTTP{gitRepoRoot: gitRoot, pass: []byte(ret.pass)},
Earl Lee2e463fb2025-04-17 11:22:22 -0700372 }
373 ret.srv = &srv
374
375 _, gitPort, err := net.SplitHostPort(gitLn.Addr().String())
376 if err != nil {
377 return nil, fmt.Errorf("git port: %w", err)
378 }
379 ret.gitPort = gitPort
380 return ret, nil
381}
382
383func createDockerContainer(ctx context.Context, cntrName, hostPort, relPath, imgName string, config ContainerConfig) error {
384 //, config.SessionID, config.GitUsername, config.GitEmail, config.SkabandAddr
385 // sessionID, gitUsername, gitEmail, skabandAddr string
David Crawshaw69c67312025-04-17 13:42:00 -0700386 cmdArgs := []string{
387 "create",
Earl Lee2e463fb2025-04-17 11:22:22 -0700388 "-it",
389 "--name", cntrName,
390 "-p", hostPort + ":80", // forward container port 80 to a host port
391 "-e", "ANTHROPIC_API_KEY=" + config.AntAPIKey,
392 }
393 if config.AntURL != "" {
394 cmdArgs = append(cmdArgs, "-e", "ANT_URL="+config.AntURL)
395 }
396 if config.SketchPubKey != "" {
397 cmdArgs = append(cmdArgs, "-e", "SKETCH_PUB_KEY="+config.SketchPubKey)
398 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700399 if config.SSHPort > 0 {
400 cmdArgs = append(cmdArgs, "-p", fmt.Sprintf("%d:22", config.SSHPort)) // forward container ssh port to host ssh port
401 } else {
402 cmdArgs = append(cmdArgs, "-p", "22") // use an ephemeral host port for ssh.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700403 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700404 if relPath != "." {
405 cmdArgs = append(cmdArgs, "-w", "/app/"+relPath)
406 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700407 // colima does this by default, but Linux docker seems to need this set explicitly
408 cmdArgs = append(cmdArgs, "--add-host", "host.docker.internal:host-gateway")
Earl Lee2e463fb2025-04-17 11:22:22 -0700409 cmdArgs = append(
410 cmdArgs,
411 imgName,
412 "/bin/sketch",
413 "-unsafe",
414 "-addr=:80",
415 "-session-id="+config.SessionID,
Philip Zeyligerd1402952025-04-23 03:54:37 +0000416 "-git-username="+config.GitUsername,
417 "-git-email="+config.GitEmail,
Philip Zeyliger18532b22025-04-23 21:11:46 +0000418 "-outside-hostname="+config.OutsideHostname,
419 "-outside-os="+config.OutsideOS,
420 "-outside-working-dir="+config.OutsideWorkingDir,
Earl Lee2e463fb2025-04-17 11:22:22 -0700421 )
422 if config.SkabandAddr != "" {
423 cmdArgs = append(cmdArgs, "-skaband-addr="+config.SkabandAddr)
424 }
425 if out, err := combinedOutput(ctx, "docker", cmdArgs...); err != nil {
426 return fmt.Errorf("docker create: %s, %w", out, err)
427 }
428 return nil
429}
430
431func buildLinuxSketchBin(ctx context.Context, path string) (string, error) {
David Crawshaw8a617cb2025-04-18 01:28:43 -0700432 homeDir, err := os.UserHomeDir()
David Crawshaw69c67312025-04-17 13:42:00 -0700433 if err != nil {
434 return "", err
435 }
David Crawshaw8a617cb2025-04-18 01:28:43 -0700436 linuxGopath := filepath.Join(homeDir, ".cache", "sketch", "linuxgo")
437 if err := os.MkdirAll(linuxGopath, 0o777); err != nil {
438 return "", err
439 }
440
441 verToInstall := "@latest"
442 if out, err := exec.Command("go", "list", "-m").CombinedOutput(); err != nil {
443 return "", fmt.Errorf("failed to run go list -m: %s: %v", out, err)
444 } else {
445 if strings.TrimSpace(string(out)) == "sketch.dev" {
446 fmt.Printf("building linux agent from currently checked out module\n")
447 verToInstall = ""
448 }
449 }
David Crawshaw69c67312025-04-17 13:42:00 -0700450
Earl Lee2e463fb2025-04-17 11:22:22 -0700451 start := time.Now()
David Crawshaw8a617cb2025-04-18 01:28:43 -0700452 cmd := exec.CommandContext(ctx, "go", "install", "sketch.dev/cmd/sketch"+verToInstall)
David Crawshawb9eaef52025-04-17 15:23:18 -0700453 cmd.Env = append(
454 os.Environ(),
455 "GOOS=linux",
456 "CGO_ENABLED=0",
457 "GOTOOLCHAIN=auto",
David Crawshaw8a617cb2025-04-18 01:28:43 -0700458 "GOPATH="+linuxGopath,
Josh Bleecher Snyderfae17572025-04-21 11:48:05 -0700459 "GOBIN=",
David Crawshawb9eaef52025-04-17 15:23:18 -0700460 )
Earl Lee2e463fb2025-04-17 11:22:22 -0700461
462 fmt.Printf("building linux agent binary...\n")
463 out, err := cmd.CombinedOutput()
464 if err != nil {
465 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))))
466 return "", fmt.Errorf("failed to build linux sketch binary: %s: %w", out, err)
467 } else {
468 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))))
469 }
470
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700471 var src string
472 if runtime.GOOS != "linux" {
473 src = filepath.Join(linuxGopath, "bin", "linux_"+runtime.GOARCH, "sketch")
474 } else {
475 // If we are already on Linux, there's no extra platform name in the path
476 src = filepath.Join(linuxGopath, "bin", "sketch")
477 }
478
David Crawshaw69c67312025-04-17 13:42:00 -0700479 dst := filepath.Join(path, "tmp-sketch-binary-linux")
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700480 if err := moveFile(src, dst); err != nil {
David Crawshaw69c67312025-04-17 13:42:00 -0700481 return "", err
482 }
483
Earl Lee2e463fb2025-04-17 11:22:22 -0700484 fmt.Printf("built linux agent binary in %s\n", time.Since(start).Round(100*time.Millisecond))
485
David Crawshaw69c67312025-04-17 13:42:00 -0700486 return dst, nil
Earl Lee2e463fb2025-04-17 11:22:22 -0700487}
488
Sean McCulloughae3480f2025-04-23 15:28:20 -0700489func getContainerPort(ctx context.Context, cntrName, cntrPort string) (string, error) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700490 localAddr := ""
Sean McCulloughae3480f2025-04-23 15:28:20 -0700491 if out, err := combinedOutput(ctx, "docker", "port", cntrName, cntrPort); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700492 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
493 } else {
494 v4, _, found := strings.Cut(string(out), "\n")
495 if !found {
496 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
497 }
498 localAddr = v4
499 if strings.HasPrefix(localAddr, "0.0.0.0") {
500 localAddr = "127.0.0.1" + strings.TrimPrefix(localAddr, "0.0.0.0")
501 }
502 }
503 return localAddr, nil
504}
505
506// Contact the container and configure it.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700507func postContainerInitConfig(ctx context.Context, localAddr, commit, gitPort, gitPass string, sshServerIdentity, sshAuthorizedKeys []byte) error {
Earl Lee2e463fb2025-04-17 11:22:22 -0700508 localURL := "http://" + localAddr
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700509
510 initMsg, err := json.Marshal(
511 server.InitRequest{
512 Commit: commit,
513 GitRemoteAddr: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s/.git", gitPass, gitPort),
514 HostAddr: localAddr,
515 SSHAuthorizedKeys: sshAuthorizedKeys,
516 SSHServerIdentity: sshServerIdentity,
517 })
Earl Lee2e463fb2025-04-17 11:22:22 -0700518 if err != nil {
519 return fmt.Errorf("init msg: %w", err)
520 }
521
Earl Lee2e463fb2025-04-17 11:22:22 -0700522 // Note: this /init POST is handled in loop/server/loophttp.go:
523 initMsgByteReader := bytes.NewReader(initMsg)
524 req, err := http.NewRequest("POST", localURL+"/init", initMsgByteReader)
525 if err != nil {
526 return err
527 }
528
529 var res *http.Response
530 for i := 0; ; i++ {
531 time.Sleep(100 * time.Millisecond)
532 // If you DON'T reset this byteReader, then subsequent retries may end up sending 0 bytes.
533 initMsgByteReader.Reset(initMsg)
534 res, err = http.DefaultClient.Do(req)
535 if err != nil {
536 // In addition to "connection refused", we also occasionally see "EOF" errors that can succeed on retries.
537 if i < 100 && (strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "EOF")) {
538 slog.DebugContext(ctx, "postContainerInitConfig retrying", slog.Int("retry", i), slog.String("err", err.Error()))
539 continue
540 }
541 return fmt.Errorf("failed to %s/init sketch in container, NOT retrying: err: %v", localURL, err)
542 }
543 break
544 }
545 resBytes, _ := io.ReadAll(res.Body)
546 if res.StatusCode != http.StatusOK {
547 return fmt.Errorf("failed to initialize sketch in container, response status code %d: %s", res.StatusCode, resBytes)
548 }
549 return nil
550}
551
552func findOrBuildDockerImage(ctx context.Context, stdout, stderr io.Writer, cwd, gitRoot, antURL, antAPIKey string, forceRebuild bool) (imgName string, err error) {
553 h := sha256.Sum256([]byte(gitRoot))
554 imgName = "sketch-" + hex.EncodeToString(h[:6])
555
556 var curImgInitFilesHash string
557 if out, err := combinedOutput(ctx, "docker", "inspect", "--format", "{{json .Config.Labels}}", imgName); err != nil {
558 if strings.Contains(string(out), "No such object") {
559 // Image does not exist, continue and build it.
560 curImgInitFilesHash = ""
561 } else {
562 return "", fmt.Errorf("docker inspect failed: %s, %v", out, err)
563 }
564 } else {
565 m := map[string]string{}
566 if err := json.Unmarshal(bytes.TrimSpace(out), &m); err != nil {
567 return "", fmt.Errorf("docker inspect output unparsable: %s, %v", out, err)
568 }
569 curImgInitFilesHash = m["sketch_context"]
570 }
571
572 candidates, err := findRepoDockerfiles(cwd, gitRoot)
573 if err != nil {
574 return "", fmt.Errorf("find dockerfile: %w", err)
575 }
576
577 var initFiles map[string]string
578 var dockerfilePath string
579
580 // TODO: prefer a "Dockerfile.sketch" so users can tailor any env to this tool.
581 if len(candidates) == 1 && strings.ToLower(filepath.Base(candidates[0])) == "dockerfile" {
582 dockerfilePath = candidates[0]
583 contents, err := os.ReadFile(dockerfilePath)
584 if err != nil {
585 return "", err
586 }
587 fmt.Printf("using %s as dev env\n", candidates[0])
588 if hashInitFiles(map[string]string{dockerfilePath: string(contents)}) == curImgInitFilesHash && !forceRebuild {
589 fmt.Printf("using existing docker image %s\n", imgName)
590 return imgName, nil
591 }
592 } else {
593 initFiles, err = readInitFiles(os.DirFS(gitRoot))
594 if err != nil {
595 return "", err
596 }
597 subPathWorkingDir, err := filepath.Rel(gitRoot, cwd)
598 if err != nil {
599 return "", err
600 }
601 initFileHash := hashInitFiles(initFiles)
602 if curImgInitFilesHash == initFileHash && !forceRebuild {
603 fmt.Printf("using existing docker image %s\n", imgName)
604 return imgName, nil
605 }
606
607 start := time.Now()
608 dockerfile, err := createDockerfile(ctx, http.DefaultClient, antURL, antAPIKey, initFiles, subPathWorkingDir)
609 if err != nil {
610 return "", fmt.Errorf("create dockerfile: %w", err)
611 }
612 dockerfilePath = filepath.Join(cwd, "tmp-sketch-dockerfile")
613 if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0o666); err != nil {
614 return "", err
615 }
616 defer os.Remove(dockerfilePath)
617
618 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))
619 }
620
621 var gitUserEmail, gitUserName string
622 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.email"); err != nil {
623 return "", fmt.Errorf("git config: %s: %v", out, err)
624 } else {
625 gitUserEmail = strings.TrimSpace(string(out))
626 }
627 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.name"); err != nil {
628 return "", fmt.Errorf("git config: %s: %v", out, err)
629 } else {
630 gitUserName = strings.TrimSpace(string(out))
631 }
632
633 start := time.Now()
634 cmd := exec.CommandContext(ctx,
635 "docker", "build",
636 "-t", imgName,
637 "-f", dockerfilePath,
638 "--build-arg", "GIT_USER_EMAIL="+gitUserEmail,
639 "--build-arg", "GIT_USER_NAME="+gitUserName,
640 ".",
641 )
642 cmd.Dir = gitRoot
643 cmd.Stdout = stdout
644 cmd.Stderr = stderr
Philip Zeyligercc3ba222025-04-23 14:52:21 -0700645 fmt.Printf("building docker image %s... (use -verbose to see build output)\n", imgName)
Philip Zeyligere4fa0e32025-04-23 14:15:55 -0700646 dockerfileContent, err := os.ReadFile(dockerfilePath)
647 if err != nil {
648 return "", fmt.Errorf("failed to read Dockerfile: %w", err)
649 }
Philip Zeyliger5d6af872025-04-23 19:48:34 -0700650 fmt.Fprintf(stdout, "Dockerfile:\n%s\n", string(dockerfileContent))
Earl Lee2e463fb2025-04-17 11:22:22 -0700651
652 err = run(ctx, "docker build", cmd)
653 if err != nil {
654 return "", fmt.Errorf("docker build failed: %v", err)
655 }
656 fmt.Printf("built docker image %s in %s\n", imgName, time.Since(start).Round(time.Millisecond))
657 return imgName, nil
658}
659
660func findRepoDockerfiles(cwd, gitRoot string) ([]string, error) {
661 files, err := findDirDockerfiles(cwd)
662 if err != nil {
663 return nil, err
664 }
665 if len(files) > 0 {
666 return files, nil
667 }
668
669 path := cwd
670 for path != gitRoot {
671 path = filepath.Dir(path)
672 files, err := findDirDockerfiles(path)
673 if err != nil {
674 return nil, err
675 }
676 if len(files) > 0 {
677 return files, nil
678 }
679 }
680 return files, nil
681}
682
683// findDirDockerfiles finds all "Dockerfile*" files in a directory.
684func findDirDockerfiles(root string) (res []string, err error) {
685 err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
686 if err != nil {
687 return err
688 }
689 if info.IsDir() && root != path {
690 return filepath.SkipDir
691 }
692 name := strings.ToLower(info.Name())
693 if name == "dockerfile" || strings.HasPrefix(name, "dockerfile.") {
694 res = append(res, path)
695 }
696 return nil
697 })
698 if err != nil {
699 return nil, err
700 }
701 return res, nil
702}
703
704func findGitRoot(ctx context.Context, path string) (string, error) {
705 cmd := exec.CommandContext(ctx, "git", "rev-parse", "--git-common-dir")
706 cmd.Dir = path
707 out, err := cmd.CombinedOutput()
708 if err != nil {
709 if strings.Contains(string(out), "not a git repository") {
710 return "", fmt.Errorf(`sketch needs to run from within a git repo, but %s is not part of a git repo.
711Consider one of the following options:
712 - cd to a different dir that is already part of a git repo first, or
713 - to create a new git repo from this directory (%s), run this command:
714
715 git init . && git commit --allow-empty -m "initial commit"
716
717and try running sketch again.
718`, path, path)
719 }
720 return "", fmt.Errorf("git rev-parse --git-common-dir: %s: %w", out, err)
721 }
722 gitDir := strings.TrimSpace(string(out)) // location of .git dir, often as a relative path
723 absGitDir := filepath.Join(path, gitDir)
724 return filepath.Dir(absGitDir), err
725}
726
727func OpenBrowser(ctx context.Context, url string) {
728 var cmd *exec.Cmd
729 switch runtime.GOOS {
730 case "darwin":
731 cmd = exec.CommandContext(ctx, "open", url)
732 case "windows":
733 cmd = exec.CommandContext(ctx, "cmd", "/c", "start", url)
734 default: // Linux and other Unix-like systems
735 cmd = exec.CommandContext(ctx, "xdg-open", url)
736 }
737 if b, err := cmd.CombinedOutput(); err != nil {
738 fmt.Fprintf(os.Stderr, "failed to open browser: %v: %s\n", err, b)
739 }
740}
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700741
742// moveFile is like Python's shutil.move, in that it tries a rename, and, if that fails,
743// copies and deletes
744func moveFile(src, dst string) error {
745 if err := os.Rename(src, dst); err == nil {
746 return nil
747 }
748
749 stat, err := os.Stat(src)
750 if err != nil {
751 return err
752 }
753
754 sourceFile, err := os.Open(src)
755 if err != nil {
756 return err
757 }
758 defer sourceFile.Close()
759
760 destFile, err := os.Create(dst)
761 if err != nil {
762 return err
763 }
764 defer destFile.Close()
765
766 _, err = io.Copy(destFile, sourceFile)
767 if err != nil {
768 return err
769 }
770
771 sourceFile.Close()
772 destFile.Close()
773
774 os.Chmod(dst, stat.Mode())
775
776 return os.Remove(src)
777}