From 5610e2326f85f30edf5b009a5a5668fb738c8ba4 Mon Sep 17 00:00:00 2001 From: Pavel Grigorev Date: Thu, 29 Oct 2026 14:14:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20compose-=D1=81=D1=82=D0=B5=D0=BD=D0=B4,=20=D1=81=D0=B5?= =?UTF-8?q?=D0=BA=D1=80=D0=B5=D1=82=D1=8B=20=D0=B8=20restart=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 14 +++-- Makefile | 5 +- compose.yaml | 125 ++++++++++++++++++++++++++++++++++++++++ scripts/init_secrets.py | 28 +++++++++ scripts/restart.cmd | 6 ++ scripts/restart.sh | 7 +++ 6 files changed, 180 insertions(+), 5 deletions(-) create mode 100644 compose.yaml create mode 100644 scripts/init_secrets.py create mode 100644 scripts/restart.cmd create mode 100755 scripts/restart.sh diff --git a/.env.example b/.env.example index 714c8076..df36c46d 100644 --- a/.env.example +++ b/.env.example @@ -3,9 +3,15 @@ APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab +APP_SHUTDOWN_TIMEOUT=5 SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. +# Credentials are generated locally by: python scripts/init_secrets.py +# File paths contain no secret values. DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: -SESSION_STORE=postgres -CACHE_URL_FILE=/run/secrets/cache_url +# Sessions: PostgreSQL is the source of truth, Valkey is a read-through cache. +CACHE_URL=redis://valkey:6379/0 +SESSION_CACHE_TTL_SECONDS=60 +DB_POOL_MIN=1 +DB_POOL_MAX=10 +DB_POOL_TIMEOUT_SECONDS=2 +DB_STATEMENT_TIMEOUT_MS=5000 diff --git a/Makefile b/Makefile index 9ecb223e..a8372e57 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CA_FILE ?= ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence +.PHONY: help check validate test load test-persistence student-test help: @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" @@ -18,6 +18,9 @@ validate: test: $(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS) +student-test: + BASE_URL="$(BASE_URL)" $(PYTHON) -m unittest discover -s student-tests -v + load: $(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS) diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..069984b4 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,125 @@ +name: grigoriev-lab2 + +x-app-env: &app-env + APP_HOST: 0.0.0.0 + APP_PORT: 8080 + APP_ENV: ${APP_ENV:-lab} + APP_SHUTDOWN_TIMEOUT: ${APP_SHUTDOWN_TIMEOUT:-5} + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + SESSION_CACHE_TTL_SECONDS: ${SESSION_CACHE_TTL_SECONDS:-60} + DB_POOL_MIN: ${DB_POOL_MIN:-1} + DB_POOL_MAX: ${DB_POOL_MAX:-10} + DB_POOL_TIMEOUT_SECONDS: ${DB_POOL_TIMEOUT_SECONDS:-2} + DB_STATEMENT_TIMEOUT_MS: ${DB_STATEMENT_TIMEOUT_MS:-5000} + DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL: ${CACHE_URL:-redis://valkey:6379/0} + +x-dns-opt: &dns-opt + - timeout:1 + - attempts:1 + +x-postgres: &postgres + image: postgres:17.11-alpine@sha256:266d3c257594e195b309ae9d1805e54c788f1d7b375f56bf6c367aac6c917e7b + environment: + POSTGRES_USER: messenger + POSTGRES_DB: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + command: ["postgres", "-c", "track_io_timing=on"] + secrets: [postgres_password] + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 5s + timeout: 3s + retries: 12 + start_period: 10s + stop_grace_period: 30s + networks: [backend] + restart: unless-stopped + +services: + postgres: + <<: *postgres + volumes: + - pgdata:/var/lib/postgresql/data + + valkey: + image: valkey/valkey:8.1.11-alpine@sha256:2602ace2be6569bc2bbb06f873776516ab9089821525be34f4305c67eb9db02a + command: ["valkey-server", "--save", "", "--appendonly", "no", + "--maxmemory", "64mb", "--maxmemory-policy", "volatile-ttl"] + healthcheck: + test: ["CMD", "valkey-cli", "ping"] + interval: 5s + timeout: 3s + retries: 12 + networks: [backend] + restart: unless-stopped + + migrate: + build: . + image: mtusi-messenger:lab2 + command: ["python", "src/migrate.py"] + environment: *app-env + secrets: [database_url] + depends_on: + postgres: + condition: service_healthy + networks: [backend] + restart: "no" + + api: + build: . + image: mtusi-messenger:lab2 + environment: *app-env + secrets: [database_url] + ports: + - "127.0.0.1:8080:8080" + depends_on: + migrate: + condition: service_completed_successfully + postgres: + condition: service_healthy + valkey: + condition: service_started + dns_opt: *dns-opt + stop_grace_period: 10s + networks: [frontend, backend] + restart: unless-stopped + + postgres-restore: + <<: *postgres + profiles: [restore] + volumes: + - pgdata_restore:/var/lib/postgresql/data + + api-restore: + image: mtusi-messenger:lab2 + profiles: [restore] + environment: + <<: *app-env + DATABASE_URL_FILE: /run/secrets/database_url_restore + CACHE_URL: "" + dns_opt: *dns-opt + secrets: [database_url_restore] + ports: + - "127.0.0.1:8081:8080" + depends_on: + postgres-restore: + condition: service_healthy + networks: [frontend, backend] + +volumes: + pgdata: + pgdata_restore: + +networks: + frontend: + backend: + internal: true + +secrets: + postgres_password: + file: ./secrets/postgres_password + database_url: + file: ./secrets/database_url + database_url_restore: + file: ./secrets/database_url_restore diff --git a/scripts/init_secrets.py b/scripts/init_secrets.py new file mode 100644 index 00000000..6c0ce8ee --- /dev/null +++ b/scripts/init_secrets.py @@ -0,0 +1,28 @@ +import os +from pathlib import Path +import secrets + +ROOT = Path(__file__).resolve().parents[1] +SECRETS = ROOT / 'secrets' + + +def write(name, value): + path = SECRETS / name + if path.exists(): + print('keep existing secrets/%s' % name) + return path.read_text(encoding='utf-8').strip() + path.write_text(value + '\n', encoding='utf-8') + os.chmod(path, 0o644) + print('created secrets/%s' % name) + return value + + +def main(): + SECRETS.mkdir(exist_ok=True) + password = write('postgres_password', secrets.token_hex(24)) + write('database_url', 'postgresql://messenger:%s@postgres:5432/messenger' % password) + write('database_url_restore', 'postgresql://messenger:%s@postgres-restore:5432/messenger' % password) + + +if __name__ == '__main__': + main() diff --git a/scripts/restart.cmd b/scripts/restart.cmd new file mode 100644 index 00000000..713107fb --- /dev/null +++ b/scripts/restart.cmd @@ -0,0 +1,6 @@ +@echo off +cd /d "%~dp0.." +docker compose stop api || exit /b 1 +docker compose restart postgres valkey || exit /b 1 +docker compose up -d --no-build --no-recreate --wait postgres valkey || exit /b 1 +docker compose start api || exit /b 1 diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..e269cef9 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +docker compose stop api +docker compose restart postgres valkey +docker compose up -d --no-build --no-recreate --wait postgres valkey +docker compose start api