Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destroy stop-vm continues to remove-vm before vms have stopped. #275

Open
timvia opened this issue Oct 21, 2024 · 1 comment
Open

Destroy stop-vm continues to remove-vm before vms have stopped. #275

timvia opened this issue Oct 21, 2024 · 1 comment

Comments

@timvia
Copy link

timvia commented Oct 21, 2024

When running a destroy our vms get stuck in a shutting down state. They never complete the shutdown and fail.

We would like the ability to use the Turnoff flag when running a destroy.

This would then run "Stop-VM -Name TestVM -TurnOff" rather than "Stop-VM -Name VM1 -Force"

As this is a destroy task we do not care about a clean shutdown.

Terraform Version

1.9.8

Expected Behavior

When destroying a vm the vm should shutdown quickly and forcefully.

Actual Behavior

VMs runnning K8s on linux (rhel 8/9) change state to shutting down and hang there until it fails to shutdown.

Terraform error message is

│ Remove-VM : Deleting 'cluster-1' failed.
│ The operation cannot be performed while the object is in its current state.
│ At C:\Users\proteanautomation\AppData\Local\Temp\shell-terraform-2nkPyhRSQesox83fe8cgpPoodEv.ps1:3 char:106
│ + ... ?{$_.Name -eq 'pipeline-dp-performance-cluster-1'} | Remove-VM -force

This is due to the state of the VM still being in a shutting down state.

To work around this we are manually going to hyperv and turning off the vms before running the terraform. Not ideal for automation though

@timvia timvia changed the title Destroy stop-vm continues before vms have stopped. Destroy stop-vm continues to remove-vm before vms have stopped. Oct 21, 2024
@timvia
Copy link
Author

timvia commented Dec 19, 2024

Using a null resource ro issue the turn off command to work around this issue.

resource "null_resource" "force_shutdown_vm_on_destory" {
  triggers = {
    vm_names = hyperv_machine_instance.default.name
    host     = var.hyperv_host_name
    user     = var.hyperv_user
    password = var.hyperv_password
  }
  provisioner "local-exec" {
    when    = destroy
    command = <<EOT
      curl -k -u "${self.triggers.user}:${self.triggers.password}" \
      -H "Content-Type: application/json" \
      -d '{"PowerShell":"Stop-VM -Name ${self.triggers.vm_names} -TurnOff -Force; while((Get-VM -Name ${self.triggers.vm_names}).State -ne \"Off\") { Start-Sleep -s 2 }}"}' \
      https://${self.triggers.host}:5986/wsman
    EOT
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant