Commit 1a45b966 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/vmware: add VNC to vmx

parent b72605c2
...@@ -16,6 +16,9 @@ type RunConfig struct { ...@@ -16,6 +16,9 @@ type RunConfig struct {
HTTPPortMin uint `mapstructure:"http_port_min"` HTTPPortMin uint `mapstructure:"http_port_min"`
HTTPPortMax uint `mapstructure:"http_port_max"` HTTPPortMax uint `mapstructure:"http_port_max"`
VNCPortMin uint `mapstructure:"vnc_port_min"`
VNCPortMax uint `mapstructure:"vnc_port_max"`
BootWait time.Duration `` BootWait time.Duration ``
} }
...@@ -32,6 +35,14 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error { ...@@ -32,6 +35,14 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
c.HTTPPortMax = 9000 c.HTTPPortMax = 9000
} }
if c.VNCPortMin == 0 {
c.VNCPortMin = 5900
}
if c.VNCPortMax == 0 {
c.VNCPortMax = 6000
}
templates := map[string]*string{ templates := map[string]*string{
"boot_wait": &c.RawBootWait, "boot_wait": &c.RawBootWait,
"http_directory": &c.HTTPDir, "http_directory": &c.HTTPDir,
...@@ -59,5 +70,10 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error { ...@@ -59,5 +70,10 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
errors.New("http_port_min must be less than http_port_max")) 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 return errs
} }
package iso package common
import ( import (
"fmt" "fmt"
"github.com/mitchellh/multistep"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
"github.com/mitchellh/packer/packer"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand" "math/rand"
"net" "net"
"os" "os"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
) )
// This step configures the VM to enable the VNC server. // This step configures the VM to enable the VNC server.
// //
// Uses: // Uses:
// config *config
// ui packer.Ui // ui packer.Ui
// vmx_path string // vmx_path string
// //
// 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
VNCPortMax uint
}
type VNCAddressFinder interface { type VNCAddressFinder interface {
VNCAddress(uint, uint) (string, uint, error) 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 // 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.
...@@ -50,9 +52,8 @@ func (stepConfigureVNC) VNCAddress(portMin, portMax uint) (string, uint, error) ...@@ -50,9 +52,8 @@ func (stepConfigureVNC) VNCAddress(portMin, portMax uint) (string, uint, error)
return "127.0.0.1", vncPort, nil return "127.0.0.1", vncPort, nil
} }
func (s *stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction { func (s *StepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*config) driver := state.Get("driver").(Driver)
driver := state.Get("driver").(vmwcommon.Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
vmxPath := state.Get("vmx_path").(string) vmxPath := state.Get("vmx_path").(string)
...@@ -78,8 +79,8 @@ func (s *stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction { ...@@ -78,8 +79,8 @@ func (s *stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction {
} else { } else {
vncFinder = s vncFinder = s
} }
log.Printf("Looking for available port between %d and %d", config.VNCPortMin, config.VNCPortMax) log.Printf("Looking for available port between %d and %d", s.VNCPortMin, s.VNCPortMax)
vncIp, vncPort, err := vncFinder.VNCAddress(config.VNCPortMin, config.VNCPortMax) vncIp, vncPort, err := vncFinder.VNCAddress(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())
...@@ -88,11 +89,11 @@ func (s *stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction { ...@@ -88,11 +89,11 @@ func (s *stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction {
log.Printf("Found available VNC port: %d", vncPort) 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.enabled"] = "TRUE"
vmxData["remotedisplay.vnc.port"] = fmt.Sprintf("%d", vncPort) 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) err := fmt.Errorf("Error writing VMX data: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
...@@ -105,5 +106,5 @@ func (s *stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction { ...@@ -105,5 +106,5 @@ func (s *stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionContinue return multistep.ActionContinue
} }
func (stepConfigureVNC) Cleanup(multistep.StateBag) { func (StepConfigureVNC) Cleanup(multistep.StateBag) {
} }
...@@ -44,8 +44,6 @@ type config struct { ...@@ -44,8 +44,6 @@ type config struct {
BootCommand []string `mapstructure:"boot_command"` BootCommand []string `mapstructure:"boot_command"`
SkipCompaction bool `mapstructure:"skip_compaction"` SkipCompaction bool `mapstructure:"skip_compaction"`
VMXTemplatePath string `mapstructure:"vmx_template_path"` VMXTemplatePath string `mapstructure:"vmx_template_path"`
VNCPortMin uint `mapstructure:"vnc_port_min"`
VNCPortMax uint `mapstructure:"vnc_port_max"`
RemoteType string `mapstructure:"remote_type"` RemoteType string `mapstructure:"remote_type"`
RemoteDatastore string `mapstructure:"remote_datastore"` RemoteDatastore string `mapstructure:"remote_datastore"`
...@@ -112,14 +110,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { ...@@ -112,14 +110,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
b.config.VMName = fmt.Sprintf("packer-%s", b.config.PackerBuildName) 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 == "" { if b.config.RemoteUser == "" {
b.config.RemoteUser = "root" b.config.RemoteUser = "root"
} }
...@@ -230,11 +220,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { ...@@ -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 // Remote configuration validation
if b.config.RemoteType != "" { if b.config.RemoteType != "" {
if b.config.RemoteHost == "" { if b.config.RemoteHost == "" {
...@@ -328,7 +313,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -328,7 +313,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
HTTPPortMin: b.config.HTTPPortMin, HTTPPortMin: b.config.HTTPPortMin,
HTTPPortMax: b.config.HTTPPortMax, HTTPPortMax: b.config.HTTPPortMax,
}, },
&stepConfigureVNC{}, &vmwcommon.StepConfigureVNC{
VNCPortMin: b.config.VNCPortMin,
VNCPortMax: b.config.VNCPortMax,
},
&StepRegister{}, &StepRegister{},
&vmwcommon.StepRun{ &vmwcommon.StepRun{
BootWait: b.config.BootWait, BootWait: b.config.BootWait,
......
...@@ -67,6 +67,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -67,6 +67,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
HTTPPortMin: b.config.HTTPPortMin, HTTPPortMin: b.config.HTTPPortMin,
HTTPPortMax: b.config.HTTPPortMax, HTTPPortMax: b.config.HTTPPortMax,
}, },
&vmwcommon.StepConfigureVNC{
VNCPortMin: b.config.VNCPortMin,
VNCPortMax: b.config.VNCPortMax,
},
&StepCloneVMX{ &StepCloneVMX{
OutputDir: b.config.OutputDir, OutputDir: b.config.OutputDir,
Path: b.config.SourcePath, Path: b.config.SourcePath,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment