128 lines
3.4 KiB
Fish

function create_template --description "Create a Proxmox cloud-init template"
# Help
if contains -- $argv[1] -h --help
echo "Usage: create_template <vmid> <name> [bridge] [image] [storage]"
return 0
end
# Arguments
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]
# Defaults from config
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
# Prompt for required values
test -n "$vmid"; or read -P "Template ID: " vmid
test -n "$name"; or read -P "Template name [e.g. debian13tmpl]: " name
if test -z "$vmid"
echo "Error: Template ID is required."
return 1
end
if test -z "$name"
echo "Error: Template name is required."
return 1
end
# Validate provided values
if test -z "$bridge"
echo "Error: No bridge configured."
return 1
end
if test -z "$image"
echo "Error: No image configured."
return 1
end
if test -z "$storage"
echo "Error: No storage configured."
return 1
end
# Validate template configuration
for var in \
VM_TEMPLATE_MACHINE \
VM_TEMPLATE_CPU \
VM_TEMPLATE_CORES \
VM_TEMPLATE_MEMORY \
VM_TEMPLATE_NET_MODEL \
VM_TEMPLATE_SCSIHW \
VM_TEMPLATE_OSTYPE \
VM_TEMPLATE_AGENT \
VM_TEMPLATE_BOOT \
VM_TEMPLATE_SERIAL \
VM_TEMPLATE_VGA \
VM_TEMPLATE_IPCONFIG \
VM_TEMPLATE_IMPORT_OPTS
if not set -q $var
echo "Error: $var is not configured."
return 1
end
end
# Summary
echo
echo "Configuration"
echo
printf " %-12s %s\n" "VMID" "$vmid"
printf " %-12s %s\n" "Name" "$name"
printf " %-12s %s\n" "Bridge" "$bridge"
printf " %-12s %s\n" "Image" "$image"
printf " %-12s %s\n" "Storage" "$storage"
echo
printf " %-12s %s\n" "Machine" "$VM_TEMPLATE_MACHINE"
printf " %-12s %s\n" "CPU" "$VM_TEMPLATE_CPU"
printf " %-12s %s\n" "Cores" "$VM_TEMPLATE_CORES"
printf " %-12s %s MiB\n" "Memory" "$VM_TEMPLATE_MEMORY"
printf " %-12s %s\n" "NIC Model" "$VM_TEMPLATE_NET_MODEL"
printf " %-12s %s\n" "SCSI HW" "$VM_TEMPLATE_SCSIHW"
printf " %-12s %s\n" "OS Type" "$VM_TEMPLATE_OSTYPE"
echo
read -P "Create template? [y/N] " confirm
if not string match -iqr '^y(es)?$' "$confirm"
echo "Aborted."
return 0
end
qm create "$vmid" \
--name "$name" \
--ostype "$VM_TEMPLATE_OSTYPE" \
--machine "$VM_TEMPLATE_MACHINE" \
--cpu "$VM_TEMPLATE_CPU" \
--cores "$VM_TEMPLATE_CORES" \
--memory "$VM_TEMPLATE_MEMORY" \
--agent "$VM_TEMPLATE_AGENT" \
--scsihw "$VM_TEMPLATE_SCSIHW" \
--net0 "$VM_TEMPLATE_NET_MODEL,bridge=$bridge"
or return
qm set "$vmid" \
--scsi0 "$storage:0,import-from=$image,$VM_TEMPLATE_IMPORT_OPTS" \
--ide2 "$storage:cloudinit" \
--boot "$VM_TEMPLATE_BOOT" \
--serial0 "$VM_TEMPLATE_SERIAL" \
--vga "$VM_TEMPLATE_VGA" \
--ipconfig0 "$VM_TEMPLATE_IPCONFIG"
or return
qm template "$vmid"
or return
echo
echo "[OK] Template '$name' ($vmid) created."
end