From 1a45b96674ba41da76430d0ffa3e1cea420a7c0d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 5 Sep 2014 12:10:40 -0700 Subject: [PATCH] builder/vmware: add VNC to vmx --- builder/vmware/common/run_config.go | 16 ++++++++++ .../{iso => common}/step_configure_vnc.go | 31 ++++++++++--------- builder/vmware/iso/builder.go | 20 +++--------- builder/vmware/vmx/builder.go | 4 +++ 4 files changed, 40 insertions(+), 31 deletions(-) rename builder/vmware/{iso => common}/step_configure_vnc.go (74%) diff --git a/builder/vmware/common/run_config.go b/builder/vmware/common/run_config.go index ffcb3d040..2315f7769 100644 --- a/builder/vmware/common/run_config.go +++ b/builder/vmware/common/run_config.go @@ -16,6 +16,9 @@ type RunConfig struct { HTTPPortMin uint `mapstructure:"http_port_min"` HTTPPortMax uint `mapstructure:"http_port_max"` + VNCPortMin uint `mapstructure:"vnc_port_min"` + VNCPortMax uint `mapstructure:"vnc_port_max"` + BootWait time.Duration `` } @@ -32,6 +35,14 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error { c.HTTPPortMax = 9000 } + if c.VNCPortMin == 0 { + c.VNCPortMin = 5900 + } + + if c.VNCPortMax == 0 { + c.VNCPortMax = 6000 + } + templates := map[string]*string{ "boot_wait": &c.RawBootWait, "http_directory": &c.HTTPDir, @@ -59,5 +70,10 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error { errors.New("http_port_min must be less than http_port_max")) } + if c.VNCPortMin > c.VNCPortMax { + errs = append( + errs, fmt.Errorf("vnc_port_min must be less than vnc_port_max")) + } + return errs } diff --git a/builder/vmware/iso/step_configure_vnc.go b/builder/vmware/common/step_configure_vnc.go similarity index 74% rename from builder/vmware/iso/step_configure_vnc.go rename to builder/vmware/common/step_configure_vnc.go index a09d92baa..3e884887f 100644 --- a/builder/vmware/iso/step_configure_vnc.go +++ b/builder/vmware/common/step_configure_vnc.go @@ -1,33 +1,35 @@ -package iso +package common import ( "fmt" - "github.com/mitchellh/multistep" - vmwcommon "github.com/mitchellh/packer/builder/vmware/common" - "github.com/mitchellh/packer/packer" "io/ioutil" "log" "math/rand" "net" "os" + + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" ) // This step configures the VM to enable the VNC server. // // Uses: -// config *config // ui packer.Ui // vmx_path string // // Produces: // vnc_port uint - The port that VNC is configured to listen on. -type stepConfigureVNC struct{} +type StepConfigureVNC struct{ + VNCPortMin uint + VNCPortMax uint +} type VNCAddressFinder interface { VNCAddress(uint, uint) (string, uint, error) } -func (stepConfigureVNC) VNCAddress(portMin, portMax uint) (string, uint, error) { +func (StepConfigureVNC) VNCAddress(portMin, portMax uint) (string, uint, error) { // 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 // best. @@ -50,9 +52,8 @@ func (stepConfigureVNC) VNCAddress(portMin, portMax uint) (string, uint, error) return "127.0.0.1", vncPort, nil } -func (s *stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(*config) - driver := state.Get("driver").(vmwcommon.Driver) +func (s *StepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction { + driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) vmxPath := state.Get("vmx_path").(string) @@ -78,8 +79,8 @@ func (s *stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction { } else { vncFinder = s } - log.Printf("Looking for available port between %d and %d", config.VNCPortMin, config.VNCPortMax) - vncIp, vncPort, err := vncFinder.VNCAddress(config.VNCPortMin, config.VNCPortMax) + log.Printf("Looking for available port between %d and %d", s.VNCPortMin, s.VNCPortMax) + vncIp, vncPort, err := vncFinder.VNCAddress(s.VNCPortMin, s.VNCPortMax) if err != nil { state.Put("error", err) ui.Error(err.Error()) @@ -88,11 +89,11 @@ func (s *stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction { log.Printf("Found available VNC port: %d", vncPort) - vmxData := vmwcommon.ParseVMX(string(vmxBytes)) + vmxData := ParseVMX(string(vmxBytes)) vmxData["remotedisplay.vnc.enabled"] = "TRUE" vmxData["remotedisplay.vnc.port"] = fmt.Sprintf("%d", vncPort) - if err := vmwcommon.WriteVMX(vmxPath, vmxData); err != nil { + if err := WriteVMX(vmxPath, vmxData); err != nil { err := fmt.Errorf("Error writing VMX data: %s", err) state.Put("error", err) ui.Error(err.Error()) @@ -105,5 +106,5 @@ func (s *stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionContinue } -func (stepConfigureVNC) Cleanup(multistep.StateBag) { +func (StepConfigureVNC) Cleanup(multistep.StateBag) { } diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 80b3dfb98..693d97e9b 100644 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -44,8 +44,6 @@ type config struct { BootCommand []string `mapstructure:"boot_command"` SkipCompaction bool `mapstructure:"skip_compaction"` VMXTemplatePath string `mapstructure:"vmx_template_path"` - VNCPortMin uint `mapstructure:"vnc_port_min"` - VNCPortMax uint `mapstructure:"vnc_port_max"` RemoteType string `mapstructure:"remote_type"` RemoteDatastore string `mapstructure:"remote_datastore"` @@ -112,14 +110,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { b.config.VMName = fmt.Sprintf("packer-%s", b.config.PackerBuildName) } - if b.config.VNCPortMin == 0 { - b.config.VNCPortMin = 5900 - } - - if b.config.VNCPortMax == 0 { - b.config.VNCPortMax = 6000 - } - if b.config.RemoteUser == "" { b.config.RemoteUser = "root" } @@ -230,11 +220,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } - if b.config.VNCPortMin > b.config.VNCPortMax { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("vnc_port_min must be less than vnc_port_max")) - } - // Remote configuration validation if b.config.RemoteType != "" { if b.config.RemoteHost == "" { @@ -328,7 +313,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe HTTPPortMin: b.config.HTTPPortMin, HTTPPortMax: b.config.HTTPPortMax, }, - &stepConfigureVNC{}, + &vmwcommon.StepConfigureVNC{ + VNCPortMin: b.config.VNCPortMin, + VNCPortMax: b.config.VNCPortMax, + }, &StepRegister{}, &vmwcommon.StepRun{ BootWait: b.config.BootWait, diff --git a/builder/vmware/vmx/builder.go b/builder/vmware/vmx/builder.go index fe30988df..91f221dd6 100644 --- a/builder/vmware/vmx/builder.go +++ b/builder/vmware/vmx/builder.go @@ -67,6 +67,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe HTTPPortMin: b.config.HTTPPortMin, HTTPPortMax: b.config.HTTPPortMax, }, + &vmwcommon.StepConfigureVNC{ + VNCPortMin: b.config.VNCPortMin, + VNCPortMax: b.config.VNCPortMax, + }, &StepCloneVMX{ OutputDir: b.config.OutputDir, Path: b.config.SourcePath,