Decouple id and name

This commit is contained in:
Nicolas Jeannerod 2025-02-21 11:08:06 +01:00
parent d77b04ec18
commit f8ec8e7d93
Signed by untrusted user: Niols
GPG key ID: 35DB9EC8886E1CB8
2 changed files with 44 additions and 28 deletions

View file

@ -29,6 +29,15 @@ help () {
cat <<EOF cat <<EOF
Usage: $0 [OPTION...] ID [ID...] Usage: $0 [OPTION...] ID [ID...]
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'.
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)
@ -174,28 +183,28 @@ 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 %s...\n' "$2"
nix build \ nix build \
--impure --expr " --impure --expr "
let flake = builtins.getFlake (builtins.toString ./.); in let flake = builtins.getFlake (builtins.toString ./.); in
flake.lib.makeInstallerIso { flake.lib.makeInstallerIso {
nixosConfiguration = flake.nixosConfigurations.provisioning.fedi$1; nixosConfiguration = flake.nixosConfigurations.$2;
nixpkgs = flake.inputs.nixpkgs; nixpkgs = flake.inputs.nixpkgs;
hostKeys = { hostKeys = {
ed25519 = { ed25519 = {
private = ./deployment/hostKeys/fedi$1/ssh_host_ed25519_key; private = ./deployment/hostKeys/$2/ssh_host_ed25519_key;
public = ./deployment/hostKeys/fedi$1/ssh_host_ed25519_key.pub; public = ./deployment/hostKeys/$2/ssh_host_ed25519_key.pub;
}; };
}; };
} }
" \ " \
--log-format raw --quiet \ --log-format raw --quiet \
--out-link "$tmpdir/installer-fedi$1" --out-link "$tmpdir/installer-$2"
ln -sf "$tmpdir/installer-fedi$1/iso/installer.iso" "$tmpdir/installer-fedi$1.iso" ln -sf "$tmpdir/installer-$2/iso/installer.iso" "$tmpdir/installer-$2.iso"
printf 'done building ISO for VM %d.\n' "$1" printf 'done building ISO for VM %s.\n' "$2"
release_lock build release_lock build
} }
@ -204,13 +213,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 %s...\n' "$2"
proxmox_sync POST "$api_url/nodes/$node/storage/local/upload" \ proxmox_sync POST "$api_url/nodes/$node/storage/local/upload" \
"filename@$tmpdir/installer-fedi$1.iso" \ "filename@$tmpdir/installer-$2.iso" \
content==iso content==iso
printf 'done uploading ISO for VM %d.\n' "$1" printf 'done uploading ISO for VM %s.\n' "$2"
release_lock upload release_lock upload
} }
@ -218,26 +227,26 @@ upload_iso () {
## Remove ISO ## Remove ISO
remove_iso () { remove_iso () {
printf 'Removing ISO for VM %d...\n' "$1" printf 'Removing ISO for VM %s...\n' "$2"
proxmox_sync DELETE "$api_url/nodes/$node/storage/local/content/local:iso/installer-fedi$1.iso" proxmox_sync DELETE "$api_url/nodes/$node/storage/local/content/local:iso/installer-$2.iso"
printf 'done removing ISO for VM %d.\n' "$1" printf 'done removing ISO for VM %s.\n' "$2"
} }
################################################################################ ################################################################################
## Create VM ## Create VM
create_vm () { create_vm () {
printf 'Creating VM %d...\n' "$1" printf 'Creating VM %s with id %d...\n' "$2" "$1"
proxmox_sync POST "$api_url/nodes/$node/qemu" \ proxmox_sync POST "$api_url/nodes/$node/qemu" \
\ \
vmid=="$1" \ vmid=="$1" \
name=="fedi$1" \ name=="$2" \
pool==Fediversity \ pool==Fediversity \
\ \
ide2=="local:iso/installer-fedi$1.iso,media=cdrom" \ ide2=="local:iso/installer-$2.iso,media=cdrom" \
ostype==l26 \ ostype==l26 \
\ \
bios==ovmf \ bios==ovmf \
@ -256,14 +265,14 @@ create_vm () {
\ \
net0=='virtio,bridge=vnet1306' net0=='virtio,bridge=vnet1306'
printf 'done creating VM %d.\n' "$1" printf 'done creating VM %s.\n' "$2"
} }
################################################################################ ################################################################################
## Install VM ## Install VM
install_vm () ( install_vm () (
printf 'Installing VM %d...\n' "$1" printf 'Installing VM %s...\n' "$2"
proxmox_sync POST "$api_url/nodes/$node/qemu/$1/status/start" proxmox_sync POST "$api_url/nodes/$node/qemu/$1/status/start"
@ -277,14 +286,14 @@ install_vm () (
esac esac
done done
printf 'done installing VM %d.\n' "$1" printf 'done installing VM %s.\n' "$2"
) )
################################################################################ ################################################################################
## Start VM ## Start VM
start_vm () { start_vm () {
printf 'Starting VM %d...\n' "$1" printf 'Starting VM %s...\n' "$2"
proxmox_sync POST "$api_url/nodes/$node/qemu/$1/config" \ proxmox_sync POST "$api_url/nodes/$node/qemu/$1/config" \
ide2=='none,media=cdrom' \ ide2=='none,media=cdrom' \
@ -292,7 +301,7 @@ start_vm () {
proxmox_sync POST "$api_url/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 %s.\n' "$2"
} }
################################################################################ ################################################################################
@ -304,16 +313,22 @@ printf ' cores: %d\n' "$cores"
printf ' memory: %d\n' "$memory" printf ' memory: %d\n' "$memory"
provision_vm () { provision_vm () {
build_iso "$1" build_iso "$@"
upload_iso "$1" upload_iso "$@"
create_vm "$1" create_vm "$@"
install_vm "$1" install_vm "$@"
start_vm "$1" start_vm "$@"
remove_iso "$1" remove_iso "$@"
} }
for vm_id in $vm_ids; do for vm_id in $vm_ids; do
provision_vm "$vm_id" & vm_name=${vm_id#*:}
vm_id=${vm_id%:*}
if [ "$vm_id" = "$vm_name" ]; then
vm_name=$(printf 'fedi%03d' "$vm_id")
fi
provision_vm "$vm_id" "$vm_name" &
done done
wait wait

View file

@ -165,6 +165,7 @@ remove_vm () {
} }
for vm_id in $vm_ids; do for vm_id in $vm_ids; do
vm_id=${vm_id%:*}
remove_vm "$vm_id" & remove_vm "$vm_id" &
done done
wait wait