1
0
Fork 0

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=
username=
password=
sockets=1
cores=1
memory=2048
vm_ids=
vm_names=
debug=false
help () {
cat <<EOF
Usage: $0 [OPTION...] ID [ID...]
Usage: $0 [OPTION...] NAME [NAME...]
ID can be either:
- of the form INT:STR, in which case the integer will be taken as the Proxmox
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'.
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
it needs.
Options:
--api-url STR Base URL of the Proxmox API (required)
--username STR Username, with provider (eg. niols@pve; 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)
-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:
api_url=https://192.168.51.81:8006/api2/json
cores=7
username=mireille@pve
debug=true
@ -82,32 +69,24 @@ while [ $# -gt 0 ]; do
--username) readonly username="$1"; shift ;;
--password) readonly password="$1"; shift ;;
--sockets) sockets=$1; shift ;;
--cores) cores=$1; shift ;;
--memory) memory=$1; shift ;;
--debug) debug=true ;;
-h|-\?|--help) help; exit 0 ;;
-*) die_with_help "Unknown argument: '%s'." "$argument" ;;
*) vm_ids="$vm_ids $argument" ;;
*) vm_names="$vm_names $argument" ;;
esac
done
if [ -z "$vm_ids" ]; then
die_with_help "Required: at least one VM id."
if [ -z "$vm_names" ]; then
die_with_help "Required: at least one VM name."
fi
if [ -z "$api_url" ] || [ -z "$username" ] || [ -z "$password" ]; then
die_with_help "Required: '--api-url', '--username' and '--password'."
fi
readonly sockets
readonly cores
readonly memory
## FIXME: When we figure out how to use other nodes than node051.
# if [ -z "$node" ]; then
# printf 'Picking random node...'
@ -178,6 +157,19 @@ proxmox_sync () (
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
@ -188,6 +180,7 @@ build_iso () {
## FIXME: Support injecting host keys for test VMs (but not for production
## VMs as that would be unsafe).
nix build \
--impure --expr "
let flake = builtins.getFlake (builtins.toString ./.); in
@ -259,12 +252,12 @@ create_vm () {
scsihw==virtio-scsi-single \
scsi0=='linstor_storage:32,discard=on,ssd=on,iothread=on' \
\
sockets=="$sockets" \
cores=="$cores" \
sockets=="$(grab_vm_option "$2" sockets)" \
cores=="$(grab_vm_option "$2" cores)" \
cpu==x86-64-v2-AES \
numa==1 \
\
memory=="$memory" \
memory=="$(grab_vm_option "$2" memory)" \
\
net0=='virtio,bridge=vnet1306'
@ -310,10 +303,7 @@ start_vm () {
################################################################################
## Main loop
printf 'Provisioning VMs%s with:\n' "$vm_ids"
printf ' sockets: %d\n' "$sockets"
printf ' cores: %d\n' "$cores"
printf ' memory: %d\n' "$memory"
printf 'Provisioning VMs%s...\n' "$vm_names"
provision_vm () {
build_iso "$@"
@ -324,18 +314,16 @@ provision_vm () {
remove_iso "$@"
}
for vm_id in $vm_ids; do
vm_name=${vm_id#*:}
vm_id=${vm_id%:*}
if [ "$vm_id" = "$vm_name" ]; then
vm_name=$(printf 'fedi%03d' "$vm_id")
for vm_name in $vm_names; do
vm_id=$(grab_vm_option "$vm_name" vmId)
if [ "$(grab_vm_option "$vm_name" proxmox)" != fediversity ]; then
die 'This script does not know how to provision things that are not Fediversity VMs'
fi
provision_vm "$vm_id" "$vm_name" &
done
wait
printf 'done provisioning VMs%s.\n' "$vm_ids"
printf 'done provisioning VMs%s.\n' "$vm_names"
################################################################################
## Cleanup