Do not create a random public port (#4149)

This is meant to be a gentle solution for a very specific use case, but is causing more issues then it solves.

If you have a port conflict when trying to use an already associated public IP, the easiest way around it is to let the builder associate a new temporary public IP address.
This commit is contained in:
Sander van Harmelen 2016-11-10 13:59:58 +01:00 committed by GitHub
parent d1eadd91bb
commit 68c6835bb5
2 changed files with 11 additions and 30 deletions

View File

@ -2,19 +2,14 @@ package cloudstack
import (
"fmt"
"math/rand"
"strings"
"time"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"github.com/xanzy/go-cloudstack/cloudstack"
)
type stepSetupNetworking struct {
privatePort int
publicPort int
}
type stepSetupNetworking struct{}
func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*cloudstack.CloudStackClient)
@ -28,20 +23,6 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction
ui.Message("Networking has been setup!")
return multistep.ActionContinue
}
// Generate a random public port used to configure our port forward.
rand.Seed(time.Now().UnixNano())
s.publicPort = 50000 + rand.Intn(10000)
// Set the currently configured port to be the private port.
s.privatePort = config.Comm.Port()
// Set the SSH or WinRM port to be the randomly generated public port.
switch config.Comm.Type {
case "ssh":
config.Comm.SSHPort = s.publicPort
case "winrm":
config.Comm.WinRMPort = s.publicPort
}
// Retrieve the instance ID from the previously saved state.
instanceID, ok := state.Get("instance_id").(string)
@ -95,9 +76,9 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction
ui.Message("Creating port forward...")
p := client.Firewall.NewCreatePortForwardingRuleParams(
config.PublicIPAddress,
s.privatePort,
config.Comm.Port(),
"TCP",
s.publicPort,
config.Comm.Port(),
instanceID,
)
@ -129,8 +110,8 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction
p.SetAclid(network.Aclid)
p.SetAction("allow")
p.SetCidrlist(config.CIDRList)
p.SetStartport(s.publicPort)
p.SetEndport(s.publicPort)
p.SetStartport(config.Comm.Port())
p.SetEndport(config.Comm.Port())
p.SetTraffictype("ingress")
// Create the network ACL rule.
@ -150,8 +131,8 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction
// Configure the firewall rule.
p.SetCidrlist(config.CIDRList)
p.SetStartport(s.publicPort)
p.SetEndport(s.publicPort)
p.SetStartport(config.Comm.Port())
p.SetEndport(config.Comm.Port())
fwRule, err := client.Firewall.CreateFirewallRule(p)
if err != nil {

View File

@ -32,6 +32,10 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction
p.SetName(config.InstanceName)
p.SetDisplayname("Created by Packer")
if config.Keypair != "" {
p.SetKeypair(config.Keypair)
}
// If we use an ISO, configure the disk offering.
if config.SourceISO != "" {
p.SetDiskofferingid(config.DiskOffering)
@ -60,10 +64,6 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction
p.SetProjectid(config.Project)
}
if config.Keypair != "" {
p.SetKeypair(config.Keypair)
}
if config.UserData != "" {
ud, err := getUserData(config.UserData, config.HTTPGetOnly)
if err != nil {