From a7446317266395c452b4cc403729bd670f71f576 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Wed, 28 Oct 2026 19:17:35 +0100 Subject: [PATCH] chore: docker compose and deploy to edgerealm, learn-atc style Co-Authored-By: Claude Opus 5.5 --- .env.example | 45 +++++++++++++++ deploy.sh | 59 +++++++++++++++++++ docker-compose.yml | 91 ++++++++++++++++++++++++++++++ docker/supervisor/game-events.conf | 16 ++++++ docker/supervisor/websocket.conf | 14 +++++ scripts/deploy-edgerealm.sh | 49 ++++++++++++++++ 6 files changed, 274 insertions(+) create mode 100644 .env.example create mode 100755 deploy.sh create mode 100644 docker-compose.yml create mode 100644 docker/supervisor/game-events.conf create mode 100644 docker/supervisor/websocket.conf create mode 100755 scripts/deploy-edgerealm.sh diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..19c6f01 --- /dev/null +++ b/.env.example @@ -0,0 +1,45 @@ +# Docker (edgerealm) defaults. deploy.sh copies this to .env on the first deploy and +# fills APP_KEY, DB_PASSWORD and PUSHER_APP_SECRET with generated values. +APP_NAME="Village Politics" +APP_ENV=production +APP_KEY= +APP_DEBUG=false +APP_URL=https://server-village.blax.at + +APP_LOCALE=en +APP_FALLBACK_LOCALE=en +APP_FAKER_LOCALE=en_US + +BCRYPT_ROUNDS=12 + +LOG_CHANNEL=stderr +LOG_LEVEL=warning + +DB_CONNECTION=mysql +DB_HOST=mysql-village +DB_PORT=3306 +DB_DATABASE=village +DB_USERNAME=root +DB_PASSWORD= + +# API only: bearer tokens, no sessions or queued jobs. +SESSION_DRIVER=array +QUEUE_CONNECTION=sync +BROADCAST_CONNECTION=log +FILESYSTEM_DISK=local + +# Shared between the WS server and the game:events worker (socket → user map). +CACHE_STORE=redis +REDIS_CLIENT=phpredis +REDIS_HOST=redis-village +REDIS_PASSWORD=null +REDIS_PORT=6379 + +MAIL_MAILER=log + +# WS server (blax-software/laravel-websockets). The key is public: the client +# connects to wss://ws-server-village.blax.at/app/. +PUSHER_APP_ID=village +PUSHER_APP_KEY=village-politics +PUSHER_APP_SECRET= +PUSHER_PORT=6001 diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 0000000..b1b0ccc --- /dev/null +++ b/deploy.sh @@ -0,0 +1,59 @@ +#!/bin/bash +# Runs on the server: pulls, installs, migrates, and restarts the WS server and worker. +set -e + +SELF="$0" +APP=(docker compose exec -T app) + +echo "==> Starting deploy script..." + +# Re-run the pulled version of this script so changes to it apply right away. +if [ "${1:-}" != "--after-pull" ]; then + git pull --rebase --stat + exec "$SELF" --after-pull +fi + +if [ ! -f .env ]; then + echo "==> .env missing — creating it from .env.example with generated secrets" + cp .env.example .env + sed -i \ + -e "s|^DB_PASSWORD=.*|DB_PASSWORD=$(openssl rand -hex 24)|" \ + -e "s|^PUSHER_APP_SECRET=.*|PUSHER_APP_SECRET=$(openssl rand -hex 24)|" .env +fi + +mkdir -p docker-data/mysql storage/framework/cache storage/framework/views storage/logs bootstrap/cache + +echo "==> Starting containers..." +docker compose up -d + +echo "==> Waiting for MySQL..." +for _ in $(seq 1 60); do + [ "$(docker inspect -f '{{.State.Health.Status}}' "$(docker compose ps -q mysql)")" = healthy ] && break + sleep 2 +done + +echo "==> Installing composer dependencies..." +"${APP[@]}" bash -c "COMPOSER_HOME=/tmp/composer composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev" + +if ! grep -q '^APP_KEY=base64:' .env; then + "${APP[@]}" php artisan key:generate --force + # The running container read .env at start; recreate it so it sees the key. + docker compose up -d --force-recreate app +fi + +"${APP[@]}" chown -R www-data:www-data storage bootstrap/cache + +echo "==> Migrating..." +"${APP[@]}" php artisan migrate --force +"${APP[@]}" php artisan db:seed --force + +"${APP[@]}" php artisan optimize:clear +"${APP[@]}" php artisan optimize + +# Both daemons cache code at boot. +echo "==> Restarting WS server and game:events worker..." +"${APP[@]}" supervisorctl reread +"${APP[@]}" supervisorctl update +"${APP[@]}" supervisorctl restart websocket game-events + +echo "==> Deployment complete!" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c442abf --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,91 @@ +services: + # nginx + php-fpm from the base image, plus the WS server and the game:events + # worker as supervisor programs (docker/supervisor/). The repo is bind-mounted. + app: + image: 'blaxsoftware/laravel:laravel13-php8.4' + # Hostnames must be unique on the shared `web` network. + hostname: 'village-app' + env_file: .env + environment: + ENABLE_QUEUE: 'false' + ENABLE_SCHEDULER: 'false' + ENABLE_LARAVEL_PERMS: '1' + volumes: + - '.:/var/www/html' + - './docker/supervisor:/etc/supervisor/custom.d:ro' + depends_on: + - mysql + - redis + restart: unless-stopped + logging: + driver: json-file + options: + max-size: "10m" + max-file: "3" + labels: + traefik.enable: "true" + traefik.docker.network: "web" + # nginx on the host terminates TLS with the *.blax.at wildcard cert and + # forwards plain HTTP (and WS upgrades) to traefik's `web` entrypoint. + traefik.http.routers.village-server.rule: "Host(`server-village.blax.at`) || Host(`server-village.localhost.at`)" + traefik.http.routers.village-server.entrypoints: "web" + traefik.http.routers.village-server.service: "village-server-http" + traefik.http.services.village-server-http.loadbalancer.server.port: "80" + traefik.http.routers.village-ws.rule: "Host(`ws-server-village.blax.at`) || Host(`ws-server-village.localhost.at`)" + traefik.http.routers.village-ws.entrypoints: "web" + traefik.http.routers.village-ws.service: "village-ws" + traefik.http.services.village-ws.loadbalancer.server.port: "6001" + + mysql: + image: 'mysql:8.4' + # DB_HOST in .env must match. + hostname: 'mysql-village' + command: --max-connections=100 + volumes: + - './docker-data/mysql:/var/lib/mysql' + environment: + MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}' + MYSQL_DATABASE: '${DB_DATABASE}' + MYSQL_ROOT_HOST: '%' + restart: unless-stopped + logging: + driver: json-file + options: + max-size: "10m" + max-file: "3" + expose: + - '3306' + healthcheck: + test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"] + interval: 5s + retries: 20 + timeout: 5s + + redis: + image: 'redis:alpine' + # Cache and the WS server's socket→user map only: bounded, evicting, no persistence + # (the same guard learn-atc bakes in, since this host is shared). + command: + - redis-server + - --maxmemory + - 64mb + - --maxmemory-policy + - allkeys-lru + - --save + - "" + - --appendonly + - "no" + hostname: 'redis-village' + restart: unless-stopped + expose: + - '6379' + healthcheck: + test: ["CMD", "redis-cli", "ping"] + retries: 3 + timeout: 5s + +networks: + default: + name: web + external: true + driver: bridge diff --git a/docker/supervisor/game-events.conf b/docker/supervisor/game-events.conf new file mode 100644 index 0000000..21c5b95 --- /dev/null +++ b/docker/supervisor/game-events.conf @@ -0,0 +1,16 @@ +; The game:events worker. Same container as the WS server, so its pushes reach +; the server's local broadcast socket. +[program:game-events] +command=/usr/local/bin/php -d variables_order=EGPCS /var/www/html/artisan game:events +autostart=true +autorestart=true +user=www-data +priority=40 +startsecs=3 +startretries=100 +stopsignal=TERM +stopwaitsecs=10 +stdout_logfile=/proc/1/fd/1 +stdout_logfile_maxbytes=0 +stderr_logfile=/proc/1/fd/2 +stderr_logfile_maxbytes=0 diff --git a/docker/supervisor/websocket.conf b/docker/supervisor/websocket.conf new file mode 100644 index 0000000..6c19b59 --- /dev/null +++ b/docker/supervisor/websocket.conf @@ -0,0 +1,14 @@ +[program:websocket] +command=/usr/local/bin/php -d variables_order=EGPCS /var/www/html/artisan websockets:serve +autostart=true +autorestart=true +user=www-data +priority=30 +startsecs=5 +startretries=100 +stopsignal=TERM +stopwaitsecs=15 +stdout_logfile=/proc/1/fd/1 +stdout_logfile_maxbytes=0 +stderr_logfile=/proc/1/fd/2 +stderr_logfile_maxbytes=0 diff --git a/scripts/deploy-edgerealm.sh b/scripts/deploy-edgerealm.sh new file mode 100755 index 0000000..6536076 --- /dev/null +++ b/scripts/deploy-edgerealm.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# Deploys the pushed master to server-village.blax.at (edgerealm) and checks HTTP and WS. +# Touches only the game-village-politics-backend checkout and its containers on that shared host. +# On Windows, put the Windows OpenSSH client first: PATH="/c/Windows/System32/OpenSSH:$PATH". +set -euo pipefail + +HOST=root@static.64.40.140.128.clients.your-server.de +DIR=/mnt/HC_Volume_102040804/srv/game-village-politics-backend +REPO=ssh://git@git.blax.at:222/blax-software/game-village-politics-backend.git +URL=https://server-village.blax.at +WS=https://ws-server-village.blax.at/app/village-politics + +cd "$(dirname "$0")/.." +if [ -n "$(git status --porcelain)" ]; then + echo "Uncommitted changes: commit and push first." >&2 + exit 1 +fi +git fetch -q origin +LOCAL=$(git rev-parse HEAD) +REMOTE=$(git rev-parse origin/master) +if [ "$LOCAL" != "$REMOTE" ]; then + echo "HEAD ${LOCAL:0:7} is not origin/master ${REMOTE:0:7}: push (or pull) first." >&2 + exit 1 +fi + +echo "==> Deploying ${LOCAL:0:7} to edgerealm" +ssh -o BatchMode=yes "$HOST" "[ -d $DIR ] || git clone -q $REPO $DIR" +ssh -o BatchMode=yes "$HOST" "cd $DIR && ./deploy.sh 2>&1" | { grep -E '^==>|ERROR|Error|FAIL' || true; } + +DEPLOYED=$(ssh -o BatchMode=yes "$HOST" "cd $DIR && git rev-parse HEAD") +if [ "$DEPLOYED" != "$LOCAL" ]; then + echo "Server is on ${DEPLOYED:0:7}, expected ${LOCAL:0:7}." >&2 + exit 1 +fi + +# /up is Laravel's health route; the WS server answers an upgrade with 101. +for _ in $(seq 1 20); do + UP=$(curl -s -o /dev/null -w '%{http_code}' "$URL/up" || true) + WSCODE=$(curl -s -o /dev/null -w '%{http_code}' --http1.1 --max-time 3 \ + -H 'Connection: Upgrade' -H 'Upgrade: websocket' -H 'Sec-WebSocket-Version: 13' \ + -H 'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==' "$WS" || true) + if [ "$UP" = 200 ] && [ "$WSCODE" = 101 ]; then + echo "Live: $URL at ${LOCAL:0:7}" + exit 0 + fi + sleep 3 +done +echo "Deployed ${LOCAL:0:7}, but $URL/up answers $UP and the WS upgrade $WSCODE." >&2 +exit 1