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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
package common
import (
"context"
"errors"
"fmt"
"time"
"net/http"
"github.com/antihax/optional"
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
"github.com/outscale/osc-sdk-go/osc"
)
type oscDescriber interface {
ReadVms(ctx context.Context, localVarOptionals *osc.ReadVmsOpts) (osc.ReadVmsResponse, *http.Response, error)
}
var (
// modified in tests
sshHostSleepDuration = time.Second
)
// SSHHost returns a function that can be given to the SSH communicator
// for determining the SSH address based on the vm DNS name.
func SSHHost(e oscDescriber, sshInterface string) func(multistep.StateBag) (string, error) {
return func(state multistep.StateBag) (string, error) {
const tries = 2
// <= with current structure to check result of describing `tries` times
for j := 0; j <= tries; j++ {
var host string
i := state.Get("vm").(osc.Vm)
if sshInterface != "" {
switch sshInterface {
case "public_ip":
if i.PublicIp != "" {
host = i.PublicIp
}
case "public_dns":
if i.PublicDnsName != "" {
host = i.PublicDnsName
}
case "private_ip":
if i.PrivateIp != "" {
host = i.PrivateIp
}
case "private_dns":
if i.PrivateDnsName != "" {
host = i.PrivateDnsName
}
default:
panic(fmt.Sprintf("Unknown interface type: %s", sshInterface))
}
} else if i.NetId != "" {
if i.PublicIp != "" {
host = i.PublicIp
} else if i.PrivateIp != "" {
host = i.PrivateIp
}
} else if i.PublicDnsName != "" {
host = i.PublicDnsName
}
if host != "" {
return host, nil
}
r, _, err := e.ReadVms(context.Background(), &osc.ReadVmsOpts{
ReadVmsRequest: optional.NewInterface(osc.ReadVmsRequest{
Filters: osc.FiltersVm{
VmIds: []string{i.VmId},
},
}),
})
if err != nil {
return "", err
}
if len(r.Vms) == 0 {
return "", fmt.Errorf("vm not found: %s", i.VmId)
}
state.Put("vm", r.Vms[0])
time.Sleep(sshHostSleepDuration)
}
return "", errors.New("couldn't determine address for vm")
}
}
// SSHHost returns a function that can be given to the SSH communicator
// for determining the SSH address based on the vm DNS name.
func OscSSHHost(e oscDescriber, sshInterface string) func(multistep.StateBag) (string, error) {
return func(state multistep.StateBag) (string, error) {
const tries = 2
// <= with current structure to check result of describing `tries` times
for j := 0; j <= tries; j++ {
var host string
i := state.Get("vm").(osc.Vm)
if sshInterface != "" {
switch sshInterface {
case "public_ip":
if i.PublicIp != "" {
host = i.PublicIp
}
case "public_dns":
if i.PublicDnsName != "" {
host = i.PublicDnsName
}
case "private_ip":
if i.PrivateIp != "" {
host = i.PrivateIp
}
case "private_dns":
if i.PrivateDnsName != "" {
host = i.PrivateDnsName
}
default:
panic(fmt.Sprintf("Unknown interface type: %s", sshInterface))
}
} else if i.NetId != "" {
if i.PublicIp != "" {
host = i.PublicIp
} else if i.PrivateIp != "" {
host = i.PrivateIp
}
} else if i.PublicDnsName != "" {
host = i.PublicDnsName
}
if host != "" {
return host, nil
}
r, _, err := e.ReadVms(context.Background(), &osc.ReadVmsOpts{
ReadVmsRequest: optional.NewInterface(osc.ReadVmsRequest{
Filters: osc.FiltersVm{
VmIds: []string{i.VmId},
},
}),
})
if err != nil {
return "", err
}
if len(r.Vms) == 0 {
return "", fmt.Errorf("vm not found: %s", i.VmId)
}
state.Put("vm", r.Vms[0])
time.Sleep(sshHostSleepDuration)
}
return "", errors.New("couldn't determine address for vm")
}
}
|