forked from Fediversity/Fediversity
Parallelise removal script
This commit is contained in:
parent
74bf29bb75
commit
94e5356886
|
@ -11,6 +11,9 @@ readonly apiurl=https://192.168.51.81:8006/api2/json
|
||||||
## now, let us just use `node051` everywhere.
|
## now, let us just use `node051` everywhere.
|
||||||
readonly node=node051
|
readonly node=node051
|
||||||
|
|
||||||
|
readonly tmpdir=/tmp/proxmox-provision-$RANDOM$RANDOM
|
||||||
|
mkdir $tmpdir
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
## Parse arguments
|
## Parse arguments
|
||||||
|
|
||||||
|
@ -35,7 +38,8 @@ Others:
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
die () { printf "$@"; printf '\n'; help; exit 2; }
|
die () { printf '\033[31m'; printf "$@"; printf '\033[0m\n'; exit 2; }
|
||||||
|
die_with_help () { printf '\033[31m'; printf "$@"; printf '\033[0m\n'; help; exit 2; }
|
||||||
|
|
||||||
while [ $# -gt 0 ]; do
|
while [ $# -gt 0 ]; do
|
||||||
argument=$1
|
argument=$1
|
||||||
|
@ -46,7 +50,7 @@ while [ $# -gt 0 ]; do
|
||||||
|
|
||||||
-h|-\?|--help) help; exit 0 ;;
|
-h|-\?|--help) help; exit 0 ;;
|
||||||
|
|
||||||
-*) die 'Unknown argument: `%s`.' "$argument" ;;
|
-*) die_with_help 'Unknown argument: `%s`.' "$argument" ;;
|
||||||
|
|
||||||
*) vmids="$vmids $argument" ;;
|
*) vmids="$vmids $argument" ;;
|
||||||
esac
|
esac
|
||||||
|
@ -56,15 +60,13 @@ if [ -z "$username" ] || [ -z "$password" ]; then
|
||||||
if [ -f .proxmox ]; then
|
if [ -f .proxmox ]; then
|
||||||
{ read username; read password; } < .proxmox
|
{ read username; read password; } < .proxmox
|
||||||
else
|
else
|
||||||
die 'Required: `--username` and `--password`.\n'
|
die_with_help 'Required: `--username` and `--password`.\n'
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
## Getting started
|
## Getting started
|
||||||
|
|
||||||
from_response () { echo "$response" | jq -r "$1"; }
|
|
||||||
|
|
||||||
printf 'Authenticating...'
|
printf 'Authenticating...'
|
||||||
response=$(
|
response=$(
|
||||||
http \
|
http \
|
||||||
|
@ -73,65 +75,89 @@ response=$(
|
||||||
"username=$username" \
|
"username=$username" \
|
||||||
"password=$password"
|
"password=$password"
|
||||||
)
|
)
|
||||||
readonly csrfToken=$(from_response .data.CSRFPreventionToken)
|
readonly ticket=$(echo "$response" | jq -r .data.ticket)
|
||||||
readonly ticket=$(from_response .data.ticket)
|
readonly csrfToken=$(echo "$response" | jq -r .data.CSRFPreventionToken)
|
||||||
printf ' done.\n'
|
printf ' done.\n'
|
||||||
|
|
||||||
|
acquire_lock () {
|
||||||
|
until mkdir $tmpdir/lock-$1 2>/dev/null; do sleep 1; done
|
||||||
|
}
|
||||||
|
release_lock () {
|
||||||
|
rmdir $tmpdir/lock-$1
|
||||||
|
}
|
||||||
|
|
||||||
proxmox () {
|
proxmox () {
|
||||||
response=$(
|
acquire_lock proxmox
|
||||||
http \
|
http \
|
||||||
--verify no \
|
--verify no \
|
||||||
--form \
|
--form \
|
||||||
"$@" \
|
"$@" \
|
||||||
"Cookie:PVEAuthCookie=$ticket" \
|
"Cookie:PVEAuthCookie=$ticket" \
|
||||||
"CSRFPreventionToken:$csrfToken"
|
"CSRFPreventionToken:$csrfToken"
|
||||||
)
|
release_lock proxmox
|
||||||
}
|
}
|
||||||
|
|
||||||
wait_ () {
|
## Synchronous variant for when the `proxmox` function would just respond an
|
||||||
upid=$1
|
## UPID in the `data` JSON field.
|
||||||
|
proxmox_sync () (
|
||||||
|
response=$(proxmox "$@")
|
||||||
|
upid=$(echo "$response" | jq -r .data)
|
||||||
|
|
||||||
while :; do
|
while :; do
|
||||||
proxmox GET $apiurl/nodes/$node/tasks/$upid/status
|
response=$(proxmox GET $apiurl/nodes/$node/tasks/$upid/status)
|
||||||
status=$(from_response .data.status)
|
status=$(echo "$response" | jq -r .data.status)
|
||||||
|
|
||||||
case $status in
|
case $status in
|
||||||
running) printf '.'; sleep 1 ;;
|
running) sleep 1 ;;
|
||||||
stopped) break ;;
|
stopped) break ;;
|
||||||
*) printf ' unexpected status: `%s`\n' "$status"; exit 2 ;;
|
*) die 'unexpected status: `%s`' "$status" ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
}
|
)
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
## Stop VM
|
## Stop VM
|
||||||
|
|
||||||
stop_vm () {
|
stop_vm () {
|
||||||
printf 'Stopping VM %d...' $1
|
printf 'Stopping VM %d...\n' $1
|
||||||
|
|
||||||
proxmox POST $apiurl/nodes/$node/qemu/$1/status/stop \
|
proxmox_sync POST $apiurl/nodes/$node/qemu/$1/status/stop \
|
||||||
'overrule-shutdown'==1
|
'overrule-shutdown'==1
|
||||||
|
|
||||||
wait_ $(from_response .data)
|
printf 'done stopping VM %d.\n' $1
|
||||||
printf ' done.\n'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
## Delete VM
|
## Delete VM
|
||||||
|
|
||||||
delete_vm () {
|
delete_vm () {
|
||||||
printf 'Deleting VM %d...' $1
|
printf 'Deleting VM %d...\n' $1
|
||||||
|
|
||||||
proxmox DELETE $apiurl/nodes/$node/qemu/$1 \
|
proxmox_sync DELETE $apiurl/nodes/$node/qemu/$1 \
|
||||||
'destroy-unreferenced-disks'==1 \
|
'destroy-unreferenced-disks'==1 \
|
||||||
'purge'==1
|
'purge'==1
|
||||||
|
|
||||||
wait_ $(from_response .data)
|
printf 'done deleting VM %d.\n' $1
|
||||||
printf ' done.\n'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
## Main loop
|
## Main loop
|
||||||
|
|
||||||
|
printf 'Removing VMs%s...\n' "$vmids"
|
||||||
|
|
||||||
|
remove_vm () {
|
||||||
|
stop_vm $1
|
||||||
|
delete_vm $1
|
||||||
|
}
|
||||||
|
|
||||||
for vmid in $vmids; do
|
for vmid in $vmids; do
|
||||||
stop_vm $vmid
|
remove_vm $vmid &
|
||||||
delete_vm $vmid
|
|
||||||
done
|
done
|
||||||
|
wait
|
||||||
|
|
||||||
|
printf 'done removing VMs%s.\n' "$vmids"
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
## Cleanup
|
||||||
|
|
||||||
|
rm -Rf $tmpdir
|
||||||
|
|
Loading…
Reference in a new issue