Telmate / terraform-provider-proxmox

Terraform provider plugin for proxmox
MIT License
2.11k stars 510 forks source link

Enabling guest agent for new VM crashes plugin #1074

Closed ynnckvdv closed 1 week ago

ynnckvdv commented 1 month ago

When creating a new VM from an unprovisioned ISO (from scratch) using the argument agent = 1 in the proxmox_vm_qemu resource, the plugin breaks. Even though the VM is created, it becomes independent of Terraform and terraform destroy will say that there is nothing to destroy.

│ Error: Request cancelled
│ 
│   with proxmox_vm_qemu.kube["k3s-master-1"],
│   on instances.tf line 1, in resource "proxmox_vm_qemu" "kube":
│    1: resource "proxmox_vm_qemu" "kube" {
│ 
│ The plugin.(*GRPCProvider).ApplyResourceChange request was cancelled.
╵

Stack trace from the terraform-provider-proxmox_v3.0.1-rc3 plugin:

panic: interface conversion: interface {} is nil, not string

goroutine 55 [running]:
github.com/Telmate/terraform-provider-proxmox/v2/proxmox.getPrimaryIP(0xc000278780, 0xc000bf2048, 0xc000bf1950?, 0x0?, {0xc000ed5c00?, 0x1c?, 0x13a5340?}, 0x5, 0x0, 0x1, ...)
        github.com/Telmate/terraform-provider-proxmox/v2/proxmox/resource_vm_qemu.go:1933 +0xf26
github.com/Telmate/terraform-provider-proxmox/v2/proxmox.initConnInfo({0xc0000aa2a0?, 0x0?}, 0xc0002f4080, 0xc000bf1ee8?, 0xc000edaf10?, 0xc000bf2048, 0xc000278780, 0x1f4?)
        github.com/Telmate/terraform-provider-proxmox/v2/proxmox/resource_vm_qemu.go:1869 +0x825
github.com/Telmate/terraform-provider-proxmox/v2/proxmox.resourceVmQemuRead({0xe47340, 0xc00069dc70}, 0xc0002f4080, {0xbb8f80?, 0xc000263ae0})
        github.com/Telmate/terraform-provider-proxmox/v2/proxmox/resource_vm_qemu.go:1440 +0x92e
github.com/Telmate/terraform-provider-proxmox/v2/proxmox.resourceVmQemuCreate({0xe47340, 0xc00069dc70}, 0xc0002f4080, {0xbb8f80?, 0xc000263ae0?})
        github.com/Telmate/terraform-provider-proxmox/v2/proxmox/resource_vm_qemu.go:1094 +0x2ee5
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).create(0xc0005ccc40, {0xe47298, 0xc000ae6d50}, 0xd?, {0xbb8f80, 0xc000263ae0})
        github.com/hashicorp/terraform-plugin-sdk/v2@v2.34.0/helper/schema/resource.go:806 +0x11b
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).Apply(0xc0005ccc40, {0xe47298, 0xc000ae6d50}, 0xc000162c30, 0xc000135280, {0xbb8f80, 0xc000263ae0})
        github.com/hashicorp/terraform-plugin-sdk/v2@v2.34.0/helper/schema/resource.go:937 +0xa89
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ApplyResourceChange(0xc000390000, {0xe47298?, 0xc000ae6c60?}, 0xc000a86370)
        github.com/hashicorp/terraform-plugin-sdk/v2@v2.34.0/helper/schema/grpc_provider.go:1153 +0xdbc
github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ApplyResourceChange(0xc0002500a0, {0xe47298?, 0xc000ae6240?}, 0xc00069c1c0)
        github.com/hashicorp/terraform-plugin-go@v0.23.0/tfprotov5/tf5server/server.go:865 +0x3d0
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0xce8520?, 0xc0002500a0}, {0xe47298, 0xc000ae6240}, 0xc000134300, 0x0)
        github.com/hashicorp/terraform-plugin-go@v0.23.0/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:518 +0x169
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0001a5000, {0xe47298, 0xc000ae61b0}, {0xe4b2f8, 0xc000248780}, 0xc00041a120, 0xc00059ccf0, 0x1397d78, 0x0)
        google.golang.org/grpc@v1.63.2/server.go:1369 +0xe23
google.golang.org/grpc.(*Server).handleStream(0xc0001a5000, {0xe4b2f8, 0xc000248780}, 0xc00041a120)
        google.golang.org/grpc@v1.63.2/server.go:1780 +0x1016
google.golang.org/grpc.(*Server).serveStreams.func2.1()
        google.golang.org/grpc@v1.63.2/server.go:1019 +0x8b
created by google.golang.org/grpc.(*Server).serveStreams.func2 in goroutine 36
        google.golang.org/grpc@v1.63.2/server.go:1030 +0x135

