Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@ IMAGE := okfn
NAME := okfn
PORT := 8888

# Local Postgres 13 cluster — used by `make run-pg` and `make db-shell`.
# Override on the command line if you put the cluster on a different port,
# e.g. `make run-pg DB_HOST_PORT=5435`.
DB_HOST_PORT ?= 5434
DB_NAME ?= okfn
DB_USER ?= hermes
DB_PASSWORD ?= devpass

# Detect if the container is currently running.
RUNNING := $(shell docker ps --filter name=^/$(NAME)$$ --format '{{.Names}}' 2>/dev/null)

.PHONY: help build run stop restart bash logs test check lint shell migrate css deps-compile clean
.PHONY: help build run run-pg stop restart bash logs test check lint shell migrate css deps-compile db-shell fixtures-dump fixtures-load clean

# Tunable: number of CMS pages (treenodes) to include in the sample fixture.
MAX_PAGES ?= 100

help:
@echo "Targets:"
@echo " make build Build the Docker image ($(IMAGE))."
@echo " make run Start the container detached on port $(PORT)."
@echo " make run Start the container with the SQLite fallback DB."
@echo " make run-pg Start the container against the local PG 13 cluster ($(DB_NAME)@:$(DB_HOST_PORT))."
@echo " make stop Stop the running container."
@echo " make restart Stop + run."
@echo " make bash Open an interactive shell inside the container."
Expand All @@ -25,6 +37,9 @@ help:
@echo " make migrate Apply pending migrations."
@echo " make css Compile Tailwind/PostCSS to static/css/styles.css."
@echo " make deps-compile Recompile requirements.txt + requirements.dev.txt from .in files."
@echo " make db-shell Open psql against the local PG 13 cluster."
@echo " make fixtures-dump Regenerate foundation/tests/fixtures/sample_data.json from the live DB (MAX_PAGES=$(MAX_PAGES))."
@echo " make fixtures-load Load the sample fixture into the container's DB."
@echo " make clean Stop the container and prune dangling images."

build:
Expand All @@ -34,6 +49,33 @@ run:
docker run -d --rm --name $(NAME) -p $(PORT):80 $(IMAGE)
@echo "Container '$(NAME)' running on http://localhost:$(PORT)"

# Run the container against the host's local PG 13 cluster. Requires:
# 1. PG 13 cluster online (see `pg_lsclusters`).
# 2. Role $(DB_USER) has a TCP password set; pg_hba.conf accepts the
# Docker bridge network (172.16.0.0/12) for that role.
# `--add-host host.docker.internal:host-gateway` makes the host reachable
# from inside the container by that hostname (Linux Docker convention).
# We write the DB settings to .env.docker-pg and mount it as /app/.env
# inside the container; settings.py reads `.env` *after* `.env.base`, so
# these values override the SQLite defaults. Plain `-e DB_HOST=...` flags
# don't work because settings.py reads `.env.base` with overwrite=True,
# clobbering the container env.
run-pg:
@printf '%s\n' \
'DB_ENGINE=django.db.backends.postgresql_psycopg2' \
'DB_HOST=host.docker.internal' \
'DB_PORT=$(DB_HOST_PORT)' \
'DB_NAME=$(DB_NAME)' \
'DB_USER=$(DB_USER)' \
'DB_PASSWORD=$(DB_PASSWORD)' \
> .env.docker-pg
docker run -d --rm --name $(NAME) \
-p $(PORT):80 \
--add-host host.docker.internal:host-gateway \
-v $(PWD)/.env.docker-pg:/app/.env:ro \
$(IMAGE)
@echo "Container '$(NAME)' running on http://localhost:$(PORT) against PG 13 ($(DB_NAME)@:$(DB_HOST_PORT))"

stop:
-docker stop $(NAME)

Expand Down Expand Up @@ -88,5 +130,22 @@ deps-compile:
uv pip compile requirements.in -o requirements.txt
uv pip compile requirements.dev.in -o requirements.dev.txt

# Quick psql shell against the local PG 13 cluster (uses peer auth — no password).
db-shell:
psql -p $(DB_HOST_PORT) -d $(DB_NAME)

# Regenerate the sample fixture from the running container's DB. Requires
# `make run-pg` first (the container must be connected to a populated DB).
# Tune size with `make fixtures-dump MAX_PAGES=50` for a smaller slice.
fixtures-dump:
docker exec $(NAME) python manage.py dump_sample --max-pages $(MAX_PAGES) --out /tmp/sample_data.json
docker cp $(NAME):/tmp/sample_data.json foundation/tests/fixtures/sample_data.json
@echo "Wrote foundation/tests/fixtures/sample_data.json"

# Load the sample fixture into the running container's DB. Useful for
# seeding a fresh `make run` (SQLite) container with realistic content.
fixtures-load:
docker exec $(NAME) python manage.py loaddata /app/foundation/tests/fixtures/sample_data.json

clean: stop
-docker image prune -f
Empty file.
Empty file.
Loading
Loading