github.com/daniellockard/packer@v0.7.6-0.20141210173435-5a9390934716/builder/digitalocean/step_power_off.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/mitchellh/multistep"
     8  	"github.com/mitchellh/packer/packer"
     9  )
    10  
    11  type stepPowerOff struct{}
    12  
    13  func (s *stepPowerOff) Run(state multistep.StateBag) multistep.StepAction {
    14  	client := state.Get("client").(DigitalOceanClient)
    15  	c := state.Get("config").(config)
    16  	ui := state.Get("ui").(packer.Ui)
    17  	dropletId := state.Get("droplet_id").(uint)
    18  
    19  	_, status, err := client.DropletStatus(dropletId)
    20  	if err != nil {
    21  		err := fmt.Errorf("Error checking droplet state: %s", err)
    22  		state.Put("error", err)
    23  		ui.Error(err.Error())
    24  		return multistep.ActionHalt
    25  	}
    26  
    27  	if status == "off" {
    28  		// Droplet is already off, don't do anything
    29  		return multistep.ActionContinue
    30  	}
    31  
    32  	// Pull the plug on the Droplet
    33  	ui.Say("Forcefully shutting down Droplet...")
    34  	err = client.PowerOffDroplet(dropletId)
    35  	if err != nil {
    36  		err := fmt.Errorf("Error powering off droplet: %s", err)
    37  		state.Put("error", err)
    38  		ui.Error(err.Error())
    39  		return multistep.ActionHalt
    40  	}
    41  
    42  	log.Println("Waiting for poweroff event to complete...")
    43  	err = waitForDropletState("off", dropletId, client, c.stateTimeout)
    44  	if err != nil {
    45  		state.Put("error", err)
    46  		ui.Error(err.Error())
    47  		return multistep.ActionHalt
    48  	}
    49  
    50  	return multistep.ActionContinue
    51  }
    52  
    53  func (s *stepPowerOff) Cleanup(state multistep.StateBag) {
    54  	// no cleanup
    55  }