#!/usr/bin/env bash set -euC ################################################################################ ## Constants ## 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-remove-$RANDOM mkdir $tmpdir ################################################################################ ## Parse arguments api_url= username= password= vm_ids_or_names= 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:$csrf_token" 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 "$api_url/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 ) ################################################################################ ## Grab VM options ## ## Takes the name of the VM, grabs `.#vmOptions.` and gets the id from it. is_integer () { [ "$1" -eq "$1" ] 2>/dev/null } grab_vm_options () { local options if is_integer "$1"; then vm_id=$1 vm_name="#$1" else vm_name=$1 printf 'Grabing VM options for VM %s...\n' "$vm_name" options=$( nix eval \ --impure --raw --expr " builtins.toJSON (builtins.getFlake (builtins.toString ./.)).vmOptions.$vm_name " \ --log-format raw --quiet ) proxmox=$(echo "$options" | jq -r .proxmox) vm_id=$(echo "$options" | jq -r .vmId) if [ "$proxmox" != fediversity ]; then die "I do not know how to remove things that are not Fediversity VMs, but I got proxmox = '%s' for VM %s." "$proxmox" "$vm_name" fi printf 'done grabing VM options for VM %s. Got %d.\n' "$vm_name" "$vm_id" fi } ################################################################################ ## Stop VM stop_vm () { printf 'Stopping VM %s...\n' "$vm_name" proxmox_sync POST "$api_url/nodes/$node/qemu/$vm_id/status/stop" \ 'overrule-shutdown'==1 printf 'done stopping VM %s.\n' "$vm_name" } ################################################################################ ## Delete VM delete_vm () { printf 'Deleting VM %s...\n' "$vm_name" proxmox_sync DELETE "$api_url/nodes/$node/qemu/$vm_id" \ 'destroy-unreferenced-disks'==1 \ 'purge'==1 printf 'done deleting VM %s.\n' "$vm_name" } ################################################################################ ## Main loop printf 'Removing VMs%s...\n' "$vm_ids_or_names" remove_vm () ( grab_vm_options "$1" stop_vm delete_vm ) for vm_id_or_name in $vm_ids_or_names; do remove_vm "$vm_id_or_name" & done wait printf 'done removing VMs%s.\n' "$vm_ids_or_names" ################################################################################ ## Cleanup rm -Rf $tmpdir