dmacvicar / terraform-provider-libvirt

Terraform provider to provision infrastructure with Linux's KVM using libvirt
Apache License 2.0
1.59k stars 458 forks source link

guest is not shutdown after changing running to "false" #622

Open muroj opened 5 years ago

muroj commented 5 years ago

System Information

Linux distribution

Ubuntu 18.04.2 LTS

Terraform version

Terraform v0.11.14
+ provider.libvirt (unversioned)
+ provider.template v2.1.2

Provider and libvirt versions

terraform-provider-libvirt 0.5.2
Compiled against library: libvirt 4.0.0
Using library: libvirt 4.0.0

Checklist

Description of Issue/Question

Changing running = "false" domain argument has no effect. Yet terraform apply reports all that update was successful. Same as #119

terraform apply
data.template_file.network_config: Refreshing state...
data.template_file.user_data: Refreshing state...
libvirt_cloudinit_disk.commoninit_guest1: Refreshing state... (ID: /guestimages/data1/terraform/joe_terraf...o;5d4adfc2-9d4c-9f28-f090-f002d2df7cd0)
libvirt_volume.guest1: Refreshing state... (ID: /guestimages/data1/terraform/joe_terraform_demo.qcow2)
libvirt_domain.guest1: Refreshing state... (ID: 8680a09d-72c8-4210-a31a-0733d4f7d1c6)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  ~ libvirt_domain.guest1
      running: "true" => "false"

Plan: 0 to add, 1 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

libvirt_domain.guest1: Modifying... (ID: 8680a09d-72c8-4210-a31a-0733d4f7d1c6)
  running: "true" => "false"
libvirt_domain.guest1: Modifications complete after 0s (ID: 8680a09d-72c8-4210-a31a-0733d4f7d1c6)
virsh list
 Id   Name                 State
------------------------------------
 53   joe_terraform_demo   running

Setup

provider "libvirt" {
    uri = "qemu+ssh://myuser@myhost/system"
}

resource "libvirt_volume" "guest1" {
  name = "joe_terraform_demo.qcow2"
  pool = "terraform"
  base_volume_name = "sles15-sp1-master"
  base_volume_pool = "terraform"
  format = "qcow2"
}

resource "libvirt_domain" "guest1" {
  name = "joe_terraform_demo"
  vcpu = "2"
  memory = "1024"
  running = "false"

  cloudinit = "${libvirt_cloudinit_disk.commoninit_guest1.id}"

  disk = [
      {
          volume_id = "${libvirt_volume.guest1.id}"
      },
      {
          file = "/home/jmuro/images/isos/SLE-15-Packages-s390x-GM-DVD1.iso"
      }
  ]

  network_interface {
    bridge = "mybridge"
  }

  console {
      type = "pty"
      target_port = "0"
  }

  xml {
    xslt = "${file("${path.module}/xsl/xml_overrides.xsl")}"
  }

  provisioner "local-exec" {
    command = "echo ${libvirt_domain.guest1.network_interface.0.mac}  >> domain.txt"
  }
}

Steps to Reproduce Issue

Change running to false -> terraform apply

Guest is still running on host

MalloZup commented 5 years ago

@muroj thx for issue. One question afaik I understand the issue, can you confirm that if you create for the 1st time a domain with running = false this work, but if you update ( I emphatise this), the update doesn't work?

Just to be sure on this. thx!

Afaik the update mechanism isn't working but the creation should work. thx for confirmation!

muroj commented 5 years ago

Hi @MalloZup, thanks for response. Yes I can confirm that when starting from scratch (empty terraform state) with running="false" the domain is created but not started (as expected). Output from virsh list --all confirms this. Furthermore, changing running="true" and running terraform apply again will successfully start the domain. However, changing back to running="false" does not affect the running domain.

Let me know I can provide libvirtd logs if they are useful.

MalloZup commented 5 years ago

@muroj thx! I don't think the libvirt logs are needed here.

The situation is really well described and we have an HOWTO reproduce. thx so far

MalloZup commented 5 years ago

For contributors

I'm providing a short description for people who perhaps want to contribute:

On the highlevel the problem is following:

currently when an user specify running = false, this works only when we have the domain is created, but not updated

See later the functions.

The main problem is because we don't have a function during the update lifecycle of the domain.

https://github.com/dmacvicar/terraform-provider-libvirt/blob/19c23424ccfdecf6dfcbdd87783bf835ba5f585e/libvirt/resource_libvirt_domain.go#L593

the function destroyDomainByUserRequest destroy (shutdown) only a domain during the create lifecycle of domain

https://github.com/dmacvicar/terraform-provider-libvirt/blob/c0e46b59df8718cdd905b1a3fb9738b0d4905143/libvirt/domain.go#L771

some hints what to do:

we should in theory have a function called like updateRunningStatus which act as shutting down or turn on a domain.

This function should be added during the lifecycle update and also some testAcc should be added + doc as usual