github.com/rothwerx/packer@v0.9.0/builder/digitalocean/step_droplet_info.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/digitalocean/godo"
     7  	"github.com/mitchellh/multistep"
     8  	"github.com/mitchellh/packer/packer"
     9  )
    10  
    11  type stepDropletInfo struct{}
    12  
    13  func (s *stepDropletInfo) Run(state multistep.StateBag) multistep.StepAction {
    14  	client := state.Get("client").(*godo.Client)
    15  	ui := state.Get("ui").(packer.Ui)
    16  	c := state.Get("config").(Config)
    17  	dropletID := state.Get("droplet_id").(int)
    18  
    19  	ui.Say("Waiting for droplet to become active...")
    20  
    21  	err := waitForDropletState("active", dropletID, client, c.StateTimeout)
    22  	if err != nil {
    23  		err := fmt.Errorf("Error waiting for droplet to become active: %s", err)
    24  		state.Put("error", err)
    25  		ui.Error(err.Error())
    26  		return multistep.ActionHalt
    27  	}
    28  
    29  	// Set the IP on the state for later
    30  	droplet, _, err := client.Droplets.Get(dropletID)
    31  	if err != nil {
    32  		err := fmt.Errorf("Error retrieving droplet: %s", err)
    33  		state.Put("error", err)
    34  		ui.Error(err.Error())
    35  		return multistep.ActionHalt
    36  	}
    37  
    38  	// Verify we have an IPv4 address
    39  	invalid := droplet.Networks == nil ||
    40  		len(droplet.Networks.V4) == 0
    41  	if invalid {
    42  		err := fmt.Errorf("IPv4 address not found for droplet")
    43  		state.Put("error", err)
    44  		ui.Error(err.Error())
    45  		return multistep.ActionHalt
    46  	}
    47  
    48  	// Find a public IPv4 network
    49  	foundNetwork := false
    50  	for _, network := range droplet.Networks.V4 {
    51  		if network.Type == "public" {
    52  			state.Put("droplet_ip", network.IPAddress)
    53  			foundNetwork = true
    54  			break
    55  		}
    56  	}
    57  	if !foundNetwork {
    58  		err := fmt.Errorf("Count not find a public IPv4 address for this droplet")
    59  		state.Put("error", err)
    60  		ui.Error(err.Error())
    61  		return multistep.ActionHalt
    62  	}
    63  
    64  	return multistep.ActionContinue
    65  }
    66  
    67  func (s *stepDropletInfo) Cleanup(state multistep.StateBag) {
    68  	// no cleanup
    69  }