forked from Fediversity/Fediversity
Make removal script support ids or names
This commit is contained in:
parent
fb5bed9042
commit
bc8ad08228
1 changed files with 61 additions and 21 deletions
|
@ -18,11 +18,11 @@ mkdir $tmpdir
|
||||||
api_url=
|
api_url=
|
||||||
username=
|
username=
|
||||||
password=
|
password=
|
||||||
vm_ids=
|
vm_ids_or_names=
|
||||||
|
|
||||||
help () {
|
help () {
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
Usage: $0 [OPTION...] ID [ID...]
|
Usage: $0 [OPTION...] ID_OR_NAME [ID_OR_NAME...]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
--api-url STR Base URL of the Proxmox API (required)
|
--api-url STR Base URL of the Proxmox API (required)
|
||||||
|
@ -35,7 +35,6 @@ Options can also be provided by adding assignments to a '.proxmox' file in the
|
||||||
current working directory. For instance, it could contain:
|
current working directory. For instance, it could contain:
|
||||||
|
|
||||||
api_url=https://192.168.51.81:8006/api2/json
|
api_url=https://192.168.51.81:8006/api2/json
|
||||||
cores=7
|
|
||||||
username=mireille@pve
|
username=mireille@pve
|
||||||
debug=true
|
debug=true
|
||||||
|
|
||||||
|
@ -64,12 +63,12 @@ while [ $# -gt 0 ]; do
|
||||||
|
|
||||||
-*) die_with_help "Unknown argument: '%s'." "$argument" ;;
|
-*) die_with_help "Unknown argument: '%s'." "$argument" ;;
|
||||||
|
|
||||||
*) vm_ids="$vm_ids $argument" ;;
|
*) vm_ids_or_names="$vm_ids_or_names $argument" ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ -z "$vm_ids" ]; then
|
if [ -z "$vm_ids_or_names" ]; then
|
||||||
die_with_help "Required: at least one VM id.\n"
|
die_with_help "Required: at least one VM id or name.\n"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -z "$api_url" ] || [ -z "$username" ] || [ -z "$password" ]; then
|
if [ -z "$api_url" ] || [ -z "$username" ] || [ -z "$password" ]; then
|
||||||
|
@ -129,48 +128,89 @@ proxmox_sync () (
|
||||||
done
|
done
|
||||||
)
|
)
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
## Grab VM options
|
||||||
|
##
|
||||||
|
## Takes the name of the VM, grabs `.#vmOptions.<name>` 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
|
||||||
|
|
||||||
stop_vm () {
|
stop_vm () {
|
||||||
printf 'Stopping VM %d...\n' "$1"
|
printf 'Stopping VM %s...\n' "$vm_name"
|
||||||
|
|
||||||
proxmox_sync POST "$api_url/nodes/$node/qemu/$1/status/stop" \
|
proxmox_sync POST "$api_url/nodes/$node/qemu/$vm_id/status/stop" \
|
||||||
'overrule-shutdown'==1
|
'overrule-shutdown'==1
|
||||||
|
|
||||||
printf 'done stopping VM %d.\n' "$1"
|
printf 'done stopping VM %s.\n' "$vm_name"
|
||||||
}
|
}
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
## Delete VM
|
## Delete VM
|
||||||
|
|
||||||
delete_vm () {
|
delete_vm () {
|
||||||
printf 'Deleting VM %d...\n' "$1"
|
printf 'Deleting VM %s...\n' "$vm_name"
|
||||||
|
|
||||||
proxmox_sync DELETE "$api_url/nodes/$node/qemu/$1" \
|
proxmox_sync DELETE "$api_url/nodes/$node/qemu/$vm_id" \
|
||||||
'destroy-unreferenced-disks'==1 \
|
'destroy-unreferenced-disks'==1 \
|
||||||
'purge'==1
|
'purge'==1
|
||||||
|
|
||||||
printf 'done deleting VM %d.\n' "$1"
|
printf 'done deleting VM %s.\n' "$vm_name"
|
||||||
}
|
}
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
## Main loop
|
## Main loop
|
||||||
|
|
||||||
printf 'Removing VMs%s...\n' "$vm_ids"
|
printf 'Removing VMs%s...\n' "$vm_ids_or_names"
|
||||||
|
|
||||||
remove_vm () {
|
remove_vm () (
|
||||||
stop_vm "$1"
|
grab_vm_options "$1"
|
||||||
delete_vm "$1"
|
stop_vm
|
||||||
}
|
delete_vm
|
||||||
|
)
|
||||||
|
|
||||||
for vm_id in $vm_ids; do
|
for vm_id_or_name in $vm_ids_or_names; do
|
||||||
vm_id=${vm_id%:*}
|
remove_vm "$vm_id_or_name" &
|
||||||
remove_vm "$vm_id" &
|
|
||||||
done
|
done
|
||||||
wait
|
wait
|
||||||
|
|
||||||
printf 'done removing VMs%s.\n' "$vm_ids"
|
printf 'done removing VMs%s.\n' "$vm_ids_or_names"
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
## Cleanup
|
## Cleanup
|
||||||
|
|
Loading…
Add table
Reference in a new issue