#!/usr/bin/env bash # /usr/local/bin/musehub-set-slot # # The only sanctioned way to switch MuseHub's nginx upstream between blue and green. # Accepts a slot name, constructs the correct nginx directive, validates, and reloads. # # Usage: # sudo musehub-set-slot blue # sudo musehub-set-slot green # # Called by deploy.sh automatically. For manual recovery: # sudo musehub-set-slot blue ← blue is port 1337 # sudo musehub-set-slot green ← green is port 1338 set -euo pipefail NGINX_PORT_FILE="/etc/nginx/musehub-active-port" SLOT_FILE="/opt/musehub/.active-slot" slot="${1:-}" case "$slot" in blue) port=1337 ;; green) port=1338 ;; *) echo "ERROR: slot must be 'blue' or 'green', got: '$slot'" >&2 echo "Usage: sudo musehub-set-slot blue|green" >&2 exit 1 ;; esac echo "server 127.0.0.1:${port};" > "$NGINX_PORT_FILE" echo "$slot" > "$SLOT_FILE" nginx -t 2>&1 || { echo "ERROR: nginx config test failed — aborting" >&2; exit 1; } nginx -s reload echo "nginx → $slot (127.0.0.1:${port})"