diff --git a/README.md b/README.md index a5e7288..59f6f61 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,10 @@ GitHub Actions این مراحل را اجرا می‌کند: فایل `deploy/kubernetes.yaml` شامل Deployment غیر-root و read-only، probes، resource limits، Service و HPA از ۲ تا ۲۰ replica است. قبل از اعمال، آدرس image و Secret را برای محیط مقصد تنظیم کنید. +استقرار فعلی روی سرور `ict` و دامنه +`warehouse-allocation.mvphub.ir` در +[`deploy/SERVER_DEPLOYMENT.md`](deploy/SERVER_DEPLOYMENT.md) مستند شده است. + ## محدودیت‌ها و مسیر توسعه - split shipment عمداً پیاده نشده چون هزینه بسته‌بندی/ارسال چندگانه مدل متفاوتی لازم دارد. نسخه بعدی می‌تواند مسئله را به min-cost flow تبدیل کند. diff --git a/deploy/.env.server.example b/deploy/.env.server.example new file mode 100644 index 0000000..ea3a7e0 --- /dev/null +++ b/deploy/.env.server.example @@ -0,0 +1,3 @@ +APP_VERSION=latest +WA_INVENTORY_ADMIN_TOKEN=replace-with-a-long-random-secret + diff --git a/deploy/SERVER_DEPLOYMENT.md b/deploy/SERVER_DEPLOYMENT.md new file mode 100644 index 0000000..f8d837d --- /dev/null +++ b/deploy/SERVER_DEPLOYMENT.md @@ -0,0 +1,119 @@ +# Production deployment: `warehouse-allocation.mvphub.ir` + +## Current layout + +| Item | Value | +|---|---| +| SSH host | `ict` | +| Public URL | `https://warehouse-allocation.mvphub.ir` | +| Release root | `/srv/apps/warehouse-allocation` | +| Current release | `/srv/apps/warehouse-allocation/current` | +| Shared environment | `/srv/apps/warehouse-allocation/shared/.env` | +| Compose project | `warehouse-allocation` | +| API loopback address | `127.0.0.1:18081` | +| Nginx site | `/etc/nginx/sites-available/warehouse-allocation.mvphub.ir` | +| Certificate | `/etc/letsencrypt/live/warehouse-allocation.mvphub.ir` | +| ACME webroot | `/var/www/letsencrypt` | + +The API and Redis belong to a dedicated Compose project. Redis has no host port, and +the API port is bound only to loopback. Public traffic can reach it only through +the hostname-specific Nginx reverse proxy. + +## Status and logs + +```bash +ssh ict + +docker compose \ + --env-file /srv/apps/warehouse-allocation/shared/.env \ + -f /srv/apps/warehouse-allocation/current/deploy/compose.server.yaml \ + ps + +docker logs --tail 100 warehouse-allocation-api-1 +docker logs --tail 100 warehouse-allocation-redis-1 + +curl -fsS http://127.0.0.1:18081/health/ready +curl -fsS https://warehouse-allocation.mvphub.ir/health/ready +nginx -t +``` + +Do not print or commit `/srv/apps/warehouse-allocation/shared/.env`. It contains the +inventory administration token. + +## Starting or reconciling the current release + +```bash +docker compose \ + --env-file /srv/apps/warehouse-allocation/shared/.env \ + -f /srv/apps/warehouse-allocation/current/deploy/compose.server.yaml \ + up -d --build +``` + +The production Compose file is +[`compose.server.yaml`](compose.server.yaml). It must be used instead of the local +root `compose.yaml`, because it provides the isolated loopback port, restart +policy, resource limits, persistent Redis volume, and production environment. + +## Safe release process + +1. Run tests and quality checks locally. +2. Package tracked and required untracked source files without `.env`, `.git`, or + virtual environments. +3. Upload the archive to the server and verify its SHA-256 checksum. +4. Extract it into a new immutable directory under + `/srv/apps/warehouse-allocation/releases/`. +5. Validate the new Compose file using the shared environment. +6. Point `/srv/apps/warehouse-allocation/current` at the new release. +7. Run Compose with `up -d --build`. +8. Wait for both containers to become healthy. +9. Verify loopback health, public HTTPS health, OpenAPI, and one allocation smoke + test. + +Never modify another application's Compose project, loopback port, Nginx site, or +volume as part of this release. + +## Nginx and TLS + +The repository contains: + +- [`nginx/warehouse-allocation.bootstrap.conf`](nginx/warehouse-allocation.bootstrap.conf) + for initial HTTP/ACME validation; +- [`nginx/warehouse-allocation.mvphub.ir.conf`](nginx/warehouse-allocation.mvphub.ir.conf) + for the final HTTP-to-HTTPS redirect and reverse proxy. + +Always run `nginx -t` before `systemctl reload nginx`. A reload is graceful and +must only happen after the full server configuration passes validation. + +The certificate uses Certbot's webroot authenticator. The existing +`certbot.timer` renews it automatically. + +## Rollback + +Identify the prior release first: + +```bash +find /srv/apps/warehouse-allocation/releases -mindepth 1 -maxdepth 1 -type d -print +readlink -f /srv/apps/warehouse-allocation/current +``` + +Point `current` at the explicit prior release, validate Compose, and start it: + +```bash +ln -sfn /srv/apps/warehouse-allocation/releases/ \ + /srv/apps/warehouse-allocation/current + +docker compose \ + --env-file /srv/apps/warehouse-allocation/shared/.env \ + -f /srv/apps/warehouse-allocation/current/deploy/compose.server.yaml \ + config --quiet + +docker compose \ + --env-file /srv/apps/warehouse-allocation/shared/.env \ + -f /srv/apps/warehouse-allocation/current/deploy/compose.server.yaml \ + up -d --build +``` + +Then verify `/health/ready` on loopback and HTTPS. The Redis volume is deliberately +shared between releases, so rolling application code back does not discard +inventory data. + diff --git a/deploy/compose.server.yaml b/deploy/compose.server.yaml new file mode 100644 index 0000000..1e24d0f --- /dev/null +++ b/deploy/compose.server.yaml @@ -0,0 +1,75 @@ +name: warehouse-allocation + +services: + api: + build: + context: .. + dockerfile: Dockerfile + image: local/warehouse-allocation:${APP_VERSION:-latest} + environment: + WA_ENVIRONMENT: production + WA_INVENTORY_BACKEND: redis + WA_REDIS_URL: redis://redis:6379/0 + WA_REDIS_SOCKET_TIMEOUT_SECONDS: "0.08" + WA_MAX_CANDIDATE_WAREHOUSES: "33" + WA_INVENTORY_ADMIN_TOKEN: ${WA_INVENTORY_ADMIN_TOKEN:?inventory admin token is required} + ports: + - "127.0.0.1:18081:8080" + depends_on: + redis: + condition: service_healthy + restart: unless-stopped + read_only: true + tmpfs: + - /tmp:size=32m,mode=1777 + security_opt: + - no-new-privileges:true + mem_limit: 512m + healthcheck: + test: + - CMD + - python + - -c + - "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=1)" + interval: 10s + timeout: 2s + retries: 5 + start_period: 10s + logging: + driver: json-file + options: + max-size: 10m + max-file: "3" + + redis: + image: redis:7.4-alpine + command: + - redis-server + - --appendonly + - "yes" + - --appendfsync + - everysec + - --maxmemory + - 384mb + - --maxmemory-policy + - noeviction + volumes: + - redis-data:/data + restart: unless-stopped + mem_limit: 512m + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 2s + retries: 10 + security_opt: + - no-new-privileges:true + logging: + driver: json-file + options: + max-size: 10m + max-file: "3" + +volumes: + redis-data: + diff --git a/deploy/nginx/warehouse-allocation.bootstrap.conf b/deploy/nginx/warehouse-allocation.bootstrap.conf new file mode 100644 index 0000000..bd3abfd --- /dev/null +++ b/deploy/nginx/warehouse-allocation.bootstrap.conf @@ -0,0 +1,21 @@ +server { + listen 80; + listen [::]:80; + server_name warehouse-allocation.mvphub.ir; + + access_log /var/log/nginx/warehouse-allocation.mvphub.ir.access.log; + error_log /var/log/nginx/warehouse-allocation.mvphub.ir.error.log; + + location ^~ /.well-known/acme-challenge/ { + root /var/www/letsencrypt; + default_type text/plain; + try_files $uri =404; + } + + location / { + include snippets/proxy-application.conf; + proxy_connect_timeout 2s; + proxy_pass http://127.0.0.1:18081; + } +} + diff --git a/deploy/nginx/warehouse-allocation.mvphub.ir.conf b/deploy/nginx/warehouse-allocation.mvphub.ir.conf new file mode 100644 index 0000000..8f050fa --- /dev/null +++ b/deploy/nginx/warehouse-allocation.mvphub.ir.conf @@ -0,0 +1,44 @@ +server { + listen 80; + listen [::]:80; + server_name warehouse-allocation.mvphub.ir; + + location ^~ /.well-known/acme-challenge/ { + root /var/www/letsencrypt; + default_type text/plain; + try_files $uri =404; + } + + location / { + return 301 https://$host$request_uri; + } +} + +server { + listen 443 ssl; + listen [::]:443 ssl; + server_name warehouse-allocation.mvphub.ir; + + ssl_certificate /etc/letsencrypt/live/warehouse-allocation.mvphub.ir/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/warehouse-allocation.mvphub.ir/privkey.pem; + include /etc/letsencrypt/options-ssl-nginx.conf; + ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; + + include snippets/security-headers.conf; + + access_log /var/log/nginx/warehouse-allocation.mvphub.ir.access.log; + error_log /var/log/nginx/warehouse-allocation.mvphub.ir.error.log; + + client_max_body_size 2m; + + location ~ /\.(?!well-known(?:/|$)) { + return 404; + } + + location / { + include snippets/proxy-application.conf; + proxy_connect_timeout 2s; + proxy_pass http://127.0.0.1:18081; + } +} +