#!/usr/bin/env bash # Publish the page renderer a release kit holds, or check the published one. # # scripts/publish-renderer.sh publish # scripts/publish-renderer.sh check # # is a runner release directory (make release-runner): # renderer.tar.gz, SOURCES, equipo-runner and COMMIT. The Linux runner in # it fetches renderer.tar.gz from the forge release whose tag is linked # into it (internal/release rendererTag), at # https://amadan.net/oficina/equipo/-/releases//renderer.tar.gz, # never from the instance. Both modes first require that everything # linked into the kit's runner about the renderer (tree pin, archive # digest, size and tag) and what the kit's install manifest records are # the kit's archive, that the tag names its digest, and that SOURCES lists # exactly the pinned packages of the kit's commit. # # publish: if the release exists, check it (below). Otherwise tag the # kit's commit (never the current checkout), push the tag, create the # release with renderer.tar.gz and SOURCES (scripts/renderer-sources.py # wrote SOURCES with the archive), and check it. Needs writer access to # oficina/equipo. # # check: download both assets in full, without credentials, into , # and require each to be the kit's file byte for byte. Never a HEAD or a # ranged request: the forge counts a redirect it issues as a download. # staging-thin.py runs it before it installs anything, and installs the # renderer from the downloaded copy. set -euo pipefail FORGE=https://amadan.net/oficina/equipo/-/releases REPO=oficina/equipo AMADAN=${AMADAN:-amadan} die() { echo "publish-renderer: $*" >&2; exit 1; } sum() { sha256sum "$1" | cut -d ' ' -f 1; } [[ $# -ge 2 ]] || die "usage: publish | check " verb=$1 kit=$2 case $verb in publish) [[ $# -eq 2 ]] || die "usage: publish " ;; check) [[ $# -eq 3 ]] || die "usage: check "; out=$3 ;; *) die "usage: publish | check " ;; esac for f in renderer.tar.gz SOURCES equipo-runner install-manifest.json COMMIT; do [[ -f $kit/$f && ! -L $kit/$f ]] || die "$kit/$f is missing" done commit=$(cat "$kit/COMMIT") [[ $commit =~ ^[0-9a-f]{40}$ ]] || die "$kit/COMMIT is not a commit" archive_sum=$(sum "$kit/renderer.tar.gz") sources_sum=$(sum "$kit/SOURCES") # What is linked into the kit's runner, as the runner itself reports it # (renderer-asset --json). Never its build information: a release build is # -trimpath, which keeps no -ldflags there (go.dev/issue/52372). It runs # before anything has vouched for the kit, so it runs in a bubblewrap # sandbox with no network, no home, no credentials and an empty # environment: only the system's own directories, read-only, and the # runner. The runner is the Linux amd64 one, so this needs a Linux amd64 # host (docs-t1 is one). isolated() { # env -i: bubblewrap's own process (pid 1 in the sandbox, whose # /proc/1/environ the runner can read) starts with no environment # either, not only the runner (--clearenv). env -i PATH=/usr/bin:/bin bwrap --unshare-all --die-with-parent --new-session --clearenv --setenv PATH /usr/bin:/bin \ --ro-bind /usr /usr --ro-bind-try /bin /bin --ro-bind-try /lib /lib --ro-bind-try /lib64 /lib64 \ --proc /proc --dev /dev --tmpfs /tmp --ro-bind "$1" /runner /runner "${@:2}" } asset=$(isolated "$kit/equipo-runner" renderer-asset --json) || die "no renderer asset is linked into $kit/equipo-runner (or it cannot run here: it needs a Linux amd64 host and bubblewrap)" linked() { python3 -I -c 'import json, sys; print(json.loads(sys.argv[1])[sys.argv[2]])' "$asset" "$1" 2>/dev/null || true; } tag=$(linked tag) [[ $tag =~ ^renderer-([0-9a-f]{12})(-r[1-9][0-9]{0,2})?$ ]] || die "no renderer tag is linked into $kit/equipo-runner" [[ ${BASH_REMATCH[1]} == "${archive_sum:0:12}" ]] || die "the linked tag $tag does not name $kit/renderer.tar.gz ($archive_sum)" pin=$(tar -xzOf "$kit/renderer.tar.gz" MANIFEST | sha256sum | cut -d ' ' -f 1) || die "$kit/renderer.tar.gz has no MANIFEST" size=$(stat -c %s "$kit/renderer.tar.gz") [[ $(linked pin) == "$pin" ]] || die "the tree pin linked into $kit/equipo-runner is not $kit/renderer.tar.gz's ($pin)" [[ $(linked sha256) == "$archive_sum" ]] || die "the archive digest linked into $kit/equipo-runner is not $kit/renderer.tar.gz's" [[ $(linked size) == "$size" ]] || die "the archive size linked into $kit/equipo-runner is not $kit/renderer.tar.gz's ($size)" python3 -I -c 'import json, sys t = json.load(open(sys.argv[1]))["targets"]["linux/amd64"] 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])))' \ "$kit/install-manifest.json" "$pin" "$archive_sum" "$size" || die "$kit/install-manifest.json does not record $kit/renderer.tar.gz" # SOURCES lists exactly the pinned packages of the kit's commit. scratch=$(mktemp -d "${TMPDIR:-/tmp}/publish-renderer.XXXXXX") trap 'rm -rf "$scratch"' EXIT git show "$commit:internal/release/renderer.json" >"$scratch/renderer.json" || die "no renderer.json at $commit" python3 -I "$(dirname "$0")/renderer-sources.py" check "$scratch/renderer.json" "$kit/SOURCES" || die "$kit/SOURCES is not the pinned packages'" # fetch NAME DEST: the published asset, whole, following the forge's # redirect to its storage. Prints the HTTP status ("404" when there is # none). fetch() { curl --silent --show-error --location --max-redirs 1 --proto '=https' --proto-redir '=https' \ --max-time 1800 --output "$2" --write-out '%{http_code}' "$FORGE/$tag/$1" } # check_into DIR: both assets downloaded into DIR are the kit's. check_into() { local dir=$1 name status for name in renderer.tar.gz SOURCES; do status=$(fetch "$name" "$dir/$name") || die "downloading $tag/$name failed" [[ $status == 200 ]] || die "$FORGE/$tag/$name answered $status" cmp -s "$dir/$name" "$kit/$name" || die "the published $tag/$name is not $kit/$name" done echo "publish-renderer: $tag matches $kit (renderer.tar.gz $archive_sum, SOURCES $sources_sum)" } if [[ $verb == check ]]; then mkdir -p "$out" check_into "$out" exit 0 fi # publish: an existing release is only checked, never replaced. status=$(fetch renderer.tar.gz "$scratch/existing") || die "asking the forge for $tag failed" if [[ $status == 200 ]]; then check_into "$scratch" exit 0 fi [[ $status == 404 ]] || die "$FORGE/$tag/renderer.tar.gz answered $status" # The tag names the kit's commit, on the forge too. remote=$(git ls-remote --tags origin "refs/tags/$tag" | cut -f 1) if [[ -z $remote ]]; then git tag "$tag" "$commit" 2>/dev/null || [[ $(git rev-parse "$tag^{commit}") == "$commit" ]] || die "a local tag $tag names another commit" git push origin "refs/tags/$tag" elif [[ $remote != "$commit" ]]; then die "the forge's tag $tag names $remote, not the kit's $commit" fi version=$(head -1 "$kit/SOURCES" | sed -n 's/^The page renderer: Chromium headless shell \([^,]*\), from Debian\.$/\1/p') [[ -n $version ]] || die "$kit/SOURCES has no Chromium version" printf '%s\n' "The page renderer: Debian packages and Chromium headless shell $version. Where each one's source is: SOURCES." >"$scratch/notes.md" "$AMADAN" release create -repo "$REPO" -notes-file "$scratch/notes.md" "$tag" "$kit/renderer.tar.gz" "$kit/SOURCES" check_into "$scratch"