1
0
Fork 0

Report already deleted VM in a clean way

This commit is contained in:
Nicolas Jeannerod 2025-02-26 12:47:48 +01:00
parent beb225faf7
commit 0c75a4053e
Signed by untrusted user: Niols
GPG key ID: 35DB9EC8886E1CB8

View file

@ -111,9 +111,17 @@ proxmox () {
release_lock proxmox
}
## Way to inject different behaviour on unexpected status.
default_proxmox_sync_unexpected_status_handler () {
die "unexpected status: '%s'" "$1"
}
proxmox_sync_unexpected_status_handler=default_proxmox_sync_unexpected_status_handler
## Synchronous variant for when the `proxmox` function would just respond an
## UPID in the `data` JSON field.
proxmox_sync () (
proxmox_sync () {
local response upid status
response=$(proxmox "$@")
upid=$(echo "$response" | jq -r .data)
@ -124,10 +132,10 @@ proxmox_sync () (
case $status in
running) sleep 1 ;;
stopped) break ;;
*) die "unexpected status: '%s'" "$status" ;;
*) "$proxmox_sync_unexpected_status_handler" "$status" ;;
esac
done
)
}
################################################################################
## Grab VM options
@ -186,12 +194,27 @@ stop_vm () {
################################################################################
## Delete VM
proxmox_sync_unexpected_status_handler_ignore_null () {
case $1 in
null)
printf "Attempted to delete VM %s, but got 'null' status. Maybe the VM already does not exist?\n" \
"$vm_name"
exit 0
;;
*)
default_proxmox_sync_unexpected_status_handler "$1"
;;
esac
}
delete_vm () {
printf 'Deleting VM %s...\n' "$vm_name"
proxmox_sync_unexpected_status_handler=proxmox_sync_unexpected_status_handler_ignore_null
proxmox_sync DELETE "$api_url/nodes/$node/qemu/$vm_id" \
'destroy-unreferenced-disks'==1 \
'purge'==1
proxmox_sync_unexpected_status_handler=default_proxmox_sync_unexpected_status_handler
printf 'done deleting VM %s.\n' "$vm_name"
}