Merge pull request #8077 from marinsalinas/master

Outscale Builder: Get SSH Host from VM.Nics insead VM's Root
This commit is contained in:
Adrien Delorme 2019-09-05 15:40:23 +02:00 committed by GitHub
commit a5b899770e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 26 deletions

View File

@ -25,15 +25,6 @@ type OMIConfig struct {
SnapshotGroups []string `mapstructure:"snapshot_groups"`
}
func stringInSlice(s []string, searchstr string) bool {
for _, item := range s {
if item == searchstr {
return true
}
}
return false
}
func (c *OMIConfig) Prepare(accessConfig *AccessConfig, ctx *interpolate.Context) []error {
var errs []error

View File

@ -3,6 +3,7 @@ package common
import (
"errors"
"fmt"
"sort"
"time"
"github.com/hashicorp/packer/helper/multistep"
@ -27,35 +28,42 @@ func SSHHost(e oapiDescriber, sshInterface string) func(multistep.StateBag) (str
for j := 0; j <= tries; j++ {
var host string
i := state.Get("vm").(oapi.Vm)
if len(i.Nics) <= 0 {
return "", errors.New("couldn't determine address for vm, nics are empty")
}
nic := i.Nics[0]
if sshInterface != "" {
switch sshInterface {
case "public_ip":
if i.PublicIp != "" {
host = i.PublicIp
}
case "private_ip":
if i.PrivateIp != "" {
host = i.PrivateIp
if nic.LinkPublicIp.PublicIp != "" {
host = nic.LinkPublicIp.PublicIp
}
case "public_dns":
if i.PublicDnsName != "" {
host = i.PublicDnsName
if nic.LinkPublicIp.PublicDnsName != "" {
host = nic.LinkPublicIp.PublicDnsName
}
case "private_ip":
if privateIP, err := getPrivateIP(nic); err != nil {
host = privateIP.PrivateIp
}
case "private_dns":
if i.PrivateDnsName != "" {
host = i.PrivateDnsName
if privateIP, err := getPrivateIP(nic); err != nil {
host = privateIP.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
if nic.LinkPublicIp.PublicIp != "" {
host = nic.LinkPublicIp.PublicIp
} else if privateIP, err := getPrivateIP(nic); err != nil {
host = privateIP.PrivateIp
}
} else if i.PublicDnsName != "" {
host = i.PublicDnsName
} else if nic.LinkPublicIp.PublicDnsName != "" {
host = nic.LinkPublicIp.PublicDnsName
}
if host != "" {
@ -82,3 +90,20 @@ func SSHHost(e oapiDescriber, sshInterface string) func(multistep.StateBag) (str
return "", errors.New("couldn't determine address for vm")
}
}
func getPrivateIP(nic oapi.NicLight) (oapi.PrivateIpLightForVm, error) {
isPrimary := true
i := sort.Search(len(nic.PrivateIps), func(i int) bool { return nic.PrivateIps[i].IsPrimary == isPrimary })
if i < len(nic.PrivateIps) && nic.PrivateIps[i].IsPrimary == isPrimary {
return nic.PrivateIps[i], nil
}
if len(nic.PrivateIps) > 0 {
return nic.PrivateIps[0], nil
}
return oapi.PrivateIpLightForVm{}, fmt.Errorf("couldn't determine private address for vm")
}

View File

@ -248,7 +248,7 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
}
if vm.PrivateIp != "" {
ui.Message(fmt.Sprintf("Private IP: %s", vm.PublicIp))
ui.Message(fmt.Sprintf("Private IP: %s", vm.PrivateIp))
}
}