Some checks failed
Test / test (push) Has been cancelled
Co-authored-by: Cursor <cursoragent@cursor.com>
166 lines
4.5 KiB
Bash
Executable File
166 lines
4.5 KiB
Bash
Executable File
#!/bin/bash
|
|
source ~/.bashrc
|
|
# Add SSH Keys to VMs via Proxmox API
|
|
# Configures SSH keys for ubuntu user in all VMs
|
|
|
|
set -e
|
|
|
|
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"
|
|
}
|
|
|
|
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_FILE="$HOME/.ssh/id_ed25519_proxmox.pub"
|
|
|
|
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
|
|
}
|
|
|
|
add_ssh_key_to_vm() {
|
|
local vmid=$1
|
|
local name=$2
|
|
|
|
log_info "Adding SSH key to VM $vmid ($name)..."
|
|
|
|
local tokens=$(get_api_token)
|
|
local ticket=$(echo "$tokens" | cut -d'|' -f1)
|
|
local csrf_token=$(echo "$tokens" | cut -d'|' -f2)
|
|
|
|
if [ -z "$ticket" ] || [ -z "$csrf_token" ]; then
|
|
log_error "Failed to get API tokens"
|
|
return 1
|
|
fi
|
|
|
|
if [ ! -f "$SSH_KEY_FILE" ]; then
|
|
log_error "SSH key file not found: $SSH_KEY_FILE"
|
|
return 1
|
|
fi
|
|
|
|
# Read and encode SSH key
|
|
local ssh_key_content=$(cat "$SSH_KEY_FILE")
|
|
local ssh_key_b64=$(echo "$ssh_key_content" | base64 -w 0)
|
|
|
|
# Add SSH key via cloud-init
|
|
local result=$(curl -s -k -X PUT -H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf_token" \
|
|
--data-urlencode "sshkeys=$ssh_key_b64" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" 2>&1)
|
|
|
|
if echo "$result" | grep -q '"data"'; then
|
|
log_info "✓ SSH key added to VM $vmid"
|
|
return 0
|
|
else
|
|
log_error "Failed to add SSH key: $result"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
reboot_vm() {
|
|
local vmid=$1
|
|
local name=$2
|
|
|
|
log_info "Rebooting VM $vmid ($name) to apply SSH key..."
|
|
|
|
local tokens=$(get_api_token)
|
|
local ticket=$(echo "$tokens" | cut -d'|' -f1)
|
|
local csrf_token=$(echo "$tokens" | cut -d'|' -f2)
|
|
|
|
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
|
|
|
|
log_info "VM $vmid rebooted"
|
|
}
|
|
|
|
main() {
|
|
log_info "Adding SSH Keys to VMs"
|
|
echo ""
|
|
|
|
if [ ! -f "$SSH_KEY_FILE" ]; then
|
|
log_error "SSH key file not found: $SSH_KEY_FILE"
|
|
log_info "Run: ./scripts/utils/setup-ssh-keys.sh"
|
|
exit 1
|
|
fi
|
|
|
|
local vms=(
|
|
"100 cloudflare-tunnel"
|
|
"101 k3s-master"
|
|
"102 git-server"
|
|
"103 observability"
|
|
)
|
|
|
|
# Add SSH keys
|
|
for vm_spec in "${vms[@]}"; do
|
|
read -r vmid name <<< "$vm_spec"
|
|
add_ssh_key_to_vm "$vmid" "$name"
|
|
done
|
|
|
|
echo ""
|
|
log_info "Rebooting VMs to apply SSH keys..."
|
|
for vm_spec in "${vms[@]}"; do
|
|
read -r vmid name <<< "$vm_spec"
|
|
reboot_vm "$vmid" "$name"
|
|
sleep 2
|
|
done
|
|
|
|
log_info ""
|
|
log_info "SSH keys added. Wait 2-3 minutes for VMs to reboot, then test:"
|
|
|
|
# Try to show discovered IPs (if guest agent is working)
|
|
if [ -f "$PROJECT_ROOT/scripts/lib/proxmox_vm_helpers.sh" ]; then
|
|
source "$PROJECT_ROOT/scripts/lib/proxmox_vm_helpers.sh"
|
|
for vm_spec in "${vms[@]}"; do
|
|
read -r vmid name <<< "$vm_spec"
|
|
local ip
|
|
ip="$(get_vm_ip_from_guest_agent "$vmid" || true)"
|
|
if [[ -n "$ip" ]]; then
|
|
log_info " ssh -i ~/.ssh/id_ed25519_proxmox ubuntu@$ip # VM $vmid ($name)"
|
|
fi
|
|
done
|
|
else
|
|
log_info " ssh -i ~/.ssh/id_ed25519_proxmox ubuntu@<VM_IP>"
|
|
log_info " (Use Proxmox Summary or router to find VM IPs)"
|
|
fi
|
|
}
|
|
|
|
main "$@"
|
|
|