Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions builder/osc/common/omi_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
57 changes: 41 additions & 16 deletions builder/osc/common/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"errors"
"fmt"
"sort"
"time"

"github.com/hashicorp/packer/helper/multistep"
Expand All @@ -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 != "" {
Expand All @@ -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")

}
2 changes: 1 addition & 1 deletion builder/osc/common/step_run_source_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down