await SSH availability, resolving a race condition

Signed-off-by: Kiara Grouwstra <kiara@procolix.eu>
This commit is contained in:
Kiara Grouwstra 2025-10-13 12:15:11 +02:00
parent eb39b278a3
commit d76e0ddb98
Signed by: kiara
SSH key fingerprint: SHA256:COspvLoLJ5WC5rFb9ZDe5urVCkK4LJZOsjfF4duRJFU
2 changed files with 23 additions and 0 deletions

View file

@ -187,8 +187,30 @@ resource "proxmox_virtual_environment_vm" "nix_vm" {
# }
}
resource "null_resource" "wait_for_ssh" {
depends_on = [
proxmox_virtual_environment_vm.nix_vm
]
provisioner "local-exec" {
command = <<-EOT
for i in $(seq 1 30); do
if ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@${proxmox_virtual_environment_vm.nix_vm.ipv4_addresses[1][0]} "true" 2>/dev/null; then
exit 0
fi
echo "Waiting for SSH (attempt #$i)..."
sleep 5
done
echo "SSH never came up!" >&2
exit 1
EOT
}
}
# FIXME expose (and handle thru) [`exec`](https://pve.proxmox.com/pve-docs/api-viewer/#/nodes/{node}/qemu/{vmid}/agent/exec) endpoint in proxmox TF provider? wait, what command would i use it for?: https://github.com/bpg/terraform-provider-proxmox/issues/1576
module "nixos-rebuild" {
depends_on = [
null_resource.wait_for_ssh
]
source = "../tf-single-host"
nixos_conf = var.nixos_conf
# username = var.ssh_user # refers to the proxmox ssh user, not the VM one

View file

@ -35,6 +35,7 @@ in
).withPlugins
(p: [
p.external
p.null
(mkProvider {
owner = "bpg";
repo = "terraform-provider-proxmox";