forked from Fediversity/Fediversity
Decouple id and name
This commit is contained in:
parent
d77b04ec18
commit
f8ec8e7d93
2 changed files with 44 additions and 28 deletions
|
@ -29,6 +29,15 @@ help () {
|
|||
cat <<EOF
|
||||
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:
|
||||
--api-url STR Base URL of the Proxmox API (required)
|
||||
--username STR Username, with provider (eg. niols@pve; required)
|
||||
|
@ -174,28 +183,28 @@ proxmox_sync () (
|
|||
|
||||
build_iso () {
|
||||
acquire_lock build
|
||||
printf 'Building ISO for VM %d...\n' "$1"
|
||||
printf 'Building ISO for VM %s...\n' "$2"
|
||||
|
||||
nix build \
|
||||
--impure --expr "
|
||||
let flake = builtins.getFlake (builtins.toString ./.); in
|
||||
flake.lib.makeInstallerIso {
|
||||
nixosConfiguration = flake.nixosConfigurations.provisioning.fedi$1;
|
||||
nixosConfiguration = flake.nixosConfigurations.$2;
|
||||
nixpkgs = flake.inputs.nixpkgs;
|
||||
hostKeys = {
|
||||
ed25519 = {
|
||||
private = ./deployment/hostKeys/fedi$1/ssh_host_ed25519_key;
|
||||
public = ./deployment/hostKeys/fedi$1/ssh_host_ed25519_key.pub;
|
||||
private = ./deployment/hostKeys/$2/ssh_host_ed25519_key;
|
||||
public = ./deployment/hostKeys/$2/ssh_host_ed25519_key.pub;
|
||||
};
|
||||
};
|
||||
}
|
||||
" \
|
||||
--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
|
||||
}
|
||||
|
||||
|
@ -204,13 +213,13 @@ build_iso () {
|
|||
|
||||
upload_iso () {
|
||||
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" \
|
||||
"filename@$tmpdir/installer-fedi$1.iso" \
|
||||
"filename@$tmpdir/installer-$2.iso" \
|
||||
content==iso
|
||||
|
||||
printf 'done uploading ISO for VM %d.\n' "$1"
|
||||
printf 'done uploading ISO for VM %s.\n' "$2"
|
||||
release_lock upload
|
||||
}
|
||||
|
||||
|
@ -218,26 +227,26 @@ upload_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 () {
|
||||
printf 'Creating VM %d...\n' "$1"
|
||||
printf 'Creating VM %s with id %d...\n' "$2" "$1"
|
||||
|
||||
proxmox_sync POST "$api_url/nodes/$node/qemu" \
|
||||
\
|
||||
vmid=="$1" \
|
||||
name=="fedi$1" \
|
||||
name=="$2" \
|
||||
pool==Fediversity \
|
||||
\
|
||||
ide2=="local:iso/installer-fedi$1.iso,media=cdrom" \
|
||||
ide2=="local:iso/installer-$2.iso,media=cdrom" \
|
||||
ostype==l26 \
|
||||
\
|
||||
bios==ovmf \
|
||||
|
@ -256,14 +265,14 @@ create_vm () {
|
|||
\
|
||||
net0=='virtio,bridge=vnet1306'
|
||||
|
||||
printf 'done creating VM %d.\n' "$1"
|
||||
printf 'done creating VM %s.\n' "$2"
|
||||
}
|
||||
|
||||
################################################################################
|
||||
## 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"
|
||||
|
||||
|
@ -277,14 +286,14 @@ install_vm () (
|
|||
esac
|
||||
done
|
||||
|
||||
printf 'done installing VM %d.\n' "$1"
|
||||
printf 'done installing VM %s.\n' "$2"
|
||||
)
|
||||
|
||||
################################################################################
|
||||
## 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" \
|
||||
ide2=='none,media=cdrom' \
|
||||
|
@ -292,7 +301,7 @@ start_vm () {
|
|||
|
||||
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"
|
||||
|
||||
provision_vm () {
|
||||
build_iso "$1"
|
||||
upload_iso "$1"
|
||||
create_vm "$1"
|
||||
install_vm "$1"
|
||||
start_vm "$1"
|
||||
remove_iso "$1"
|
||||
build_iso "$@"
|
||||
upload_iso "$@"
|
||||
create_vm "$@"
|
||||
install_vm "$@"
|
||||
start_vm "$@"
|
||||
remove_iso "$@"
|
||||
}
|
||||
|
||||
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
|
||||
wait
|
||||
|
||||
|
|
|
@ -165,6 +165,7 @@ remove_vm () {
|
|||
}
|
||||
|
||||
for vm_id in $vm_ids; do
|
||||
vm_id=${vm_id%:*}
|
||||
remove_vm "$vm_id" &
|
||||
done
|
||||
wait
|
||||
|
|
Loading…
Add table
Reference in a new issue