Add Ant deploy target, mirroring xzpect-timer's build.xml pattern
install-scripts(@host) syncs upgrade.sh/upgrade-cmds.sh/upgrade-funs.sh/ tomcat_ctl.sh/restart.sh to the server so it never runs a stale hand-copied version; deploy(@host) runs that first, then sshexecs upgrade.sh itself, then checks the site responds. Reuses the sportrum webapp project's own account (sportrum@host:2222, same key as its release-all target) rather than a separate dedicated deploy user like xzpect-timer's xztimer - upgrade.sh needs real privilege (Tomcat control, pg_dump, DB migrations via the container's NOPASSWD sudo), so a second account would need the same rights anyway. This project's build.xml never builds or uploads sportrum.war/ manager.zip itself; that's release-all's job in the webapp project - this one only does the actual deploy once those are already staged. Uses the -lib lib.build / checked-in jsch jar approach for the JSch classpath gotcha, same as xzpect-timer, rather than the webapp project's ant-classloadertask trick - that one's JDK-8-only, and this build isn't guaranteed to run under the same JDK as the webapp's. Verified end-to-end short of an actual server: ant -p and ant help both hit the documented pre-flight failures correctly (missing key, missing JSch on classpath) before -lib is added, succeed after; with a real generated key, sshexec correctly gets past key-parsing and attempts a real connection. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
This commit is contained in:
parent
9291393965
commit
3fd244a5be
4 changed files with 353 additions and 0 deletions
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
.DS_Store
|
||||
Thumbs.db
|
||||
.project
|
||||
.settings/
|
||||
|
||||
# Local overrides for build.xml (deploy.keyfile, deploy.key.passphrase, ...)
|
||||
# — never commit a passphrase, even to a private repo.
|
||||
build.local.properties
|
||||
281
build.xml
Normal file
281
build.xml
Normal file
|
|
@ -0,0 +1,281 @@
|
|||
<?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"/>
|
||||
|
||||
<!-- 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/<you>/.ssh
|
||||
cp ~/.ssh/sportrum_key_new /mnt/c/Users/<you>/.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 >> /mnt/c/Users/<you>/.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}@<server>") 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"/>
|
||||
</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 "ant deploy(@@@{host})", or SSH in and run ${deploy.remotepath}upgrade.sh yourself, to actually upgrade."/>
|
||||
</sequential>
|
||||
</macrodef>
|
||||
|
||||
<!-- The actual upgrade — sshexecs upgrade.sh on the server, which:
|
||||
calls storeall and stops Tomcat, backs up the current SQL/webapp
|
||||
(the short-lived tier — see upgrade-cmds.sh), unzips the NEW
|
||||
sportrum.war/manager.zip, runs manager-exec.sh's DB migrations,
|
||||
then restarts Tomcat.
|
||||
|
||||
This target deliberately does NOT build or upload
|
||||
sportrum.war/manager.zip itself — it assumes they're 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 this Ant
|
||||
target too, not just get logged and ignored. -->
|
||||
<macrodef name="run-upgrade">
|
||||
<attribute name="host"/>
|
||||
<sequential>
|
||||
<echo message="Running upgrade.sh on @{host} — this stops Tomcat, backs up the current SQL dump/webapp/logs, installs the new sportrum.war/manager.zip, runs DB migrations, then 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} && ./upgrade.sh"
|
||||
failonerror="true"/>
|
||||
</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 upgrade.sh and friends to the server (does NOT run an upgrade)
|
||||
ant "deploy(@local.sportrum.se)" - install-scripts(@local.sportrum.se), then run upgrade.sh, then verify the site responds
|
||||
|
||||
Before the FIRST run against any given server: ssh -p ${deploy.ssh.port} ${deploy.user}@<that server>
|
||||
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 the deploy target 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.
|
||||
|
||||
install-scripts/deploy are deliberately separate steps, not one big
|
||||
"deploy" that both syncs scripts and upgrades:
|
||||
- install-scripts(@server) only uploads this repo's control scripts
|
||||
(upgrade.sh, upgrade-cmds.sh, upgrade-funs.sh, tomcat_ctl.sh,
|
||||
restart.sh) to ${deploy.remotepath} on the server. Nothing on the
|
||||
server is touched otherwise — no Tomcat stop, no backup, no DB
|
||||
migration.
|
||||
- deploy(@server) runs install-scripts(@server) first (so the server is
|
||||
always running this repo's current scripts, never a stale hand-copied
|
||||
version), then sshexecs upgrade.sh itself, then checks the site
|
||||
responds afterwards.
|
||||
|
||||
Because the upgrade 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" (or "./restart.sh", for a plain
|
||||
restart with no upgrade) yourself, at whatever moment you choose.
|
||||
|
||||
Adding a new server: add
|
||||
<target name="install-scripts(@new-server-hostname)" description="...">
|
||||
<upload-scripts host="new-server-hostname"/>
|
||||
</target>
|
||||
<target name="deploy(@new-server-hostname)" depends="install-scripts(@new-server-hostname)" description="...">
|
||||
<run-upgrade host="new-server-hostname"/>
|
||||
<verify-upgrade host="new-server-hostname"/>
|
||||
</target>
|
||||
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, and restart.sh to local.sportrum.se. Does NOT run an upgrade — see deploy(@local.sportrum.se).">
|
||||
<upload-scripts host="local.sportrum.se"/>
|
||||
</target>
|
||||
|
||||
<target name="deploy(@local.sportrum.se)" depends="install-scripts(@local.sportrum.se)"
|
||||
description="install-scripts(@local.sportrum.se), then run upgrade.sh on local.sportrum.se (stop Tomcat, back up, install the new sportrum.war/manager.zip, migrate the DB, restart Tomcat), then confirm https://local.sportrum.se/ responds.">
|
||||
<run-upgrade host="local.sportrum.se"/>
|
||||
<verify-upgrade host="local.sportrum.se"/>
|
||||
</target>
|
||||
|
||||
</project>
|
||||
64
lib.build/README.md
Normal file
64
lib.build/README.md
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
# lib.build/
|
||||
|
||||
Third-party jars needed by `build.xml`, checked in so a fresh clone on any
|
||||
machine can run `ant -lib lib.build "deploy(@local.sportrum.se)"` with
|
||||
nothing to download. See the comment above `<path id="deploy.classpath">`
|
||||
in `build.xml` for why a plain `classpathref` isn't always enough on its
|
||||
own (short version: Ant always loads its own `Scp`/`SSHExec` classes from
|
||||
whatever's already on Ant's own classpath — and if something else, like
|
||||
Debian/Ubuntu's `ant-optional` apt package, already put a different/older
|
||||
jsch there first, this project's own copy in `lib.build/` becomes invisible
|
||||
to it no matter what the project's own classpath says).
|
||||
|
||||
**Command line: always use `-lib lib.build`.** Confirmed this is required
|
||||
whenever a system `ant-optional` package (or similar) is installed — that's
|
||||
the case for a plain apt-installed Ubuntu/Debian `ant`. Leaving it off fails
|
||||
fast with a clear message pointing back here, rather than a cryptic
|
||||
`NoClassDefFoundError`.
|
||||
|
||||
**Eclipse: try it with no extra configuration first.** Whether this is
|
||||
needed in Eclipse depends entirely on whether that particular Eclipse
|
||||
install's bundled Ant distribution ships its own conflicting `ant-jsch.jar`.
|
||||
If yours hits `taskdef ... cannot be found: com/jcraft/jsch/Logger`, add
|
||||
`lib.build/jsch-*.jar` under Window -> Preferences -> Ant -> Runtime ->
|
||||
Classpath -> Ant Home Entries (same underlying fix as `-lib`, just via
|
||||
Eclipse's UI instead of a flag).
|
||||
|
||||
Currently just:
|
||||
|
||||
- `jsch-2.28.7.jar` — [com.github.mwiede/jsch](https://mvnrepository.com/artifact/com.github.mwiede/jsch),
|
||||
a maintained fork of the original com.jcraft:jsch (same package name,
|
||||
drop-in). Ant's `<scp>`/`<sshexec>` tasks need this on the classpath; the
|
||||
original jcraft version predates modern SSH key-exchange algorithms and
|
||||
fails against current OpenSSH defaults. Direct download:
|
||||
https://repo1.maven.org/maven2/com/github/mwiede/jsch/2.28.7/jsch-2.28.7.jar
|
||||
|
||||
Same jar `xzpect-timer/lib.build/` uses — copied over rather than
|
||||
re-resolved, so both projects are pinned to an identical, already-tested
|
||||
version. If this ever needs a version bump, replace the jar here (and
|
||||
probably there too, for consistency) and update the filename/link above —
|
||||
nothing else in `build.xml` references the version number directly (it
|
||||
just loads every `*.jar` under this folder).
|
||||
|
||||
## Why not `ant-classloadertask`, like the sportrum webapp project?
|
||||
|
||||
The sportrum webapp's own `build.xml` (its `release-all` target scps
|
||||
`sportrum.war`/`manager.zip` to the server) avoids needing `-lib` at all,
|
||||
via a third-party `<classloader loader="project" classpath="...">` task
|
||||
that injects the jsch jar directly into Ant's own running core classloader.
|
||||
That trick only works on JDK 8 — it reflectively pokes
|
||||
`URLClassLoader.addURL()`, which stopped being how the JVM's application
|
||||
classloader works from JDK 9 onward. The webapp project gets away with it
|
||||
only because it's still built on JDK 8; xzpect-timer's `build.xml` tested
|
||||
this same approach directly against JDK 21 and confirmed it's a hard dead
|
||||
end there (see xzpect-timer's `lib.build/README.md` for the exact
|
||||
exceptions).
|
||||
|
||||
This project (`sportrum-deploy`) deliberately follows xzpect-timer's
|
||||
`-lib`-based approach instead of the webapp's, even though it's deployed
|
||||
right alongside that JDK-8 webapp project: `ant deploy(@host)` here is a
|
||||
separate Ant invocation, and there's no guarantee it always runs under the
|
||||
same JDK as the webapp build — someone could easily run it from a newer
|
||||
default `ant` on their machine. The `-lib` approach works on any JDK, so
|
||||
it's the safer default here rather than inheriting an assumption tied to a
|
||||
different project's toolchain.
|
||||
BIN
lib.build/jsch-2.28.7.jar
Normal file
BIN
lib.build/jsch-2.28.7.jar
Normal file
Binary file not shown.
Loading…
Add table
Reference in a new issue