builder/vmware: add VNC to vmx

This commit is contained in:
Mitchell Hashimoto 2014-09-05 12:10:40 -07:00
parent b72605c2f6
commit 1a45b96674
4 changed files with 40 additions and 31 deletions

View File

@ -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
}

View File

@ -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) {
}

View File

@ -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,

View File

@ -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,