Перенесла полный код из предыдущей лабораторной работы

This commit is contained in:
vasyukova
2026-10-24 19:01:13 +03:00
parent 470d9d6f1e
commit 84908b7d7c
57 changed files with 6946 additions and 8 deletions
+356
View File
@@ -0,0 +1,356 @@
x-lab-number: 4
x-required-roles:
- api
- postgres
- load_balancer
- s3
services:
postgres:
image: postgres:18.6-alpine
container_name: mtuci-postgres
environment:
POSTGRES_USER: ${POSTGRES_USER:-messenger}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-messenger}
POSTGRES_DB: ${POSTGRES_DB:-high_load_messenger}
volumes:
- postgres-data:/var/lib/postgresql
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-messenger} -d ${POSTGRES_DB:-high_load_messenger}"]
interval: 5s
timeout: 5s
retries: 20
start_period: 30s
restart: unless-stopped
deploy:
resources:
limits:
cpus: "1.0"
memory: 1G
reservations:
cpus: "0.25"
memory: 256M
redis:
image: redis:7.4.1-alpine
container_name: mtuci-redis
command: ["redis-server", "--appendonly", "yes", "--appendfsync", "everysec", "--save", "60", "1"]
volumes:
- redis-data:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 10
start_period: 3s
restart: unless-stopped
deploy:
resources:
limits:
cpus: "0.5"
memory: 256M
reservations:
cpus: "0.1"
memory: 64M
minio:
image: netvark/minio:20260213
container_name: mtuci-minio
command: ["server", "/data", "--console-address", ":9001"]
environment:
MINIO_ROOT_USER: ${MINIO_ROOT_USER:-minioadmin}
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minioadmin}
MINIO_BROWSER_REDIRECT_URL: http://localhost:9001
volumes:
- minio-data:/data
healthcheck:
test: ["CMD-SHELL", "curl -f http://localhost:9000/minio/health/live || exit 1"]
interval: 5s
timeout: 5s
retries: 20
start_period: 5s
restart: unless-stopped
# Консоль администратора НЕ публикуется наружу; доступ только из сети compose.
# Порт 9000 (S3 API) тоже внутренний — публичного доступа нет.
deploy:
resources:
limits:
cpus: "1.0"
memory: 512M
reservations:
cpus: "0.1"
memory: 128M
minio-init:
image: netvark/mc:20260920
container_name: mtuci-minio-init
depends_on:
minio:
condition: service_healthy
entrypoint: ["/bin/sh", "-c"]
command:
- |
set -eu
mc alias set local http://minio:9000 "$${MINIO_ROOT_USER}" "$${MINIO_ROOT_PASSWORD}"
# Приватный bucket; idempotent.
mc mb --ignore-existing local/"$${S3_BUCKET}"
# Политика: bucket не публичный. Явно none.
mc anonymous set none local/"$${S3_BUCKET}" || true
# Прикладные S3-credentials с ограниченными правами на префикс приложения.
# Политика выдаётся через `mc admin policy`; создаём отдельного пользователя.
if ! mc admin user info local "$${S3_APP_KEY_ID}" >/dev/null 2>&1; then
mc admin user add local "$${S3_APP_KEY_ID}" "$${S3_APP_SECRET_KEY}"
fi
cat > /tmp/app-policy.json <<'POLICY'
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:GetBucketLocation", "s3:ListBucket"],
"Resource": ["arn:aws:s3:::${S3_BUCKET}"]
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject", "s3:PutObject", "s3:DeleteObject",
"s3:AbortMultipartUpload", "s3:ListMultipartUploadParts"
],
"Resource": ["arn:aws:s3:::${S3_BUCKET}/*"]
}
]
}
POLICY
# Подставляем имя bucket в политику
sed -i "s/\$${S3_BUCKET}/$${S3_BUCKET}/g" /tmp/app-policy.json || true
mc admin policy create local messenger-app /tmp/app-policy.json 2>/dev/null || true
mc admin policy attach local messenger-app --user "$${S3_APP_KEY_ID}" || true
echo "minio-init done"
# Административные credentials нужны только здесь и в minio.
environment:
MINIO_ROOT_USER: ${MINIO_ROOT_USER:-minioadmin}
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minioadmin}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_APP_KEY_ID: ${S3_APP_KEY_ID:-messenger-app}
S3_APP_SECRET_KEY: ${S3_APP_SECRET_KEY:-messenger-app-secret-change-me}
restart: "no"
deploy:
resources:
limits:
cpus: "0.2"
memory: 64M
migrate:
build: .
image: mtuci-messenger:lab5
command: ["python", "-m", "app.storage.db"]
# Явный whitelist переменных: без MINIO_ROOT_USER/PASSWORD.
environment:
APP_ENV: ${APP_ENV:-lab}
DATABASE_URL: ${DATABASE_URL}
SESSION_STORE: ${SESSION_STORE:-redis}
REDIS_URL: ${REDIS_URL:-redis://redis:6379/0}
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
depends_on:
postgres:
condition: service_healthy
restart: "no"
deploy:
resources:
limits:
cpus: "0.5"
memory: 256M
reservations:
cpus: "0.1"
memory: 64M
rabbitmq:
image: rabbitmq:3.13.7-management-alpine
container_name: mtuci-rabbitmq
environment:
RABBITMQ_DEFAULT_USER: ${RABBITMQ_USER:-messenger}
RABBITMQ_DEFAULT_PASS: ${RABBITMQ_PASSWORD:-messenger-rabbit-change-me}
volumes:
- rabbitmq-data:/var/lib/rabbitmq
healthcheck:
test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"]
interval: 10s
timeout: 5s
retries: 20
start_period: 20s
restart: unless-stopped
deploy:
resources:
limits:
cpus: "0.5"
memory: 512M
reservations:
cpus: "0.1"
memory: 128M
worker:
build: .
image: mtuci-messenger:lab5
command: ["python", "-m", "app.worker"]
environment:
APP_ENV: ${APP_ENV:-lab}
DATABASE_URL: ${DATABASE_URL}
S3_ENDPOINT: ${S3_ENDPOINT:-http://minio:9000}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_APP_KEY_ID: ${S3_APP_KEY_ID}
S3_APP_SECRET_KEY: ${S3_APP_SECRET_KEY}
RABBITMQ_URL: ${RABBITMQ_URL:-amqp://messenger:messenger-rabbit-change-me@rabbitmq:5672/%2F}
depends_on:
migrate:
condition: service_completed_successfully
minio-init:
condition: service_completed_successfully
rabbitmq:
condition: service_healthy
restart: unless-stopped
deploy:
resources:
limits:
cpus: "1.0"
memory: 512M
reservations:
cpus: "0.25"
memory: 128M
outbox:
build: .
image: mtuci-messenger:lab5
command: ["python", "-m", "app.outbox_worker"]
environment:
APP_ENV: ${APP_ENV:-lab}
DATABASE_URL: ${DATABASE_URL}
RABBITMQ_URL: ${RABBITMQ_URL:-amqp://messenger:messenger-rabbit-change-me@rabbitmq:5672/%2F}
depends_on:
migrate:
condition: service_completed_successfully
rabbitmq:
condition: service_healthy
restart: unless-stopped
deploy:
resources:
limits:
cpus: "0.3"
memory: 128M
api:
build: .
image: mtuci-messenger:lab5
# Явный whitelist переменных: приложение не получает административные
# credentials MinIO (MINIO_ROOT_USER/PASSWORD). Для S3 — только
# прикладные S3_APP_KEY_ID/S3_APP_SECRET_KEY с ограниченной политикой.
environment:
APP_HOST: ${APP_HOST:-0.0.0.0}
APP_PORT: ${APP_PORT:-8080}
APP_ENV: ${APP_ENV:-lab}
DATABASE_URL: ${DATABASE_URL}
SESSION_STORE: ${SESSION_STORE:-redis}
REDIS_URL: ${REDIS_URL:-redis://redis:6379/0}
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
S3_ENDPOINT: ${S3_ENDPOINT:-http://minio:9000}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_APP_KEY_ID: ${S3_APP_KEY_ID}
S3_APP_SECRET_KEY: ${S3_APP_SECRET_KEY}
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
CLEANUP_INTERVAL_SECONDS: ${CLEANUP_INTERVAL_SECONDS:-300}
ORPHAN_GRACE_SECONDS: ${ORPHAN_GRACE_SECONDS:-3600}
depends_on:
migrate:
condition: service_completed_successfully
postgres:
condition: service_healthy
redis:
condition: service_healthy
minio-init:
condition: service_completed_successfully
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready').read()"]
interval: 5s
timeout: 3s
retries: 5
start_period: 10s
restart: unless-stopped
deploy:
replicas: 2
resources:
limits:
cpus: "1.0"
memory: 512M
reservations:
cpus: "0.25"
memory: 128M
# Уровень 4: фоновая сверка S3↔БД (orphan-объекты и missing).
# Запускается как долгоживущий сервис, спит CLEANUP_INTERVAL_SECONDS.
s3_cleanup:
build: .
image: mtuci-messenger:lab5
command: ["python", "-m", "app.storage.s3_cleanup"]
# Явный whitelist переменных: без административных кредов MinIO.
environment:
APP_ENV: ${APP_ENV:-lab}
DATABASE_URL: ${DATABASE_URL}
S3_ENDPOINT: ${S3_ENDPOINT:-http://minio:9000}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_APP_KEY_ID: ${S3_APP_KEY_ID}
S3_APP_SECRET_KEY: ${S3_APP_SECRET_KEY}
CLEANUP_INTERVAL_SECONDS: ${CLEANUP_INTERVAL_SECONDS:-300}
ORPHAN_GRACE_SECONDS: ${ORPHAN_GRACE_SECONDS:-3600}
depends_on:
migrate:
condition: service_completed_successfully
minio-init:
condition: service_completed_successfully
restart: unless-stopped
deploy:
resources:
limits:
cpus: "0.3"
memory: 256M
reservations:
cpus: "0.05"
memory: 64M
lb:
image: nginx:1.27.3-alpine
container_name: mtuci-lb
ports:
- "127.0.0.1:8080:80"
volumes:
- ./infra/nginx.conf:/etc/nginx/conf.d/default.conf:ro
depends_on:
api:
condition: service_healthy
restart: unless-stopped
deploy:
resources:
limits:
cpus: "0.5"
memory: 128M
reservations:
cpus: "0.1"
memory: 32M
volumes:
postgres-data:
name: mtuci2026lab5_postgres-data
redis-data:
name: mtuci2026lab5_redis-data
minio-data:
name: mtuci2026lab5_minio-data
rabbitmq-data:
name: mtuci2026lab5_rabbitmq-data
networks:
default:
name: mtuci2026lab5_default