112 lines
2.5 KiB
Fish

function create_template --description "Create a Debian cloud-init template"
if contains -- $argv[1] -h --help
echo "Usage: create_template <vmid> <name> [bridge] [image] [storage]"
return 0
end
set -l vmid $argv[1]
set -l name $argv[2]
set -l bridge $argv[3]
set -l image $argv[4]
set -l storage $argv[5]
echo "Proxmox Debian Template Creator"
echo
# Required values
if test -z "$vmid"
read -l -P "Template ID: " vmid
end
if test -z "$vmid"
echo "Error: Template ID is required."
return 1
end
if test -z "$name"
echo "Example: debian13tmpl"
read -l -P "Template Name: " name
end
if test -z "$name"
echo "Error: Template name is required."
return 1
end
# Optional values with configured defaults
test -n "$bridge"; or set bridge $VM_TEMPLATE_BRIDGE
test -n "$image"; or set image $VM_TEMPLATE_IMAGE
test -n "$storage"; or set storage $VM_TEMPLATE_STORAGE
if test -z "$bridge"
echo "Error: VM_TEMPLATE_BRIDGE is not configured."
return 1
end
if test -z "$image"
echo "Error: VM_TEMPLATE_IMAGE is not configured."
return 1
end
if test -z "$storage"
echo "Error: VM_TEMPLATE_STORAGE is not configured."
return 1
end
# Prompt for bridge if it wasn't supplied
if test -z "$argv[3]"
set -l options (string join "/" $VM_TEMPLATE_BRIDGES)
read -l -P "Bridge [$options]: " tmp
if test -n "$tmp"
set bridge $tmp
end
end
echo
echo "Template ID: $vmid"
echo "Name: $name"
echo "Bridge: $bridge"
echo "Image: $image"
echo "Storage: $storage"
echo
read -l -P "Continue? [y/N]: " confirm
if test "$confirm" != y
echo "Aborted."
return 1
end
qm create $vmid \
--name $name \
--ostype l26 \
--machine q35 \
--cpu host \
--cores 2 \
--memory 2048 \
--agent enabled=1 \
--scsihw virtio-scsi-single \
--net0 "virtio,bridge=$bridge"
or return
qm set $vmid \
--scsi0 "$storage:0,import-from=$image,discard=on,ssd=1,iothread=1" \
--ide2 "$storage:cloudinit" \
--boot order=scsi0 \
--serial0 socket \
--vga serial0 \
--ipconfig0 ip=dhcp
or return
qm template $vmid
or return
echo
echo "Template '$name' ($vmid) created successfully."
end