gabriel / musehub public
activate_muse_release.sh bash
103 lines 5.9 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 if [ "${2:-}" = "--rollback-to" ]; then
51 [ "${5:-}" = "--expect" ] && [ -n "${8:-}" ] && [ -n "${3:-}" ] && [ -n "${4:-}" ] || die "$USAGE"
52 POINTER="$(python3 -m deploy.release_safety --bucket "$S3_BUCKET" rollback --env "$ENV" \
53 --to-version "$3" --to-sha256 "$4" --expect-version "$6" --expect-sha256 "$7" --expect-activated-at "$8" \
54 ${APPROVE[@]+"${APPROVE[@]}"})" \
55 || die "rollback refused; nothing changed"
56 elif [ -n "${2:-}" ] && [ -n "${3:-}" ] && [ "${4:-}" = "--expect-absent" ] && [ -z "${5:-}" ]; then
57 POINTER="$(python3 -m deploy.release_safety --bucket "$S3_BUCKET" activate --env "$ENV" --version "$2" --sha256 "$3" --expect-absent \
58 ${APPROVE[@]+"${APPROVE[@]}"})" || die "activation refused; nothing changed"
59 elif [ -n "${2:-}" ] && [ -n "${3:-}" ] && [ "${4:-}" = "--expect" ] && [ -n "${5:-}" ] && [ -n "${6:-}" ] && [ -n "${7:-}" ] && [ -z "${8:-}" ]; then
60 POINTER="$(python3 -m deploy.release_safety --bucket "$S3_BUCKET" activate --env "$ENV" --version "$2" --sha256 "$3" \
61 --expect-version "$5" --expect-sha256 "$6" --expect-activated-at "$7" \
62 ${APPROVE[@]+"${APPROVE[@]}"})" || die "activation refused; nothing changed"
63 else
64 die "$USAGE"
65 fi
66
67 VERSION="$(printf '%s' "$POINTER" | python3 -c 'import json,sys; print(json.load(sys.stdin)["version"])')"
68 SHA256="$(printf '%s' "$POINTER" | python3 -c 'import json,sys; print(json.load(sys.stdin)["sha256"])')"
69 log "[1/3] $ENV pointer now -> $VERSION ($SHA256)"
70
71 B64="$(printf '%s' "$POINTER" | base64 | tr -d '\n')"
72 CMD="docker run --rm -v musehub_data:/data alpine sh -c \
73 'cd /data/releases && echo \\\"$SHA256 muse-$VERSION.tar.gz\\\" | sha256sum -c - && \
74 echo $B64 | base64 -d > active.json.new && mv -f active.json.new active.json'"
75 log "[2/3] Writing active.json on $ENV (after verifying the instance's tarball hash)"
76 ssm_run "activate muse $VERSION" "$CMD" || die "instance sync failed; the ledger already points at $VERSION -- re-run this command"
77
78 log "[3/3] Smoke test against $BASE_URL"
79 curl -fsS "$BASE_URL/install.sh" | grep -q "MUSE_VERSION=\"$VERSION\"" || die "$BASE_URL/install.sh does not serve $VERSION"
80 bash "$SCRIPT_DIR/smoke_muse.sh" --url "$BASE_URL" --version "$VERSION"
81
82 # ── Tag (production only) ────────────────────────────────────────────────────
83 #
84 # pyproject.toml stays in canonical PEP 440 form, but `muse tag add` enforces strict
85 # SemVer 2.0 (hyphen before a pre-release identifier), so the tag is derived, not
86 # hand-typed -- see muse#64. A failure here (e.g. the tag already exists) is a
87 # warning, not fatal: the release is already live by this point. Staging
88 # candidates are not tagged as releases.
89
90 MUSE_VERSION="$VERSION"
91 MUSE_REPO="${MUSE_REPO:-$HOME/ecosystem/muse}"
92 if [ "$ENV" = "production" ]; then
93 TAG=$(python3 "$SCRIPT_DIR/pep440_to_semver_tag.py" "$MUSE_VERSION")
94 log "Tagging release as $TAG"
95 if muse -C "$MUSE_REPO" tag add "$TAG" --message "release $MUSE_VERSION"; then
96 log " Tagged $TAG"
97 else
98 log " Tagging failed (release is still live) -- tag manually with:"
99 log " muse -C \"$MUSE_REPO\" tag add \"$TAG\" --message \"release $MUSE_VERSION\""
100 fi
101 fi
102
103 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 ⚠ 13 days ago