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

127 lines
3.0 KiB
Go
Raw Normal View History

2014-09-05 15:10:40 -04:00
package common
import (
"context"
"fmt"
2013-06-07 18:12:24 -04:00
"log"
"math/rand"
2014-09-05 15:10:40 -04:00
"github.com/hashicorp/packer/common/net"
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
)
// This step configures the VM to enable the VNC server.
//
// Uses:
// ui packer.Ui
// vmx_path string
//
// Produces:
// vnc_port int - The port that VNC is configured to listen on.
2014-09-08 13:28:21 -04:00
type StepConfigureVNC struct {
2017-10-09 20:12:33 -04:00
Enabled bool
VNCBindAddress string
VNCPortMin int
VNCPortMax int
VNCDisablePassword bool
l *net.Listener
2014-09-05 15:10:40 -04:00
}
type VNCAddressFinder interface {
// UpdateVMX, sets driver specific VNC values to VMX data.
UpdateVMX(vncAddress, vncPassword string, vncPort int, vmxData map[string]string)
}
func VNCPassword(skipPassword bool) string {
if skipPassword {
return ""
}
length := int(8)
charSet := []byte("012345689abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
charSetLength := len(charSet)
password := make([]byte, length)
for i := 0; i < length; i++ {
password[i] = charSet[rand.Intn(charSetLength)]
}
return string(password)
}
func (s *StepConfigureVNC) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
2017-10-09 20:12:33 -04:00
if !s.Enabled {
log.Println("Skipping VNC configuration step...")
return multistep.ActionContinue
}
2014-09-05 15:10:40 -04:00
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)
vmxData, err := ReadVMX(vmxPath)
if err != nil {
err := fmt.Errorf("Error reading VMX file: %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)
s.l, err = net.ListenRangeConfig{
Addr: s.VNCBindAddress,
Min: s.VNCPortMin,
Max: s.VNCPortMax,
Network: "tcp",
}.Listen(ctx)
if err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
s.l.Listener.Close() // free port, but don't unlock lock file
vncPassword := VNCPassword(s.VNCDisablePassword)
log.Printf("Found available VNC port: %v", s.l)
2013-06-07 17:50:07 -04:00
vncFinder.UpdateVMX(s.l.Address, vncPassword, s.l.Port, 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
}
state.Put("vnc_password", vncPassword)
return multistep.ActionContinue
}
func (*StepConfigureVNC) UpdateVMX(address, password string, port int, data map[string]string) {
data["remotedisplay.vnc.enabled"] = "TRUE"
data["remotedisplay.vnc.port"] = fmt.Sprintf("%d", port)
data["remotedisplay.vnc.ip"] = address
if len(password) > 0 {
data["remotedisplay.vnc.password"] = password
}
}
func (s *StepConfigureVNC) Cleanup(multistep.StateBag) {
if err := s.l.Close(); err != nil {
log.Printf("failed to unlock port lockfile: %v", err)
}
}