github.com/kaixiang/packer@v0.5.2-0.20140114230416-1f5786b0d7f1/builder/digitalocean/step_power_off.go (about)

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