Have the provisioning script grab options from the flake

This commit is contained in:
Nicolas Jeannerod 2025-02-24 13:03:29 +01:00
parent 4fda719992
commit 4ae3fc27be
Signed by untrusted user: Niols
GPG key ID: 35DB9EC8886E1CB8

View file

@ -18,35 +18,23 @@ mkdir $tmpdir
api_url= api_url=
username= username=
password= password=
sockets=1 vm_names=
cores=1
memory=2048
vm_ids=
debug=false debug=false
help () { help () {
cat <<EOF cat <<EOF
Usage: $0 [OPTION...] ID [ID...] Usage: $0 [OPTION...] NAME [NAME...]
ID can be either: NAME is a string identifying the VM in the flake. This script will look for a
`vmOptions.<NAME>` and `nixosConfigurations.<NAME>` to get the informations that
- of the form INT:STR, in which case the integer will be taken as the Proxmox it needs.
id for the machine, and the string as its name in Proxmox and the name of
the NixOS configuration to install,
- an integer, in which case it will be taken as the Proxmox id for the
machine, and the name will be the id prefixed by 'fedi'.
Options: Options:
--api-url STR Base URL of the Proxmox API (required) --api-url STR Base URL of the Proxmox API (required)
--username STR Username, with provider (eg. niols@pve; required) --username STR Username, with provider (eg. niols@pve; required)
--password STR Password (required) --password STR Password (required)
--cores INT Number of cores (default: $cores)
--memory INT Memory (default: $memory)
--sockets INT Number of sockets (default: $sockets)
--debug Run this script in debug mode (default: $debug) --debug Run this script in debug mode (default: $debug)
-h|-?|--help Show this help and exit -h|-?|--help Show this help and exit
@ -54,7 +42,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
@ -82,32 +69,24 @@ while [ $# -gt 0 ]; do
--username) readonly username="$1"; shift ;; --username) readonly username="$1"; shift ;;
--password) readonly password="$1"; shift ;; --password) readonly password="$1"; shift ;;
--sockets) sockets=$1; shift ;;
--cores) cores=$1; shift ;;
--memory) memory=$1; shift ;;
--debug) debug=true ;; --debug) debug=true ;;
-h|-\?|--help) help; exit 0 ;; -h|-\?|--help) help; exit 0 ;;
-*) die_with_help "Unknown argument: '%s'." "$argument" ;; -*) die_with_help "Unknown argument: '%s'." "$argument" ;;
*) vm_ids="$vm_ids $argument" ;; *) vm_names="$vm_names $argument" ;;
esac esac
done done
if [ -z "$vm_ids" ]; then if [ -z "$vm_names" ]; then
die_with_help "Required: at least one VM id." die_with_help "Required: at least one VM name."
fi fi
if [ -z "$api_url" ] || [ -z "$username" ] || [ -z "$password" ]; then if [ -z "$api_url" ] || [ -z "$username" ] || [ -z "$password" ]; then
die_with_help "Required: '--api-url', '--username' and '--password'." die_with_help "Required: '--api-url', '--username' and '--password'."
fi fi
readonly sockets
readonly cores
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...'
@ -178,6 +157,19 @@ proxmox_sync () (
done done
) )
################################################################################
## Grab VM option
##
## Takes the name of the VM and an option and grabs `vmOptions.<name>.<option>`
## in the flake.
grab_vm_option () {
nix eval \
--impure --raw --expr "
builtins.toJSON (builtins.getFlake (builtins.toString ./.)).vmOptions.$1
" | jq -r ."$2"
}
################################################################################ ################################################################################
## Build ISO ## Build ISO
@ -188,6 +180,7 @@ build_iso () {
## FIXME: Support injecting host keys for test VMs (but not for production ## FIXME: Support injecting host keys for test VMs (but not for production
## VMs as that would be unsafe). ## VMs as that would be unsafe).
nix build \ nix build \
--impure --expr " --impure --expr "
let flake = builtins.getFlake (builtins.toString ./.); in let flake = builtins.getFlake (builtins.toString ./.); in
@ -259,12 +252,12 @@ 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=="$(grab_vm_option "$2" sockets)" \
cores=="$cores" \ cores=="$(grab_vm_option "$2" cores)" \
cpu==x86-64-v2-AES \ cpu==x86-64-v2-AES \
numa==1 \ numa==1 \
\ \
memory=="$memory" \ memory=="$(grab_vm_option "$2" memory)" \
\ \
net0=='virtio,bridge=vnet1306' net0=='virtio,bridge=vnet1306'
@ -310,10 +303,7 @@ start_vm () {
################################################################################ ################################################################################
## Main loop ## Main loop
printf 'Provisioning VMs%s with:\n' "$vm_ids" printf 'Provisioning VMs%s...\n' "$vm_names"
printf ' sockets: %d\n' "$sockets"
printf ' cores: %d\n' "$cores"
printf ' memory: %d\n' "$memory"
provision_vm () { provision_vm () {
build_iso "$@" build_iso "$@"
@ -324,18 +314,16 @@ provision_vm () {
remove_iso "$@" remove_iso "$@"
} }
for vm_id in $vm_ids; do for vm_name in $vm_names; do
vm_name=${vm_id#*:} vm_id=$(grab_vm_option "$vm_name" vmId)
vm_id=${vm_id%:*} if [ "$(grab_vm_option "$vm_name" proxmox)" != fediversity ]; then
if [ "$vm_id" = "$vm_name" ]; then die 'This script does not know how to provision things that are not Fediversity VMs'
vm_name=$(printf 'fedi%03d' "$vm_id")
fi fi
provision_vm "$vm_id" "$vm_name" & provision_vm "$vm_id" "$vm_name" &
done done
wait wait
printf 'done provisioning VMs%s.\n' "$vm_ids" printf 'done provisioning VMs%s.\n' "$vm_names"
################################################################################ ################################################################################
## Cleanup ## Cleanup