diff --git a/builder/osc/common/omi_config.go b/builder/osc/common/omi_config.go index 3cb352e65..c4c8e5e0d 100644 --- a/builder/osc/common/omi_config.go +++ b/builder/osc/common/omi_config.go @@ -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 diff --git a/builder/osc/common/ssh.go b/builder/osc/common/ssh.go index 751c3c925..f00c7710e 100644 --- a/builder/osc/common/ssh.go +++ b/builder/osc/common/ssh.go @@ -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") + +} diff --git a/builder/osc/common/step_run_source_vm.go b/builder/osc/common/step_run_source_vm.go index 85e74e68c..04fe1d5b3 100644 --- a/builder/osc/common/step_run_source_vm.go +++ b/builder/osc/common/step_run_source_vm.go @@ -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)) } }