| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
set -euo pipefail |
| 26 |
|
| 27 |
SSH_TARGET="${MT_ASTRA_SSH:-astra}" |
| 28 |
DB="${MT_ASTRA_DB:-multithreaded}" |
| 29 |
SERVICE="multithreaded.service" |
| 30 |
APP_DIR="/opt/multithreaded" |
| 31 |
BIN="$APP_DIR/multithreaded" |
| 32 |
ENV_FILE="$APP_DIR/.env" |
| 33 |
APP_USER="multithreaded" |
| 34 |
PORT="${MT_ASTRA_PORT:-3400}" |
| 35 |
|
| 36 |
log() { echo "[$(date -u +%H:%M:%S)] $*"; } |
| 37 |
on_astra() { ssh "$SSH_TARGET" "$@"; } |
| 38 |
|
| 39 |
log "stopping $SERVICE on $SSH_TARGET" |
| 40 |
on_astra "sudo systemctl stop $SERVICE" |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
log "resetting schema in $DB" |
| 48 |
on_astra "sudo -u postgres psql -v ON_ERROR_STOP=1 -d $DB" <<SQL |
| 49 |
DO \$\$ |
| 50 |
DECLARE s text; |
| 51 |
BEGIN |
| 52 |
FOR s IN |
| 53 |
SELECT nspname FROM pg_namespace |
| 54 |
WHERE nspname NOT LIKE 'pg_%' AND nspname <> 'information_schema' |
| 55 |
LOOP |
| 56 |
EXECUTE format('DROP SCHEMA IF EXISTS %I CASCADE', s); |
| 57 |
END LOOP; |
| 58 |
EXECUTE 'CREATE SCHEMA public AUTHORIZATION $APP_USER'; |
| 59 |
END \$\$; |
| 60 |
SQL |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
log "migrating + seeding" |
| 67 |
on_astra "sudo systemd-run --pipe --wait --collect --service-type=exec \ |
| 68 |
-p EnvironmentFile=$ENV_FILE -p WorkingDirectory=$APP_DIR -p User=$APP_USER \ |
| 69 |
$BIN --seed" |
| 70 |
|
| 71 |
log "starting $SERVICE" |
| 72 |
on_astra "sudo systemctl start $SERVICE" |
| 73 |
|
| 74 |
healthy=0 |
| 75 |
for _ in $(seq 1 20); do |
| 76 |
code=$(on_astra "curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:$PORT/api/health" || echo 000) |
| 77 |
[ "$code" = "200" ] && { log "health OK"; healthy=1; break; } |
| 78 |
sleep 3 |
| 79 |
done |
| 80 |
if [ "$healthy" -ne 1 ]; then |
| 81 |
echo "mt did not return healthy after the reset" >&2 |
| 82 |
exit 1 |
| 83 |
fi |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
log "verifying harness preconditions" |
| 91 |
counts=$(on_astra "sudo -u postgres psql -v ON_ERROR_STOP=1 -At -d $DB" <<'SQL' |
| 92 |
SELECT count(*) FROM users |
| 93 |
WHERE mnw_account_id IN ( |
| 94 |
'00000000-0000-0000-0000-00000000f001', |
| 95 |
'00000000-0000-0000-0000-00000000f002', |
| 96 |
'00000000-0000-0000-0000-00000000f003'); |
| 97 |
SELECT count(*) FROM memberships |
| 98 |
WHERE user_id = '00000000-0000-0000-0000-00000000f003' AND role = 'owner'; |
| 99 |
SELECT count(*) FROM threads; |
| 100 |
SQL |
| 101 |
) |
| 102 |
accounts=$(sed -n 1p <<<"$counts") |
| 103 |
owner_rows=$(sed -n 2p <<<"$counts") |
| 104 |
threads=$(sed -n 3p <<<"$counts") |
| 105 |
|
| 106 |
[ "$accounts" = "3" ] || { echo "expected 3 harness accounts, found ${accounts:-none}" >&2; exit 1; } |
| 107 |
[ "${owner_rows:-0}" -ge 1 ] || { echo "harness owner holds no owner membership" >&2; exit 1; } |
| 108 |
[ "${threads:-0}" -gt 0 ] || { echo "no threads seeded" >&2; exit 1; } |
| 109 |
log "harness OK: 3 accounts, owner membership present, $threads threads" |
| 110 |
|
| 111 |
log "done" |
| 112 |
|