Fediversity/deployment/proxmox/remove.sh

177 lines
4.1 KiB
Bash
Executable file

#!/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=
help () {
cat <<EOF
Usage: $0 [OPTION...] ID [ID...]
Options:
--api-url STR Base URL of the Proxmox API (required)
--username STR Username, with provider (eg. niols@pve)
--password STR Password
-h|-?|--help Show this help and exit
Options can also be provided by adding assignments to a '.proxmox' file in the
current working directory. For instance, it could contain:
api_url=https://192.168.51.81:8006/api2/json
cores=7
username=mireille@pve
debug=true
Command line options take precedence over options found in the '.proxmox' file.
EOF
}
# shellcheck disable=SC2059
die () { printf '\033[31m'; printf "$@"; printf '\033[0m\n'; exit 2; }
# shellcheck disable=SC2059
die_with_help () { printf '\033[31m'; printf "$@"; printf '\033[0m\n'; help; exit 2; }
if [ -f .proxmox ]; then
. "$PWD"/.proxmox
fi
while [ $# -gt 0 ]; do
argument=$1
shift
case $argument in
--api-url|--api_url) readonly api_url="$1"; shift ;;
--username) readonly username=$1; shift ;;
--password) readonly password=$1; shift ;;
-h|-\?|--help) help; exit 0 ;;
-*) die_with_help "Unknown argument: '%s'." "$argument" ;;
*) vm_ids="$vm_ids $argument" ;;
esac
done
if [ -z "$vm_ids" ]; then
die_with_help "Required: at least one VM id.\n"
fi
if [ -z "$api_url" ] || [ -z "$username" ] || [ -z "$password" ]; then
die_with_help "Required: '--api-url', '--username' and '--password'."
fi
################################################################################
## Getting started
printf 'Authenticating...'
response=$(
http \
--verify no \
POST $api_url/access/ticket \
"username=$username" \
"password=$password"
)
ticket=$(echo "$response" | jq -r .data.ticket)
readonly ticket
csrf_token=$(echo "$response" | jq -r .data.CSRFPreventionToken)
readonly csrf_token
printf ' done.\n'
acquire_lock () {
until mkdir "$tmpdir/lock-$1" 2>/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
)
################################################################################
## Stop VM
stop_vm () {
printf 'Stopping VM %d...\n' "$1"
proxmox_sync POST "$api_url/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 "$api_url/nodes/$node/qemu/$1" \
'destroy-unreferenced-disks'==1 \
'purge'==1
printf 'done deleting VM %d.\n' "$1"
}
################################################################################
## Main loop
printf 'Removing VMs%s...\n' "$vm_ids"
remove_vm () {
stop_vm "$1"
delete_vm "$1"
}
for vm_id in $vm_ids; do
remove_vm "$vm_id" &
done
wait
printf 'done removing VMs%s.\n' "$vm_ids"
################################################################################
## Cleanup
rm -Rf $tmpdir