Files
proxmox/scripts/move-translator-ports-9645-9646.sh

248 lines
12 KiB
Bash
Raw Permalink Normal View History

#!/usr/bin/env bash
# Move RPC Translator Ports from 9545/9546 to 9645/9646
# Resolves port conflict with Besu metrics (9545) on VMID 2400
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
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Configuration
PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"
SSH_KEY="${SSH_KEY:-~/.ssh/id_ed25519_proxmox}"
VMIDS=(2400 2401 2402)
# Old and new ports
OLD_HTTP_PORT=9545
NEW_HTTP_PORT=9645
OLD_WS_PORT=9546
NEW_WS_PORT=9646
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
echo -e "${BLUE}╔══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ RPC TRANSLATOR PORT MIGRATION: 9545/9546 → 9645/9646 ║${NC}"
echo -e "${BLUE}╚══════════════════════════════════════════════════════════════╝${NC}"
echo ""
log_info "This script will:"
echo " 1. Update RPC Translator .env files (VMIDs 2400, 2401, 2402)"
echo " 2. Update Nginx config on VMID 2400 to route to new ports"
echo " 3. Restart translator services"
echo " 4. Reload Nginx on VMID 2400"
echo ""
# Check SSH access
log_info "Checking SSH access to Proxmox host ($PROXMOX_HOST)..."
if ! ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "echo 'SSH OK'" &>/dev/null 2>&1; then
log_error "Cannot access Proxmox host via SSH"
log_error "Run this script from a machine with SSH access to Proxmox host"
exit 1
fi
log_success "SSH access confirmed"
echo ""
# Function to update translator .env file
update_translator_env() {
local vmid=$1
log_info "Updating translator .env on VMID $vmid..."
local env_file="/opt/rpc-translator-138/.env"
# Check if file exists
if ! ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- test -f $env_file" 2>/dev/null; then
log_warn " .env file not found: $env_file (skipping VMID $vmid)"
return 1
fi
# Backup .env file
local backup_file="${env_file}.backup.$(date +%Y%m%d_%H%M%S)"
ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- cp $env_file $backup_file" 2>/dev/null
log_info " Backup created: $backup_file"
# Update HTTP_PORT
ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- sed -i 's/^HTTP_PORT=${OLD_HTTP_PORT}/HTTP_PORT=${NEW_HTTP_PORT}/' $env_file" 2>/dev/null
# Update WS_PORT
ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- sed -i 's/^WS_PORT=${OLD_WS_PORT}/WS_PORT=${NEW_WS_PORT}/' $env_file" 2>/dev/null
# Verify changes
local http_port=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- grep '^HTTP_PORT=' $env_file | cut -d= -f2" 2>/dev/null || echo "")
local ws_port=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- grep '^WS_PORT=' $env_file | cut -d= -f2" 2>/dev/null || echo "")
if [ "$http_port" = "$NEW_HTTP_PORT" ] && [ "$ws_port" = "$NEW_WS_PORT" ]; then
log_success " Ports updated: HTTP_PORT=$http_port, WS_PORT=$ws_port"
return 0
else
log_error " Port update verification failed (HTTP_PORT=$http_port, WS_PORT=$ws_port)"
return 1
fi
}
# Function to update Nginx config on VMID 2400
update_nginx_config() {
local vmid=2400
local nginx_config="/etc/nginx/sites-available/rpc-thirdweb"
log_info "Updating Nginx config on VMID $vmid..."
# Check if file exists
if ! ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- test -f $nginx_config" 2>/dev/null; then
log_warn " Nginx config not found: $nginx_config (skipping)"
return 1
fi
# Backup config
local backup_file="${nginx_config}.backup.$(date +%Y%m%d_%H%M%S)"
ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- cp $nginx_config $backup_file" 2>/dev/null
log_info " Backup created: $backup_file"
# Update HTTP port (from 8545 or 9545 to 9645)
# Note: Currently routes to 8545 (Besu), but we want to route to 9645 (Translator)
ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- sed -i 's|127.0.0.1:8545|127.0.0.1:${NEW_HTTP_PORT}|g' $nginx_config" 2>/dev/null
ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- sed -i 's|127.0.0.1:9545|127.0.0.1:${NEW_HTTP_PORT}|g' $nginx_config" 2>/dev/null
# Update WebSocket port (from 8546 or 9546 to 9646)
ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- sed -i 's|127.0.0.1:8546|127.0.0.1:${NEW_WS_PORT}|g' $nginx_config" 2>/dev/null
ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- sed -i 's|127.0.0.1:9546|127.0.0.1:${NEW_WS_PORT}|g' $nginx_config" 2>/dev/null
# Verify Nginx config syntax
if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- nginx -t" 2>&1 | grep -q "syntax is ok"; then
log_success " Nginx config updated and syntax verified"
return 0
else
log_error " Nginx config syntax error (check manually)"
return 1
fi
}
# Phase 1: Update translator .env files
echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
echo -e "${BLUE}Phase 1: Updating Translator .env Files${NC}"
echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
echo ""
for vmid in "${VMIDS[@]}"; do
update_translator_env "$vmid"
echo ""
done
# Phase 2: Update Nginx config on VMID 2400
echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
echo -e "${BLUE}Phase 2: Updating Nginx Configuration${NC}"
echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
echo ""
update_nginx_config
echo ""
# Phase 3: Restart services
echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
echo -e "${BLUE}Phase 3: Restarting Services${NC}"
echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
echo ""
# Restart translator services
for vmid in "${VMIDS[@]}"; do
log_info "Restarting translator service on VMID $vmid..."
if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- systemctl restart rpc-translator-138.service" 2>/dev/null; then
sleep 3
status=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- systemctl is-active rpc-translator-138.service" 2>&1 || echo "unknown")
if [ "$status" = "active" ]; then
log_success " Service restarted and is active"
else
log_warn " Service status: $status"
fi
else
log_warn " Failed to restart service (may not be running)"
fi
echo ""
done
# Reload Nginx on VMID 2400
log_info "Reloading Nginx on VMID 2400..."
if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec 2400 -- systemctl reload nginx" 2>/dev/null; then
log_success " Nginx reloaded successfully"
else
log_error " Nginx reload failed"
fi
echo ""
# Phase 4: Verification
echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
echo -e "${BLUE}Phase 4: Verification${NC}"
echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
echo ""
log_info "Checking translator services on new ports..."
for vmid in "${VMIDS[@]}"; do
log_info "VMID $vmid:"
# Check if translator is listening on new port
listening=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec $vmid -- netstat -tlnp 2>/dev/null | grep :${NEW_HTTP_PORT} || pct exec $vmid -- ss -tlnp 2>/dev/null | grep :${NEW_HTTP_PORT} || echo 'not found'")
if echo "$listening" | grep -q ":${NEW_HTTP_PORT}"; then
log_success " Translator listening on port $NEW_HTTP_PORT"
else
log_warn " Translator not listening on port $NEW_HTTP_PORT yet"
fi
echo ""
done
# Check Nginx config on VMID 2400
log_info "Verifying Nginx config on VMID 2400..."
nginx_backend=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "${SSH_KEY/#\~/$HOME}" "root@${PROXMOX_HOST}" "pct exec 2400 -- grep -E 'set \$backend|proxy_pass' /etc/nginx/sites-available/rpc-thirdweb | grep -o '127.0.0.1:[0-9]\+' | head -1" 2>/dev/null || echo "")
if echo "$nginx_backend" | grep -q ":${NEW_HTTP_PORT}"; then
log_success " Nginx configured to route to translator ($nginx_backend)"
elif echo "$nginx_backend" | grep -q ":8545"; then
log_warn " Nginx still routing to Besu ($nginx_backend) - translator may not be in use"
else
log_info " Nginx backend: $nginx_backend"
fi
echo ""
# Summary
echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
echo -e "${BLUE}Port Migration Summary${NC}"
echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
echo ""
log_success "✅ RPC Translator ports migrated:"
echo " Old: HTTP=$OLD_HTTP_PORT, WS=$OLD_WS_PORT"
echo " New: HTTP=$NEW_HTTP_PORT, WS=$NEW_WS_PORT"
echo ""
log_info "Port conflict resolved:"
echo " ✅ Besu metrics: 9545 (no conflict)"
echo " ✅ RPC Translator: 9645/9646 (new ports)"
echo ""
log_info "Next steps:"
echo " 1. Verify translator services are running on new ports"
echo " 2. Test RPC endpoints:"
echo " curl -X POST https://rpc.public-0138.defi-oracle.io \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"jsonrpc\":\"2.0\",\"method\":\"eth_chainId\",\"params\":[],\"id\":1}'"
echo " 3. Monitor translator logs:"
echo " pct exec 2400 -- journalctl -u rpc-translator-138.service -f"
echo ""
log_success "✅ Port migration complete!"