blob: f2e0f25a3c29d7468156388e07e9f4fe652ab761 [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",
David Crawshaw66cf74e2025-05-05 08:48:39 -0700420 "-i",
Earl Lee2e463fb2025-04-17 11:22:22 -0700421 "--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 }
David Crawshaw66cf74e2025-05-05 08:48:39 -0700425 if !config.OneShot {
426 cmdArgs = append(cmdArgs, "-t")
427 }
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000428
429 for _, envVar := range getEnvForwardingFromGitConfig(ctx) {
430 cmdArgs = append(cmdArgs, "-e", envVar)
431 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700432 if config.AntURL != "" {
Philip Zeyliger6234a8d2025-05-02 14:31:20 -0700433 cmdArgs = append(cmdArgs, "-e", "SKETCH_ANT_URL="+config.AntURL)
Earl Lee2e463fb2025-04-17 11:22:22 -0700434 }
435 if config.SketchPubKey != "" {
436 cmdArgs = append(cmdArgs, "-e", "SKETCH_PUB_KEY="+config.SketchPubKey)
437 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700438 if config.SSHPort > 0 {
439 cmdArgs = append(cmdArgs, "-p", fmt.Sprintf("%d:22", config.SSHPort)) // forward container ssh port to host ssh port
440 } else {
441 cmdArgs = append(cmdArgs, "-p", "22") // use an ephemeral host port for ssh.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700442 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700443 if relPath != "." {
444 cmdArgs = append(cmdArgs, "-w", "/app/"+relPath)
445 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700446 // colima does this by default, but Linux docker seems to need this set explicitly
447 cmdArgs = append(cmdArgs, "--add-host", "host.docker.internal:host-gateway")
Earl Lee2e463fb2025-04-17 11:22:22 -0700448 cmdArgs = append(
449 cmdArgs,
450 imgName,
451 "/bin/sketch",
452 "-unsafe",
453 "-addr=:80",
454 "-session-id="+config.SessionID,
Philip Zeyligerd1402952025-04-23 03:54:37 +0000455 "-git-username="+config.GitUsername,
456 "-git-email="+config.GitEmail,
Philip Zeyliger18532b22025-04-23 21:11:46 +0000457 "-outside-hostname="+config.OutsideHostname,
458 "-outside-os="+config.OutsideOS,
459 "-outside-working-dir="+config.OutsideWorkingDir,
Josh Bleecher Snyder3cae7d92025-04-30 09:54:29 -0700460 "-open=false",
Earl Lee2e463fb2025-04-17 11:22:22 -0700461 )
462 if config.SkabandAddr != "" {
463 cmdArgs = append(cmdArgs, "-skaband-addr="+config.SkabandAddr)
464 }
Pokey Rule0dcebe12025-04-28 14:51:04 +0100465 if config.Prompt != "" {
466 cmdArgs = append(cmdArgs, "-prompt", config.Prompt)
467 }
468 if config.OneShot {
469 cmdArgs = append(cmdArgs, "-one-shot")
Philip Zeyligerb74c4f62025-04-25 19:18:49 -0700470 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700471 if out, err := combinedOutput(ctx, "docker", cmdArgs...); err != nil {
472 return fmt.Errorf("docker create: %s, %w", out, err)
473 }
474 return nil
475}
476
David Crawshawb5f6a002025-05-05 08:27:16 -0700477func buildLinuxSketchBin(ctx context.Context) (string, error) {
David Crawshaw8a617cb2025-04-18 01:28:43 -0700478 homeDir, err := os.UserHomeDir()
David Crawshaw69c67312025-04-17 13:42:00 -0700479 if err != nil {
480 return "", err
481 }
David Crawshaw8a617cb2025-04-18 01:28:43 -0700482 linuxGopath := filepath.Join(homeDir, ".cache", "sketch", "linuxgo")
483 if err := os.MkdirAll(linuxGopath, 0o777); err != nil {
484 return "", err
485 }
486
487 verToInstall := "@latest"
488 if out, err := exec.Command("go", "list", "-m").CombinedOutput(); err != nil {
489 return "", fmt.Errorf("failed to run go list -m: %s: %v", out, err)
490 } else {
491 if strings.TrimSpace(string(out)) == "sketch.dev" {
David Crawshaw094e4d22025-04-24 11:35:14 -0700492 slog.DebugContext(ctx, "built linux agent from currently checked out module")
David Crawshaw8a617cb2025-04-18 01:28:43 -0700493 verToInstall = ""
494 }
495 }
David Crawshaw69c67312025-04-17 13:42:00 -0700496
Earl Lee2e463fb2025-04-17 11:22:22 -0700497 start := time.Now()
David Crawshaw8a617cb2025-04-18 01:28:43 -0700498 cmd := exec.CommandContext(ctx, "go", "install", "sketch.dev/cmd/sketch"+verToInstall)
David Crawshawb9eaef52025-04-17 15:23:18 -0700499 cmd.Env = append(
500 os.Environ(),
501 "GOOS=linux",
502 "CGO_ENABLED=0",
503 "GOTOOLCHAIN=auto",
David Crawshaw8a617cb2025-04-18 01:28:43 -0700504 "GOPATH="+linuxGopath,
Josh Bleecher Snyderfae17572025-04-21 11:48:05 -0700505 "GOBIN=",
David Crawshawb9eaef52025-04-17 15:23:18 -0700506 )
Earl Lee2e463fb2025-04-17 11:22:22 -0700507
Earl Lee2e463fb2025-04-17 11:22:22 -0700508 out, err := cmd.CombinedOutput()
509 if err != nil {
David Crawshawc7e77962025-05-03 13:20:18 -0700510 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 -0700511 return "", fmt.Errorf("failed to build linux sketch binary: %s: %w", out, err)
512 } else {
David Crawshawc7e77962025-05-03 13:20:18 -0700513 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 -0700514 }
515
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700516 if runtime.GOOS != "linux" {
David Crawshawc7e77962025-05-03 13:20:18 -0700517 return filepath.Join(linuxGopath, "bin", "linux_"+runtime.GOARCH, "sketch"), nil
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700518 }
David Crawshawc7e77962025-05-03 13:20:18 -0700519 // If we are already on Linux, there's no extra platform name in the path
520 return filepath.Join(linuxGopath, "bin", "sketch"), nil
Earl Lee2e463fb2025-04-17 11:22:22 -0700521}
522
Sean McCulloughae3480f2025-04-23 15:28:20 -0700523func getContainerPort(ctx context.Context, cntrName, cntrPort string) (string, error) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700524 localAddr := ""
Sean McCulloughae3480f2025-04-23 15:28:20 -0700525 if out, err := combinedOutput(ctx, "docker", "port", cntrName, cntrPort); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700526 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
527 } else {
528 v4, _, found := strings.Cut(string(out), "\n")
529 if !found {
530 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
531 }
532 localAddr = v4
533 if strings.HasPrefix(localAddr, "0.0.0.0") {
534 localAddr = "127.0.0.1" + strings.TrimPrefix(localAddr, "0.0.0.0")
535 }
536 }
537 return localAddr, nil
538}
539
540// Contact the container and configure it.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700541func postContainerInitConfig(ctx context.Context, localAddr, commit, gitPort, gitPass string, sshServerIdentity, sshAuthorizedKeys []byte) error {
Earl Lee2e463fb2025-04-17 11:22:22 -0700542 localURL := "http://" + localAddr
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700543
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000544 // Check if SSH is available by checking for the Include directive in ~/.ssh/config
545 sshAvailable := true
546 sshError := ""
547 if err := CheckForInclude(); err != nil {
548 sshAvailable = false
549 sshError = err.Error()
550 }
551
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700552 initMsg, err := json.Marshal(
553 server.InitRequest{
554 Commit: commit,
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000555 OutsideHTTP: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s", gitPass, gitPort),
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700556 GitRemoteAddr: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s/.git", gitPass, gitPort),
557 HostAddr: localAddr,
558 SSHAuthorizedKeys: sshAuthorizedKeys,
559 SSHServerIdentity: sshServerIdentity,
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000560 SSHAvailable: sshAvailable,
561 SSHError: sshError,
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700562 })
Earl Lee2e463fb2025-04-17 11:22:22 -0700563 if err != nil {
564 return fmt.Errorf("init msg: %w", err)
565 }
566
Earl Lee2e463fb2025-04-17 11:22:22 -0700567 // Note: this /init POST is handled in loop/server/loophttp.go:
568 initMsgByteReader := bytes.NewReader(initMsg)
569 req, err := http.NewRequest("POST", localURL+"/init", initMsgByteReader)
570 if err != nil {
571 return err
572 }
573
574 var res *http.Response
575 for i := 0; ; i++ {
576 time.Sleep(100 * time.Millisecond)
577 // If you DON'T reset this byteReader, then subsequent retries may end up sending 0 bytes.
578 initMsgByteReader.Reset(initMsg)
579 res, err = http.DefaultClient.Do(req)
580 if err != nil {
David Crawshaw99231ba2025-05-03 10:48:26 -0700581 if i < 100 {
582 if i%10 == 0 {
583 slog.DebugContext(ctx, "postContainerInitConfig retrying", slog.Int("retry", i), slog.String("err", err.Error()))
584 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700585 continue
586 }
587 return fmt.Errorf("failed to %s/init sketch in container, NOT retrying: err: %v", localURL, err)
588 }
589 break
590 }
591 resBytes, _ := io.ReadAll(res.Body)
592 if res.StatusCode != http.StatusOK {
593 return fmt.Errorf("failed to initialize sketch in container, response status code %d: %s", res.StatusCode, resBytes)
594 }
595 return nil
596}
597
David Crawshawb5f6a002025-05-05 08:27:16 -0700598func findOrBuildDockerImage(ctx context.Context, cwd, gitRoot, antURL, antAPIKey string, forceRebuild, verbose bool) (imgName string, err error) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700599 h := sha256.Sum256([]byte(gitRoot))
600 imgName = "sketch-" + hex.EncodeToString(h[:6])
601
602 var curImgInitFilesHash string
603 if out, err := combinedOutput(ctx, "docker", "inspect", "--format", "{{json .Config.Labels}}", imgName); err != nil {
604 if strings.Contains(string(out), "No such object") {
605 // Image does not exist, continue and build it.
606 curImgInitFilesHash = ""
607 } else {
608 return "", fmt.Errorf("docker inspect failed: %s, %v", out, err)
609 }
610 } else {
611 m := map[string]string{}
612 if err := json.Unmarshal(bytes.TrimSpace(out), &m); err != nil {
613 return "", fmt.Errorf("docker inspect output unparsable: %s, %v", out, err)
614 }
615 curImgInitFilesHash = m["sketch_context"]
616 }
617
618 candidates, err := findRepoDockerfiles(cwd, gitRoot)
619 if err != nil {
620 return "", fmt.Errorf("find dockerfile: %w", err)
621 }
622
623 var initFiles map[string]string
624 var dockerfilePath string
625
626 // TODO: prefer a "Dockerfile.sketch" so users can tailor any env to this tool.
627 if len(candidates) == 1 && strings.ToLower(filepath.Base(candidates[0])) == "dockerfile" {
628 dockerfilePath = candidates[0]
629 contents, err := os.ReadFile(dockerfilePath)
630 if err != nil {
631 return "", err
632 }
633 fmt.Printf("using %s as dev env\n", candidates[0])
634 if hashInitFiles(map[string]string{dockerfilePath: string(contents)}) == curImgInitFilesHash && !forceRebuild {
Earl Lee2e463fb2025-04-17 11:22:22 -0700635 return imgName, nil
636 }
637 } else {
638 initFiles, err = readInitFiles(os.DirFS(gitRoot))
639 if err != nil {
640 return "", err
641 }
642 subPathWorkingDir, err := filepath.Rel(gitRoot, cwd)
643 if err != nil {
644 return "", err
645 }
646 initFileHash := hashInitFiles(initFiles)
647 if curImgInitFilesHash == initFileHash && !forceRebuild {
Earl Lee2e463fb2025-04-17 11:22:22 -0700648 return imgName, nil
649 }
650
651 start := time.Now()
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700652 srv := &ant.Service{
653 URL: antURL,
654 APIKey: antAPIKey,
655 HTTPC: http.DefaultClient,
656 }
657 dockerfile, err := createDockerfile(ctx, srv, initFiles, subPathWorkingDir)
Earl Lee2e463fb2025-04-17 11:22:22 -0700658 if err != nil {
659 return "", fmt.Errorf("create dockerfile: %w", err)
660 }
661 dockerfilePath = filepath.Join(cwd, "tmp-sketch-dockerfile")
662 if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0o666); err != nil {
663 return "", err
664 }
665 defer os.Remove(dockerfilePath)
666
David Crawshawb5f6a002025-05-05 08:27:16 -0700667 if verbose {
668 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))
669 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700670 }
671
672 var gitUserEmail, gitUserName string
673 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.email"); err != nil {
674 return "", fmt.Errorf("git config: %s: %v", out, err)
675 } else {
676 gitUserEmail = strings.TrimSpace(string(out))
677 }
678 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.name"); err != nil {
679 return "", fmt.Errorf("git config: %s: %v", out, err)
680 } else {
681 gitUserName = strings.TrimSpace(string(out))
682 }
683
684 start := time.Now()
685 cmd := exec.CommandContext(ctx,
686 "docker", "build",
687 "-t", imgName,
688 "-f", dockerfilePath,
689 "--build-arg", "GIT_USER_EMAIL="+gitUserEmail,
690 "--build-arg", "GIT_USER_NAME="+gitUserName,
Earl Lee2e463fb2025-04-17 11:22:22 -0700691 )
David Crawshawb5f6a002025-05-05 08:27:16 -0700692 if !verbose {
693 cmd.Args = append(cmd.Args, "--progress=quiet")
Philip Zeyligere4fa0e32025-04-23 14:15:55 -0700694 }
David Crawshawb5f6a002025-05-05 08:27:16 -0700695 cmd.Args = append(cmd.Args, ".")
696 cmd.Dir = gitRoot
697 cmd.Stdout = os.Stdout
698 cmd.Stderr = os.Stderr
699 fmt.Printf("🏗️ building docker image %s... (use -verbose to see build output)\n", imgName)
Earl Lee2e463fb2025-04-17 11:22:22 -0700700
701 err = run(ctx, "docker build", cmd)
702 if err != nil {
703 return "", fmt.Errorf("docker build failed: %v", err)
704 }
705 fmt.Printf("built docker image %s in %s\n", imgName, time.Since(start).Round(time.Millisecond))
706 return imgName, nil
707}
708
709func findRepoDockerfiles(cwd, gitRoot string) ([]string, error) {
710 files, err := findDirDockerfiles(cwd)
711 if err != nil {
712 return nil, err
713 }
714 if len(files) > 0 {
715 return files, nil
716 }
717
718 path := cwd
719 for path != gitRoot {
720 path = filepath.Dir(path)
721 files, err := findDirDockerfiles(path)
722 if err != nil {
723 return nil, err
724 }
725 if len(files) > 0 {
726 return files, nil
727 }
728 }
729 return files, nil
730}
731
732// findDirDockerfiles finds all "Dockerfile*" files in a directory.
733func findDirDockerfiles(root string) (res []string, err error) {
734 err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
735 if err != nil {
736 return err
737 }
738 if info.IsDir() && root != path {
739 return filepath.SkipDir
740 }
741 name := strings.ToLower(info.Name())
742 if name == "dockerfile" || strings.HasPrefix(name, "dockerfile.") {
743 res = append(res, path)
744 }
745 return nil
746 })
747 if err != nil {
748 return nil, err
749 }
750 return res, nil
751}
752
753func findGitRoot(ctx context.Context, path string) (string, error) {
754 cmd := exec.CommandContext(ctx, "git", "rev-parse", "--git-common-dir")
755 cmd.Dir = path
756 out, err := cmd.CombinedOutput()
757 if err != nil {
758 if strings.Contains(string(out), "not a git repository") {
759 return "", fmt.Errorf(`sketch needs to run from within a git repo, but %s is not part of a git repo.
760Consider one of the following options:
761 - cd to a different dir that is already part of a git repo first, or
762 - to create a new git repo from this directory (%s), run this command:
763
764 git init . && git commit --allow-empty -m "initial commit"
765
766and try running sketch again.
767`, path, path)
768 }
769 return "", fmt.Errorf("git rev-parse --git-common-dir: %s: %w", out, err)
770 }
771 gitDir := strings.TrimSpace(string(out)) // location of .git dir, often as a relative path
772 absGitDir := filepath.Join(path, gitDir)
773 return filepath.Dir(absGitDir), err
774}
775
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000776// getEnvForwardingFromGitConfig retrieves environment variables to pass through to Docker
777// from git config using the sketch.envfwd multi-valued key.
778func getEnvForwardingFromGitConfig(ctx context.Context) []string {
779 outb, err := exec.CommandContext(ctx, "git", "config", "--get-all", "sketch.envfwd").CombinedOutput()
780 out := string(outb)
781 if err != nil {
782 if strings.Contains(out, "key does not exist") {
783 return nil
784 }
785 slog.ErrorContext(ctx, "failed to get sketch.envfwd from git config", "err", err, "output", out)
786 return nil
787 }
788
789 var envVars []string
790 for envVar := range strings.Lines(out) {
791 envVar = strings.TrimSpace(envVar)
792 if envVar == "" {
793 continue
794 }
795 envVars = append(envVars, envVar+"="+os.Getenv(envVar))
796 }
797 return envVars
798}