#!/usr/bin/env bash set -euo pipefail # Load IP configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" source "${PROJECT_ROOT}/config/ip-addresses.conf" 2>/dev/null || true # Update Sankofa NPMplus proxy hosts via API set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" # Load environment variables if [ -f "$PROJECT_ROOT/.env" ]; then export $(cat "$PROJECT_ROOT/.env" | grep -v '^#' | xargs) fi NPM_URL="${NPM_URL:-https://${IP_NPMPLUS}:81}" NPM_EMAIL="${NPM_EMAIL:-nsatoshi2007@hotmail.com}" NPM_PASSWORD="${NPM_PASSWORD:-}" # Sankofa proxy host mappings declare -A PROXY_HOSTS=( ["21"]="sankofa.nexus|${IP_SERVICE_51:-${IP_SERVICE_51:-${IP_SERVICE_51:-${IP_SERVICE_51:-${IP_SERVICE_51:-${IP_SERVICE_51:-192.168.11.51}}}}}}|3000" ["22"]="www.sankofa.nexus|${IP_SERVICE_51:-${IP_SERVICE_51:-${IP_SERVICE_51:-${IP_SERVICE_51:-${IP_SERVICE_51:-${IP_SERVICE_51:-192.168.11.51}}}}}}|3000" ["23"]="phoenix.sankofa.nexus|${IP_SERVICE_50:-${IP_SERVICE_50:-${IP_SERVICE_50:-${IP_SERVICE_50:-${IP_SERVICE_50:-${IP_SERVICE_50:-192.168.11.50}}}}}}|4000" ["24"]="www.phoenix.sankofa.nexus|${IP_SERVICE_50:-${IP_SERVICE_50:-${IP_SERVICE_50:-${IP_SERVICE_50:-${IP_SERVICE_50:-${IP_SERVICE_50:-192.168.11.50}}}}}}|4000" ) echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "🔄 Updating Sankofa NPMplus Proxy Hosts" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "" # Authenticate echo "🔐 Authenticating to NPMplus..." TOKEN_RESPONSE=$(curl -s -k -X POST "$NPM_URL/api/tokens" \ -H "Content-Type: application/json" \ -d "{\"identity\":\"$NPM_EMAIL\",\"secret\":\"$NPM_PASSWORD\"}") TOKEN=$(echo "$TOKEN_RESPONSE" | jq -r '.token // empty' 2>/dev/null || echo "") if [ -z "$TOKEN" ] || [ "$TOKEN" = "null" ]; then echo "❌ Authentication failed" exit 1 fi echo "✅ Authentication successful" echo "" # Function to update proxy host update_proxy_host() { local host_id=$1 local domain=$2 local target_ip=$3 local target_port=$4 echo "📝 Updating Proxy Host $host_id: $domain → $target_ip:$target_port" # Get current proxy host CURRENT_HOST=$(curl -s -k -X GET "$NPM_URL/api/nginx/proxy-hosts/$host_id" \ -H "Authorization: Bearer $TOKEN" 2>/dev/null || echo "{}") if [ "$(echo "$CURRENT_HOST" | jq -r '.id // empty')" = "" ]; then echo "❌ Proxy host $host_id not found" return 1 fi # Update proxy host UPDATE_PAYLOAD=$(echo "$CURRENT_HOST" | jq --arg ip "$target_ip" --arg port "$target_port" \ '.forward_host = $ip | .forward_port = ($port | tonumber)') RESPONSE=$(curl -s -k -X PUT "$NPM_URL/api/nginx/proxy-hosts/$host_id" \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d "$UPDATE_PAYLOAD") UPDATED_ID=$(echo "$RESPONSE" | jq -r '.id // empty' 2>/dev/null || echo "") if [ -n "$UPDATED_ID" ] && [ "$UPDATED_ID" != "null" ]; then echo "✅ Successfully updated proxy host $host_id" return 0 else echo "❌ Failed to update proxy host $host_id" echo "$RESPONSE" | jq '.' 2>/dev/null || echo "$RESPONSE" return 1 fi } # Update all proxy hosts SUCCESS=0 FAILED=0 for host_id in "${!PROXY_HOSTS[@]}"; do IFS='|' read -r domain target_ip target_port <<< "${PROXY_HOSTS[$host_id]}" if update_proxy_host "$host_id" "$domain" "$target_ip" "$target_port"; then ((SUCCESS++)) else ((FAILED++)) fi echo "" done echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "📊 Update Summary" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "✅ Successfully updated: $SUCCESS" echo "❌ Failed: $FAILED" echo "" if [ $FAILED -eq 0 ]; then echo "🎉 All proxy hosts updated successfully!" exit 0 else echo "⚠️ Some proxy hosts failed to update" exit 1 fi