| 1 | #!/usr/bin/env bash |
| 2 | # Publish the page renderer a release kit holds, or check the published one. |
| 3 | # |
| 4 | # scripts/publish-renderer.sh publish <kit> |
| 5 | # scripts/publish-renderer.sh check <kit> <dir> |
| 6 | # |
| 7 | # <kit> is a runner release directory (make release-runner): |
| 8 | # renderer.tar.gz, SOURCES, equipo-runner and COMMIT. The Linux runner in |
| 9 | # it fetches renderer.tar.gz from the forge release whose tag is linked |
| 10 | # into it (internal/release rendererTag), at |
| 11 | # https://amadan.net/oficina/equipo/-/releases/<tag>/renderer.tar.gz, |
| 12 | # never from the instance. Both modes first require that everything |
| 13 | # linked into the kit's runner about the renderer (tree pin, archive |
| 14 | # digest, size and tag) and what the kit's install manifest records are |
| 15 | # the kit's archive, that the tag names its digest, and that SOURCES lists |
| 16 | # exactly the pinned packages of the kit's commit. |
| 17 | # |
| 18 | # publish: if the release exists, check it (below). Otherwise tag the |
| 19 | # kit's commit (never the current checkout), push the tag, create the |
| 20 | # release with renderer.tar.gz and SOURCES (scripts/renderer-sources.py |
| 21 | # wrote SOURCES with the archive), and check it. Needs writer access to |
| 22 | # oficina/equipo. |
| 23 | # |
| 24 | # check: download both assets in full, without credentials, into <dir>, |
| 25 | # and require each to be the kit's file byte for byte. Never a HEAD or a |
| 26 | # ranged request: the forge counts a redirect it issues as a download. |
| 27 | # staging-thin.py runs it before it installs anything, and installs the |
| 28 | # renderer from the downloaded copy. |
| 29 | set -euo pipefail |
| 30 | |
| 31 | FORGE=https://amadan.net/oficina/equipo/-/releases |
| 32 | REPO=oficina/equipo |
| 33 | AMADAN=${AMADAN:-amadan} |
| 34 | |
| 35 | die() { echo "publish-renderer: $*" >&2; exit 1; } |
| 36 | sum() { sha256sum "$1" | cut -d ' ' -f 1; } |
| 37 | |
| 38 | [[ $# -ge 2 ]] || die "usage: publish <kit> | check <kit> <dir>" |
| 39 | verb=$1 kit=$2 |
| 40 | case $verb in |
| 41 | publish) [[ $# -eq 2 ]] || die "usage: publish <kit>" ;; |
| 42 | check) [[ $# -eq 3 ]] || die "usage: check <kit> <dir>"; out=$3 ;; |
| 43 | *) die "usage: publish <kit> | check <kit> <dir>" ;; |
| 44 | esac |
| 45 | for f in renderer.tar.gz SOURCES equipo-runner install-manifest.json COMMIT; do |
| 46 | [[ -f $kit/$f && ! -L $kit/$f ]] || die "$kit/$f is missing" |
| 47 | done |
| 48 | commit=$(cat "$kit/COMMIT") |
| 49 | [[ $commit =~ ^[0-9a-f]{40}$ ]] || die "$kit/COMMIT is not a commit" |
| 50 | archive_sum=$(sum "$kit/renderer.tar.gz") |
| 51 | sources_sum=$(sum "$kit/SOURCES") |
| 52 | |
| 53 | # What is linked into the kit's runner, as the runner itself reports it |
| 54 | # (renderer-asset --json). Never its build information: a release build is |
| 55 | # -trimpath, which keeps no -ldflags there (go.dev/issue/52372). It runs |
| 56 | # before anything has vouched for the kit, so it runs in a bubblewrap |
| 57 | # sandbox with no network, no home, no credentials and an empty |
| 58 | # environment: only the system's own directories, read-only, and the |
| 59 | # runner. The runner is the Linux amd64 one, so this needs a Linux amd64 |
| 60 | # host (docs-t1 is one). |
| 61 | isolated() { |
| 62 | # env -i: bubblewrap's own process (pid 1 in the sandbox, whose |
| 63 | # /proc/1/environ the runner can read) starts with no environment |
| 64 | # either, not only the runner (--clearenv). |
| 65 | env -i PATH=/usr/bin:/bin bwrap --unshare-all --die-with-parent --new-session --clearenv --setenv PATH /usr/bin:/bin \ |
| 66 | --ro-bind /usr /usr --ro-bind-try /bin /bin --ro-bind-try /lib /lib --ro-bind-try /lib64 /lib64 \ |
| 67 | --proc /proc --dev /dev --tmpfs /tmp --ro-bind "$1" /runner /runner "${@:2}" |
| 68 | } |
| 69 | asset=$(isolated "$kit/equipo-runner" renderer-asset --json) || |
| 70 | die "no renderer asset is linked into $kit/equipo-runner (or it cannot run here: it needs a Linux amd64 host and bubblewrap)" |
| 71 | linked() { python3 -I -c 'import json, sys; print(json.loads(sys.argv[1])[sys.argv[2]])' "$asset" "$1" 2>/dev/null || true; } |
| 72 | tag=$(linked tag) |
| 73 | [[ $tag =~ ^renderer-([0-9a-f]{12})(-r[1-9][0-9]{0,2})?$ ]] || die "no renderer tag is linked into $kit/equipo-runner" |
| 74 | [[ ${BASH_REMATCH[1]} == "${archive_sum:0:12}" ]] || die "the linked tag $tag does not name $kit/renderer.tar.gz ($archive_sum)" |
| 75 | pin=$(tar -xzOf "$kit/renderer.tar.gz" MANIFEST | sha256sum | cut -d ' ' -f 1) || die "$kit/renderer.tar.gz has no MANIFEST" |
| 76 | size=$(stat -c %s "$kit/renderer.tar.gz") |
| 77 | [[ $(linked pin) == "$pin" ]] || die "the tree pin linked into $kit/equipo-runner is not $kit/renderer.tar.gz's ($pin)" |
| 78 | [[ $(linked sha256) == "$archive_sum" ]] || die "the archive digest linked into $kit/equipo-runner is not $kit/renderer.tar.gz's" |
| 79 | [[ $(linked size) == "$size" ]] || die "the archive size linked into $kit/equipo-runner is not $kit/renderer.tar.gz's ($size)" |
| 80 | python3 -I -c 'import json, sys |
| 81 | t = json.load(open(sys.argv[1]))["targets"]["linux/amd64"] |
| 82 | sys.exit((t.get("renderer_sha256"), t.get("renderer_archive_sha256"), t.get("renderer_archive_size")) != (sys.argv[2], sys.argv[3], int(sys.argv[4])))' \ |
| 83 | "$kit/install-manifest.json" "$pin" "$archive_sum" "$size" || die "$kit/install-manifest.json does not record $kit/renderer.tar.gz" |
| 84 | |
| 85 | # SOURCES lists exactly the pinned packages of the kit's commit. |
| 86 | scratch=$(mktemp -d "${TMPDIR:-/tmp}/publish-renderer.XXXXXX") |
| 87 | trap 'rm -rf "$scratch"' EXIT |
| 88 | git show "$commit:internal/release/renderer.json" >"$scratch/renderer.json" || die "no renderer.json at $commit" |
| 89 | python3 -I "$(dirname "$0")/renderer-sources.py" check "$scratch/renderer.json" "$kit/SOURCES" || die "$kit/SOURCES is not the pinned packages'" |
| 90 | |
| 91 | # fetch NAME DEST: the published asset, whole, following the forge's |
| 92 | # redirect to its storage. Prints the HTTP status ("404" when there is |
| 93 | # none). |
| 94 | fetch() { |
| 95 | curl --silent --show-error --location --max-redirs 1 --proto '=https' --proto-redir '=https' \ |
| 96 | --max-time 1800 --output "$2" --write-out '%{http_code}' "$FORGE/$tag/$1" |
| 97 | } |
| 98 | |
| 99 | # check_into DIR: both assets downloaded into DIR are the kit's. |
| 100 | check_into() { |
| 101 | local dir=$1 name status |
| 102 | for name in renderer.tar.gz SOURCES; do |
| 103 | status=$(fetch "$name" "$dir/$name") || die "downloading $tag/$name failed" |
| 104 | [[ $status == 200 ]] || die "$FORGE/$tag/$name answered $status" |
| 105 | cmp -s "$dir/$name" "$kit/$name" || die "the published $tag/$name is not $kit/$name" |
| 106 | done |
| 107 | echo "publish-renderer: $tag matches $kit (renderer.tar.gz $archive_sum, SOURCES $sources_sum)" |
| 108 | } |
| 109 | |
| 110 | if [[ $verb == check ]]; then |
| 111 | mkdir -p "$out" |
| 112 | check_into "$out" |
| 113 | exit 0 |
| 114 | fi |
| 115 | |
| 116 | # publish: an existing release is only checked, never replaced. |
| 117 | status=$(fetch renderer.tar.gz "$scratch/existing") || die "asking the forge for $tag failed" |
| 118 | if [[ $status == 200 ]]; then |
| 119 | check_into "$scratch" |
| 120 | exit 0 |
| 121 | fi |
| 122 | [[ $status == 404 ]] || die "$FORGE/$tag/renderer.tar.gz answered $status" |
| 123 | |
| 124 | # The tag names the kit's commit, on the forge too. |
| 125 | remote=$(git ls-remote --tags origin "refs/tags/$tag" | cut -f 1) |
| 126 | if [[ -z $remote ]]; then |
| 127 | git tag "$tag" "$commit" 2>/dev/null || [[ $(git rev-parse "$tag^{commit}") == "$commit" ]] || die "a local tag $tag names another commit" |
| 128 | git push origin "refs/tags/$tag" |
| 129 | elif [[ $remote != "$commit" ]]; then |
| 130 | die "the forge's tag $tag names $remote, not the kit's $commit" |
| 131 | fi |
| 132 | version=$(head -1 "$kit/SOURCES" | sed -n 's/^The page renderer: Chromium headless shell \([^,]*\), from Debian\.$/\1/p') |
| 133 | [[ -n $version ]] || die "$kit/SOURCES has no Chromium version" |
| 134 | printf '%s\n' "The page renderer: Debian packages and Chromium headless shell $version. Where each one's source is: SOURCES." >"$scratch/notes.md" |
| 135 | "$AMADAN" release create -repo "$REPO" -notes-file "$scratch/notes.md" "$tag" "$kit/renderer.tar.gz" "$kit/SOURCES" |
| 136 | check_into "$scratch" |
| 137 | |