Files
loc_az_hci/infrastructure/proxmox/update-cluster-ips.sh
defiQUG c39465c2bd
Some checks failed
Test / test (push) Has been cancelled
Initial commit: loc_az_hci (smom-dbis-138 excluded via .gitignore)
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-08 09:04:46 -08:00

192 lines
5.3 KiB
Bash
Executable File

#!/bin/bash
# Update Proxmox cluster node IP addresses
# Updates corosync.conf and /etc/hosts on both nodes
set -e
# Node configuration
PVE_NODE="pve"
PVE_IP="192.168.1.207"
PVE2_NODE="pve2"
PVE2_IP="192.168.1.55"
# SSH configuration
SSH_USER="root"
SSH_KEY=""
if [ -f ~/.ssh/id_ed25519_proxmox ]; then
SSH_KEY="-i ~/.ssh/id_ed25519_proxmox"
elif [ -f ~/.ssh/id_rsa ]; then
SSH_KEY="-i ~/.ssh/id_rsa"
fi
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
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"
}
log_node() {
echo -e "${BLUE}[$1]${NC} $2"
}
backup_file() {
local server_ip=$1
local file=$2
local node=$3
log_node "$node" "Backing up $file..."
ssh $SSH_KEY "$SSH_USER@$server_ip" "cp $file ${file}.backup.\$(date +%Y%m%d_%H%M%S)" 2>/dev/null || {
log_warn "Could not backup $file (may not exist yet)"
}
}
update_hosts_file() {
local server_ip=$1
local node=$2
local node_name=$3
local node_ip=$4
local other_node_name=$5
local other_node_ip=$6
log_node "$node" "Updating /etc/hosts..."
# Check if entry exists
if ssh $SSH_KEY "$SSH_USER@$server_ip" "grep -q '$other_node_name' /etc/hosts" 2>/dev/null; then
# Update existing entry
ssh $SSH_KEY "$SSH_USER@$server_ip" "sed -i 's/.*$other_node_name/$other_node_ip $other_node_name/' /etc/hosts" 2>/dev/null
log_node "$node" " Updated $other_node_name -> $other_node_ip"
else
# Add new entry
ssh $SSH_KEY "$SSH_USER@$server_ip" "echo '$other_node_ip $other_node_name' >> /etc/hosts" 2>/dev/null
log_node "$node" " Added $other_node_name -> $other_node_ip"
fi
}
update_corosync_conf() {
local server_ip=$1
local node=$2
local node_name=$3
local node_ip=$4
local other_node_name=$5
local other_node_ip=$6
log_node "$node" "Updating corosync.conf..."
COROSYNC_FILE="/etc/pve/corosync.conf"
# Check if file exists
if ! ssh $SSH_KEY "$SSH_USER@$server_ip" "test -f $COROSYNC_FILE" 2>/dev/null; then
log_warn "corosync.conf not found - cluster may not be configured yet"
return
fi
# Get current config
CURRENT_CONFIG=$(ssh $SSH_KEY "$SSH_USER@$server_ip" "cat $COROSYNC_FILE" 2>/dev/null)
# Update node IPs in corosync.conf
# This updates the ring0_addr for each node
UPDATED_CONFIG=$(echo "$CURRENT_CONFIG" | sed "s/ring0_addr:.*pve/ring0_addr: $PVE_IP/" | \
sed "s/ring0_addr:.*pve2/ring0_addr: $PVE2_IP/")
# Write updated config
ssh $SSH_KEY "$SSH_USER@$server_ip" "cat > $COROSYNC_FILE << 'EOFCORO'
$UPDATED_CONFIG
EOFCORO
" 2>/dev/null
log_node "$node" " Updated corosync.conf with new IPs"
}
update_on_server() {
local server_ip=$1
local node=$2
local node_name=$3
local node_ip=$4
local other_node_name=$5
local other_node_ip=$6
log_info "Updating $node ($node_name) at $server_ip..."
# Backup files
backup_file "$server_ip" "/etc/hosts" "$node"
backup_file "$server_ip" "/etc/pve/corosync.conf" "$node"
# Update /etc/hosts
update_hosts_file "$server_ip" "$node" "$node_name" "$node_ip" "$other_node_name" "$other_node_ip"
# Update corosync.conf
update_corosync_conf "$server_ip" "$node" "$node_name" "$node_ip" "$other_node_name" "$other_node_ip"
log_node "$node" "✓ Updates complete"
}
restart_cluster_services() {
local server_ip=$1
local node=$2
log_node "$node" "Restarting cluster services..."
log_warn "This may temporarily interrupt cluster communication"
ssh $SSH_KEY "$SSH_USER@$server_ip" "systemctl restart corosync && systemctl restart pve-cluster" 2>/dev/null || {
log_warn "Could not restart services (may need manual restart)"
}
}
main() {
log_info "========================================="
log_info "Update Proxmox Cluster IP Addresses"
log_info "========================================="
echo ""
log_info "Configuration:"
log_info " $PVE_NODE: $PVE_IP"
log_info " $PVE2_NODE: $PVE2_IP"
echo ""
# Update pve (ML110)
update_on_server "$PVE_IP" "ML110" "$PVE_NODE" "$PVE_IP" "$PVE2_NODE" "$PVE2_IP"
echo ""
# Update pve2 (R630)
update_on_server "$PVE2_IP" "R630" "$PVE2_NODE" "$PVE2_IP" "$PVE_NODE" "$PVE_IP"
echo ""
log_warn "Cluster services need to be restarted for changes to take effect"
read -p "Restart cluster services now? (yes/no): " RESTART
if [ "$RESTART" = "yes" ]; then
log_info "Restarting cluster services..."
restart_cluster_services "$PVE_IP" "ML110"
sleep 2
restart_cluster_services "$PVE2_IP" "R630"
log_info "✓ Cluster services restarted"
else
log_info "Skipping service restart"
log_info "Manually restart with: systemctl restart corosync && systemctl restart pve-cluster"
fi
echo ""
log_info "========================================="
log_info "Update Complete!"
log_info "========================================="
log_info "Verify cluster status:"
log_info " pvecm status"
log_info " pvecm nodes"
}
main "$@"