/
step_instance_info.go
67 lines (55 loc) · 1.65 KB
/
step_instance_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package civo
import (
"context"
"fmt"
"github.com/civo/civogo"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
type stepInstanceInfo struct{}
func (s *stepInstanceInfo) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*civogo.Client)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(*Config)
instanceID := state.Get("instance_id").(string)
ui.Say("Waiting for instance to become active...")
err := waitForInstanceState("ACTIVE", instanceID, client, c.StateTimeout)
if err != nil {
err := fmt.Errorf("Error waiting for instance to become active: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Set the IP on the state for later
instance, err := client.GetInstance(instanceID)
if err != nil {
err := fmt.Errorf("Error retrieving instance: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Verify we have an IPv4 address
invalid := instance.PublicIP == ""
if invalid {
err := fmt.Errorf("IPv4 address not found for instance")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Find a public IPv4 network
foundNetwork := false
if instance.PublicIP != "" {
state.Put("instance_ip", instance.PublicIP)
foundNetwork = true
}
if !foundNetwork {
err := fmt.Errorf("Count not find a public IPv4 address for this instance")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (s *stepInstanceInfo) Cleanup(state multistep.StateBag) {
// no cleanup
}