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