From 759c46487cb715eef35038badf0c757f071e0925 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 5 Sep 2014 11:59:46 -0700 Subject: [PATCH] builder/vmware-vmx: support http files --- CHANGELOG.md | 2 ++ builder/vmware/common/run_config.go | 21 ++++++++++++++++- .../{iso => common}/step_http_server.go | 22 ++++++++++-------- builder/vmware/iso/builder.go | 23 ++++--------------- builder/vmware/vmx/builder.go | 5 ++++ 5 files changed, 44 insertions(+), 29 deletions(-) rename builder/vmware/{iso => common}/step_http_server.go (77%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2df7edb6d..4b04d207f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ FEATURES: * builder/virtualbox-ovf: Boot commands and the HTTP server are supported. [GH-1169] * builder/vmware: VMware Player 6 is now supported. [GH-1168] + * builder/vmware-vmx: Boot commands and the HTTP server are supported. + [GH-1169] IMPROVEMENTS: diff --git a/builder/vmware/common/run_config.go b/builder/vmware/common/run_config.go index ce3480e9e..ffcb3d040 100644 --- a/builder/vmware/common/run_config.go +++ b/builder/vmware/common/run_config.go @@ -1,6 +1,7 @@ package common import ( + "errors" "fmt" "time" @@ -11,6 +12,10 @@ type RunConfig struct { Headless bool `mapstructure:"headless"` RawBootWait string `mapstructure:"boot_wait"` + HTTPDir string `mapstructure:"http_directory"` + HTTPPortMin uint `mapstructure:"http_port_min"` + HTTPPortMax uint `mapstructure:"http_port_max"` + BootWait time.Duration `` } @@ -19,8 +24,17 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error { c.RawBootWait = "10s" } + if c.HTTPPortMin == 0 { + c.HTTPPortMin = 8000 + } + + if c.HTTPPortMax == 0 { + c.HTTPPortMax = 9000 + } + templates := map[string]*string{ - "boot_wait": &c.RawBootWait, + "boot_wait": &c.RawBootWait, + "http_directory": &c.HTTPDir, } var err error @@ -40,5 +54,10 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error { } } + if c.HTTPPortMin > c.HTTPPortMax { + errs = append(errs, + errors.New("http_port_min must be less than http_port_max")) + } + return errs } diff --git a/builder/vmware/iso/step_http_server.go b/builder/vmware/common/step_http_server.go similarity index 77% rename from builder/vmware/iso/step_http_server.go rename to builder/vmware/common/step_http_server.go index 3d86139e3..440d9adbc 100644 --- a/builder/vmware/iso/step_http_server.go +++ b/builder/vmware/common/step_http_server.go @@ -1,4 +1,4 @@ -package iso +package common import ( "fmt" @@ -15,28 +15,30 @@ import ( // template. // // Uses: -// config *config // ui packer.Ui // // Produces: // http_port int - The port the HTTP server started on. -type stepHTTPServer struct { +type StepHTTPServer struct { + HTTPDir string + HTTPPortMin uint + HTTPPortMax uint + l net.Listener } -func (s *stepHTTPServer) Run(state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(*config) +func (s *StepHTTPServer) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) var httpPort uint = 0 - if config.HTTPDir == "" { + if s.HTTPDir == "" { state.Put("http_port", httpPort) return multistep.ActionContinue } // Find an available TCP port for our HTTP server var httpAddr string - portRange := int(config.HTTPPortMax - config.HTTPPortMin) + portRange := int(s.HTTPPortMax - s.HTTPPortMin) for { var err error var offset uint = 0 @@ -46,7 +48,7 @@ func (s *stepHTTPServer) Run(state multistep.StateBag) multistep.StepAction { offset = uint(rand.Intn(portRange)) } - httpPort = offset + config.HTTPPortMin + httpPort = offset + s.HTTPPortMin httpAddr = fmt.Sprintf(":%d", httpPort) log.Printf("Trying port: %d", httpPort) s.l, err = net.Listen("tcp", httpAddr) @@ -58,7 +60,7 @@ func (s *stepHTTPServer) Run(state multistep.StateBag) multistep.StepAction { ui.Say(fmt.Sprintf("Starting HTTP server on port %d", httpPort)) // Start the HTTP server and run it in the background - fileServer := http.FileServer(http.Dir(config.HTTPDir)) + fileServer := http.FileServer(http.Dir(s.HTTPDir)) server := &http.Server{Addr: httpAddr, Handler: fileServer} go server.Serve(s.l) @@ -68,7 +70,7 @@ func (s *stepHTTPServer) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionContinue } -func (s *stepHTTPServer) Cleanup(multistep.StateBag) { +func (s *StepHTTPServer) Cleanup(multistep.StateBag) { if s.l != nil { // Close the listener so that the HTTP server stops s.l.Close() diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 574b2fb24..80b3dfb98 100644 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -41,9 +41,6 @@ type config struct { ISOChecksumType string `mapstructure:"iso_checksum_type"` ISOUrls []string `mapstructure:"iso_urls"` VMName string `mapstructure:"vm_name"` - HTTPDir string `mapstructure:"http_directory"` - HTTPPortMin uint `mapstructure:"http_port_min"` - HTTPPortMax uint `mapstructure:"http_port_max"` BootCommand []string `mapstructure:"boot_command"` SkipCompaction bool `mapstructure:"skip_compaction"` VMXTemplatePath string `mapstructure:"vmx_template_path"` @@ -115,14 +112,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { b.config.VMName = fmt.Sprintf("packer-%s", b.config.PackerBuildName) } - if b.config.HTTPPortMin == 0 { - b.config.HTTPPortMin = 8000 - } - - if b.config.HTTPPortMax == 0 { - b.config.HTTPPortMax = 9000 - } - if b.config.VNCPortMin == 0 { b.config.VNCPortMin = 5900 } @@ -147,7 +136,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { templates := map[string]*string{ "disk_name": &b.config.DiskName, "guest_os_type": &b.config.GuestOSType, - "http_directory": &b.config.HTTPDir, "iso_checksum": &b.config.ISOChecksum, "iso_checksum_type": &b.config.ISOChecksumType, "iso_url": &b.config.RawSingleISOUrl, @@ -195,11 +183,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } } - if b.config.HTTPPortMin > b.config.HTTPPortMax { - errs = packer.MultiErrorAppend( - errs, errors.New("http_port_min must be less than http_port_max")) - } - if b.config.ISOChecksumType == "" { errs = packer.MultiErrorAppend( errs, errors.New("The iso_checksum_type must be specified.")) @@ -340,7 +323,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe CustomData: b.config.VMXData, }, &vmwcommon.StepSuppressMessages{}, - &stepHTTPServer{}, + &vmwcommon.StepHTTPServer{ + HTTPDir: b.config.HTTPDir, + HTTPPortMin: b.config.HTTPPortMin, + HTTPPortMax: b.config.HTTPPortMax, + }, &stepConfigureVNC{}, &StepRegister{}, &vmwcommon.StepRun{ diff --git a/builder/vmware/vmx/builder.go b/builder/vmware/vmx/builder.go index 1d44f4485..fe30988df 100644 --- a/builder/vmware/vmx/builder.go +++ b/builder/vmware/vmx/builder.go @@ -62,6 +62,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &common.StepCreateFloppy{ Files: b.config.FloppyFiles, }, + &vmwcommon.StepHTTPServer{ + HTTPDir: b.config.HTTPDir, + HTTPPortMin: b.config.HTTPPortMin, + HTTPPortMax: b.config.HTTPPortMax, + }, &StepCloneVMX{ OutputDir: b.config.OutputDir, Path: b.config.SourcePath,