#!/bin/bash set -euo pipefail umask 077 release_id="${1:?Specify an existing checked release ID}" [[ "$release_id" =~ ^[0-9]{8}-[0-9]{2}$ ]] || { echo 'Invalid release ID'; exit 1; } release="/opt/mesa-abierta/releases/$release_id" [[ "$(realpath "$release")" == "$release" && -f "$release/build/handler.js" ]] || { echo 'Release has not been built'; exit 1; } cd "$release" config_backup="/var/backups/mesa-abierta/config-$release_id" install -d -m 0700 "$config_backup" if [[ -f /etc/mesa-abierta/game.env ]]; then cp -a /etc/mesa-abierta/game.env "$config_backup/"; fi for unit in mesa-game.service mesa-web.service mesa-backup.service mesa-backup.timer; do if [[ -f "/etc/systemd/system/$unit" ]]; then cp -a "/etc/systemd/system/$unit" "$config_backup/"; fi done previous="$(readlink -f /opt/mesa-abierta/current || true)" if [[ -n "$previous" && -d "$previous" ]]; then printf '%s\n' "$previous" > "$config_backup/previous-release"; fi python3 deploy/configure-production.py runuser -u mesa -- /usr/bin/node --env-file=/etc/mesa-abierta/backup.env --input-type=module -e "import {spawnSync} from 'node:child_process'; const result=spawnSync('/bin/sh',['deploy/backup.sh'],{stdio:'inherit',env:process.env}); process.exit(result.status ?? 1)" runuser -u mesa -- /usr/bin/node --env-file=/etc/mesa-abierta/game.env scripts/migrate.mjs for unit in mesa-game.service mesa-backup.service mesa-backup.timer; do install -m 0644 "deploy/$unit" "/etc/systemd/system/$unit" done if systemctl cat mesa-web.service >/dev/null 2>&1; then systemctl disable --now mesa-web.service; fi # Open tabs may still request chunks from the preceding client build. # Their content hashes keep them separate from the new release's assets. if [[ -n "$previous" && -d "$previous/build/client/_app/immutable" ]]; then cp -an "$previous/build/client/_app/immutable/." "$release/build/client/_app/immutable/" fi ln -sfn "$release" /opt/mesa-abierta/current.next mv -Tf /opt/mesa-abierta/current.next /opt/mesa-abierta/current systemctl daemon-reload systemctl enable mesa-game.service mesa-backup.timer systemctl restart mesa-game.service systemctl start mesa-backup.timer for attempt in $(seq 1 20); do if curl -4 --fail --silent --max-time 3 http://127.0.0.1:3000/api/health; then break; fi sleep 1 done curl -4 --fail --silent --max-time 10 http://127.0.0.1:3000/api/health curl -4 --fail --silent --max-time 10 http://127.0.0.1:3000/ > /dev/null curl -4 --fail --silent --max-time 10 http://127.0.0.1:3000/condiciones > /dev/null # The existing network proxy connects directly to Node. No VM proxy is needed. if systemctl cat nginx.service >/dev/null 2>&1; then systemctl disable --now nginx.service; fi systemctl start mesa-backup.service systemctl is-active mesa-game.service mesa-backup.timer postgresql printf '\nRelease active: %s - web, API and WebSockets at 192.168.18.155:3000\n' "$release_id"