Skip to content

feat(deploy): blue/green backend deploys without downtime - #406

Merged
adRn-s merged 15 commits into
developfrom
feat/blue-green-deploy
Dec 1, 2026
Merged

adRn-s merged 15 commits into
developfrom
feat/blue-green-deploy

Conversation

@adRn-s

@adRn-s adRn-s commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Adds an optional, per-host blue/green swap of the Django container. The stop-everything make prod and the Updating wiki page stay valid and are the default; a host only changes after make bg-init.

Why

  • Today make prod stops everything: downtime, dropped requests.
  • Sessions are DB-backed and SECRET_KEY is shared through misc/parkour.env.ignore, so a swap logs nobody out (prod: django_session, 2996 rows, 66 live).

What

  • misc/bluegreen.yml: parkour2-django-blue/green, code baked into the image (pk2_prod target), no ./backend bind mount. Env comes from misc/parkour.env.ignore (the real file; misc/parkour.env is a template that make clean restores).
  • nginx reads its upstream from misc/active/backend.conf; X-Pk2-Color header; worker_shutdown_timeout 180s.
  • misc/bg-deploy.sh + make bg-init|bg-deploy|bg-rollback|bg-status|manage: backup (rsnapshot skipped if the container is absent), env parity check against the live color, migrate and smoke test on the idle color, flip with a 20-request check (auto-restore on failure), drain, stop the old color.
  • Migration gate: refuses schema-breaking migrations unless --compatible (zero downtime) or --breaking (maintenance window).
  • make prod refuses on a host already in blue/green mode (it would leave nginx on the old color). Going back is documented in the wiki.
  • misc/bg-nightly.sh: nightly staging refresh (git, prod dump restore excluding django_session so host logins survive, bg-deploy).
  • Works with docker compose v2 and podman (throwaway project on podman 5.8.2).

Decisions

  • Rollback does not revert migrations: releases must be additive (expand, then contract).
  • Wiki: new page Blue-Green-Deployment; Updating is untouched.
  • Cron: docker system prune -a -f --volumes deletes the stopped (rollback) color; replaced by docker image prune -a -f on parkour, parkour-dev, parkour-test.

Verified

  • On parkour-dev and parkour-test: legacy to blue, then blue/green flips: login cookie survives, 0 502s in the nginx log, make bg-rollback works, env parity check, automatic restore after a failed traffic check.
  • Real migrations on parkour-test (CreateModel): planned operations listed by the gate, applied on the idle color before the flip, new model usable after; rollback to the old image with the extra table stays healthy.
  • Gate refuses RemoveField without a flag (rc=1, nothing applied, live color unchanged); --compatible applies and flips with zero downtime; --breaking stops the live color, migrates, flips (502 window measured < 1 s for two tiny migrations).
  • A release with a stale max_migration.txt fails its healthcheck, the deploy refuses to flip and the live color keeps serving.
  • In-flight request (50 s) survives the flip: HTTP 200, 0 502s.
  • Go-back recipe on parkour-test: make prod returns the legacy stack (documented dump/restore included, make prod deletes the parkour2_pgdb volume), then bg-init + bg-deploy returns to blue/green.
  • misc/bg-deploy.sh end to end under podman 5.8.2 (deploy, rollback, gate refusal, --compatible), plus the real bluegreen.yml and real nginx configs.
  • Nightly refresh on parkour-test: end to end, host sessions survive.

Not verified yet

  • Production.

Deploy note

  • Hosts must run make bg-init once: nginx now includes misc/active/backend.conf. It recreates nginx (seconds of downtime).

- misc/bluegreen.yml: parkour2-django-blue/green from one image (pk2_prod target, code baked in, no bind mount)
- nginx reads its upstream from misc/active/backend.conf; X-Pk2-Color header; worker_shutdown_timeout 180s
- misc/bg-deploy.sh + make bg-init/bg-deploy/bg-rollback/bg-status/manage: backup, migrate+smoke on idle color, flip with traffic check, drain, retire
- migration gate refuses schema-breaking migrations unless --compatible or --breaking (maintenance window)
Sessions are DB-backed and SECRET_KEY is shared, so a flip logs nobody out.
check --database alone does not connect: a container with template DB
credentials passes as healthy. Add --deploy and compare SECRET_KEY and
DATABASE_URL between the live and idle containers before migrating, so a
stale misc/parkour.env (dev hosts) fails the deploy instead of breaking
sessions on the flip.
The check docker execs into , which the old position stopped first. Now
the leftover container is only reported, and the comparison runs right
after up --force-recreate.
…d template

- bluegreen.yml env_file now points at the real, untracked parkour.env.ignore; make clean restores the tracked parkour.env template after every deploy, so containers created later got placeholder secrets
- preflight requires parkour.env.ignore; parity guard also compares READONLY_DATABASE_URL
- environment: still overrides env_file (INSTANCE_VERSION), checked on podman
- nginx -s reload only signals the master; the first request can still hit an old worker, which made the first pk2dev flip fail and auto-restore
- wait up to 30s for 200 + new X-Pk2-Color, then require 20 consecutive matches
- dev hosts don't run the rsnapshot service; the deploy should not warn on missing container
- new check-not-bluegreen runs before down: if misc/active/backend.conf points at a -blue/-green container, make prod stops with the go-back recipe
- without it make prod left nginx on the old color while the new parkour2-django ran unused
- misc/bg-nightly.sh: update code, import the production dump, restore it in one transaction, then bg-deploy
- replaces the old nightly import-pgdb + dev-migras on a staging host; refuses to run on a dirty checkout or with a dump older than 60 min
…session)

- bg-nightly.sh: pg_restore -l | grep -v django_session -> -L list, so the host's own logins survive the nightly refresh
- go-back test on pk2test showed make prod runs make down, which removes parkour2_pgdb
- guard message now says to run make save-postgres first and restore after
- settings hardcode ALLOWED_HOSTS = ["*"] and never read it from the environment, so the comparison was always empty == empty
- restore the trailing newline at the end of misc/bg-deploy.sh
@adRn-s
adRn-s marked this pull request as ready for review December 1, 2026 13:58
@adRn-s
adRn-s merged commit f80feb1 into develop Dec 1, 2026
8 checks passed
@adRn-s
adRn-s deleted the feat/blue-green-deploy branch December 1, 2026 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant