2014-11-24 11:36:14 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2014-11-24 11:36:14 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2014-11-24 11:36:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepTeardownInstance represents a Packer build step that tears down GCE
|
|
|
|
// instances.
|
|
|
|
type StepTeardownInstance struct {
|
|
|
|
Debug bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes the Packer build step that tears down a GCE instance.
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepTeardownInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-11-24 11:36:14 -05:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2014-11-24 11:36:14 -05:00
|
|
|
|
|
|
|
name := config.InstanceName
|
|
|
|
if name == "" {
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Deleting instance...")
|
2016-05-24 20:13:36 -04:00
|
|
|
instanceLog, _ := driver.GetSerialPortOutput(config.Zone, name)
|
|
|
|
state.Put("instance_log", instanceLog)
|
2014-11-24 11:36:14 -05:00
|
|
|
errCh, err := driver.DeleteInstance(config.Zone, name)
|
|
|
|
if err == nil {
|
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
2019-10-31 10:49:34 -04:00
|
|
|
case <-time.After(config.StateTimeout):
|
2014-11-24 11:36:14 -05:00
|
|
|
err = errors.New("time out while waiting for instance to delete")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error deleting instance. Please delete it manually.\n\n"+
|
|
|
|
"Name: %s\n"+
|
|
|
|
"Error: %s", name, err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
ui.Message("Instance has been deleted!")
|
|
|
|
state.Put("instance_name", "")
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deleting the instance does not remove the boot disk. This cleanup removes
|
|
|
|
// the disk.
|
|
|
|
func (s *StepTeardownInstance) Cleanup(state multistep.StateBag) {
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2014-11-24 11:36:14 -05:00
|
|
|
|
|
|
|
ui.Say("Deleting disk...")
|
|
|
|
errCh, err := driver.DeleteDisk(config.Zone, config.DiskName)
|
|
|
|
if err == nil {
|
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
2019-10-31 10:49:34 -04:00
|
|
|
case <-time.After(config.StateTimeout):
|
2014-11-24 11:36:14 -05:00
|
|
|
err = errors.New("time out while waiting for disk to delete")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error deleting disk. Please delete it manually.\n\n"+
|
2016-09-29 17:13:04 -04:00
|
|
|
"DiskName: %s\n"+
|
|
|
|
"Zone: %s\n"+
|
2016-07-07 17:50:46 -04:00
|
|
|
"Error: %s", config.DiskName, config.Zone, err))
|
2014-11-24 11:36:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message("Disk has been deleted!")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|