| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 1 | package dockerimg |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto/sha256" |
| 7 | "encoding/hex" |
| 8 | "encoding/json" |
| 9 | "fmt" |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 10 | "io" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 11 | "io/fs" |
| 12 | "maps" |
| 13 | "net/http" |
| 14 | "slices" |
| 15 | "strings" |
| 16 | "text/template" |
| 17 | |
| 18 | "sketch.dev/ant" |
| 19 | ) |
| 20 | |
| 21 | func hashInitFiles(initFiles map[string]string) string { |
| 22 | h := sha256.New() |
| 23 | for _, path := range slices.Sorted(maps.Keys(initFiles)) { |
| 24 | fmt.Fprintf(h, "%s\n%s\n\n", path, initFiles[path]) |
| 25 | } |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 26 | fmt.Fprintf(h, "docker template\n%s\n", dockerfileDefaultTmpl) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 27 | return hex.EncodeToString(h.Sum(nil)) |
| 28 | } |
| 29 | |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 30 | // DefaultImage is intended to ONLY be used by the pushdockerimg.go script. |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 31 | func DefaultImage() (name, dockerfile, tag string) { |
| 32 | return dockerImgName, dockerfileBase, dockerfileBaseHash() |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 35 | const ( |
| 36 | dockerImgRepo = "boldsoftware/sketch" |
| 37 | dockerImgName = "ghcr.io/" + dockerImgRepo |
| 38 | ) |
| David Crawshaw | 5bff650 | 2025-04-26 09:11:40 -0700 | [diff] [blame] | 39 | |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 40 | func dockerfileBaseHash() string { |
| 41 | h := sha256.New() |
| 42 | io.WriteString(h, dockerfileBase) |
| 43 | return hex.EncodeToString(h.Sum(nil))[:32] |
| 44 | } |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 45 | |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 46 | const dockerfileBase = `FROM golang:1.24-bookworm |
| David Crawshaw | be10fa9 | 2025-04-18 01:16:00 -0700 | [diff] [blame] | 47 | |
| David Crawshaw | 5228b58 | 2025-05-01 11:18:12 -0700 | [diff] [blame] | 48 | # attempt to keep package installs lean |
| 49 | RUN printf '%s\n' \ |
| 50 | 'path-exclude=/usr/share/man/*' \ |
| 51 | 'path-exclude=/usr/share/doc/*' \ |
| 52 | 'path-exclude=/usr/share/doc-base/*' \ |
| 53 | 'path-exclude=/usr/share/info/*' \ |
| 54 | 'path-exclude=/usr/share/locale/*' \ |
| 55 | 'path-exclude=/usr/share/groff/*' \ |
| 56 | 'path-exclude=/usr/share/lintian/*' \ |
| 57 | 'path-exclude=/usr/share/zoneinfo/*' \ |
| 58 | > /etc/dpkg/dpkg.cfg.d/01_nodoc |
| 59 | |
| David Crawshaw | fa67fe5 | 2025-05-01 20:42:08 +0000 | [diff] [blame] | 60 | RUN set -eux; \ |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 61 | apt-get update; \ |
| 62 | apt-get install -y --no-install-recommends \ |
| David Crawshaw | fa67fe5 | 2025-05-01 20:42:08 +0000 | [diff] [blame] | 63 | git jq sqlite3 npm nodejs gh ripgrep fzf python3 curl vim && \ |
| 64 | apt-get clean && \ |
| 65 | rm -rf /var/lib/apt/lists/* |
| David Crawshaw | be10fa9 | 2025-04-18 01:16:00 -0700 | [diff] [blame] | 66 | |
| David Crawshaw | 5228b58 | 2025-05-01 11:18:12 -0700 | [diff] [blame] | 67 | # Strip out docs from debian. |
| 68 | RUN rm -rf /usr/share/{doc,doc-base,info,lintian,man,groff,locale,zoneinfo}/* |
| 69 | |
| David Crawshaw | be10fa9 | 2025-04-18 01:16:00 -0700 | [diff] [blame] | 70 | ENV PATH="$GOPATH/bin:$PATH" |
| 71 | |
| David Crawshaw | 5228b58 | 2025-05-01 11:18:12 -0700 | [diff] [blame] | 72 | # While these binaries install generally useful supporting packages, |
| 73 | # the specific versions are rarely what a user wants so there is no |
| 74 | # point polluting the base image module with them. |
| 75 | |
| David Crawshaw | fa67fe5 | 2025-05-01 20:42:08 +0000 | [diff] [blame] | 76 | RUN set -eux; \ |
| David Crawshaw | 5228b58 | 2025-05-01 11:18:12 -0700 | [diff] [blame] | 77 | go install golang.org/x/tools/cmd/goimports@latest; \ |
| 78 | go install golang.org/x/tools/gopls@latest; \ |
| 79 | go install mvdan.cc/gofumpt@latest; \ |
| David Crawshaw | fa67fe5 | 2025-05-01 20:42:08 +0000 | [diff] [blame] | 80 | go clean -cache -testcache -modcache |
| David Crawshaw | be10fa9 | 2025-04-18 01:16:00 -0700 | [diff] [blame] | 81 | |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 82 | ENV GOTOOLCHAIN=auto |
| David Crawshaw | 5228b58 | 2025-05-01 11:18:12 -0700 | [diff] [blame] | 83 | ENV SKETCH=1 |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 84 | |
| David Crawshaw | be10fa9 | 2025-04-18 01:16:00 -0700 | [diff] [blame] | 85 | RUN mkdir -p /root/.cache/sketch/webui |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 86 | ` |
| David Crawshaw | be10fa9 | 2025-04-18 01:16:00 -0700 | [diff] [blame] | 87 | |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 88 | const dockerfileFragment = ` |
| David Crawshaw | be10fa9 | 2025-04-18 01:16:00 -0700 | [diff] [blame] | 89 | ARG GIT_USER_EMAIL |
| 90 | ARG GIT_USER_NAME |
| 91 | |
| 92 | RUN git config --global user.email "$GIT_USER_EMAIL" && \ |
| 93 | git config --global user.name "$GIT_USER_NAME" |
| 94 | |
| Josh Bleecher Snyder | c76a392 | 2025-05-01 01:18:56 +0000 | [diff] [blame] | 95 | LABEL sketch_context="{{.InitFilesHash}}" |
| David Crawshaw | be10fa9 | 2025-04-18 01:16:00 -0700 | [diff] [blame] | 96 | COPY . /app |
| 97 | |
| 98 | WORKDIR /app{{.SubDir}} |
| 99 | RUN if [ -f go.mod ]; then go mod download; fi |
| 100 | |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 101 | {{.ExtraCmds}} |
| 102 | |
| 103 | CMD ["/bin/sketch"] |
| 104 | ` |
| 105 | |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 106 | var dockerfileDefaultTmpl = "FROM " + dockerImgName + ":" + dockerfileBaseHash() + "\n" + dockerfileFragment |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 107 | |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 108 | func readPublishedTags() ([]string, error) { |
| 109 | req, err := http.NewRequest("GET", "https://ghcr.io/token?service=ghcr.io&scope=repository:"+dockerImgRepo+":pull", nil) |
| 110 | if err != nil { |
| 111 | return nil, fmt.Errorf("token: %w", err) |
| 112 | } |
| 113 | res, err := http.DefaultClient.Do(req) |
| 114 | if err != nil { |
| 115 | return nil, fmt.Errorf("token: %w", err) |
| 116 | } |
| 117 | body, err := io.ReadAll(res.Body) |
| 118 | res.Body.Close() |
| 119 | if err != nil || res.StatusCode != 200 { |
| 120 | return nil, fmt.Errorf("token: %d: %s: %w", res.StatusCode, body, err) |
| 121 | } |
| 122 | var tokenBody struct { |
| 123 | Token string `json:"token"` |
| 124 | } |
| 125 | if err := json.Unmarshal(body, &tokenBody); err != nil { |
| 126 | return nil, fmt.Errorf("token: %w: %s", err, body) |
| 127 | } |
| 128 | |
| 129 | req, err = http.NewRequest("GET", "https://ghcr.io/v2/"+dockerImgRepo+"/tags/list", nil) |
| 130 | if err != nil { |
| 131 | return nil, fmt.Errorf("tags: %w", err) |
| 132 | } |
| 133 | req.Header.Set("Authorization", "Bearer "+tokenBody.Token) |
| 134 | res, err = http.DefaultClient.Do(req) |
| 135 | if err != nil { |
| 136 | return nil, fmt.Errorf("tags: %w", err) |
| 137 | } |
| 138 | body, err = io.ReadAll(res.Body) |
| 139 | res.Body.Close() |
| 140 | if err != nil || res.StatusCode != 200 { |
| 141 | return nil, fmt.Errorf("tags: %d: %s: %w", res.StatusCode, body, err) |
| 142 | } |
| 143 | var tags struct { |
| 144 | Tags []string `json:"tags"` |
| 145 | } |
| 146 | if err := json.Unmarshal(body, &tags); err != nil { |
| 147 | return nil, fmt.Errorf("tags: %w: %s", err, body) |
| 148 | } |
| 149 | return tags.Tags, nil |
| 150 | } |
| 151 | |
| 152 | func checkTagExists(tag string) error { |
| 153 | tags, err := readPublishedTags() |
| 154 | if err != nil { |
| 155 | return fmt.Errorf("check tag exists: %w", err) |
| 156 | } |
| 157 | for _, t := range tags { |
| 158 | if t == tag { |
| 159 | return nil // found it |
| 160 | } |
| 161 | } |
| 162 | return fmt.Errorf("check tag exists: %q not found in %v", tag, tags) |
| 163 | } |
| David Crawshaw | be10fa9 | 2025-04-18 01:16:00 -0700 | [diff] [blame] | 164 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 165 | // createDockerfile creates a Dockerfile for a git repo. |
| 166 | // It expects the relevant initFiles to have been provided. |
| 167 | // If the sketch binary is being executed in a sub-directory of the repository, |
| 168 | // the relative path is provided on subPathWorkingDir. |
| 169 | func createDockerfile(ctx context.Context, httpc *http.Client, antURL, antAPIKey string, initFiles map[string]string, subPathWorkingDir string) (string, error) { |
| 170 | if subPathWorkingDir == "." { |
| 171 | subPathWorkingDir = "" |
| 172 | } else if subPathWorkingDir != "" && subPathWorkingDir[0] != '/' { |
| 173 | subPathWorkingDir = "/" + subPathWorkingDir |
| 174 | } |
| 175 | toolCalled := false |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 176 | var dockerfileExtraCmds string |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 177 | runDockerfile := func(ctx context.Context, input json.RawMessage) (string, error) { |
| 178 | // TODO: unmarshal straight into a struct |
| 179 | var m map[string]any |
| 180 | if err := json.Unmarshal(input, &m); err != nil { |
| 181 | return "", fmt.Errorf(`input=%[1]v (%[1]T), wanted a map[string]any, got: %w`, input, err) |
| 182 | } |
| 183 | var ok bool |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 184 | dockerfileExtraCmds, ok = m["extra_cmds"].(string) |
| 185 | if !ok { |
| 186 | return "", fmt.Errorf(`input["extra_cmds"]=%[1]v (%[1]T), wanted a string`, m["path"]) |
| 187 | } |
| 188 | toolCalled = true |
| 189 | return "OK", nil |
| 190 | } |
| 191 | convo := ant.NewConvo(ctx, antAPIKey) |
| 192 | if httpc != nil { |
| 193 | convo.HTTPC = httpc |
| 194 | } |
| 195 | if antURL != "" { |
| 196 | convo.URL = antURL |
| 197 | } |
| 198 | convo.Tools = []*ant.Tool{{ |
| 199 | Name: "dockerfile", |
| 200 | Description: "Helps define a Dockerfile that sets up a dev environment for this project.", |
| 201 | Run: runDockerfile, |
| 202 | InputSchema: ant.MustSchema(`{ |
| 203 | "type": "object", |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 204 | "required": ["extra_cmds"], |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 205 | "properties": { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 206 | "extra_cmds": { |
| 207 | "type": "string", |
| 208 | "description": "Extra commands to add to the dockerfile." |
| 209 | } |
| 210 | } |
| 211 | }`), |
| 212 | }} |
| 213 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 214 | // TODO: it's basically impossible to one-shot a python env. We need an agent loop for that. |
| 215 | // Right now the prompt contains a set of half-baked workarounds. |
| 216 | |
| 217 | // If you want to edit the model prompt, run: |
| 218 | // |
| Philip Zeyliger | cc3ba22 | 2025-04-23 14:52:21 -0700 | [diff] [blame] | 219 | // go test ./dockerimg -httprecord ".*" -rewritewant |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 220 | // |
| 221 | // Then look at the changes with: |
| 222 | // |
| Philip Zeyliger | cc3ba22 | 2025-04-23 14:52:21 -0700 | [diff] [blame] | 223 | // git diff dockerimg/testdata/*.dockerfile |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 224 | // |
| 225 | // If the dockerfile changes are a strict improvement, commit all the changes. |
| 226 | msg := ant.Message{ |
| 227 | Role: ant.MessageRoleUser, |
| 228 | Content: []ant.Content{{ |
| 229 | Type: ant.ContentTypeText, |
| 230 | Text: ` |
| 231 | Call the dockerfile tool to create a Dockerfile. |
| 232 | The parameters to dockerfile fill out the From and ExtraCmds |
| 233 | template variables in the following Go template: |
| 234 | |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 235 | ` + "```\n" + dockerfileBase + dockerfileFragment + "\n```" + ` |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 236 | |
| 237 | In particular: |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 238 | - Assume it is primarily a Go project. |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 239 | - Python env setup is challenging and often no required, so any RUN commands involving python tooling should be written to let docker build continue if there is a failure. |
| 240 | - Include any tools particular to this repository that can be inferred from the given context. |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 241 | - Append || true to any apt-get install commands in case the package does not exist. |
| 242 | - MINIMIZE the number of extra_cmds generated. Straightforward environments do not need any. |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 243 | - Do NOT expose any ports. |
| 244 | - Do NOT generate any CMD or ENTRYPOINT extra commands. |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 245 | `, |
| 246 | }}, |
| 247 | } |
| 248 | if len(initFiles) > 0 { |
| 249 | msg.Content[0].Text += "Here is the content of several files from the repository that may be relevant:\n\n" |
| 250 | } |
| 251 | |
| 252 | for _, name := range slices.Sorted(maps.Keys(initFiles)) { |
| Josh Bleecher Snyder | a3dcd86 | 2025-04-30 19:47:16 +0000 | [diff] [blame] | 253 | msg.Content = append(msg.Content, ant.StringContent(fmt.Sprintf("Here is the contents %s:\n<file>\n%s\n</file>\n\n", name, initFiles[name]))) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 254 | } |
| Josh Bleecher Snyder | a3dcd86 | 2025-04-30 19:47:16 +0000 | [diff] [blame] | 255 | msg.Content = append(msg.Content, ant.StringContent("Now call the dockerfile tool.")) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 256 | res, err := convo.SendMessage(msg) |
| 257 | if err != nil { |
| 258 | return "", err |
| 259 | } |
| 260 | if res.StopReason != ant.StopReasonToolUse { |
| 261 | return "", fmt.Errorf("expected stop reason %q, got %q", ant.StopReasonToolUse, res.StopReason) |
| 262 | } |
| 263 | if _, err := convo.ToolResultContents(context.TODO(), res); err != nil { |
| 264 | return "", err |
| 265 | } |
| 266 | if !toolCalled { |
| 267 | return "", fmt.Errorf("no dockerfile returned") |
| 268 | } |
| 269 | |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 270 | tmpl := dockerfileDefaultTmpl |
| 271 | if tag := dockerfileBaseHash(); checkTagExists(tag) != nil { |
| 272 | // In development, if you edit dockerfileBase but don't release |
| 273 | // (as is reasonable for testing things!) the hash won't exist |
| 274 | // yet. In that case, we skip the sketch image and build it ourselves. |
| 275 | fmt.Printf("published container tag %s:%s missing; building locally\n", dockerImgName, tag) |
| 276 | tmpl = dockerfileBase + dockerfileFragment |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 277 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 278 | buf := new(bytes.Buffer) |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 279 | err = template.Must(template.New("dockerfile").Parse(tmpl)).Execute(buf, map[string]string{ |
| Josh Bleecher Snyder | c76a392 | 2025-05-01 01:18:56 +0000 | [diff] [blame] | 280 | "ExtraCmds": dockerfileExtraCmds, |
| 281 | "SubDir": subPathWorkingDir, |
| 282 | "InitFilesHash": hashInitFiles(initFiles), |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 283 | }) |
| 284 | if err != nil { |
| 285 | return "", fmt.Errorf("dockerfile template failed: %w", err) |
| 286 | } |
| 287 | |
| 288 | return buf.String(), nil |
| 289 | } |
| 290 | |
| 291 | // For future reference: we can find the current git branch/checkout with: git symbolic-ref -q --short HEAD || git describe --tags --exact-match 2>/dev/null || git rev-parse HEAD |
| 292 | |
| 293 | func readInitFiles(fsys fs.FS) (map[string]string, error) { |
| 294 | result := make(map[string]string) |
| 295 | |
| 296 | err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error { |
| 297 | if err != nil { |
| 298 | return err |
| 299 | } |
| 300 | if d.IsDir() && (d.Name() == ".git" || d.Name() == "node_modules") { |
| 301 | return fs.SkipDir |
| 302 | } |
| 303 | if !d.Type().IsRegular() { |
| 304 | return nil |
| 305 | } |
| 306 | |
| 307 | // Case 1: Check for README files |
| 308 | // TODO: find README files between the .git root (where we start) |
| 309 | // and the dir that sketch was initialized. This needs more info |
| 310 | // plumbed to this function. |
| 311 | if strings.HasPrefix(strings.ToLower(path), "readme") { |
| 312 | content, err := fs.ReadFile(fsys, path) |
| 313 | if err != nil { |
| 314 | return err |
| 315 | } |
| 316 | result[path] = string(content) |
| 317 | return nil |
| 318 | } |
| 319 | |
| 320 | // Case 2: Check for GitHub workflow files |
| 321 | if strings.HasPrefix(path, ".github/workflows/") { |
| 322 | content, err := fs.ReadFile(fsys, path) |
| 323 | if err != nil { |
| 324 | return err |
| 325 | } |
| 326 | result[path] = string(content) |
| 327 | return nil |
| 328 | } |
| 329 | |
| 330 | return nil |
| 331 | }) |
| 332 | if err != nil { |
| 333 | return nil, err |
| 334 | } |
| 335 | return result, nil |
| 336 | } |