sportrum-deploy/build.xml
Claude 80f18a9f2b
Tag migrations with build-provenance + WAR hash, not a bare git version
WEB-INF/VERSION only ever held a git short-hash, which claimed more than
it could prove: two builds of the same commit can still differ (local
library jars, anything outside git), so a git version alone doesn't
guarantee what's actually in the WAR on the server.

Replaced with WEB-INF/TAG (<build-timestamp>-<git-short-hash>[-dirty] -
build time, not commit time, so a rebuilt/redeployed same commit still
gets a unique value, and "-dirty" flags an uncommitted working tree at
build time) plus a sha256 of the sportrum.war actually sitting on the
server, computed here rather than trusted from the build since a file
can't hash itself and this also catches corruption/tampering in transit.
resolve-migration-tag now reads both and the migration tag becomes
<build-tag>-<war-hash>[-<label>] instead of <git-version>[-<label>].

README and build.xml's inline docs/target descriptions updated to match.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
2026-10-01 09:37:59 +00:00

474 lines
26 KiB
XML

<?xml version="1.0" encoding="UTF-8"?>
<project name="sportrum-deploy" default="help" basedir=".">
<!-- Reuses the SAME account/key/port the sportrum webapp's own
build.xml already uses for its "release-all" target (there: scp
sportrum.war/manager.zip to sportrum@<host>:2222 with
${confdir}/sportrum_key_new) — not a separate, more-restricted
deploy account like xzpect-timer's xztimer user.
That's deliberate, not a shortcut: unlike xztimer (which can only
ever write into one directory tree), upgrade.sh needs real
privilege on this server — stopping/starting Tomcat, pg_dump,
running manager-exec.sh's DB migrations — all via the sportrum
container user's NOPASSWD sudo (see sportrum-ansible's
files/app/Dockerfile). A second, "dedicated" deploy account here
would just end up needing the exact same sudo rights, so there's
nothing to gain from provisioning one — it'd be one more key to
manage for no reduction in what a compromise of it could do. -->
<property name="deploy.user" value="sportrum"/>
<property name="deploy.ssh.port" value="2222"/>
<!-- Same physical private key the sportrum webapp project's
release-all target uses (there: ${confdir}/sportrum_key_new) — set
deploy.keyfile in build.local.properties to point at that exact
file if it doesn't live at this default path for you. Sharing one
key across both projects is intentional: it's the same account on
the same server either way, so a second key would just be one more
thing to keep in sync (and to revoke, if it's ever compromised). -->
<property name="deploy.keyfile" value="${user.home}/.ssh/sportrum_key_new"/>
<property name="deploy.knownhosts" value="${user.home}/.ssh/known_hosts"/>
<!-- Local, untracked overrides — put deploy.keyfile=... or
deploy.key.passphrase=... in here (see .gitignore) rather than
typing either in plain text anywhere that gets committed. Missing
file is fine; Ant just skips it. -->
<property file="${basedir}/build.local.properties"/>
<!-- Default so an unencrypted key doesn't get passed the LITERAL
string "${deploy.key.passphrase}" as its passphrase - an unset
Ant property is left as its own ${...} text rather than becoming
empty, which is exactly the kind of thing that fails silently
confusingly deep in a JSch stack trace. Confirmed this is a real
gotcha, not a hypothetical one, by testing an unset reference
directly. Set the real value in build.local.properties if your
key needs one; this default only applies when nothing overrides
it. -->
<property name="deploy.key.passphrase" value=""/>
<!-- Same Windows-vs-WSL ${user.home} gotcha documented at length in
xzpect-timer/build.xml: which home directory this resolves to
depends entirely on what actually runs the JVM invoking Ant
(native Windows vs. WSL/git-bash are separate filesystems with
separate ~/.ssh dirs, nothing shared automatically). Fails fast
with a clear pointer back here instead of a bare
"com.jcraft.jsch.JSchException: java.io.FileNotFoundException"
deep in an scp/sshexec stack trace. -->
<available file="${deploy.keyfile}" property="deploy.keyfile.exists"/>
<fail unless="deploy.keyfile.exists">
The SSH deploy key isn't where this build expects it:
${deploy.keyfile}
If it already works from a different shell (e.g. WSL/git-bash) but not from
here, this is almost certainly the Windows-vs-WSL home directory split
described in xzpect-timer/build.xml's comments, not a missing key. Copy it
(and known_hosts, checked separately below) over from wherever it already
works, e.g. from a WSL bash shell:
mkdir -p /mnt/c/Users/&lt;you&gt;/.ssh
cp ~/.ssh/sportrum_key_new /mnt/c/Users/&lt;you&gt;/.ssh/
If you keep this key somewhere else entirely (e.g. alongside the sportrum
webapp project's confdir/), point deploy.keyfile at it in
build.local.properties instead of moving the file.
</fail>
<available file="${deploy.knownhosts}" property="deploy.knownhosts.exists"/>
<fail unless="deploy.knownhosts.exists">
known_hosts isn't where this build expects it:
${deploy.knownhosts}
Same Windows-vs-WSL split as the deploy key above most likely applies here
too. Copy or append it over from wherever it already works (append, don't
overwrite, if the destination already has unrelated entries you want to
keep):
cat ~/.ssh/known_hosts &gt;&gt; /mnt/c/Users/&lt;you&gt;/.ssh/known_hosts
If this is truly the first time connecting to the target server from
anywhere, SSH in manually once first (e.g.
"ssh -p ${deploy.ssh.port} ${deploy.user}@&lt;server&gt;") and accept its host
key interactively — this build's scp/sshexec tasks deliberately don't
auto-trust unknown host keys.
</fail>
<!-- Ant's scp/sshexec tasks need JSch on the classpath. The jar itself
is checked into the repo (lib.build/, not lib/, since it's only
ever needed to RUN this build — nothing here ships to the server)
so there's nothing to download. See lib.build/README.md for why
this project uses the -lib approach rather than the
ant-classloadertask trick the sportrum webapp's own build.xml
uses. -->
<path id="deploy.classpath">
<fileset dir="${basedir}/lib.build" includes="*.jar"/>
</path>
<!-- classpathref alone is NOT enough on every machine — see
xzpect-timer/build.xml's comment above its identical check for the
full explanation (short version: a system ant-optional/ant-jsch
package, if one happens to be installed, gets loaded instead, and
then can't see this project's own jsch jar at all no matter what
classpathref says). -->
<available classname="com.jcraft.jsch.Logger" property="jsch.on.core.classpath"/>
<fail unless="jsch.on.core.classpath">
This build's scp/sshexec targets need JSch on ANT'S OWN classpath, not just
this project's taskdef classpathref (see lib.build/README.md). Re-run with
-lib, e.g.:
ant -lib lib.build "deploy(@local.sportrum.se)"
Running from Eclipse instead? Add lib.build/jsch-*.jar under Window ->
Preferences -> Ant -> Runtime -> Classpath -> Ant Home Entries — same fix.
</fail>
<taskdef name="scp"
classname="org.apache.tools.ant.taskdefs.optional.ssh.Scp"
classpathref="deploy.classpath"/>
<taskdef name="sshexec"
classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec"
classpathref="deploy.classpath"/>
<!-- Where this repo's own control scripts live inside the app
container. Chosen fresh (nothing was already using a fixed path
here) — a directory named after this repo, under the sportrum
user's own home, alongside webapp/ and backup/. -->
<property name="deploy.remotepath" value="/home/sportrum/sportrum-deploy/"/>
<!-- install-scripts(@host) (see below) always re-uploads these before
anything runs, so the server can never end up running a
hand-edited or stale copy that's drifted from what's actually in
this repo — the same reasoning as xzpect-timer re-installing
deploy-pwa.sh on every install. Deliberately NOT emergency-
storeall.sh/nightly-storeall.sh: those are cron-driven and live on
the server independently of any given upgrade; bundling them into
every deploy would mean an unrelated deploy could silently change
what a running cron job does mid-cycle. -->
<macrodef name="upload-scripts">
<attribute name="host"/>
<sequential>
<sshexec host="@{host}"
port="${deploy.ssh.port}"
username="${deploy.user}"
keyfile="${deploy.keyfile}"
passphrase="${deploy.key.passphrase}"
knownhosts="${deploy.knownhosts}"
command="mkdir -p ${deploy.remotepath}"/>
<echo message="Syncing upgrade.sh and friends to ${deploy.user}@@@{host}:${deploy.remotepath}..."/>
<scp todir="${deploy.user}@@@{host}:${deploy.remotepath}"
port="${deploy.ssh.port}"
keyfile="${deploy.keyfile}"
passphrase="${deploy.key.passphrase}"
knownhosts="${deploy.knownhosts}">
<fileset dir="${basedir}">
<include name="upgrade.sh"/>
<include name="upgrade-cmds.sh"/>
<include name="upgrade-funs.sh"/>
<include name="tomcat_ctl.sh"/>
<include name="restart.sh"/>
<include name="check-schema-diff.sh"/>
</fileset>
</scp>
<sshexec host="@{host}"
port="${deploy.ssh.port}"
username="${deploy.user}"
keyfile="${deploy.keyfile}"
passphrase="${deploy.key.passphrase}"
knownhosts="${deploy.knownhosts}"
command="chmod +x ${deploy.remotepath}*.sh"/>
<echo message="Scripts synced. Nothing has run yet — run &quot;ant deploy(@@@{host})&quot;, or SSH in and run ${deploy.remotepath}upgrade.sh yourself, to actually upgrade."/>
</sequential>
</macrodef>
<!-- Both deploy modes below deliberately do NOT build or upload
sportrum.war/manager.zip themselves — they assume those are
ALREADY sitting at /home/sportrum/webapp/sportrum.war and
/home/sportrum/manager.zip from a prior run of the sportrum webapp
project's own "ant release-all" (its release-all target scps them
there). That's the split the two projects deliberately keep: the
webapp project's build.xml builds and installs the war; this
project's build.xml does the actual deploy. Forgetting to run
release-all first isn't silent, though — upgrade-cmds.sh's own
"unzip -q sportrum.war -d sportrum" fails loudly if the file isn't
there, and failonerror below makes that failure fail the Ant
target too, not just get logged and ignored. -->
<!-- Verifies the "no pending DB schema change" assumption BEFORE
anything live is touched - see check-schema-diff.sh's own header
comment for why this is safe to run standalone (it only reads a
staged, not-yet-installed copy of manager.zip and the live DB,
never the running webapp/Tomcat). haltonerror="true" (the
default, for hotfix-code) fails the whole Ant build the moment
the remote check comes back non-clean, before Tomcat is ever
stopped - a wrong assumption there costs zero downtime.
haltonerror="false" (used by migrate) logs the same diff for
visibility without blocking, since migrate is exactly the mode
where a schema difference is expected, not a sign anything is
wrong. Either way, sshexec streams the remote command's output as
it runs, so the diff is visible in the build log regardless of
whether it ends up blocking anything. -->
<macrodef name="run-schema-check">
<attribute name="host"/>
<attribute name="haltonerror" default="true"/>
<sequential>
<echo message="Checking for pending schema differences against @{host} (not touching the live webapp or Tomcat)..."/>
<sshexec host="@{host}"
port="${deploy.ssh.port}"
username="${deploy.user}"
keyfile="${deploy.keyfile}"
passphrase="${deploy.key.passphrase}"
knownhosts="${deploy.knownhosts}"
command="${deploy.remotepath}check-schema-diff.sh /home/sportrum/manager.zip"
failonerror="@{haltonerror}"/>
</sequential>
</macrodef>
<!-- hotfix-code: no SQL backup, no DB migration - see upgrade.sh's own
header comment for the full rationale. Only ever reached after
run-schema-check above has already confirmed there's nothing
pending, so upgrade-cmds.sh isn't guessing when it skips the DB
step. -->
<macrodef name="run-hotfix-code">
<attribute name="host"/>
<sequential>
<echo message="Running upgrade.sh hotfix-code on @{host} — stops Tomcat, backs up the current webapp/logs (no SQL dump - no schema change to protect against), installs the new sportrum.war, restarts Tomcat. No DB migration runs..."/>
<sshexec host="@{host}"
port="${deploy.ssh.port}"
username="${deploy.user}"
keyfile="${deploy.keyfile}"
passphrase="${deploy.key.passphrase}"
knownhosts="${deploy.knownhosts}"
command="cd ${deploy.remotepath} &amp;&amp; ./upgrade.sh hotfix-code"
failonerror="true"/>
</sequential>
</macrodef>
<!-- migrate: full careful path, including the tagged, never-pruned
before/after backup and DB migration transcript under
backup/migrations/<tag>/ on the server - see upgrade-cmds.sh's own
comments for exactly what gets captured. -->
<macrodef name="run-migrate">
<attribute name="host"/>
<attribute name="tag"/>
<sequential>
<echo message="Running upgrade.sh migrate on @{host} (tag: @{tag}) — stops Tomcat, takes a tagged before-snapshot (kept indefinitely) plus the regular short-lived backup, installs the new sportrum.war/manager.zip, runs the DB migration with its transcript saved under the tag, takes a tagged after-snapshot, restarts Tomcat..."/>
<sshexec host="@{host}"
port="${deploy.ssh.port}"
username="${deploy.user}"
keyfile="${deploy.keyfile}"
passphrase="${deploy.key.passphrase}"
knownhosts="${deploy.knownhosts}"
command="cd ${deploy.remotepath} &amp;&amp; ./upgrade.sh migrate '@{tag}'"
failonerror="true"/>
</sequential>
</macrodef>
<!-- Resolves deploy.tag for migrate(@host): <build-tag>-<war-hash>[-<label>].
build-tag is read back off the sportrum.war ALREADY uploaded by the
webapp project's own release-all - see this project's README for
the WEB-INF/TAG snippet that project's build.xml needs for this to
work. It's a build-provenance string (timestamp-githash, plus a
"-dirty" suffix if the working tree had uncommitted changes at
build time) - NOT a guarantee of what the WAR actually contains,
since things outside git (e.g. local library jars) can still
differ between two builds of the same commit. That's what
war-hash is for: a hash of the actual sportrum.war sitting on the
server, computed here rather than embedded at build time, since a
file can't contain a hash of itself and this also catches any
corruption/tampering in transit. WEB-INF/, not the WAR root, since
anything outside WEB-INF/ is directly servable over HTTP by Tomcat
and this shouldn't be publicly fetchable.
The label half is optional and interactive by design (Ant's
<input> task, confirmed empirically: it skips prompting entirely
if deploy.label is already set, e.g. via -Ddeploy.label=... for a
scripted run - and confirmed working from a plain piped/typed
stdin). Hit enter to skip it; build-tag plus war-hash is already a
complete, unique, non-blank tag on its own. -->
<macrodef name="resolve-migration-tag">
<attribute name="host"/>
<sequential>
<sshexec host="@{host}"
port="${deploy.ssh.port}"
username="${deploy.user}"
keyfile="${deploy.keyfile}"
passphrase="${deploy.key.passphrase}"
knownhosts="${deploy.knownhosts}"
command="unzip -p /home/sportrum/webapp/sportrum.war WEB-INF/TAG | tr -d '\n'"
outputproperty="deploy.buildtag"
failonerror="true"/>
<!-- tr -d strips any trailing newline the webapp build's echo/
file-write may have left in WEB-INF/TAG - whether or not it
did isn't this project's business to assume either way,
and a stray newline landing in a directory name would be a
much stranger failure to debug than this one-liner is to
write. -->
<condition property="deploy.buildtag.blank">
<equals arg1="${deploy.buildtag}" arg2=""/>
</condition>
<fail if="deploy.buildtag.blank">
Couldn't read WEB-INF/TAG out of the sportrum.war on @{host}. Either
"ant release-all" hasn't been run against this server yet, or the webapp
project's build.xml doesn't embed WEB-INF/TAG yet - see this project's
README for the snippet it needs.
</fail>
<sshexec host="@{host}"
port="${deploy.ssh.port}"
username="${deploy.user}"
keyfile="${deploy.keyfile}"
passphrase="${deploy.key.passphrase}"
knownhosts="${deploy.knownhosts}"
command="sha256sum /home/sportrum/webapp/sportrum.war | cut -c1-12"
outputproperty="deploy.warhash"
failonerror="true"/>
<input message="Optional short label for this migration (e.g. 'invoice-table') - press Enter to skip:" addproperty="deploy.label"/>
<condition property="deploy.label.unsafe">
<and>
<not><equals arg1="${deploy.label}" arg2=""/></not>
<not><matches pattern="^[A-Za-z0-9._-]*$" string="${deploy.label}"/></not>
</and>
</condition>
<fail if="deploy.label.unsafe">
The migration label can only contain letters, digits, dots, dashes, and
underscores (got: "${deploy.label}") - it becomes part of a directory name
on the server. Run again without spaces or other punctuation.
</fail>
<!-- Set-once semantics do the branching here: this only takes
effect (and only when deploy.tag isn't already set, e.g.
via -Ddeploy.tag=... for a fully scripted run) when the
label is non-blank; the plain <property> right after it
is the fallback for a blank label, since it won't
override a value the condition above already set.
Confirmed both branches empirically before relying on
this. -->
<condition property="deploy.tag" value="${deploy.buildtag}-${deploy.warhash}-${deploy.label}">
<not><equals arg1="${deploy.label}" arg2=""/></not>
</condition>
<property name="deploy.tag" value="${deploy.buildtag}-${deploy.warhash}"/>
<echo message="Migration tag: ${deploy.tag}"/>
</sequential>
</macrodef>
<!-- Post-deploy check, same idea as xzpect-timer's verify-pwa — the app
itself is reverse-proxied at the domain root by Caddy (see
sportrum-ansible's templates/Caddyfile.j2), so @{host} IS the URL
to check, no path suffix needed. -->
<macrodef name="verify-upgrade">
<attribute name="host"/>
<sequential>
<echo message="Verifying https://@{host}/ responds..."/>
<get src="https://@{host}/"
dest="${java.io.tmpdir}/sportrum-deploy-check.html"
verbose="false"/>
<echo message="OK — @{host} responded. Worth also checking @{host}'s Tomcat/manager logs by hand to confirm the DB migration itself completed cleanly — an HTTP 200 here only means Tomcat came back up, not that the upgrade's migration step succeeded."/>
</sequential>
</macrodef>
<!-- No-op default so running plain "ant" never touches a live server by accident. -->
<target name="help">
<echo>Available targets:
ant "install-scripts(@local.sportrum.se)" - sync this repo's control scripts to the server (does NOT run anything)
ant "hotfix-code(@local.sportrum.se)" - for a Java/resource change with NO DB migration
ant "migrate(@local.sportrum.se)" - for a change that DOES migrate the DB schema/data
Before the FIRST run against any given server: ssh -p ${deploy.ssh.port} ${deploy.user}@&lt;that server&gt;
once, interactively (accept the host key, then Ctrl+D) so ${deploy.knownhosts}
already has it — these targets do not auto-trust unknown host keys.
Also run the sportrum webapp project's own "ant release-all" FIRST, against
the same server — it builds and uploads sportrum.war/manager.zip, which
upgrade.sh (run by both deploy targets here) expects to already be sitting
at /home/sportrum/webapp/sportrum.war and /home/sportrum/manager.zip. This
project's build.xml never builds or uploads those itself.
Two deploy targets, not one, because the two scenarios genuinely need
different handling - see upgrade.sh's own header comment for the full
rationale:
- hotfix-code(@server): for a change with no DB migration. First runs
check-schema-diff.sh against the server and HARD-FAILS if it finds any
pending schema difference, before Tomcat is ever touched - a wrong
assumption here costs zero downtime instead of a broken mid-upgrade
state. Then: WAR+logs backup only (no SQL dump - nothing to protect
against), install the new sportrum.war, restart Tomcat (still needed;
class files may have changed). No manager-exec.sh runs at all.
- migrate(@server): for a change that DOES touch the DB. Resolves a tag
from the build-provenance string already embedded in sportrum.war's
WEB-INF/TAG (see this project's README for the webapp project's own
build.xml needs) plus a hash of the sportrum.war itself, plus an
optional short label - then runs the
same schema check as above but only logs the result rather than
blocking (a diff here is expected, not a sign anything's wrong). Then:
full SQL+WAR+logs backup (the regular short-lived tier AND an
indefinitely-kept copy under backup/migrations/&lt;tag&gt;/ on the
server), install the new sportrum.war/manager.zip, run the DB
migration (its full transcript saved next to the tagged backup),
take a matching post-migration snapshot, restart Tomcat.
Both depend on install-scripts(@server) first, so the server is always
running this repo's current scripts, never a stale hand-copied version.
A third scenario - a template-only hotfix needing no Tomcat restart at all
- is intentionally not implemented as a target yet; see upgrade.sh's
header comment for why.
Because each deploy is just upgrade.sh living on the server (at
${deploy.remotepath}), you don't need ant for it at all if you'd rather
not: run "ant install-scripts(@server)" whenever you like, then SSH in as
${deploy.user} and run "./upgrade.sh hotfix-code", "./upgrade.sh migrate
&lt;tag&gt;", or "./restart.sh" (a plain restart, no upgrade) yourself, at
whatever moment you choose - check-schema-diff.sh is there too, if you
want to run that check by hand first.
Adding a new server: add
&lt;target name="install-scripts(@new-server-hostname)" description="..."&gt;
&lt;upload-scripts host="new-server-hostname"/&gt;
&lt;/target&gt;
&lt;target name="hotfix-code(@new-server-hostname)" depends="install-scripts(@new-server-hostname)" description="..."&gt;
&lt;run-schema-check host="new-server-hostname"/&gt;
&lt;run-hotfix-code host="new-server-hostname"/&gt;
&lt;verify-upgrade host="new-server-hostname"/&gt;
&lt;/target&gt;
&lt;target name="migrate(@new-server-hostname)" depends="install-scripts(@new-server-hostname)" description="..."&gt;
&lt;resolve-migration-tag host="new-server-hostname"/&gt;
&lt;run-schema-check host="new-server-hostname" haltonerror="false"/&gt;
&lt;run-migrate host="new-server-hostname" tag="${deploy.tag}"/&gt;
&lt;verify-upgrade host="new-server-hostname"/&gt;
&lt;/target&gt;
below — the shared sportrum user/key/port above cover it already, nothing
else to configure here.</echo>
</target>
<target name="install-scripts(@local.sportrum.se)"
description="Upload upgrade.sh, upgrade-cmds.sh, upgrade-funs.sh, tomcat_ctl.sh, restart.sh, and check-schema-diff.sh to local.sportrum.se. Does NOT run anything.">
<upload-scripts host="local.sportrum.se"/>
</target>
<target name="hotfix-code(@local.sportrum.se)" depends="install-scripts(@local.sportrum.se)"
description="For a Java/resource change with NO DB migration. Verifies no schema change is pending (hard fail if one is, before Tomcat is touched), then runs upgrade.sh hotfix-code on local.sportrum.se, then confirms https://local.sportrum.se/ responds.">
<run-schema-check host="local.sportrum.se"/>
<run-hotfix-code host="local.sportrum.se"/>
<verify-upgrade host="local.sportrum.se"/>
</target>
<target name="migrate(@local.sportrum.se)" depends="install-scripts(@local.sportrum.se)"
description="For a change that DOES migrate the DB. Resolves a tag (build-provenance string from sportrum.war's WEB-INF/TAG + a hash of sportrum.war itself + optional label), logs the pending schema diff, then runs upgrade.sh migrate on local.sportrum.se (tagged before/after backup kept indefinitely, DB migration with saved transcript), then confirms https://local.sportrum.se/ responds.">
<resolve-migration-tag host="local.sportrum.se"/>
<run-schema-check host="local.sportrum.se" haltonerror="false"/>
<run-migrate host="local.sportrum.se" tag="${deploy.tag}"/>
<verify-upgrade host="local.sportrum.se"/>
</target>
</project>