#!/bin/sh set -eu umask 077 export FAM_RUNTIME_DIR="${FAM_RUNTIME_DIR:-/opt/fam}" export FAM_DATA_DIR="${FAM_DATA_DIR:-/data}" export GIT_REPO_URL="${GIT_REPO_URL:-https://git.casaderoll.de/michael/Fil-A-Management.git}" export GIT_REVISION="${GIT_REVISION:-main}" export GITEA_TOKEN_FILE="${GITEA_TOKEN_FILE:-/run/secrets/gitea_token}" export GITEA_SSH_KEY_FILE="${GITEA_SSH_KEY_FILE:-/run/secrets/gitea_ssh_key}" export GITEA_KNOWN_HOSTS_FILE="${GITEA_KNOWN_HOSTS_FILE:-/run/secrets/gitea_known_hosts}" # Fail before touching files; credentials must never be embedded in a remote URL. python3 - <<'PY' import os import sys from pathlib import Path from urllib.parse import urlsplit try: url = urlsplit(os.environ["GIT_REPO_URL"]) local = url.scheme == "file" and not url.netloc and os.environ.get("FAM_ALLOW_LOCAL_GIT") == "1" if not local and (url.scheme not in {"https", "ssh"} or not url.hostname): raise ValueError("GIT_REPO_URL muss eine HTTPS- oder SSH-URL sein.") if url.password or (url.username and url.scheme != "ssh") or url.query or url.fragment: raise ValueError("Zugangsdaten in URLs sind verboten. Bitte Secret-Dateien verwenden.") if url.scheme == "ssh" and url.username not in {None, "git"}: raise ValueError("SSH-Zugriff verwendet den technischen Benutzer git.") revision = os.environ["GIT_REVISION"] if not revision or revision.startswith("-") or any(c.isspace() for c in revision): raise ValueError("Ungültige Git-Revision.") runtime = Path(os.environ["FAM_RUNTIME_DIR"]).resolve() data = Path(os.environ["FAM_DATA_DIR"]).resolve() if runtime.is_relative_to(data) or data.is_relative_to(runtime): raise ValueError("Daten- und Programmverzeichnis müssen vollständig getrennt sein.") except ValueError as error: sys.exit(str(error)) PY # The image starts as root only to initialize a fresh named volume and read mounts. # Git, pip, migrations and the web server always run as the unprivileged app user. if [ "$(id -u)" = "0" ]; then mkdir -p "$FAM_RUNTIME_DIR/credentials" "$FAM_DATA_DIR" chmod 700 "$FAM_DATA_DIR" "$FAM_RUNTIME_DIR/credentials" chown fam:fam "$FAM_RUNTIME_DIR" "$FAM_RUNTIME_DIR/credentials" "$FAM_DATA_DIR" copy_secret() { if [ -f "$1" ]; then cp "$1" "$FAM_RUNTIME_DIR/credentials/$2" chmod 600 "$FAM_RUNTIME_DIR/credentials/$2" chown fam:fam "$FAM_RUNTIME_DIR/credentials/$2" else rm -f "$FAM_RUNTIME_DIR/credentials/$2" fi } copy_secret "$GITEA_TOKEN_FILE" token copy_secret "$GITEA_SSH_KEY_FILE" ssh-key copy_secret "$GITEA_KNOWN_HOSTS_FILE" known-hosts export GITEA_TOKEN_FILE="$FAM_RUNTIME_DIR/credentials/token" export GITEA_SSH_KEY_FILE="$FAM_RUNTIME_DIR/credentials/ssh-key" export GITEA_KNOWN_HOSTS_FILE="$FAM_RUNTIME_DIR/credentials/known-hosts" exec su-exec fam "$0" "$@" fi # Test-only file URLs must become local paths: Git treats file:// as an # upload-pack repository transport and cannot read a bundle through that URL. if [ "${FAM_ALLOW_LOCAL_GIT:-0}" = "1" ]; then GIT_REPO_URL=$(python3 -c 'import os; from urllib.parse import urlsplit; from urllib.request import url2pathname; value=os.environ["GIT_REPO_URL"]; parsed=urlsplit(value); print(url2pathname(parsed.path) if parsed.scheme == "file" else value)') export GIT_REPO_URL fi mkdir -p "$FAM_RUNTIME_DIR/app" "$FAM_DATA_DIR" export HOME="$FAM_RUNTIME_DIR" export GIT_TERMINAL_PROMPT=0 GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null export GIT_ASKPASS="${FAM_GIT_HELPERS:-/usr/local/bin}/fam-git-askpass" export GIT_SSH="${FAM_GIT_HELPERS:-/usr/local/bin}/fam-git-ssh" export GIT_SSH_VARIANT=ssh if [ ! -d "$FAM_RUNTIME_DIR/app/.git" ]; then git init --quiet "$FAM_RUNTIME_DIR/app" git -C "$FAM_RUNTIME_DIR/app" remote add origin "$GIT_REPO_URL" else git -C "$FAM_RUNTIME_DIR/app" remote set-url origin "$GIT_REPO_URL" fi cd "$FAM_RUNTIME_DIR/app" git -c credential.helper= fetch --quiet --force --prune origin '+refs/heads/*:refs/remotes/origin/*' '+refs/tags/*:refs/tags/*' case "$GIT_REVISION" in refs/heads/*) target="refs/remotes/origin/${GIT_REVISION#refs/heads/}" ;; refs/tags/*) target="$GIT_REVISION" ;; *) if git show-ref --verify --quiet "refs/remotes/origin/$GIT_REVISION"; then target="refs/remotes/origin/$GIT_REVISION" elif git show-ref --verify --quiet "refs/tags/$GIT_REVISION"; then target="refs/tags/$GIT_REVISION" else python3 -c 'import os,re,sys; sys.exit(0 if re.fullmatch(r"[0-9a-fA-F]{40}",os.environ["GIT_REVISION"]) else "Git-Revision nicht gefunden.")' target="$GIT_REVISION" fi ;; esac commit=$(git rev-parse --verify "$target^{commit}") git checkout --quiet --detach --force "$commit" # Only the disposable runtime clone is cleaned. /data can never be inside it. git clean -fdx --quiet printf 'Fil-A-Management: starte Revision %s\n' "$commit" python3 -m venv --clear "$FAM_RUNTIME_DIR/venv" export PATH="$FAM_RUNTIME_DIR/venv/bin:$PATH" python -m pip --isolated install --disable-pip-version-check --no-cache-dir --require-hashes -r requirements.txt python - <<'PY' import os import secrets from pathlib import Path path = Path(os.environ["FAM_DATA_DIR"]) / "secret.key" if not os.environ.get("DJANGO_SECRET_KEY") and not os.environ.get("DJANGO_SECRET_KEY_FILE"): try: fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) except FileExistsError: pass else: with os.fdopen(fd, "w") as handle: handle.write(secrets.token_urlsafe(64)) PY python manage.py check python manage.py migrate --noinput if [ "${SEED_CATALOG:-0}" = "1" ]; then python manage.py seed_catalog fi python manage.py collectstatic --noinput --verbosity 0 if [ "$#" -gt 0 ]; then exec "$@" fi exec gunicorn config.wsgi:application --bind "${FAM_BIND:-0.0.0.0:8000}" \ --workers 1 --worker-class gthread --threads 2 --timeout 60 \ --forwarded-allow-ips='' --no-control-socket --access-logfile - --error-logfile -