2013-12-24 00:58:41 -05:00
|
|
|
package iso
|
2013-06-07 17:48:59 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
2013-12-24 01:09:47 -05:00
|
|
|
vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
|
2013-06-07 17:48:59 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"io/ioutil"
|
2013-06-07 18:12:24 -04:00
|
|
|
"log"
|
2013-06-07 17:48:59 -04:00
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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{}
|
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
type VNCAddressFinder interface {
|
|
|
|
VNCAddress(uint, uint) (string, uint)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stepConfigureVNC) VNCAddress(portMin, portMax uint) (string, uint) {
|
|
|
|
// 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.
|
|
|
|
var vncPort uint
|
|
|
|
portRange := int(portMax - portMin)
|
|
|
|
for {
|
|
|
|
vncPort = uint(rand.Intn(portRange)) + portMin
|
|
|
|
log.Printf("Trying port: %d", vncPort)
|
|
|
|
l, err := net.Listen("tcp", fmt.Sprintf(":%d", vncPort))
|
|
|
|
if err == nil {
|
|
|
|
defer l.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "127.0.0.1", vncPort
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:50:25 -04:00
|
|
|
config := state.Get("config").(*config)
|
2013-12-24 13:31:57 -05:00
|
|
|
driver := state.Get("driver").(vmwcommon.Driver)
|
2013-08-31 15:50:25 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmxPath := state.Get("vmx_path").(string)
|
2013-06-07 17:48:59 -04:00
|
|
|
|
|
|
|
f, err := os.Open(vmxPath)
|
|
|
|
if err != nil {
|
2013-06-20 00:20:48 -04:00
|
|
|
err := fmt.Errorf("Error reading VMX data: %s", err)
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:20:48 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-07 17:48:59 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
vmxBytes, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
2013-06-20 00:20:48 -04:00
|
|
|
err := fmt.Errorf("Error reading VMX data: %s", err)
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:20:48 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-07 17:48:59 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
var vncFinder VNCAddressFinder
|
|
|
|
if finder, ok := driver.(VNCAddressFinder); ok {
|
|
|
|
vncFinder = finder
|
|
|
|
} else {
|
|
|
|
vncFinder = s
|
|
|
|
}
|
2013-06-07 17:50:07 -04:00
|
|
|
log.Printf("Looking for available port between %d and %d", config.VNCPortMin, config.VNCPortMax)
|
2013-09-19 20:07:04 -04:00
|
|
|
vncIp, vncPort := vncFinder.VNCAddress(config.VNCPortMin, config.VNCPortMax)
|
|
|
|
if vncPort == 0 {
|
|
|
|
err := fmt.Errorf("Unable to find available VNC port between %d and %d",
|
|
|
|
config.VNCPortMin, config.VNCPortMax)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2013-06-07 17:48:59 -04:00
|
|
|
}
|
|
|
|
|
2013-06-07 17:50:07 -04:00
|
|
|
log.Printf("Found available VNC port: %d", vncPort)
|
|
|
|
|
2013-12-24 01:09:47 -05:00
|
|
|
vmxData := vmwcommon.ParseVMX(string(vmxBytes))
|
2013-11-08 17:21:31 -05:00
|
|
|
vmxData["remotedisplay.vnc.enabled"] = "TRUE"
|
|
|
|
vmxData["remotedisplay.vnc.port"] = fmt.Sprintf("%d", vncPort)
|
2013-06-07 17:48:59 -04:00
|
|
|
|
2013-12-24 01:09:47 -05:00
|
|
|
if err := vmwcommon.WriteVMX(vmxPath, vmxData); err != nil {
|
2013-06-20 00:20:48 -04:00
|
|
|
err := fmt.Errorf("Error writing VMX data: %s", err)
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:20:48 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-07 17:48:59 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("vnc_port", vncPort)
|
2013-09-19 20:07:04 -04:00
|
|
|
state.Put("vnc_ip", vncIp)
|
2013-06-07 17:48:59 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:50:25 -04:00
|
|
|
func (stepConfigureVNC) Cleanup(multistep.StateBag) {
|
2013-06-07 17:48:59 -04:00
|
|
|
}
|