#!/usr/bin/env bash # Automates docs/install.md's "Upgrading" section: pull the latest code, # reinstall dependencies, migrate, collect static files, restart services. # # 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 the app directory to be a git checkout — if you # deploy from release tarballs instead, extract the new one over this # directory yourself before running this script. 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}" 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 echo "==> Upgrading Evection in $APP_DIR" if [ -d "$APP_DIR/.git" ]; then echo "==> Pulling latest code" sudo -u "$EVECTION_USER" -H git -C "$APP_DIR" pull --ff-only else echo "$APP_DIR isn't a git checkout — extract the new release over it" \ "yourself, then re-run this script to finish the upgrade." >&2 exit 1 fi echo "==> Installing updated dependencies" sudo -u "$EVECTION_USER" -H "$VENV_DIR/bin/pip" install --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" 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" fi done echo echo "==> Upgrade complete." echo " If this release changed cached static assets (CSS/JS/icons), bump" echo " EVECTION_PWA_CACHE_VERSION in $APP_DIR/.env and restart evection-gunicorn" echo " again so installed PWAs drop their old cache instead of serving stale assets."