packer-cn/builder/vsphere/common/cleanup_vm.go

31 lines
654 B
Go

package common
import (
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/builder/vsphere/driver"
)
func CleanupVM(state multistep.StateBag) {
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
_, destroy := state.GetOk("destroy_vm")
if !cancelled && !halted && !destroy {
return
}
ui := state.Get("ui").(packersdk.Ui)
st := state.Get("vm")
if st == nil {
return
}
vm := st.(driver.VirtualMachine)
ui.Say("Destroying VM...")
err := vm.Destroy()
if err != nil {
ui.Error(err.Error())
}
}