bash scripts: snake-case variables, deduplicate $RANDOM, satisfy LSP

Co-authored-by: Nicolas “Niols” Jeannerod <nicolas.jeannerod@moduscreate.com>
This commit is contained in:
kiara Grouwstra 2025-02-19 09:43:19 +01:00 committed by Nicolas “Niols” Jeannerod
parent 59122901ce
commit 807808ed00
Signed by untrusted user: Niols
GPG key ID: 35DB9EC8886E1CB8
2 changed files with 97 additions and 89 deletions

View file

@ -1,17 +1,17 @@
#!/usr/bin/env sh #!/usr/bin/env bash
set -euC set -euC
################################################################################ ################################################################################
## Constants ## Constants
readonly apiurl=https://192.168.51.81:8006/api2/json readonly api_url=https://192.168.51.81:8006/api2/json
## FIXME: There seems to be a problem with file upload where the task is ## 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 ## registered to `node051` no matter what node we are actually uploading to? For
## 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 readonly tmpdir=/tmp/proxmox-provision-$RANDOM
mkdir $tmpdir mkdir $tmpdir
################################################################################ ################################################################################
@ -22,7 +22,7 @@ password=
sockets=1 sockets=1
cores=1 cores=1
memory=2048 memory=2048
vmids= vm_ids=
help () { help () {
cat <<EOF cat <<EOF
@ -46,15 +46,17 @@ Others:
EOF EOF
} }
# shellcheck disable=SC2059
die () { printf '\033[31m'; printf "$@"; printf '\033[0m\n'; exit 2; } 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; } 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
shift shift
case $argument in case $argument in
--username) readonly username=$1; shift ;; --username) readonly username="$1"; shift ;;
--password) readonly password=$1; shift ;; --password) readonly password="$1"; shift ;;
--sockets) sockets=$1; shift ;; --sockets) sockets=$1; shift ;;
--cores) cores=$1; shift ;; --cores) cores=$1; shift ;;
@ -62,17 +64,17 @@ while [ $# -gt 0 ]; do
-h|-\?|--help) help; exit 0 ;; -h|-\?|--help) help; exit 0 ;;
-*) die_with_help 'Unknown argument: `%s`.' "$argument" ;; -*) die_with_help "Unknown argument: '%s'." "$argument" ;;
*) vmids="$vmids $argument" ;; *) vm_ids="$vm_ids $argument" ;;
esac esac
done done
if [ -z "$username" ] || [ -z "$password" ]; then if [ -z "$username" ] || [ -z "$password" ]; then
if [ -f .proxmox ]; then if [ -f .proxmox ]; then
{ read username; read password; } < .proxmox { read -r username; read -r password; } < .proxmox
else else
die_with_help 'Required: `--username` and `--password`.\n' die_with_help "Required: '--username' and '--password'.\n"
fi fi
fi fi
@ -83,9 +85,9 @@ readonly memory
## FIXME: When we figure out how to use other nodes than node051. ## FIXME: When we figure out how to use other nodes than node051.
# if [ -z "$node" ]; then # if [ -z "$node" ]; then
# printf 'Picking random node...' # printf 'Picking random node...'
# proxmox GET $apiurl/nodes # proxmox GET $api_url/nodes
# node=$(from_response .data[].node | sort -R | head -n 1) # node=$(from_response .data[].node | sort -R | head -n 1)
# printf ' done. Picked `%s`.\n' "$node" # printf " done. Picked '%s'.\n" "$node"
# fi # fi
# readonly node # readonly node
@ -96,19 +98,21 @@ printf 'Authenticating...'
response=$( response=$(
http \ http \
--verify no \ --verify no \
POST $apiurl/access/ticket \ POST $api_url/access/ticket \
"username=$username" \ "username=$username" \
"password=$password" "password=$password"
) )
readonly ticket=$(echo "$response" | jq -r .data.ticket) ticket=$(echo "$response" | jq -r .data.ticket)
readonly csrfToken=$(echo "$response" | jq -r .data.CSRFPreventionToken) readonly ticket
csrf_token=$(echo "$response" | jq -r .data.CSRFPreventionToken)
readonly csrf_token
printf ' done.\n' printf ' done.\n'
acquire_lock () { acquire_lock () {
until mkdir $tmpdir/lock-$1 2>/dev/null; do sleep 1; done until mkdir "$tmpdir/lock-$1" 2>/dev/null; do sleep 1; done
} }
release_lock () { release_lock () {
rmdir $tmpdir/lock-$1 rmdir "$tmpdir/lock-$1"
} }
proxmox () { proxmox () {
@ -119,7 +123,7 @@ proxmox () {
--ignore-stdin \ --ignore-stdin \
"$@" \ "$@" \
"Cookie:PVEAuthCookie=$ticket" \ "Cookie:PVEAuthCookie=$ticket" \
"CSRFPreventionToken:$csrfToken" "CSRFPreventionToken:$csrf_token"
release_lock proxmox release_lock proxmox
} }
@ -130,13 +134,13 @@ proxmox_sync () (
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 "$api_url/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
) )
@ -146,16 +150,16 @@ proxmox_sync () (
build_iso () { build_iso () {
acquire_lock build acquire_lock build
printf 'Building ISO for VM %d...\n' $1 printf 'Building ISO for VM %d...\n' "$1"
nix build \ nix build \
.#isoInstallers.provisioning.fedi$1 \ ".#isoInstallers.provisioning.fedi$1" \
--log-format raw --quiet \ --log-format raw --quiet \
--out-link $tmpdir/installer-fedi$1 --out-link "$tmpdir/installer-fedi$1"
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 release_lock build
} }
@ -164,13 +168,13 @@ build_iso () {
upload_iso () { upload_iso () {
acquire_lock upload 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"$api_url/nodes/$node/storage/local/upload" \
filename@$tmpdir/installer-fedi$1.iso \ "filename@$tmpdir/installer-fedi$1.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 release_lock upload
} }
@ -178,22 +182,22 @@ upload_iso () {
## Remove ISO ## Remove ISO
remove_iso () { remove_iso () {
printf 'Removing ISO for VM %d...\n' $1 printf 'Removing ISO for VM %d...\n' "$1"
proxmox_sync DELETE $apiurl/nodes/$node/storage/local/content/local:iso/installer-fedi$1.iso proxmox_sync DELETE "$api_url/nodes/$node/storage/local/content/local:iso/installer-fedi$1.iso"
printf 'done removing ISO for VM %d.\n' $1 printf 'done removing ISO for VM %d.\n' "$1"
} }
################################################################################ ################################################################################
## Create VM ## Create VM
create_vm () { create_vm () {
printf 'Creating VM %d...\n' $1 printf 'Creating VM %d...\n' "$1"
proxmox_sync POST $apiurl/nodes/$node/qemu \ proxmox_sync POST "$api_url/nodes/$node/qemu" \
\ \
vmid==$1 \ vm_id=="$1" \
name=="fedi$1" \ name=="fedi$1" \
pool==Fediversity \ pool==Fediversity \
\ \
@ -207,77 +211,77 @@ create_vm () {
scsihw==virtio-scsi-single \ scsihw==virtio-scsi-single \
scsi0=='linstor_storage:32,discard=on,ssd=on,iothread=on' \ scsi0=='linstor_storage:32,discard=on,ssd=on,iothread=on' \
\ \
sockets==$sockets \ sockets=="$sockets" \
cores==$cores \ cores=="$cores" \
cpu==x86-64-v2-AES \ cpu==x86-64-v2-AES \
numa==1 \ numa==1 \
\ \
memory==$memory \ memory=="$memory" \
\ \
net0=='virtio,bridge=vnet1306' net0=='virtio,bridge=vnet1306'
printf 'done creating VM %d.\n' $1 printf 'done creating VM %d.\n' "$1"
} }
################################################################################ ################################################################################
## 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 "$api_url/nodes/$node/qemu/$1/status/start"
while :; do while :; do
response=$(proxmox GET $apiurl/nodes/$node/qemu/$1/status/current) response=$(proxmox GET "$api_url/nodes/$node/qemu/$1/status/current")
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 ;;
*) printf ' unexpected status: `%s`\n' "$status"; exit 2 ;; *) printf " unexpected status: '%s'\n" "$status"; exit 2 ;;
esac esac
done done
printf 'done installing VM %d.\n' $1 printf 'done installing VM %d.\n' "$1"
) )
################################################################################ ################################################################################
## Start VM ## Start VM
start_vm () { start_vm () {
printf 'Starting VM %d...\n' $1 printf 'Starting VM %d...\n' "$1"
proxmox_sync POST $apiurl/nodes/$node/qemu/$1/config \ proxmox_sync POST "$api_url/nodes/$node/qemu/$1/config" \
ide2=='none,media=cdrom' \ ide2=='none,media=cdrom' \
net0=='virtio,bridge=vnet1305' net0=='virtio,bridge=vnet1305'
proxmox_sync POST $apiurl/nodes/$node/qemu/$1/status/start proxmox_sync POST "$api_url/nodes/$node/qemu/$1/status/start"
printf 'done starting VM %d.\n' $1 printf 'done starting VM %d.\n' "$1"
} }
################################################################################ ################################################################################
## Main loop ## Main loop
printf 'Provisioning VMs%s with:\n' "$vmids" printf 'Provisioning VMs%s with:\n' "$vm_ids"
printf ' sockets: %d\n' $sockets printf ' sockets: %d\n' "$sockets"
printf ' cores: %d\n' $cores printf ' cores: %d\n' "$cores"
printf ' memory: %d\n' $memory printf ' memory: %d\n' "$memory"
provision_vm () { provision_vm () {
build_iso $1 build_iso "$1"
upload_iso $1 upload_iso "$1"
create_vm $1 create_vm "$1"
install_vm $1 install_vm "$1"
start_vm $1 start_vm "$1"
remove_iso $1 remove_iso "$1"
} }
for vmid in $vmids; do for vm_id in $vm_ids; do
provision_vm $vmid & provision_vm "$vm_id" &
done done
wait wait
printf 'done provisioning VMs%s.\n' "$vmids" printf 'done provisioning VMs%s.\n' "$vm_ids"
################################################################################ ################################################################################
## Cleanup ## Cleanup

