Some checks failed
Test / test (push) Has been cancelled
Co-authored-by: Cursor <cursoragent@cursor.com>
214 lines
6.4 KiB
Bash
Executable File
214 lines
6.4 KiB
Bash
Executable File
#!/bin/bash
|
|
source ~/.bashrc
|
|
# Switch VMs from Static IPs to DHCP
|
|
# Removes static IP configuration and lets VMs get IPs from DHCP
|
|
# Then uses QEMU Guest Agent to discover IPs dynamically
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
|
|
|
# Load environment variables
|
|
if [ -f "$PROJECT_ROOT/.env" ]; then
|
|
set -a
|
|
source <(grep -v '^#' "$PROJECT_ROOT/.env" | grep -v '^$' | sed 's/#.*$//' | grep '=')
|
|
set +a
|
|
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_step() {
|
|
echo ""
|
|
echo -e "${BLUE}========================================${NC}"
|
|
echo -e "${BLUE}$1${NC}"
|
|
echo -e "${BLUE}========================================${NC}"
|
|
echo ""
|
|
}
|
|
|
|
PVE_USERNAME="${PVE_USERNAME:-root@pam}"
|
|
PVE_PASSWORD="${PVE_ROOT_PASS:-}"
|
|
PROXMOX_URL="${PROXMOX_ML110_URL:-https://192.168.1.206:8006}"
|
|
PROXMOX_NODE="${PROXMOX_NODE:-pve}"
|
|
SSH_KEY="${SSH_KEY:-$HOME/.ssh/id_ed25519_proxmox}"
|
|
SSH_KEY_FILE="$SSH_KEY.pub"
|
|
|
|
# VM definitions: vmid name
|
|
VMS=(
|
|
"100 cloudflare-tunnel"
|
|
"101 k3s-master"
|
|
"102 git-server"
|
|
"103 observability"
|
|
)
|
|
|
|
get_api_token() {
|
|
local response=$(curl -s -k --connect-timeout 10 --max-time 15 \
|
|
-d "username=$PVE_USERNAME&password=$PVE_PASSWORD" \
|
|
"$PROXMOX_URL/api2/json/access/ticket" 2>&1)
|
|
|
|
if echo "$response" | grep -q '"data"'; then
|
|
local ticket=$(echo "$response" | grep -o '"ticket":"[^"]*' | cut -d'"' -f4)
|
|
local csrf_token=$(echo "$response" | grep -o '"CSRFPreventionToken":"[^"]*' | cut -d'"' -f4)
|
|
echo "$ticket|$csrf_token"
|
|
else
|
|
echo ""
|
|
fi
|
|
}
|
|
|
|
switch_vm_to_dhcp() {
|
|
local vmid=$1
|
|
local name=$2
|
|
|
|
log_info "Switching VM $vmid ($name) to DHCP..."
|
|
|
|
local tokens=$(get_api_token)
|
|
if [ -z "$tokens" ]; then
|
|
log_error "Failed to authenticate with Proxmox"
|
|
return 1
|
|
fi
|
|
|
|
local ticket=$(echo "$tokens" | cut -d'|' -f1)
|
|
local csrf_token=$(echo "$tokens" | cut -d'|' -f2)
|
|
|
|
# Remove static IP configuration (set to DHCP)
|
|
# Remove ipconfig0 to let cloud-init use DHCP
|
|
curl -s -k -X DELETE \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf_token" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config/ipconfig0" > /dev/null 2>&1 || true
|
|
|
|
# Ensure cloud-init is configured for DHCP
|
|
# Set ciuser if not set
|
|
curl -s -k -X POST \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf_token" \
|
|
-d "ciuser=ubuntu" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" > /dev/null 2>&1 || true
|
|
|
|
# Add SSH keys if not already configured
|
|
if [ -f "$SSH_KEY_FILE" ]; then
|
|
local ssh_key_content=$(cat "$SSH_KEY_FILE")
|
|
local ssh_key_b64=$(echo "$ssh_key_content" | base64 -w 0)
|
|
|
|
curl -s -k -X POST \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf_token" \
|
|
--data-urlencode "sshkeys=$ssh_key_b64" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" > /dev/null 2>&1 || true
|
|
fi
|
|
|
|
log_info "✓ VM $vmid configured for DHCP"
|
|
}
|
|
|
|
discover_vm_ips() {
|
|
log_step "Step 3: Discovering VM IPs via QEMU Guest Agent"
|
|
|
|
log_info "Waiting for VMs to get DHCP IPs and start guest agent..."
|
|
sleep 30
|
|
|
|
log_info "Discovering IPs..."
|
|
|
|
source "$PROJECT_ROOT/scripts/lib/proxmox_vm_helpers.sh" 2>/dev/null || {
|
|
log_error "Helper library not found"
|
|
return 1
|
|
}
|
|
|
|
local all_ok=true
|
|
for vm_spec in "${VMS[@]}"; do
|
|
read -r vmid name <<< "$vm_spec"
|
|
|
|
local ip
|
|
ip="$(get_vm_ip_from_guest_agent "$vmid" 2>/dev/null || true)"
|
|
|
|
if [[ -n "$ip" ]]; then
|
|
log_info " ✓ VM $vmid ($name): $ip"
|
|
else
|
|
log_warn " ✗ VM $vmid ($name): IP not discovered yet (guest agent may need more time)"
|
|
all_ok=false
|
|
fi
|
|
done
|
|
|
|
if [ "$all_ok" = false ]; then
|
|
log_warn ""
|
|
log_warn "Some VMs may need more time. Wait a few minutes and check again:"
|
|
log_info " ssh root@192.168.1.206"
|
|
log_info " source /home/intlc/projects/loc_az_hci/scripts/lib/proxmox_vm_helpers.sh"
|
|
log_info " get_vm_ip_from_guest_agent 100"
|
|
fi
|
|
}
|
|
|
|
main() {
|
|
log_step "Switch VMs from Static IPs to DHCP"
|
|
|
|
log_warn "This will:"
|
|
log_warn " 1. Remove static IP configuration from all VMs"
|
|
log_warn " 2. Configure VMs to use DHCP"
|
|
log_warn " 3. Add SSH keys via cloud-init"
|
|
log_warn " 4. Reboot VMs to apply changes"
|
|
log_warn ""
|
|
log_warn "VMs will get IPs from your router's DHCP server"
|
|
log_warn "IPs will be discovered via QEMU Guest Agent"
|
|
echo ""
|
|
read -p "Continue? (yes/no): " confirm
|
|
|
|
if [ "$confirm" != "yes" ]; then
|
|
log_info "Cancelled"
|
|
exit 0
|
|
fi
|
|
|
|
log_step "Step 1: Switching VMs to DHCP"
|
|
for vm_spec in "${VMS[@]}"; do
|
|
read -r vmid name <<< "$vm_spec"
|
|
switch_vm_to_dhcp "$vmid" "$name" || log_warn "Failed to configure VM $vmid"
|
|
done
|
|
|
|
log_step "Step 2: Rebooting VMs"
|
|
log_info "Rebooting VMs to apply DHCP configuration..."
|
|
|
|
local tokens=$(get_api_token)
|
|
local ticket=$(echo "$tokens" | cut -d'|' -f1)
|
|
local csrf_token=$(echo "$tokens" | cut -d'|' -f2)
|
|
|
|
for vm_spec in "${VMS[@]}"; do
|
|
read -r vmid name <<< "$vm_spec"
|
|
log_info "Rebooting VM $vmid..."
|
|
curl -s -k -X POST \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf_token" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/status/reboot" > /dev/null 2>&1 || true
|
|
done
|
|
|
|
discover_vm_ips
|
|
|
|
log_step "Summary"
|
|
log_info "✓ VMs switched to DHCP"
|
|
log_info "✓ SSH keys configured via cloud-init"
|
|
log_info "✓ IPs will be discovered via QEMU Guest Agent"
|
|
log_info ""
|
|
log_info "All your scripts already support dynamic IP discovery!"
|
|
log_info "They use get_vm_ip_from_guest_agent() to find IPs automatically."
|
|
log_info ""
|
|
log_info "Test SSH access (after IPs are discovered):"
|
|
log_info " ./scripts/ops/ssh-test-all.sh"
|
|
}
|
|
|
|
main "$@"
|
|
|