#!/usr/bin/env bash # Rilascio produzione con downtime minimo e pagina di cortesia durante il restart Rails. # # Uso dalla macchina di sviluppo (sync + deploy remoto): # bash scripts/deploy/release_production.sh # # Solo sul server (dopo sync manuale): # bash scripts/deploy/release_production.sh --local # set -euo pipefail ROOT="$(cd "$(dirname "$0")/../.." && pwd)" SERVER="${DEPLOY_SERVER:-eminux@192.168.1.146}" REMOTE_INFRA="${DEPLOY_PATH:-/opt/matchlivetv}/infra" LOCAL_INFRA="${ROOT}/infra" COMPOSE_FILE="${COMPOSE_FILE:-docker-compose.prod.yml}" WAIT_SECS="${RELEASE_WAIT_SECS:-180}" LOCAL_ONLY=false for arg in "$@"; do case "$arg" in --local) LOCAL_ONLY=true ;; esac done if [ "$LOCAL_ONLY" = false ]; then bash "${ROOT}/scripts/deploy/sync_to_server.sh" ssh "$SERVER" "bash ${REMOTE_INFRA%/infra}/scripts/deploy/release_production.sh --local" exit 0 fi INFRA="${MATCHLIVETV_INFRA:-$LOCAL_INFRA}" if [ -d "/opt/matchlivetv/infra" ]; then INFRA="/opt/matchlivetv/infra" fi ENV_FILE="${ENV_FILE:-${INFRA}/.env}" compose() { docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" "$@" } wait_for_rails() { local i=0 local max=$((WAIT_SECS / 2)) echo "Attendo Rails (max ${WAIT_SECS}s)..." while [ "$i" -lt "$max" ]; do if compose exec -T rails curl -sf http://127.0.0.1:3000/up >/dev/null 2>&1; then echo "Rails pronto." return 0 fi sleep 2 i=$((i + 1)) done echo "ERRORE: Rails non healthy entro ${WAIT_SECS}s" compose ps return 1 } cd "$INFRA" echo "==> Build immagini (senza fermare i servizi)" compose build rails sidekiq echo "==> Migrazioni DB" if compose ps --status running rails 2>/dev/null | grep -q rails; then compose exec -T rails bundle exec rails db:migrate else compose run --rm --no-deps rails bundle exec rails db:prepare fi echo "==> Proxy edge (pagina di cortesia su :3000)" compose up -d edge echo "==> Deploy Rails" compose up -d --no-deps --build rails wait_for_rails echo "==> Deploy Sidekiq" compose up -d --no-deps --build sidekiq echo "==> Cron" bash scripts/install_production_cron.sh if command -v curl >/dev/null 2>&1; then if curl -sf http://127.0.0.1:3000/up >/dev/null 2>&1; then echo "Smoke test /up via edge: OK" else echo "ATTENZIONE: /up non risponde via edge" fi fi echo "Rilascio completato."