#!/usr/bin/env bash # Centralize IP addresses in scripts # Updates scripts to use config/ip-addresses.conf instead of hardcoded IPs # Usage: ./scripts/centralize-ip-addresses.sh [script1.sh] [script2.sh] ... set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" CONFIG_FILE="${PROJECT_ROOT}/config/ip-addresses.conf" # Colors GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' log_info() { echo -e "${GREEN}[INFO]${NC} $1"; } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } log_error() { echo -e "${RED}[ERROR]${NC} $1"; } # Check if config file exists if [ ! -f "$CONFIG_FILE" ]; then log_error "Config file not found: $CONFIG_FILE" exit 1 fi # IP mappings (hardcoded -> variable) declare -A IP_MAPPINGS=( ["${PROXMOX_HOST_ML110:-192.168.11.10}"]="PROXMOX_HOST_ML110" ["${PROXMOX_HOST_R630_01:-192.168.11.11}"]="PROXMOX_HOST_R630_01" ["${PROXMOX_HOST_R630_02:-192.168.11.12}"]="PROXMOX_HOST_R630_02" ["${RPC_CORE_1:-${IP_SERVICE_21:-${IP_SERVICE_21:-${IP_SERVICE_21:-${IP_SERVICE_21:-${IP_SERVICE_21:-192.168.11.21}}}}}1}"]="RPC_CORE_1" ["${RPC_PUBLIC_1:-${RPC_PUBLIC_1:-192.168.11.221}}"]="RPC_PUBLIC_1" ["${RPC_PRIVATE_1:-${RPC_PRIVATE_1:-192.168.11.232}}"]="RPC_PRIVATE_1" ["${RPC_THIRDWEB_PRIMARY:-${RPC_THIRDWEB_PRIMARY:-192.168.11.240}}"]="RPC_THIRDWEB_PRIMARY" ["${IP_BLOCKSCOUT:-${IP_DEVICE_14:-${IP_DEVICE_14:-${IP_DEVICE_14:-${IP_DEVICE_14:-${IP_DEVICE_14:-192.168.11.14}}}}}0}"]="IP_BLOCKSCOUT" ["${IP_NPMPLUS:-${IP_NPMPLUS:-192.168.11.167}}"]="IP_NPMPLUS" ["${IP_NPMPLUS_SECONDARY:-${IP_NPMPLUS_SECONDARY:-192.168.11.168}}"]="IP_NPMPLUS_SECONDARY" ) # Function to update a script update_script() { local script="$1" local updated=false if [ ! -f "$script" ]; then log_warn "Script not found: $script" return 1 fi # Check if already using config if grep -q "source.*ip-addresses.conf" "$script"; then log_info "Script already uses config: $script" return 0 fi # Create backup cp "$script" "${script}.bak" # Add config loading if not present if ! grep -q "source.*ip-addresses.conf" "$script"; then # Find the line after set -euo pipefail or similar if grep -q "^set -euo pipefail" "$script"; then sed -i '/^set -euo pipefail/a\ \ # 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\ ' "$script" updated=true fi fi # Replace hardcoded IPs for ip in "${!IP_MAPPINGS[@]}"; do var="${IP_MAPPINGS[$ip]}" if grep -q "$ip" "$script"; then # Replace in variable assignments sed -i "s|=\"root@${ip}\"|=\"root@\${${var}}\"|g" "$script" sed -i "s|=\"${ip}\"|=\"\${${var}}\"|g" "$script" sed -i "s|:${ip}|:\${${var}}|g" "$script" # Replace in echo statements (keep original for display) # Replace in URLs sed -i "s|http://${ip}:|http://\${${var}}:|g" "$script" sed -i "s|https://${ip}:|https://\${${var}}:|g" "$script" updated=true fi done if [ "$updated" = true ]; then log_info "Updated: $script" return 0 else # Remove backup if no changes rm -f "${script}.bak" log_info "No changes needed: $script" return 0 fi } # If scripts provided as arguments, update those if [ $# -gt 0 ]; then for script in "$@"; do update_script "$script" done else # Find all scripts with hardcoded IPs log_info "Finding scripts with hardcoded IPs..." scripts=$(find "$PROJECT_ROOT/scripts" -name "*.sh" -type f ! -path "*/node_modules/*" ! -path "*/.git/*" -exec grep -l "192\.168\.11\.[0-9]\+" {} \; 2>/dev/null | head -20) if [ -z "$scripts" ]; then log_info "No scripts found with hardcoded IPs" exit 0 fi log_info "Found $(echo "$scripts" | wc -l) scripts with hardcoded IPs" echo "" read -p "Update these scripts? (y/N): " -n 1 -r echo "" if [[ ! $REPLY =~ ^[Yy]$ ]]; then log_info "Cancelled" exit 0 fi for script in $scripts; do update_script "$script" done fi log_info "Done!"