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