2013-06-13 12:48:19 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2013-06-20 00:00:51 -04:00
|
|
|
"fmt"
|
2014-09-03 17:37:33 -04:00
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
"github.com/digitalocean/godo"
|
2013-06-13 12:48:19 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepDropletInfo struct{}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepDropletInfo) Run(state multistep.StateBag) multistep.StepAction {
|
2015-06-10 17:02:06 -04:00
|
|
|
client := state.Get("client").(*godo.Client)
|
2013-08-31 15:25:08 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2015-05-27 15:50:43 -04:00
|
|
|
c := state.Get("config").(Config)
|
2015-06-29 17:47:28 -04:00
|
|
|
dropletID := state.Get("droplet_id").(int)
|
2013-06-13 12:48:19 -04:00
|
|
|
|
|
|
|
ui.Say("Waiting for droplet to become active...")
|
|
|
|
|
2015-06-29 17:47:28 -04:00
|
|
|
err := waitForDropletState("active", dropletID, client, c.StateTimeout)
|
2013-06-13 13:56:34 -04:00
|
|
|
if err != nil {
|
2013-06-20 00:00:51 -04:00
|
|
|
err := fmt.Errorf("Error waiting for droplet to become active: %s", err)
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("error", err)
|
2013-06-13 13:56:34 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2013-06-13 12:48:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the IP on the state for later
|
2015-06-29 17:47:28 -04:00
|
|
|
droplet, _, err := client.Droplets.Get(dropletID)
|
2013-06-13 12:48:19 -04:00
|
|
|
if err != nil {
|
2015-06-10 17:02:06 -04:00
|
|
|
err := fmt.Errorf("Error retrieving droplet: %s", err)
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("error", err)
|
2013-06-13 12:48:19 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
// Verify we have an IPv4 address
|
|
|
|
invalid := droplet.Networks == nil ||
|
|
|
|
len(droplet.Networks.V4) == 0
|
|
|
|
if invalid {
|
2015-06-29 17:47:28 -04:00
|
|
|
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")
|
2015-06-10 17:02:06 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-06-13 12:48:19 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepDropletInfo) Cleanup(state multistep.StateBag) {
|
2013-06-13 12:48:19 -04:00
|
|
|
// no cleanup
|
|
|
|
}
|