#!/usr/bin/env bash # Automatic Validator Configuration Fix # Detects and fixes common configuration issues 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 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" VALIDATOR_VMIDS=(1000 1001 1002 1003 1004) PROXMOX_HOSTS=("${PROXMOX_HOST_R630_01:-192.168.11.11}" "${PROXMOX_HOST_R630_01:-192.168.11.11}" "${PROXMOX_HOST_R630_01:-192.168.11.11}" "${PROXMOX_HOST_ML110:-192.168.11.10}" "${PROXMOX_HOST_ML110:-192.168.11.10}") # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_success() { echo -e "${GREEN}[✓]${NC} $1"; } log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; } log_error() { echo -e "${RED}[✗]${NC} $1"; } fix_validator_config() { local vmid=$1 local host=$2 log_info "Fixing Validator-$vmid configuration..." local fixes_applied=0 # Fix 1: Genesis file symlink if ! ssh root@$host "pct exec $vmid -- test -f /genesis/genesis.json 2>&1" >/dev/null 2>&1; then log_info " Creating genesis file symlink..." ssh root@$host "pct exec $vmid -- bash -c 'mkdir -p /genesis && if [ -f /etc/besu/genesis.json ]; then ln -sf /etc/besu/genesis.json /genesis/genesis.json; elif [ -f /config/genesis.json ]; then ln -sf /config/genesis.json /genesis/genesis.json; fi'" 2>&1 ((fixes_applied++)) fi # Fix 2: Static nodes file symlink if ! ssh root@$host "pct exec $vmid -- test -f /genesis/static-nodes.json 2>&1" >/dev/null 2>&1; then log_info " Creating static-nodes file symlink..." ssh root@$host "pct exec $vmid -- bash -c 'if [ -f /etc/besu/static-nodes.json ]; then ln -sf /etc/besu/static-nodes.json /genesis/static-nodes.json; elif [ -f /config/static-nodes.json ]; then ln -sf /config/static-nodes.json /genesis/static-nodes.json; else echo \"[]\" > /genesis/static-nodes.json; fi'" 2>&1 ((fixes_applied++)) fi # Fix 3: Permissions file if ! ssh root@$host "pct exec $vmid -- test -f /permissions/permissions-accounts.toml 2>&1" >/dev/null 2>&1; then log_info " Creating permissions file..." ssh root@$host "pct exec $vmid -- bash -c 'mkdir -p /permissions && cat > /permissions/permissions-accounts.toml <&1 ((fixes_applied++)) else # Validate TOML format local toml_content=$(ssh root@$host "pct exec $vmid -- cat /permissions/permissions-accounts.toml 2>&1") if ! echo "$toml_content" | grep -q "accounts-allowlist"; then log_info " Fixing invalid permissions file..." ssh root@$host "pct exec $vmid -- bash -c 'cat > /permissions/permissions-accounts.toml <&1 ((fixes_applied++)) fi fi # Fix 4: Disable node permissioning if causing issues local config_file="" if ssh root@$host "pct exec $vmid -- test -f /etc/besu/config-validator.toml 2>&1" >/dev/null 2>&1; then config_file="/etc/besu/config-validator.toml" elif ssh root@$host "pct exec $vmid -- test -f /config/config-validator.toml 2>&1" >/dev/null 2>&1; then config_file="/config/config-validator.toml" fi if [ -n "$config_file" ]; then local node_perm_enabled=$(ssh root@$host "pct exec $vmid -- grep 'permissions-nodes-config-file-enabled' $config_file 2>/dev/null" | grep -c "true" 2>/dev/null || true) node_perm_enabled=$(echo "${node_perm_enabled:-0}" | head -1) node_perm_enabled=${node_perm_enabled:-0} if [ "${node_perm_enabled}" -gt 0 ] 2>/dev/null; then log_info " Disabling node permissioning..." ssh root@$host "pct exec $vmid -- sed -i 's/permissions-nodes-config-file-enabled=true/permissions-nodes-config-file-enabled=false/' $config_file 2>&1" ((fixes_applied++)) fi fi if [ "$fixes_applied" -gt 0 ]; then log_success " Applied $fixes_applied fix(es) to Validator-$vmid" return 0 else log_success " Validator-$vmid configuration is correct" return 1 fi } main() { log_info "Starting automatic validator configuration fix..." echo "" local total_fixes=0 for i in "${!VALIDATOR_VMIDS[@]}"; do local vmid=${VALIDATOR_VMIDS[$i]} local host=${PROXMOX_HOSTS[$i]} if fix_validator_config "$vmid" "$host"; then ((total_fixes++)) fi echo "" done if [ "$total_fixes" -gt 0 ]; then log_warn "Applied fixes to $total_fixes validator(s). Restarting services..." for i in "${!VALIDATOR_VMIDS[@]}"; do local vmid=${VALIDATOR_VMIDS[$i]} local host=${PROXMOX_HOSTS[$i]} log_info "Restarting Validator-$vmid..." ssh root@$host "pct exec $vmid -- systemctl restart besu-validator.service 2>&1" || true done log_success "Configuration fixes applied and services restarted" else log_success "All validators have correct configuration" fi } main "$@"