Files
lincodex/deploy/Makefile
T

173 lines
13 KiB
Makefile

.DEFAULT_GOAL := help
ROOT := $(abspath ..)
DOCKER ?= docker
APP_PROJECT ?= app
ML_PROJECT ?= ml
MONITORING_PROJECT ?= monitoring
APP_NETWORK ?= $(APP_PROJECT)_default
ENV_FILE ?= $(ROOT)/.env
CHECK_ENV_FILE := $(ROOT)/.env.example
# The example file has no usable IDENTITY_KEY_SECRET, which Compose requires.
# Commands that render or build from it get this value in their own
# environment only, never in .env targets, where it would override the real one.
CHECK_IDENTITY_KEY_SECRET := render-only-identity-key-secret-not-for-deployment
CORE_FILE := $(ROOT)/deploy/docker/docker-compose.yml
ML_FILE := $(ROOT)/deploy/docker/docker-compose.ml.yml
MONITORING_FILE := $(ROOT)/deploy/docker/docker-compose.monitoring.yml
E2E_FILE := $(ROOT)/deploy/docker/docker-compose.e2e.yml
E2E_MAIL_FILE := $(ROOT)/deploy/docker/docker-compose.e2e-mail.yml
RELEASE_FILE := $(ROOT)/deploy/docker/docker-compose.release.yml
ML_RELEASE_FILE := $(ROOT)/deploy/docker/docker-compose.ml.release.yml
RELEASE_SCRIPT := $(ROOT)/deploy/scripts/release_manifest.py
RELEASE := PYTHONDONTWRITEBYTECODE=1 python3 "$(RELEASE_SCRIPT)"
# Pull policy of release-build for base and service images: missing or always.
RELEASE_PULL ?= missing
# Compose project whose running containers release-manifest-verify compares.
RELEASE_VERIFY_PROJECT ?= $(APP_PROJECT)
# Optional manifest path for release-manifest-check, verify, and restore.
# Empty selects RELEASE_ID when set, otherwise the current commit-based ID.
RELEASE_MANIFEST ?=
# Optional immutable delivery ID; empty keeps the current commit-based ID.
RELEASE_ID ?=
# Optional repository-relative manifest describing bank, policy, and seed inputs.
RELEASE_DATA_MANIFEST ?=
RELEASE_MANIFEST_ARG = $(if $(strip $(RELEASE_MANIFEST)),--manifest "$(RELEASE_MANIFEST)",$(if $(strip $(RELEASE_ID)),--manifest "deploy/release/$(RELEASE_ID)/manifest.json",))
# Recursive on purpose: git is asked only by the targets that use the tag. The
# project-specific name keeps a generic RELEASE_TAG from enabling release mode.
LINCODEX_RELEASE_TAG ?= $(shell PYTHONDONTWRITEBYTECODE=1 python3 "$(RELEASE_SCRIPT)" tag)
PLAYWRIGHT_VERSION := $(shell sed -n 's/^ *"@playwright\/test": "\([^"]*\)",\{0,1\}$$/\1/p' "$(ROOT)/frontend/package.json")
.PHONY: help config build up down logs monitoring-config monitoring-up monitoring-down monitoring-logs ml-config ml-build ml-up ml-down ml-logs check check-workflows check-release check-perimeter check-e2e-mail perimeter-test smoke test-e2e test-e2e-registration release-build release-smoke release-e2e release-manifest release-manifest-verify release-manifest-check release-up release-down release-clean
help: ## Show deployment commands
@awk 'BEGIN {FS = ":.*## "} /^[a-zA-Z0-9_-]+:.*## / {printf "%-24s %s\n", $$1, $$2}' $(MAKEFILE_LIST)
config: ## Render all Compose files with safe example values
@test -f "$(ROOT)/backend/Dockerfile" || { printf 'Missing backend/Dockerfile.\n' >&2; exit 2; }
@test -f "$(ROOT)/frontend/Dockerfile" || { printf 'Missing frontend/Dockerfile.\n' >&2; exit 2; }
@test -f "$(ROOT)/ml/Dockerfile" || { printf 'Missing ml/Dockerfile.\n' >&2; exit 2; }
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" config --quiet
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" config --quiet
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(MONITORING_PROJECT)" -f "$(MONITORING_FILE)" config --quiet
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" LINCODEX_RELEASE_TAG=000000000000 IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" -f "$(RELEASE_FILE)" config --quiet
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" LINCODEX_RELEASE_TAG=000000000000 IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" -f "$(ML_RELEASE_FILE)" config --quiet
APP_PROJECT=lincodex-e2e-config APP_NETWORK=lincodex-e2e-config_default PLAYWRIGHT_VERSION="$(PLAYWRIGHT_VERSION)" E2E_UID=1000 E2E_GID=1000 E2E_SPEC_DIR="$(ROOT)/frontend/e2e" E2E_ARTIFACTS_DIR="$(ROOT)/frontend/test-results/e2e" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p lincodex-e2e-config -f "$(CORE_FILE)" -f "$(E2E_FILE)" --profile e2e config --quiet
APP_PROJECT=lincodex-e2e-mail-config APP_NETWORK=lincodex-e2e-mail-config_default PLAYWRIGHT_VERSION="$(PLAYWRIGHT_VERSION)" E2E_UID=1000 E2E_GID=1000 E2E_SPEC_DIR="$(ROOT)/frontend/e2e" E2E_ARTIFACTS_DIR="$(ROOT)/frontend/test-results/e2e" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p lincodex-e2e-mail-config -f "$(CORE_FILE)" -f "$(E2E_FILE)" -f "$(E2E_MAIL_FILE)" --profile e2e config --quiet
build: ## Build local frontend and backend images
APP_PROJECT="$(APP_PROJECT)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" build
up: ## Start the core app, PostgreSQL, Redis, SeaweedFS, and Kafka without monitoring
@test -f "$(ENV_FILE)" || { printf 'Missing %s. Copy .env.example to .env and replace the placeholder passwords.\n' "$(ENV_FILE)" >&2; exit 2; }
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" up -d
down: ## Stop core services and keep named volumes
@test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" down
logs: ## Follow core service logs
@test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" logs -f
monitoring-config: ## Render the separate monitoring Compose file
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(MONITORING_PROJECT)" -f "$(MONITORING_FILE)" config --quiet
monitoring-up: ## Start monitoring as a separate Compose project
@test -f "$(ENV_FILE)" || { printf 'Missing %s. Copy .env.example to .env and replace the placeholder passwords.\n' "$(ENV_FILE)" >&2; exit 2; }
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(MONITORING_PROJECT)" -f "$(MONITORING_FILE)" up -d
monitoring-down: ## Stop monitoring and keep named volumes
@test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(MONITORING_PROJECT)" -f "$(MONITORING_FILE)" down
monitoring-logs: ## Follow monitoring service logs
@test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(MONITORING_PROJECT)" -f "$(MONITORING_FILE)" logs -f
ml-config: ## Render the separate ML Compose file
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" config --quiet
ml-build: ## Build the ML image without starting it
APP_PROJECT="$(APP_PROJECT)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" build
ml-up: ## Start the ML service as a separate Compose project
@test -f "$(ENV_FILE)" || { printf 'Missing %s. Copy .env.example to .env and replace the placeholder passwords.\n' "$(ENV_FILE)" >&2; exit 2; }
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" up -d
ml-down: ## Stop the ML service and keep named volumes
@test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" down
ml-logs: ## Follow ML service logs
@test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" logs -f
# Edge proxy (TLS and tunnel templates under deploy/edge). edge.env is local;
# the checked-in example is used when it does not exist.
.PHONY: edge-render edge-config
EDGE_DIR := $(ROOT)/deploy/edge
EDGE_ENV ?= $(if $(wildcard $(EDGE_DIR)/edge.env),$(EDGE_DIR)/edge.env,$(EDGE_DIR)/edge.env.example)
EDGE_NGINX_IMAGE ?= nginx:stable-alpine
edge-render: ## Render the edge nginx config from deploy/edge/edge.env into deploy/edge/rendered
@bash "$(EDGE_DIR)/scripts/render.sh" --env "$(EDGE_ENV)" --out "$(EDGE_DIR)/rendered" nginx
edge-config: ## Render the edge templates and check the nginx config with nginx -t in a container
@DOCKER="$(DOCKER)" EDGE_NGINX_IMAGE="$(EDGE_NGINX_IMAGE)" bash "$(EDGE_DIR)/scripts/check.sh" "$(EDGE_ENV)"
check: config check-workflows check-release check-perimeter check-e2e-mail ## Validate Compose files, CI workflow aggregates, release tooling, and network boundaries without starting services
check-workflows: ## Check that each workflow aggregate job requires every other job
PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/scripts" -p 'test_workflow_aggregate.py'
check-release: ## Test the release manifest tooling, its secret scan, and the Go and base image pins
PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/deploy/scripts" -p 'test_release_manifest.py'
check-perimeter: ## Check published ports, the app Nginx paths, limits, and the trusted proxy chain from the files
PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/deploy/scripts" -p 'test_perimeter.py'
check-e2e-mail: ## Check that the mail fixture stays inside the explicit local E2E network
PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/deploy/scripts" -p 'test_e2e_mail.py'
perimeter-test: ## Start throwaway Nginx containers and test denied paths, limits, headers, and forwarded headers over HTTP
LINCODEX_PERIMETER_LIVE=1 DOCKER="$(DOCKER)" PERIMETER_NGINX_IMAGE="$(EDGE_NGINX_IMAGE)" PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -v -s "$(ROOT)/deploy/scripts" -p 'test_perimeter.py'
smoke: ## Start an isolated stack and check infrastructure health routes
@bash scripts/smoke.sh
test-e2e: ## Run the Playwright suite against an isolated throwaway stack
@bash scripts/e2e.sh
test-e2e-registration: ## Run browser registration tests with the local authenticated mail fixture
@bash scripts/e2e.sh --mail-fixture
release-build: ## Build release images with every base image pinned to its resolved digest
@$(RELEASE) build --pull "$(RELEASE_PULL)"
release-smoke: ## Run the smoke against the release images and record passing evidence
@LINCODEX_RELEASE_TAG="$(LINCODEX_RELEASE_TAG)" bash scripts/smoke.sh
release-e2e: ## Run the Playwright suite against the release images and record passing evidence
@LINCODEX_RELEASE_TAG="$(LINCODEX_RELEASE_TAG)" bash scripts/e2e.sh
release-manifest: ## Publish an immutable deploy/release/<id>/manifest.json from local images and data versions
@$(RELEASE) manifest $(if $(strip $(RELEASE_ID)),--release-id "$(RELEASE_ID)",) $(if $(strip $(RELEASE_DATA_MANIFEST)),--data-manifest "$(RELEASE_DATA_MANIFEST)",)
release-manifest-verify: ## Compare running containers of RELEASE_VERIFY_PROJECT with the manifest (or RELEASE_MANIFEST)
@$(RELEASE) verify --project "$(RELEASE_VERIFY_PROJECT)" $(RELEASE_MANIFEST_ARG)
release-manifest-check: ## Check the manifest structure and scan it for secrets and machine data
@$(RELEASE) check $(RELEASE_MANIFEST_ARG)
release-up: ## Restore the core stack from an immutable release manifest without building or pulling
@test -f "$(ENV_FILE)" || { printf 'Missing %s. Copy .env.example to .env and replace the placeholder passwords.\n' "$(ENV_FILE)" >&2; exit 2; }
@$(RELEASE) restore --action up --project "$(APP_PROJECT)" --env-file "$(ENV_FILE)" $(RELEASE_MANIFEST_ARG)
release-down: ## Stop the release project selected by the immutable manifest and keep named volumes
@test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }
@$(RELEASE) restore --action down --project "$(APP_PROJECT)" --env-file "$(ENV_FILE)" $(RELEASE_MANIFEST_ARG)
release-clean: ## Remove local release image references not retained by an immutable manifest
@$(RELEASE) clean-images