-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathstep_get_ip.go
93 lines (77 loc) · 2.86 KB
/
step_get_ip.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package vpc
import (
"context"
"fmt"
"io/ioutil"
"os"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
)
type stepGetIP struct{}
func (step *stepGetIP) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*IBMCloudClient)
config := state.Get("config").(Config)
ui := state.Get("ui").(packer.Ui)
instanceData := state.Get("instance_data").(*vpcv1.Instance)
ui.Say(fmt.Sprintf("Getting %s IP...", config.VSIInterface))
var ipAddress string
if config.VSIInterface == "private" {
primaryNetworkInterface := instanceData.PrimaryNetworkInterface
// Post 3/29/22 Reserved IP P2
ipAddress = *primaryNetworkInterface.PrimaryIP.Address
} else if config.VSIInterface == "public" {
ui.Say("Reserve a Floating IP and associate it to the instance's network interface")
// Create Floating IP
ui.Say("Reserving a Floating IP")
floatingIPData, errIP := client.createFloatingIP(state)
if errIP != nil {
err := fmt.Errorf("[ERROR] Error creating FloatingIP: %s", errIP)
state.Put("error", err)
ui.Error(err.Error())
// log.Fatalf(err.Error())
return multistep.ActionHalt
}
// Wait until the Floating IP is ACTIVE
ui.Say("Waiting for the Floating IP to become ACTIVE...")
floatingIPID := *floatingIPData.ID
state.Put("floating_ip_id", floatingIPID)
err := client.waitForResourceReady(floatingIPID, "floating_ips", config.StateTimeout, state)
if err != nil {
err := fmt.Errorf("[ERROR] Error waiting for Floating IP to become ACTIVE: %s", err)
state.Put("error", err)
ui.Error(err.Error())
// log.Fatalf(err.Error())
return multistep.ActionHalt
}
ui.Say("Floating IP is ACTIVE!")
ipAddress = *floatingIPData.Address
}
state.Put("floating_ip", ipAddress)
///// Update the Communicator with the ipAddres value /////
if config.Comm.Type == "winrm" {
config.Comm.WinRMHost = ipAddress
} else if config.Comm.Type == "ssh" {
config.Comm.SSHHost = ipAddress
}
state.Put("config", config)
// Write IP Address to ANSIBLE_INVENTORY_FILE, so there is no need to
// manually accept the connection with the instance during SSH Communication
hostsFilePath := os.Getenv("ANSIBLE_INVENTORY_FILE")
if hostsFilePath == "" {
// No inventory file specified, continuing on to next step
return multistep.ActionContinue
}
ipAddressBytes := []byte(fmt.Sprintf("%s\n", ipAddress))
err := ioutil.WriteFile(hostsFilePath, ipAddressBytes, 0644)
if err != nil {
err := fmt.Errorf("[ERROR] Failed to write IP address to file: %s", err)
state.Put("error", err)
ui.Error(err.Error())
// log.Fatalf(err.Error())
return multistep.ActionHalt
}
// ui.Say(fmt.Sprintf("IP address has been written into file %s", hostsFilePath))
return multistep.ActionContinue
}
func (client *stepGetIP) Cleanup(state multistep.StateBag) {}