packer-cn/builder/vmware/common/step_configure_vnc.go

139 lines
3.4 KiB
Go
Raw Normal View History

2014-09-05 15:10:40 -04:00
package common
import (
"fmt"
"io/ioutil"
2013-06-07 18:12:24 -04:00
"log"
"math/rand"
"net"
"os"
2014-09-05 15:10:40 -04:00
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
// This step configures the VM to enable the VNC server.
//
// Uses:
// ui packer.Ui
// vmx_path string
//
// Produces:
// vnc_port uint - The port that VNC is configured to listen on.
2014-09-08 13:28:21 -04:00
type StepConfigureVNC struct {
VNCBindAddress string
VNCPortMin uint
VNCPortMax uint
2014-09-05 15:10:40 -04:00
}
type VNCAddressFinder interface {
VNCAddress(string, uint, uint) (string, uint, error)
// UpdateVMX, sets driver specific VNC values to VMX data.
UpdateVMX(vncAddress, vncPassword string, vncPort uint, vmxData map[string]string)
}
func (StepConfigureVNC) VNCAddress(vncBindAddress string, 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.
var vncPort uint
portRange := int(portMax - portMin)
for {
if portRange > 0 {
vncPort = uint(rand.Intn(portRange)) + portMin
} else {
vncPort = portMin
}
log.Printf("Trying port: %d", vncPort)
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", vncBindAddress, vncPort))
if err == nil {
defer l.Close()
break
}
}
return vncBindAddress, vncPort, nil
}
func VNCPassword() string {
length := int(8)
charSet := []byte("1234567890-=qwertyuiop[]asdfghjkl;zxcvbnm,./!@#%^*()_+QWERTYUIOP{}|ASDFGHJKL:XCVBNM<>?")
charSetLength := len(charSet)
password := make([]byte, length)
for i := 0; i < length; i++ {
password[i] = charSet[rand.Intn(charSetLength)]
}
return string(password)
}
2014-09-05 15:10:40 -04:00
func (s *StepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
2013-08-31 15:50:25 -04:00
ui := state.Get("ui").(packer.Ui)
vmxPath := state.Get("vmx_path").(string)
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())
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())
return multistep.ActionHalt
}
var vncFinder VNCAddressFinder
if finder, ok := driver.(VNCAddressFinder); ok {
vncFinder = finder
} else {
vncFinder = s
}
2014-09-05 15:10:40 -04:00
log.Printf("Looking for available port between %d and %d", s.VNCPortMin, s.VNCPortMax)
vncBindAddress, vncPort, err := vncFinder.VNCAddress(s.VNCBindAddress, s.VNCPortMin, s.VNCPortMax)
if err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
vncPassword := VNCPassword()
2013-06-07 17:50:07 -04:00
log.Printf("Found available VNC port: %d", vncPort)
2014-09-05 15:10:40 -04:00
vmxData := ParseVMX(string(vmxBytes))
vncFinder.UpdateVMX(vncBindAddress, vncPassword, vncPort, vmxData)
2014-09-05 15:10:40 -04:00
if err := 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())
return multistep.ActionHalt
}
2013-08-31 15:50:25 -04:00
state.Put("vnc_port", vncPort)
state.Put("vnc_ip", vncBindAddress)
state.Put("vnc_password", vncPassword)
return multistep.ActionContinue
}
func (StepConfigureVNC) UpdateVMX(address, password string, port uint, data map[string]string) {
data["remotedisplay.vnc.enabled"] = "TRUE"
data["remotedisplay.vnc.port"] = fmt.Sprintf("%d", port)
data["remotedisplay.vnc.ip"] = address
data["remotedisplay.vnc.password"] = password
}
2014-09-05 15:10:40 -04:00
func (StepConfigureVNC) Cleanup(multistep.StateBag) {
}