Files
Evection/scripts/upgrade.sh
T
ksmithandClaude Sonnet 5.5 fed5bcc49e Document the release tag format: v<year>.<month>.<day>, then .2, .3
The docs and upgrade.sh's hint showed zero-padded `date +%Y.%m.%d` and said
nothing about a second release in a day. Tags are unpadded
(v2026.11.1) with .2, .3, ... appended for each further release that day.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-01 12:17:49 -07:00

177 lines
7.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Day-2 deploy/upgrade script, matching the same pattern as the ds-chat
# project's deploy/upgrade.sh: always deploys the latest git tag via a
# detached-HEAD checkout, never whatever the default branch's tip happens
# to be. Run as root, from inside an existing install:
#
# sudo ./scripts/upgrade.sh
#
# Requires scripts/install.sh (or the equivalent manual steps) to have
# been run already, and at least one git tag to exist in the repo (there
# aren't any yet in a fresh clone of this project — cut and push a release
# tag first; this script has nothing to deploy otherwise and says so).
#
# Fails loudly and stops before restarting anything if any step fails —
# the previous deploy keeps running rather than being torn down mid-
# upgrade.
set -euo pipefail
if [ "$(id -u)" -ne 0 ]; then
echo "Run this as root: sudo ./scripts/upgrade.sh" >&2
exit 1
fi
APP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
EVECTION_HOME="$(dirname "$APP_DIR")"
VENV_DIR="$EVECTION_HOME/venv"
EVECTION_USER="${EVECTION_USER:-evection}"
# $APP_DIR is owned by $EVECTION_USER, but the git steps below run as
# root — without this, git refuses to touch a repo it doesn't own
# ("detected dubious ownership").
git config --global --add safe.directory "$APP_DIR"
if ! id -u "$EVECTION_USER" >/dev/null 2>&1; then
echo "System user '$EVECTION_USER' doesn't exist — is Evection installed?" \
"Run scripts/install.sh first." >&2
exit 1
fi
if [ ! -x "$VENV_DIR/bin/python" ]; then
echo "No virtualenv found at $VENV_DIR — is Evection installed?" \
"Run scripts/install.sh first." >&2
exit 1
fi
if [ ! -d "$APP_DIR/.git" ]; then
echo "$APP_DIR isn't a git checkout, so there's no tag history to deploy from." \
"This script only supports git-based installs." >&2
exit 1
fi
echo "==> Fetching latest release"
cd "$APP_DIR"
# Run git as root (this script already requires it), not as $EVECTION_USER:
# the service user has no reason to have its own git credentials set up,
# and GIT_TERMINAL_PROMPT=0 means a credential problem fails fast instead
# of hanging the upgrade on a silent username/password prompt.
#
# Deliberately NOT --force (S7): a plain `git fetch --tags` refuses to
# move a tag ref this checkout already has onto a different commit — the
# exact situation if a release tag were rewritten (rolled back, re-cut
# against a different commit, or tampered with) rather than a new tag
# being added. --force would silently follow that rewritten tag into
# production; refusing and stopping here means a human has to look at
# what actually changed before this script will deploy it.
set +e
FETCH_OUTPUT="$(GIT_TERMINAL_PROMPT=0 git fetch --tags 2>&1)"
FETCH_STATUS=$?
set -e
if [ "$FETCH_STATUS" -ne 0 ]; then
echo "$FETCH_OUTPUT" >&2
if echo "$FETCH_OUTPUT" | grep -q "would clobber existing tag"; then
echo
echo "A release tag points at a different commit than this checkout" \
"already has for it, rather than only new tags having been added." \
"Refusing to force-follow a rewritten tag (S7) — review what" \
"actually changed before deciding to update it yourself, e.g.:" \
" git fetch --tags --dry-run -v" >&2
fi
exit 1
fi
LATEST_TAG="$(git tag --sort=-creatordate | head -n1)"
if [ -z "$LATEST_TAG" ]; then
echo "No tags found in this repo — nothing to deploy." \
"Cut and push a release tag first (e.g. git tag v$(date +%Y.%-m.%-d) && git push --tags)." >&2
exit 1
fi
CURRENT_REF="$(git describe --tags --exact-match 2>/dev/null || echo "none")"
if [ "$CURRENT_REF" = "$LATEST_TAG" ]; then
echo "Already on the latest tag ($LATEST_TAG) — nothing to do."
exit 0
fi
echo "Deploying $LATEST_TAG (currently on: $CURRENT_REF)"
# Detached HEAD, not a branch checkout — this directory only ever runs a
# tagged release, never whatever the default branch's tip happens to be.
git checkout --quiet --detach "$LATEST_TAG"
chown -R "$EVECTION_USER:$EVECTION_USER" "$APP_DIR"
# Bump the PWA's cache-busting version to this release's tag on every
# deploy, not just ones that happened to touch static assets — a missed
# manual bump otherwise leaves installed PWAs serving a stale cached
# shell/pages indefinitely. Cheap even when nothing cacheable changed:
# it just makes the service worker open a fresh cache name next time it
# activates.
if grep -q '^EVECTION_PWA_CACHE_VERSION=' "$APP_DIR/.env"; then
sed -i "s/^EVECTION_PWA_CACHE_VERSION=.*/EVECTION_PWA_CACHE_VERSION=$LATEST_TAG/" "$APP_DIR/.env"
else
echo "EVECTION_PWA_CACHE_VERSION=$LATEST_TAG" >> "$APP_DIR/.env"
fi
echo "==> Installing updated dependencies"
# requirements.txt (generated by `pip-compile --generate-hashes`, committed
# alongside pyproject.toml, and checked out as part of $LATEST_TAG above)
# pins every dependency to an exact, hash-verified version (S7) — this
# deploy gets exactly the dependency code that release was tested
# against, and a tampered or substituted package on the index fails the
# hash check instead of installing silently.
sudo -u "$EVECTION_USER" -H "$VENV_DIR/bin/pip" install --require-hashes -r "$APP_DIR/requirements.txt"
sudo -u "$EVECTION_USER" -H "$VENV_DIR/bin/pip" install --no-deps --upgrade "$APP_DIR"
echo "==> Running migrations"
sudo -u "$EVECTION_USER" -H env DJANGO_SETTINGS_MODULE=config.settings.prod \
"$VENV_DIR/bin/python" "$APP_DIR/manage.py" migrate --noinput
echo "==> Collecting static files"
sudo -u "$EVECTION_USER" -H env DJANGO_SETTINGS_MODULE=config.settings.prod \
"$VENV_DIR/bin/python" "$APP_DIR/manage.py" collectstatic --noinput
echo "==> Restarting services"
RESTARTED_ANY=false
for unit in evection-gunicorn evection-celery-worker evection-celery-beat; do
if systemctl list-unit-files "${unit}.service" 2>/dev/null | grep -q "${unit}.service"; then
systemctl restart "$unit"
RESTARTED_ANY=true
fi
done
if [ "$RESTARTED_ANY" = false ]; then
echo "No evection systemd units found to restart — did scripts/install.sh run?" >&2
exit 1
fi
echo "==> Verifying"
sleep 2
# gunicorn always listens on a plain TCP address (EVECTION_GUNICORN_BIND in
# .env, set by scripts/install.sh) rather than a Unix socket, since the
# reverse proxy in front of it may be on a different host entirely — see
# docs/install.md. 0.0.0.0 isn't a valid address to connect *to*, so check
# against localhost in that case; this health check only needs to prove
# gunicorn itself is answering on this host, not reach it the way the
# proxy does.
HEALTH_BIND="$(grep -m1 '^EVECTION_GUNICORN_BIND=' "$APP_DIR/.env" | cut -d= -f2-)"
HEALTH_BIND="${HEALTH_BIND:-127.0.0.1:8000}"
HEALTH_HOST="${HEALTH_BIND%:*}"
HEALTH_PORT="${HEALTH_BIND##*:}"
if [ "$HEALTH_HOST" = "0.0.0.0" ]; then
HEALTH_HOST="127.0.0.1"
fi
# Use the venv's own Python rather than curl — curl isn't guaranteed to be
# installed on a minimal host, but the venv obviously already is.
if "$VENV_DIR/bin/python" -c "
import sys, urllib.request
try:
urllib.request.urlopen('http://$HEALTH_HOST:$HEALTH_PORT/health/', timeout=5)
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)
"; then
echo "Health check OK"
else
echo "Health check FAILED — check: journalctl -u evection-gunicorn -n 50" >&2
exit 1
fi
systemctl status evection-gunicorn --no-pager -l | head -10
echo
echo "==> Deployed $LATEST_TAG."
echo " journalctl -u evection-gunicorn -f to watch logs."