From 9ec319e296765208dd543decdb52656392475678 Mon Sep 17 00:00:00 2001 From: Ian Duffy Date: Mon, 23 May 2016 14:24:16 +0100 Subject: [PATCH 1/2] Allow configurable VNC bind IP for VirtualBox builders Signed-off-by: Ian Duffy --- builder/virtualbox/common/run_config.go | 9 +++++-- builder/virtualbox/common/run_config_test.go | 24 +++++++++++++++++++ .../virtualbox/common/step_configure_vrdp.go | 13 +++++----- builder/virtualbox/iso/builder.go | 5 ++-- builder/virtualbox/ovf/builder.go | 5 ++-- 5 files changed, 44 insertions(+), 12 deletions(-) diff --git a/builder/virtualbox/common/run_config.go b/builder/virtualbox/common/run_config.go index 653b19294..595667b11 100644 --- a/builder/virtualbox/common/run_config.go +++ b/builder/virtualbox/common/run_config.go @@ -11,8 +11,9 @@ type RunConfig struct { Headless bool `mapstructure:"headless"` RawBootWait string `mapstructure:"boot_wait"` - VRDPPortMin uint `mapstructure:"vrdp_port_min"` - VRDPPortMax uint `mapstructure:"vrdp_port_max"` + VRDPBindAddress string `mapstructure:"vrdp_bind_address"` + VRDPPortMin uint `mapstructure:"vrdp_port_min"` + VRDPPortMax uint `mapstructure:"vrdp_port_max"` BootWait time.Duration `` } @@ -22,6 +23,10 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { c.RawBootWait = "10s" } + if c.VRDPBindAddress == "" { + c.VRDPBindAddress = "127.0.0.1" + } + if c.VRDPPortMin == 0 { c.VRDPPortMin = 5900 } diff --git a/builder/virtualbox/common/run_config_test.go b/builder/virtualbox/common/run_config_test.go index 8068fe625..87b9b1b69 100644 --- a/builder/virtualbox/common/run_config_test.go +++ b/builder/virtualbox/common/run_config_test.go @@ -35,3 +35,27 @@ func TestRunConfigPrepare_BootWait(t *testing.T) { t.Fatalf("should not have error: %s", errs) } } + +func TestRunConfigPrepare_VRDPBindAddress(t *testing.T) { + var c *RunConfig + var errs []error + + // Test a default VRDPBindAddress + c = new(RunConfig) + errs = c.Prepare(testConfigTemplate(t)) + if len(errs) > 0 { + t.Fatalf("should not have error: %s", errs) + } + + if c.VRDPBindAddress != "127.0.0.1" { + t.Fatalf("bad value: %s", c.VRDPBindAddress) + } + + // Test with a good one + c = new(RunConfig) + c.VRDPBindAddress = "192.168.0.1" + errs = c.Prepare(testConfigTemplate(t)) + if len(errs) > 0 { + t.Fatalf("should not have error: %s", errs) + } +} diff --git a/builder/virtualbox/common/step_configure_vrdp.go b/builder/virtualbox/common/step_configure_vrdp.go index ba4f6b9c0..b48a70d60 100644 --- a/builder/virtualbox/common/step_configure_vrdp.go +++ b/builder/virtualbox/common/step_configure_vrdp.go @@ -21,8 +21,9 @@ import ( // Produces: // vrdp_port unit - The port that VRDP is configured to listen on. type StepConfigureVRDP struct { - VRDPPortMin uint - VRDPPortMax uint + VRDPBindAddress string + VRDPPortMin uint + VRDPPortMax uint } func (s *StepConfigureVRDP) Run(state multistep.StateBag) multistep.StepAction { @@ -30,7 +31,7 @@ func (s *StepConfigureVRDP) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) vmName := state.Get("vmName").(string) - log.Printf("Looking for available port between %d and %d", s.VRDPPortMin, s.VRDPPortMax) + log.Printf("Looking for available port between %d and %d on %s", s.VRDPPortMin, s.VRDPPortMax, s.VRDPBindAddress) var vrdpPort uint portRange := int(s.VRDPPortMax - s.VRDPPortMin) @@ -42,7 +43,7 @@ func (s *StepConfigureVRDP) Run(state multistep.StateBag) multistep.StepAction { } log.Printf("Trying port: %d", vrdpPort) - l, err := net.Listen("tcp", fmt.Sprintf(":%d", vrdpPort)) + l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.VRDPBindAddress, vrdpPort)) if err == nil { defer l.Close() break @@ -51,7 +52,7 @@ func (s *StepConfigureVRDP) Run(state multistep.StateBag) multistep.StepAction { command := []string{ "modifyvm", vmName, - "--vrdeaddress", "127.0.0.1", + "--vrdeaddress", fmt.Sprintf("%s", s.VRDPBindAddress), "--vrdeauthtype", "null", "--vrde", "on", "--vrdeport", @@ -64,7 +65,7 @@ func (s *StepConfigureVRDP) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } - state.Put("vrdpIp", "127.0.0.1") + state.Put("vrdpIp", s.VRDPBindAddress) state.Put("vrdpPort", vrdpPort) return multistep.ActionContinue diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index e4f9ebb2e..99dff6263 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -209,8 +209,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe GuestAdditionsMode: b.config.GuestAdditionsMode, }, &vboxcommon.StepConfigureVRDP{ - VRDPPortMin: b.config.VRDPPortMin, - VRDPPortMax: b.config.VRDPPortMax, + VRDPBindAddress: b.config.VRDPBindAddress, + VRDPPortMin: b.config.VRDPPortMin, + VRDPPortMax: b.config.VRDPPortMax, }, new(vboxcommon.StepAttachFloppy), &vboxcommon.StepForwardSSH{ diff --git a/builder/virtualbox/ovf/builder.go b/builder/virtualbox/ovf/builder.go index fa9c68c2f..ab99f6544 100644 --- a/builder/virtualbox/ovf/builder.go +++ b/builder/virtualbox/ovf/builder.go @@ -78,8 +78,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe GuestAdditionsMode: b.config.GuestAdditionsMode, }, &vboxcommon.StepConfigureVRDP{ - VRDPPortMin: b.config.VRDPPortMin, - VRDPPortMax: b.config.VRDPPortMax, + VRDPBindAddress: b.config.VRDPBindAddress, + VRDPPortMin: b.config.VRDPPortMin, + VRDPPortMax: b.config.VRDPPortMax, }, new(vboxcommon.StepAttachFloppy), &vboxcommon.StepForwardSSH{ From 4ca03a95c6657cc0da09deffd15d287d1215440b Mon Sep 17 00:00:00 2001 From: Ian Duffy Date: Wed, 25 May 2016 09:34:53 +0100 Subject: [PATCH 2/2] Update documentation to include vrdp_bind_address Signed-off-by: Ian Duffy --- website/source/docs/builders/virtualbox-iso.html.md | 4 ++++ website/source/docs/builders/virtualbox-ovf.html.md | 3 +++ 2 files changed, 7 insertions(+) diff --git a/website/source/docs/builders/virtualbox-iso.html.md b/website/source/docs/builders/virtualbox-iso.html.md index 2abd2f0af..59f7d1e59 100644 --- a/website/source/docs/builders/virtualbox-iso.html.md +++ b/website/source/docs/builders/virtualbox-iso.html.md @@ -242,6 +242,10 @@ builder. machine, without the file extension. By default this is "packer-BUILDNAME", where "BUILDNAME" is the name of the build. +- `vrdp_bind_address` (string / IP address) - The IP address that should be binded + to for VRDP. By default packer will use 127.0.0.1 for this. If you wish to bind + to all interfaces use 0.0.0.0 + - `vrdp_port_min` and `vrdp_port_max` (integer) - The minimum and maximum port to use for VRDP access to the virtual machine. Packer uses a randomly chosen port in this range that appears available. By default this is 5900 to 6000. diff --git a/website/source/docs/builders/virtualbox-ovf.html.md b/website/source/docs/builders/virtualbox-ovf.html.md index fe1e66b5e..0e53b0316 100644 --- a/website/source/docs/builders/virtualbox-ovf.html.md +++ b/website/source/docs/builders/virtualbox-ovf.html.md @@ -207,6 +207,9 @@ builder. is exported. By default this is "packer-BUILDNAME", where "BUILDNAME" is the name of the build. +- `vrdp_bind_address` (string / IP address) - The IP address that should be binded + to for VRDP. By default packer will use 127.0.0.1 for this. + - `vrdp_port_min` and `vrdp_port_max` (integer) - The minimum and maximum port to use for VRDP access to the virtual machine. Packer uses a randomly chosen port in this range that appears available. By default this is 5900 to 6000.