mirror of
https://github.com/hanzoai/ci.git
synced 2026-11-23 20:34:08 +00:00
A site's host serves its prefix in place: the ingress reads s3://hanzo-sites/<org>/<slug> directly, and a publish rewrites that prefix and removes what its manifest does not name. So the Sites plane's releases are not what a reader of hanzo.community sees, and activating one rolled back nothing there. The publish step now keeps the release it is about to replace: bin/ rollback keep reads the live _site/manifest.txt, fetches every file it names over the public host and checks each against its hash, and the copy goes to this org's store under the run's commit. A red verify publishes that copy again through bin/site, but only while the release this run made live, named by its manifest's digest, is still serving. A site past 2000 files or 512 MiB is not kept, and says so.
43 lines
2.6 KiB
Bash
Executable File
43 lines
2.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# rollback — what a site serves, read back from its live host and kept, so a
|
|
# release that fails verification can be replaced by the one it replaced.
|
|
#
|
|
# rollback live <url> # the sha-256 of the manifest <url> serves now
|
|
# rollback keep <url> <dir> # every file of that release into <dir>; prints the same digest
|
|
#
|
|
# A site's host serves its prefix in place (the ingress reads s3://hanzo-sites/
|
|
# <org>/<slug> directly) and a publish rewrites that prefix, so the release
|
|
# before a publish exists only until the publish lands. bin/site leaves a
|
|
# `<sha256> <key>` manifest of every file at _site/manifest.txt; `keep` reads it
|
|
# and fetches each file over the public host, sixteen at a time, checking each
|
|
# against its hash, so <dir> is that release byte for byte and bin/site can
|
|
# publish it again. `live` names a release by its manifest's digest, so the
|
|
# verify job rolls back only while the release this run published still serves.
|
|
#
|
|
# A site with no manifest, or one past ROLLBACK_MAX_FILES (2000) files or
|
|
# ROLLBACK_MAX_BYTES (512 MiB), is not kept: `keep` says so and exits 3.
|
|
set -uo pipefail
|
|
cmd=${1:-}; url=${2:-}; url=${url%/}
|
|
case "$cmd" in live) [ $# = 2 ] ;; keep) [ $# = 3 ] ;; *) false ;; esac && [[ $url == https://* ]] \
|
|
|| { echo "usage: rollback live <https-url> | rollback keep <https-url> <dir>" >&2; exit 2; }
|
|
work=$(mktemp -d); trap 'rm -rf "$work"' EXIT
|
|
|
|
curl -fsS -m 30 -H 'Cache-Control: no-cache' -o "$work/m" "$url/_site/manifest.txt?t=$(date +%s)" 2>/dev/null \
|
|
&& [ -s "$work/m" ] && ! grep -qvE '^[0-9a-f]{64} [^ ]' "$work/m" \
|
|
|| { echo "::warning::rollback: $url serves no manifest, so its release cannot be kept" >&2; exit 3; }
|
|
digest=$(sha256sum < "$work/m" | cut -c1-64)
|
|
[ "$cmd" = keep ] || { echo "$digest"; exit 0; }
|
|
|
|
dir=$3 max=${ROLLBACK_MAX_FILES:-2000}
|
|
n=$(grep -c . "$work/m")
|
|
[ "$n" -le "$max" ] || { echo "::warning::rollback: $url serves $n files, past the $max a release is kept for" >&2; exit 3; }
|
|
rm -rf "$dir"; mkdir -p "$dir"
|
|
cut -c67- "$work/m" | U="$url" D="$dir" xargs -P 16 -I{} bash -c \
|
|
'mkdir -p "$(dirname "$D/$1")" && curl -fsS -g -m 120 -o "$D/$1" "$U/$1" 2>/dev/null || exit 255' _ {} \
|
|
|| { echo "::warning::rollback: a file of the release at $url did not download, so it is not kept" >&2; exit 3; }
|
|
(cd "$dir" && sha256sum -c --quiet "$work/m" >/dev/null 2>&1) \
|
|
|| { echo "::warning::rollback: the release at $url changed while it was read, so it is not kept" >&2; exit 3; }
|
|
bytes=$(du -sb "$dir" | cut -f1)
|
|
[ "$bytes" -le "${ROLLBACK_MAX_BYTES:-536870912}" ] || { echo "::warning::rollback: $url serves $bytes bytes, past what a release is kept for" >&2; exit 3; }
|
|
echo "$digest"
|