Error: The terraform-provider-proxmox_v3.0.1-rc3 plugin crashed!

I understand that enabling the guest agent won't work in a VM where qemu_guest_agent isn't installed, but I don't think this is expected behaviour either?

Thank you.

zehndi77 commented 4 weeks ago

+1

zehndi77 commented 3 weeks ago

https://github.com/Telmate/terraform-provider-proxmox/issues/863#issuecomment-1916274439

upping the provider to 3... release candidate helped:

terraform {

    required_version = ">= 0.13.0"

    required_providers {
        proxmox = {
            source = "telmate/proxmox"
            version = "3.0.1-rc3"
            # version = ">= 2.9.14"
        }
    }
}
ynnckvdv commented 3 weeks ago

@zehndi77 Strange, because I am on 3.0.1-rc3. See the last line of my attached code block in my post.

zehndi77 commented 3 weeks ago

@ynnckvdv

i have just today succeeded with the following setup:

https://github.com/thesheff17/tf_examples/blob/main/create_vm_9000.sh used this script to provision the clone, but replaces scsi with virtio: qm set 9000 --scsi0 local-lvm:0,import-from=/root/jammy-server-cloudimg-amd64.img qm set 9000 --boot order=scsi0 qm set 9000 --virtio0 local-lvm:0,import-from=/root/jammy-server-cloudimg-amd64.img qm set 9000 --boot order=virtio0

you might need to install the virt-customize tools for this script: apt install libguestfs-tools -y

then I used this .tf file to provision the VM:

https://github.com/Telmate/terraform-provider-proxmox/blob/v3.0.1-rc3/examples/cloudinit_example.tf

adapted to my local needs:

# Proxmox Full-Clone
# ---
# Create a new VM from a clone

resource "proxmox_vm_qemu" "vm-test-01" {

    # VM General Settings
    target_node = "gladsheim"
    vmid = "9001"
    name = "vm-test-01"
    desc = "Description"

    # VM OS Settings
    clone = "VM 9000"

    # VM Network Settings
    network {
        bridge = "vmbr0"
        model  = "virtio"
        tag = 1337
    }

    agent                     = 1
    automatic_reboot          = true
    #balloon                   = 0
    #bios                      = "seabios"

    boot                      = "order=virtio0;ide2"
    cores                     = 2
    cpu                       = "host"
    define_connection_info    = true
    force_create              = false
    hotplug                   = "network,disk,usb"
    kvm                       = true
    memory                    = 2048
    numa                      = false
    onboot                    = false
    vm_state                  = "running"
    #qemu_os                   = "l26"
    #scsihw                    = "virtio-scsi-pci"
    sockets                   = 1
    protection                = false
    #tablet                    = true
    vcpus                     = 0

    # VM Disk Settings
    disks {
        ide {
            ide2 {
                cloudinit {
                    storage = "local-lvm"
                }
            }
        }
        virtio {
            virtio0 {
                disk {
                    backup             = true
                    cache              = "none"
                    discard            = true
                    # emulatessd         = true
                    # iothread           = true
                    # mbps_r_burst       = 0.0
                    # mbps_r_concurrent  = 0.0
                    # mbps_wr_burst      = 0.0
                    # mbps_wr_concurrent = 0.0
                    # replicate          = true
                    size               = "2252M"
                    storage            = "local-zfs"
                }
            }
        }
    }

    # VM Cloud-Init Settings
    os_type = "cloud-init"
    # (Optional) IP Address and Gateway
    ipconfig0 = "ip=10.13.37.7/24,gw=10.13.37.1"

    # (Optional) Default User
    ciuser = "ubuntu"

    # (Optional) Add your SSH KEY
    sshkeys = <<EOF
    <enter your keys here>
    EOF
}

very important (but maybe known to you) is that the disk do have to follow the template config (size, storage location etc.) as closely as possible. (not sure how close is necessary..)

let me know how it goes. Best

Tinyblargon commented 3 weeks ago

@ynnckvdv Does your vm have a network interface?

Currently unsure if it should give a warning that no interfaces are found or if the logic should be able to find the correct interface.

Found the line causing the panic: https://github.com/Telmate/terraform-provider-proxmox/blob/3c87e4571cbf00df4f8dfe0328457f68754398f1/proxmox/resource_vm_qemu.go#L1877

Tinyblargon commented 3 weeks ago

@ynnckvdv would you be able to test with #1076

ynnckvdv commented 3 weeks ago

@zehndi77 Unfortunately, that wasn't the fix for me, but thank you

ynnckvdv commented 3 weeks ago

@Tinyblargon I would like to but I'm having trouble installing the plugin manually. But I indeed do not have a network adapter configured (and I do not use cloud-init), so that must be it!