| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 1 | // Package dockerimg |
| 2 | package dockerimg |
| 3 | |
| 4 | import ( |
| 5 | "bytes" |
| 6 | "context" |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 7 | "crypto/rand" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 8 | "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 Snyder | 9957046 | 2025-05-05 10:26:14 -0700 | [diff] [blame] | 21 | "sync/atomic" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 22 | "time" |
| 23 | |
| Josh Bleecher Snyder | 78707d6 | 2025-04-30 21:06:49 +0000 | [diff] [blame] | 24 | "sketch.dev/browser" |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 25 | "sketch.dev/llm/ant" |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 26 | "sketch.dev/loop/server" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 27 | "sketch.dev/skribe" |
| Philip Zeyliger | 5d6af87 | 2025-04-23 19:48:34 -0700 | [diff] [blame] | 28 | "sketch.dev/webui" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | // ContainerConfig holds all configuration for launching a container |
| 32 | type 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 Zeyliger | d140295 | 2025-04-23 03:54:37 +0000 | [diff] [blame] | 74 | |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 75 | // Host port for the container's ssh server |
| 76 | SSHPort int |
| 77 | |
| Philip Zeyliger | 18532b2 | 2025-04-23 21:11:46 +0000 | [diff] [blame] | 78 | // Outside information to pass to the container |
| 79 | OutsideHostname string |
| 80 | OutsideOS string |
| 81 | OutsideWorkingDir string |
| Philip Zeyliger | b74c4f6 | 2025-04-25 19:18:49 -0700 | [diff] [blame] | 82 | |
| Pokey Rule | 0dcebe1 | 2025-04-28 14:51:04 +0100 | [diff] [blame] | 83 | // If true, exit after the first turn |
| 84 | OneShot bool |
| 85 | |
| 86 | // Initial prompt |
| 87 | Prompt string |
| Philip Zeyliger | 1b47aa2 | 2025-04-28 19:25:38 +0000 | [diff] [blame] | 88 | |
| 89 | // Initial commit to use as starting point |
| 90 | InitialCommit string |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 91 | |
| 92 | // Verbose enables verbose output |
| 93 | Verbose bool |
| Philip Zeyliger | 1dc2137 | 2025-05-05 19:54:44 +0000 | [diff] [blame] | 94 | |
| 95 | // DockerArgs are additional arguments to pass to the docker create command |
| 96 | DockerArgs string |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 97 | } |
| 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 Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 101 | func LaunchContainer(ctx context.Context, config ContainerConfig) error { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 102 | if _, err := exec.LookPath("docker"); err != nil { |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 103 | 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 108 | } |
| 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 Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 127 | imgName, err := findOrBuildDockerImage(ctx, config.Path, gitRoot, config.AntURL, config.AntAPIKey, config.ForceRebuild, config.Verbose) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 128 | if err != nil { |
| 129 | return err |
| 130 | } |
| 131 | |
| 132 | linuxSketchBin := config.SketchBinaryLinux |
| 133 | if linuxSketchBin == "" { |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 134 | linuxSketchBin, err = buildLinuxSketchBin(ctx) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 135 | if err != nil { |
| 136 | return err |
| 137 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 140 | cntrName := "sketch-" + config.SessionID |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 141 | 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 Zeyliger | 1b47aa2 | 2025-04-28 19:25:38 +0000 | [diff] [blame] | 171 | 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 173 | } 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 Snyder | 2772f63 | 2025-05-01 21:42:35 +0000 | [diff] [blame] | 187 | return fmt.Errorf("failed to create docker container: %w", err) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 188 | } |
| 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 McCullough | f5bb3d3 | 2025-04-18 10:47:59 -0700 | [diff] [blame] | 194 | |
| 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 Crawshaw | 8bff16a | 2025-04-18 01:16:49 -0700 | [diff] [blame] | 201 | 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 208 | |
| David Crawshaw | 53786ef | 2025-04-24 12:52:51 -0700 | [diff] [blame] | 209 | fmt.Printf("📦 running in container %s\n", cntrName) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 210 | |
| 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 Snyder | 7660e4e | 2025-04-24 10:34:17 -0700 | [diff] [blame] | 226 | prefix := []byte("structured logs:") |
| 227 | for line := range bytes.Lines(out) { |
| 228 | rest, ok := bytes.CutPrefix(line, prefix) |
| 229 | if !ok { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 230 | continue |
| 231 | } |
| Josh Bleecher Snyder | 7660e4e | 2025-04-24 10:34:17 -0700 | [diff] [blame] | 232 | logFile := string(bytes.TrimSpace(rest)) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 233 | 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 Zeyliger | d140295 | 2025-04-23 03:54:37 +0000 | [diff] [blame] | 253 | if logsErr != nil { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 254 | 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 McCullough | ae3480f | 2025-04-23 15:28:20 -0700 | [diff] [blame] | 264 | localAddr, err := getContainerPort(ctx, cntrName, "80") |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 265 | if err != nil { |
| 266 | return appendInternalErr(err) |
| 267 | } |
| 268 | |
| Sean McCullough | ae3480f | 2025-04-23 15:28:20 -0700 | [diff] [blame] | 269 | 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 Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 275 | return appendInternalErr(fmt.Errorf("failed to split ssh host and port: %w", err)) |
| Sean McCullough | ae3480f | 2025-04-23 15:28:20 -0700 | [diff] [blame] | 276 | } |
| Sean McCullough | 4854c65 | 2025-04-24 18:37:02 -0700 | [diff] [blame] | 277 | |
| Sean McCullough | f5e28f6 | 2025-04-25 10:48:00 -0700 | [diff] [blame] | 278 | var sshServerIdentity, sshUserIdentity []byte |
| Sean McCullough | 4854c65 | 2025-04-24 18:37:02 -0700 | [diff] [blame] | 279 | |
| Sean McCullough | f5e28f6 | 2025-04-25 10:48:00 -0700 | [diff] [blame] | 280 | 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 Snyder | 50608b1 | 2025-05-03 22:55:49 +0000 | [diff] [blame] | 284 | cst, err := NewSSHTheater(cntrName, sshHost, sshPort) |
| Sean McCullough | f5e28f6 | 2025-04-25 10:48:00 -0700 | [diff] [blame] | 285 | if err != nil { |
| 286 | return appendInternalErr(fmt.Errorf("NewContainerSSHTheather: %w", err)) |
| 287 | } |
| 288 | |
| Sean McCullough | ea3fc20 | 2025-04-28 12:53:37 -0700 | [diff] [blame] | 289 | // 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 McCullough | f5e28f6 | 2025-04-25 10:48:00 -0700 | [diff] [blame] | 291 | fmt.Printf(`Connect to this container via any of these methods: |
| Sean McCullough | 4854c65 | 2025-04-24 18:37:02 -0700 | [diff] [blame] | 292 | 🖥️ ssh %s |
| 293 | 🖥️ code --remote ssh-remote+root@%s /app -n |
| Sean McCullough | ea3fc20 | 2025-04-28 12:53:37 -0700 | [diff] [blame] | 294 | 🔗 vscode://vscode-remote/ssh-remote+root@%s/app?windowId=_blank |
| Sean McCullough | 4854c65 | 2025-04-24 18:37:02 -0700 | [diff] [blame] | 295 | `, cntrName, cntrName, cntrName) |
| Sean McCullough | f5e28f6 | 2025-04-25 10:48:00 -0700 | [diff] [blame] | 296 | sshUserIdentity = cst.userIdentity |
| 297 | sshServerIdentity = cst.serverIdentity |
| 298 | defer func() { |
| 299 | if err := cst.Cleanup(); err != nil { |
| 300 | appendInternalErr(err) |
| 301 | } |
| 302 | }() |
| 303 | } |
| Sean McCullough | ae3480f | 2025-04-23 15:28:20 -0700 | [diff] [blame] | 304 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 305 | // 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 McCullough | f5e28f6 | 2025-04-25 10:48:00 -0700 | [diff] [blame] | 312 | if err := postContainerInitConfig(ctx, localAddr, commit, gitSrv.gitPort, gitSrv.pass, sshServerIdentity, sshUserIdentity); err != nil { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 313 | slog.ErrorContext(ctx, "LaunchContainer.postContainerInitConfig", slog.String("err", err.Error())) |
| 314 | errCh <- appendInternalErr(err) |
| 315 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 316 | |
| Philip Zeyliger | 6ed6adb | 2025-04-23 19:56:38 -0700 | [diff] [blame] | 317 | // We open the browser after the init config because the above waits for the web server to be serving. |
| Josh Bleecher Snyder | 9957046 | 2025-05-05 10:26:14 -0700 | [diff] [blame] | 318 | ps1URL := "http://" + localAddr |
| 319 | if config.SkabandAddr != "" { |
| 320 | ps1URL = fmt.Sprintf("%s/s/%s", config.SkabandAddr, config.SessionID) |
| Philip Zeyliger | 6ed6adb | 2025-04-23 19:56:38 -0700 | [diff] [blame] | 321 | } |
| Josh Bleecher Snyder | 9957046 | 2025-05-05 10:26:14 -0700 | [diff] [blame] | 322 | if config.OpenBrowser { |
| 323 | browser.Open(ps1URL) |
| 324 | } |
| 325 | gitSrv.ps1URL.Store(&ps1URL) |
| Philip Zeyliger | 6ed6adb | 2025-04-23 19:56:38 -0700 | [diff] [blame] | 326 | }() |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 327 | |
| 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 | |
| 351 | func combinedOutput(ctx context.Context, cmdName string, args ...string) ([]byte, error) { |
| 352 | cmd := exec.CommandContext(ctx, cmdName, args...) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 353 | start := time.Now() |
| 354 | |
| 355 | out, err := cmd.CombinedOutput() |
| 356 | if err != nil { |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 357 | 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 358 | } else { |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 359 | 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 360 | } |
| 361 | return out, err |
| 362 | } |
| 363 | |
| 364 | func run(ctx context.Context, cmdName string, cmd *exec.Cmd) error { |
| 365 | start := time.Now() |
| 366 | err := cmd.Run() |
| 367 | if err != nil { |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 368 | 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 369 | } else { |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 370 | 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 371 | } |
| 372 | return err |
| 373 | } |
| 374 | |
| 375 | type gitServer struct { |
| 376 | gitLn net.Listener |
| 377 | gitPort string |
| 378 | srv *http.Server |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 379 | pass string |
| Josh Bleecher Snyder | 9957046 | 2025-05-05 10:26:14 -0700 | [diff] [blame] | 380 | ps1URL atomic.Pointer[string] |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | func (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. |
| 389 | func (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 | |
| 394 | func newGitServer(gitRoot string) (*gitServer, error) { |
| Josh Bleecher Snyder | 9f6a998 | 2025-04-22 17:34:15 -0700 | [diff] [blame] | 395 | ret := &gitServer{ |
| 396 | pass: rand.Text(), |
| 397 | } |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 398 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 399 | 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 Snyder | 9957046 | 2025-05-05 10:26:14 -0700 | [diff] [blame] | 405 | browserC := make(chan bool, 1) // channel of browser open requests |
| 406 | |
| Josh Bleecher Snyder | 3e2111b | 2025-04-30 17:53:28 +0000 | [diff] [blame] | 407 | go func() { |
| Josh Bleecher Snyder | 9957046 | 2025-05-05 10:26:14 -0700 | [diff] [blame] | 408 | for range browserC { |
| 409 | browser.Open(*ret.ps1URL.Load()) |
| Josh Bleecher Snyder | 3e2111b | 2025-04-30 17:53:28 +0000 | [diff] [blame] | 410 | } |
| 411 | }() |
| 412 | |
| 413 | srv := http.Server{Handler: &gitHTTP{gitRepoRoot: gitRoot, pass: []byte(ret.pass), browserC: browserC}} |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 414 | 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 | |
| 424 | func createDockerContainer(ctx context.Context, cntrName, hostPort, relPath, imgName string, config ContainerConfig) error { |
| David Crawshaw | 69c6731 | 2025-04-17 13:42:00 -0700 | [diff] [blame] | 425 | cmdArgs := []string{ |
| 426 | "create", |
| David Crawshaw | 66cf74e | 2025-05-05 08:48:39 -0700 | [diff] [blame] | 427 | "-i", |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 428 | "--name", cntrName, |
| 429 | "-p", hostPort + ":80", // forward container port 80 to a host port |
| Philip Zeyliger | 6234a8d | 2025-05-02 14:31:20 -0700 | [diff] [blame] | 430 | "-e", "SKETCH_ANTHROPIC_API_KEY=" + config.AntAPIKey, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 431 | } |
| David Crawshaw | 66cf74e | 2025-05-05 08:48:39 -0700 | [diff] [blame] | 432 | if !config.OneShot { |
| 433 | cmdArgs = append(cmdArgs, "-t") |
| 434 | } |
| Josh Bleecher Snyder | 2772f63 | 2025-05-01 21:42:35 +0000 | [diff] [blame] | 435 | |
| 436 | for _, envVar := range getEnvForwardingFromGitConfig(ctx) { |
| 437 | cmdArgs = append(cmdArgs, "-e", envVar) |
| 438 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 439 | if config.AntURL != "" { |
| Philip Zeyliger | 6234a8d | 2025-05-02 14:31:20 -0700 | [diff] [blame] | 440 | cmdArgs = append(cmdArgs, "-e", "SKETCH_ANT_URL="+config.AntURL) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 441 | } |
| 442 | if config.SketchPubKey != "" { |
| 443 | cmdArgs = append(cmdArgs, "-e", "SKETCH_PUB_KEY="+config.SketchPubKey) |
| 444 | } |
| Sean McCullough | ae3480f | 2025-04-23 15:28:20 -0700 | [diff] [blame] | 445 | 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 McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 449 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 450 | if relPath != "." { |
| 451 | cmdArgs = append(cmdArgs, "-w", "/app/"+relPath) |
| 452 | } |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 453 | // 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 455 | cmdArgs = append( |
| 456 | cmdArgs, |
| 457 | imgName, |
| 458 | "/bin/sketch", |
| 459 | "-unsafe", |
| 460 | "-addr=:80", |
| 461 | "-session-id="+config.SessionID, |
| Philip Zeyliger | d140295 | 2025-04-23 03:54:37 +0000 | [diff] [blame] | 462 | "-git-username="+config.GitUsername, |
| 463 | "-git-email="+config.GitEmail, |
| Philip Zeyliger | 18532b2 | 2025-04-23 21:11:46 +0000 | [diff] [blame] | 464 | "-outside-hostname="+config.OutsideHostname, |
| 465 | "-outside-os="+config.OutsideOS, |
| 466 | "-outside-working-dir="+config.OutsideWorkingDir, |
| Josh Bleecher Snyder | 3cae7d9 | 2025-04-30 09:54:29 -0700 | [diff] [blame] | 467 | "-open=false", |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 468 | ) |
| 469 | if config.SkabandAddr != "" { |
| 470 | cmdArgs = append(cmdArgs, "-skaband-addr="+config.SkabandAddr) |
| 471 | } |
| Pokey Rule | 0dcebe1 | 2025-04-28 14:51:04 +0100 | [diff] [blame] | 472 | if config.Prompt != "" { |
| 473 | cmdArgs = append(cmdArgs, "-prompt", config.Prompt) |
| 474 | } |
| 475 | if config.OneShot { |
| 476 | cmdArgs = append(cmdArgs, "-one-shot") |
| Philip Zeyliger | b74c4f6 | 2025-04-25 19:18:49 -0700 | [diff] [blame] | 477 | } |
| Philip Zeyliger | 1dc2137 | 2025-05-05 19:54:44 +0000 | [diff] [blame] | 478 | |
| 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 489 | 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 Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 495 | func buildLinuxSketchBin(ctx context.Context) (string, error) { |
| David Crawshaw | 8a617cb | 2025-04-18 01:28:43 -0700 | [diff] [blame] | 496 | homeDir, err := os.UserHomeDir() |
| David Crawshaw | 69c6731 | 2025-04-17 13:42:00 -0700 | [diff] [blame] | 497 | if err != nil { |
| 498 | return "", err |
| 499 | } |
| David Crawshaw | 8a617cb | 2025-04-18 01:28:43 -0700 | [diff] [blame] | 500 | 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 Crawshaw | 094e4d2 | 2025-04-24 11:35:14 -0700 | [diff] [blame] | 510 | slog.DebugContext(ctx, "built linux agent from currently checked out module") |
| David Crawshaw | 8a617cb | 2025-04-18 01:28:43 -0700 | [diff] [blame] | 511 | verToInstall = "" |
| 512 | } |
| 513 | } |
| David Crawshaw | 69c6731 | 2025-04-17 13:42:00 -0700 | [diff] [blame] | 514 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 515 | start := time.Now() |
| David Crawshaw | 8a617cb | 2025-04-18 01:28:43 -0700 | [diff] [blame] | 516 | cmd := exec.CommandContext(ctx, "go", "install", "sketch.dev/cmd/sketch"+verToInstall) |
| David Crawshaw | b9eaef5 | 2025-04-17 15:23:18 -0700 | [diff] [blame] | 517 | cmd.Env = append( |
| 518 | os.Environ(), |
| 519 | "GOOS=linux", |
| 520 | "CGO_ENABLED=0", |
| 521 | "GOTOOLCHAIN=auto", |
| David Crawshaw | 8a617cb | 2025-04-18 01:28:43 -0700 | [diff] [blame] | 522 | "GOPATH="+linuxGopath, |
| Josh Bleecher Snyder | fae1757 | 2025-04-21 11:48:05 -0700 | [diff] [blame] | 523 | "GOBIN=", |
| David Crawshaw | b9eaef5 | 2025-04-17 15:23:18 -0700 | [diff] [blame] | 524 | ) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 525 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 526 | out, err := cmd.CombinedOutput() |
| 527 | if err != nil { |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 528 | 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 529 | return "", fmt.Errorf("failed to build linux sketch binary: %s: %w", out, err) |
| 530 | } else { |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 531 | 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 534 | if runtime.GOOS != "linux" { |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 535 | return filepath.Join(linuxGopath, "bin", "linux_"+runtime.GOARCH, "sketch"), nil |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 536 | } |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 537 | // If we are already on Linux, there's no extra platform name in the path |
| 538 | return filepath.Join(linuxGopath, "bin", "sketch"), nil |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| Sean McCullough | ae3480f | 2025-04-23 15:28:20 -0700 | [diff] [blame] | 541 | func getContainerPort(ctx context.Context, cntrName, cntrPort string) (string, error) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 542 | localAddr := "" |
| Sean McCullough | ae3480f | 2025-04-23 15:28:20 -0700 | [diff] [blame] | 543 | if out, err := combinedOutput(ctx, "docker", "port", cntrName, cntrPort); err != nil { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 544 | 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 McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 559 | func postContainerInitConfig(ctx context.Context, localAddr, commit, gitPort, gitPass string, sshServerIdentity, sshAuthorizedKeys []byte) error { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 560 | localURL := "http://" + localAddr |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 561 | |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 562 | // 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 McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 570 | initMsg, err := json.Marshal( |
| 571 | server.InitRequest{ |
| 572 | Commit: commit, |
| Josh Bleecher Snyder | 3e2111b | 2025-04-30 17:53:28 +0000 | [diff] [blame] | 573 | OutsideHTTP: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s", gitPass, gitPort), |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 574 | GitRemoteAddr: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s/.git", gitPass, gitPort), |
| 575 | HostAddr: localAddr, |
| 576 | SSHAuthorizedKeys: sshAuthorizedKeys, |
| 577 | SSHServerIdentity: sshServerIdentity, |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 578 | SSHAvailable: sshAvailable, |
| 579 | SSHError: sshError, |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 580 | }) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 581 | if err != nil { |
| 582 | return fmt.Errorf("init msg: %w", err) |
| 583 | } |
| 584 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 585 | // 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 Crawshaw | 99231ba | 2025-05-03 10:48:26 -0700 | [diff] [blame] | 599 | 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 603 | 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 Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 616 | func findOrBuildDockerImage(ctx context.Context, cwd, gitRoot, antURL, antAPIKey string, forceRebuild, verbose bool) (imgName string, err error) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 617 | 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 653 | 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 666 | return imgName, nil |
| 667 | } |
| 668 | |
| 669 | start := time.Now() |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 670 | srv := &ant.Service{ |
| 671 | URL: antURL, |
| 672 | APIKey: antAPIKey, |
| 673 | HTTPC: http.DefaultClient, |
| 674 | } |
| 675 | dockerfile, err := createDockerfile(ctx, srv, initFiles, subPathWorkingDir) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 676 | if err != nil { |
| 677 | return "", fmt.Errorf("create dockerfile: %w", err) |
| 678 | } |
| David Crawshaw | 8fd5104 | 2025-05-05 12:52:43 -0700 | [diff] [blame] | 679 | dockerfilePath = filepath.Join(cwd, tmpSketchDockerfile) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 680 | if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0o666); err != nil { |
| 681 | return "", err |
| 682 | } |
| 683 | defer os.Remove(dockerfilePath) |
| 684 | |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 685 | 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 688 | } |
| 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 709 | ) |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 710 | if !verbose { |
| 711 | cmd.Args = append(cmd.Args, "--progress=quiet") |
| Philip Zeyliger | e4fa0e3 | 2025-04-23 14:15:55 -0700 | [diff] [blame] | 712 | } |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 713 | 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 Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 718 | |
| 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 | |
| 727 | func 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. |
| 751 | func 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 | |
| 771 | func 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. |
| 778 | Consider 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 | |
| 784 | and 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 Snyder | 2772f63 | 2025-05-01 21:42:35 +0000 | [diff] [blame] | 794 | // getEnvForwardingFromGitConfig retrieves environment variables to pass through to Docker |
| 795 | // from git config using the sketch.envfwd multi-valued key. |
| 796 | func 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 Zeyliger | 1dc2137 | 2025-05-05 19:54:44 +0000 | [diff] [blame] | 817 | |
| 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\""] |
| 826 | func 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 | } |