diff --git a/tools/bump/android.go b/tools/bump/android.go new file mode 100644 index 000000000..fd1849398 --- /dev/null +++ b/tools/bump/android.go @@ -0,0 +1,190 @@ +package main + +import ( + "context" + "encoding/xml" + "fmt" + "net/http" + "regexp" + + "golang.org/x/mod/semver" +) + +const ( + // androidRepoURL is the index sdkmanager itself reads. + androidRepoURL = "https://dl.google.com/android/repository/repository2-3.xml" + androidArchiveURL = "https://dl.google.com/android/repository/" + androidStable = "channel-0" + androidDockerfile = "Dockerfile.android-integration" +) + +var ( + cmdlineToolsRef = regexp.MustCompile(`(?m)^(ARG ANDROID_CMDLINE_TOOLS_VERSION=)(\d+)$`) + buildToolsRef = regexp.MustCompile(`(?m)^(ARG ANDROID_BUILD_TOOLS=)(\S+)$`) + + cmdlineToolsArchive = regexp.MustCompile(`^commandlinetools-linux-(\d+)_latest\.zip$`) + stableBuildTools = regexp.MustCompile(`^build-tools;(\d+\.\d+\.\d+)$`) +) + +type androidRepo struct { + Packages []struct { + Path string `xml:"path,attr"` + Channel struct { + Ref string `xml:"ref,attr"` + } `xml:"channelRef"` + Archives []struct { + URL string `xml:"complete>url"` + HostOS string `xml:"host-os"` + } `xml:"archives>archive"` + } `xml:"remotePackage"` +} + +// androidSDK is what the stable channel of the SDK index currently offers. +type androidSDK struct { + // CmdlineTools is the build number in the Linux command-line tools + // archive name, which is how the Dockerfile downloads them. + CmdlineTools string + BuildTools string + // Listed holds every stable build-tools release in the index. + Listed map[string]bool +} + +func parseAndroidRepo(body []byte) (androidSDK, error) { + var repo androidRepo + + err := xml.Unmarshal(body, &repo) + if err != nil { + return androidSDK{}, fmt.Errorf("decoding Android SDK index: %w", err) + } + + sdk := androidSDK{Listed: map[string]bool{}} + + for _, p := range repo.Packages { + if p.Channel.Ref != androidStable { + continue + } + + if p.Path == "cmdline-tools;latest" { + for _, a := range p.Archives { + m := cmdlineToolsArchive.FindStringSubmatch(a.URL) + if a.HostOS == "linux" && m != nil { + sdk.CmdlineTools = m[1] + } + } + } + + // Release candidates share the stable channel; only X.Y.Z is a release. + m := stableBuildTools.FindStringSubmatch(p.Path) + if m == nil { + continue + } + + sdk.Listed[m[1]] = true + + if sdk.BuildTools == "" || semver.Compare("v"+m[1], "v"+sdk.BuildTools) > 0 { + sdk.BuildTools = m[1] + } + } + + if sdk.CmdlineTools == "" || sdk.BuildTools == "" { + return androidSDK{}, fmt.Errorf("%w: Android SDK index lacks stable cmdline-tools or build-tools", errNoMatchingTag) + } + + return sdk, nil +} + +func latestAndroidSDK(ctx context.Context) (androidSDK, error) { + body, err := fetch(ctx, androidRepoURL) + if err != nil { + return androidSDK{}, err + } + + return parseAndroidRepo(body) +} + +// archivePublished asks Google's download server for the archive the +// Dockerfile would fetch, so a bump cannot point at a missing file. +func archivePublished(ctx context.Context, name string) error { + req, err := http.NewRequestWithContext(ctx, http.MethodHead, androidArchiveURL+name, nil) + if err != nil { + return fmt.Errorf("building request for %s: %w", name, err) + } + + client := &http.Client{Timeout: proxyTimeout} + + resp, err := client.Do(req) + if err != nil { + return fmt.Errorf("HEAD %s: %w", name, err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("%w: %s (%s)", errTagMissing, name, resp.Status) + } + + return nil +} + +// androidBumps are the Android SDK pins of the emulator image. They reuse the +// image machinery: a value captured in a file, resolved and verified. The API +// level is not among them; it is the Android release under test, picked on +// purpose. +func androidBumps() []imageBump { + return []imageBump{ + { + Name: "cmdline-tools", + Prefix: androidDockerfile, + Files: []string{androidDockerfile}, + Ref: cmdlineToolsRef, + Resolve: func(ctx context.Context, _ string) (string, error) { + sdk, err := latestAndroidSDK(ctx) + + return sdk.CmdlineTools, err + }, + Verify: func(ctx context.Context, want string) error { + return archivePublished(ctx, "commandlinetools-linux-"+want+"_latest.zip") + }, + }, + { + Name: "build-tools", + Prefix: androidDockerfile, + Files: []string{androidDockerfile}, + Ref: buildToolsRef, + Resolve: func(ctx context.Context, _ string) (string, error) { + sdk, err := latestAndroidSDK(ctx) + + return sdk.BuildTools, err + }, + // sdkmanager installs build tools by name from the same index, + // so being listed there is being published. + Verify: func(ctx context.Context, want string) error { + sdk, err := latestAndroidSDK(ctx) + if err != nil { + return err + } + + if !sdk.Listed[want] { + return fmt.Errorf("%w: build-tools %s", errTagMissing, want) + } + + return nil + }, + }, + } +} + +func androidAreas() []area { + defs := androidBumps() + areas := make([]area, 0, len(defs)) + + for _, def := range defs { + areas = append(areas, area{ + Name: "android:" + def.Name, + Apply: applyImage(def), + Gate: gateImage(def), + Message: func(c change) string { return def.Prefix + ": bump " + c.Summary }, + }) + } + + return areas +} diff --git a/tools/bump/area.go b/tools/bump/area.go index 03ad4f143..8de290e8a 100644 --- a/tools/bump/area.go +++ b/tools/bump/area.go @@ -82,6 +82,7 @@ func allAreas() []area { }, ) areas = append(areas, imageAreas()...) + areas = append(areas, androidAreas()...) areas = append(areas, actionAreas()...) areas = append(areas, area{ diff --git a/tools/bump/images.go b/tools/bump/images.go index b1784f5f1..f798f6416 100644 --- a/tools/bump/images.go +++ b/tools/bump/images.go @@ -34,16 +34,23 @@ var ( nodeRef = regexp.MustCompile(`(?m)^(FROM\s+node:)(\S+)`) rustRef = regexp.MustCompile(`(?m)^(FROM\s+rust:)(\S+)`) distrolessRef = regexp.MustCompile(`(gcr\.io/distroless/base-debian)(\d+)`) + temurinRef = regexp.MustCompile(`(?m)^(FROM\s+(?:--platform=\S+\s+)?eclipse-temurin:)(\S+)`) alpineTag = regexp.MustCompile(`^(\d+\.\d+)$`) nodeTag = regexp.MustCompile(`^(\d+)-alpine$`) + + // temurinTag splits "21-jdk-noble" into the Java major and the flavour. + temurinTag = regexp.MustCompile(`^(\d+)(-jdk-[a-z]+)$`) ) +// dockerfilePrefix is the commit subject package for Dockerfile image bumps. +const dockerfilePrefix = "Dockerfile" + func imageBumps() []imageBump { return []imageBump{ { Name: "alpine", - Prefix: "Dockerfile", + Prefix: dockerfilePrefix, Files: []string{"Dockerfile.derper", "Dockerfile.tailscale-HEAD"}, Ref: alpineRef, Resolve: func(ctx context.Context, _ string) (string, error) { @@ -55,7 +62,7 @@ func imageBumps() []imageBump { }, { Name: "debian", - Prefix: "Dockerfile", + Prefix: dockerfilePrefix, Files: []string{"Dockerfile.integration", "Dockerfile.integration-ci", "Dockerfile.tailscale-rs"}, Ref: debianRef, Resolve: func(ctx context.Context, _ string) (string, error) { @@ -72,7 +79,7 @@ func imageBumps() []imageBump { }, { Name: "node", - Prefix: "Dockerfile", + Prefix: dockerfilePrefix, Files: []string{"Dockerfile.wasmclient"}, Ref: nodeRef, Resolve: func(ctx context.Context, _ string) (string, error) { @@ -91,7 +98,7 @@ func imageBumps() []imageBump { }, { Name: "rust", - Prefix: "Dockerfile", + Prefix: dockerfilePrefix, Files: []string{"Dockerfile.tailscale-rs"}, Ref: rustRef, // The Rust tag carries the Debian codename it is built on, so it @@ -115,6 +122,33 @@ func imageBumps() []imageBump { return tagExists(ctx, "rust", want) }, }, + { + Name: "temurin", + Prefix: dockerfilePrefix, + Files: []string{"Dockerfile.android-integration"}, + Ref: temurinRef, + // Follows Java LTS releases and keeps the Ubuntu flavour: the + // Android SDK tools only need a supported JDK, and a new Ubuntu + // release is a separate, riskier move. + Resolve: func(ctx context.Context, have string) (string, error) { + m := temurinTag.FindStringSubmatch(have) + if m == nil { + return "", fmt.Errorf("%w: eclipse-temurin %s", errNoMatchingTag, have) + } + + pattern := regexp.MustCompile(`^(\d+)` + regexp.QuoteMeta(m[2]) + `$`) + + major, err := highestTag(ctx, "eclipse-temurin", m[2], pattern, isJavaLTS) + if err != nil { + return "", err + } + + return major + m[2], nil + }, + Verify: func(ctx context.Context, want string) error { + return tagExists(ctx, "eclipse-temurin", want) + }, + }, { Name: "distroless", Prefix: "ko", @@ -132,6 +166,14 @@ func imageBumps() []imageBump { } } +// isJavaLTS reports whether a Java major is a long-term support release: +// every fourth from 17, on a two-year cadence. +func isJavaLTS(v string) bool { + n, err := strconv.Atoi(v) + + return err == nil && n >= 17 && (n-17)%4 == 0 +} + func isEvenMajor(v string) bool { n, err := strconv.Atoi(v) diff --git a/tools/bump/images_test.go b/tools/bump/images_test.go index 0234ffae8..d70dd3df4 100644 --- a/tools/bump/images_test.go +++ b/tools/bump/images_test.go @@ -113,3 +113,76 @@ func TestIsEvenMajor(t *testing.T) { }) } } + +func TestTemurinRef(t *testing.T) { + const dockerfile = "FROM --platform=linux/amd64 eclipse-temurin:21-jdk-noble\nRUN true\n" + + m := temurinRef.FindStringSubmatch(dockerfile) + if m == nil || m[2] != "21-jdk-noble" { + t.Fatalf("temurinRef captured %v", m) + } + + want := "FROM --platform=linux/amd64 eclipse-temurin:25-jdk-noble\nRUN true\n" + if got := temurinRef.ReplaceAllString(dockerfile, "${1}25-jdk-noble"); got != want { + t.Errorf("rewrite = %q, want %q", got, want) + } +} + +func TestIsJavaLTS(t *testing.T) { + for in, want := range map[string]bool{"17": true, "21": true, "25": true, "29": true, "11": false, "22": false, "26": false, "x": false} { + if got := isJavaLTS(in); got != want { + t.Errorf("isJavaLTS(%q) = %v, want %v", in, got, want) + } + } +} + +func TestAndroidRefs(t *testing.T) { + const dockerfile = "ARG ANDROID_API=33\nARG ANDROID_CMDLINE_TOOLS_VERSION=9477386\nARG ANDROID_BUILD_TOOLS=34.0.0\n" + + if got := cmdlineToolsRef.FindStringSubmatch(dockerfile)[2]; got != "9477386" { + t.Errorf("cmdlineToolsRef captured %q", got) + } + + if got := buildToolsRef.FindStringSubmatch(dockerfile)[2]; got != "34.0.0" { + t.Errorf("buildToolsRef captured %q", got) + } +} + +// A trimmed copy of the index sdkmanager reads: a newer cmdline-tools on a +// preview channel and build-tools release candidates must not win. +const androidIndex = ` + + stable + dev + + + + + commandlinetools-linux-99_latest.ziplinux + + + + commandlinetools-mac_arm64-16111833_latest.zipmacosx + commandlinetools-linux-16111833_latest.ziplinux + + +` + +func TestParseAndroidRepo(t *testing.T) { + sdk, err := parseAndroidRepo([]byte(androidIndex)) + if err != nil { + t.Fatal(err) + } + + if sdk.CmdlineTools != "16111833" { + t.Errorf("CmdlineTools = %q, want 16111833", sdk.CmdlineTools) + } + + if sdk.BuildTools != "36.1.0" { + t.Errorf("BuildTools = %q, want 36.1.0", sdk.BuildTools) + } + + if !sdk.Listed["36.0.0"] || sdk.Listed["37.0.0-rc2"] { + t.Errorf("Listed = %v", sdk.Listed) + } +} diff --git a/tools/bump/plan.go b/tools/bump/plan.go index 199e061c1..518e91f7c 100644 --- a/tools/bump/plan.go +++ b/tools/bump/plan.go @@ -56,7 +56,7 @@ func planProviders(ctx context.Context, r *repo) string { } func planImages(ctx context.Context, r *repo) []string { - defs := imageBumps() + defs := append(imageBumps(), androidBumps()...) lines := make([]string, 0, len(defs)) for _, def := range defs {