Parallelise provisioning script

This commit is contained in:
Nicolas Jeannerod 2024-11-14 13:55:26 +01:00
parent 56d125a5b0
commit 74bf29bb75
Signed by untrusted user: Niols
GPG key ID: 35DB9EC8886E1CB8

View file

@ -46,7 +46,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
@ -61,7 +62,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
@ -71,7 +72,7 @@ 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
@ -103,35 +104,48 @@ readonly ticket=$(echo "$response" | jq -r .data.ticket)
readonly csrfToken=$(echo "$response" | jq -r .data.CSRFPreventionToken) 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 () {
acquire_lock proxmox
http \ http \
--form \ --form \
--verify no \ --verify no \
--ignore-stdin \
"$@" \ "$@" \
"Cookie:PVEAuthCookie=$ticket" \ "Cookie:PVEAuthCookie=$ticket" \
"CSRFPreventionToken:$csrfToken" "CSRFPreventionToken:$csrfToken"
release_lock proxmox
} }
## Synchronous variant for when the `proxmox` function would just respond an ## Synchronous variant for when the `proxmox` function would just respond an
## UPID in the `data` JSON field. ## UPID in the `data` JSON field.
proxmox_sync () { proxmox_sync () (
response=$(proxmox "$@") response=$(proxmox "$@")
upid=$(echo "$response" | jq -r .data) upid=$(echo "$response" | jq -r .data)
while :; do while :; do
response=$(proxmox GET $apiurl/nodes/$node/tasks/$upid/status) response=$(proxmox GET $apiurl/nodes/$node/tasks/$upid/status)
status=$(echo "$response" | jq -r .data.status) status=$(echo "$response" | jq -r .data.status)
case $status in case $status in
running) sleep 1 ;; running) sleep 1 ;;
stopped) break ;; stopped) break ;;
*) die 'unexpected status: `%s`' "$status" ;; *) die 'unexpected status: `%s`' "$status" ;;
esac esac
done done
} )
################################################################################ ################################################################################
## Build ISO ## Build ISO
build_iso () { build_iso () {
acquire_lock build
printf 'Building ISO for VM %d...\n' $1 printf 'Building ISO for VM %d...\n' $1
nix build \ nix build \
@ -142,12 +156,14 @@ build_iso () {
ln -sf $tmpdir/installer-fedi$1/iso/installer.iso $tmpdir/installer-fedi$1.iso ln -sf $tmpdir/installer-fedi$1/iso/installer.iso $tmpdir/installer-fedi$1.iso
printf 'done building ISO for VM %d.\n' $1 printf 'done building ISO for VM %d.\n' $1
release_lock build
} }
################################################################################ ################################################################################
## Upload ISO ## Upload ISO
upload_iso () { upload_iso () {
acquire_lock upload
printf 'Uploading ISO for VM %d...\n' $1 printf 'Uploading ISO for VM %d...\n' $1
proxmox_sync POST $apiurl/nodes/$node/storage/local/upload \ proxmox_sync POST $apiurl/nodes/$node/storage/local/upload \
@ -155,6 +171,7 @@ upload_iso () {
content==iso content==iso
printf 'done uploading ISO for VM %d.\n' $1 printf 'done uploading ISO for VM %d.\n' $1
release_lock upload
} }
################################################################################ ################################################################################
@ -201,7 +218,7 @@ create_vm () {
################################################################################ ################################################################################
## Install VM ## Install VM
install_vm () { install_vm () (
printf 'Installing VM %d...\n' $1 printf 'Installing VM %d...\n' $1
proxmox_sync POST $apiurl/nodes/$node/qemu/$1/status/start proxmox_sync POST $apiurl/nodes/$node/qemu/$1/status/start
@ -217,7 +234,7 @@ install_vm () {
done done
printf 'done installing VM %d.\n' $1 printf 'done installing VM %d.\n' $1
} )
################################################################################ ################################################################################
## Start VM ## Start VM
@ -252,8 +269,9 @@ provision_vm () {
} }
for vmid in $vmids; do for vmid in $vmids; do
provision_vm $vmid provision_vm $vmid &
done done
wait
printf 'done provisioning VMs%s.\n' "$vmids" printf 'done provisioning VMs%s.\n' "$vmids"