Private
Public Access
pyproject.toml used broad version ranges with no lock file, so two deployments of the same release could end up with different dependency versions, and there was no check that a package actually matches what was reviewed/tested — a compromised or substituted package on the index could install silently. requirements.txt and requirements-dev.txt (generated via `pip-compile --generate-hashes`, see the new README section on regenerating them) pin every dependency to an exact version with its hash. install.sh and upgrade.sh now install from requirements.txt with --require-hashes, then the app itself with --no-deps — a tampered or substituted package fails the hash check instead of installing. Both scripts also no longer `git fetch --tags --force`: a plain fetch refuses to move a tag ref they already have onto a different commit, so a rewritten/tampered release tag is reported and stops the deploy/install instead of being silently followed into production. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
283 lines
13 KiB
Bash
Executable File
283 lines
13 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Automates docs/install.md's manual steps: system user, virtualenv, .env,
|
|
# migrate/collectstatic, systemd units. Does NOT set up a reverse proxy —
|
|
# Evection always requires one in front of gunicorn (see docs/install.md),
|
|
# but which one (local nginx/Caddy/Traefik, or something like Nginx Proxy
|
|
# Manager on a different host entirely) is up to you, so this script just
|
|
# leaves gunicorn listening on EVECTION_GUNICORN_BIND for you to point it
|
|
# at.
|
|
#
|
|
# Run as root, from inside the checkout you want to serve:
|
|
# sudo ./scripts/install.sh
|
|
#
|
|
# Safe to re-run: it skips steps that are already done (existing user,
|
|
# existing venv, existing .env) rather than redoing or overwriting them.
|
|
#
|
|
# Non-interactive use (e.g. scripted provisioning) — set these before
|
|
# running, and the script won't prompt:
|
|
# EVECTION_ALLOWED_HOSTS, EVECTION_DATABASE_URL, EVECTION_REDIS_URL,
|
|
# EVECTION_GUNICORN_BIND, EVECTION_USER (defaults to "evection")
|
|
|
|
set -euo pipefail
|
|
|
|
if [ "$(id -u)" -ne 0 ]; then
|
|
echo "Run this as root: sudo ./scripts/install.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}"
|
|
PYTHON_BIN="${PYTHON_BIN:-python3}"
|
|
|
|
# $APP_DIR ends up owned by $EVECTION_USER (see below), but the git steps
|
|
# in this script run as root — without this, git refuses to touch a repo
|
|
# it doesn't own ("detected dubious ownership"), which is exactly what
|
|
# happens re-running this script after a previous run got far enough to
|
|
# chown the checkout.
|
|
git config --global --add safe.directory "$APP_DIR"
|
|
|
|
echo "==> Installing Evection"
|
|
echo " App directory: $APP_DIR"
|
|
echo " Service user: $EVECTION_USER"
|
|
echo " Virtualenv: $VENV_DIR"
|
|
echo
|
|
|
|
# --- 1. Prerequisites -------------------------------------------------
|
|
|
|
if ! command -v "$PYTHON_BIN" >/dev/null 2>&1; then
|
|
echo "'$PYTHON_BIN' not found. Install Python 3.11+ first." >&2
|
|
exit 1
|
|
fi
|
|
PY_VERSION="$("$PYTHON_BIN" -c 'import sys; print("%d.%d" % sys.version_info[:2])')"
|
|
PY_MAJOR="${PY_VERSION%%.*}"
|
|
PY_MINOR="${PY_VERSION##*.}"
|
|
if [ "$PY_MAJOR" -lt 3 ] || { [ "$PY_MAJOR" -eq 3 ] && [ "$PY_MINOR" -lt 11 ]; }; then
|
|
echo "Python $PY_VERSION found, but Evection needs 3.11 or newer." >&2
|
|
exit 1
|
|
fi
|
|
if ! "$PYTHON_BIN" -c 'import ensurepip' >/dev/null 2>&1; then
|
|
echo "'$PYTHON_BIN' can't create virtualenvs (no ensurepip) — on" \
|
|
"Debian/Ubuntu, install it with: apt install python${PY_VERSION}-venv" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Celery (and therefore all feed fetching) is entirely dependent on Redis
|
|
# being reachable — it fails silently from the installer's point of view
|
|
# (the worker/beat processes just retry forever in the background), so this
|
|
# has to be a hard failure here, not a warning that's easy to miss while
|
|
# every later step "succeeds" at installing an app that can never fetch a
|
|
# single feed. Check the actual REDIS_URL that will end up in .env (an
|
|
# explicit EVECTION_REDIS_URL override, if set — the default local Redis
|
|
# otherwise), not just whether some redis-cli happens to be on PATH.
|
|
REDIS_URL_TO_CHECK="${EVECTION_REDIS_URL:-redis://localhost:6379/0}"
|
|
check_redis() {
|
|
"$PYTHON_BIN" -c "
|
|
import sys, socket, urllib.parse
|
|
u = urllib.parse.urlparse('$REDIS_URL_TO_CHECK')
|
|
try:
|
|
with socket.create_connection((u.hostname or 'localhost', u.port or 6379), timeout=3):
|
|
pass
|
|
except OSError as e:
|
|
print(e, file=sys.stderr)
|
|
sys.exit(1)
|
|
"
|
|
}
|
|
REDIS_HOST="$("$PYTHON_BIN" -c "import urllib.parse; print(urllib.parse.urlparse('$REDIS_URL_TO_CHECK').hostname or 'localhost')")"
|
|
if ! check_redis; then
|
|
if [[ "$REDIS_HOST" =~ ^(localhost|127\.0\.0\.1|::1)$ ]] && command -v apt-get >/dev/null 2>&1; then
|
|
echo "==> Redis isn't reachable at $REDIS_URL_TO_CHECK — installing redis-server"
|
|
apt-get install -y redis-server
|
|
systemctl enable --now redis-server
|
|
if ! check_redis; then
|
|
echo "Redis still isn't reachable after installing redis-server —" \
|
|
"check: systemctl status redis-server" >&2
|
|
exit 1
|
|
fi
|
|
else
|
|
echo "Redis isn't reachable at $REDIS_URL_TO_CHECK. Evection's background" \
|
|
"feed-fetching depends on it entirely, so this has to be fixed before" \
|
|
"continuing — either start/install Redis there, or set" \
|
|
"EVECTION_REDIS_URL to point at wherever it actually runs." >&2
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# --- 2. System user -----------------------------------------------------
|
|
|
|
if ! id -u "$EVECTION_USER" >/dev/null 2>&1; then
|
|
echo "==> Creating system user '$EVECTION_USER'"
|
|
adduser --system --group --home "$EVECTION_HOME" "$EVECTION_USER"
|
|
else
|
|
echo "==> System user '$EVECTION_USER' already exists"
|
|
fi
|
|
|
|
# --- 2b. Pin to the latest release tag (if any exist) ----------------
|
|
#
|
|
# Run as root (this script already requires it), not as $EVECTION_USER:
|
|
# this repo was typically cloned by root (or whoever has git credentials
|
|
# configured), and the service user has no reason to have its own git
|
|
# auth set up. GIT_TERMINAL_PROMPT=0 means a credential problem fails
|
|
# fast instead of hanging the install on a silent username/password
|
|
# prompt.
|
|
if [ -d "$APP_DIR/.git" ]; then
|
|
# Deliberately NOT --force (S7, matches scripts/upgrade.sh): a plain
|
|
# `git fetch --tags` refuses to move a tag ref this checkout already
|
|
# has onto a different commit, so a rewritten/tampered release tag
|
|
# gets reported here instead of silently followed. A fresh checkout
|
|
# with no tags at all, or an unreachable remote, both legitimately
|
|
# fail this fetch too — this is a first install, not an upgrade, so
|
|
# either case still falls through to "stay on whatever's checked out"
|
|
# below rather than blocking the install.
|
|
FETCH_OUTPUT="$(GIT_TERMINAL_PROMPT=0 git -C "$APP_DIR" fetch --tags 2>&1)" || {
|
|
echo "$FETCH_OUTPUT" >&2
|
|
if echo "$FETCH_OUTPUT" | grep -q "would clobber existing tag"; then
|
|
echo "==> A release tag points at a different commit than expected —" \
|
|
"leaving it alone rather than force-following it. Investigate" \
|
|
"with 'git fetch --tags --dry-run -v' before deploying." >&2
|
|
fi
|
|
}
|
|
LATEST_TAG="$(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.
|
|
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
|
|
|
|
# Everything under $EVECTION_HOME (not just $APP_DIR — the venv created
|
|
# below lives next to it, not inside it) needs to belong to the service
|
|
# user, whether $EVECTION_HOME already existed (e.g. you mkdir'd it
|
|
# yourself before cloning) or adduser just created it.
|
|
chown "$EVECTION_USER:$EVECTION_USER" "$EVECTION_HOME"
|
|
chown -R "$EVECTION_USER:$EVECTION_USER" "$APP_DIR"
|
|
|
|
# --- 3. Virtualenv + install ---------------------------------------------
|
|
|
|
if [ -d "$VENV_DIR" ] && [ ! -x "$VENV_DIR/bin/pip" ]; then
|
|
echo "==> $VENV_DIR exists but looks incomplete (no bin/pip) — recreating it"
|
|
rm -rf "$VENV_DIR"
|
|
fi
|
|
if [ ! -d "$VENV_DIR" ]; then
|
|
echo "==> Creating virtualenv at $VENV_DIR"
|
|
sudo -u "$EVECTION_USER" -H "$PYTHON_BIN" -m venv "$VENV_DIR"
|
|
fi
|
|
echo "==> Installing Evection and its dependencies"
|
|
sudo -u "$EVECTION_USER" -H "$VENV_DIR/bin/pip" install --upgrade pip
|
|
# requirements.txt (generated by `pip-compile --generate-hashes`, committed
|
|
# alongside pyproject.toml) pins every dependency to an exact, hash-
|
|
# verified version (S7) — two installs of the same release get identical
|
|
# dependency code, and a tampered or substituted package on the index
|
|
# fails the hash check instead of installing silently. --require-hashes
|
|
# also means every entry in the file must carry a hash, so an
|
|
# unreviewed/unpinned addition fails closed rather than installing
|
|
# unverified. The app itself is installed separately with --no-deps: its
|
|
# own version is pinned by which git tag is checked out, not by this file.
|
|
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 "$APP_DIR"
|
|
|
|
# --- 4. .env --------------------------------------------------------------
|
|
|
|
ENV_FILE="$APP_DIR/.env"
|
|
if [ ! -f "$ENV_FILE" ]; then
|
|
echo "==> Creating $ENV_FILE"
|
|
SECRET_KEY="$("$VENV_DIR/bin/python" -c 'import secrets; print(secrets.token_urlsafe(50))')"
|
|
|
|
ALLOWED_HOSTS="${EVECTION_ALLOWED_HOSTS:-}"
|
|
if [ -z "$ALLOWED_HOSTS" ] && [ -t 0 ]; then
|
|
read -rp "Domain name(s) this instance will be served from (comma-separated) [localhost]: " ALLOWED_HOSTS
|
|
fi
|
|
ALLOWED_HOSTS="${ALLOWED_HOSTS:-localhost}"
|
|
|
|
DATABASE_URL="${EVECTION_DATABASE_URL:-}"
|
|
if [ -z "$DATABASE_URL" ] && [ -t 0 ]; then
|
|
read -rp "Use PostgreSQL instead of SQLite? [y/N]: " USE_PG
|
|
if [[ "${USE_PG:-}" =~ ^[Yy]$ ]]; then
|
|
read -rp "Postgres DATABASE_URL (postgres://user:pass@host:5432/dbname): " DATABASE_URL
|
|
fi
|
|
fi
|
|
DATABASE_URL="${DATABASE_URL:-sqlite:///$APP_DIR/db.sqlite3}"
|
|
|
|
GUNICORN_BIND="${EVECTION_GUNICORN_BIND:-}"
|
|
if [ -z "$GUNICORN_BIND" ] && [ -t 0 ]; then
|
|
echo
|
|
echo "Evection must run behind a reverse proxy — gunicorn needs an" \
|
|
"address:port that proxy can reach."
|
|
read -rp "Is the reverse proxy on this same machine, or a different one? [same/different] [same]: " PROXY_LOCATION
|
|
if [[ "${PROXY_LOCATION:-}" =~ ^[Dd] ]]; then
|
|
read -rp "Address:port for gunicorn to listen on (e.g. 0.0.0.0:8000, or this host's own IP:8000) [0.0.0.0:8000]: " GUNICORN_BIND
|
|
GUNICORN_BIND="${GUNICORN_BIND:-0.0.0.0:8000}"
|
|
echo " Remember to firewall this port to only accept connections" \
|
|
"from the reverse proxy's host."
|
|
else
|
|
read -rp "Address:port for gunicorn to listen on [127.0.0.1:8000]: " GUNICORN_BIND
|
|
GUNICORN_BIND="${GUNICORN_BIND:-127.0.0.1:8000}"
|
|
fi
|
|
fi
|
|
GUNICORN_BIND="${GUNICORN_BIND:-127.0.0.1:8000}"
|
|
|
|
cat > "$ENV_FILE" <<EOF
|
|
SECRET_KEY=$SECRET_KEY
|
|
DEBUG=False
|
|
ALLOWED_HOSTS=$ALLOWED_HOSTS
|
|
DATABASE_URL=$DATABASE_URL
|
|
REDIS_URL=${EVECTION_REDIS_URL:-redis://localhost:6379/0}
|
|
EVECTION_GUNICORN_BIND=$GUNICORN_BIND
|
|
EVECTION_SIGNUP_ENABLED=False
|
|
EVECTION_MIN_FETCH_INTERVAL_MINUTES=15
|
|
EVECTION_PWA_CACHE_VERSION=${LATEST_TAG:-v1}
|
|
EOF
|
|
chown "$EVECTION_USER:$EVECTION_USER" "$ENV_FILE"
|
|
chmod 600 "$ENV_FILE"
|
|
else
|
|
echo "==> $ENV_FILE already exists — leaving it alone"
|
|
fi
|
|
|
|
# Needed below for the final "gunicorn is listening on..." message even
|
|
# when $ENV_FILE already existed and the block above was skipped.
|
|
GUNICORN_BIND="$(grep -m1 '^EVECTION_GUNICORN_BIND=' "$ENV_FILE" | cut -d= -f2-)"
|
|
GUNICORN_BIND="${GUNICORN_BIND:-127.0.0.1:8000}"
|
|
|
|
# --- 5. Migrate + static files --------------------------------------------
|
|
|
|
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
|
|
|
|
# --- 6. systemd units -------------------------------------------------
|
|
|
|
echo "==> Installing systemd units"
|
|
for unit in evection-gunicorn evection-celery-worker evection-celery-beat; do
|
|
sed -e "s#/opt/evection#$EVECTION_HOME#g" \
|
|
-e "s/User=evection/User=$EVECTION_USER/" \
|
|
-e "s/Group=evection/Group=$EVECTION_USER/" \
|
|
"$APP_DIR/docs/deploy/${unit}.service" > "/etc/systemd/system/${unit}.service"
|
|
done
|
|
systemctl daemon-reload
|
|
systemctl enable evection-gunicorn evection-celery-worker evection-celery-beat
|
|
# Not "enable --now": that only starts a service if it's stopped, so a
|
|
# re-run after a unit file changed (e.g. a new EVECTION_GUNICORN_BIND)
|
|
# would reload the daemon but leave the already-running process on its
|
|
# stale old ExecStart indefinitely. Always restart explicitly instead.
|
|
systemctl restart evection-gunicorn evection-celery-worker evection-celery-beat
|
|
|
|
echo
|
|
echo "==> Services installed. Checking status..."
|
|
systemctl --no-pager status evection-gunicorn evection-celery-worker evection-celery-beat \
|
|
|| echo "One or more services didn't start cleanly — see 'journalctl -u evection-gunicorn' etc." >&2
|
|
|
|
echo
|
|
echo "==> Done."
|
|
echo " gunicorn is listening on $GUNICORN_BIND — point your reverse proxy at it"
|
|
echo " (see docs/install.md's \"Reverse proxy\" section) before visiting the site."
|
|
echo " Once that's in place, the setup wizard creates your admin account on first visit."
|
|
echo " To upgrade later: sudo ./scripts/upgrade.sh"
|