2025-02-19 09:43:19 +01:00
|
|
|
#!/usr/bin/env bash
|
2024-11-14 11:43:49 +01:00
|
|
|
set -euC
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
## Constants
|
|
|
|
|
2025-02-19 09:43:19 +01:00
|
|
|
readonly api_url=https://192.168.51.81:8006/api2/json
|
2024-11-14 11:43:49 +01:00
|
|
|
|
|
|
|
## 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
|
|
|
|
|
2025-02-19 09:43:19 +01:00
|
|
|
readonly tmpdir=/tmp/proxmox-provision-$RANDOM
|
2024-11-14 16:24:52 +01:00
|
|
|
mkdir $tmpdir
|
|
|
|
|
2024-11-14 11:43:49 +01:00
|
|
|
################################################################################
|
|
|
|
## Parse arguments
|
|
|
|
|
|
|
|
username=
|
|
|
|
password=
|
2025-02-19 09:43:19 +01:00
|
|
|
vm_ids=
|
2024-11-14 11:43:49 +01:00
|
|
|
|
|
|
|
help () {
|
|
|
|
cat <<EOF
|
|
|
|
Usage: $0 [OPTION...] [ID...]
|
|
|
|
|
|
|
|
Authentication options:
|
|
|
|
--username STR Username, with provider (eg. niols@pve)
|
|
|
|
--password STR Password
|
|
|
|
|
|
|
|
If not provided via the command line, username and password will be looked for
|
|
|
|
in a '.proxmox' file in the current working directory, the username on the
|
|
|
|
first line, and the password on the second.
|
|
|
|
|
|
|
|
Others:
|
|
|
|
-h|-?|--help Show this help and exit
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2025-02-19 09:43:19 +01:00
|
|
|
# shellcheck disable=SC2059
|
2024-11-14 16:24:52 +01:00
|
|
|
die () { printf '\033[31m'; printf "$@"; printf '\033[0m\n'; exit 2; }
|
2025-02-19 09:43:19 +01:00
|
|
|
# shellcheck disable=SC2059
|
2024-11-14 16:24:52 +01:00
|
|
|
die_with_help () { printf '\033[31m'; printf "$@"; printf '\033[0m\n'; help; exit 2; }
|
2024-11-14 11:43:49 +01:00
|
|
|
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
argument=$1
|
|
|
|
shift
|
|
|
|
case $argument in
|
|
|
|
--username) readonly username=$1; shift ;;
|
|
|
|
--password) readonly password=$1; shift ;;
|
|
|
|
|
|
|
|
-h|-\?|--help) help; exit 0 ;;
|
|
|
|
|
2025-02-19 09:43:19 +01:00
|
|
|
-*) die_with_help "Unknown argument: '%s'." "$argument" ;;
|
2024-11-14 11:43:49 +01:00
|
|
|
|
2025-02-19 09:43:19 +01:00
|
|
|
*) vm_ids="$vm_ids $argument" ;;
|
2024-11-14 11:43:49 +01:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z "$username" ] || [ -z "$password" ]; then
|
|
|
|
if [ -f .proxmox ]; then
|
2025-02-19 09:43:19 +01:00
|
|
|
{ read -r username; read -r password; } < .proxmox
|
2024-11-14 11:43:49 +01:00
|
|
|
else
|
2025-02-19 09:43:19 +01:00
|
|
|
die_with_help "Required: '--username' and '--password'.\n"
|
2024-11-14 11:43:49 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
## Getting started
|
|
|
|
|
|
|
|
printf 'Authenticating...'
|
|
|
|
response=$(
|
|
|
|
http \
|
|
|
|
--verify no \
|
2025-02-19 09:43:19 +01:00
|
|
|
POST $api_url/access/ticket \
|
2024-11-14 11:43:49 +01:00
|
|
|
"username=$username" \
|
|
|
|
"password=$password"
|
|
|
|
)
|
2025-02-19 09:43:19 +01:00
|
|
|
ticket=$(echo "$response" | jq -r .data.ticket)
|
|
|
|
readonly ticket
|
|
|
|
csrf_token=$(echo "$response" | jq -r .data.CSRFPreventionToken)
|
|
|
|
readonly csrf_token
|
2024-11-14 11:43:49 +01:00
|
|
|
printf ' done.\n'
|
|
|
|
|
2024-11-14 16:24:52 +01:00
|
|
|
acquire_lock () {
|
2025-02-19 09:43:19 +01:00
|
|
|
until mkdir "$tmpdir/lock-$1" 2>/dev/null; do sleep 1; done
|
2024-11-14 16:24:52 +01:00
|
|
|
}
|
|
|
|
release_lock () {
|
2025-02-19 09:43:19 +01:00
|
|
|
rmdir "$tmpdir/lock-$1"
|
2024-11-14 16:24:52 +01:00
|
|
|
}
|
|
|
|
|
2024-11-14 11:43:49 +01:00
|
|
|
proxmox () {
|
2024-11-14 16:24:52 +01:00
|
|
|
acquire_lock proxmox
|
|
|
|
http \
|
|
|
|
--verify no \
|
|
|
|
--form \
|
|
|
|
"$@" \
|
|
|
|
"Cookie:PVEAuthCookie=$ticket" \
|
2025-02-19 09:43:19 +01:00
|
|
|
"CSRFPreventionToken:$csrf_token"
|
2024-11-14 16:24:52 +01:00
|
|
|
release_lock proxmox
|
2024-11-14 11:43:49 +01:00
|
|
|
}
|
|
|
|
|
2024-11-14 16:24:52 +01:00
|
|
|
## 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)
|
|
|
|
|
2024-11-14 11:43:49 +01:00
|
|
|
while :; do
|
2025-02-19 09:43:19 +01:00
|
|
|
response=$(proxmox GET "$api_url/nodes/$node/tasks/$upid/status")
|
2024-11-14 16:24:52 +01:00
|
|
|
status=$(echo "$response" | jq -r .data.status)
|
|
|
|
|
2024-11-14 11:43:49 +01:00
|
|
|
case $status in
|
2024-11-14 16:24:52 +01:00
|
|
|
running) sleep 1 ;;
|
2024-11-14 11:43:49 +01:00
|
|
|
stopped) break ;;
|
2025-02-19 09:43:19 +01:00
|
|
|
*) die "unexpected status: '%s'" "$status" ;;
|
2024-11-14 11:43:49 +01:00
|
|
|
esac
|
|
|
|
done
|
2024-11-14 16:24:52 +01:00
|
|
|
)
|
2024-11-14 11:43:49 +01:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
## Stop VM
|
|
|
|
|
|
|
|
stop_vm () {
|
2025-02-19 09:43:19 +01:00
|
|
|
printf 'Stopping VM %d...\n' "$1"
|
2024-11-14 11:43:49 +01:00
|
|
|
|
2025-02-19 09:43:19 +01:00
|
|
|
proxmox_sync POST "$api_url/nodes/$node/qemu/$1/status/stop" \
|
2024-11-14 11:43:49 +01:00
|
|
|
'overrule-shutdown'==1
|
|
|
|
|
2025-02-19 09:43:19 +01:00
|
|
|
printf 'done stopping VM %d.\n' "$1"
|
2024-11-14 11:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
## Delete VM
|
|
|
|
|
|
|
|
delete_vm () {
|
2025-02-19 09:43:19 +01:00
|
|
|
printf 'Deleting VM %d...\n' "$1"
|
2024-11-14 11:43:49 +01:00
|
|
|
|
2025-02-19 09:43:19 +01:00
|
|
|
proxmox_sync DELETE "$api_url/nodes/$node/qemu/$1" \
|
2024-11-14 11:43:49 +01:00
|
|
|
'destroy-unreferenced-disks'==1 \
|
|
|
|
'purge'==1
|
|
|
|
|
2025-02-19 09:43:19 +01:00
|
|
|
printf 'done deleting VM %d.\n' "$1"
|
2024-11-14 11:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
## Main loop
|
|
|
|
|
2025-02-19 09:43:19 +01:00
|
|
|
printf 'Removing VMs%s...\n' "$vm_ids"
|
2024-11-14 16:24:52 +01:00
|
|
|
|
|
|
|
remove_vm () {
|
2025-02-19 09:43:19 +01:00
|
|
|
stop_vm "$1"
|
|
|
|
delete_vm "$1"
|
2024-11-14 16:24:52 +01:00
|
|
|
}
|
|
|
|
|
2025-02-19 09:43:19 +01:00
|
|
|
for vm_id in $vm_ids; do
|
|
|
|
remove_vm "$vm_id" &
|
2024-11-14 11:43:49 +01:00
|
|
|
done
|
2024-11-14 16:24:52 +01:00
|
|
|
wait
|
|
|
|
|
2025-02-19 09:43:19 +01:00
|
|
|
printf 'done removing VMs%s.\n' "$vm_ids"
|
2024-11-14 16:24:52 +01:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
## Cleanup
|
|
|
|
|
|
|
|
rm -Rf $tmpdir
|