Allow configurable VNC bind IP for VMware builders

Signed-off-by: Ian Duffy <ian@ianduffy.ie>
This commit is contained in:
Ian Duffy 2016-05-23 14:02:56 +01:00
parent 873760e69e
commit 0327f6c935
5 changed files with 24 additions and 15 deletions

View File

@ -11,8 +11,9 @@ type RunConfig struct {
Headless bool `mapstructure:"headless"` Headless bool `mapstructure:"headless"`
RawBootWait string `mapstructure:"boot_wait"` RawBootWait string `mapstructure:"boot_wait"`
VNCPortMin uint `mapstructure:"vnc_port_min"` VNCBindAddress string `mapstructure:"vnc_bind_address"`
VNCPortMax uint `mapstructure:"vnc_port_max"` VNCPortMin uint `mapstructure:"vnc_port_min"`
VNCPortMax uint `mapstructure:"vnc_port_max"`
BootWait time.Duration `` BootWait time.Duration ``
} }
@ -30,6 +31,10 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
c.VNCPortMax = 6000 c.VNCPortMax = 6000
} }
if c.VNCBindAddress == "" {
c.VNCBindAddress = "127.0.0.1"
}
var errs []error var errs []error
var err error var err error
if c.RawBootWait != "" { if c.RawBootWait != "" {

View File

@ -21,15 +21,16 @@ import (
// Produces: // Produces:
// vnc_port uint - The port that VNC is configured to listen on. // vnc_port uint - The port that VNC is configured to listen on.
type StepConfigureVNC struct { type StepConfigureVNC struct {
VNCPortMin uint VNCBindAddress string
VNCPortMax uint VNCPortMin uint
VNCPortMax uint
} }
type VNCAddressFinder interface { type VNCAddressFinder interface {
VNCAddress(uint, uint) (string, uint, error) VNCAddress(string, uint, uint) (string, uint, error)
} }
func (StepConfigureVNC) VNCAddress(portMin, portMax uint) (string, uint, error) { func (StepConfigureVNC) VNCAddress(vncBindAddress string, portMin, portMax uint) (string, uint, error) {
// Find an open VNC port. Note that this can still fail later on // Find an open VNC port. Note that this can still fail later on
// because we have to release the port at some point. But this does its // because we have to release the port at some point. But this does its
// best. // best.
@ -43,13 +44,13 @@ func (StepConfigureVNC) VNCAddress(portMin, portMax uint) (string, uint, error)
} }
log.Printf("Trying port: %d", vncPort) log.Printf("Trying port: %d", vncPort)
l, err := net.Listen("tcp", fmt.Sprintf(":%d", vncPort)) l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", vncBindAddress, vncPort))
if err == nil { if err == nil {
defer l.Close() defer l.Close()
break break
} }
} }
return "127.0.0.1", vncPort, nil return vncBindAddress, vncPort, nil
} }
func (s *StepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction { func (s *StepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction {
@ -80,7 +81,7 @@ func (s *StepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction {
vncFinder = s vncFinder = s
} }
log.Printf("Looking for available port between %d and %d", s.VNCPortMin, s.VNCPortMax) log.Printf("Looking for available port between %d and %d", s.VNCPortMin, s.VNCPortMax)
vncIp, vncPort, err := vncFinder.VNCAddress(s.VNCPortMin, s.VNCPortMax) vncBindAddress, vncPort, err := vncFinder.VNCAddress(s.VNCBindAddress, s.VNCPortMin, s.VNCPortMax)
if err != nil { if err != nil {
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
@ -92,6 +93,7 @@ func (s *StepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction {
vmxData := ParseVMX(string(vmxBytes)) vmxData := ParseVMX(string(vmxBytes))
vmxData["remotedisplay.vnc.enabled"] = "TRUE" vmxData["remotedisplay.vnc.enabled"] = "TRUE"
vmxData["remotedisplay.vnc.port"] = fmt.Sprintf("%d", vncPort) vmxData["remotedisplay.vnc.port"] = fmt.Sprintf("%d", vncPort)
vmxData["remotedisplay.vnc.ip"] = fmt.Sprintf("%s", vncBindAddress)
if err := WriteVMX(vmxPath, vmxData); err != nil { if err := WriteVMX(vmxPath, vmxData); err != nil {
err := fmt.Errorf("Error writing VMX data: %s", err) err := fmt.Errorf("Error writing VMX data: %s", err)
@ -101,7 +103,7 @@ func (s *StepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction {
} }
state.Put("vnc_port", vncPort) state.Put("vnc_port", vncPort)
state.Put("vnc_ip", vncIp) state.Put("vnc_ip", vncBindAddress)
return multistep.ActionContinue return multistep.ActionContinue
} }

View File

@ -254,8 +254,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
HTTPPortMax: b.config.HTTPPortMax, HTTPPortMax: b.config.HTTPPortMax,
}, },
&vmwcommon.StepConfigureVNC{ &vmwcommon.StepConfigureVNC{
VNCPortMin: b.config.VNCPortMin, VNCBindAddress: b.config.VNCBindAddress,
VNCPortMax: b.config.VNCPortMax, VNCPortMin: b.config.VNCPortMin,
VNCPortMax: b.config.VNCPortMax,
}, },
&StepRegister{ &StepRegister{
Format: b.config.Format, Format: b.config.Format,

View File

@ -176,7 +176,7 @@ func (d *ESX5Driver) HostIP() (string, error) {
return host, err return host, err
} }
func (d *ESX5Driver) VNCAddress(portMin, portMax uint) (string, uint, error) { func (d *ESX5Driver) VNCAddress(vncBindIP string, portMin, portMax uint) (string, uint, error) {
var vncPort uint var vncPort uint
//Process ports ESXi is listening on to determine which are available //Process ports ESXi is listening on to determine which are available

View File

@ -79,8 +79,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
HTTPPortMax: b.config.HTTPPortMax, HTTPPortMax: b.config.HTTPPortMax,
}, },
&vmwcommon.StepConfigureVNC{ &vmwcommon.StepConfigureVNC{
VNCPortMin: b.config.VNCPortMin, VNCBindAddress: b.config.VNCBindAddress,
VNCPortMax: b.config.VNCPortMax, VNCPortMin: b.config.VNCPortMin,
VNCPortMax: b.config.VNCPortMax,
}, },
&vmwcommon.StepRun{ &vmwcommon.StepRun{
BootWait: b.config.BootWait, BootWait: b.config.BootWait,