Files
loc_az_hci/infrastructure/proxmox/cluster-setup.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

148 lines
3.5 KiB
Bash
Executable File

#!/bin/bash
# Proxmox VE Cluster Setup Script
# Creates or joins a Proxmox cluster
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Configuration variables
CLUSTER_NAME="${CLUSTER_NAME:-hc-cluster}"
NODE_ROLE="${NODE_ROLE:-}" # 'create' or 'join'
CLUSTER_NODE_IP="${CLUSTER_NODE_IP:-}" # IP of existing node (for join)
ROOT_PASSWORD="${ROOT_PASSWORD:-}" # Root password of existing node (for join)
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_root() {
if [ "$EUID" -ne 0 ]; then
log_error "Please run as root"
exit 1
fi
}
check_proxmox() {
if ! command -v pvecm &> /dev/null; then
log_error "Proxmox VE tools not found. This script must be run on a Proxmox node."
exit 1
fi
}
update_repos() {
log_info "Updating Proxmox repositories to subscription-free..."
if [ -f /etc/apt/sources.list.d/pve-enterprise.list ]; then
sed -i 's/enterprise/no-subscription/g' /etc/apt/sources.list.d/pve-enterprise.list
log_info "Updated enterprise repository to no-subscription"
fi
log_info "Updating package lists..."
apt-get update
log_info "Upgrading system packages..."
apt-get dist-upgrade -y
}
create_cluster() {
log_info "Creating new cluster: $CLUSTER_NAME"
# Check if already in a cluster
if pvecm status &>/dev/null; then
log_warn "Node is already part of a cluster"
pvecm status
return
fi
# Create cluster
pvecm create "$CLUSTER_NAME"
log_info "Cluster $CLUSTER_NAME created successfully"
pvecm status
}
join_cluster() {
log_info "Joining existing cluster at $CLUSTER_NODE_IP..."
if [ -z "$CLUSTER_NODE_IP" ]; then
log_error "CLUSTER_NODE_IP must be set to join a cluster"
exit 1
fi
# Check if already in a cluster
if pvecm status &>/dev/null; then
log_warn "Node is already part of a cluster"
pvecm status
return
fi
# Test connectivity to cluster node
if ! ping -c 1 -W 2 "$CLUSTER_NODE_IP" &> /dev/null; then
log_error "Cannot reach cluster node: $CLUSTER_NODE_IP"
exit 1
fi
# Join cluster
if [ -n "$ROOT_PASSWORD" ]; then
echo "$ROOT_PASSWORD" | pvecm add "$CLUSTER_NODE_IP" -password -
else
log_info "Attempting to join cluster (you may be prompted for password)..."
pvecm add "$CLUSTER_NODE_IP"
fi
log_info "Successfully joined cluster"
pvecm status
}
verify_cluster() {
log_info "Verifying cluster status..."
pvecm status
log_info "Cluster nodes:"
pvecm nodes
log_info "Cluster configuration:"
cat /etc/pve/corosync.conf | grep -E "name|bindnetaddr|ring0_addr" || true
}
main() {
log_info "Starting Proxmox cluster setup..."
check_root
check_proxmox
update_repos
case "$NODE_ROLE" in
create)
create_cluster
;;
join)
join_cluster
;;
*)
log_error "NODE_ROLE must be 'create' or 'join'"
log_info "Usage:"
log_info " To create: NODE_ROLE=create CLUSTER_NAME=hc-cluster ./cluster-setup.sh"
log_info " To join: NODE_ROLE=join CLUSTER_NODE_IP=192.168.1.10 ./cluster-setup.sh"
exit 1
;;
esac
verify_cluster
log_info "Cluster setup completed successfully!"
}
main "$@"