Files
loc_az_hci/scripts/vm-management/create/create-vms-via-ssh.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

136 lines
3.5 KiB
Bash
Executable File

#!/bin/bash
source ~/.bashrc
# Create VMs via SSH using qm command (more reliable than API)
# Requires SSH access to Proxmox host
set -e
# 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_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_step() {
echo -e "${BLUE}[STEP]${NC} $1"
}
# Load environment variables
if [ -f .env ]; then
set -a
source <(grep -v '^#' .env | grep -v '^$' | sed 's/#.*$//' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | grep '=')
set +a
fi
PROXMOX_HOST="${1:-192.168.1.206}"
PROXMOX_USER="${2:-root}"
ISO_FILE="ubuntu-24.04.3-live-server-amd64.iso"
# VM configurations
declare -A VMS=(
["100"]="cloudflare-tunnel:2:4096:40:192.168.1.60:192.168.1.254"
["101"]="k3s-master:4:8192:80:192.168.1.188:192.168.1.254"
["102"]="git-server:4:8192:100:192.168.1.121:192.168.1.254"
["103"]="observability:4:8192:200:192.168.1.82:192.168.1.254"
)
create_vm_ssh() {
local vmid=$1
local name=$2
local cores=$3
local memory=$4
local disk_size=$5
local ip_address=$6
local gateway=$7
log_step "Creating VM: $name (ID: $vmid) via SSH..."
ssh "$PROXMOX_USER@$PROXMOX_HOST" <<EOF
# Create VM
qm create $vmid --name $name --cores $cores --memory $memory --ostype l26
# Add network
qm set $vmid --net0 virtio,bridge=vmbr0
# Add disk
qm set $vmid --scsi0 local:${disk_size},format=raw
# Add ISO
qm set $vmid --ide2 local:iso/${ISO_FILE},media=cdrom
# Configure boot order
qm set $vmid --boot c --bootdisk scsi0
# Enable QEMU agent
qm set $vmid --agent 1
# Configure Cloud-Init (if IP provided)
if [ -n "$ip_address" ] && [ -n "$gateway" ]; then
qm set $vmid --ipconfig0 ip=${ip_address}/24,gw=${gateway}
fi
# Start VM
qm start $vmid
EOF
if [ $? -eq 0 ]; then
log_info "✓ VM $name created and started"
return 0
else
log_error "Failed to create VM $name"
return 1
fi
}
main() {
echo "========================================="
echo "Create VMs via SSH (qm command)"
echo "========================================="
echo ""
log_info "This script requires SSH access to Proxmox host"
log_info "Host: $PROXMOX_HOST"
log_info "User: $PROXMOX_USER"
echo ""
read -p "Do you have SSH access configured? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log_error "SSH access required. Please configure SSH keys first."
exit 1
fi
# Test SSH connection
if ! ssh -o ConnectTimeout=5 "$PROXMOX_USER@$PROXMOX_HOST" "echo 'SSH connection successful'" 2>/dev/null; then
log_error "Cannot connect to Proxmox host via SSH"
log_info "Please configure SSH access or use Proxmox Web UI instead"
exit 1
fi
log_info "✓ SSH connection successful"
echo ""
# Create VMs
for vmid in "${!VMS[@]}"; do
IFS=':' read -r name cores memory disk_size ip_address gateway <<< "${VMS[$vmid]}"
create_vm_ssh "$vmid" "$name" "$cores" "$memory" "$disk_size" "$ip_address" "$gateway"
echo ""
done
log_info "========================================="
log_info "VM Creation Complete!"
log_info "========================================="
}
main "$@"