blob: b8fbd98f1f124b9ce53b99ee6d80cba1ccac12fd [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001// Package dockerimg
2package dockerimg
3
4import (
Josh Bleecher Snyderc9898fd2025-07-08 21:09:18 +00005 "archive/tar"
Earl Lee2e463fb2025-04-17 11:22:22 -07006 "bytes"
7 "context"
Philip Zeyliger5e227dd2025-04-21 15:55:29 -07008 "crypto/rand"
Earl Lee2e463fb2025-04-17 11:22:22 -07009 "crypto/sha256"
10 "encoding/hex"
11 "encoding/json"
12 "fmt"
13 "io"
14 "log/slog"
15 "net"
16 "net/http"
17 "os"
18 "os/exec"
19 "path/filepath"
20 "runtime"
21 "strings"
Josh Bleecher Snyder99570462025-05-05 10:26:14 -070022 "sync/atomic"
Earl Lee2e463fb2025-04-17 11:22:22 -070023 "time"
24
Sean McCullough7013e9e2025-05-14 02:03:58 +000025 "golang.org/x/crypto/ssh"
Josh Bleecher Snyder78707d62025-04-30 21:06:49 +000026 "sketch.dev/browser"
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -070027 "sketch.dev/embedded"
Sean McCulloughbaa2b592025-04-23 10:40:08 -070028 "sketch.dev/loop/server"
Earl Lee2e463fb2025-04-17 11:22:22 -070029 "sketch.dev/skribe"
30)
31
32// ContainerConfig holds all configuration for launching a container
33type ContainerConfig struct {
34 // SessionID is the unique identifier for this session
35 SessionID string
36
37 // LocalAddr is the initial address to use (though it may be overwritten later)
38 LocalAddr string
39
40 // SkabandAddr is the address of the skaband service if available
41 SkabandAddr string
42
David Crawshaw5a7b3692025-05-05 16:49:15 -070043 // Model is the name of the LLM model to use.
44 Model string
Earl Lee2e463fb2025-04-17 11:22:22 -070045
David Crawshaw5a7b3692025-05-05 16:49:15 -070046 // ModelURL is the URL of the LLM service.
47 ModelURL string
48
49 // ModelAPIKey is the API key for LLM service.
50 ModelAPIKey string
Earl Lee2e463fb2025-04-17 11:22:22 -070051
52 // Path is the local filesystem path to use
53 Path string
54
55 // GitUsername is the username to use for git operations
56 GitUsername string
57
58 // GitEmail is the email to use for git operations
59 GitEmail string
60
61 // OpenBrowser determines whether to open a browser automatically
62 OpenBrowser bool
63
64 // NoCleanup prevents container cleanup when set to true
65 NoCleanup bool
66
67 // ForceRebuild forces rebuilding of the Docker image even if it exists
68 ForceRebuild bool
69
Philip Zeyliger983b58a2025-07-02 19:42:08 -070070 // BaseImage is the base Docker image to use for layering the repo
71 BaseImage string
72
Earl Lee2e463fb2025-04-17 11:22:22 -070073 // Host directory to copy container logs into, if not set to ""
74 ContainerLogDest string
75
76 // Path to pre-built linux sketch binary, or build a new one if set to ""
77 SketchBinaryLinux string
78
79 // Sketch client public key.
80 SketchPubKey string
Philip Zeyligerd1402952025-04-23 03:54:37 +000081
Sean McCulloughbaa2b592025-04-23 10:40:08 -070082 // Host port for the container's ssh server
83 SSHPort int
84
Philip Zeyliger18532b22025-04-23 21:11:46 +000085 // Outside information to pass to the container
86 OutsideHostname string
87 OutsideOS string
88 OutsideWorkingDir string
Philip Zeyligerb74c4f62025-04-25 19:18:49 -070089
Pokey Rule0dcebe12025-04-28 14:51:04 +010090 // If true, exit after the first turn
91 OneShot bool
92
93 // Initial prompt
94 Prompt string
Philip Zeyliger1b47aa22025-04-28 19:25:38 +000095
David Crawshawb5f6a002025-05-05 08:27:16 -070096 // Verbose enables verbose output
97 Verbose bool
Philip Zeyliger1dc21372025-05-05 19:54:44 +000098
99 // DockerArgs are additional arguments to pass to the docker create command
100 DockerArgs string
Josh Bleecher Snyderb1cca6f2025-05-06 01:52:55 +0000101
Josh Bleecher Snyderac761c92025-05-16 18:58:45 +0000102 // Mounts specifies volumes to mount in the container in format /path/on/host:/path/in/container
103 Mounts []string
104
Josh Bleecher Snyderb1cca6f2025-05-06 01:52:55 +0000105 // ExperimentFlag contains the experimental features to enable
106 ExperimentFlag string
Philip Zeyliger613c0f52025-05-15 16:36:22 -0700107
108 // TermUI enables terminal UI
109 TermUI bool
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700110
Josh Bleecher Snyder33032d32025-05-30 16:28:21 +0000111 // Budget configuration
Philip Zeyligere6c294d2025-06-04 16:55:21 +0000112 MaxDollars float64
Josh Bleecher Snyder33032d32025-05-30 16:28:21 +0000113
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700114 GitRemoteUrl string
115
Josh Bleecher Snyder664404e2025-06-04 21:56:42 +0000116 // Upstream branch for git work
117 Upstream string
118
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700119 // Commit hash to checkout from GetRemoteUrl
120 Commit string
121
122 // Outtie's HTTP server
123 OutsideHTTP string
Philip Zeyligerbe7802a2025-06-04 20:15:25 +0000124
125 // Prefix for git branches created by sketch
126 BranchPrefix string
philip.zeyliger6d3de482025-06-10 19:38:14 -0700127
128 // LinkToGitHub enables GitHub branch linking in UI
129 LinkToGitHub bool
Philip Zeyligerd4be7a22025-06-15 09:39:00 -0700130
131 // SubtraceToken enables running sketch under subtrace.dev (development only)
132 SubtraceToken string
Philip Zeyliger194bfa82025-06-24 06:03:06 -0700133
134 // MCPServers contains MCP server configurations
135 MCPServers []string
Earl Lee2e463fb2025-04-17 11:22:22 -0700136}
137
138// LaunchContainer creates a docker container for a project, installs sketch and opens a connection to it.
139// It writes status to stdout.
David Crawshawb5f6a002025-05-05 08:27:16 -0700140func LaunchContainer(ctx context.Context, config ContainerConfig) error {
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700141 slog.Debug("Container Config", slog.String("config", fmt.Sprintf("%+v", config)))
Earl Lee2e463fb2025-04-17 11:22:22 -0700142 if _, err := exec.LookPath("docker"); err != nil {
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700143 if runtime.GOOS == "darwin" {
144 return fmt.Errorf("cannot find `docker` binary; run: brew install docker colima && colima start")
145 } else {
146 return fmt.Errorf("cannot find `docker` binary; install docker (e.g., apt-get install docker.io)")
147 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700148 }
149
150 if out, err := combinedOutput(ctx, "docker", "ps"); err != nil {
151 // `docker ps` provides a good error message here that can be
152 // easily chatgpt'ed by users, so send it to the user as-is:
153 // Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
154 return fmt.Errorf("docker ps: %s (%w)", out, err)
155 }
156
157 _, hostPort, err := net.SplitHostPort(config.LocalAddr)
158 if err != nil {
159 return err
160 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700161 gitRoot, err := findGitRoot(ctx, config.Path)
162 if err != nil {
163 return err
164 }
Philip Zeyligerd6d12d12025-05-19 19:19:21 -0700165 err = checkForEmptyGitRepo(ctx, config.Path)
166 if err != nil {
167 return err
168 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700169
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700170 imgName, err := findOrBuildDockerImage(ctx, gitRoot, config.BaseImage, config.ForceRebuild, config.Verbose)
Earl Lee2e463fb2025-04-17 11:22:22 -0700171 if err != nil {
172 return err
173 }
174
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000175 cntrName := "sketch-" + config.SessionID
Earl Lee2e463fb2025-04-17 11:22:22 -0700176 defer func() {
177 if config.NoCleanup {
178 return
179 }
180 if out, err := combinedOutput(ctx, "docker", "kill", cntrName); err != nil {
181 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
182 _ = out
183 }
184 if out, err := combinedOutput(ctx, "docker", "rm", cntrName); err != nil {
185 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
186 _ = out
187 }
188 }()
189
190 // errCh receives errors from operations that this function calls in separate goroutines.
191 errCh := make(chan error)
192
193 // Start the git server
194 gitSrv, err := newGitServer(gitRoot)
195 if err != nil {
196 return fmt.Errorf("failed to start git server: %w", err)
197 }
198 defer gitSrv.shutdown(ctx)
199
200 go func() {
201 errCh <- gitSrv.serve(ctx)
202 }()
203
204 // Get the current host git commit
205 var commit string
Philip Zeyligera347b172025-06-04 16:18:57 +0000206 if out, err := combinedOutput(ctx, "git", "rev-parse", "HEAD"); err != nil {
207 return fmt.Errorf("git rev-parse HEAD: %w", err)
Earl Lee2e463fb2025-04-17 11:22:22 -0700208 } else {
209 commit = strings.TrimSpace(string(out))
210 }
Josh Bleecher Snyder664404e2025-06-04 21:56:42 +0000211
212 var upstream string
213 if out, err := combinedOutput(ctx, "git", "branch", "--show-current"); err != nil {
214 slog.DebugContext(ctx, "git branch --show-current failed (continuing)", "error", err)
215 } else {
216 upstream = strings.TrimSpace(string(out))
217 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700218 if out, err := combinedOutput(ctx, "git", "config", "http.receivepack", "true"); err != nil {
219 return fmt.Errorf("git config http.receivepack true: %s: %w", out, err)
220 }
221
222 relPath, err := filepath.Rel(gitRoot, config.Path)
223 if err != nil {
224 return err
225 }
226
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700227 config.OutsideHTTP = fmt.Sprintf("http://sketch:%s@host.docker.internal:%s", gitSrv.pass, gitSrv.gitPort)
228 config.GitRemoteUrl = fmt.Sprintf("http://sketch:%s@host.docker.internal:%s/.git", gitSrv.pass, gitSrv.gitPort)
Josh Bleecher Snyder664404e2025-06-04 21:56:42 +0000229 config.Upstream = upstream
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700230 config.Commit = commit
231
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -0700232 // Create the sketch container, copy over linux sketch
Earl Lee2e463fb2025-04-17 11:22:22 -0700233 if err := createDockerContainer(ctx, cntrName, hostPort, relPath, imgName, config); err != nil {
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000234 return fmt.Errorf("failed to create docker container: %w", err)
Earl Lee2e463fb2025-04-17 11:22:22 -0700235 }
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -0700236 if err := copyEmbeddedLinuxBinaryToContainer(ctx, cntrName); err != nil {
237 return fmt.Errorf("failed to copy linux binary to container: %w", err)
David Crawshaw8bff16a2025-04-18 01:16:49 -0700238 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700239
David Crawshaw53786ef2025-04-24 12:52:51 -0700240 fmt.Printf("📦 running in container %s\n", cntrName)
Earl Lee2e463fb2025-04-17 11:22:22 -0700241
Philip Zeyligerd4be7a22025-06-15 09:39:00 -0700242 // Setup subtrace if token is provided (development only) - after container creation, before start
243 if config.SubtraceToken != "" {
244 fmt.Println("🔍 Setting up subtrace (development only)")
245 if err := setupSubtraceBeforeStart(ctx, cntrName, config.SubtraceToken); err != nil {
246 return fmt.Errorf("failed to setup subtrace: %w", err)
247 }
248 }
249
Earl Lee2e463fb2025-04-17 11:22:22 -0700250 // Start the sketch container
251 if out, err := combinedOutput(ctx, "docker", "start", cntrName); err != nil {
252 return fmt.Errorf("docker start: %s, %w", out, err)
253 }
254
255 // Copies structured logs from the container to the host.
256 copyLogs := func() {
257 if config.ContainerLogDest == "" {
258 return
259 }
260 out, err := combinedOutput(ctx, "docker", "logs", cntrName)
261 if err != nil {
262 fmt.Fprintf(os.Stderr, "docker logs failed: %v\n", err)
263 return
264 }
Josh Bleecher Snyder7660e4e2025-04-24 10:34:17 -0700265 prefix := []byte("structured logs:")
266 for line := range bytes.Lines(out) {
267 rest, ok := bytes.CutPrefix(line, prefix)
268 if !ok {
Earl Lee2e463fb2025-04-17 11:22:22 -0700269 continue
270 }
Josh Bleecher Snyder7660e4e2025-04-24 10:34:17 -0700271 logFile := string(bytes.TrimSpace(rest))
Earl Lee2e463fb2025-04-17 11:22:22 -0700272 srcPath := fmt.Sprintf("%s:%s", cntrName, logFile)
273 logFileName := filepath.Base(logFile)
274 dstPath := filepath.Join(config.ContainerLogDest, logFileName)
275 _, err := combinedOutput(ctx, "docker", "cp", srcPath, dstPath)
276 if err != nil {
277 fmt.Fprintf(os.Stderr, "docker cp %s %s failed: %v\n", srcPath, dstPath, err)
278 }
279 fmt.Fprintf(os.Stderr, "\ncopied container log %s to %s\n", srcPath, dstPath)
280 }
281 }
282
283 // NOTE: we want to see what the internal sketch binary prints
284 // regardless of the setting of the verbosity flag on the external
285 // binary, so reading "docker logs", which is the stdout/stderr of
286 // the internal binary is not conditional on the verbose flag.
287 appendInternalErr := func(err error) error {
288 if err == nil {
289 return nil
290 }
291 out, logsErr := combinedOutput(ctx, "docker", "logs", cntrName)
Philip Zeyligerd1402952025-04-23 03:54:37 +0000292 if logsErr != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700293 return fmt.Errorf("%w; and docker logs failed: %s, %v", err, out, logsErr)
294 }
295 out = bytes.TrimSpace(out)
296 if len(out) > 0 {
297 return fmt.Errorf("docker logs: %s;\n%w", out, err)
298 }
299 return err
300 }
301
302 // Get the sketch server port from the container
Sean McCulloughae3480f2025-04-23 15:28:20 -0700303 localAddr, err := getContainerPort(ctx, cntrName, "80")
Earl Lee2e463fb2025-04-17 11:22:22 -0700304 if err != nil {
305 return appendInternalErr(err)
306 }
307
Philip Zeyliger00442412025-05-14 11:03:23 -0700308 if config.Verbose {
309 fmt.Fprintf(os.Stderr, "Host web server: http://%s/\n", localAddr)
310 }
311
Sean McCulloughae3480f2025-04-23 15:28:20 -0700312 localSSHAddr, err := getContainerPort(ctx, cntrName, "22")
313 if err != nil {
314 return appendInternalErr(err)
315 }
316 sshHost, sshPort, err := net.SplitHostPort(localSSHAddr)
317 if err != nil {
David Crawshawb5f6a002025-05-05 08:27:16 -0700318 return appendInternalErr(fmt.Errorf("failed to split ssh host and port: %w", err))
Sean McCulloughae3480f2025-04-23 15:28:20 -0700319 }
Sean McCullough4854c652025-04-24 18:37:02 -0700320
Sean McCullough7013e9e2025-05-14 02:03:58 +0000321 var sshServerIdentity, sshUserIdentity, containerCAPublicKey, hostCertificate []byte
Sean McCullough4854c652025-04-24 18:37:02 -0700322
banksean29d689f2025-06-23 15:41:26 +0000323 cst, err := NewLocalSSHimmer(cntrName, sshHost, sshPort)
Sean McCullough078e85a2025-05-08 17:28:34 -0700324 if err != nil {
325 return appendInternalErr(fmt.Errorf("NewContainerSSHTheather: %w", err))
326 }
327
328 sshErr := CheckSSHReachability(cntrName)
Sean McCullough15c95282025-05-08 16:48:38 -0700329 sshAvailable := false
330 sshErrMsg := ""
331 if sshErr != nil {
332 fmt.Println(sshErr.Error())
333 sshErrMsg = sshErr.Error()
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700334 // continue - ssh config is not required for the rest of sketch to function locally.
335 } else {
Sean McCullough15c95282025-05-08 16:48:38 -0700336 sshAvailable = true
Sean McCulloughea3fc202025-04-28 12:53:37 -0700337 // Note: The vscode: link uses an undocumented request parameter that I really had to dig to find:
338 // https://github.com/microsoft/vscode/blob/2b9486161abaca59b5132ce3c59544f3cc7000f6/src/vs/code/electron-main/app.ts#L878
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700339 fmt.Printf(`Connect to this container via any of these methods:
Sean McCullough4854c652025-04-24 18:37:02 -0700340🖥️ ssh %s
341🖥️ code --remote ssh-remote+root@%s /app -n
Sean McCulloughea3fc202025-04-28 12:53:37 -0700342🔗 vscode://vscode-remote/ssh-remote+root@%s/app?windowId=_blank
Sean McCullough4854c652025-04-24 18:37:02 -0700343`, cntrName, cntrName, cntrName)
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700344 sshUserIdentity = cst.userIdentity
345 sshServerIdentity = cst.serverIdentity
Sean McCullough7013e9e2025-05-14 02:03:58 +0000346
347 // Get the Container CA public key for mutual auth
348 if cst.containerCAPublicKey != nil {
349 containerCAPublicKey = ssh.MarshalAuthorizedKey(cst.containerCAPublicKey)
350 fmt.Println("🔒 SSH Mutual Authentication enabled (container will verify host)")
351 }
352
353 // Get the host certificate for mutual auth
354 hostCertificate = cst.hostCertificate
355
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700356 defer func() {
357 if err := cst.Cleanup(); err != nil {
358 appendInternalErr(err)
359 }
360 }()
361 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700362
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700363 // Tell the sketch container to Init(), which starts the SSH server
364 // and checks out the right commit.
365 // TODO: I'm trying to move as much configuration as possible into the command-line
366 // arguments to avoid splitting them up. "localAddr" is the only difficult one:
367 // we run (effectively) "docker run -p 0:80 image sketch -flags" and you can't
368 // get the port Docker chose until after the process starts. The SSH config is
369 // mostly available ahead of time, but whether it works ("sshAvailable"/"sshErrMsg")
370 // may also empirically need to be done after the SSH server is up and running.
Earl Lee2e463fb2025-04-17 11:22:22 -0700371 go func() {
372 // TODO: Why is this called in a goroutine? I have found that when I pull this out
373 // of the goroutine and call it inline, then the terminal UI clears itself and all
374 // the scrollback (which is not good, but also not fatal). I can't see why it does this
375 // though, since none of the calls in postContainerInitConfig obviously write to stdout
376 // or stderr.
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700377 if err := postContainerInitConfig(ctx, localAddr, sshAvailable, sshErrMsg, sshServerIdentity, sshUserIdentity, containerCAPublicKey, hostCertificate); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700378 slog.ErrorContext(ctx, "LaunchContainer.postContainerInitConfig", slog.String("err", err.Error()))
379 errCh <- appendInternalErr(err)
380 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700381
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700382 // 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 -0700383 ps1URL := "http://" + localAddr
384 if config.SkabandAddr != "" {
385 ps1URL = fmt.Sprintf("%s/s/%s", config.SkabandAddr, config.SessionID)
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700386 }
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700387 if config.OpenBrowser {
388 browser.Open(ps1URL)
389 }
390 gitSrv.ps1URL.Store(&ps1URL)
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700391 }()
Earl Lee2e463fb2025-04-17 11:22:22 -0700392
393 go func() {
394 cmd := exec.CommandContext(ctx, "docker", "attach", cntrName)
395 cmd.Stdin = os.Stdin
396 cmd.Stdout = os.Stdout
397 cmd.Stderr = os.Stderr
398 errCh <- run(ctx, "docker attach", cmd)
399 }()
400
401 defer copyLogs()
402
403 for {
404 select {
405 case <-ctx.Done():
406 return ctx.Err()
407 case err := <-errCh:
408 if err != nil {
409 return appendInternalErr(fmt.Errorf("container process: %w", err))
410 }
411 return nil
412 }
413 }
414}
415
416func combinedOutput(ctx context.Context, cmdName string, args ...string) ([]byte, error) {
417 cmd := exec.CommandContext(ctx, cmdName, args...)
Earl Lee2e463fb2025-04-17 11:22:22 -0700418 start := time.Now()
419
420 out, err := cmd.CombinedOutput()
421 if err != nil {
David Crawshawc7e77962025-05-03 13:20:18 -0700422 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 -0700423 } else {
David Crawshawc7e77962025-05-03 13:20:18 -0700424 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 -0700425 }
426 return out, err
427}
428
429func run(ctx context.Context, cmdName string, cmd *exec.Cmd) error {
430 start := time.Now()
431 err := cmd.Run()
432 if err != nil {
David Crawshawc7e77962025-05-03 13:20:18 -0700433 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 -0700434 } else {
David Crawshawc7e77962025-05-03 13:20:18 -0700435 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 -0700436 }
437 return err
438}
439
440type gitServer struct {
441 gitLn net.Listener
442 gitPort string
443 srv *http.Server
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700444 pass string
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700445 ps1URL atomic.Pointer[string]
Earl Lee2e463fb2025-04-17 11:22:22 -0700446}
447
448func (gs *gitServer) shutdown(ctx context.Context) {
449 gs.srv.Shutdown(ctx)
450 gs.gitLn.Close()
451}
452
453// Serve a git remote from the host for the container to fetch from and push to.
454func (gs *gitServer) serve(ctx context.Context) error {
455 slog.DebugContext(ctx, "starting git server", slog.String("git_remote_addr", "http://host.docker.internal:"+gs.gitPort+"/.git"))
456 return gs.srv.Serve(gs.gitLn)
457}
458
459func newGitServer(gitRoot string) (*gitServer, error) {
Josh Bleecher Snyder9f6a9982025-04-22 17:34:15 -0700460 ret := &gitServer{
461 pass: rand.Text(),
462 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700463
Earl Lee2e463fb2025-04-17 11:22:22 -0700464 gitLn, err := net.Listen("tcp4", ":0")
465 if err != nil {
466 return nil, fmt.Errorf("git listen: %w", err)
467 }
468 ret.gitLn = gitLn
469
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700470 browserC := make(chan bool, 1) // channel of browser open requests
471
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000472 go func() {
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700473 for range browserC {
474 browser.Open(*ret.ps1URL.Load())
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000475 }
476 }()
477
478 srv := http.Server{Handler: &gitHTTP{gitRepoRoot: gitRoot, pass: []byte(ret.pass), browserC: browserC}}
Earl Lee2e463fb2025-04-17 11:22:22 -0700479 ret.srv = &srv
480
481 _, gitPort, err := net.SplitHostPort(gitLn.Addr().String())
482 if err != nil {
483 return nil, fmt.Errorf("git port: %w", err)
484 }
485 ret.gitPort = gitPort
486 return ret, nil
487}
488
489func createDockerContainer(ctx context.Context, cntrName, hostPort, relPath, imgName string, config ContainerConfig) error {
David Crawshaw69c67312025-04-17 13:42:00 -0700490 cmdArgs := []string{
491 "create",
David Crawshaw66cf74e2025-05-05 08:48:39 -0700492 "-i",
Earl Lee2e463fb2025-04-17 11:22:22 -0700493 "--name", cntrName,
494 "-p", hostPort + ":80", // forward container port 80 to a host port
David Crawshaw3659d872025-05-05 17:52:23 -0700495 "-e", "SKETCH_MODEL_API_KEY=" + config.ModelAPIKey,
Earl Lee2e463fb2025-04-17 11:22:22 -0700496 }
Philip Zeyliger3d2eff02025-05-27 09:30:31 -0700497 if !(config.OneShot || !config.TermUI) {
David Crawshaw66cf74e2025-05-05 08:48:39 -0700498 cmdArgs = append(cmdArgs, "-t")
499 }
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000500
501 for _, envVar := range getEnvForwardingFromGitConfig(ctx) {
502 cmdArgs = append(cmdArgs, "-e", envVar)
503 }
David Crawshaw5a7b3692025-05-05 16:49:15 -0700504 if config.ModelURL != "" {
David Crawshaw3659d872025-05-05 17:52:23 -0700505 cmdArgs = append(cmdArgs, "-e", "SKETCH_MODEL_URL="+config.ModelURL)
Earl Lee2e463fb2025-04-17 11:22:22 -0700506 }
507 if config.SketchPubKey != "" {
508 cmdArgs = append(cmdArgs, "-e", "SKETCH_PUB_KEY="+config.SketchPubKey)
509 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700510 if config.SSHPort > 0 {
511 cmdArgs = append(cmdArgs, "-p", fmt.Sprintf("%d:22", config.SSHPort)) // forward container ssh port to host ssh port
512 } else {
Philip Zeyliger87d29ef2025-05-16 20:25:28 -0700513 cmdArgs = append(cmdArgs, "-p", "0:22") // use an ephemeral host port for ssh.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700514 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700515 if relPath != "." {
516 cmdArgs = append(cmdArgs, "-w", "/app/"+relPath)
517 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700518 // colima does this by default, but Linux docker seems to need this set explicitly
519 cmdArgs = append(cmdArgs, "--add-host", "host.docker.internal:host-gateway")
Josh Bleecher Snyderac761c92025-05-16 18:58:45 +0000520
David Crawshaw1bd636c2025-06-13 19:56:27 +0000521 // Add seccomp profile to prevent killing PID 1 (the sketch process itself)
522 // Write the seccomp profile to cache directory if it doesn't exist
523 seccompPath, err := ensureSeccompProfile(ctx)
524 if err != nil {
525 return fmt.Errorf("failed to create seccomp profile: %w", err)
526 }
527 cmdArgs = append(cmdArgs, "--security-opt", "seccomp="+seccompPath)
528
Philip Zeyligerd4be7a22025-06-15 09:39:00 -0700529 // Add subtrace environment variable if token is provided
530 if config.SubtraceToken != "" {
531 cmdArgs = append(cmdArgs, "-e", "SUBTRACE_TOKEN="+config.SubtraceToken)
532 cmdArgs = append(cmdArgs, "-e", "SUBTRACE_HTTP2=1")
533 }
534
Josh Bleecher Snyderac761c92025-05-16 18:58:45 +0000535 // Add volume mounts if specified
536 for _, mount := range config.Mounts {
537 if mount != "" {
538 cmdArgs = append(cmdArgs, "-v", mount)
539 }
540 }
Philip Zeyligerd4be7a22025-06-15 09:39:00 -0700541 cmdArgs = append(cmdArgs, imgName)
542
543 // Add command: either [sketch] or [subtrace run -- sketch]
544 if config.SubtraceToken != "" {
545 cmdArgs = append(cmdArgs, "/usr/local/bin/subtrace", "run", "--", "/bin/sketch")
546 } else {
547 cmdArgs = append(cmdArgs, "/bin/sketch")
548 }
549
550 // Add all sketch arguments
551 cmdArgs = append(cmdArgs,
Earl Lee2e463fb2025-04-17 11:22:22 -0700552 "-unsafe",
553 "-addr=:80",
554 "-session-id="+config.SessionID,
Philip Zeyligerd1402952025-04-23 03:54:37 +0000555 "-git-username="+config.GitUsername,
556 "-git-email="+config.GitEmail,
Philip Zeyliger18532b22025-04-23 21:11:46 +0000557 "-outside-hostname="+config.OutsideHostname,
558 "-outside-os="+config.OutsideOS,
559 "-outside-working-dir="+config.OutsideWorkingDir,
Josh Bleecher Snyder33032d32025-05-30 16:28:21 +0000560 fmt.Sprintf("-max-dollars=%f", config.MaxDollars),
Josh Bleecher Snyder3cae7d92025-04-30 09:54:29 -0700561 "-open=false",
Philip Zeyliger613c0f52025-05-15 16:36:22 -0700562 "-termui="+fmt.Sprintf("%t", config.TermUI),
Philip Zeyligercabfa552025-05-19 16:14:28 -0700563 "-verbose="+fmt.Sprintf("%t", config.Verbose),
Josh Bleecher Snyderb1cca6f2025-05-06 01:52:55 +0000564 "-x="+config.ExperimentFlag,
Philip Zeyligerbe7802a2025-06-04 20:15:25 +0000565 "-branch-prefix="+config.BranchPrefix,
philip.zeyliger6d3de482025-06-10 19:38:14 -0700566 "-link-to-github="+fmt.Sprintf("%t", config.LinkToGitHub),
Earl Lee2e463fb2025-04-17 11:22:22 -0700567 )
philip.zeyliger8773e682025-06-11 21:36:21 -0700568 // Set SSH connection string based on session ID for SSH Theater
569 cmdArgs = append(cmdArgs, "-ssh-connection-string=sketch-"+config.SessionID)
David Crawshaw5a7b3692025-05-05 16:49:15 -0700570 if config.Model != "" {
571 cmdArgs = append(cmdArgs, "-model="+config.Model)
572 }
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700573 if config.GitRemoteUrl != "" {
574 cmdArgs = append(cmdArgs, "-git-remote-url="+config.GitRemoteUrl)
575 if config.Commit == "" {
576 panic("Commit should have been set when GitRemoteUrl was set")
577 }
578 cmdArgs = append(cmdArgs, "-commit="+config.Commit)
Josh Bleecher Snyder664404e2025-06-04 21:56:42 +0000579 cmdArgs = append(cmdArgs, "-upstream="+config.Upstream)
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700580 }
581 if config.OutsideHTTP != "" {
582 cmdArgs = append(cmdArgs, "-outside-http="+config.OutsideHTTP)
583 }
Josh Bleecher Snydere3c2f222025-05-15 20:54:52 +0000584 cmdArgs = append(cmdArgs, "-skaband-addr="+config.SkabandAddr)
Pokey Rule0dcebe12025-04-28 14:51:04 +0100585 if config.Prompt != "" {
586 cmdArgs = append(cmdArgs, "-prompt", config.Prompt)
587 }
588 if config.OneShot {
589 cmdArgs = append(cmdArgs, "-one-shot")
Philip Zeyligerb74c4f62025-04-25 19:18:49 -0700590 }
Josh Bleecher Snydere3c2f222025-05-15 20:54:52 +0000591 if config.ModelURL == "" {
592 // Forward ANTHROPIC_API_KEY for direct use.
593 // TODO: have outtie run an http proxy?
594 // TODO: select and forward the relevant API key based on the model
595 cmdArgs = append(cmdArgs, "-llm-api-key="+os.Getenv("ANTHROPIC_API_KEY"))
596 }
Philip Zeyliger194bfa82025-06-24 06:03:06 -0700597 // Add MCP server configurations
598 for _, mcpServer := range config.MCPServers {
599 cmdArgs = append(cmdArgs, "-mcp", mcpServer)
600 }
Philip Zeyliger1dc21372025-05-05 19:54:44 +0000601
602 // Add additional docker arguments if provided
603 if config.DockerArgs != "" {
604 // Parse space-separated docker arguments with support for quotes and escaping
605 args := parseDockerArgs(config.DockerArgs)
606 // Insert arguments after "create" but before other arguments
607 for i := len(args) - 1; i >= 0; i-- {
608 cmdArgs = append(cmdArgs[:1], append([]string{args[i]}, cmdArgs[1:]...)...)
609 }
610 }
611
Earl Lee2e463fb2025-04-17 11:22:22 -0700612 if out, err := combinedOutput(ctx, "docker", cmdArgs...); err != nil {
613 return fmt.Errorf("docker create: %s, %w", out, err)
614 }
615 return nil
616}
617
Sean McCulloughae3480f2025-04-23 15:28:20 -0700618func getContainerPort(ctx context.Context, cntrName, cntrPort string) (string, error) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700619 localAddr := ""
Sean McCulloughae3480f2025-04-23 15:28:20 -0700620 if out, err := combinedOutput(ctx, "docker", "port", cntrName, cntrPort); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700621 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
622 } else {
623 v4, _, found := strings.Cut(string(out), "\n")
624 if !found {
625 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
626 }
627 localAddr = v4
628 if strings.HasPrefix(localAddr, "0.0.0.0") {
629 localAddr = "127.0.0.1" + strings.TrimPrefix(localAddr, "0.0.0.0")
630 }
631 }
632 return localAddr, nil
633}
634
635// Contact the container and configure it.
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700636func postContainerInitConfig(ctx context.Context, localAddr string, sshAvailable bool, sshError string, sshServerIdentity, sshAuthorizedKeys, sshContainerCAKey, sshHostCertificate []byte) error {
Earl Lee2e463fb2025-04-17 11:22:22 -0700637 localURL := "http://" + localAddr
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700638
639 initMsg, err := json.Marshal(
640 server.InitRequest{
Sean McCullough7013e9e2025-05-14 02:03:58 +0000641 HostAddr: localAddr,
642 SSHAuthorizedKeys: sshAuthorizedKeys,
643 SSHServerIdentity: sshServerIdentity,
644 SSHContainerCAKey: sshContainerCAKey,
645 SSHHostCertificate: sshHostCertificate,
646 SSHAvailable: sshAvailable,
647 SSHError: sshError,
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700648 })
Earl Lee2e463fb2025-04-17 11:22:22 -0700649 if err != nil {
650 return fmt.Errorf("init msg: %w", err)
651 }
652
Earl Lee2e463fb2025-04-17 11:22:22 -0700653 // Note: this /init POST is handled in loop/server/loophttp.go:
654 initMsgByteReader := bytes.NewReader(initMsg)
655 req, err := http.NewRequest("POST", localURL+"/init", initMsgByteReader)
656 if err != nil {
657 return err
658 }
659
660 var res *http.Response
661 for i := 0; ; i++ {
662 time.Sleep(100 * time.Millisecond)
663 // If you DON'T reset this byteReader, then subsequent retries may end up sending 0 bytes.
664 initMsgByteReader.Reset(initMsg)
665 res, err = http.DefaultClient.Do(req)
666 if err != nil {
David Crawshaw99231ba2025-05-03 10:48:26 -0700667 if i < 100 {
668 if i%10 == 0 {
669 slog.DebugContext(ctx, "postContainerInitConfig retrying", slog.Int("retry", i), slog.String("err", err.Error()))
670 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700671 continue
672 }
673 return fmt.Errorf("failed to %s/init sketch in container, NOT retrying: err: %v", localURL, err)
674 }
675 break
676 }
677 resBytes, _ := io.ReadAll(res.Body)
678 if res.StatusCode != http.StatusOK {
679 return fmt.Errorf("failed to initialize sketch in container, response status code %d: %s", res.StatusCode, resBytes)
680 }
681 return nil
682}
683
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700684func findOrBuildDockerImage(ctx context.Context, gitRoot, baseImage string, forceRebuild, verbose bool) (imgName string, err error) {
685 // Default to the published sketch image if no base image is specified
686 if baseImage == "" {
687 imageTag := dockerfileBaseHash()
688 baseImage = fmt.Sprintf("%s:%s", dockerImgName, imageTag)
Earl Lee2e463fb2025-04-17 11:22:22 -0700689 }
690
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700691 // Ensure the base image exists locally, pull if necessary
692 if err := ensureBaseImageExists(ctx, baseImage); err != nil {
693 return "", fmt.Errorf("failed to ensure base image %s exists: %w", baseImage, err)
694 }
695
696 // Get the base image container ID for caching
697 baseImageID, err := getDockerImageID(ctx, baseImage)
Earl Lee2e463fb2025-04-17 11:22:22 -0700698 if err != nil {
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700699 return "", fmt.Errorf("failed to get base image ID for %s: %w", baseImage, err)
Earl Lee2e463fb2025-04-17 11:22:22 -0700700 }
701
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700702 // Create a cache key based on base image ID and working directory
703 // Docker naming conventions restrict you to 20 characters per path component
704 // and only allow lowercase letters, digits, underscores, and dashes, so encoding
705 // the hash and the repo directory is sadly a bit of a non-starter.
706 cacheKey := createCacheKey(baseImageID, gitRoot)
707 imgName = "sketch-" + cacheKey
Earl Lee2e463fb2025-04-17 11:22:22 -0700708
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700709 // Check if the cached image exists and is up to date
710 if !forceRebuild {
711 if exists, err := dockerImageExists(ctx, imgName); err != nil {
712 return "", fmt.Errorf("failed to check if image exists: %w", err)
713 } else if exists {
714 if verbose {
715 fmt.Printf("using cached image %s\n", imgName)
Kilian Lackhove23772f42025-06-18 20:28:58 +0200716 }
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700717 return imgName, nil
David Crawshawb5f6a002025-05-05 08:27:16 -0700718 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700719 }
720
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700721 // Build the layered image
722 if err := buildLayeredImage(ctx, imgName, baseImage, gitRoot, verbose); err != nil {
723 return "", fmt.Errorf("failed to build layered image: %w", err)
724 }
725
726 return imgName, nil
727}
728
729// ensureBaseImageExists checks if the base image exists locally and pulls it if not
730func ensureBaseImageExists(ctx context.Context, imageName string) error {
731 exists, err := dockerImageExists(ctx, imageName)
732 if err != nil {
733 return fmt.Errorf("failed to check if image exists: %w", err)
734 }
735
736 if !exists {
737 fmt.Printf("🐋 pulling base image %s...\n", imageName)
738 if out, err := combinedOutput(ctx, "docker", "pull", imageName); err != nil {
739 return fmt.Errorf("docker pull %s failed: %s: %w", imageName, out, err)
740 }
741 fmt.Printf("✅ successfully pulled %s\n", imageName)
742 }
743
744 return nil
745}
746
747// getDockerImageID gets the container ID for a Docker image
748func getDockerImageID(ctx context.Context, imageName string) (string, error) {
749 out, err := combinedOutput(ctx, "docker", "inspect", "--format", "{{.Id}}", imageName)
750 if err != nil {
751 return "", err
752 }
753 return strings.TrimSpace(string(out)), nil
754}
755
756// createCacheKey creates a cache key from base image ID and working directory
757func createCacheKey(baseImageID, gitRoot string) string {
758 h := sha256.New()
759 h.Write([]byte(baseImageID))
760 h.Write([]byte(gitRoot))
761 return hex.EncodeToString(h.Sum(nil))[:12] // Use first 12 chars for shorter name
762}
763
764// dockerImageExists checks if a Docker image exists locally
765func dockerImageExists(ctx context.Context, imageName string) (bool, error) {
766 out, err := combinedOutput(ctx, "docker", "inspect", imageName)
767 if err != nil {
768 if strings.Contains(strings.ToLower(string(out)), "no such object") ||
769 strings.Contains(strings.ToLower(string(out)), "no such image") {
770 return false, nil
771 }
772 return false, err
773 }
774 return true, nil
775}
776
777// buildLayeredImage builds a new Docker image by layering the repo on top of the base image
778// TODO: git config stuff could be environment variables at runtime for email and username.
779// The git docs seem to say that http.postBuffer is a bug in our git proxy more than a thing
780// that's needed, but we haven't found the bug yet!
Philip Zeyliger882b1d12025-07-02 20:04:08 -0700781//
782// TODO: There is a caching tension. A base image is great for tools (like, some version
783// of Go). Then you want a git repo, which is much faster to incrementally fetch rather
784// than cloning every time. Then you want some build artifacts, like perhaps the
785// "go mod download" cache, or the "go build" cache or the "npm install" cache.
786// The implementation here copies the working directory (not just the git repo!),
787// and runs "go mod download". This is an ok compromise, but a power user might want
788// less caching or more caching, depending on their use case. One approach we could take
789// is to punt entirely if /app/.git already exists. If the user has provided a -base-image with
790// their git repo, let's assume they know what they're doing, and they've customized their image
791// for their use case. On the other side of the spectrum is cloning their repo every time,
792// or running git clean -xdf, which minimizes surprises but slows down builds.
793// Note that buildx has some support for conditional COPY, but without buildx, which
794// we can't reliably depend on, we have to run the base image to inspect its file system,
795// and then we can decide what to do.
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700796func buildLayeredImage(ctx context.Context, imgName, baseImage, gitRoot string, _ bool) error {
797 dockerfileContent := fmt.Sprintf(`FROM %s
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700798COPY . /app
799WORKDIR /app
800RUN if [ -f go.mod ]; then go mod download; fi
801CMD ["/bin/sketch"]
802`, baseImage)
803
804 // Create a temporary directory for the Dockerfile
805 tmpDir, err := os.MkdirTemp("", "sketch-docker-*")
806 if err != nil {
807 return fmt.Errorf("failed to create temporary directory: %w", err)
808 }
809 defer os.RemoveAll(tmpDir)
810
811 dockerfilePath := filepath.Join(tmpDir, "Dockerfile")
812 if err := os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0o666); err != nil {
813 return fmt.Errorf("failed to write Dockerfile: %w", err)
814 }
815
816 // Get git user info
Earl Lee2e463fb2025-04-17 11:22:22 -0700817 var gitUserEmail, gitUserName string
818 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.email"); err != nil {
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700819 return fmt.Errorf("git user.email is not set. Please run 'git config --global user.email \"your.email@example.com\"' to set your email address")
Earl Lee2e463fb2025-04-17 11:22:22 -0700820 } else {
821 gitUserEmail = strings.TrimSpace(string(out))
822 }
823 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.name"); err != nil {
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700824 return fmt.Errorf("git user.name is not set. Please run 'git config --global user.name \"Your Name\"' to set your name")
Earl Lee2e463fb2025-04-17 11:22:22 -0700825 } else {
826 gitUserName = strings.TrimSpace(string(out))
827 }
828
829 start := time.Now()
Philip Zeyliger2343f8a2025-06-17 06:16:19 -0700830 cmdArgs := []string{
831 "build",
Earl Lee2e463fb2025-04-17 11:22:22 -0700832 "-t", imgName,
833 "-f", dockerfilePath,
Philip Zeyliger2343f8a2025-06-17 06:16:19 -0700834 "--build-arg", "GIT_USER_EMAIL=" + gitUserEmail,
835 "--build-arg", "GIT_USER_NAME=" + gitUserName,
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700836 ".",
Philip Zeyliger2343f8a2025-06-17 06:16:19 -0700837 }
838
Philip Zeyliger2343f8a2025-06-17 06:16:19 -0700839 cmd := exec.CommandContext(ctx, "docker", cmdArgs...)
David Crawshawb5f6a002025-05-05 08:27:16 -0700840 cmd.Dir = gitRoot
David Crawshaw31f15242025-05-06 16:03:49 -0700841 // We print the docker build output whether or not the user
842 // has selected --verbose. Building an image takes a while
843 // and this gives good context.
David Crawshawb5f6a002025-05-05 08:27:16 -0700844 cmd.Stdout = os.Stdout
845 cmd.Stderr = os.Stderr
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700846 fmt.Printf("🏗️ building docker image %s from base %s...\n", imgName, baseImage)
Earl Lee2e463fb2025-04-17 11:22:22 -0700847
848 err = run(ctx, "docker build", cmd)
849 if err != nil {
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700850 return fmt.Errorf("docker build failed: %v", err)
Earl Lee2e463fb2025-04-17 11:22:22 -0700851 }
852 fmt.Printf("built docker image %s in %s\n", imgName, time.Since(start).Round(time.Millisecond))
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700853 return nil
Earl Lee2e463fb2025-04-17 11:22:22 -0700854}
855
Philip Zeyligerd6d12d12025-05-19 19:19:21 -0700856func checkForEmptyGitRepo(ctx context.Context, path string) error {
857 cmd := exec.CommandContext(ctx, "git", "rev-parse", "-q", "--verify", "HEAD")
858 cmd.Dir = path
859 _, err := cmd.CombinedOutput()
860 if err != nil {
861 return fmt.Errorf("sketch needs to run from within a git repo with at least one commit.\nRun: %s",
862 "git commit --allow-empty -m 'initial commit'")
863 }
864 return nil
865}
866
Earl Lee2e463fb2025-04-17 11:22:22 -0700867func findGitRoot(ctx context.Context, path string) (string, error) {
Marc-Antoine Ruel467c3962025-06-29 13:32:59 -0400868 cmd := exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel")
Earl Lee2e463fb2025-04-17 11:22:22 -0700869 cmd.Dir = path
870 out, err := cmd.CombinedOutput()
871 if err != nil {
872 if strings.Contains(string(out), "not a git repository") {
873 return "", fmt.Errorf(`sketch needs to run from within a git repo, but %s is not part of a git repo.
874Consider one of the following options:
875 - cd to a different dir that is already part of a git repo first, or
876 - to create a new git repo from this directory (%s), run this command:
877
878 git init . && git commit --allow-empty -m "initial commit"
879
880and try running sketch again.
881`, path, path)
882 }
Marc-Antoine Ruel467c3962025-06-29 13:32:59 -0400883 return "", fmt.Errorf("git rev-parse --show-toplevel: %s: %w", out, err)
Earl Lee2e463fb2025-04-17 11:22:22 -0700884 }
Marc-Antoine Ruel467c3962025-06-29 13:32:59 -0400885 // The returned path is absolute.
886 return strings.TrimSpace(string(out)), nil
Earl Lee2e463fb2025-04-17 11:22:22 -0700887}
888
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000889// getEnvForwardingFromGitConfig retrieves environment variables to pass through to Docker
890// from git config using the sketch.envfwd multi-valued key.
891func getEnvForwardingFromGitConfig(ctx context.Context) []string {
892 outb, err := exec.CommandContext(ctx, "git", "config", "--get-all", "sketch.envfwd").CombinedOutput()
893 out := string(outb)
894 if err != nil {
895 if strings.Contains(out, "key does not exist") {
896 return nil
897 }
898 slog.ErrorContext(ctx, "failed to get sketch.envfwd from git config", "err", err, "output", out)
899 return nil
900 }
901
902 var envVars []string
903 for envVar := range strings.Lines(out) {
904 envVar = strings.TrimSpace(envVar)
905 if envVar == "" {
906 continue
907 }
908 envVars = append(envVars, envVar+"="+os.Getenv(envVar))
909 }
910 return envVars
911}
Philip Zeyliger1dc21372025-05-05 19:54:44 +0000912
913// parseDockerArgs parses a string containing space-separated Docker arguments into an array of strings.
914// It handles quoted arguments and escaped characters.
915//
916// Examples:
917//
918// --memory=2g --cpus=2 -> ["--memory=2g", "--cpus=2"]
919// --label="my label" --env=FOO=bar -> ["--label=my label", "--env=FOO=bar"]
920// --env="KEY=\"quoted value\"" -> ["--env=KEY=\"quoted value\""]
921func parseDockerArgs(args string) []string {
922 if args = strings.TrimSpace(args); args == "" {
923 return []string{}
924 }
925
926 var result []string
927 var current strings.Builder
928 inQuotes := false
929 escapeNext := false
930 quoteChar := rune(0)
931
932 for _, char := range args {
933 if escapeNext {
934 current.WriteRune(char)
935 escapeNext = false
936 continue
937 }
938
939 if char == '\\' {
940 escapeNext = true
941 continue
942 }
943
944 if char == '"' || char == '\'' {
945 if !inQuotes {
946 inQuotes = true
947 quoteChar = char
948 continue
949 } else if char == quoteChar {
950 inQuotes = false
951 quoteChar = rune(0)
952 continue
953 }
954 // Non-matching quote character inside quotes
955 current.WriteRune(char)
956 continue
957 }
958
959 // Space outside of quotes is an argument separator
960 if char == ' ' && !inQuotes {
961 if current.Len() > 0 {
962 result = append(result, current.String())
963 current.Reset()
964 }
965 continue
966 }
967
968 current.WriteRune(char)
969 }
970
971 // Add the last argument if there is one
972 if current.Len() > 0 {
973 result = append(result, current.String())
974 }
975
976 return result
977}
Philip Zeyliger4acf0062025-05-22 13:53:46 -0700978
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -0700979// copyEmbeddedLinuxBinaryToContainer copies the embedded linux binary to the container
980func copyEmbeddedLinuxBinaryToContainer(ctx context.Context, containerName string) error {
Josh Bleecher Snyder5ae245b2025-07-08 22:00:24 +0000981 out, err := combinedOutput(ctx, "docker", "version", "--format", "{{.Server.Arch}}")
982 if err != nil {
983 return fmt.Errorf("failed to detect Docker server architecture: %s: %w", out, err)
984 }
985 arch := strings.TrimSpace(string(out))
986
987 bin := embedded.LinuxBinary(arch)
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -0700988 if bin == nil {
Josh Bleecher Snyder5ae245b2025-07-08 22:00:24 +0000989 return fmt.Errorf("no embedded linux binary for architecture %q", arch)
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -0700990 }
991
Josh Bleecher Snyderc9898fd2025-07-08 21:09:18 +0000992 // Stream a tarball to docker cp.
993 pr, pw := io.Pipe()
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -0700994
Josh Bleecher Snyderc9898fd2025-07-08 21:09:18 +0000995 errCh := make(chan error, 1)
996 go func() {
997 defer pw.Close()
998 tw := tar.NewWriter(pw)
999
1000 hdr := &tar.Header{
1001 Name: "bin/sketch", // final path inside the container
1002 Mode: 0o700,
1003 Size: int64(len(bin)),
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -07001004 }
Josh Bleecher Snyderc9898fd2025-07-08 21:09:18 +00001005 if err := tw.WriteHeader(hdr); err != nil {
1006 errCh <- fmt.Errorf("failed to write tar header: %w", err)
1007 return
1008 }
1009 if _, err := tw.Write(bin); err != nil {
1010 errCh <- fmt.Errorf("failed to write binary to tar: %w", err)
1011 return
1012 }
1013 if err := tw.Close(); err != nil {
1014 errCh <- fmt.Errorf("failed to close tar writer: %w", err)
1015 return
1016 }
1017 errCh <- nil
1018 }()
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -07001019
Josh Bleecher Snyderc9898fd2025-07-08 21:09:18 +00001020 cmd := exec.CommandContext(ctx, "docker", "cp", "-", containerName+":/")
1021 cmd.Stdin = pr
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -07001022
Josh Bleecher Snyderc9898fd2025-07-08 21:09:18 +00001023 out, cmdErr := cmd.CombinedOutput()
1024
1025 if tarErr := <-errCh; tarErr != nil {
1026 return tarErr
1027 }
1028 if cmdErr != nil {
1029 return fmt.Errorf("docker cp failed: %s: %w", out, cmdErr)
1030 }
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -07001031 return nil
1032}
1033
David Crawshaw1bd636c2025-06-13 19:56:27 +00001034const seccompProfile = `{
1035 "defaultAction": "SCMP_ACT_ALLOW",
1036 "syscalls": [
1037 {
1038 "names": ["kill", "tkill", "tgkill", "pidfd_send_signal"],
1039 "action": "SCMP_ACT_ERRNO",
1040 "args": [
1041 {
1042 "index": 0,
1043 "value": 1,
1044 "op": "SCMP_CMP_EQ"
1045 }
1046 ]
1047 }
1048 ]
1049}`
1050
1051// ensureSeccompProfile creates the seccomp profile file in the sketch cache directory if it doesn't exist.
1052func ensureSeccompProfile(ctx context.Context) (seccompPath string, err error) {
1053 homeDir, err := os.UserHomeDir()
1054 if err != nil {
1055 return "", fmt.Errorf("failed to get home directory: %w", err)
1056 }
1057 cacheDir := filepath.Join(homeDir, ".cache", "sketch")
1058 if err := os.MkdirAll(cacheDir, 0o755); err != nil {
1059 return "", fmt.Errorf("failed to create cache directory: %w", err)
1060 }
1061 seccompPath = filepath.Join(cacheDir, "seccomp-no-kill-1.json")
1062
1063 curBytes, err := os.ReadFile(seccompPath)
1064 if err != nil && !os.IsNotExist(err) {
1065 return "", fmt.Errorf("failed to read seccomp profile file %s: %w", seccompPath, err)
1066 }
1067 if string(curBytes) == seccompProfile {
1068 return seccompPath, nil // File already exists and matches the expected profile
1069 }
1070
1071 if err := os.WriteFile(seccompPath, []byte(seccompProfile), 0o644); err != nil {
1072 return "", fmt.Errorf("failed to write seccomp profile to %s: %w", seccompPath, err)
1073 }
1074 slog.DebugContext(ctx, "created seccomp profile", "path", seccompPath)
1075 return seccompPath, nil
1076}