Files

72 lines
5.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# sage-1004/1006 Window B rehearsal, step 2, on the root seed.sh made: activate ca15d73e9 --plan, then
# --pm --migrate (3h to 3i), the post-stamp counts (mos-0838 D3), then rollback --restore --plan and
# rollback --restore. Every stack-release call names the scratch --copies and --bin. Scratch paths only.
set -uo pipefail
. /home/jwoltje/darkwing-scratch/r1004/b/env.sh
OUT=$R/out/scratch-activate-ca15d73e9.txt
[ ! -e "$OUT" ] || { echo "run.sh: $OUT exists" >&2; exit 1; }
[ -f "$RB/data/state/stack-active.json" ] || { echo "run.sh: no seeded root" >&2; exit 1; }
SR=(scripts/stack-release.sh)
CB=(--copies "$COPIES" --bin "$RB/bin")
STORE=$RB/data/bus/bus.sqlite
# A read-only read of the scratch store: schema, or the rows after STAMP in events, usage_records, instance_names.
sq() { node -e 'const {DatabaseSync}=require("node:sqlite");const [f,m,s]=process.argv.slice(1);const d=new DatabaseSync(f,{readOnly:true});
if(m==="schema")console.log(d.prepare("SELECT value FROM meta WHERE key=?").get("schema_version").value);
else for(const t of ["events","usage_records","instance_names"])console.log(`${t} after ${s}: ${d.prepare(`SELECT count(*) AS n FROM ${t} WHERE at > ?`).get(s).n} (total ${d.prepare(`SELECT count(*) AS n FROM ${t}`).get().n})`);
d.close()' -- "$STORE" "$@"; }
part() { # part TITLE CMD... : one labeled part of the transcript, with its command and exit code
local title="$1"; shift
{ echo; echo "## $title"; echo "# command: (cd $REPO && <scratch env> $*)"; } >> "$OUT"
step "run: $*"
local rc=0; (cd "$REPO" && "${E[@]}" "$@") >> "$OUT" 2>&1 || rc=$?
echo "# exit $rc" >> "$OUT"; step "exit $rc"; RC=$rc
}
note() { echo "# $*" >> "$OUT"; step "$*"; }
lastlog() { tail -1 "$RB/data/state/stack-activation-log.jsonl"; }
{
echo "# SCRATCH ROOT, NOT LIVE. Window B rehearsal (repin-ca15d73e9.md P3, mos-0838 D2, sage-1004/1006/1008),"
echo "# run by darkwing from $(date -u +%FT%TZ)."
echo "# Tool: $REPO, a git clone --shared of the canonical checkout at $(git -C "$REPO" rev-parse HEAD);"
echo "# scripts/stack-release.sh blob $(git -C "$REPO" hash-object scripts/stack-release.sh) (row 166 has not landed)."
echo "# Root: $RB. Config $RB/cfg/config.json, data root $RB/data, units in $RB/xcfg/systemd/user."
echo "# Business file: scratch-business-B.json (sha256 $(sha256sum < "$RB/cfg/businesses/mosaic-stack.json" | cut -c1-64)):"
echo "# the committed example with Window A's plug-ins, usage-source transcript and channel cli,webui."
echo "# Seeded by seed.sh at $OLD and 3h: the copy's real bus host started on stand-in units, then adopt."
echo "# Stand-ins ($B): systemctl runs the copy's real 'scripts/mosaic bus start mosaic-stack' WITHOUT --pm."
echo "# The PM launch is EMULATED: no claude runs. A waiting runner process, one sessions.json entry and one"
echo "# 'launched chief' journal line stand in for it. The Console is a /healthz stand-in on 127.0.0.1:$PORT."
echo "# journalctl and systemd-analyze are the stack-release test's. claude is a fixture that prints a version."
echo "# Credential files are fake placeholders; both base URLs in the file are 127.0.0.1:9."
echo "# Before: pointer $(cat "$RB/data/state/stack-active.json")"
echo "# Before: store schema $(sq schema)"
} > "$OUT"
step "--- run.sh"
part "1. activate --plan" "${SR[@]}" activate $NEW mosaic-stack --plan "${CB[@]}"
part "2. activate --pm --migrate" "${SR[@]}" activate $NEW mosaic-stack --pm --migrate "${CB[@]}"
A_RC=$RC; L="$(lastlog)"; note "activation log, last line: $L"
ACT="$(node -e 'console.log(JSON.parse(process.argv[1]).activation)' "$L")"
BK="$(node -e 'console.log(JSON.parse(process.argv[1]).backup??"")' "$L")"
note "activation id: $ACT; backup: $BK"
[ -n "$BK" ] && [ -f "$BK/STAMP" ] && { note "backup STAMP: $(cat "$BK/STAMP"); SCHEMA: $(cat "$BK/SCHEMA"); files: $(cd "$BK" && find . -type f | sort | tr '\n' ' ')"; } || note "no backup STAMP found"
[ "$A_RC" = 0 ] || { note "activate exit $A_RC; stopping before the rollback"; exit 1; }
part "3. status after activate" "${SR[@]}" status mosaic-stack "${CB[@]}"
note "store schema after activate (read-only, held by the bus): $(sq schema)"
part "4. launches list after activate (the NEW copy's CLI)" "$COPIES/$NEW/scripts/mosaic" launches list --json
STAMP="$(cat "$BK/STAMP")"
note "mos-0838 D3 counts, read-only from the scratch store the bus holds, rows with at > the backup STAMP:"
sq count "$STAMP" | while read -r l; do note " $l"; done
part "5. rollback --restore --plan" "${SR[@]}" rollback mosaic-stack --restore "$ACT" --plan "${CB[@]}"
part "6. rollback --restore" "${SR[@]}" rollback mosaic-stack --restore "$ACT" "${CB[@]}"
if [ "$RC" != 0 ] && grep -q 'post-backup-events' "$OUT"; then
N="$(grep -o '[0-9][0-9]* event(s) were written after the backup' "$OUT" | tail -1 | cut -d' ' -f1)"
note "the restore refused post-backup-events; rerunning with --events $N"
part "6b. rollback --restore --events $N" "${SR[@]}" rollback mosaic-stack --restore "$ACT" --events "$N" "${CB[@]}"
fi
note "activation log, last line: $(lastlog)"
part "7. status after rollback" "${SR[@]}" status mosaic-stack "${CB[@]}"
note "store schema after rollback: $(sq schema)"
note "pointer: $(cat "$RB/data/state/stack-active.json")"
note "data root bus dirs: $(cd "$RB/data" && ls -d bus* backups/stack/* 2>/dev/null | tr '\n' ' ')"
echo "done: $OUT"