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

143 lines
3.5 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
2020-11-17 19:31:03 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer/packer-plugin-sdk/net"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
)
// This step configures the VM to enable the VNC server.
//
// Uses:
// ui packersdk.Ui
// vmx_path string
//
// Produces:
2019-03-19 09:47:21 -04:00
// 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
2019-03-19 09:47:21 -04:00
VNCPortMin int
VNCPortMax int
VNCDisablePassword bool
l *net.Listener
2014-09-05 15:10:40 -04:00
}
type VNCAddressFinder interface {
2019-03-19 09:47:21 -04:00
VNCAddress(context.Context, string, int, int) (string, int, error)
// UpdateVMX, sets driver specific VNC values to VMX data.
2019-03-19 09:47:21 -04:00
UpdateVMX(vncAddress, vncPassword string, vncPort int, vmxData map[string]string)
}
2019-03-19 09:47:21 -04:00
func (s *StepConfigureVNC) VNCAddress(ctx context.Context, vncBindAddress string, portMin, portMax int) (string, int, error) {
var err error
s.l, err = net.ListenRangeConfig{
Addr: s.VNCBindAddress,
Min: s.VNCPortMin,
Max: s.VNCPortMax,
Network: "tcp",
}.Listen(ctx)
if err != nil {
return "", 0, err
}
s.l.Listener.Close() // free port, but don't unlock lock file
return s.l.Address, s.l.Port, nil
}
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)
ui := state.Get("ui").(packersdk.Ui)
2013-08-31 15:50:25 -04:00
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)
vncBindAddress, vncPort, err := vncFinder.VNCAddress(ctx, s.VNCBindAddress, s.VNCPortMin, s.VNCPortMax)
if err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
vncPassword := VNCPassword(s.VNCDisablePassword)
log.Printf("Found available VNC port: %s:%d", vncBindAddress, vncPort)
2013-06-07 17:50:07 -04:00
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
}
state.Put("vnc_port", vncPort)
state.Put("vnc_ip", vncBindAddress)
state.Put("vnc_password", vncPassword)
return multistep.ActionContinue
}
2019-03-19 09:47:21 -04:00
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 s.l != nil {
if err := s.l.Close(); err != nil {
log.Printf("failed to unlock port lockfile: %v", err)
}
}
}