2019-02-04 13:17:32 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2020-08-20 21:35:51 -04:00
|
|
|
"context"
|
2019-02-04 13:17:32 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-08-20 21:35:51 -04:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/antihax/optional"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-08-20 21:35:51 -04:00
|
|
|
"github.com/outscale/osc-sdk-go/osc"
|
2019-02-04 13:17:32 -05:00
|
|
|
)
|
|
|
|
|
2020-08-20 21:35:51 -04:00
|
|
|
type oscDescriber interface {
|
|
|
|
ReadVms(ctx context.Context, localVarOptionals *osc.ReadVmsOpts) (osc.ReadVmsResponse, *http.Response, error)
|
|
|
|
}
|
|
|
|
|
2019-02-04 13:17:32 -05:00
|
|
|
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.
|
2020-09-30 11:37:09 -04:00
|
|
|
func SSHHost(e oscDescriber, sshInterface string) func(multistep.StateBag) (string, error) {
|
2019-02-04 13:17:32 -05:00
|
|
|
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
|
2020-08-20 21:37:09 -04:00
|
|
|
i := state.Get("vm").(osc.Vm)
|
2019-09-05 09:21:07 -04:00
|
|
|
|
2019-02-04 13:17:32 -05:00
|
|
|
if sshInterface != "" {
|
|
|
|
switch sshInterface {
|
|
|
|
case "public_ip":
|
2019-11-26 11:46:00 -05:00
|
|
|
if i.PublicIp != "" {
|
|
|
|
host = i.PublicIp
|
2019-02-04 13:17:32 -05:00
|
|
|
}
|
|
|
|
case "public_dns":
|
2019-11-26 11:46:00 -05:00
|
|
|
if i.PublicDnsName != "" {
|
|
|
|
host = i.PublicDnsName
|
2019-09-05 09:21:07 -04:00
|
|
|
}
|
|
|
|
case "private_ip":
|
2019-11-26 11:46:00 -05:00
|
|
|
if i.PrivateIp != "" {
|
|
|
|
host = i.PrivateIp
|
2019-02-04 13:17:32 -05:00
|
|
|
}
|
|
|
|
case "private_dns":
|
2019-11-26 11:46:00 -05:00
|
|
|
if i.PrivateDnsName != "" {
|
|
|
|
host = i.PrivateDnsName
|
2019-02-04 13:17:32 -05:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown interface type: %s", sshInterface))
|
|
|
|
}
|
|
|
|
} else if i.NetId != "" {
|
2019-11-26 11:46:00 -05:00
|
|
|
if i.PublicIp != "" {
|
|
|
|
host = i.PublicIp
|
|
|
|
} else if i.PrivateIp != "" {
|
|
|
|
host = i.PrivateIp
|
2019-02-04 13:17:32 -05:00
|
|
|
}
|
2019-11-26 11:46:00 -05:00
|
|
|
} else if i.PublicDnsName != "" {
|
|
|
|
host = i.PublicDnsName
|
2019-02-04 13:17:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if host != "" {
|
|
|
|
return host, nil
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:37:09 -04:00
|
|
|
r, _, err := e.ReadVms(context.Background(), &osc.ReadVmsOpts{
|
|
|
|
ReadVmsRequest: optional.NewInterface(osc.ReadVmsRequest{
|
|
|
|
Filters: osc.FiltersVm{
|
|
|
|
VmIds: []string{i.VmId},
|
|
|
|
},
|
|
|
|
}),
|
2019-02-04 13:17:32 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:37:09 -04:00
|
|
|
if len(r.Vms) == 0 {
|
2019-02-04 13:17:32 -05:00
|
|
|
return "", fmt.Errorf("vm not found: %s", i.VmId)
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:37:09 -04:00
|
|
|
state.Put("vm", r.Vms[0])
|
2019-02-04 13:17:32 -05:00
|
|
|
time.Sleep(sshHostSleepDuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("couldn't determine address for vm")
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 21:35:51 -04:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|