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"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/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.
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepTeardownInstance) Run(_ 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)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
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:
|
|
|
|
case <-time.After(config.stateTimeout):
|
|
|
|
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)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Deleting disk...")
|
|
|
|
errCh, err := driver.DeleteDisk(config.Zone, config.DiskName)
|
|
|
|
if err == nil {
|
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
|
|
|
case <-time.After(config.stateTimeout):
|
|
|
|
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
|
|
|
|
}
|