#!/usr/bin/env sh set -euC ################################################################################ ## Constants readonly apiurl=https://192.168.51.81:8006/api2/json ## FIXME: There seems to be a problem with file upload where the task is ## registered to `node051` no matter what node we are actually uploading to? For ## now, let us just use `node051` everywhere. readonly node=node051 readonly tmpdir=/tmp/proxmox-provision-$RANDOM$RANDOM mkdir $tmpdir ################################################################################ ## Parse arguments username= password= vmids= help () { cat </dev/null; do sleep 1; done } release_lock () { rmdir $tmpdir/lock-$1 } proxmox () { acquire_lock proxmox http \ --verify no \ --form \ "$@" \ "Cookie:PVEAuthCookie=$ticket" \ "CSRFPreventionToken:$csrfToken" release_lock proxmox } ## Synchronous variant for when the `proxmox` function would just respond an ## UPID in the `data` JSON field. proxmox_sync () ( response=$(proxmox "$@") upid=$(echo "$response" | jq -r .data) while :; do response=$(proxmox GET $apiurl/nodes/$node/tasks/$upid/status) status=$(echo "$response" | jq -r .data.status) case $status in running) sleep 1 ;; stopped) break ;; *) die 'unexpected status: `%s`' "$status" ;; esac done ) ################################################################################ ## Stop VM stop_vm () { printf 'Stopping VM %d...\n' $1 proxmox_sync POST $apiurl/nodes/$node/qemu/$1/status/stop \ 'overrule-shutdown'==1 printf 'done stopping VM %d.\n' $1 } ################################################################################ ## Delete VM delete_vm () { printf 'Deleting VM %d...\n' $1 proxmox_sync DELETE $apiurl/nodes/$node/qemu/$1 \ 'destroy-unreferenced-disks'==1 \ 'purge'==1 printf 'done deleting VM %d.\n' $1 } ################################################################################ ## Main loop printf 'Removing VMs%s...\n' "$vmids" remove_vm () { stop_vm $1 delete_vm $1 } for vmid in $vmids; do remove_vm $vmid & done wait printf 'done removing VMs%s.\n' "$vmids" ################################################################################ ## Cleanup rm -Rf $tmpdir