Find a public IP address to provision a digital ocean box

This commit is contained in:
Chris Bednarski 2015-06-29 14:47:28 -07:00
parent aa3a336e72
commit 51804e9132
1 changed files with 20 additions and 5 deletions

View File

@ -14,11 +14,11 @@ func (s *stepDropletInfo) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*godo.Client)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config)
dropletId := state.Get("droplet_id").(int)
dropletID := state.Get("droplet_id").(int)
ui.Say("Waiting for droplet to become active...")
err := waitForDropletState("active", dropletId, client, c.StateTimeout)
err := waitForDropletState("active", dropletID, client, c.StateTimeout)
if err != nil {
err := fmt.Errorf("Error waiting for droplet to become active: %s", err)
state.Put("error", err)
@ -27,7 +27,7 @@ func (s *stepDropletInfo) Run(state multistep.StateBag) multistep.StepAction {
}
// Set the IP on the state for later
droplet, _, err := client.Droplets.Get(dropletId)
droplet, _, err := client.Droplets.Get(dropletID)
if err != nil {
err := fmt.Errorf("Error retrieving droplet: %s", err)
state.Put("error", err)
@ -39,13 +39,28 @@ func (s *stepDropletInfo) Run(state multistep.StateBag) multistep.StepAction {
invalid := droplet.Networks == nil ||
len(droplet.Networks.V4) == 0
if invalid {
err := fmt.Errorf("IPv4 address not found for droplet!")
err := fmt.Errorf("IPv4 address not found for droplet")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Find a public IPv4 network
foundNetwork := false
for _, network := range droplet.Networks.V4 {
if network.Type == "public" {
state.Put("droplet_ip", network.IPAddress)
foundNetwork = true
break
}
}
if !foundNetwork {
err := fmt.Errorf("Count not find a public IPv4 address for this droplet")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put("droplet_ip", droplet.Networks.V4[0].IPAddress)
return multistep.ActionContinue
}