github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/digitalocean/step_power_off.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/digitalocean/godo"
     8  	"github.com/mitchellh/multistep"
     9  	"github.com/mitchellh/packer/packer"
    10  )
    11  
    12  type stepPowerOff struct{}
    13  
    14  func (s *stepPowerOff) Run(state multistep.StateBag) multistep.StepAction {
    15  	client := state.Get("client").(*godo.Client)
    16  	c := state.Get("config").(Config)
    17  	ui := state.Get("ui").(packer.Ui)
    18  	dropletId := state.Get("droplet_id").(int)
    19  
    20  	droplet, _, err := client.Droplets.Get(dropletId)
    21  	if err != nil {
    22  		err := fmt.Errorf("Error checking droplet state: %s", err)
    23  		state.Put("error", err)
    24  		ui.Error(err.Error())
    25  		return multistep.ActionHalt
    26  	}
    27  
    28  	if droplet.Status == "off" {
    29  		// Droplet is already off, don't do anything
    30  		return multistep.ActionContinue
    31  	}
    32  
    33  	// Pull the plug on the Droplet
    34  	ui.Say("Forcefully shutting down Droplet...")
    35  	_, _, err = client.DropletActions.PowerOff(dropletId)
    36  	if err != nil {
    37  		err := fmt.Errorf("Error powering off droplet: %s", err)
    38  		state.Put("error", err)
    39  		ui.Error(err.Error())
    40  		return multistep.ActionHalt
    41  	}
    42  
    43  	log.Println("Waiting for poweroff event to complete...")
    44  	err = waitForDropletState("off", dropletId, client, c.StateTimeout)
    45  	if err != nil {
    46  		state.Put("error", err)
    47  		ui.Error(err.Error())
    48  		return multistep.ActionHalt
    49  	}
    50  
    51  	// Wait for the droplet to become unlocked for future steps
    52  	if err := waitForDropletUnlocked(client, dropletId, c.StateTimeout); err != nil {
    53  		// If we get an error the first time, actually report it
    54  		err := fmt.Errorf("Error powering off droplet: %s", err)
    55  		state.Put("error", err)
    56  		ui.Error(err.Error())
    57  		return multistep.ActionHalt
    58  	}
    59  
    60  	return multistep.ActionContinue
    61  }
    62  
    63  func (s *stepPowerOff) Cleanup(state multistep.StateBag) {
    64  	// no cleanup
    65  }