forked from ppc64le-cloud/packer-plugin-powervs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh.go
113 lines (100 loc) · 3.32 KB
/
ssh.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package common
import (
"errors"
"fmt"
"time"
"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)
var (
// modified in tests
sshHostSleepDuration = time.Minute
)
// SSHHost returns a function that can be given to the SSH communicator
// for determining the SSH address of the instance.
func SSHHost() func(multistep.StateBag) (string, error) {
return func(state multistep.StateBag) (string, error) {
ui := state.Get("ui").(packersdk.Ui)
ui.Message("Fetching IP for machine")
instanceClient := state.Get("instanceClient").(*instance.IBMPIInstanceClient)
host := ""
const tries = 25
subnet := state.Get("network").(*models.Network)
for j := 0; j <= tries; j++ {
i := state.Get("instance").(*models.PVMInstance)
in, err := instanceClient.Get(*i.PvmInstanceID)
if err != nil {
return "", errors.New("couldn't determine address for instance: failed to get instance")
}
for _, net := range in.Networks {
if *subnet.NetworkID != net.NetworkID {
continue
}
if *subnet.Type == "vlan" {
host = net.IPAddress
} else if *subnet.Type == "pub-vlan" {
host = net.ExternalIP
}
}
if host != "" {
return host, nil
}
// TODO: following code doesn't look good, needs refactoring.
dhcpServerID, ok := state.GetOk("dhcpServerID")
if !ok {
// if the dhcpServerID is not set, dont try to fetch IP from DHCP server, instead wait for address to get populated.
ui.Message("Machine IP is not yet found, Trying again")
time.Sleep(sshHostSleepDuration)
continue
}
dhcpClient := state.Get("dhcpClient").(*instance.IBMPIDhcpClient)
ui.Message("Getting Instance IP from DHCP server")
net := state.Get("network").(*models.Network)
networkID := net.NetworkID
var pvmNetwork *models.PVMInstanceNetwork
for _, network := range in.Networks {
if network.NetworkID == *networkID {
pvmNetwork = network
ui.Message("Found network attached to VM")
}
}
if pvmNetwork == nil {
ui.Message("Failed to get network attached to VM, Trying again")
time.Sleep(sshHostSleepDuration)
continue
}
dhcpServerDetails, err := dhcpClient.Get(dhcpServerID.(string))
if err != nil {
ui.Error(fmt.Sprintf("Failed to get DHCP server details: %v", err))
return "", err
}
if dhcpServerDetails == nil {
ui.Error(fmt.Sprintf("DHCP server details is nil, DHCPServerID: %s", dhcpServerID))
return "", err
}
var internalIP string
for _, lease := range dhcpServerDetails.Leases {
if *lease.InstanceMacAddress == pvmNetwork.MacAddress {
ui.Message(fmt.Sprintf("Found internal ip for VM from DHCP lease IP %s", *lease.InstanceIP))
internalIP = *lease.InstanceIP
break
}
}
if internalIP != "" {
return internalIP, nil
}
ui.Message("Machine IP is not yet found from DHCP server lease, Trying again")
time.Sleep(sshHostSleepDuration)
}
return "", errors.New("couldn't determine address for instance")
}
}
// Port returns a function that can be given to the SSH communicator
// for determining the SSH Port
func Port() func(multistep.StateBag) (int, error) {
return func(state multistep.StateBag) (int, error) {
return 22, nil
}
}