Make the updater deploy tagged releases, like ds-chat's upgrade.sh

scripts/upgrade.sh now fetches tags and does a detached-HEAD checkout of
the latest one (git tag --sort=-creatordate | head -n1), matching the
ds-chat project's deploy/upgrade.sh exactly rather than a plain git pull
on whatever branch happened to be checked out. Errors clearly if the repo
has no tags yet (true for Evection right now — nothing to deploy until a
release is tagged and pushed), and no-ops cleanly if already on the
latest tag. scripts/install.sh does the same tag pin on first install
(falling back to whatever's checked out if no tags exist yet), so the
very first deploy is consistent with every upgrade after it.

Added a GET /health/ endpoint (apps.core.views.health) that checks DB
connectivity and is exempt from the first-run setup redirect, for the
upgrade script's post-restart verification (curled over the gunicorn
unix socket, same as ds-chat's curl-based check) and for any external
uptime monitor.

While wiring the setup-wizard exemption for /health/, found that
SetupWizardMiddleware._a_user_exists's reset fixture was scoped only to
apps/accounts/tests.py, so a test anywhere else that creates a user and
makes a request could leak a stale "a user exists" into later test files
with no reset — confirmed this concretely by temporarily breaking the new
exemption and watching the "works before setup" test still pass purely
from accounts/tests.py leaving the module-order-adjacent apps/articles
tests to poison the shared class attribute. Moved the fixture to a new
top-level conftest.py so it's autouse across every test file, which also
makes the new health-check test meaningful regardless of run order.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-30 22:15:37 -06:00
1 parent a89f651a71
commit 2e4674a18f
8 files changed
+128 -28

No files matched your search

+9 -5
View File
@@ -24,7 +24,7 @@ class SetupWizardMiddleware:
def __call__(self, request): def __call__(self, request):
path = request.path path = request.path
if not path.startswith(_SETUP_EXEMPT_PREFIXES) and path not in _pwa_exempt_paths(): if not path.startswith(_SETUP_EXEMPT_PREFIXES) and path not in _setup_exempt_paths():
if not SetupWizardMiddleware._a_user_exists: if not SetupWizardMiddleware._a_user_exists:
SetupWizardMiddleware._a_user_exists = User.objects.exists() SetupWizardMiddleware._a_user_exists = User.objects.exists()
setup_url = reverse("accounts:setup") setup_url = reverse("accounts:setup")
@@ -33,12 +33,16 @@ class SetupWizardMiddleware:
return self.get_response(request) return self.get_response(request)
def _pwa_exempt_paths(): def _setup_exempt_paths():
"""The manifest/service worker/offline page must work even before the """Routes that must work even before the instance's first-run setup is
instance's first-run setup is complete — a browser can fetch these on complete: the manifest/service worker/offline page (a browser can fetch
any visit, setup page included (it extends the same base template).""" these on any visit, setup page included — it extends the same base
template) and the health check (ops tooling, e.g. scripts/upgrade.sh,
needs it reachable immediately after a fresh deploy, before anyone has
created an account)."""
return { return {
reverse("pwa:manifest"), reverse("pwa:manifest"),
reverse("pwa:service_worker"), reverse("pwa:service_worker"),
reverse("pwa:offline"), reverse("pwa:offline"),
reverse("core:health"),
} }
+2 -7
View File
@@ -1,15 +1,10 @@
import pytest import pytest
from django.urls import reverse from django.urls import reverse
from .middleware import SetupWizardMiddleware
from .models import User from .models import User
# SetupWizardMiddleware's cache is reset around every test by the autouse
@pytest.fixture(autouse=True) # fixture in the top-level conftest.py, not just here.
def _reset_setup_wizard_cache():
SetupWizardMiddleware._a_user_exists = False
yield
SetupWizardMiddleware._a_user_exists = False
@pytest.mark.django_db @pytest.mark.django_db
+16
View File
@@ -6,6 +6,22 @@ from django.urls import reverse
from apps.feeds.models import Feed, Subscription from apps.feeds.models import Feed, Subscription
@pytest.mark.django_db
def test_health_check_returns_ok_without_login(client):
response = client.get(reverse("core:health"))
assert response.status_code == 200
assert response.json() == {"status": "ok"}
@pytest.mark.django_db
def test_health_check_works_before_first_run_setup(client):
# No User exists yet, so SetupWizardMiddleware would normally redirect
# every request to /setup/ — scripts/upgrade.sh needs this reachable
# immediately after a fresh deploy, before anyone's created an account.
response = client.get(reverse("core:health"))
assert response.status_code == 200
@pytest.mark.django_db @pytest.mark.django_db
def test_dashboard_requires_staff(client, django_user_model): def test_dashboard_requires_staff(client, django_user_model):
django_user_model.objects.create_user("regular", password="pw-regular-123") django_user_model.objects.create_user("regular", password="pw-regular-123")
+1
View File
@@ -5,6 +5,7 @@ from . import views
app_name = "core" app_name = "core"
urlpatterns = [ urlpatterns = [
path("health/", views.health, name="health"),
path("dashboard/", views.dashboard, name="dashboard"), path("dashboard/", views.dashboard, name="dashboard"),
path("dashboard/feeds/<int:feed_id>/retry/", views.retry_feed, name="retry_feed"), path("dashboard/feeds/<int:feed_id>/retry/", views.retry_feed, name="retry_feed"),
] ]
+15
View File
@@ -1,6 +1,8 @@
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.decorators import user_passes_test from django.contrib.auth.decorators import user_passes_test
from django.db import connection
from django.db.models import Count, Q from django.db.models import Count, Q
from django.http import JsonResponse
from django.shortcuts import get_object_or_404, redirect, render from django.shortcuts import get_object_or_404, redirect, render
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
@@ -11,6 +13,19 @@ from apps.feeds.tasks import fetch_feed, reset_feed_backoff
is_admin = user_passes_test(lambda u: u.is_authenticated and u.is_staff) is_admin = user_passes_test(lambda u: u.is_authenticated and u.is_staff)
def health(request):
"""Unauthenticated liveness/readiness check — reachable even before
first-run setup (see SetupWizardMiddleware). Used by scripts/upgrade.sh
to confirm a freshly restarted deploy actually came up, and is a
reasonable target for any external uptime monitor too."""
try:
with connection.cursor() as cursor:
cursor.execute("SELECT 1")
except Exception:
return JsonResponse({"status": "error"}, status=503)
return JsonResponse({"status": "ok"})
@is_admin @is_admin
def dashboard(request): def dashboard(request):
feeds = Feed.objects.annotate(subscriber_count=Count("subscriptions")) feeds = Feed.objects.annotate(subscriber_count=Count("subscriptions"))
+18
View File
@@ -0,0 +1,18 @@
import pytest
from apps.accounts.middleware import SetupWizardMiddleware
@pytest.fixture(autouse=True)
def _reset_setup_wizard_cache():
"""SetupWizardMiddleware caches "does any User exist" on the class
itself (not per-request), so it survives across tests in the same
pytest process regardless of Django's per-test DB rollback. Autouse
and global (not scoped to apps/accounts/tests.py) so no test file can
leak a stale True into another — a test exercising "no user exists
yet" behavior must actually start from that state, not from whatever
an earlier, unrelated test happened to leave behind.
"""
SetupWizardMiddleware._a_user_exists = False
yield
SetupWizardMiddleware._a_user_exists = False
+15
View File
@@ -63,6 +63,21 @@ else
fi fi
chown -R "$EVECTION_USER:$EVECTION_USER" "$APP_DIR" chown -R "$EVECTION_USER:$EVECTION_USER" "$APP_DIR"
# --- 2b. Pin to the latest release tag (if any exist) ----------------
if [ -d "$APP_DIR/.git" ]; then
sudo -u "$EVECTION_USER" -H git -C "$APP_DIR" fetch --tags --force || true
LATEST_TAG="$(sudo -u "$EVECTION_USER" -H git -C "$APP_DIR" tag --sort=-creatordate | head -n1)"
if [ -n "$LATEST_TAG" ]; then
echo "==> Checking out latest release: $LATEST_TAG"
# Detached HEAD, not a branch checkout — matches scripts/upgrade.sh,
# which only ever deploys a tagged release, never a branch tip.
sudo -u "$EVECTION_USER" -H git -C "$APP_DIR" checkout --quiet --detach "$LATEST_TAG"
else
echo "==> No release tags found yet — staying on whatever's currently checked out."
fi
fi
# --- 3. Virtualenv + install --------------------------------------------- # --- 3. Virtualenv + install ---------------------------------------------
if [ ! -d "$VENV_DIR" ]; then if [ ! -d "$VENV_DIR" ]; then
+52 -16
View File
@@ -1,14 +1,19 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Automates docs/install.md's "Upgrading" section: pull the latest code, # Day-2 deploy/upgrade script, matching the same pattern as the ds-chat
# reinstall dependencies, migrate, collect static files, restart services. # 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:
# #
# Run as root, from inside an existing install:
# sudo ./scripts/upgrade.sh # sudo ./scripts/upgrade.sh
# #
# Requires scripts/install.sh (or the equivalent manual steps) to have # 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 # been run already, and at least one git tag to exist in the repo (there
# deploy from release tarballs instead, extract the new one over this # aren't any yet in a fresh clone of this project — cut and push a release
# directory yourself before running this script. # 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 set -euo pipefail
@@ -21,6 +26,7 @@ APP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
EVECTION_HOME="$(dirname "$APP_DIR")" EVECTION_HOME="$(dirname "$APP_DIR")"
VENV_DIR="$EVECTION_HOME/venv" VENV_DIR="$EVECTION_HOME/venv"
EVECTION_USER="${EVECTION_USER:-evection}" EVECTION_USER="${EVECTION_USER:-evection}"
SOCKET="/run/evection/gunicorn.sock"
if ! id -u "$EVECTION_USER" >/dev/null 2>&1; then if ! id -u "$EVECTION_USER" >/dev/null 2>&1; then
echo "System user '$EVECTION_USER' doesn't exist — is Evection installed?" \ echo "System user '$EVECTION_USER' doesn't exist — is Evection installed?" \
@@ -32,18 +38,31 @@ if [ ! -x "$VENV_DIR/bin/python" ]; then
"Run scripts/install.sh first." >&2 "Run scripts/install.sh first." >&2
exit 1 exit 1
fi fi
if [ ! -d "$APP_DIR/.git" ]; then
echo "==> Upgrading Evection in $APP_DIR" 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
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 exit 1
fi fi
echo "==> Fetching latest release"
cd "$APP_DIR"
sudo -u "$EVECTION_USER" -H git fetch --tags --force
LATEST_TAG="$(sudo -u "$EVECTION_USER" -H 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 v1.0.0 && git push --tags)." >&2
exit 1
fi
CURRENT_REF="$(sudo -u "$EVECTION_USER" -H 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.
sudo -u "$EVECTION_USER" -H git checkout --quiet --detach "$LATEST_TAG"
echo "==> Installing updated dependencies" echo "==> Installing updated dependencies"
sudo -u "$EVECTION_USER" -H "$VENV_DIR/bin/pip" install --upgrade "$APP_DIR" sudo -u "$EVECTION_USER" -H "$VENV_DIR/bin/pip" install --upgrade "$APP_DIR"
@@ -56,14 +75,31 @@ sudo -u "$EVECTION_USER" -H env DJANGO_SETTINGS_MODULE=config.settings.prod \
"$VENV_DIR/bin/python" "$APP_DIR/manage.py" collectstatic --noinput "$VENV_DIR/bin/python" "$APP_DIR/manage.py" collectstatic --noinput
echo "==> Restarting services" echo "==> Restarting services"
RESTARTED_ANY=false
for unit in evection-gunicorn evection-celery-worker evection-celery-beat; do 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 if systemctl list-unit-files "${unit}.service" 2>/dev/null | grep -q "${unit}.service"; then
systemctl restart "$unit" systemctl restart "$unit"
RESTARTED_ANY=true
fi fi
done 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
if curl -sf --unix-socket "$SOCKET" http://localhost/health/ >/dev/null; 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
echo "==> Upgrade complete." echo "==> Deployed $LATEST_TAG."
echo " If this release changed cached static assets (CSS/JS/icons), bump" 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 " 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." echo " again so installed PWAs drop their old cache instead of serving stale assets."
echo " journalctl -u evection-gunicorn -f to watch logs."