sportrum-deploy/upgrade-cmds.sh
Claude 8c5c0fa3a0
Replace flag-based upgrade.sh with named migrate/hotfix-code modes
upgrade.sh's old -f/-w/-s flags let you skip a backup or the DB
migration independently of each other, with nothing to stop a
combination that didn't actually make sense for the change being
shipped. Replace them with two named modes:

- migrate: for a change that touches the DB. Full SQL+WAR+logs
  backup, both the existing short-lived tier and a new, never-pruned
  backup/migrations/<tag>/ tier (before/after snapshot plus the full
  DB migration transcript, tee'd from manager-exec.sh). The tag comes
  from build.xml's migrate(@host) target: the git version already
  embedded in sportrum.war's WEB-INF/VERSION (see README for the
  webapp project's own build.xml snippet this needs), plus an
  optional interactive label via Ant's <input> task.

- hotfix-code: for a Java/resource change with no DB migration. WAR
  backup only (no SQL dump, no indefinite copy), no manager-exec.sh
  at all. Only reached after check-schema-diff.sh - a new script that
  runs ICPManager's diff-meta against a staged, not-yet-installed
  copy of manager.zip, entirely independent of the live webapp/Tomcat
  - has already confirmed there's no pending schema change, hard
  gating build.xml's hotfix-code(@host) target before Tomcat is ever
  touched.

A third scenario (template-only hotfix, no Tomcat restart at all) is
intentionally not implemented yet - which files count as "templates"
and how they'd sync into the live webapp directory is still an open
question.

Dropped without replacement: the old storeonly (-s) mode and the
"leave Tomcat stopped" action argument - undocumented, flag-only
behaviors nothing here exercised, easy to reintroduce as an explicit
mode later if actually needed.

Also fixes a real gap found while testing this: a failed migration
left upgrade-cmds.sh exiting 0, which would have made upgrade.sh (and
an Ant sshexec with failonerror) report success on a failed DB
migration. And build.xml's deploy.key.passphrase had no default,
which (confirmed by testing) left the literal string
"${deploy.key.passphrase}" as the passphrase for anyone not setting
it in build.local.properties.

Verified with a full fake fixture (fake pg_dump, fake manager.zip/
manager-exec.sh, fake sportrum.war): migrate success, migrate
collision-refusal on a reused tag, migrate failure preserving the
before-snapshot without pruning, hotfix-code skipping the SQL/DB
steps, and check-schema-diff.sh's three exit codes (clean/
differences/broken) all behave as designed. Ant-side tag assembly
(<input> skip-when-preset, blank-label fallback, unsafe-character
rejection) verified the same way.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
2026-09-30 20:00:35 +00:00

161 lines
7 KiB
Bash

