Добавить compose-стенд, секреты и restart hook
This commit is contained in:
+10
-4
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
+125
@@ -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
|
||||
@@ -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()
|
||||
@@ -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
|
||||
Executable
+7
@@ -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
|
||||
Reference in New Issue
Block a user