blob: 5e493920ad11b4ab554c9bfaef4461208e4ee9b8 [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"
Josh Bleecher Snyder99570462025-05-05 10:26:14 -070021 "sync/atomic"
Earl Lee2e463fb2025-04-17 11:22:22 -070022 "time"
23
Josh Bleecher Snyder78707d62025-04-30 21:06:49 +000024 "sketch.dev/browser"
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070025 "sketch.dev/llm/ant"
Sean McCulloughbaa2b592025-04-23 10:40:08 -070026 "sketch.dev/loop/server"
Earl Lee2e463fb2025-04-17 11:22:22 -070027 "sketch.dev/skribe"
Philip Zeyliger5d6af872025-04-23 19:48:34 -070028 "sketch.dev/webui"
Earl Lee2e463fb2025-04-17 11:22:22 -070029)
30
31// ContainerConfig holds all configuration for launching a container
32type ContainerConfig struct {
33 // SessionID is the unique identifier for this session
34 SessionID string
35
36 // LocalAddr is the initial address to use (though it may be overwritten later)
37 LocalAddr string
38
39 // SkabandAddr is the address of the skaband service if available
40 SkabandAddr string
41
42 // AntURL is the URL of the LLM service.
43 AntURL string
44
45 // AntAPIKey is the API key for LLM service.
46 AntAPIKey string
47
48 // Path is the local filesystem path to use
49 Path string
50
51 // GitUsername is the username to use for git operations
52 GitUsername string
53
54 // GitEmail is the email to use for git operations
55 GitEmail string
56
57 // OpenBrowser determines whether to open a browser automatically
58 OpenBrowser bool
59
60 // NoCleanup prevents container cleanup when set to true
61 NoCleanup bool
62
63 // ForceRebuild forces rebuilding of the Docker image even if it exists
64 ForceRebuild bool
65
66 // Host directory to copy container logs into, if not set to ""
67 ContainerLogDest string
68
69 // Path to pre-built linux sketch binary, or build a new one if set to ""
70 SketchBinaryLinux string
71
72 // Sketch client public key.
73 SketchPubKey string
Philip Zeyligerd1402952025-04-23 03:54:37 +000074
Sean McCulloughbaa2b592025-04-23 10:40:08 -070075 // Host port for the container's ssh server
76 SSHPort int
77
Philip Zeyliger18532b22025-04-23 21:11:46 +000078 // Outside information to pass to the container
79 OutsideHostname string
80 OutsideOS string
81 OutsideWorkingDir string
Philip Zeyligerb74c4f62025-04-25 19:18:49 -070082
Pokey Rule0dcebe12025-04-28 14:51:04 +010083 // If true, exit after the first turn
84 OneShot bool
85
86 // Initial prompt
87 Prompt string
Philip Zeyliger1b47aa22025-04-28 19:25:38 +000088
89 // Initial commit to use as starting point
90 InitialCommit string
David Crawshawb5f6a002025-05-05 08:27:16 -070091
92 // Verbose enables verbose output
93 Verbose bool
Philip Zeyliger1dc21372025-05-05 19:54:44 +000094
95 // DockerArgs are additional arguments to pass to the docker create command
96 DockerArgs string
Earl Lee2e463fb2025-04-17 11:22:22 -070097}
98
99// LaunchContainer creates a docker container for a project, installs sketch and opens a connection to it.
100// It writes status to stdout.
David Crawshawb5f6a002025-05-05 08:27:16 -0700101func LaunchContainer(ctx context.Context, config ContainerConfig) error {
Earl Lee2e463fb2025-04-17 11:22:22 -0700102 if _, err := exec.LookPath("docker"); err != nil {
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700103 if runtime.GOOS == "darwin" {
104 return fmt.Errorf("cannot find `docker` binary; run: brew install docker colima && colima start")
105 } else {
106 return fmt.Errorf("cannot find `docker` binary; install docker (e.g., apt-get install docker.io)")
107 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700108 }
109
110 if out, err := combinedOutput(ctx, "docker", "ps"); err != nil {
111 // `docker ps` provides a good error message here that can be
112 // easily chatgpt'ed by users, so send it to the user as-is:
113 // Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
114 return fmt.Errorf("docker ps: %s (%w)", out, err)
115 }
116
117 _, hostPort, err := net.SplitHostPort(config.LocalAddr)
118 if err != nil {
119 return err
120 }
121
122 gitRoot, err := findGitRoot(ctx, config.Path)
123 if err != nil {
124 return err
125 }
126
David Crawshawb5f6a002025-05-05 08:27:16 -0700127 imgName, err := findOrBuildDockerImage(ctx, config.Path, gitRoot, config.AntURL, config.AntAPIKey, config.ForceRebuild, config.Verbose)
Earl Lee2e463fb2025-04-17 11:22:22 -0700128 if err != nil {
129 return err
130 }
131
132 linuxSketchBin := config.SketchBinaryLinux
133 if linuxSketchBin == "" {
David Crawshawb5f6a002025-05-05 08:27:16 -0700134 linuxSketchBin, err = buildLinuxSketchBin(ctx)
Earl Lee2e463fb2025-04-17 11:22:22 -0700135 if err != nil {
136 return err
137 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700138 }
139
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000140 cntrName := "sketch-" + config.SessionID
Earl Lee2e463fb2025-04-17 11:22:22 -0700141 defer func() {
142 if config.NoCleanup {
143 return
144 }
145 if out, err := combinedOutput(ctx, "docker", "kill", cntrName); err != nil {
146 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
147 _ = out
148 }
149 if out, err := combinedOutput(ctx, "docker", "rm", cntrName); err != nil {
150 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
151 _ = out
152 }
153 }()
154
155 // errCh receives errors from operations that this function calls in separate goroutines.
156 errCh := make(chan error)
157
158 // Start the git server
159 gitSrv, err := newGitServer(gitRoot)
160 if err != nil {
161 return fmt.Errorf("failed to start git server: %w", err)
162 }
163 defer gitSrv.shutdown(ctx)
164
165 go func() {
166 errCh <- gitSrv.serve(ctx)
167 }()
168
169 // Get the current host git commit
170 var commit string
Philip Zeyliger1b47aa22025-04-28 19:25:38 +0000171 if out, err := combinedOutput(ctx, "git", "rev-parse", config.InitialCommit); err != nil {
172 return fmt.Errorf("git rev-parse %s: %w", config.InitialCommit, err)
Earl Lee2e463fb2025-04-17 11:22:22 -0700173 } else {
174 commit = strings.TrimSpace(string(out))
175 }
176 if out, err := combinedOutput(ctx, "git", "config", "http.receivepack", "true"); err != nil {
177 return fmt.Errorf("git config http.receivepack true: %s: %w", out, err)
178 }
179
180 relPath, err := filepath.Rel(gitRoot, config.Path)
181 if err != nil {
182 return err
183 }
184
185 // Create the sketch container
186 if err := createDockerContainer(ctx, cntrName, hostPort, relPath, imgName, config); err != nil {
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000187 return fmt.Errorf("failed to create docker container: %w", err)
Earl Lee2e463fb2025-04-17 11:22:22 -0700188 }
189
190 // Copy the sketch linux binary into the container
191 if out, err := combinedOutput(ctx, "docker", "cp", linuxSketchBin, cntrName+":/bin/sketch"); err != nil {
192 return fmt.Errorf("docker cp: %s, %w", out, err)
193 }
Sean McCulloughf5bb3d32025-04-18 10:47:59 -0700194
195 // Make sure that the webui is built so we can copy the results to the container.
196 _, err = webui.Build()
197 if err != nil {
198 return fmt.Errorf("failed to build webui: %w", err)
199 }
200
David Crawshaw8bff16a2025-04-18 01:16:49 -0700201 webuiZipPath, err := webui.ZipPath()
202 if err != nil {
203 return err
204 }
205 if out, err := combinedOutput(ctx, "docker", "cp", webuiZipPath, cntrName+":/root/.cache/sketch/webui/"+filepath.Base(webuiZipPath)); err != nil {
206 return fmt.Errorf("docker cp: %s, %w", out, err)
207 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700208
David Crawshaw53786ef2025-04-24 12:52:51 -0700209 fmt.Printf("📦 running in container %s\n", cntrName)
Earl Lee2e463fb2025-04-17 11:22:22 -0700210
211 // Start the sketch container
212 if out, err := combinedOutput(ctx, "docker", "start", cntrName); err != nil {
213 return fmt.Errorf("docker start: %s, %w", out, err)
214 }
215
216 // Copies structured logs from the container to the host.
217 copyLogs := func() {
218 if config.ContainerLogDest == "" {
219 return
220 }
221 out, err := combinedOutput(ctx, "docker", "logs", cntrName)
222 if err != nil {
223 fmt.Fprintf(os.Stderr, "docker logs failed: %v\n", err)
224 return
225 }
Josh Bleecher Snyder7660e4e2025-04-24 10:34:17 -0700226 prefix := []byte("structured logs:")
227 for line := range bytes.Lines(out) {
228 rest, ok := bytes.CutPrefix(line, prefix)
229 if !ok {
Earl Lee2e463fb2025-04-17 11:22:22 -0700230 continue
231 }
Josh Bleecher Snyder7660e4e2025-04-24 10:34:17 -0700232 logFile := string(bytes.TrimSpace(rest))
Earl Lee2e463fb2025-04-17 11:22:22 -0700233 srcPath := fmt.Sprintf("%s:%s", cntrName, logFile)
234 logFileName := filepath.Base(logFile)
235 dstPath := filepath.Join(config.ContainerLogDest, logFileName)
236 _, err := combinedOutput(ctx, "docker", "cp", srcPath, dstPath)
237 if err != nil {
238 fmt.Fprintf(os.Stderr, "docker cp %s %s failed: %v\n", srcPath, dstPath, err)
239 }
240 fmt.Fprintf(os.Stderr, "\ncopied container log %s to %s\n", srcPath, dstPath)
241 }
242 }
243
244 // NOTE: we want to see what the internal sketch binary prints
245 // regardless of the setting of the verbosity flag on the external
246 // binary, so reading "docker logs", which is the stdout/stderr of
247 // the internal binary is not conditional on the verbose flag.
248 appendInternalErr := func(err error) error {
249 if err == nil {
250 return nil
251 }
252 out, logsErr := combinedOutput(ctx, "docker", "logs", cntrName)
Philip Zeyligerd1402952025-04-23 03:54:37 +0000253 if logsErr != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700254 return fmt.Errorf("%w; and docker logs failed: %s, %v", err, out, logsErr)
255 }
256 out = bytes.TrimSpace(out)
257 if len(out) > 0 {
258 return fmt.Errorf("docker logs: %s;\n%w", out, err)
259 }
260 return err
261 }
262
263 // Get the sketch server port from the container
Sean McCulloughae3480f2025-04-23 15:28:20 -0700264 localAddr, err := getContainerPort(ctx, cntrName, "80")
Earl Lee2e463fb2025-04-17 11:22:22 -0700265 if err != nil {
266 return appendInternalErr(err)
267 }
268
Sean McCulloughae3480f2025-04-23 15:28:20 -0700269 localSSHAddr, err := getContainerPort(ctx, cntrName, "22")
270 if err != nil {
271 return appendInternalErr(err)
272 }
273 sshHost, sshPort, err := net.SplitHostPort(localSSHAddr)
274 if err != nil {
David Crawshawb5f6a002025-05-05 08:27:16 -0700275 return appendInternalErr(fmt.Errorf("failed to split ssh host and port: %w", err))
Sean McCulloughae3480f2025-04-23 15:28:20 -0700276 }
Sean McCullough4854c652025-04-24 18:37:02 -0700277
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700278 var sshServerIdentity, sshUserIdentity []byte
Sean McCullough4854c652025-04-24 18:37:02 -0700279
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700280 if err := CheckForInclude(); err != nil {
281 fmt.Println(err.Error())
282 // continue - ssh config is not required for the rest of sketch to function locally.
283 } else {
Josh Bleecher Snyder50608b12025-05-03 22:55:49 +0000284 cst, err := NewSSHTheater(cntrName, sshHost, sshPort)
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700285 if err != nil {
286 return appendInternalErr(fmt.Errorf("NewContainerSSHTheather: %w", err))
287 }
288
Sean McCulloughea3fc202025-04-28 12:53:37 -0700289 // Note: The vscode: link uses an undocumented request parameter that I really had to dig to find:
290 // https://github.com/microsoft/vscode/blob/2b9486161abaca59b5132ce3c59544f3cc7000f6/src/vs/code/electron-main/app.ts#L878
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700291 fmt.Printf(`Connect to this container via any of these methods:
Sean McCullough4854c652025-04-24 18:37:02 -0700292🖥️ ssh %s
293🖥️ code --remote ssh-remote+root@%s /app -n
Sean McCulloughea3fc202025-04-28 12:53:37 -0700294🔗 vscode://vscode-remote/ssh-remote+root@%s/app?windowId=_blank
Sean McCullough4854c652025-04-24 18:37:02 -0700295`, cntrName, cntrName, cntrName)
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700296 sshUserIdentity = cst.userIdentity
297 sshServerIdentity = cst.serverIdentity
298 defer func() {
299 if err := cst.Cleanup(); err != nil {
300 appendInternalErr(err)
301 }
302 }()
303 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700304
Earl Lee2e463fb2025-04-17 11:22:22 -0700305 // Tell the sketch container which git server port and commit to initialize with.
306 go func() {
307 // TODO: Why is this called in a goroutine? I have found that when I pull this out
308 // of the goroutine and call it inline, then the terminal UI clears itself and all
309 // the scrollback (which is not good, but also not fatal). I can't see why it does this
310 // though, since none of the calls in postContainerInitConfig obviously write to stdout
311 // or stderr.
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700312 if err := postContainerInitConfig(ctx, localAddr, commit, gitSrv.gitPort, gitSrv.pass, sshServerIdentity, sshUserIdentity); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700313 slog.ErrorContext(ctx, "LaunchContainer.postContainerInitConfig", slog.String("err", err.Error()))
314 errCh <- appendInternalErr(err)
315 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700316
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700317 // We open the browser after the init config because the above waits for the web server to be serving.
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700318 ps1URL := "http://" + localAddr
319 if config.SkabandAddr != "" {
320 ps1URL = fmt.Sprintf("%s/s/%s", config.SkabandAddr, config.SessionID)
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700321 }
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700322 if config.OpenBrowser {
323 browser.Open(ps1URL)
324 }
325 gitSrv.ps1URL.Store(&ps1URL)
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700326 }()
Earl Lee2e463fb2025-04-17 11:22:22 -0700327
328 go func() {
329 cmd := exec.CommandContext(ctx, "docker", "attach", cntrName)
330 cmd.Stdin = os.Stdin
331 cmd.Stdout = os.Stdout
332 cmd.Stderr = os.Stderr
333 errCh <- run(ctx, "docker attach", cmd)
334 }()
335
336 defer copyLogs()
337
338 for {
339 select {
340 case <-ctx.Done():
341 return ctx.Err()
342 case err := <-errCh:
343 if err != nil {
344 return appendInternalErr(fmt.Errorf("container process: %w", err))
345 }
346 return nil
347 }
348 }
349}
350
351func combinedOutput(ctx context.Context, cmdName string, args ...string) ([]byte, error) {
352 cmd := exec.CommandContext(ctx, cmdName, args...)
Earl Lee2e463fb2025-04-17 11:22:22 -0700353 start := time.Now()
354
355 out, err := cmd.CombinedOutput()
356 if err != nil {
David Crawshawc7e77962025-05-03 13:20:18 -0700357 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 -0700358 } else {
David Crawshawc7e77962025-05-03 13:20:18 -0700359 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 -0700360 }
361 return out, err
362}
363
364func run(ctx context.Context, cmdName string, cmd *exec.Cmd) error {
365 start := time.Now()
366 err := cmd.Run()
367 if err != nil {
David Crawshawc7e77962025-05-03 13:20:18 -0700368 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 -0700369 } else {
David Crawshawc7e77962025-05-03 13:20:18 -0700370 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 -0700371 }
372 return err
373}
374
375type gitServer struct {
376 gitLn net.Listener
377 gitPort string
378 srv *http.Server
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700379 pass string
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700380 ps1URL atomic.Pointer[string]
Earl Lee2e463fb2025-04-17 11:22:22 -0700381}
382
383func (gs *gitServer) shutdown(ctx context.Context) {
384 gs.srv.Shutdown(ctx)
385 gs.gitLn.Close()
386}
387
388// Serve a git remote from the host for the container to fetch from and push to.
389func (gs *gitServer) serve(ctx context.Context) error {
390 slog.DebugContext(ctx, "starting git server", slog.String("git_remote_addr", "http://host.docker.internal:"+gs.gitPort+"/.git"))
391 return gs.srv.Serve(gs.gitLn)
392}
393
394func newGitServer(gitRoot string) (*gitServer, error) {
Josh Bleecher Snyder9f6a9982025-04-22 17:34:15 -0700395 ret := &gitServer{
396 pass: rand.Text(),
397 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700398
Earl Lee2e463fb2025-04-17 11:22:22 -0700399 gitLn, err := net.Listen("tcp4", ":0")
400 if err != nil {
401 return nil, fmt.Errorf("git listen: %w", err)
402 }
403 ret.gitLn = gitLn
404
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700405 browserC := make(chan bool, 1) // channel of browser open requests
406
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000407 go func() {
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700408 for range browserC {
409 browser.Open(*ret.ps1URL.Load())
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000410 }
411 }()
412
413 srv := http.Server{Handler: &gitHTTP{gitRepoRoot: gitRoot, pass: []byte(ret.pass), browserC: browserC}}
Earl Lee2e463fb2025-04-17 11:22:22 -0700414 ret.srv = &srv
415
416 _, gitPort, err := net.SplitHostPort(gitLn.Addr().String())
417 if err != nil {
418 return nil, fmt.Errorf("git port: %w", err)
419 }
420 ret.gitPort = gitPort
421 return ret, nil
422}
423
424func createDockerContainer(ctx context.Context, cntrName, hostPort, relPath, imgName string, config ContainerConfig) error {
David Crawshaw69c67312025-04-17 13:42:00 -0700425 cmdArgs := []string{
426 "create",
David Crawshaw66cf74e2025-05-05 08:48:39 -0700427 "-i",
Earl Lee2e463fb2025-04-17 11:22:22 -0700428 "--name", cntrName,
429 "-p", hostPort + ":80", // forward container port 80 to a host port
Philip Zeyliger6234a8d2025-05-02 14:31:20 -0700430 "-e", "SKETCH_ANTHROPIC_API_KEY=" + config.AntAPIKey,
Earl Lee2e463fb2025-04-17 11:22:22 -0700431 }
David Crawshaw66cf74e2025-05-05 08:48:39 -0700432 if !config.OneShot {
433 cmdArgs = append(cmdArgs, "-t")
434 }
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000435
436 for _, envVar := range getEnvForwardingFromGitConfig(ctx) {
437 cmdArgs = append(cmdArgs, "-e", envVar)
438 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700439 if config.AntURL != "" {
Philip Zeyliger6234a8d2025-05-02 14:31:20 -0700440 cmdArgs = append(cmdArgs, "-e", "SKETCH_ANT_URL="+config.AntURL)
Earl Lee2e463fb2025-04-17 11:22:22 -0700441 }
442 if config.SketchPubKey != "" {
443 cmdArgs = append(cmdArgs, "-e", "SKETCH_PUB_KEY="+config.SketchPubKey)
444 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700445 if config.SSHPort > 0 {
446 cmdArgs = append(cmdArgs, "-p", fmt.Sprintf("%d:22", config.SSHPort)) // forward container ssh port to host ssh port
447 } else {
448 cmdArgs = append(cmdArgs, "-p", "22") // use an ephemeral host port for ssh.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700449 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700450 if relPath != "." {
451 cmdArgs = append(cmdArgs, "-w", "/app/"+relPath)
452 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700453 // colima does this by default, but Linux docker seems to need this set explicitly
454 cmdArgs = append(cmdArgs, "--add-host", "host.docker.internal:host-gateway")
Earl Lee2e463fb2025-04-17 11:22:22 -0700455 cmdArgs = append(
456 cmdArgs,
457 imgName,
458 "/bin/sketch",
459 "-unsafe",
460 "-addr=:80",
461 "-session-id="+config.SessionID,
Philip Zeyligerd1402952025-04-23 03:54:37 +0000462 "-git-username="+config.GitUsername,
463 "-git-email="+config.GitEmail,
Philip Zeyliger18532b22025-04-23 21:11:46 +0000464 "-outside-hostname="+config.OutsideHostname,
465 "-outside-os="+config.OutsideOS,
466 "-outside-working-dir="+config.OutsideWorkingDir,
Josh Bleecher Snyder3cae7d92025-04-30 09:54:29 -0700467 "-open=false",
Earl Lee2e463fb2025-04-17 11:22:22 -0700468 )
469 if config.SkabandAddr != "" {
470 cmdArgs = append(cmdArgs, "-skaband-addr="+config.SkabandAddr)
471 }
Pokey Rule0dcebe12025-04-28 14:51:04 +0100472 if config.Prompt != "" {
473 cmdArgs = append(cmdArgs, "-prompt", config.Prompt)
474 }
475 if config.OneShot {
476 cmdArgs = append(cmdArgs, "-one-shot")
Philip Zeyligerb74c4f62025-04-25 19:18:49 -0700477 }
Philip Zeyliger1dc21372025-05-05 19:54:44 +0000478
479 // Add additional docker arguments if provided
480 if config.DockerArgs != "" {
481 // Parse space-separated docker arguments with support for quotes and escaping
482 args := parseDockerArgs(config.DockerArgs)
483 // Insert arguments after "create" but before other arguments
484 for i := len(args) - 1; i >= 0; i-- {
485 cmdArgs = append(cmdArgs[:1], append([]string{args[i]}, cmdArgs[1:]...)...)
486 }
487 }
488
Earl Lee2e463fb2025-04-17 11:22:22 -0700489 if out, err := combinedOutput(ctx, "docker", cmdArgs...); err != nil {
490 return fmt.Errorf("docker create: %s, %w", out, err)
491 }
492 return nil
493}
494
David Crawshawb5f6a002025-05-05 08:27:16 -0700495func buildLinuxSketchBin(ctx context.Context) (string, error) {
David Crawshaw8a617cb2025-04-18 01:28:43 -0700496 homeDir, err := os.UserHomeDir()
David Crawshaw69c67312025-04-17 13:42:00 -0700497 if err != nil {
498 return "", err
499 }
David Crawshaw8a617cb2025-04-18 01:28:43 -0700500 linuxGopath := filepath.Join(homeDir, ".cache", "sketch", "linuxgo")
501 if err := os.MkdirAll(linuxGopath, 0o777); err != nil {
502 return "", err
503 }
504
505 verToInstall := "@latest"
506 if out, err := exec.Command("go", "list", "-m").CombinedOutput(); err != nil {
507 return "", fmt.Errorf("failed to run go list -m: %s: %v", out, err)
508 } else {
509 if strings.TrimSpace(string(out)) == "sketch.dev" {
David Crawshaw094e4d22025-04-24 11:35:14 -0700510 slog.DebugContext(ctx, "built linux agent from currently checked out module")
David Crawshaw8a617cb2025-04-18 01:28:43 -0700511 verToInstall = ""
512 }
513 }
David Crawshaw69c67312025-04-17 13:42:00 -0700514
Earl Lee2e463fb2025-04-17 11:22:22 -0700515 start := time.Now()
David Crawshaw8a617cb2025-04-18 01:28:43 -0700516 cmd := exec.CommandContext(ctx, "go", "install", "sketch.dev/cmd/sketch"+verToInstall)
David Crawshawb9eaef52025-04-17 15:23:18 -0700517 cmd.Env = append(
518 os.Environ(),
519 "GOOS=linux",
520 "CGO_ENABLED=0",
521 "GOTOOLCHAIN=auto",
David Crawshaw8a617cb2025-04-18 01:28:43 -0700522 "GOPATH="+linuxGopath,
Josh Bleecher Snyderfae17572025-04-21 11:48:05 -0700523 "GOBIN=",
David Crawshawb9eaef52025-04-17 15:23:18 -0700524 )
Earl Lee2e463fb2025-04-17 11:22:22 -0700525
Earl Lee2e463fb2025-04-17 11:22:22 -0700526 out, err := cmd.CombinedOutput()
527 if err != nil {
David Crawshawc7e77962025-05-03 13:20:18 -0700528 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 -0700529 return "", fmt.Errorf("failed to build linux sketch binary: %s: %w", out, err)
530 } else {
David Crawshawc7e77962025-05-03 13:20:18 -0700531 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 -0700532 }
533
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700534 if runtime.GOOS != "linux" {
David Crawshawc7e77962025-05-03 13:20:18 -0700535 return filepath.Join(linuxGopath, "bin", "linux_"+runtime.GOARCH, "sketch"), nil
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700536 }
David Crawshawc7e77962025-05-03 13:20:18 -0700537 // If we are already on Linux, there's no extra platform name in the path
538 return filepath.Join(linuxGopath, "bin", "sketch"), nil
Earl Lee2e463fb2025-04-17 11:22:22 -0700539}
540
Sean McCulloughae3480f2025-04-23 15:28:20 -0700541func getContainerPort(ctx context.Context, cntrName, cntrPort string) (string, error) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700542 localAddr := ""
Sean McCulloughae3480f2025-04-23 15:28:20 -0700543 if out, err := combinedOutput(ctx, "docker", "port", cntrName, cntrPort); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700544 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
545 } else {
546 v4, _, found := strings.Cut(string(out), "\n")
547 if !found {
548 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
549 }
550 localAddr = v4
551 if strings.HasPrefix(localAddr, "0.0.0.0") {
552 localAddr = "127.0.0.1" + strings.TrimPrefix(localAddr, "0.0.0.0")
553 }
554 }
555 return localAddr, nil
556}
557
558// Contact the container and configure it.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700559func postContainerInitConfig(ctx context.Context, localAddr, commit, gitPort, gitPass string, sshServerIdentity, sshAuthorizedKeys []byte) error {
Earl Lee2e463fb2025-04-17 11:22:22 -0700560 localURL := "http://" + localAddr
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700561
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000562 // Check if SSH is available by checking for the Include directive in ~/.ssh/config
563 sshAvailable := true
564 sshError := ""
565 if err := CheckForInclude(); err != nil {
566 sshAvailable = false
567 sshError = err.Error()
568 }
569
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700570 initMsg, err := json.Marshal(
571 server.InitRequest{
572 Commit: commit,
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000573 OutsideHTTP: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s", gitPass, gitPort),
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700574 GitRemoteAddr: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s/.git", gitPass, gitPort),
575 HostAddr: localAddr,
576 SSHAuthorizedKeys: sshAuthorizedKeys,
577 SSHServerIdentity: sshServerIdentity,
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000578 SSHAvailable: sshAvailable,
579 SSHError: sshError,
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700580 })
Earl Lee2e463fb2025-04-17 11:22:22 -0700581 if err != nil {
582 return fmt.Errorf("init msg: %w", err)
583 }
584
Earl Lee2e463fb2025-04-17 11:22:22 -0700585 // Note: this /init POST is handled in loop/server/loophttp.go:
586 initMsgByteReader := bytes.NewReader(initMsg)
587 req, err := http.NewRequest("POST", localURL+"/init", initMsgByteReader)
588 if err != nil {
589 return err
590 }
591
592 var res *http.Response
593 for i := 0; ; i++ {
594 time.Sleep(100 * time.Millisecond)
595 // If you DON'T reset this byteReader, then subsequent retries may end up sending 0 bytes.
596 initMsgByteReader.Reset(initMsg)
597 res, err = http.DefaultClient.Do(req)
598 if err != nil {
David Crawshaw99231ba2025-05-03 10:48:26 -0700599 if i < 100 {
600 if i%10 == 0 {
601 slog.DebugContext(ctx, "postContainerInitConfig retrying", slog.Int("retry", i), slog.String("err", err.Error()))
602 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700603 continue
604 }
605 return fmt.Errorf("failed to %s/init sketch in container, NOT retrying: err: %v", localURL, err)
606 }
607 break
608 }
609 resBytes, _ := io.ReadAll(res.Body)
610 if res.StatusCode != http.StatusOK {
611 return fmt.Errorf("failed to initialize sketch in container, response status code %d: %s", res.StatusCode, resBytes)
612 }
613 return nil
614}
615
David Crawshawb5f6a002025-05-05 08:27:16 -0700616func findOrBuildDockerImage(ctx context.Context, cwd, gitRoot, antURL, antAPIKey string, forceRebuild, verbose bool) (imgName string, err error) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700617 h := sha256.Sum256([]byte(gitRoot))
618 imgName = "sketch-" + hex.EncodeToString(h[:6])
619
620 var curImgInitFilesHash string
621 if out, err := combinedOutput(ctx, "docker", "inspect", "--format", "{{json .Config.Labels}}", imgName); err != nil {
622 if strings.Contains(string(out), "No such object") {
623 // Image does not exist, continue and build it.
624 curImgInitFilesHash = ""
625 } else {
626 return "", fmt.Errorf("docker inspect failed: %s, %v", out, err)
627 }
628 } else {
629 m := map[string]string{}
630 if err := json.Unmarshal(bytes.TrimSpace(out), &m); err != nil {
631 return "", fmt.Errorf("docker inspect output unparsable: %s, %v", out, err)
632 }
633 curImgInitFilesHash = m["sketch_context"]
634 }
635
636 candidates, err := findRepoDockerfiles(cwd, gitRoot)
637 if err != nil {
638 return "", fmt.Errorf("find dockerfile: %w", err)
639 }
640
641 var initFiles map[string]string
642 var dockerfilePath string
643
644 // TODO: prefer a "Dockerfile.sketch" so users can tailor any env to this tool.
645 if len(candidates) == 1 && strings.ToLower(filepath.Base(candidates[0])) == "dockerfile" {
646 dockerfilePath = candidates[0]
647 contents, err := os.ReadFile(dockerfilePath)
648 if err != nil {
649 return "", err
650 }
651 fmt.Printf("using %s as dev env\n", candidates[0])
652 if hashInitFiles(map[string]string{dockerfilePath: string(contents)}) == curImgInitFilesHash && !forceRebuild {
Earl Lee2e463fb2025-04-17 11:22:22 -0700653 return imgName, nil
654 }
655 } else {
656 initFiles, err = readInitFiles(os.DirFS(gitRoot))
657 if err != nil {
658 return "", err
659 }
660 subPathWorkingDir, err := filepath.Rel(gitRoot, cwd)
661 if err != nil {
662 return "", err
663 }
664 initFileHash := hashInitFiles(initFiles)
665 if curImgInitFilesHash == initFileHash && !forceRebuild {
Earl Lee2e463fb2025-04-17 11:22:22 -0700666 return imgName, nil
667 }
668
669 start := time.Now()
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700670 srv := &ant.Service{
671 URL: antURL,
672 APIKey: antAPIKey,
673 HTTPC: http.DefaultClient,
674 }
675 dockerfile, err := createDockerfile(ctx, srv, initFiles, subPathWorkingDir)
Earl Lee2e463fb2025-04-17 11:22:22 -0700676 if err != nil {
677 return "", fmt.Errorf("create dockerfile: %w", err)
678 }
David Crawshaw8fd51042025-05-05 12:52:43 -0700679 dockerfilePath = filepath.Join(cwd, tmpSketchDockerfile)
Earl Lee2e463fb2025-04-17 11:22:22 -0700680 if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0o666); err != nil {
681 return "", err
682 }
683 defer os.Remove(dockerfilePath)
684
David Crawshawb5f6a002025-05-05 08:27:16 -0700685 if verbose {
686 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))
687 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700688 }
689
690 var gitUserEmail, gitUserName string
691 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.email"); err != nil {
692 return "", fmt.Errorf("git config: %s: %v", out, err)
693 } else {
694 gitUserEmail = strings.TrimSpace(string(out))
695 }
696 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.name"); err != nil {
697 return "", fmt.Errorf("git config: %s: %v", out, err)
698 } else {
699 gitUserName = strings.TrimSpace(string(out))
700 }
701
702 start := time.Now()
703 cmd := exec.CommandContext(ctx,
704 "docker", "build",
705 "-t", imgName,
706 "-f", dockerfilePath,
707 "--build-arg", "GIT_USER_EMAIL="+gitUserEmail,
708 "--build-arg", "GIT_USER_NAME="+gitUserName,
Earl Lee2e463fb2025-04-17 11:22:22 -0700709 )
David Crawshawb5f6a002025-05-05 08:27:16 -0700710 if !verbose {
711 cmd.Args = append(cmd.Args, "--progress=quiet")
Philip Zeyligere4fa0e32025-04-23 14:15:55 -0700712 }
David Crawshawb5f6a002025-05-05 08:27:16 -0700713 cmd.Args = append(cmd.Args, ".")
714 cmd.Dir = gitRoot
715 cmd.Stdout = os.Stdout
716 cmd.Stderr = os.Stderr
717 fmt.Printf("🏗️ building docker image %s... (use -verbose to see build output)\n", imgName)
Earl Lee2e463fb2025-04-17 11:22:22 -0700718
719 err = run(ctx, "docker build", cmd)
720 if err != nil {
721 return "", fmt.Errorf("docker build failed: %v", err)
722 }
723 fmt.Printf("built docker image %s in %s\n", imgName, time.Since(start).Round(time.Millisecond))
724 return imgName, nil
725}
726
727func findRepoDockerfiles(cwd, gitRoot string) ([]string, error) {
728 files, err := findDirDockerfiles(cwd)
729 if err != nil {
730 return nil, err
731 }
732 if len(files) > 0 {
733 return files, nil
734 }
735
736 path := cwd
737 for path != gitRoot {
738 path = filepath.Dir(path)
739 files, err := findDirDockerfiles(path)
740 if err != nil {
741 return nil, err
742 }
743 if len(files) > 0 {
744 return files, nil
745 }
746 }
747 return files, nil
748}
749
750// findDirDockerfiles finds all "Dockerfile*" files in a directory.
751func findDirDockerfiles(root string) (res []string, err error) {
752 err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
753 if err != nil {
754 return err
755 }
756 if info.IsDir() && root != path {
757 return filepath.SkipDir
758 }
759 name := strings.ToLower(info.Name())
760 if name == "dockerfile" || strings.HasPrefix(name, "dockerfile.") {
761 res = append(res, path)
762 }
763 return nil
764 })
765 if err != nil {
766 return nil, err
767 }
768 return res, nil
769}
770
771func findGitRoot(ctx context.Context, path string) (string, error) {
772 cmd := exec.CommandContext(ctx, "git", "rev-parse", "--git-common-dir")
773 cmd.Dir = path
774 out, err := cmd.CombinedOutput()
775 if err != nil {
776 if strings.Contains(string(out), "not a git repository") {
777 return "", fmt.Errorf(`sketch needs to run from within a git repo, but %s is not part of a git repo.
778Consider one of the following options:
779 - cd to a different dir that is already part of a git repo first, or
780 - to create a new git repo from this directory (%s), run this command:
781
782 git init . && git commit --allow-empty -m "initial commit"
783
784and try running sketch again.
785`, path, path)
786 }
787 return "", fmt.Errorf("git rev-parse --git-common-dir: %s: %w", out, err)
788 }
789 gitDir := strings.TrimSpace(string(out)) // location of .git dir, often as a relative path
790 absGitDir := filepath.Join(path, gitDir)
791 return filepath.Dir(absGitDir), err
792}
793
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000794// getEnvForwardingFromGitConfig retrieves environment variables to pass through to Docker
795// from git config using the sketch.envfwd multi-valued key.
796func getEnvForwardingFromGitConfig(ctx context.Context) []string {
797 outb, err := exec.CommandContext(ctx, "git", "config", "--get-all", "sketch.envfwd").CombinedOutput()
798 out := string(outb)
799 if err != nil {
800 if strings.Contains(out, "key does not exist") {
801 return nil
802 }
803 slog.ErrorContext(ctx, "failed to get sketch.envfwd from git config", "err", err, "output", out)
804 return nil
805 }
806
807 var envVars []string
808 for envVar := range strings.Lines(out) {
809 envVar = strings.TrimSpace(envVar)
810 if envVar == "" {
811 continue
812 }
813 envVars = append(envVars, envVar+"="+os.Getenv(envVar))
814 }
815 return envVars
816}
Philip Zeyliger1dc21372025-05-05 19:54:44 +0000817
818// parseDockerArgs parses a string containing space-separated Docker arguments into an array of strings.
819// It handles quoted arguments and escaped characters.
820//
821// Examples:
822//
823// --memory=2g --cpus=2 -> ["--memory=2g", "--cpus=2"]
824// --label="my label" --env=FOO=bar -> ["--label=my label", "--env=FOO=bar"]
825// --env="KEY=\"quoted value\"" -> ["--env=KEY=\"quoted value\""]
826func parseDockerArgs(args string) []string {
827 if args = strings.TrimSpace(args); args == "" {
828 return []string{}
829 }
830
831 var result []string
832 var current strings.Builder
833 inQuotes := false
834 escapeNext := false
835 quoteChar := rune(0)
836
837 for _, char := range args {
838 if escapeNext {
839 current.WriteRune(char)
840 escapeNext = false
841 continue
842 }
843
844 if char == '\\' {
845 escapeNext = true
846 continue
847 }
848
849 if char == '"' || char == '\'' {
850 if !inQuotes {
851 inQuotes = true
852 quoteChar = char
853 continue
854 } else if char == quoteChar {
855 inQuotes = false
856 quoteChar = rune(0)
857 continue
858 }
859 // Non-matching quote character inside quotes
860 current.WriteRune(char)
861 continue
862 }
863
864 // Space outside of quotes is an argument separator
865 if char == ' ' && !inQuotes {
866 if current.Len() > 0 {
867 result = append(result, current.String())
868 current.Reset()
869 }
870 continue
871 }
872
873 current.WriteRune(char)
874 }
875
876 // Add the last argument if there is one
877 if current.Len() > 0 {
878 result = append(result, current.String())
879 }
880
881 return result
882}