sportrum-deploy/upgrade.sh

115 lines
5.2 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
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
# upgrade.sh <migrate|hotfix-code> [tag]
#
# Replaces the old "-f/-w/-s flags + optional action argument" interface
# with named modes - a flag combination was too easy to get subtly wrong
# (nothing stopped you from skipping the SQL backup on a deploy that
# actually needed a DB migration), whereas a named mode says up front what
# kind of change this is and only allows the combination that actually
# makes sense for it:
#
# migrate Full careful path, for a deploy that changes the DB
# schema or data. Takes a SQL+WAR+logs backup (both the
# short-lived tier AND an indefinitely-kept copy tagged
# with the required <tag>), installs the new
# sportrum.war/manager.zip, runs the DB migration via
# manager-exec.sh (its full transcript saved next to the
# tagged backup - see upgrade-cmds.sh), takes a matching
# post-migration snapshot, restarts Tomcat.
#
# hotfix-code For a Java/resource change with NO DB migration. Takes a
# WAR+logs backup (short-lived tier only - no SQL dump, no
# indefinite copy: there's no schema change to protect
# against that the short-lived tier's existing history
# doesn't already cover), installs the new sportrum.war,
# restarts Tomcat (still needed - class files may have
# changed and the JVM can't hot-swap those). Does NOT run
# manager-exec.sh at all: the caller (sportrum-deploy's
# build.xml, via check-schema-diff.sh) is expected to have
# already confirmed there's no pending schema change
# BEFORE ever invoking this, so skipping the DB step here
# isn't a guess made by this script.
#
# hotfix-templates (a zero-downtime template-only sync with no Tomcat
# stop/restart, no storeall, no backup at all) is intentionally NOT
# implemented yet - which files count as "templates" and how they get
# synced into the live, already-running webapp directory is still an open
# design question, not something to guess at here.
#
# Also dropped in this refactor, neither carried forward nor replaced:
# the old storeonly ("-s") mode and the "leave Tomcat stopped instead of
# restarting" action argument. Both were undocumented, flag-only
# behaviors nobody had asked about when this was redesigned around named
# modes - easy to reintroduce as an explicit mode later if actually
# needed, rather than silently keep flag combinations nothing here
# exercises or tests.
. ./upgrade-funs.sh
pid=$$
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
mode="${1:?Usage: upgrade.sh <migrate|hotfix-code> [tag]}"
tag="${2:-}"
case "$mode" in
migrate)
if [ -z "$tag" ]; then
echo "ERROR: migrate mode requires a tag (second argument) - it's how the indefinitely-kept before/after backup gets named. See sportrum-deploy's build.xml migrate(@host) target, which resolves one for you." >&2
exit 1
fi
if [[ ! "$tag" =~ ^[A-Za-z0-9._-]+$ ]]; then
echo "ERROR: tag '$tag' contains characters other than letters, digits, dots, dashes, and underscores - it becomes a directory name on the server." >&2
exit 1
fi
;;
hotfix-code)
;;
*)
echo "ERROR: unknown mode '$mode' - expected 'migrate' or 'hotfix-code'." >&2
exit 1
;;
esac
# KNOWN GAP (2026-10-01): storeall is not actually guaranteed to see a
# fully quiesced app at the moment it runs. InitServlet.destroy() calls
# HttpController.setRequestsOnHold(true) and waits (up to 2s) for
# HttpController.getRequestCount() to hit zero before calling
# icp.Registry.storeAll() - but that only tracks threads servicing
# incoming HTTP requests through HttpController. Any background thread the
# webapp itself started (e.g. the order schedulers InitServlet.init() spins
# up - see "Starting order schedulers" in InitServlet.java) is invisible to
# that counter and can still be running, and potentially still mutating
# state, while storeall takes its snapshot. storeAll()'s own
# synchronization keeps this from corrupting the DB, but a write from one
# of these threads can still land after storeall's pass and never get
# persisted before shutdown.
#
# Not fixed by anything in this script or by Tomcat's stopwaitsecs (see
# sportrum-ansible's supervisord.conf) - that only protects storeall
# itself from being killed mid-write, it does nothing about threads other
# than the app's own request-handling threads. Properly closing this needs
# a real thread manager in the webapp that can be told to halt (or at
# least pause/join) every thread it started, not just hold new requests,
# called from InitServlet.destroy() before storeAll() runs. Not
# implemented yet - noted here so it isn't forgotten.
running=$(./tomcat_ctl.sh status | grep "tomcat8 is stopped")
if [ "x$running" == "x" ]; then
printf "Call server command storeall.."; sleep 2
runcmd result storeall
if [ "x$result" == "xsuccess" ]
then
echo " done!"
else
echo " failed! exiting..."
exit 1
fi
echo "Stopping tomcat.."; sleep 2
./tomcat_ctl.sh stop
else
echo "Tomcat is not running!"
fi
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
runcmds=$(cat upgrade-cmds.sh)
sudo su - sportrum -c "mode='$mode'; tag='$tag'; ${runcmds}"
echo "Starting tomcat..."; sleep 2
./tomcat_ctl.sh start