blob: 66f1654e307447487f90ba7772eaa6ecd28dac2a [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001// Package dockerimg
2package dockerimg
3
4import (
5 "bytes"
6 "context"
Philip Zeyliger5e227dd2025-04-21 15:55:29 -07007 "crypto/rand"
Earl Lee2e463fb2025-04-17 11:22:22 -07008 "crypto/sha256"
9 "encoding/hex"
10 "encoding/json"
11 "fmt"
12 "io"
13 "log/slog"
14 "net"
15 "net/http"
16 "os"
17 "os/exec"
18 "path/filepath"
19 "runtime"
20 "strings"
21 "time"
22
Josh Bleecher Snyder78707d62025-04-30 21:06:49 +000023 "sketch.dev/browser"
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070024 "sketch.dev/llm/ant"
Sean McCulloughbaa2b592025-04-23 10:40:08 -070025 "sketch.dev/loop/server"
Earl Lee2e463fb2025-04-17 11:22:22 -070026 "sketch.dev/skribe"
Philip Zeyliger5d6af872025-04-23 19:48:34 -070027 "sketch.dev/webui"
Earl Lee2e463fb2025-04-17 11:22:22 -070028)
29
30// ContainerConfig holds all configuration for launching a container
31type ContainerConfig struct {
32 // SessionID is the unique identifier for this session
33 SessionID string
34
35 // LocalAddr is the initial address to use (though it may be overwritten later)
36 LocalAddr string
37
38 // SkabandAddr is the address of the skaband service if available
39 SkabandAddr string
40
41 // AntURL is the URL of the LLM service.
42 AntURL string
43
44 // AntAPIKey is the API key for LLM service.
45 AntAPIKey string
46
47 // Path is the local filesystem path to use
48 Path string
49
50 // GitUsername is the username to use for git operations
51 GitUsername string
52
53 // GitEmail is the email to use for git operations
54 GitEmail string
55
56 // OpenBrowser determines whether to open a browser automatically
57 OpenBrowser bool
58
59 // NoCleanup prevents container cleanup when set to true
60 NoCleanup bool
61
62 // ForceRebuild forces rebuilding of the Docker image even if it exists
63 ForceRebuild bool
64
65 // Host directory to copy container logs into, if not set to ""
66 ContainerLogDest string
67
68 // Path to pre-built linux sketch binary, or build a new one if set to ""
69 SketchBinaryLinux string
70
71 // Sketch client public key.
72 SketchPubKey string
Philip Zeyligerd1402952025-04-23 03:54:37 +000073
Sean McCulloughbaa2b592025-04-23 10:40:08 -070074 // Host port for the container's ssh server
75 SSHPort int
76
Philip Zeyliger18532b22025-04-23 21:11:46 +000077 // Outside information to pass to the container
78 OutsideHostname string
79 OutsideOS string
80 OutsideWorkingDir string
Philip Zeyligerb74c4f62025-04-25 19:18:49 -070081
Pokey Rule0dcebe12025-04-28 14:51:04 +010082 // If true, exit after the first turn
83 OneShot bool
84
85 // Initial prompt
86 Prompt string
Philip Zeyliger1b47aa22025-04-28 19:25:38 +000087
88 // Initial commit to use as starting point
89 InitialCommit string
David Crawshawb5f6a002025-05-05 08:27:16 -070090
91 // Verbose enables verbose output
92 Verbose bool
Earl Lee2e463fb2025-04-17 11:22:22 -070093}
94
95// LaunchContainer creates a docker container for a project, installs sketch and opens a connection to it.
96// It writes status to stdout.
David Crawshawb5f6a002025-05-05 08:27:16 -070097func LaunchContainer(ctx context.Context, config ContainerConfig) error {
Earl Lee2e463fb2025-04-17 11:22:22 -070098 if _, err := exec.LookPath("docker"); err != nil {
Philip Zeyliger5e227dd2025-04-21 15:55:29 -070099 if runtime.GOOS == "darwin" {
100 return fmt.Errorf("cannot find `docker` binary; run: brew install docker colima && colima start")
101 } else {
102 return fmt.Errorf("cannot find `docker` binary; install docker (e.g., apt-get install docker.io)")
103 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700104 }
105
106 if out, err := combinedOutput(ctx, "docker", "ps"); err != nil {
107 // `docker ps` provides a good error message here that can be
108 // easily chatgpt'ed by users, so send it to the user as-is:
109 // Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
110 return fmt.Errorf("docker ps: %s (%w)", out, err)
111 }
112
113 _, hostPort, err := net.SplitHostPort(config.LocalAddr)
114 if err != nil {
115 return err
116 }
117
118 gitRoot, err := findGitRoot(ctx, config.Path)
119 if err != nil {
120 return err
121 }
122
David Crawshawb5f6a002025-05-05 08:27:16 -0700123 imgName, err := findOrBuildDockerImage(ctx, config.Path, gitRoot, config.AntURL, config.AntAPIKey, config.ForceRebuild, config.Verbose)
Earl Lee2e463fb2025-04-17 11:22:22 -0700124 if err != nil {
125 return err
126 }
127
128 linuxSketchBin := config.SketchBinaryLinux
129 if linuxSketchBin == "" {
David Crawshawb5f6a002025-05-05 08:27:16 -0700130 linuxSketchBin, err = buildLinuxSketchBin(ctx)
Earl Lee2e463fb2025-04-17 11:22:22 -0700131 if err != nil {
132 return err
133 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700134 }
135
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000136 cntrName := "sketch-" + config.SessionID
Earl Lee2e463fb2025-04-17 11:22:22 -0700137 defer func() {
138 if config.NoCleanup {
139 return
140 }
141 if out, err := combinedOutput(ctx, "docker", "kill", cntrName); err != nil {
142 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
143 _ = out
144 }
145 if out, err := combinedOutput(ctx, "docker", "rm", cntrName); err != nil {
146 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
147 _ = out
148 }
149 }()
150
151 // errCh receives errors from operations that this function calls in separate goroutines.
152 errCh := make(chan error)
153
154 // Start the git server
155 gitSrv, err := newGitServer(gitRoot)
156 if err != nil {
157 return fmt.Errorf("failed to start git server: %w", err)
158 }
159 defer gitSrv.shutdown(ctx)
160
161 go func() {
162 errCh <- gitSrv.serve(ctx)
163 }()
164
165 // Get the current host git commit
166 var commit string
Philip Zeyliger1b47aa22025-04-28 19:25:38 +0000167 if out, err := combinedOutput(ctx, "git", "rev-parse", config.InitialCommit); err != nil {
168 return fmt.Errorf("git rev-parse %s: %w", config.InitialCommit, err)
Earl Lee2e463fb2025-04-17 11:22:22 -0700169 } else {
170 commit = strings.TrimSpace(string(out))
171 }
172 if out, err := combinedOutput(ctx, "git", "config", "http.receivepack", "true"); err != nil {
173 return fmt.Errorf("git config http.receivepack true: %s: %w", out, err)
174 }
175
176 relPath, err := filepath.Rel(gitRoot, config.Path)
177 if err != nil {
178 return err
179 }
180
181 // Create the sketch container
182 if err := createDockerContainer(ctx, cntrName, hostPort, relPath, imgName, config); err != nil {
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000183 return fmt.Errorf("failed to create docker container: %w", err)
Earl Lee2e463fb2025-04-17 11:22:22 -0700184 }
185
186 // Copy the sketch linux binary into the container
187 if out, err := combinedOutput(ctx, "docker", "cp", linuxSketchBin, cntrName+":/bin/sketch"); err != nil {
188 return fmt.Errorf("docker cp: %s, %w", out, err)
189 }
Sean McCulloughf5bb3d32025-04-18 10:47:59 -0700190
191 // Make sure that the webui is built so we can copy the results to the container.
192 _, err = webui.Build()
193 if err != nil {
194 return fmt.Errorf("failed to build webui: %w", err)
195 }
196
David Crawshaw8bff16a2025-04-18 01:16:49 -0700197 webuiZipPath, err := webui.ZipPath()
198 if err != nil {
199 return err
200 }
201 if out, err := combinedOutput(ctx, "docker", "cp", webuiZipPath, cntrName+":/root/.cache/sketch/webui/"+filepath.Base(webuiZipPath)); err != nil {
202 return fmt.Errorf("docker cp: %s, %w", out, err)
203 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700204
David Crawshaw53786ef2025-04-24 12:52:51 -0700205 fmt.Printf("📦 running in container %s\n", cntrName)
Earl Lee2e463fb2025-04-17 11:22:22 -0700206
207 // Start the sketch container
208 if out, err := combinedOutput(ctx, "docker", "start", cntrName); err != nil {
209 return fmt.Errorf("docker start: %s, %w", out, err)
210 }
211
212 // Copies structured logs from the container to the host.
213 copyLogs := func() {
214 if config.ContainerLogDest == "" {
215 return
216 }
217 out, err := combinedOutput(ctx, "docker", "logs", cntrName)
218 if err != nil {
219 fmt.Fprintf(os.Stderr, "docker logs failed: %v\n", err)
220 return
221 }
Josh Bleecher Snyder7660e4e2025-04-24 10:34:17 -0700222 prefix := []byte("structured logs:")
223 for line := range bytes.Lines(out) {
224 rest, ok := bytes.CutPrefix(line, prefix)
225 if !ok {
Earl Lee2e463fb2025-04-17 11:22:22 -0700226 continue
227 }
Josh Bleecher Snyder7660e4e2025-04-24 10:34:17 -0700228 logFile := string(bytes.TrimSpace(rest))
Earl Lee2e463fb2025-04-17 11:22:22 -0700229 srcPath := fmt.Sprintf("%s:%s", cntrName, logFile)
230 logFileName := filepath.Base(logFile)
231 dstPath := filepath.Join(config.ContainerLogDest, logFileName)
232 _, err := combinedOutput(ctx, "docker", "cp", srcPath, dstPath)
233 if err != nil {
234 fmt.Fprintf(os.Stderr, "docker cp %s %s failed: %v\n", srcPath, dstPath, err)
235 }
236 fmt.Fprintf(os.Stderr, "\ncopied container log %s to %s\n", srcPath, dstPath)
237 }
238 }
239
240 // NOTE: we want to see what the internal sketch binary prints
241 // regardless of the setting of the verbosity flag on the external
242 // binary, so reading "docker logs", which is the stdout/stderr of
243 // the internal binary is not conditional on the verbose flag.
244 appendInternalErr := func(err error) error {
245 if err == nil {
246 return nil
247 }
248 out, logsErr := combinedOutput(ctx, "docker", "logs", cntrName)
Philip Zeyligerd1402952025-04-23 03:54:37 +0000249 if logsErr != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700250 return fmt.Errorf("%w; and docker logs failed: %s, %v", err, out, logsErr)
251 }
252 out = bytes.TrimSpace(out)
253 if len(out) > 0 {
254 return fmt.Errorf("docker logs: %s;\n%w", out, err)
255 }
256 return err
257 }
258
259 // Get the sketch server port from the container
Sean McCulloughae3480f2025-04-23 15:28:20 -0700260 localAddr, err := getContainerPort(ctx, cntrName, "80")
Earl Lee2e463fb2025-04-17 11:22:22 -0700261 if err != nil {
262 return appendInternalErr(err)
263 }
264
Sean McCulloughae3480f2025-04-23 15:28:20 -0700265 localSSHAddr, err := getContainerPort(ctx, cntrName, "22")
266 if err != nil {
267 return appendInternalErr(err)
268 }
269 sshHost, sshPort, err := net.SplitHostPort(localSSHAddr)
270 if err != nil {
David Crawshawb5f6a002025-05-05 08:27:16 -0700271 return appendInternalErr(fmt.Errorf("failed to split ssh host and port: %w", err))
Sean McCulloughae3480f2025-04-23 15:28:20 -0700272 }
Sean McCullough4854c652025-04-24 18:37:02 -0700273
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700274 var sshServerIdentity, sshUserIdentity []byte
Sean McCullough4854c652025-04-24 18:37:02 -0700275
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700276 if err := CheckForInclude(); err != nil {
277 fmt.Println(err.Error())
278 // continue - ssh config is not required for the rest of sketch to function locally.
279 } else {
280 cst, err := NewSSHTheather(cntrName, sshHost, sshPort)
281 if err != nil {
282 return appendInternalErr(fmt.Errorf("NewContainerSSHTheather: %w", err))
283 }
284
Sean McCulloughea3fc202025-04-28 12:53:37 -0700285 // Note: The vscode: link uses an undocumented request parameter that I really had to dig to find:
286 // https://github.com/microsoft/vscode/blob/2b9486161abaca59b5132ce3c59544f3cc7000f6/src/vs/code/electron-main/app.ts#L878
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700287 fmt.Printf(`Connect to this container via any of these methods:
Sean McCullough4854c652025-04-24 18:37:02 -0700288🖥️ ssh %s
289🖥️ code --remote ssh-remote+root@%s /app -n
Sean McCulloughea3fc202025-04-28 12:53:37 -0700290🔗 vscode://vscode-remote/ssh-remote+root@%s/app?windowId=_blank
Sean McCullough4854c652025-04-24 18:37:02 -0700291`, cntrName, cntrName, cntrName)
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700292 sshUserIdentity = cst.userIdentity
293 sshServerIdentity = cst.serverIdentity
294 defer func() {
295 if err := cst.Cleanup(); err != nil {
296 appendInternalErr(err)
297 }
298 }()
299 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700300
Earl Lee2e463fb2025-04-17 11:22:22 -0700301 // Tell the sketch container which git server port and commit to initialize with.
302 go func() {
303 // TODO: Why is this called in a goroutine? I have found that when I pull this out
304 // of the goroutine and call it inline, then the terminal UI clears itself and all
305 // the scrollback (which is not good, but also not fatal). I can't see why it does this
306 // though, since none of the calls in postContainerInitConfig obviously write to stdout
307 // or stderr.
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700308 if err := postContainerInitConfig(ctx, localAddr, commit, gitSrv.gitPort, gitSrv.pass, sshServerIdentity, sshUserIdentity); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700309 slog.ErrorContext(ctx, "LaunchContainer.postContainerInitConfig", slog.String("err", err.Error()))
310 errCh <- appendInternalErr(err)
311 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700312
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700313 // We open the browser after the init config because the above waits for the web server to be serving.
314 if config.OpenBrowser {
315 if config.SkabandAddr != "" {
Josh Bleecher Snydere54b00a2025-04-30 16:48:02 -0700316 browser.Open(fmt.Sprintf("%s/s/%s", config.SkabandAddr, config.SessionID))
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700317 } else {
Josh Bleecher Snydere54b00a2025-04-30 16:48:02 -0700318 browser.Open("http://" + localAddr)
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700319 }
320 }
321 }()
Earl Lee2e463fb2025-04-17 11:22:22 -0700322
323 go func() {
324 cmd := exec.CommandContext(ctx, "docker", "attach", cntrName)
325 cmd.Stdin = os.Stdin
326 cmd.Stdout = os.Stdout
327 cmd.Stderr = os.Stderr
328 errCh <- run(ctx, "docker attach", cmd)
329 }()
330
331 defer copyLogs()
332
333 for {
334 select {
335 case <-ctx.Done():
336 return ctx.Err()
337 case err := <-errCh:
338 if err != nil {
339 return appendInternalErr(fmt.Errorf("container process: %w", err))
340 }
341 return nil
342 }
343 }
344}
345
346func combinedOutput(ctx context.Context, cmdName string, args ...string) ([]byte, error) {
347 cmd := exec.CommandContext(ctx, cmdName, args...)
Earl Lee2e463fb2025-04-17 11:22:22 -0700348 start := time.Now()
349
350 out, err := cmd.CombinedOutput()
351 if err != nil {
David Crawshawc7e77962025-05-03 13:20:18 -0700352 slog.ErrorContext(ctx, cmdName, slog.Duration("elapsed", time.Since(start)), slog.String("err", err.Error()), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
Earl Lee2e463fb2025-04-17 11:22:22 -0700353 } else {
David Crawshawc7e77962025-05-03 13:20:18 -0700354 slog.DebugContext(ctx, cmdName, slog.Duration("elapsed", time.Since(start)), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
Earl Lee2e463fb2025-04-17 11:22:22 -0700355 }
356 return out, err
357}
358
359func run(ctx context.Context, cmdName string, cmd *exec.Cmd) error {
360 start := time.Now()
361 err := cmd.Run()
362 if err != nil {
David Crawshawc7e77962025-05-03 13:20:18 -0700363 slog.ErrorContext(ctx, cmdName, slog.Duration("elapsed", time.Since(start)), slog.String("err", err.Error()), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
Earl Lee2e463fb2025-04-17 11:22:22 -0700364 } else {
David Crawshawc7e77962025-05-03 13:20:18 -0700365 slog.DebugContext(ctx, cmdName, slog.Duration("elapsed", time.Since(start)), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
Earl Lee2e463fb2025-04-17 11:22:22 -0700366 }
367 return err
368}
369
370type gitServer struct {
371 gitLn net.Listener
372 gitPort string
373 srv *http.Server
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700374 pass string
Earl Lee2e463fb2025-04-17 11:22:22 -0700375}
376
377func (gs *gitServer) shutdown(ctx context.Context) {
378 gs.srv.Shutdown(ctx)
379 gs.gitLn.Close()
380}
381
382// Serve a git remote from the host for the container to fetch from and push to.
383func (gs *gitServer) serve(ctx context.Context) error {
384 slog.DebugContext(ctx, "starting git server", slog.String("git_remote_addr", "http://host.docker.internal:"+gs.gitPort+"/.git"))
385 return gs.srv.Serve(gs.gitLn)
386}
387
388func newGitServer(gitRoot string) (*gitServer, error) {
Josh Bleecher Snyder9f6a9982025-04-22 17:34:15 -0700389 ret := &gitServer{
390 pass: rand.Text(),
391 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700392
Earl Lee2e463fb2025-04-17 11:22:22 -0700393 gitLn, err := net.Listen("tcp4", ":0")
394 if err != nil {
395 return nil, fmt.Errorf("git listen: %w", err)
396 }
397 ret.gitLn = gitLn
398
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000399 browserC := make(chan string, 1) // channel of URLs to open in browser
400 go func() {
401 for url := range browserC {
402 browser.Open(url)
403 }
404 }()
405
406 srv := http.Server{Handler: &gitHTTP{gitRepoRoot: gitRoot, pass: []byte(ret.pass), browserC: browserC}}
Earl Lee2e463fb2025-04-17 11:22:22 -0700407 ret.srv = &srv
408
409 _, gitPort, err := net.SplitHostPort(gitLn.Addr().String())
410 if err != nil {
411 return nil, fmt.Errorf("git port: %w", err)
412 }
413 ret.gitPort = gitPort
414 return ret, nil
415}
416
417func createDockerContainer(ctx context.Context, cntrName, hostPort, relPath, imgName string, config ContainerConfig) error {
David Crawshaw69c67312025-04-17 13:42:00 -0700418 cmdArgs := []string{
419 "create",
Earl Lee2e463fb2025-04-17 11:22:22 -0700420 "-it",
421 "--name", cntrName,
422 "-p", hostPort + ":80", // forward container port 80 to a host port
Philip Zeyliger6234a8d2025-05-02 14:31:20 -0700423 "-e", "SKETCH_ANTHROPIC_API_KEY=" + config.AntAPIKey,
Earl Lee2e463fb2025-04-17 11:22:22 -0700424 }
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000425
426 for _, envVar := range getEnvForwardingFromGitConfig(ctx) {
427 cmdArgs = append(cmdArgs, "-e", envVar)
428 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700429 if config.AntURL != "" {
Philip Zeyliger6234a8d2025-05-02 14:31:20 -0700430 cmdArgs = append(cmdArgs, "-e", "SKETCH_ANT_URL="+config.AntURL)
Earl Lee2e463fb2025-04-17 11:22:22 -0700431 }
432 if config.SketchPubKey != "" {
433 cmdArgs = append(cmdArgs, "-e", "SKETCH_PUB_KEY="+config.SketchPubKey)
434 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700435 if config.SSHPort > 0 {
436 cmdArgs = append(cmdArgs, "-p", fmt.Sprintf("%d:22", config.SSHPort)) // forward container ssh port to host ssh port
437 } else {
438 cmdArgs = append(cmdArgs, "-p", "22") // use an ephemeral host port for ssh.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700439 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700440 if relPath != "." {
441 cmdArgs = append(cmdArgs, "-w", "/app/"+relPath)
442 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700443 // colima does this by default, but Linux docker seems to need this set explicitly
444 cmdArgs = append(cmdArgs, "--add-host", "host.docker.internal:host-gateway")
Earl Lee2e463fb2025-04-17 11:22:22 -0700445 cmdArgs = append(
446 cmdArgs,
447 imgName,
448 "/bin/sketch",
449 "-unsafe",
450 "-addr=:80",
451 "-session-id="+config.SessionID,
Philip Zeyligerd1402952025-04-23 03:54:37 +0000452 "-git-username="+config.GitUsername,
453 "-git-email="+config.GitEmail,
Philip Zeyliger18532b22025-04-23 21:11:46 +0000454 "-outside-hostname="+config.OutsideHostname,
455 "-outside-os="+config.OutsideOS,
456 "-outside-working-dir="+config.OutsideWorkingDir,
Josh Bleecher Snyder3cae7d92025-04-30 09:54:29 -0700457 "-open=false",
Earl Lee2e463fb2025-04-17 11:22:22 -0700458 )
459 if config.SkabandAddr != "" {
460 cmdArgs = append(cmdArgs, "-skaband-addr="+config.SkabandAddr)
461 }
Pokey Rule0dcebe12025-04-28 14:51:04 +0100462 if config.Prompt != "" {
463 cmdArgs = append(cmdArgs, "-prompt", config.Prompt)
464 }
465 if config.OneShot {
466 cmdArgs = append(cmdArgs, "-one-shot")
Philip Zeyligerb74c4f62025-04-25 19:18:49 -0700467 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700468 if out, err := combinedOutput(ctx, "docker", cmdArgs...); err != nil {
469 return fmt.Errorf("docker create: %s, %w", out, err)
470 }
471 return nil
472}
473
David Crawshawb5f6a002025-05-05 08:27:16 -0700474func buildLinuxSketchBin(ctx context.Context) (string, error) {
David Crawshaw8a617cb2025-04-18 01:28:43 -0700475 homeDir, err := os.UserHomeDir()
David Crawshaw69c67312025-04-17 13:42:00 -0700476 if err != nil {
477 return "", err
478 }
David Crawshaw8a617cb2025-04-18 01:28:43 -0700479 linuxGopath := filepath.Join(homeDir, ".cache", "sketch", "linuxgo")
480 if err := os.MkdirAll(linuxGopath, 0o777); err != nil {
481 return "", err
482 }
483
484 verToInstall := "@latest"
485 if out, err := exec.Command("go", "list", "-m").CombinedOutput(); err != nil {
486 return "", fmt.Errorf("failed to run go list -m: %s: %v", out, err)
487 } else {
488 if strings.TrimSpace(string(out)) == "sketch.dev" {
David Crawshaw094e4d22025-04-24 11:35:14 -0700489 slog.DebugContext(ctx, "built linux agent from currently checked out module")
David Crawshaw8a617cb2025-04-18 01:28:43 -0700490 verToInstall = ""
491 }
492 }
David Crawshaw69c67312025-04-17 13:42:00 -0700493
Earl Lee2e463fb2025-04-17 11:22:22 -0700494 start := time.Now()
David Crawshaw8a617cb2025-04-18 01:28:43 -0700495 cmd := exec.CommandContext(ctx, "go", "install", "sketch.dev/cmd/sketch"+verToInstall)
David Crawshawb9eaef52025-04-17 15:23:18 -0700496 cmd.Env = append(
497 os.Environ(),
498 "GOOS=linux",
499 "CGO_ENABLED=0",
500 "GOTOOLCHAIN=auto",
David Crawshaw8a617cb2025-04-18 01:28:43 -0700501 "GOPATH="+linuxGopath,
Josh Bleecher Snyderfae17572025-04-21 11:48:05 -0700502 "GOBIN=",
David Crawshawb9eaef52025-04-17 15:23:18 -0700503 )
Earl Lee2e463fb2025-04-17 11:22:22 -0700504
Earl Lee2e463fb2025-04-17 11:22:22 -0700505 out, err := cmd.CombinedOutput()
506 if err != nil {
David Crawshawc7e77962025-05-03 13:20:18 -0700507 slog.ErrorContext(ctx, "go", slog.Duration("elapsed", time.Since(start)), slog.String("err", err.Error()), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
Earl Lee2e463fb2025-04-17 11:22:22 -0700508 return "", fmt.Errorf("failed to build linux sketch binary: %s: %w", out, err)
509 } else {
David Crawshawc7e77962025-05-03 13:20:18 -0700510 slog.DebugContext(ctx, "go", slog.Duration("elapsed", time.Since(start)), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
Earl Lee2e463fb2025-04-17 11:22:22 -0700511 }
512
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700513 if runtime.GOOS != "linux" {
David Crawshawc7e77962025-05-03 13:20:18 -0700514 return filepath.Join(linuxGopath, "bin", "linux_"+runtime.GOARCH, "sketch"), nil
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700515 }
David Crawshawc7e77962025-05-03 13:20:18 -0700516 // If we are already on Linux, there's no extra platform name in the path
517 return filepath.Join(linuxGopath, "bin", "sketch"), nil
Earl Lee2e463fb2025-04-17 11:22:22 -0700518}
519
Sean McCulloughae3480f2025-04-23 15:28:20 -0700520func getContainerPort(ctx context.Context, cntrName, cntrPort string) (string, error) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700521 localAddr := ""
Sean McCulloughae3480f2025-04-23 15:28:20 -0700522 if out, err := combinedOutput(ctx, "docker", "port", cntrName, cntrPort); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700523 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
524 } else {
525 v4, _, found := strings.Cut(string(out), "\n")
526 if !found {
527 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
528 }
529 localAddr = v4
530 if strings.HasPrefix(localAddr, "0.0.0.0") {
531 localAddr = "127.0.0.1" + strings.TrimPrefix(localAddr, "0.0.0.0")
532 }
533 }
534 return localAddr, nil
535}
536
537// Contact the container and configure it.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700538func postContainerInitConfig(ctx context.Context, localAddr, commit, gitPort, gitPass string, sshServerIdentity, sshAuthorizedKeys []byte) error {
Earl Lee2e463fb2025-04-17 11:22:22 -0700539 localURL := "http://" + localAddr
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700540
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000541 // Check if SSH is available by checking for the Include directive in ~/.ssh/config
542 sshAvailable := true
543 sshError := ""
544 if err := CheckForInclude(); err != nil {
545 sshAvailable = false
546 sshError = err.Error()
547 }
548
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700549 initMsg, err := json.Marshal(
550 server.InitRequest{
551 Commit: commit,
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000552 OutsideHTTP: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s", gitPass, gitPort),
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700553 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 {
David Crawshaw99231ba2025-05-03 10:48:26 -0700578 if i < 100 {
579 if i%10 == 0 {
580 slog.DebugContext(ctx, "postContainerInitConfig retrying", slog.Int("retry", i), slog.String("err", err.Error()))
581 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700582 continue
583 }
584 return fmt.Errorf("failed to %s/init sketch in container, NOT retrying: err: %v", localURL, err)
585 }
586 break
587 }
588 resBytes, _ := io.ReadAll(res.Body)
589 if res.StatusCode != http.StatusOK {
590 return fmt.Errorf("failed to initialize sketch in container, response status code %d: %s", res.StatusCode, resBytes)
591 }
592 return nil
593}
594
David Crawshawb5f6a002025-05-05 08:27:16 -0700595func findOrBuildDockerImage(ctx context.Context, cwd, gitRoot, antURL, antAPIKey string, forceRebuild, verbose bool) (imgName string, err error) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700596 h := sha256.Sum256([]byte(gitRoot))
597 imgName = "sketch-" + hex.EncodeToString(h[:6])
598
599 var curImgInitFilesHash string
600 if out, err := combinedOutput(ctx, "docker", "inspect", "--format", "{{json .Config.Labels}}", imgName); err != nil {
601 if strings.Contains(string(out), "No such object") {
602 // Image does not exist, continue and build it.
603 curImgInitFilesHash = ""
604 } else {
605 return "", fmt.Errorf("docker inspect failed: %s, %v", out, err)
606 }
607 } else {
608 m := map[string]string{}
609 if err := json.Unmarshal(bytes.TrimSpace(out), &m); err != nil {
610 return "", fmt.Errorf("docker inspect output unparsable: %s, %v", out, err)
611 }
612 curImgInitFilesHash = m["sketch_context"]
613 }
614
615 candidates, err := findRepoDockerfiles(cwd, gitRoot)
616 if err != nil {
617 return "", fmt.Errorf("find dockerfile: %w", err)
618 }
619
620 var initFiles map[string]string
621 var dockerfilePath string
622
623 // TODO: prefer a "Dockerfile.sketch" so users can tailor any env to this tool.
624 if len(candidates) == 1 && strings.ToLower(filepath.Base(candidates[0])) == "dockerfile" {
625 dockerfilePath = candidates[0]
626 contents, err := os.ReadFile(dockerfilePath)
627 if err != nil {
628 return "", err
629 }
630 fmt.Printf("using %s as dev env\n", candidates[0])
631 if hashInitFiles(map[string]string{dockerfilePath: string(contents)}) == curImgInitFilesHash && !forceRebuild {
Earl Lee2e463fb2025-04-17 11:22:22 -0700632 return imgName, nil
633 }
634 } else {
635 initFiles, err = readInitFiles(os.DirFS(gitRoot))
636 if err != nil {
637 return "", err
638 }
639 subPathWorkingDir, err := filepath.Rel(gitRoot, cwd)
640 if err != nil {
641 return "", err
642 }
643 initFileHash := hashInitFiles(initFiles)
644 if curImgInitFilesHash == initFileHash && !forceRebuild {
Earl Lee2e463fb2025-04-17 11:22:22 -0700645 return imgName, nil
646 }
647
648 start := time.Now()
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700649 srv := &ant.Service{
650 URL: antURL,
651 APIKey: antAPIKey,
652 HTTPC: http.DefaultClient,
653 }
654 dockerfile, err := createDockerfile(ctx, srv, initFiles, subPathWorkingDir)
Earl Lee2e463fb2025-04-17 11:22:22 -0700655 if err != nil {
656 return "", fmt.Errorf("create dockerfile: %w", err)
657 }
658 dockerfilePath = filepath.Join(cwd, "tmp-sketch-dockerfile")
659 if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0o666); err != nil {
660 return "", err
661 }
662 defer os.Remove(dockerfilePath)
663
David Crawshawb5f6a002025-05-05 08:27:16 -0700664 if verbose {
665 fmt.Fprintf(os.Stderr, "generated Dockerfile in %s:\n\t%s\n\n", time.Since(start).Round(time.Millisecond), strings.Replace(dockerfile, "\n", "\n\t", -1))
666 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700667 }
668
669 var gitUserEmail, gitUserName string
670 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.email"); err != nil {
671 return "", fmt.Errorf("git config: %s: %v", out, err)
672 } else {
673 gitUserEmail = strings.TrimSpace(string(out))
674 }
675 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.name"); err != nil {
676 return "", fmt.Errorf("git config: %s: %v", out, err)
677 } else {
678 gitUserName = strings.TrimSpace(string(out))
679 }
680
681 start := time.Now()
682 cmd := exec.CommandContext(ctx,
683 "docker", "build",
684 "-t", imgName,
685 "-f", dockerfilePath,
686 "--build-arg", "GIT_USER_EMAIL="+gitUserEmail,
687 "--build-arg", "GIT_USER_NAME="+gitUserName,
Earl Lee2e463fb2025-04-17 11:22:22 -0700688 )
David Crawshawb5f6a002025-05-05 08:27:16 -0700689 if !verbose {
690 cmd.Args = append(cmd.Args, "--progress=quiet")
Philip Zeyligere4fa0e32025-04-23 14:15:55 -0700691 }
David Crawshawb5f6a002025-05-05 08:27:16 -0700692 cmd.Args = append(cmd.Args, ".")
693 cmd.Dir = gitRoot
694 cmd.Stdout = os.Stdout
695 cmd.Stderr = os.Stderr
696 fmt.Printf("🏗️ building docker image %s... (use -verbose to see build output)\n", imgName)
Earl Lee2e463fb2025-04-17 11:22:22 -0700697
698 err = run(ctx, "docker build", cmd)
699 if err != nil {
700 return "", fmt.Errorf("docker build failed: %v", err)
701 }
702 fmt.Printf("built docker image %s in %s\n", imgName, time.Since(start).Round(time.Millisecond))
703 return imgName, nil
704}
705
706func findRepoDockerfiles(cwd, gitRoot string) ([]string, error) {
707 files, err := findDirDockerfiles(cwd)
708 if err != nil {
709 return nil, err
710 }
711 if len(files) > 0 {
712 return files, nil
713 }
714
715 path := cwd
716 for path != gitRoot {
717 path = filepath.Dir(path)
718 files, err := findDirDockerfiles(path)
719 if err != nil {
720 return nil, err
721 }
722 if len(files) > 0 {
723 return files, nil
724 }
725 }
726 return files, nil
727}
728
729// findDirDockerfiles finds all "Dockerfile*" files in a directory.
730func findDirDockerfiles(root string) (res []string, err error) {
731 err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
732 if err != nil {
733 return err
734 }
735 if info.IsDir() && root != path {
736 return filepath.SkipDir
737 }
738 name := strings.ToLower(info.Name())
739 if name == "dockerfile" || strings.HasPrefix(name, "dockerfile.") {
740 res = append(res, path)
741 }
742 return nil
743 })
744 if err != nil {
745 return nil, err
746 }
747 return res, nil
748}
749
750func findGitRoot(ctx context.Context, path string) (string, error) {
751 cmd := exec.CommandContext(ctx, "git", "rev-parse", "--git-common-dir")
752 cmd.Dir = path
753 out, err := cmd.CombinedOutput()
754 if err != nil {
755 if strings.Contains(string(out), "not a git repository") {
756 return "", fmt.Errorf(`sketch needs to run from within a git repo, but %s is not part of a git repo.
757Consider one of the following options:
758 - cd to a different dir that is already part of a git repo first, or
759 - to create a new git repo from this directory (%s), run this command:
760
761 git init . && git commit --allow-empty -m "initial commit"
762
763and try running sketch again.
764`, path, path)
765 }
766 return "", fmt.Errorf("git rev-parse --git-common-dir: %s: %w", out, err)
767 }
768 gitDir := strings.TrimSpace(string(out)) // location of .git dir, often as a relative path
769 absGitDir := filepath.Join(path, gitDir)
770 return filepath.Dir(absGitDir), err
771}
772
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000773// getEnvForwardingFromGitConfig retrieves environment variables to pass through to Docker
774// from git config using the sketch.envfwd multi-valued key.
775func getEnvForwardingFromGitConfig(ctx context.Context) []string {
776 outb, err := exec.CommandContext(ctx, "git", "config", "--get-all", "sketch.envfwd").CombinedOutput()
777 out := string(outb)
778 if err != nil {
779 if strings.Contains(out, "key does not exist") {
780 return nil
781 }
782 slog.ErrorContext(ctx, "failed to get sketch.envfwd from git config", "err", err, "output", out)
783 return nil
784 }
785
786 var envVars []string
787 for envVar := range strings.Lines(out) {
788 envVar = strings.TrimSpace(envVar)
789 if envVar == "" {
790 continue
791 }
792 envVars = append(envVars, envVar+"="+os.Getenv(envVar))
793 }
794 return envVars
795}