From a3472de2a43a31debbd74f150622de94d88866da Mon Sep 17 00:00:00 2001 From: Shaddi Hasan Date: Wed, 1 Jul 2026 15:17:12 -0400 Subject: [PATCH] Add the production deployment tooling A two-host layout: the web host runs nginx + the Flask backend; the data host runs Postgres 16, Redis, and the Celery worker/beat. Deploys pull CI-built images tagged by commit SHA rather than building on the hosts, so what runs in production is exactly what CI built. - deploy/: compose files for both hosts, deploy.sh (pins a checkout to a SHA and refuses a dirty tree), restore-and-migrate.sh, nginx.conf, an env template, and a README - docker_push.yml builds and pushes backend+worker images on every green main build; Dockerfile.backend now bakes the code so the image is a self-contained artifact - legacy_data_policy.py: mark migrated legacy filings filed and synthesize their service plans - remove the old build-on-host compose files and prod nginx conf --- .github/workflows/docker_push.yml | 64 +++++++++---- .gitignore | 4 +- back-end/Dockerfile.backend | 7 ++ back-end/scripts/legacy_data_policy.py | 55 +++++++++++ back-end/tests/test_legacy_data_policy.py | 37 ++++++++ deploy/README.md | 52 ++++++++++ deploy/data/docker-compose.yml | 64 +++++++++++++ deploy/deploy.sh | 37 ++++++++ deploy/env.example | 47 +++++++++ deploy/nginx.conf | 35 +++++++ deploy/restore-and-migrate.sh | 43 +++++++++ deploy/web/docker-compose.yml | 53 +++++++++++ docker-compose.build.yml | 39 -------- docker-compose.prod.server.yml | 69 -------------- docker-compose.prod.worker.yml | 29 ------ nginx.conf.prod | 110 ---------------------- 16 files changed, 479 insertions(+), 266 deletions(-) create mode 100644 back-end/scripts/legacy_data_policy.py create mode 100644 back-end/tests/test_legacy_data_policy.py create mode 100644 deploy/README.md create mode 100644 deploy/data/docker-compose.yml create mode 100755 deploy/deploy.sh create mode 100644 deploy/env.example create mode 100644 deploy/nginx.conf create mode 100755 deploy/restore-and-migrate.sh create mode 100644 deploy/web/docker-compose.yml delete mode 100644 docker-compose.build.yml delete mode 100644 docker-compose.prod.server.yml delete mode 100644 docker-compose.prod.worker.yml delete mode 100644 nginx.conf.prod diff --git a/.github/workflows/docker_push.yml b/.github/workflows/docker_push.yml index 9578cf0..e38574b 100644 --- a/.github/workflows/docker_push.yml +++ b/.github/workflows/docker_push.yml @@ -1,29 +1,57 @@ -name: Build and Push Docker Images to GitHub Registry +name: Build and push images -# on: -# push: -# branches: -# - main +# Build the backend and worker images and push them to ghcr, tagged by commit +# SHA, on every green main build (and on demand). Deploys then PULL a tagged +# image — production never builds on the host. Building an image for a commit +# that is never deployed is cheap and harmless. on: + workflow_run: + workflows: ["CI"] + types: [completed] + branches: [main] workflow_dispatch: +permissions: + contents: read + packages: write + jobs: - build-and-push: + build-push: + # Only build images for commits that passed CI. + if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }} runs-on: ubuntu-latest - + env: + SHA: ${{ github.event.workflow_run.head_sha || github.sha }} steps: - - name: Check out the repo - uses: actions/checkout@v2 + - uses: actions/checkout@v4 + with: + ref: ${{ github.event.workflow_run.head_sha || github.sha }} + + - uses: docker/setup-buildx-action@v3 - - name: Login to GitHub Docker Registry - run: echo "${{secrets.DOCKER_LOGIN_TOKEN}}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin + - uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - - name: Build Docker images using docker-compose.build.yml - run: docker-compose -f docker-compose.build.yml build + - name: backend + uses: docker/build-push-action@v6 + with: + context: ./back-end + file: ./back-end/Dockerfile.backend + push: true + tags: | + ghcr.io/spin-vt/backend:${{ env.SHA }} + ghcr.io/spin-vt/backend:latest - - name: Push Docker images to registry - run: | - docker push ghcr.io/spin-vt/worker:latest - docker push ghcr.io/spin-vt/backend:latest - docker push ghcr.io/spin-vt/frontend:latest + - name: worker + uses: docker/build-push-action@v6 + with: + context: ./back-end + file: ./back-end/Dockerfile.worker + push: true + tags: | + ghcr.io/spin-vt/worker:${{ env.SHA }} + ghcr.io/spin-vt/worker:latest diff --git a/.gitignore b/.gitignore index 7e376af..4fba123 100644 --- a/.gitignore +++ b/.gitignore @@ -25,12 +25,14 @@ front-end/Map_marker.svg:Zone.Identifier pushgateway-*/ profiling_data/ -data/ +/data/ +deploy/*/pgdata/ .env.local helpful.txt .env.* !.env.example README.md +!deploy/README.md # Real ISP data for local dev/test — never commit (sensitive FCC fabric/location data) dev-data/ diff --git a/back-end/Dockerfile.backend b/back-end/Dockerfile.backend index d88745f..1a0f26b 100644 --- a/back-end/Dockerfile.backend +++ b/back-end/Dockerfile.backend @@ -28,6 +28,13 @@ WORKDIR /app COPY pyproject.toml uv.lock ./ RUN uv sync --frozen --no-dev +# Bake the application code into the image (as Dockerfile.worker does) so a +# backend: image is a self-contained artifact: production pulls it from +# the registry; code is never built or bind-mounted on the host. The dev +# compose still bind-mounts ./back-end over /app for live reload (the venv at +# /opt/venv is outside /app, so neither path hides it). +COPY . . + COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh diff --git a/back-end/scripts/legacy_data_policy.py b/back-end/scripts/legacy_data_policy.py new file mode 100644 index 0000000..0d75143 --- /dev/null +++ b/back-end/scripts/legacy_data_policy.py @@ -0,0 +1,55 @@ +"""One-off data policy for upgrading a legacy (pre-filing-model) database, +run once after `alembic upgrade head` (see deploy/restore-and-migrate.sh). +Idempotent — safe to re-run; prints one line per decision. + +1. Upload filings whose filing window is already past are marked filed: + under the old model "filed" didn't exist, and anything from a closed + window was necessarily submitted (or abandoned) long ago. Filings in the + open or a future window stay open. +2. Service plans are synthesized for every upload filing from its files' + legacy speed columns (plan_service.synthesize_legacy_plans): one plan per + distinct tech/down/up/latency/category combination, the first per + technology marked the tech default, files attached. Export snapshots stay + plan-less — they are frozen history and resolution falls back to file + columns. +""" + +from datetime import date, datetime + +from database.models import folder +from database.sessions import Session +from services import filing_windows, plan_service + + +def main(): + session = Session() + now = date.today() + uploads = session.query(folder).filter(folder.type == "upload").order_by(folder.id).all() + for f in uploads: + w = filing_windows.window_from_deadline(f.deadline) + if f.status == "filed": + print(f"folder {f.id} ({w.label}): already filed") + elif w.due < now: + f.status = "filed" + if f.filed_at is None: + f.filed_at = datetime.combine(w.due, datetime.min.time()) + print(f"folder {f.id} ({w.label}, was due {w.due}): marked filed") + else: + print(f"folder {f.id} ({w.label}, due {w.due}): left open") + + plans = plan_service.synthesize_legacy_plans(f.id, session) + if plans: + for p in plans: + print( + f" plan: {p.name} (tech {p.tech_code}, " + f"{p.max_download}/{p.max_upload}, " + f"{'default' if p.is_default else 'extra'})" + ) + else: + print(" plans: already present") + session.commit() + session.close() + + +if __name__ == "__main__": + main() diff --git a/back-end/tests/test_legacy_data_policy.py b/back-end/tests/test_legacy_data_policy.py new file mode 100644 index 0000000..66d1a89 --- /dev/null +++ b/back-end/tests/test_legacy_data_policy.py @@ -0,0 +1,37 @@ +"""The legacy upgrade data policy (scripts/legacy_data_policy.py): filings +from past windows get marked filed, open-window filings stay open, and every +upload filing gets plans synthesized from its files' legacy columns. +Idempotent. (Plan synthesis itself is pinned in test_services_plans.)""" + +from datetime import date + +from database.models import service_plan +from scripts.legacy_data_policy import main as run_policy +from tests import conftest_helpers as H + + +def test_policy_files_past_windows_and_synthesizes_plans(db_session, monkeypatch): + s = db_session + org = H.make_org(s) + past = H.make_folder(s, org.id, name="old", deadline=date(2024, 9, 3)) + open_now = H.make_folder(s, org.id, name="current", deadline=date(2026, 9, 1)) + H.seed_coverage(s, past.id, b"x", "cov.kml", "wired", 50, maxDownloadSpeed=100) + H.seed_coverage(s, open_now.id, b"x", "cov2.kml", "wired", 50, maxDownloadSpeed=200) + s.commit() + + # The script opens its own Session; run against the test DB as-is. + run_policy() + s.expire_all() + + assert past.status == "filed" and past.filed_at is not None + assert open_now.status == "open" + plans = {p.folder_id: p for p in s.query(service_plan).all()} + assert plans[past.id].max_download == 100 and plans[past.id].is_default + assert plans[open_now.id].max_download == 200 + + # Idempotent: a second run changes nothing. + filed_at = past.filed_at + run_policy() + s.expire_all() + assert past.filed_at == filed_at + assert s.query(service_plan).count() == 2 diff --git a/deploy/README.md b/deploy/README.md new file mode 100644 index 0000000..7075727 --- /dev/null +++ b/deploy/README.md @@ -0,0 +1,52 @@ +# Deploying BDK + +Production runs on two hosts, each with its own compose file in this +directory: + +- **web/** — the public-facing box: nginx (TLS termination) and the Flask + backend under gunicorn. Stateless; everything it serves comes from the + data host. +- **data/** — the data box: PostgreSQL 16 (PostGIS-capable image, extension + off until needed), Redis (Celery broker + coordination flags), and the + Celery worker + beat, colocated with the database because the heavy + byte-flows are worker↔database. + +## The model + +CI builds the backend and worker images on every green `main` build and +pushes them to ghcr tagged by commit SHA +(`.github/workflows/docker_push.yml`). **Nothing is built on the hosts** — +they pull the tagged image. + +Each host carries a git checkout of this repository at a **pinned commit** +(for the versioned compose files / nginx.conf), owned by the deploy user and +never hand-edited (`deploy.sh` refuses a dirty tree). Deploying a version = +`deploy.sh ` on each host, which checks out the SHA, then +`docker compose pull && up -d` with `TAG` = that SHA. Rollback = the same +command with a previous SHA. The hosts need registry read access (a +`docker login ghcr.io` with a read-only token, or public packages). + +## Secrets + +Real configuration lives ONLY in a `.env` file beside each compose file +(`web/.env`, `data/.env`), created by hand from `env.example` and chmod 600. +It is never committed, never copied off the host, and rotated by editing the +file and re-running `docker compose up -d`. The database and Redis +passwords appear in both hosts' files; everything else is single-host. + +## First-time bring-up / restoring from a dump + +`restore-and-migrate.sh` builds a production database from a `pg_dump -Fc` +dump: fresh Postgres container, restore, `alembic upgrade head`, then +`legacy_data_policy.py` (marks past-window filings filed and synthesizes +service plans from legacy per-file values — idempotent, safe on every run). +A from-scratch install skips the restore and just lets the entrypoint run +migrations against the empty database. + +## Backups + +A nightly cron on the data host (see DEPLOYMENT notes; not part of compose): + + pg_dump -Fc -h localhost -U bdk bdk > /backups/bdk-$(date +%F).dump + +Keep at least 14 days and copy them off-host. diff --git a/deploy/data/docker-compose.yml b/deploy/data/docker-compose.yml new file mode 100644 index 0000000..0f81ad2 --- /dev/null +++ b/deploy/data/docker-compose.yml @@ -0,0 +1,64 @@ +# BDK production — DATA host: PostgreSQL 16 + Redis + Celery worker/beat, +# colocated (the heavy byte-flows are worker<->database). Code bind-mounted +# from this checkout (pinned by deploy.sh). Real config in ./\.env. +# +# PGDATA lives on local fast disk via ./pgdata — keep this directory on the +# NVMe volume. Resource caps come from .env (DB_CPUS etc.). +# +# docker compose --env-file .env up -d + +services: + db: + # PostGIS-capable image, extension NOT created — enabling it later is a + # one-line migration instead of an image swap. + image: postgis/postgis:16-3.4 + restart: unless-stopped + cpus: ${DB_CPUS} + mem_limit: ${DB_MEM_LIMIT} + environment: + - POSTGRES_USER=${POSTGRES_USER} + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} + - POSTGRES_DB=${POSTGRES_DB} + volumes: + - ./pgdata:/var/lib/postgresql/data + ports: + - "5432:5432" + + redis: + image: redis:7 + restart: unless-stopped + command: redis-server --requirepass ${REDIS_PASSWORD} + ports: + - "6379:6379" + + worker: + image: ghcr.io/spin-vt/worker:${TAG} + command: celery -A controllers.celery_controller.celery_config.celery worker -B --loglevel=INFO + restart: unless-stopped + cpus: ${WORKER_CPUS} + mem_limit: ${WORKER_MEM_LIMIT} + depends_on: + - db + - redis + environment: + - POSTGRES_USER=${POSTGRES_USER} + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} + - POSTGRES_DB=${POSTGRES_DB} + - DB_HOST=db + - DB_PORT=5432 + - PYTHONPATH=/app + - SECRET_KEY=${SECRET_KEY} + - JWT_SECRET=${JWT_SECRET} + - JWT_TOKEN_LOCATION=${JWT_TOKEN_LOCATION} + - JWT_ACCESS_COOKIE_NAME=${JWT_ACCESS_COOKIE_NAME} + - IN_PRODUCTION=${IN_PRODUCTION} + - PUBLIC_BASE_URL=${PUBLIC_BASE_URL} + - MAIL_SERVER=${MAIL_SERVER} + - MAIL_PORT=${MAIL_PORT} + - MAIL_USE_TLS=${MAIL_USE_TLS} + - MAIL_USE_SSL=${MAIL_USE_SSL} + - MAIL_USERNAME=${MAIL_USERNAME} + - MAIL_PASSWORD=${MAIL_PASSWORD} + - MAIL_DEFAULT_SENDER=${MAIL_DEFAULT_SENDER} + - CELERY_BROKER_URL=redis://:${REDIS_PASSWORD}@redis:6379/0 + - CELERY_RESULT_BACKEND=redis://:${REDIS_PASSWORD}@redis:6379/0 diff --git a/deploy/deploy.sh b/deploy/deploy.sh new file mode 100755 index 0000000..924ea34 --- /dev/null +++ b/deploy/deploy.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# Deploy BDK on this host by pulling the CI-built images for a commit and +# restarting — nothing is built on the host. Images are built and pushed to +# ghcr by .github/workflows/docker_push.yml on every green main build, tagged +# by commit SHA. +# +# ./deploy.sh web +# ./deploy.sh data +# +# Rollback = the same command with a previous sha. The checkout is pinned to +# that sha too (so the compose files / nginx.conf match the images) and must +# be clean — production is never hand-edited. +set -eu + +ROLE=${1:?usage: deploy.sh } +SHA=${2:?usage: deploy.sh } +DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +REPO=$(dirname "$DIR") + +[ -f "$DIR/$ROLE/.env" ] || { echo "missing $DIR/$ROLE/.env (copy from env.example)"; exit 1; } + +cd "$REPO" +if [ -n "$(git status --porcelain)" ]; then + echo "checkout is dirty — production is never hand-edited. Aborting." + git status --short + exit 1 +fi +git fetch --all --quiet +git checkout --quiet "$SHA" +TAG=$(git rev-parse HEAD) +export TAG +echo "deploying $ROLE at $(git rev-parse --short HEAD) (image tag $TAG)" + +cd "$DIR/$ROLE" +docker compose --env-file .env pull +docker compose --env-file .env up -d +docker compose --env-file .env ps diff --git a/deploy/env.example b/deploy/env.example new file mode 100644 index 0000000..7a3ea59 --- /dev/null +++ b/deploy/env.example @@ -0,0 +1,47 @@ +# BDK production configuration template. Copy to web/.env and data/.env on +# the respective hosts, fill in real values, chmod 600. Never commit a +# filled-in copy. Rotate by editing the file and `docker compose up -d`. + +# --- database (both hosts; the data host runs it, the web host dials in) --- +POSTGRES_USER=bdk +POSTGRES_PASSWORD=__GENERATE_32_RANDOM_CHARS__ +POSTGRES_DB=bdk +DB_HOST=__DATA_HOST_IP__ # on the data host: db (the compose service) +DB_PORT=5432 + +# --- redis / celery (both hosts) ------------------------------------------- +REDIS_PASSWORD=__GENERATE_32_RANDOM_CHARS__ +CELERY_BROKER_URL=redis://:${REDIS_PASSWORD}@__DATA_HOST_IP__:6379/0 +CELERY_RESULT_BACKEND=redis://:${REDIS_PASSWORD}@__DATA_HOST_IP__:6379/0 + +# --- app secrets (web + data hosts; JWT must match everywhere) -------------- +SECRET_KEY=__GENERATE_32_RANDOM_CHARS__ +JWT_SECRET=__GENERATE_AT_LEAST_32_RANDOM_CHARS__ +JWT_TOKEN_LOCATION=cookies +JWT_ACCESS_COOKIE_NAME=token +IN_PRODUCTION=1 + +# --- web host only ----------------------------------------------------------- +SERVER_NAME=__PUBLIC_HOSTNAME__ +PUBLIC_BASE_URL=https://__PUBLIC_HOSTNAME__ +NGINX_HTTP_PORT=80 +NGINX_HTTPS_PORT=443 +SSL_CERT_PATH=__/path/to/cert.crt__ +SSL_KEY_PATH=__/path/to/private_key.pem__ +DEVELOP_BACKEND_PORT=8000 +MAPTILE_STREET=https://api.maptiler.com/maps/streets/style.json?key=__MAPTILER_KEY__ + +# --- mail (web + data hosts; workers send too) ------------------------------- +MAIL_SERVER=__SMTP_HOST__ +MAIL_PORT=587 +MAIL_USE_TLS=true +MAIL_USE_SSL=false +MAIL_USERNAME=__SMTP_USER__ +MAIL_PASSWORD=__SMTP_PASSWORD__ +MAIL_DEFAULT_SENDER=__FROM_ADDRESS__ + +# --- data host resource caps (tune to the box) ------------------------------- +DB_CPUS=24 +DB_MEM_LIMIT=32g +WORKER_CPUS=16 +WORKER_MEM_LIMIT=24g diff --git a/deploy/nginx.conf b/deploy/nginx.conf new file mode 100644 index 0000000..b48e1ae --- /dev/null +++ b/deploy/nginx.conf @@ -0,0 +1,35 @@ +# BDK production nginx (templated by the official image: ${SERVER_NAME}). +# Every path is served by the backend — the server-rendered app, /api, the +# admin panel, auth pages, and static assets all live there. + +server { + listen 80; + server_name ${SERVER_NAME}; + return 301 https://$host$request_uri; +} + +server { + listen 443 ssl; + server_name ${SERVER_NAME}; + + ssl_certificate /etc/nginx/ssl/server.crt; + ssl_certificate_key /etc/nginx/ssl/server.key; + ssl_protocols TLSv1.2 TLSv1.3; + + client_max_body_size 500M; + gzip on; + gzip_types text/css application/javascript application/json application/vnd.mapbox-vector-tile; + + location / { + proxy_pass http://backend:8000; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_http_version 1.1; + proxy_buffering off; # SSE (job pill / map finalize streams) + proxy_read_timeout 300s; + proxy_send_timeout 300s; + proxy_connect_timeout 60s; + } +} diff --git a/deploy/restore-and-migrate.sh b/deploy/restore-and-migrate.sh new file mode 100755 index 0000000..808c9f9 --- /dev/null +++ b/deploy/restore-and-migrate.sh @@ -0,0 +1,43 @@ +#!/bin/sh +# One-off bring-up of the production database from a pg_dump -Fc dump, run on +# the DATA host after `docker compose up -d db` and before starting anything +# else. Restores into the running db container, runs migrations, then the +# legacy data policy. Idempotent up to the restore (refuses a non-empty DB). +# +# ./restore-and-migrate.sh /path/to/dump.dump +set -eu + +DUMP=${1:?usage: restore-and-migrate.sh } +DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +. "$DIR/data/.env" 2>/dev/null || true +DB=${POSTGRES_DB:?data/.env must define POSTGRES_DB} +# The worker image (used for migrations + the data policy) is tagged by the +# checked-out commit, matching deploy.sh. +TAG=$(git -C "$DIR" rev-parse HEAD) +export TAG + +cd "$DIR/data" +DBC=$(docker compose --env-file .env ps -q db) +[ -n "$DBC" ] || { echo "db container not running (docker compose up -d db first)"; exit 1; } + +TABLES=$(docker exec "$DBC" psql -U "$POSTGRES_USER" -d "$DB" -tAc \ + "SELECT count(*) FROM pg_tables WHERE schemaname='public'") +[ "$TABLES" = "0" ] || { echo "database is not empty ($TABLES tables) — refusing to restore"; exit 1; } + +echo "== restore (postgis/topology/spatial_ref_sys filtered: the extension is + unused; enabling PostGIS later is a one-line migration) ==" +docker cp "$DUMP" "$DBC":/tmp/restore.dump +docker exec "$DBC" sh -c " + pg_restore -l /tmp/restore.dump | grep -viE 'postgis|topology|spatial_ref_sys' > /tmp/toc.list && + pg_restore -U $POSTGRES_USER -d $DB -j 4 -L /tmp/toc.list /tmp/restore.dump && + rm /tmp/restore.dump" + +echo "== migrations ==" +docker compose --env-file .env run --rm --no-deps \ + --entrypoint sh worker -c "cd /app && alembic upgrade head" + +echo "== legacy data policy (filed status + plan synthesis; idempotent) ==" +docker compose --env-file .env run --rm --no-deps \ + --entrypoint python3 worker /app/scripts/legacy_data_policy.py + +echo "== done — start the rest: docker compose --env-file .env up -d ==" diff --git a/deploy/web/docker-compose.yml b/deploy/web/docker-compose.yml new file mode 100644 index 0000000..aa59e99 --- /dev/null +++ b/deploy/web/docker-compose.yml @@ -0,0 +1,53 @@ +# BDK production — WEB host: nginx (TLS) + the backend under gunicorn. +# The backend image is pulled from ghcr, tagged by commit SHA (${TAG}, set by +# deploy.sh — never built on the host). Real config in ./\.env (from +# ../env.example), chmod 600. +# +# TAG= docker compose --env-file .env up -d (deploy.sh wraps this) + +services: + backend: + image: ghcr.io/spin-vt/backend:${TAG} + command: gunicorn routes:app -b 0.0.0.0:8000 -w 4 --threads 8 --timeout 120 --log-level info + restart: unless-stopped + environment: + - POSTGRES_USER=${POSTGRES_USER} + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} + - POSTGRES_DB=${POSTGRES_DB} + - DB_HOST=${DB_HOST} + - DB_PORT=${DB_PORT} + - SECRET_KEY=${SECRET_KEY} + - JWT_SECRET=${JWT_SECRET} + - JWT_TOKEN_LOCATION=${JWT_TOKEN_LOCATION} + - JWT_ACCESS_COOKIE_NAME=${JWT_ACCESS_COOKIE_NAME} + - IN_PRODUCTION=${IN_PRODUCTION} + - SERVER_NAME=${SERVER_NAME} + - PUBLIC_BASE_URL=${PUBLIC_BASE_URL} + - MAIL_SERVER=${MAIL_SERVER} + - MAIL_PORT=${MAIL_PORT} + - MAIL_USE_TLS=${MAIL_USE_TLS} + - MAIL_USE_SSL=${MAIL_USE_SSL} + - MAIL_USERNAME=${MAIL_USERNAME} + - MAIL_PASSWORD=${MAIL_PASSWORD} + - MAIL_DEFAULT_SENDER=${MAIL_DEFAULT_SENDER} + - CELERY_BROKER_URL=${CELERY_BROKER_URL} + - CELERY_RESULT_BACKEND=${CELERY_RESULT_BACKEND} + - DEVELOP_BACKEND_PORT=${DEVELOP_BACKEND_PORT} + - MAPTILE_STREET=${MAPTILE_STREET} + expose: + - "8000" + + nginx: + image: nginx:1.27-alpine + restart: unless-stopped + depends_on: + - backend + ports: + - "${NGINX_HTTP_PORT}:80" + - "${NGINX_HTTPS_PORT}:443" + volumes: + - ../nginx.conf:/etc/nginx/templates/default.conf.template:ro + - ${SSL_CERT_PATH}:/etc/nginx/ssl/server.crt:ro + - ${SSL_KEY_PATH}:/etc/nginx/ssl/server.key:ro + environment: + - SERVER_NAME=${SERVER_NAME} diff --git a/docker-compose.build.yml b/docker-compose.build.yml deleted file mode 100644 index c0c9afd..0000000 --- a/docker-compose.build.yml +++ /dev/null @@ -1,39 +0,0 @@ -services: - work: - image: ghcr.io/spin-vt/backend - build: - context: ./back-end - dockerfile: Dockerfile.worker - command: celery -A controllers.celery_controller.celery_config.celery worker -B -s /tmp/celerybeat-schedule --loglevel=DEBUG - environment: - - PYTHONPATH=/app - volumes: - - ./back-end:/app - - - backend: - image: ghcr.io/spin-vt/worker - build: - context: ./back-end - dockerfile: Dockerfile.backend - command: gunicorn routes:app -b 0.0.0.0:8000 -w 4 --threads 8 --log-level debug --reload - volumes: - - ./back-end:/app - expose: - - 8000 - - frontend: - image: ghcr.io/spin-vt/frontend - build: ./front-end - volumes: - - ./front-end:/app - expose: - - 3000 - - nginx: - image: ghcr.io/spin-vt/my-nginx - build: - context: ./ - dockerfile: Dockerfile.nginx - container_name: nginx-proxy - diff --git a/docker-compose.prod.server.yml b/docker-compose.prod.server.yml deleted file mode 100644 index 8f8ed0a..0000000 --- a/docker-compose.prod.server.yml +++ /dev/null @@ -1,69 +0,0 @@ -services: - - - backend: - image: ${DOCKER_IMAGE_BACKEND} - network_mode: host - build: - context: ./back-end - dockerfile: Dockerfile.backend - command: gunicorn routes:app -b 0.0.0.0:8000 -w 4 --threads 8 --log-level debug --reload - environment: - - POSTGRES_USER=${POSTGRES_USER} - - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} - - POSTGRES_DB=${POSTGRES_DB} - - DB_HOST=${DB_HOST} - - DB_PORT=${DB_PORT} - - SECRET_KEY=${SECRET_KEY} - - JWT_SECRET=${JWT_SECRET} - - JWT_TOKEN_LOCATION=${JWT_TOKEN_LOCATION} - - JWT_ACCESS_COOKIE_NAME=${JWT_ACCESS_COOKIE_NAME} - - MAIL_SERVER=${MAIL_SERVER} - - MAIL_PORT=${MAIL_PORT} - - MAIL_USE_TLS=${MAIL_USE_TLS} - - MAIL_USE_SSL=${MAIL_USE_SSL} - - MAIL_USERNAME=${MAIL_USERNAME} - - MAIL_PASSWORD=${MAIL_PASSWORD} - - MAIL_DEFAULT_SENDER=${MAIL_DEFAULT_SENDER} - - DEVELOP_BACKEND_PORT=${DEVELOP_BACKEND_PORT} - - CELERY_BROKER_URL=${CELERY_BROKER_URL} - - CELERY_RESULT_BACKEND=${CELERY_RESULT_BACKEND} - - IN_PRODUCTION=1 - volumes: - - ./back-end:/app - expose: - - ${DEVELOP_BACKEND_PORT} - - frontend: - image: ${DOCKER_IMAGE_FRONTEND} - network_mode: host - build: ./front-end - volumes: - - ./front-end:/app - depends_on: - - backend - expose: - - ${DEVELOP_FRONTEND_PORT} - - - nginx: - image: ${DOCKER_IMAGE_NGINX} - network_mode: host - build: - context: ./ - dockerfile: Dockerfile.nginx - container_name: nginx-proxy - environment: - - SSL_PASSWORD_FILE=/ssl/${SSL_PASSPHRASE_FILE} - # ports: - # - "${NGINX_HTTP_PORT}:80" - # - "${NGINX_HTTPS_PORT}:443" - volumes: - - ${NGINX_CONF_PROD}:/etc/nginx/conf.d/default.conf - - ${SSL_PATH}/${SSL_CERT}:/etc/nginx/ssl/${SSL_CERT} - - ${SSL_PATH}/${SSL_KEY}:/etc/nginx/ssl/${SSL_KEY} - - ${SSL_PATH}/${SSL_PASSPHRASE_FILE}:/ssl/${SSL_PASSPHRASE_FILE} - depends_on: - - backend - - frontend - diff --git a/docker-compose.prod.worker.yml b/docker-compose.prod.worker.yml deleted file mode 100644 index 414b123..0000000 --- a/docker-compose.prod.worker.yml +++ /dev/null @@ -1,29 +0,0 @@ -services: - work: - image: ${DOCKER_IMAGE_WORKER} - network_mode: host - build: - context: ./back-end - dockerfile: Dockerfile.worker - command: celery -A controllers.celery_controller.celery_config.celery worker -B -s /tmp/celerybeat-schedule --loglevel=DEBUG - environment: - - PYTHONPATH=/app - - POSTGRES_USER=${POSTGRES_USER} - - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} - - POSTGRES_DB=${POSTGRES_DB} - - DB_HOST=${DB_HOST} - - DB_PORT=${DB_PORT} - - SECRET_KEY=${SECRET_KEY} - - JWT_SECRET=${JWT_SECRET} - - JWT_TOKEN_LOCATION=${JWT_TOKEN_LOCATION} - - JWT_ACCESS_COOKIE_NAME=${JWT_ACCESS_COOKIE_NAME} - - MAIL_SERVER=${MAIL_SERVER} - - MAIL_PORT=${MAIL_PORT} - - MAIL_USE_TLS=${MAIL_USE_TLS} - - MAIL_USE_SSL=${MAIL_USE_SSL} - - MAIL_USERNAME=${MAIL_USERNAME} - - MAIL_PASSWORD=${MAIL_PASSWORD} - - CELERY_BROKER_URL=${CELERY_BROKER_URL} - - CELERY_RESULT_BACKEND=${CELERY_RESULT_BACKEND} - volumes: - - ./back-end:/app diff --git a/nginx.conf.prod b/nginx.conf.prod deleted file mode 100644 index 2ed4b14..0000000 --- a/nginx.conf.prod +++ /dev/null @@ -1,110 +0,0 @@ -# Redirect HTTP traffic to HTTPS -server { - listen 80; - server_name bdk.cs.vt.edu; - location / { - return 301 https://$host$request_uri; - } -} - -# HTTPS configuration -server { - listen 443 ssl; - server_name bdk.cs.vt.edu; - - # SSL certificate and key paths - ssl_certificate /etc/nginx/ssl/bdk.cs.vt.edu.crt; - ssl_certificate_key /etc/nginx/ssl/private_key.pem; - - # Recommended SSL settings (you might want to adjust this depending on your needs) - ssl_protocols TLSv1.2 TLSv1.3; - ssl_prefer_server_ciphers on; - ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA'; - - # Set the maximum allowed size for client request bodies (e.g., 100M for 100 megabytes) - client_max_body_size 500M; - - # Enabling gzip compression for better performance. - gzip on; - gzip_disable "msie6"; - gzip_vary on; - gzip_proxied any; - gzip_comp_level 6; - gzip_buffers 16 8k; - gzip_http_version 1.1; - gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; - - # Redirect all traffic to the backend service on port 8000 - location /api/ { - proxy_pass http://localhost:8000; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection 'upgrade'; - proxy_cache_bypass $http_upgrade; - proxy_buffering off; - proxy_redirect off; - proxy_buffers 8 16k; - proxy_buffer_size 16k; - proxy_busy_buffers_size 24k; - } - - # Server-rendered app pages (the redesigned UI), brought up page by page - # beside the SPA. These paths are new (the SPA doesn't own them), so they - # all route to the backend already; a page goes live when the backend - # replaces its placeholder — no nginx change per page. - location ~ ^/(org|files|map|submissions|setup|filing|assets|auth)(/|$) { - proxy_pass http://localhost:8000; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_buffers 8 16k; - proxy_buffer_size 16k; - proxy_busy_buffers_size 24k; - } - - # Server-rendered admin panel (Flask + htmx). Covers /admin/, /admin/api/, - # and /admin/static/. Only reachable over HTTPS (the admin_session cookie is - # Secure in production). Longest-prefix match wins over `location /`. - location = /admin { return 301 https://$host/admin/; } - location /admin/ { - proxy_pass http://localhost:8000; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_buffers 8 16k; - proxy_buffer_size 16k; - proxy_busy_buffers_size 24k; - } - - # Frontend route for everything else - location / { - proxy_pass http://localhost:3000; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection 'upgrade'; - proxy_cache_bypass $http_upgrade; - - # Settings for WebSockets (assuming you might be using it in your frontend) - proxy_buffering off; - proxy_redirect off; - - # To allow POST on static pages - error_page 418 = @backend; - - # Reduce buffering for the frontend, similar to the backend. - proxy_buffers 8 16k; - proxy_buffer_size 16k; - proxy_busy_buffers_size 24k; - } - - # Fallback for POST requests on static pages - location @backend { - proxy_pass http://localhost:3000; - } -}