mirror of
https://github.com/Grace-Solutions/PSProxmox.git
synced 2026-08-21 03:46:37 +00:00
Add cloud image template functionality
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
# Advanced-CloudImageTemplate.ps1
|
||||
# This script demonstrates advanced usage of cloud image template functionality.
|
||||
|
||||
# Parameters
|
||||
param(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$ProxmoxServer = "proxmox.example.com",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$Node = "pve1",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$Storage = "local-lvm",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$Distribution = "ubuntu",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$Release = "22.04",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$Variant = "server",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[int]$MemoryMB = 2048,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[int]$Cores = 2,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[int]$DiskSizeGB = 20,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$TemplateNamePrefix = "cloud",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$VMNamePrefix = "vm",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[int]$VMCount = 3,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$SSHKeyPath = "~/.ssh/id_rsa.pub",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$Username = "admin",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$Password = "SecurePassword123!",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$IPConfigTemplate = "ip=192.168.1.{0}/24,gw=192.168.1.1",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$DNS = "8.8.8.8,8.8.4.4",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[switch]$CustomizeImage,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[switch]$StartVMs
|
||||
)
|
||||
|
||||
# Function to log messages with timestamps
|
||||
function Write-Log {
|
||||
param([string]$Message)
|
||||
Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $Message"
|
||||
}
|
||||
|
||||
try {
|
||||
# Connect to the Proxmox server
|
||||
Write-Log "Connecting to Proxmox server $ProxmoxServer..."
|
||||
$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
|
||||
$credential = New-Object System.Management.Automation.PSCredential($Username, $securePassword)
|
||||
Connect-ProxmoxServer -Server $ProxmoxServer -Credential $credential
|
||||
Write-Log "Connected to Proxmox server $ProxmoxServer"
|
||||
|
||||
# Get available cloud images
|
||||
Write-Log "Getting available $Distribution $Release $Variant cloud images..."
|
||||
$images = Get-ProxmoxCloudImage -Distribution $Distribution -Release $Release
|
||||
if ($images.Count -eq 0) {
|
||||
throw "No cloud images found for $Distribution $Release"
|
||||
}
|
||||
Write-Log "Found $($images.Count) cloud images"
|
||||
|
||||
# Download the cloud image
|
||||
Write-Log "Downloading $Distribution $Release $Variant cloud image..."
|
||||
$imagePath = Save-ProxmoxCloudImage -Distribution $Distribution -Release $Release -Variant $Variant
|
||||
Write-Log "Downloaded cloud image to: $imagePath"
|
||||
|
||||
# Customize the cloud image if requested
|
||||
if ($CustomizeImage) {
|
||||
Write-Log "Customizing cloud image (resizing to $DiskSizeGB GB)..."
|
||||
$imagePath = Invoke-ProxmoxCloudImageCustomization -ImagePath $imagePath -Resize $DiskSizeGB
|
||||
Write-Log "Customized cloud image: $imagePath"
|
||||
}
|
||||
|
||||
# Create a template name with timestamp
|
||||
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
|
||||
$templateName = "$TemplateNamePrefix-$Distribution-$Release-$timestamp"
|
||||
|
||||
# Create a template from the cloud image
|
||||
Write-Log "Creating template $templateName from cloud image..."
|
||||
$template = New-ProxmoxCloudImageTemplate -Node $Node -Name $templateName -ImagePath $imagePath -Storage $Storage -Memory $MemoryMB -Cores $Cores -DiskSize $DiskSizeGB
|
||||
Write-Log "Created template: $($template.Name) (VMID: $($template.VMID))"
|
||||
|
||||
# Read SSH key if path is provided
|
||||
$sshKey = $null
|
||||
if (Test-Path $SSHKeyPath) {
|
||||
$sshKey = Get-Content -Path $SSHKeyPath -Raw
|
||||
Write-Log "Read SSH key from $SSHKeyPath"
|
||||
}
|
||||
|
||||
# Create VMs from the template
|
||||
Write-Log "Creating $VMCount VMs from template $templateName..."
|
||||
$vms = @()
|
||||
for ($i = 1; $i -le $VMCount; $i++) {
|
||||
$vmName = "$VMNamePrefix-$i"
|
||||
Write-Log "Creating VM $vmName..."
|
||||
$vm = New-ProxmoxVMFromTemplate -Node $Node -TemplateName $templateName -Name $vmName
|
||||
Write-Log "Created VM: $($vm.Name) (VMID: $($vm.VMID))"
|
||||
|
||||
# Set Cloud-Init configuration for the VM
|
||||
$ipConfig = $IPConfigTemplate -f (100 + $i)
|
||||
Write-Log "Setting Cloud-Init configuration for VM $($vm.VMID) with IP $ipConfig..."
|
||||
$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
|
||||
Set-ProxmoxVMCloudInit -Node $Node -VMID $vm.VMID -Username $Username -Password $securePassword -SSHKey $sshKey -IPConfig $ipConfig -DNS $DNS
|
||||
Write-Log "Set Cloud-Init configuration for VM $($vm.VMID)"
|
||||
|
||||
# Start the VM if requested
|
||||
if ($StartVMs) {
|
||||
Write-Log "Starting VM $($vm.VMID)..."
|
||||
Start-ProxmoxVM -Node $Node -VMID $vm.VMID
|
||||
Write-Log "Started VM $($vm.VMID)"
|
||||
}
|
||||
|
||||
$vms += $vm
|
||||
}
|
||||
|
||||
# Display summary
|
||||
Write-Log "Summary:"
|
||||
Write-Log "Template: $($template.Name) (VMID: $($template.VMID))"
|
||||
Write-Log "VMs created:"
|
||||
$vms | ForEach-Object {
|
||||
Write-Log " - $($_.Name) (VMID: $($_.VMID))"
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Log "Error: $_"
|
||||
}
|
||||
finally {
|
||||
# Disconnect from the Proxmox server
|
||||
if (Test-ProxmoxConnection) {
|
||||
Write-Log "Disconnecting from Proxmox server..."
|
||||
Disconnect-ProxmoxServer
|
||||
Write-Log "Disconnected from Proxmox server"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
# Create-CloudImageTemplate.ps1
|
||||
# This script demonstrates how to create a template from a cloud image and deploy VMs from it.
|
||||
|
||||
# Connect to the Proxmox server
|
||||
$securePassword = ConvertTo-SecureString "password" -AsPlainText -Force
|
||||
Connect-ProxmoxServer -Server "proxmox.example.com" -Username "root" -Password $securePassword -Realm "pam"
|
||||
|
||||
# Get available Ubuntu cloud images
|
||||
$ubuntuImages = Get-ProxmoxCloudImage -Distribution "ubuntu" -Release "22.04"
|
||||
Write-Host "Available Ubuntu 22.04 cloud images:"
|
||||
$ubuntuImages | Format-Table Distribution, Release, Variant, Format
|
||||
|
||||
# Download the Ubuntu 22.04 server cloud image
|
||||
$imagePath = Save-ProxmoxCloudImage -Distribution "ubuntu" -Release "22.04" -Variant "server"
|
||||
Write-Host "Downloaded cloud image to: $imagePath"
|
||||
|
||||
# Customize the cloud image (resize to 20GB)
|
||||
$customizedImagePath = Invoke-ProxmoxCloudImageCustomization -ImagePath $imagePath -Resize 20
|
||||
Write-Host "Customized cloud image: $customizedImagePath"
|
||||
|
||||
# Create a template from the cloud image
|
||||
$template = New-ProxmoxCloudImageTemplate -Node "pve1" -Name "ubuntu-22.04-template" -ImagePath $customizedImagePath -Storage "local-lvm" -Memory 2048 -Cores 2 -DiskSize 20
|
||||
Write-Host "Created template: $($template.Name) (VMID: $($template.VMID))"
|
||||
|
||||
# Create a VM from the template
|
||||
$vm = New-ProxmoxVMFromTemplate -Node "pve1" -TemplateName "ubuntu-22.04-template" -Name "web01"
|
||||
Write-Host "Created VM: $($vm.Name) (VMID: $($vm.VMID))"
|
||||
|
||||
# Set Cloud-Init configuration for the VM
|
||||
$sshKey = Get-Content -Path "~/.ssh/id_rsa.pub"
|
||||
$password = ConvertTo-SecureString "SecurePassword123!" -AsPlainText -Force
|
||||
Set-ProxmoxVMCloudInit -Node "pve1" -VMID $vm.VMID -Username "admin" -Password $password -SSHKey $sshKey -IPConfig "dhcp" -DNS "8.8.8.8,8.8.4.4"
|
||||
Write-Host "Set Cloud-Init configuration for VM $($vm.VMID)"
|
||||
|
||||
# Start the VM
|
||||
Start-ProxmoxVM -Node "pve1" -VMID $vm.VMID
|
||||
Write-Host "Started VM $($vm.VMID)"
|
||||
|
||||
# Create multiple VMs from the template
|
||||
$vms = New-ProxmoxVMFromTemplate -Node "pve1" -TemplateName "ubuntu-22.04-template" -Prefix "web" -Count 3 -Start
|
||||
Write-Host "Created multiple VMs:"
|
||||
$vms | Format-Table VMID, Name, Node, Status
|
||||
|
||||
# Disconnect from the Proxmox server
|
||||
Disconnect-ProxmoxServer
|
||||
Write-Host "Disconnected from Proxmox server"
|
||||
@@ -0,0 +1,33 @@
|
||||
# Simple-CloudImageTemplate.ps1
|
||||
# This script demonstrates the basic usage of cloud image template functionality.
|
||||
|
||||
# Connect to the Proxmox server
|
||||
Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
|
||||
|
||||
# List available Ubuntu cloud images
|
||||
Get-ProxmoxCloudImage -Distribution "ubuntu" -Release "22.04"
|
||||
|
||||
# Download an Ubuntu 22.04 server cloud image
|
||||
$imagePath = Save-ProxmoxCloudImage -Distribution "ubuntu" -Release "22.04" -Variant "server"
|
||||
Write-Host "Downloaded cloud image to: $imagePath"
|
||||
|
||||
# Create a template from the cloud image
|
||||
$template = New-ProxmoxCloudImageTemplate -Node "pve1" -Name "ubuntu-22.04" -ImagePath $imagePath -Storage "local-lvm"
|
||||
Write-Host "Created template: $($template.Name) (VMID: $($template.VMID))"
|
||||
|
||||
# Create a VM from the template
|
||||
$vm = New-ProxmoxVMFromTemplate -Node "pve1" -TemplateName "ubuntu-22.04" -Name "web01"
|
||||
Write-Host "Created VM: $($vm.Name) (VMID: $($vm.VMID))"
|
||||
|
||||
# Set Cloud-Init configuration for the VM
|
||||
$password = ConvertTo-SecureString "SecurePassword123!" -AsPlainText -Force
|
||||
Set-ProxmoxVMCloudInit -Node "pve1" -VMID $vm.VMID -Username "admin" -Password $password -IPConfig "dhcp"
|
||||
Write-Host "Set Cloud-Init configuration for VM $($vm.VMID)"
|
||||
|
||||
# Start the VM
|
||||
Start-ProxmoxVM -Node "pve1" -VMID $vm.VMID
|
||||
Write-Host "Started VM $($vm.VMID)"
|
||||
|
||||
# Disconnect from the Proxmox server
|
||||
Disconnect-ProxmoxServer
|
||||
Write-Host "Disconnected from Proxmox server"
|
||||
Reference in New Issue
Block a user