# Runs as the sportrum user (see upgrade.sh's "sudo su - sportrum -c"),
# with $mode and $tag already set in scope by the caller.
#
# Short-lived tier: the per-upgrade safety net, not the nightly backup.
# Every run pairs a SQL dump and a webapp/WAR snapshot under the SAME
# $timestamp so a revert always restores both together, never one without
# the other. Deliberately aggressive about pruning - old pairs older than
# RETENTION_DAYS get deleted right after a successful upgrade, since these
# exist for tactical "undo the last upgrade" use, not long-term retention.
#
# Migrations tier (migrate mode only): a SEPARATE, NEVER-pruned directory
# per tag (migrations/<tag>/) holding a before/after SQL+WAR+logs snapshot
# plus the full DB migration transcript. Kept indefinitely on purpose -
# these are the record of exactly what a given schema change did, not a
# rolling safety net - so they live under their own top-level directory
# rather than any naming convention prune_upgrade_backups would need to
# know to skip.
backupdir="/home/sportrum/backup"
RETENTION_DAYS=14
timestamp=$(date +'%Y%m%d-%H%M%S')
# Deletes every SHORT-LIVED-TIER backup artifact (sql dump, war snapshot,
# log dir) whose embedded timestamp is older than RETENTION_DAYS. Never
# touches migrations/ - that lives under a different top-level directory
# entirely, not just a different filename pattern, so there's no risk of
# this ever pruning something meant to be kept forever. Works from the
# union of timestamps found across all three backup dirs - not each dir's
# own listing - so a timestamp only disappears entirely once every
# artifact that shares it is old enough. rm -f/-rf are used throughout
# since a given timestamp may legitimately be missing one artifact (e.g.
# hotfix-code mode never takes a SQL dump), and that's not an error here.
prune_upgrade_backups() {
local cutoff_epoch
cutoff_epoch=$(date -d "-${RETENTION_DAYS} days" +%s)
local timestamps
timestamps=$( {
ls "${backupdir}/sql" 2>/dev/null | sed -n 's/^sportrum-upgrade-backup-\(.*\)\.sqldump$/\1/p'
ls "${backupdir}/webapp" 2>/dev/null | sed -n 's/^sportrum-upgrade-backup-\(.*\)\.war$/\1/p'
ls "${backupdir}/logs" 2>/dev/null | sed -n 's/^sportrum-log-backup-\(.*\)$/\1/p'
} | sort -u )
for ts in $timestamps; do
local ts_epoch
ts_epoch=$(date -d "${ts:0:4}-${ts:4:2}-${ts:6:2} ${ts:9:2}:${ts:11:2}:${ts:13:2}" +%s 2>/dev/null) || continue
if [ "$ts_epoch" -lt "$cutoff_epoch" ]; then
echo "Pruning short-lived backup pair from ${ts} (older than ${RETENTION_DAYS} days)..."
rm -f "${backupdir}/sql/sportrum-upgrade-backup-${ts}.sqldump"
rm -f "${backupdir}/webapp/sportrum-upgrade-backup-${ts}.war"
rm -rf "${backupdir}/logs/sportrum-log-backup-${ts}"
fi
done
}
# Dumps the DB, zips the current webapp, and copies current logs to the
# three given destinations. Shared by the short-lived tier and, for
# migrate mode, the migrations/<tag>/ tier - one dump/zip per instant,
# written wherever it's needed, rather than dumping the same DB twice
# (nothing else can write to it while Tomcat is stopped, so a second dump
# would just be redundant load, not a more accurate snapshot). mkdir -p on
# the destination directories defensively - sportrum_backups is a fresh
# named Docker volume (see sportrum-ansible's docker-compose.yml.j2), so
# sql/webapp/logs/migrations don't necessarily already exist on it the way
# they did on the old server's plain filesystem.
take_snapshot() {
local sql_dest="$1" war_dest="$2" logs_dest="$3"
mkdir -p "$(dirname "$sql_dest")" "$(dirname "$war_dest")"
echo "Backing up database to ${sql_dest}..."
pg_dump sportrum > "${sql_dest}"
echo "Backing up webapp to ${war_dest}..."
( cd /home/sportrum/webapp && zip -q -r sportrum sportrum && mv sportrum.zip "${war_dest}" )
echo "Backing up logs to ${logs_dest}..."
mkdir -p "${logs_dest}"
cp /home/sportrum/webapp/sportrum/logs/* "${logs_dest}"
}
case "$mode" in
migrate)
migdir="${backupdir}/migrations/${tag}"
if [ -e "$migdir" ]; then
echo "ERROR: ${migdir} already exists - refusing to overwrite an indefinitely-kept migration record. Re-run with a different tag." >&2
exit 1
fi
mkdir -p "$migdir"
sql_file="${backupdir}/sql/sportrum-upgrade-backup-${timestamp}.sqldump"
war_file="${backupdir}/webapp/sportrum-upgrade-backup-${timestamp}.war"
logs_dir="${backupdir}/logs/sportrum-log-backup-${timestamp}"
take_snapshot "$sql_file" "$war_file" "$logs_dir"
# Same instant as the short-lived-tier backup just taken above, also
# preserved indefinitely under the tag - copied, not re-dumped (see
# take_snapshot's comment).
cp "$sql_file" "${migdir}/before.sqldump"
cp "$war_file" "${migdir}/before.war"
cp -r "$logs_dir" "${migdir}/before-logs"
echo "Installing new sportrum.war and manager.zip..."
cd /home/sportrum/webapp
rm -rf sportrum/* ; unzip -q sportrum.war -d sportrum
chmod -R o+w sportrum/WEB-INF/storage
chmod -R o+w sportrum/logs
cd /home/sportrum/
rm -rf manager ; unzip -q manager.zip ; cd manager
echo "Running DB migration (transcript saved to ${migdir}/upgrade-output.log)..."
bash manager-exec.sh upgrade 2>&1 | tee "${migdir}/upgrade-output.log"
# A pipe's own exit status is tee's, not manager-exec.sh's -
# PIPESTATUS[0] is the first command in the pipe, i.e. the actual
# migration.
upgrade_status=${PIPESTATUS[0]}
cd /home/sportrum/
if [ $upgrade_status -eq 0 ]; then
echo "Taking post-migration snapshot..."
take_snapshot "${migdir}/after.sqldump" "${migdir}/after.war" "${migdir}/after-logs"
prune_upgrade_backups
else
echo "manager-exec.sh upgrade failed (exit ${upgrade_status}) - skipping the post-migration snapshot and short-lived-tier pruning so nothing is discarded until this is investigated." >&2
exit "$upgrade_status"
fi
;;
hotfix-code)
war_file="${backupdir}/webapp/sportrum-upgrade-backup-${timestamp}.war"
logs_dir="${backupdir}/logs/sportrum-log-backup-${timestamp}"
mkdir -p "$(dirname "$war_file")"
echo "Backing up webapp to ${war_file}..."
( cd /home/sportrum/webapp && zip -q -r sportrum sportrum && mv sportrum.zip "${war_file}" )
echo "Backing up logs to ${logs_dir}..."
mkdir -p "${logs_dir}"
cp /home/sportrum/webapp/sportrum/logs/* "${logs_dir}"
# No SQL backup - hotfix-code is only ever reached after
# check-schema-diff.sh has already confirmed there's no pending schema
# change, so there's nothing here a SQL dump would protect against
# that the short-lived tier's existing history doesn't already cover.
echo "Installing new sportrum.war..."
cd /home/sportrum/webapp
rm -rf sportrum/* ; unzip -q sportrum.war -d sportrum
chmod -R o+w sportrum/WEB-INF/storage
chmod -R o+w sportrum/logs
# No manager-exec.sh upgrade - hotfix-code is specifically for changes
# with no DB migration to run.
prune_upgrade_backups
;;
*)
echo "ERROR: unknown mode '$mode'" >&2
exit 1
;;
esac