View file

@ -1,17 +1,17 @@
#!/usr/bin/env sh #!/usr/bin/env bash
set -euC set -euC
################################################################################ ################################################################################
## Constants ## Constants
readonly apiurl=https://192.168.51.81:8006/api2/json readonly api_url=https://192.168.51.81:8006/api2/json
## FIXME: There seems to be a problem with file upload where the task is ## 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 ## registered to `node051` no matter what node we are actually uploading to? For
## 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 readonly tmpdir=/tmp/proxmox-provision-$RANDOM
mkdir $tmpdir mkdir $tmpdir
################################################################################ ################################################################################
@ -19,7 +19,7 @@ mkdir $tmpdir
username= username=
password= password=
vmids= vm_ids=
help () { help () {
cat <<EOF cat <<EOF
@ -38,7 +38,9 @@ Others:
EOF EOF
} }
# shellcheck disable=SC2059
die () { printf '\033[31m'; printf "$@"; printf '\033[0m\n'; exit 2; } 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; } die_with_help () { printf '\033[31m'; printf "$@"; printf '\033[0m\n'; help; exit 2; }
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
@ -50,17 +52,17 @@ while [ $# -gt 0 ]; do
-h|-\?|--help) help; exit 0 ;; -h|-\?|--help) help; exit 0 ;;
-*) die_with_help 'Unknown argument: `%s`.' "$argument" ;; -*) die_with_help "Unknown argument: '%s'." "$argument" ;;
*) vmids="$vmids $argument" ;; *) vm_ids="$vm_ids $argument" ;;
esac esac
done done
if [ -z "$username" ] || [ -z "$password" ]; then if [ -z "$username" ] || [ -z "$password" ]; then
if [ -f .proxmox ]; then if [ -f .proxmox ]; then
{ read username; read password; } < .proxmox { read -r username; read -r password; } < .proxmox
else else
die_with_help 'Required: `--username` and `--password`.\n' die_with_help "Required: '--username' and '--password'.\n"
fi fi
fi fi
@ -71,19 +73,21 @@ printf 'Authenticating...'
response=$( response=$(
http \ http \
--verify no \ --verify no \
POST $apiurl/access/ticket \ POST $api_url/access/ticket \
"username=$username" \ "username=$username" \
"password=$password" "password=$password"
) )
readonly ticket=$(echo "$response" | jq -r .data.ticket) ticket=$(echo "$response" | jq -r .data.ticket)
readonly csrfToken=$(echo "$response" | jq -r .data.CSRFPreventionToken) readonly ticket
csrf_token=$(echo "$response" | jq -r .data.CSRFPreventionToken)
readonly csrf_token
printf ' done.\n' printf ' done.\n'
acquire_lock () { acquire_lock () {
until mkdir $tmpdir/lock-$1 2>/dev/null; do sleep 1; done until mkdir "$tmpdir/lock-$1" 2>/dev/null; do sleep 1; done
} }
release_lock () { release_lock () {
rmdir $tmpdir/lock-$1 rmdir "$tmpdir/lock-$1"
} }
proxmox () { proxmox () {
@ -93,7 +97,7 @@ proxmox () {
--form \ --form \
"$@" \ "$@" \
"Cookie:PVEAuthCookie=$ticket" \ "Cookie:PVEAuthCookie=$ticket" \
"CSRFPreventionToken:$csrfToken" "CSRFPreventionToken:$csrf_token"
release_lock proxmox release_lock proxmox
} }
@ -104,13 +108,13 @@ proxmox_sync () (
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 "$api_url/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
) )
@ -119,43 +123,43 @@ proxmox_sync () (
## Stop VM ## Stop VM
stop_vm () { stop_vm () {
printf 'Stopping VM %d...\n' $1 printf 'Stopping VM %d...\n' "$1"
proxmox_sync POST $apiurl/nodes/$node/qemu/$1/status/stop \ proxmox_sync POST "$api_url/nodes/$node/qemu/$1/status/stop" \
'overrule-shutdown'==1 'overrule-shutdown'==1
printf 'done stopping VM %d.\n' $1 printf 'done stopping VM %d.\n' "$1"
} }
################################################################################ ################################################################################
## Delete VM ## Delete VM
delete_vm () { delete_vm () {
printf 'Deleting VM %d...\n' $1 printf 'Deleting VM %d...\n' "$1"
proxmox_sync DELETE $apiurl/nodes/$node/qemu/$1 \ proxmox_sync DELETE "$api_url/nodes/$node/qemu/$1" \
'destroy-unreferenced-disks'==1 \ 'destroy-unreferenced-disks'==1 \
'purge'==1 'purge'==1
printf 'done deleting VM %d.\n' $1 printf 'done deleting VM %d.\n' "$1"
} }
################################################################################ ################################################################################
## Main loop ## Main loop
printf 'Removing VMs%s...\n' "$vmids" printf 'Removing VMs%s...\n' "$vm_ids"
remove_vm () { remove_vm () {
stop_vm $1 stop_vm "$1"
delete_vm $1 delete_vm "$1"
} }
for vmid in $vmids; do for vm_id in $vm_ids; do
remove_vm $vmid & remove_vm "$vm_id" &
done done
wait wait
printf 'done removing VMs%s.\n' "$vmids" printf 'done removing VMs%s.\n' "$vm_ids"
################################################################################ ################################################################################
## Cleanup ## Cleanup