| 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" |
| 21 | "time" |
| 22 | |
| Josh Bleecher Snyder | 78707d6 | 2025-04-30 21:06:49 +0000 | [diff] [blame] | 23 | "sketch.dev/browser" |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 24 | "sketch.dev/llm/ant" |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 25 | "sketch.dev/loop/server" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 26 | "sketch.dev/skribe" |
| Philip Zeyliger | 5d6af87 | 2025-04-23 19:48:34 -0700 | [diff] [blame] | 27 | "sketch.dev/webui" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | // ContainerConfig holds all configuration for launching a container |
| 31 | type ContainerConfig struct { |
| 32 | // SessionID is the unique identifier for this session |
| 33 | SessionID string |
| 34 | |
| 35 | // LocalAddr is the initial address to use (though it may be overwritten later) |
| 36 | LocalAddr string |
| 37 | |
| 38 | // SkabandAddr is the address of the skaband service if available |
| 39 | SkabandAddr string |
| 40 | |
| 41 | // AntURL is the URL of the LLM service. |
| 42 | AntURL string |
| 43 | |
| 44 | // AntAPIKey is the API key for LLM service. |
| 45 | AntAPIKey string |
| 46 | |
| 47 | // Path is the local filesystem path to use |
| 48 | Path string |
| 49 | |
| 50 | // GitUsername is the username to use for git operations |
| 51 | GitUsername string |
| 52 | |
| 53 | // GitEmail is the email to use for git operations |
| 54 | GitEmail string |
| 55 | |
| 56 | // OpenBrowser determines whether to open a browser automatically |
| 57 | OpenBrowser bool |
| 58 | |
| 59 | // NoCleanup prevents container cleanup when set to true |
| 60 | NoCleanup bool |
| 61 | |
| 62 | // ForceRebuild forces rebuilding of the Docker image even if it exists |
| 63 | ForceRebuild bool |
| 64 | |
| 65 | // Host directory to copy container logs into, if not set to "" |
| 66 | ContainerLogDest string |
| 67 | |
| 68 | // Path to pre-built linux sketch binary, or build a new one if set to "" |
| 69 | SketchBinaryLinux string |
| 70 | |
| 71 | // Sketch client public key. |
| 72 | SketchPubKey string |
| Philip Zeyliger | d140295 | 2025-04-23 03:54:37 +0000 | [diff] [blame] | 73 | |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 74 | // Host port for the container's ssh server |
| 75 | SSHPort int |
| 76 | |
| Philip Zeyliger | 18532b2 | 2025-04-23 21:11:46 +0000 | [diff] [blame] | 77 | // Outside information to pass to the container |
| 78 | OutsideHostname string |
| 79 | OutsideOS string |
| 80 | OutsideWorkingDir string |
| Philip Zeyliger | b74c4f6 | 2025-04-25 19:18:49 -0700 | [diff] [blame] | 81 | |
| Pokey Rule | 0dcebe1 | 2025-04-28 14:51:04 +0100 | [diff] [blame] | 82 | // If true, exit after the first turn |
| 83 | OneShot bool |
| 84 | |
| 85 | // Initial prompt |
| 86 | Prompt string |
| Philip Zeyliger | 1b47aa2 | 2025-04-28 19:25:38 +0000 | [diff] [blame] | 87 | |
| 88 | // Initial commit to use as starting point |
| 89 | InitialCommit string |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 90 | |
| 91 | // Verbose enables verbose output |
| 92 | Verbose bool |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // LaunchContainer creates a docker container for a project, installs sketch and opens a connection to it. |
| 96 | // It writes status to stdout. |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 97 | func LaunchContainer(ctx context.Context, config ContainerConfig) error { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 98 | if _, err := exec.LookPath("docker"); err != nil { |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 99 | if runtime.GOOS == "darwin" { |
| 100 | return fmt.Errorf("cannot find `docker` binary; run: brew install docker colima && colima start") |
| 101 | } else { |
| 102 | return fmt.Errorf("cannot find `docker` binary; install docker (e.g., apt-get install docker.io)") |
| 103 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if out, err := combinedOutput(ctx, "docker", "ps"); err != nil { |
| 107 | // `docker ps` provides a good error message here that can be |
| 108 | // easily chatgpt'ed by users, so send it to the user as-is: |
| 109 | // Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? |
| 110 | return fmt.Errorf("docker ps: %s (%w)", out, err) |
| 111 | } |
| 112 | |
| 113 | _, hostPort, err := net.SplitHostPort(config.LocalAddr) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | |
| 118 | gitRoot, err := findGitRoot(ctx, config.Path) |
| 119 | if err != nil { |
| 120 | return err |
| 121 | } |
| 122 | |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 123 | 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] | 124 | if err != nil { |
| 125 | return err |
| 126 | } |
| 127 | |
| 128 | linuxSketchBin := config.SketchBinaryLinux |
| 129 | if linuxSketchBin == "" { |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 130 | linuxSketchBin, err = buildLinuxSketchBin(ctx) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 131 | if err != nil { |
| 132 | return err |
| 133 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 136 | cntrName := "sketch-" + config.SessionID |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 137 | defer func() { |
| 138 | if config.NoCleanup { |
| 139 | return |
| 140 | } |
| 141 | if out, err := combinedOutput(ctx, "docker", "kill", cntrName); err != nil { |
| 142 | // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err) |
| 143 | _ = out |
| 144 | } |
| 145 | if out, err := combinedOutput(ctx, "docker", "rm", cntrName); err != nil { |
| 146 | // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err) |
| 147 | _ = out |
| 148 | } |
| 149 | }() |
| 150 | |
| 151 | // errCh receives errors from operations that this function calls in separate goroutines. |
| 152 | errCh := make(chan error) |
| 153 | |
| 154 | // Start the git server |
| 155 | gitSrv, err := newGitServer(gitRoot) |
| 156 | if err != nil { |
| 157 | return fmt.Errorf("failed to start git server: %w", err) |
| 158 | } |
| 159 | defer gitSrv.shutdown(ctx) |
| 160 | |
| 161 | go func() { |
| 162 | errCh <- gitSrv.serve(ctx) |
| 163 | }() |
| 164 | |
| 165 | // Get the current host git commit |
| 166 | var commit string |
| Philip Zeyliger | 1b47aa2 | 2025-04-28 19:25:38 +0000 | [diff] [blame] | 167 | if out, err := combinedOutput(ctx, "git", "rev-parse", config.InitialCommit); err != nil { |
| 168 | return fmt.Errorf("git rev-parse %s: %w", config.InitialCommit, err) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 169 | } else { |
| 170 | commit = strings.TrimSpace(string(out)) |
| 171 | } |
| 172 | if out, err := combinedOutput(ctx, "git", "config", "http.receivepack", "true"); err != nil { |
| 173 | return fmt.Errorf("git config http.receivepack true: %s: %w", out, err) |
| 174 | } |
| 175 | |
| 176 | relPath, err := filepath.Rel(gitRoot, config.Path) |
| 177 | if err != nil { |
| 178 | return err |
| 179 | } |
| 180 | |
| 181 | // Create the sketch container |
| 182 | if err := createDockerContainer(ctx, cntrName, hostPort, relPath, imgName, config); err != nil { |
| Josh Bleecher Snyder | 2772f63 | 2025-05-01 21:42:35 +0000 | [diff] [blame] | 183 | return fmt.Errorf("failed to create docker container: %w", err) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Copy the sketch linux binary into the container |
| 187 | if out, err := combinedOutput(ctx, "docker", "cp", linuxSketchBin, cntrName+":/bin/sketch"); err != nil { |
| 188 | return fmt.Errorf("docker cp: %s, %w", out, err) |
| 189 | } |
| Sean McCullough | f5bb3d3 | 2025-04-18 10:47:59 -0700 | [diff] [blame] | 190 | |
| 191 | // Make sure that the webui is built so we can copy the results to the container. |
| 192 | _, err = webui.Build() |
| 193 | if err != nil { |
| 194 | return fmt.Errorf("failed to build webui: %w", err) |
| 195 | } |
| 196 | |
| David Crawshaw | 8bff16a | 2025-04-18 01:16:49 -0700 | [diff] [blame] | 197 | webuiZipPath, err := webui.ZipPath() |
| 198 | if err != nil { |
| 199 | return err |
| 200 | } |
| 201 | if out, err := combinedOutput(ctx, "docker", "cp", webuiZipPath, cntrName+":/root/.cache/sketch/webui/"+filepath.Base(webuiZipPath)); err != nil { |
| 202 | return fmt.Errorf("docker cp: %s, %w", out, err) |
| 203 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 204 | |
| David Crawshaw | 53786ef | 2025-04-24 12:52:51 -0700 | [diff] [blame] | 205 | fmt.Printf("📦 running in container %s\n", cntrName) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 206 | |
| 207 | // Start the sketch container |
| 208 | if out, err := combinedOutput(ctx, "docker", "start", cntrName); err != nil { |
| 209 | return fmt.Errorf("docker start: %s, %w", out, err) |
| 210 | } |
| 211 | |
| 212 | // Copies structured logs from the container to the host. |
| 213 | copyLogs := func() { |
| 214 | if config.ContainerLogDest == "" { |
| 215 | return |
| 216 | } |
| 217 | out, err := combinedOutput(ctx, "docker", "logs", cntrName) |
| 218 | if err != nil { |
| 219 | fmt.Fprintf(os.Stderr, "docker logs failed: %v\n", err) |
| 220 | return |
| 221 | } |
| Josh Bleecher Snyder | 7660e4e | 2025-04-24 10:34:17 -0700 | [diff] [blame] | 222 | prefix := []byte("structured logs:") |
| 223 | for line := range bytes.Lines(out) { |
| 224 | rest, ok := bytes.CutPrefix(line, prefix) |
| 225 | if !ok { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 226 | continue |
| 227 | } |
| Josh Bleecher Snyder | 7660e4e | 2025-04-24 10:34:17 -0700 | [diff] [blame] | 228 | logFile := string(bytes.TrimSpace(rest)) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 229 | srcPath := fmt.Sprintf("%s:%s", cntrName, logFile) |
| 230 | logFileName := filepath.Base(logFile) |
| 231 | dstPath := filepath.Join(config.ContainerLogDest, logFileName) |
| 232 | _, err := combinedOutput(ctx, "docker", "cp", srcPath, dstPath) |
| 233 | if err != nil { |
| 234 | fmt.Fprintf(os.Stderr, "docker cp %s %s failed: %v\n", srcPath, dstPath, err) |
| 235 | } |
| 236 | fmt.Fprintf(os.Stderr, "\ncopied container log %s to %s\n", srcPath, dstPath) |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // NOTE: we want to see what the internal sketch binary prints |
| 241 | // regardless of the setting of the verbosity flag on the external |
| 242 | // binary, so reading "docker logs", which is the stdout/stderr of |
| 243 | // the internal binary is not conditional on the verbose flag. |
| 244 | appendInternalErr := func(err error) error { |
| 245 | if err == nil { |
| 246 | return nil |
| 247 | } |
| 248 | out, logsErr := combinedOutput(ctx, "docker", "logs", cntrName) |
| Philip Zeyliger | d140295 | 2025-04-23 03:54:37 +0000 | [diff] [blame] | 249 | if logsErr != nil { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 250 | return fmt.Errorf("%w; and docker logs failed: %s, %v", err, out, logsErr) |
| 251 | } |
| 252 | out = bytes.TrimSpace(out) |
| 253 | if len(out) > 0 { |
| 254 | return fmt.Errorf("docker logs: %s;\n%w", out, err) |
| 255 | } |
| 256 | return err |
| 257 | } |
| 258 | |
| 259 | // Get the sketch server port from the container |
| Sean McCullough | ae3480f | 2025-04-23 15:28:20 -0700 | [diff] [blame] | 260 | localAddr, err := getContainerPort(ctx, cntrName, "80") |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 261 | if err != nil { |
| 262 | return appendInternalErr(err) |
| 263 | } |
| 264 | |
| Sean McCullough | ae3480f | 2025-04-23 15:28:20 -0700 | [diff] [blame] | 265 | localSSHAddr, err := getContainerPort(ctx, cntrName, "22") |
| 266 | if err != nil { |
| 267 | return appendInternalErr(err) |
| 268 | } |
| 269 | sshHost, sshPort, err := net.SplitHostPort(localSSHAddr) |
| 270 | if err != nil { |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 271 | 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] | 272 | } |
| Sean McCullough | 4854c65 | 2025-04-24 18:37:02 -0700 | [diff] [blame] | 273 | |
| Sean McCullough | f5e28f6 | 2025-04-25 10:48:00 -0700 | [diff] [blame] | 274 | var sshServerIdentity, sshUserIdentity []byte |
| Sean McCullough | 4854c65 | 2025-04-24 18:37:02 -0700 | [diff] [blame] | 275 | |
| Sean McCullough | f5e28f6 | 2025-04-25 10:48:00 -0700 | [diff] [blame] | 276 | if err := CheckForInclude(); err != nil { |
| 277 | fmt.Println(err.Error()) |
| 278 | // continue - ssh config is not required for the rest of sketch to function locally. |
| 279 | } else { |
| 280 | cst, err := NewSSHTheather(cntrName, sshHost, sshPort) |
| 281 | if err != nil { |
| 282 | return appendInternalErr(fmt.Errorf("NewContainerSSHTheather: %w", err)) |
| 283 | } |
| 284 | |
| Sean McCullough | ea3fc20 | 2025-04-28 12:53:37 -0700 | [diff] [blame] | 285 | // Note: The vscode: link uses an undocumented request parameter that I really had to dig to find: |
| 286 | // https://github.com/microsoft/vscode/blob/2b9486161abaca59b5132ce3c59544f3cc7000f6/src/vs/code/electron-main/app.ts#L878 |
| Sean McCullough | f5e28f6 | 2025-04-25 10:48:00 -0700 | [diff] [blame] | 287 | fmt.Printf(`Connect to this container via any of these methods: |
| Sean McCullough | 4854c65 | 2025-04-24 18:37:02 -0700 | [diff] [blame] | 288 | 🖥️ ssh %s |
| 289 | 🖥️ code --remote ssh-remote+root@%s /app -n |
| Sean McCullough | ea3fc20 | 2025-04-28 12:53:37 -0700 | [diff] [blame] | 290 | 🔗 vscode://vscode-remote/ssh-remote+root@%s/app?windowId=_blank |
| Sean McCullough | 4854c65 | 2025-04-24 18:37:02 -0700 | [diff] [blame] | 291 | `, cntrName, cntrName, cntrName) |
| Sean McCullough | f5e28f6 | 2025-04-25 10:48:00 -0700 | [diff] [blame] | 292 | sshUserIdentity = cst.userIdentity |
| 293 | sshServerIdentity = cst.serverIdentity |
| 294 | defer func() { |
| 295 | if err := cst.Cleanup(); err != nil { |
| 296 | appendInternalErr(err) |
| 297 | } |
| 298 | }() |
| 299 | } |
| Sean McCullough | ae3480f | 2025-04-23 15:28:20 -0700 | [diff] [blame] | 300 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 301 | // Tell the sketch container which git server port and commit to initialize with. |
| 302 | go func() { |
| 303 | // TODO: Why is this called in a goroutine? I have found that when I pull this out |
| 304 | // of the goroutine and call it inline, then the terminal UI clears itself and all |
| 305 | // the scrollback (which is not good, but also not fatal). I can't see why it does this |
| 306 | // though, since none of the calls in postContainerInitConfig obviously write to stdout |
| 307 | // or stderr. |
| Sean McCullough | f5e28f6 | 2025-04-25 10:48:00 -0700 | [diff] [blame] | 308 | 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] | 309 | slog.ErrorContext(ctx, "LaunchContainer.postContainerInitConfig", slog.String("err", err.Error())) |
| 310 | errCh <- appendInternalErr(err) |
| 311 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 312 | |
| Philip Zeyliger | 6ed6adb | 2025-04-23 19:56:38 -0700 | [diff] [blame] | 313 | // We open the browser after the init config because the above waits for the web server to be serving. |
| 314 | if config.OpenBrowser { |
| 315 | if config.SkabandAddr != "" { |
| Josh Bleecher Snyder | e54b00a | 2025-04-30 16:48:02 -0700 | [diff] [blame] | 316 | browser.Open(fmt.Sprintf("%s/s/%s", config.SkabandAddr, config.SessionID)) |
| Philip Zeyliger | 6ed6adb | 2025-04-23 19:56:38 -0700 | [diff] [blame] | 317 | } else { |
| Josh Bleecher Snyder | e54b00a | 2025-04-30 16:48:02 -0700 | [diff] [blame] | 318 | browser.Open("http://" + localAddr) |
| Philip Zeyliger | 6ed6adb | 2025-04-23 19:56:38 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | }() |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 322 | |
| 323 | go func() { |
| 324 | cmd := exec.CommandContext(ctx, "docker", "attach", cntrName) |
| 325 | cmd.Stdin = os.Stdin |
| 326 | cmd.Stdout = os.Stdout |
| 327 | cmd.Stderr = os.Stderr |
| 328 | errCh <- run(ctx, "docker attach", cmd) |
| 329 | }() |
| 330 | |
| 331 | defer copyLogs() |
| 332 | |
| 333 | for { |
| 334 | select { |
| 335 | case <-ctx.Done(): |
| 336 | return ctx.Err() |
| 337 | case err := <-errCh: |
| 338 | if err != nil { |
| 339 | return appendInternalErr(fmt.Errorf("container process: %w", err)) |
| 340 | } |
| 341 | return nil |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | func combinedOutput(ctx context.Context, cmdName string, args ...string) ([]byte, error) { |
| 347 | cmd := exec.CommandContext(ctx, cmdName, args...) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 348 | start := time.Now() |
| 349 | |
| 350 | out, err := cmd.CombinedOutput() |
| 351 | if err != nil { |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 352 | 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] | 353 | } else { |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 354 | 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] | 355 | } |
| 356 | return out, err |
| 357 | } |
| 358 | |
| 359 | func run(ctx context.Context, cmdName string, cmd *exec.Cmd) error { |
| 360 | start := time.Now() |
| 361 | err := cmd.Run() |
| 362 | if err != nil { |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 363 | 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] | 364 | } else { |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 365 | 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] | 366 | } |
| 367 | return err |
| 368 | } |
| 369 | |
| 370 | type gitServer struct { |
| 371 | gitLn net.Listener |
| 372 | gitPort string |
| 373 | srv *http.Server |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 374 | pass string |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | func (gs *gitServer) shutdown(ctx context.Context) { |
| 378 | gs.srv.Shutdown(ctx) |
| 379 | gs.gitLn.Close() |
| 380 | } |
| 381 | |
| 382 | // Serve a git remote from the host for the container to fetch from and push to. |
| 383 | func (gs *gitServer) serve(ctx context.Context) error { |
| 384 | slog.DebugContext(ctx, "starting git server", slog.String("git_remote_addr", "http://host.docker.internal:"+gs.gitPort+"/.git")) |
| 385 | return gs.srv.Serve(gs.gitLn) |
| 386 | } |
| 387 | |
| 388 | func newGitServer(gitRoot string) (*gitServer, error) { |
| Josh Bleecher Snyder | 9f6a998 | 2025-04-22 17:34:15 -0700 | [diff] [blame] | 389 | ret := &gitServer{ |
| 390 | pass: rand.Text(), |
| 391 | } |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 392 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 393 | gitLn, err := net.Listen("tcp4", ":0") |
| 394 | if err != nil { |
| 395 | return nil, fmt.Errorf("git listen: %w", err) |
| 396 | } |
| 397 | ret.gitLn = gitLn |
| 398 | |
| Josh Bleecher Snyder | 3e2111b | 2025-04-30 17:53:28 +0000 | [diff] [blame] | 399 | browserC := make(chan string, 1) // channel of URLs to open in browser |
| 400 | go func() { |
| 401 | for url := range browserC { |
| 402 | browser.Open(url) |
| 403 | } |
| 404 | }() |
| 405 | |
| 406 | srv := http.Server{Handler: &gitHTTP{gitRepoRoot: gitRoot, pass: []byte(ret.pass), browserC: browserC}} |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 407 | ret.srv = &srv |
| 408 | |
| 409 | _, gitPort, err := net.SplitHostPort(gitLn.Addr().String()) |
| 410 | if err != nil { |
| 411 | return nil, fmt.Errorf("git port: %w", err) |
| 412 | } |
| 413 | ret.gitPort = gitPort |
| 414 | return ret, nil |
| 415 | } |
| 416 | |
| 417 | 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] | 418 | cmdArgs := []string{ |
| 419 | "create", |
| David Crawshaw | 66cf74e | 2025-05-05 08:48:39 -0700 | [diff] [blame] | 420 | "-i", |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 421 | "--name", cntrName, |
| 422 | "-p", hostPort + ":80", // forward container port 80 to a host port |
| Philip Zeyliger | 6234a8d | 2025-05-02 14:31:20 -0700 | [diff] [blame] | 423 | "-e", "SKETCH_ANTHROPIC_API_KEY=" + config.AntAPIKey, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 424 | } |
| David Crawshaw | 66cf74e | 2025-05-05 08:48:39 -0700 | [diff] [blame] | 425 | if !config.OneShot { |
| 426 | cmdArgs = append(cmdArgs, "-t") |
| 427 | } |
| Josh Bleecher Snyder | 2772f63 | 2025-05-01 21:42:35 +0000 | [diff] [blame] | 428 | |
| 429 | for _, envVar := range getEnvForwardingFromGitConfig(ctx) { |
| 430 | cmdArgs = append(cmdArgs, "-e", envVar) |
| 431 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 432 | if config.AntURL != "" { |
| Philip Zeyliger | 6234a8d | 2025-05-02 14:31:20 -0700 | [diff] [blame] | 433 | cmdArgs = append(cmdArgs, "-e", "SKETCH_ANT_URL="+config.AntURL) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 434 | } |
| 435 | if config.SketchPubKey != "" { |
| 436 | cmdArgs = append(cmdArgs, "-e", "SKETCH_PUB_KEY="+config.SketchPubKey) |
| 437 | } |
| Sean McCullough | ae3480f | 2025-04-23 15:28:20 -0700 | [diff] [blame] | 438 | if config.SSHPort > 0 { |
| 439 | cmdArgs = append(cmdArgs, "-p", fmt.Sprintf("%d:22", config.SSHPort)) // forward container ssh port to host ssh port |
| 440 | } else { |
| 441 | cmdArgs = append(cmdArgs, "-p", "22") // use an ephemeral host port for ssh. |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 442 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 443 | if relPath != "." { |
| 444 | cmdArgs = append(cmdArgs, "-w", "/app/"+relPath) |
| 445 | } |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 446 | // colima does this by default, but Linux docker seems to need this set explicitly |
| 447 | cmdArgs = append(cmdArgs, "--add-host", "host.docker.internal:host-gateway") |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 448 | cmdArgs = append( |
| 449 | cmdArgs, |
| 450 | imgName, |
| 451 | "/bin/sketch", |
| 452 | "-unsafe", |
| 453 | "-addr=:80", |
| 454 | "-session-id="+config.SessionID, |
| Philip Zeyliger | d140295 | 2025-04-23 03:54:37 +0000 | [diff] [blame] | 455 | "-git-username="+config.GitUsername, |
| 456 | "-git-email="+config.GitEmail, |
| Philip Zeyliger | 18532b2 | 2025-04-23 21:11:46 +0000 | [diff] [blame] | 457 | "-outside-hostname="+config.OutsideHostname, |
| 458 | "-outside-os="+config.OutsideOS, |
| 459 | "-outside-working-dir="+config.OutsideWorkingDir, |
| Josh Bleecher Snyder | 3cae7d9 | 2025-04-30 09:54:29 -0700 | [diff] [blame] | 460 | "-open=false", |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 461 | ) |
| 462 | if config.SkabandAddr != "" { |
| 463 | cmdArgs = append(cmdArgs, "-skaband-addr="+config.SkabandAddr) |
| 464 | } |
| Pokey Rule | 0dcebe1 | 2025-04-28 14:51:04 +0100 | [diff] [blame] | 465 | if config.Prompt != "" { |
| 466 | cmdArgs = append(cmdArgs, "-prompt", config.Prompt) |
| 467 | } |
| 468 | if config.OneShot { |
| 469 | cmdArgs = append(cmdArgs, "-one-shot") |
| Philip Zeyliger | b74c4f6 | 2025-04-25 19:18:49 -0700 | [diff] [blame] | 470 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 471 | if out, err := combinedOutput(ctx, "docker", cmdArgs...); err != nil { |
| 472 | return fmt.Errorf("docker create: %s, %w", out, err) |
| 473 | } |
| 474 | return nil |
| 475 | } |
| 476 | |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 477 | func buildLinuxSketchBin(ctx context.Context) (string, error) { |
| David Crawshaw | 8a617cb | 2025-04-18 01:28:43 -0700 | [diff] [blame] | 478 | homeDir, err := os.UserHomeDir() |
| David Crawshaw | 69c6731 | 2025-04-17 13:42:00 -0700 | [diff] [blame] | 479 | if err != nil { |
| 480 | return "", err |
| 481 | } |
| David Crawshaw | 8a617cb | 2025-04-18 01:28:43 -0700 | [diff] [blame] | 482 | linuxGopath := filepath.Join(homeDir, ".cache", "sketch", "linuxgo") |
| 483 | if err := os.MkdirAll(linuxGopath, 0o777); err != nil { |
| 484 | return "", err |
| 485 | } |
| 486 | |
| 487 | verToInstall := "@latest" |
| 488 | if out, err := exec.Command("go", "list", "-m").CombinedOutput(); err != nil { |
| 489 | return "", fmt.Errorf("failed to run go list -m: %s: %v", out, err) |
| 490 | } else { |
| 491 | if strings.TrimSpace(string(out)) == "sketch.dev" { |
| David Crawshaw | 094e4d2 | 2025-04-24 11:35:14 -0700 | [diff] [blame] | 492 | slog.DebugContext(ctx, "built linux agent from currently checked out module") |
| David Crawshaw | 8a617cb | 2025-04-18 01:28:43 -0700 | [diff] [blame] | 493 | verToInstall = "" |
| 494 | } |
| 495 | } |
| David Crawshaw | 69c6731 | 2025-04-17 13:42:00 -0700 | [diff] [blame] | 496 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 497 | start := time.Now() |
| David Crawshaw | 8a617cb | 2025-04-18 01:28:43 -0700 | [diff] [blame] | 498 | cmd := exec.CommandContext(ctx, "go", "install", "sketch.dev/cmd/sketch"+verToInstall) |
| David Crawshaw | b9eaef5 | 2025-04-17 15:23:18 -0700 | [diff] [blame] | 499 | cmd.Env = append( |
| 500 | os.Environ(), |
| 501 | "GOOS=linux", |
| 502 | "CGO_ENABLED=0", |
| 503 | "GOTOOLCHAIN=auto", |
| David Crawshaw | 8a617cb | 2025-04-18 01:28:43 -0700 | [diff] [blame] | 504 | "GOPATH="+linuxGopath, |
| Josh Bleecher Snyder | fae1757 | 2025-04-21 11:48:05 -0700 | [diff] [blame] | 505 | "GOBIN=", |
| David Crawshaw | b9eaef5 | 2025-04-17 15:23:18 -0700 | [diff] [blame] | 506 | ) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 507 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 508 | out, err := cmd.CombinedOutput() |
| 509 | if err != nil { |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 510 | 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] | 511 | return "", fmt.Errorf("failed to build linux sketch binary: %s: %w", out, err) |
| 512 | } else { |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 513 | 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] | 514 | } |
| 515 | |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 516 | if runtime.GOOS != "linux" { |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 517 | return filepath.Join(linuxGopath, "bin", "linux_"+runtime.GOARCH, "sketch"), nil |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 518 | } |
| David Crawshaw | c7e7796 | 2025-05-03 13:20:18 -0700 | [diff] [blame] | 519 | // If we are already on Linux, there's no extra platform name in the path |
| 520 | return filepath.Join(linuxGopath, "bin", "sketch"), nil |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| Sean McCullough | ae3480f | 2025-04-23 15:28:20 -0700 | [diff] [blame] | 523 | func getContainerPort(ctx context.Context, cntrName, cntrPort string) (string, error) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 524 | localAddr := "" |
| Sean McCullough | ae3480f | 2025-04-23 15:28:20 -0700 | [diff] [blame] | 525 | if out, err := combinedOutput(ctx, "docker", "port", cntrName, cntrPort); err != nil { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 526 | return "", fmt.Errorf("failed to find container port: %s: %v", out, err) |
| 527 | } else { |
| 528 | v4, _, found := strings.Cut(string(out), "\n") |
| 529 | if !found { |
| 530 | return "", fmt.Errorf("failed to find container port: %s: %v", out, err) |
| 531 | } |
| 532 | localAddr = v4 |
| 533 | if strings.HasPrefix(localAddr, "0.0.0.0") { |
| 534 | localAddr = "127.0.0.1" + strings.TrimPrefix(localAddr, "0.0.0.0") |
| 535 | } |
| 536 | } |
| 537 | return localAddr, nil |
| 538 | } |
| 539 | |
| 540 | // Contact the container and configure it. |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 541 | 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] | 542 | localURL := "http://" + localAddr |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 543 | |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 544 | // Check if SSH is available by checking for the Include directive in ~/.ssh/config |
| 545 | sshAvailable := true |
| 546 | sshError := "" |
| 547 | if err := CheckForInclude(); err != nil { |
| 548 | sshAvailable = false |
| 549 | sshError = err.Error() |
| 550 | } |
| 551 | |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 552 | initMsg, err := json.Marshal( |
| 553 | server.InitRequest{ |
| 554 | Commit: commit, |
| Josh Bleecher Snyder | 3e2111b | 2025-04-30 17:53:28 +0000 | [diff] [blame] | 555 | OutsideHTTP: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s", gitPass, gitPort), |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 556 | GitRemoteAddr: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s/.git", gitPass, gitPort), |
| 557 | HostAddr: localAddr, |
| 558 | SSHAuthorizedKeys: sshAuthorizedKeys, |
| 559 | SSHServerIdentity: sshServerIdentity, |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 560 | SSHAvailable: sshAvailable, |
| 561 | SSHError: sshError, |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 562 | }) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 563 | if err != nil { |
| 564 | return fmt.Errorf("init msg: %w", err) |
| 565 | } |
| 566 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 567 | // Note: this /init POST is handled in loop/server/loophttp.go: |
| 568 | initMsgByteReader := bytes.NewReader(initMsg) |
| 569 | req, err := http.NewRequest("POST", localURL+"/init", initMsgByteReader) |
| 570 | if err != nil { |
| 571 | return err |
| 572 | } |
| 573 | |
| 574 | var res *http.Response |
| 575 | for i := 0; ; i++ { |
| 576 | time.Sleep(100 * time.Millisecond) |
| 577 | // If you DON'T reset this byteReader, then subsequent retries may end up sending 0 bytes. |
| 578 | initMsgByteReader.Reset(initMsg) |
| 579 | res, err = http.DefaultClient.Do(req) |
| 580 | if err != nil { |
| David Crawshaw | 99231ba | 2025-05-03 10:48:26 -0700 | [diff] [blame] | 581 | if i < 100 { |
| 582 | if i%10 == 0 { |
| 583 | slog.DebugContext(ctx, "postContainerInitConfig retrying", slog.Int("retry", i), slog.String("err", err.Error())) |
| 584 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 585 | continue |
| 586 | } |
| 587 | return fmt.Errorf("failed to %s/init sketch in container, NOT retrying: err: %v", localURL, err) |
| 588 | } |
| 589 | break |
| 590 | } |
| 591 | resBytes, _ := io.ReadAll(res.Body) |
| 592 | if res.StatusCode != http.StatusOK { |
| 593 | return fmt.Errorf("failed to initialize sketch in container, response status code %d: %s", res.StatusCode, resBytes) |
| 594 | } |
| 595 | return nil |
| 596 | } |
| 597 | |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 598 | 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] | 599 | h := sha256.Sum256([]byte(gitRoot)) |
| 600 | imgName = "sketch-" + hex.EncodeToString(h[:6]) |
| 601 | |
| 602 | var curImgInitFilesHash string |
| 603 | if out, err := combinedOutput(ctx, "docker", "inspect", "--format", "{{json .Config.Labels}}", imgName); err != nil { |
| 604 | if strings.Contains(string(out), "No such object") { |
| 605 | // Image does not exist, continue and build it. |
| 606 | curImgInitFilesHash = "" |
| 607 | } else { |
| 608 | return "", fmt.Errorf("docker inspect failed: %s, %v", out, err) |
| 609 | } |
| 610 | } else { |
| 611 | m := map[string]string{} |
| 612 | if err := json.Unmarshal(bytes.TrimSpace(out), &m); err != nil { |
| 613 | return "", fmt.Errorf("docker inspect output unparsable: %s, %v", out, err) |
| 614 | } |
| 615 | curImgInitFilesHash = m["sketch_context"] |
| 616 | } |
| 617 | |
| 618 | candidates, err := findRepoDockerfiles(cwd, gitRoot) |
| 619 | if err != nil { |
| 620 | return "", fmt.Errorf("find dockerfile: %w", err) |
| 621 | } |
| 622 | |
| 623 | var initFiles map[string]string |
| 624 | var dockerfilePath string |
| 625 | |
| 626 | // TODO: prefer a "Dockerfile.sketch" so users can tailor any env to this tool. |
| 627 | if len(candidates) == 1 && strings.ToLower(filepath.Base(candidates[0])) == "dockerfile" { |
| 628 | dockerfilePath = candidates[0] |
| 629 | contents, err := os.ReadFile(dockerfilePath) |
| 630 | if err != nil { |
| 631 | return "", err |
| 632 | } |
| 633 | fmt.Printf("using %s as dev env\n", candidates[0]) |
| 634 | if hashInitFiles(map[string]string{dockerfilePath: string(contents)}) == curImgInitFilesHash && !forceRebuild { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 635 | return imgName, nil |
| 636 | } |
| 637 | } else { |
| 638 | initFiles, err = readInitFiles(os.DirFS(gitRoot)) |
| 639 | if err != nil { |
| 640 | return "", err |
| 641 | } |
| 642 | subPathWorkingDir, err := filepath.Rel(gitRoot, cwd) |
| 643 | if err != nil { |
| 644 | return "", err |
| 645 | } |
| 646 | initFileHash := hashInitFiles(initFiles) |
| 647 | if curImgInitFilesHash == initFileHash && !forceRebuild { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 648 | return imgName, nil |
| 649 | } |
| 650 | |
| 651 | start := time.Now() |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 652 | srv := &ant.Service{ |
| 653 | URL: antURL, |
| 654 | APIKey: antAPIKey, |
| 655 | HTTPC: http.DefaultClient, |
| 656 | } |
| 657 | dockerfile, err := createDockerfile(ctx, srv, initFiles, subPathWorkingDir) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 658 | if err != nil { |
| 659 | return "", fmt.Errorf("create dockerfile: %w", err) |
| 660 | } |
| 661 | dockerfilePath = filepath.Join(cwd, "tmp-sketch-dockerfile") |
| 662 | if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0o666); err != nil { |
| 663 | return "", err |
| 664 | } |
| 665 | defer os.Remove(dockerfilePath) |
| 666 | |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 667 | if verbose { |
| 668 | fmt.Fprintf(os.Stderr, "generated Dockerfile in %s:\n\t%s\n\n", time.Since(start).Round(time.Millisecond), strings.Replace(dockerfile, "\n", "\n\t", -1)) |
| 669 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | var gitUserEmail, gitUserName string |
| 673 | if out, err := combinedOutput(ctx, "git", "config", "--get", "user.email"); err != nil { |
| 674 | return "", fmt.Errorf("git config: %s: %v", out, err) |
| 675 | } else { |
| 676 | gitUserEmail = strings.TrimSpace(string(out)) |
| 677 | } |
| 678 | if out, err := combinedOutput(ctx, "git", "config", "--get", "user.name"); err != nil { |
| 679 | return "", fmt.Errorf("git config: %s: %v", out, err) |
| 680 | } else { |
| 681 | gitUserName = strings.TrimSpace(string(out)) |
| 682 | } |
| 683 | |
| 684 | start := time.Now() |
| 685 | cmd := exec.CommandContext(ctx, |
| 686 | "docker", "build", |
| 687 | "-t", imgName, |
| 688 | "-f", dockerfilePath, |
| 689 | "--build-arg", "GIT_USER_EMAIL="+gitUserEmail, |
| 690 | "--build-arg", "GIT_USER_NAME="+gitUserName, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 691 | ) |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 692 | if !verbose { |
| 693 | cmd.Args = append(cmd.Args, "--progress=quiet") |
| Philip Zeyliger | e4fa0e3 | 2025-04-23 14:15:55 -0700 | [diff] [blame] | 694 | } |
| David Crawshaw | b5f6a00 | 2025-05-05 08:27:16 -0700 | [diff] [blame] | 695 | cmd.Args = append(cmd.Args, ".") |
| 696 | cmd.Dir = gitRoot |
| 697 | cmd.Stdout = os.Stdout |
| 698 | cmd.Stderr = os.Stderr |
| 699 | fmt.Printf("🏗️ building docker image %s... (use -verbose to see build output)\n", imgName) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 700 | |
| 701 | err = run(ctx, "docker build", cmd) |
| 702 | if err != nil { |
| 703 | return "", fmt.Errorf("docker build failed: %v", err) |
| 704 | } |
| 705 | fmt.Printf("built docker image %s in %s\n", imgName, time.Since(start).Round(time.Millisecond)) |
| 706 | return imgName, nil |
| 707 | } |
| 708 | |
| 709 | func findRepoDockerfiles(cwd, gitRoot string) ([]string, error) { |
| 710 | files, err := findDirDockerfiles(cwd) |
| 711 | if err != nil { |
| 712 | return nil, err |
| 713 | } |
| 714 | if len(files) > 0 { |
| 715 | return files, nil |
| 716 | } |
| 717 | |
| 718 | path := cwd |
| 719 | for path != gitRoot { |
| 720 | path = filepath.Dir(path) |
| 721 | files, err := findDirDockerfiles(path) |
| 722 | if err != nil { |
| 723 | return nil, err |
| 724 | } |
| 725 | if len(files) > 0 { |
| 726 | return files, nil |
| 727 | } |
| 728 | } |
| 729 | return files, nil |
| 730 | } |
| 731 | |
| 732 | // findDirDockerfiles finds all "Dockerfile*" files in a directory. |
| 733 | func findDirDockerfiles(root string) (res []string, err error) { |
| 734 | err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { |
| 735 | if err != nil { |
| 736 | return err |
| 737 | } |
| 738 | if info.IsDir() && root != path { |
| 739 | return filepath.SkipDir |
| 740 | } |
| 741 | name := strings.ToLower(info.Name()) |
| 742 | if name == "dockerfile" || strings.HasPrefix(name, "dockerfile.") { |
| 743 | res = append(res, path) |
| 744 | } |
| 745 | return nil |
| 746 | }) |
| 747 | if err != nil { |
| 748 | return nil, err |
| 749 | } |
| 750 | return res, nil |
| 751 | } |
| 752 | |
| 753 | func findGitRoot(ctx context.Context, path string) (string, error) { |
| 754 | cmd := exec.CommandContext(ctx, "git", "rev-parse", "--git-common-dir") |
| 755 | cmd.Dir = path |
| 756 | out, err := cmd.CombinedOutput() |
| 757 | if err != nil { |
| 758 | if strings.Contains(string(out), "not a git repository") { |
| 759 | return "", fmt.Errorf(`sketch needs to run from within a git repo, but %s is not part of a git repo. |
| 760 | Consider one of the following options: |
| 761 | - cd to a different dir that is already part of a git repo first, or |
| 762 | - to create a new git repo from this directory (%s), run this command: |
| 763 | |
| 764 | git init . && git commit --allow-empty -m "initial commit" |
| 765 | |
| 766 | and try running sketch again. |
| 767 | `, path, path) |
| 768 | } |
| 769 | return "", fmt.Errorf("git rev-parse --git-common-dir: %s: %w", out, err) |
| 770 | } |
| 771 | gitDir := strings.TrimSpace(string(out)) // location of .git dir, often as a relative path |
| 772 | absGitDir := filepath.Join(path, gitDir) |
| 773 | return filepath.Dir(absGitDir), err |
| 774 | } |
| 775 | |
| Josh Bleecher Snyder | 2772f63 | 2025-05-01 21:42:35 +0000 | [diff] [blame] | 776 | // getEnvForwardingFromGitConfig retrieves environment variables to pass through to Docker |
| 777 | // from git config using the sketch.envfwd multi-valued key. |
| 778 | func getEnvForwardingFromGitConfig(ctx context.Context) []string { |
| 779 | outb, err := exec.CommandContext(ctx, "git", "config", "--get-all", "sketch.envfwd").CombinedOutput() |
| 780 | out := string(outb) |
| 781 | if err != nil { |
| 782 | if strings.Contains(out, "key does not exist") { |
| 783 | return nil |
| 784 | } |
| 785 | slog.ErrorContext(ctx, "failed to get sketch.envfwd from git config", "err", err, "output", out) |
| 786 | return nil |
| 787 | } |
| 788 | |
| 789 | var envVars []string |
| 790 | for envVar := range strings.Lines(out) { |
| 791 | envVar = strings.TrimSpace(envVar) |
| 792 | if envVar == "" { |
| 793 | continue |
| 794 | } |
| 795 | envVars = append(envVars, envVar+"="+os.Getenv(envVar)) |
| 796 | } |
| 797 | return envVars |
| 798 | } |