gabriel / musehub public
activate_muse_release.sh bash
115 lines 7.0 KB
Raw
sha256:ec55e4f4980249bb2c95c8c0615a40a9ded3e1eb1c181fe3de7cefbd8298ff0d feat(release): #275 safeguard — prove a signed tag can be m… Sonnet 5.5 patch 7 days ago
1 #!/usr/bin/env bash
2 # ACTIVATE (or roll back) the release an environment serves. Separate from publishing:
3 # it only moves one environment's pointer; it never uploads, prunes or deletes.
4 #
5 # Usage:
6 # bash deploy/activate_muse_release.sh staging <version> <sha256> --expect-absent
7 # bash deploy/activate_muse_release.sh staging <version> <sha256> --expect <ver> <sha> <activated_at>
8 # bash deploy/activate_muse_release.sh production <version> <sha256> --expect ... # needs APPROVE_PRODUCTION=1
9 # bash deploy/activate_muse_release.sh <env> --rollback-to <version> <sha256> \
10 # --expect <version> <sha256> <activated_at> # explicit, idempotent recovery
11 #
12 # Activation (like rollback) is a compare-and-swap: you name the pointer state you observed
13 # (read pointers/<env>.json first; --expect-absent only for a first activation). It is a no-op if
14 # the pointer is already at the target, refused if the pointer moved since, and the write itself is
15 # a conditional S3 write, so two operators racing cannot both succeed.
16 #
17 # Rollback names the release to restore AND the pointer state you observed (read
18 # pointers/<env>.json first). It is a no-op if the pointer is already at the target, and is
19 # refused if the pointer has moved since -- so it is safe to re-run, never a blind swap.
20 #
21 # Order matters for safety:
22 # 1. Move the environment's pointer in the bucket (the ledger cleanup consults, so
23 # the new AND previous release are protected from deletion first).
24 # 2. Atomically write /data/releases/active.json on the instance -- only after the
25 # instance's tarball is proven to hash to the pointer's sha256.
26 # 3. Smoke-test what is actually served.
27 # A failure after (1) leaves the ledger ahead of the instance, the safe direction;
28 # re-running the same command is idempotent.
29 #
30 # Production: requires APPROVE_PRODUCTION=1 (explicit human approval) and the version
31 # must be what staging currently serves (promotion, not a side door).
32
33 set -euo pipefail
34
35 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
36 REPO_ROOT="$(dirname "$SCRIPT_DIR")"
37 # shellcheck source=deploy/_release_env.sh
38 . "$SCRIPT_DIR/_release_env.sh"
39
40 log() { printf '\033[1;34m%s\033[0m\n' "$*" >&2; }
41 die() { printf '\033[1;31mERROR: %s\033[0m\n' "$*" >&2; exit 1; }
42
43 USAGE="Usage: bash deploy/activate_muse_release.sh <staging|production> (<version> <sha256> (--expect-absent | --expect <version> <sha256> <activated_at>) | --rollback-to <version> <sha256> --expect <version> <sha256> <activated_at>)"
44 resolve_release_env "${1:-}" || die "$USAGE"
45
46 APPROVE=()
47 if [ "${APPROVE_PRODUCTION:-}" = "1" ]; then APPROVE=(--approve-production); fi
48
49 cd "$REPO_ROOT"
50
51 # A production forward activation is a release, and is tagged at the revision its release record names (musehub#275).
52 # Everything that can be checked is checked here, before the pointer moves, so a release that cannot be tagged is
53 # never made live: the exact verified source revision, no conflicting existing tag, and a signing identity proven by
54 # a signed probe tag made in a scratch repository (the Muse repository at $MUSE_REPO is only read). A rollback
55 # restores an already-released version and is not tagged. Staging is never tagged.
56 MUSE_REPO="${MUSE_REPO:-$HOME/ecosystem/muse}"
57 MUSE_BIN="${MUSE_BIN:-muse}"
58 TAG_RELEASE=0
59 preflight_tag() {
60 [ "$ENV" = "production" ] || return 0
61 TAG_RELEASE=1
62 python3 -m deploy.release_tag preflight --bucket "$S3_BUCKET" --version "$1" --sha256 "$2" --muse-repo "$MUSE_REPO" --muse "$MUSE_BIN" >&2 \
63 || die "release tag pre-flight refused; nothing changed"
64 }
65
66 if [ "${2:-}" = "--rollback-to" ]; then
67 [ "${5:-}" = "--expect" ] && [ -n "${8:-}" ] && [ -n "${3:-}" ] && [ -n "${4:-}" ] || die "$USAGE"
68 POINTER="$(python3 -m deploy.release_safety --bucket "$S3_BUCKET" rollback --env "$ENV" \
69 --to-version "$3" --to-sha256 "$4" --expect-version "$6" --expect-sha256 "$7" --expect-activated-at "$8" \
70 ${APPROVE[@]+"${APPROVE[@]}"})" \
71 || die "rollback refused; nothing changed"
72 elif [ -n "${2:-}" ] && [ -n "${3:-}" ] && [ "${4:-}" = "--expect-absent" ] && [ -z "${5:-}" ]; then
73 preflight_tag "$2" "$3"
74 POINTER="$(python3 -m deploy.release_safety --bucket "$S3_BUCKET" activate --env "$ENV" --version "$2" --sha256 "$3" --expect-absent \
75 ${APPROVE[@]+"${APPROVE[@]}"})" || die "activation refused; nothing changed"
76 elif [ -n "${2:-}" ] && [ -n "${3:-}" ] && [ "${4:-}" = "--expect" ] && [ -n "${5:-}" ] && [ -n "${6:-}" ] && [ -n "${7:-}" ] && [ -z "${8:-}" ]; then
77 preflight_tag "$2" "$3"
78 POINTER="$(python3 -m deploy.release_safety --bucket "$S3_BUCKET" activate --env "$ENV" --version "$2" --sha256 "$3" \
79 --expect-version "$5" --expect-sha256 "$6" --expect-activated-at "$7" \
80 ${APPROVE[@]+"${APPROVE[@]}"})" || die "activation refused; nothing changed"
81 else
82 die "$USAGE"
83 fi
84
85 VERSION="$(printf '%s' "$POINTER" | python3 -c 'import json,sys; print(json.load(sys.stdin)["version"])')"
86 SHA256="$(printf '%s' "$POINTER" | python3 -c 'import json,sys; print(json.load(sys.stdin)["sha256"])')"
87 log "[1/3] $ENV pointer now -> $VERSION ($SHA256)"
88
89 B64="$(printf '%s' "$POINTER" | base64 | tr -d '\n')"
90 CMD="docker run --rm -v musehub_data:/data alpine sh -c \
91 'cd /data/releases && echo \\\"$SHA256 muse-$VERSION.tar.gz\\\" | sha256sum -c - && \
92 echo $B64 | base64 -d > active.json.new && mv -f active.json.new active.json'"
93 log "[2/3] Writing active.json on $ENV (after verifying the instance's tarball hash)"
94 ssm_run "activate muse $VERSION" "$CMD" || die "instance sync failed; the ledger already points at $VERSION -- re-run this command"
95
96 log "[3/3] Smoke test against $BASE_URL"
97 curl -fsS "$BASE_URL/install.sh" | grep -q "MUSE_VERSION=\"$VERSION\"" || die "$BASE_URL/install.sh does not serve $VERSION"
98 bash "$SCRIPT_DIR/smoke_muse.sh" --url "$BASE_URL" --version "$VERSION"
99
100 # ── Tag (production forward activations only) ────────────────────────────────
101 #
102 # The signed SemVer tag (derived from the version by pep440_to_semver_tag.py -- muse#64) goes on the exact source
103 # revision the release record names, which the pre-flight already verified (strict commit signature), and is itself
104 # verified. deploy/release_tag.py refuses an existing tag that is anywhere else. The release is live by now, so a failure
105 # cannot be undone by refusing: it fails the run, loudly. Re-running this same command is safe (activation is idempotent)
106 # and completes the tag once the cause is fixed.
107
108 if [ "$TAG_RELEASE" = "1" ]; then
109 log "Tagging release $VERSION at the source revision its release record names"
110 python3 -m deploy.release_tag apply --bucket "$S3_BUCKET" --version "$VERSION" --sha256 "$SHA256" --muse-repo "$MUSE_REPO" --muse "$MUSE_BIN" >&2 \
111 || die "RELEASE $VERSION IS LIVE on production but its tag was NOT created and verified (see above); nothing was rolled back -- fix the cause, then re-run this same command"
112 log "Release $VERSION tagged and verified."
113 fi
114
115 log "$ENV now serves muse $VERSION."
File History 5 commits
sha256:ec55e4f4980249bb2c95c8c0615a40a9ded3e1eb1c181fe3de7cefbd8298ff0d feat(release): #275 safeguard — prove a signed tag can be m… Sonnet 5.5 patch 7 days ago
sha256:0ddcb29475cfe996169320691ffb2c5705e510191a4a944487df867959d81b36 fix(release): #275 tag the verified source revision, signed… Sonnet 5.5 minor ⚠ 7 days ago
sha256:675003834b19b109f8cb126e543a064a7bd6256930dd4a80ffcfe87c6eff3f17 fix(release): idempotent concurrent publish (D3) and compar… Opus 5.5 minor ⚠ 12 days ago
sha256:9f814406166bc157b00dfd4078fb4516ad46e9844628da6bb3823e8448eca1e9 fix(release): rollback is an explicit, idempotent, stale-pr… Opus 5.5 minor ⚠ 12 days ago
sha256:86464e3e1451e0c4629967e7d2812f4d6db4fe412f2931dd5dc28b5e20b4adbe feat(deploy): publish is gated and additive; activation is … Opus 5.5 minor ⚠ 12 days ago