2014-09-05 15:10:40 -04:00
|
|
|
package common
|
2013-06-07 17:48:59 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-06-07 17:48:59 -04:00
|
|
|
"fmt"
|
2013-06-07 18:12:24 -04:00
|
|
|
"log"
|
2013-06-07 17:48:59 -04:00
|
|
|
"math/rand"
|
2014-09-05 15:10:40 -04:00
|
|
|
|
2019-03-15 11:37:44 -04:00
|
|
|
"github.com/hashicorp/packer/common/net"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-06-07 17:48:59 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step configures the VM to enable the VNC server.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// ui packer.Ui
|
|
|
|
// vmx_path string
|
|
|
|
//
|
|
|
|
// Produces:
|
2019-03-18 12:19:03 -04:00
|
|
|
// vnc_port uint - 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
|
2016-08-19 06:49:23 -04:00
|
|
|
VNCBindAddress string
|
2019-03-18 12:19:03 -04:00
|
|
|
VNCPortMin uint
|
|
|
|
VNCPortMax uint
|
2016-08-19 06:49:23 -04:00
|
|
|
VNCDisablePassword bool
|
2019-03-15 11:37:44 -04:00
|
|
|
|
|
|
|
l *net.Listener
|
2014-09-05 15:10:40 -04:00
|
|
|
}
|
2013-06-07 17:48:59 -04:00
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
type VNCAddressFinder interface {
|
2019-03-19 05:42:08 -04:00
|
|
|
VNCAddress(context.Context, string, uint, uint) (string, uint, error)
|
|
|
|
|
2016-07-21 12:14:02 -04:00
|
|
|
// UpdateVMX, sets driver specific VNC values to VMX data.
|
2019-03-18 12:19:03 -04:00
|
|
|
UpdateVMX(vncAddress, vncPassword string, vncPort uint, vmxData map[string]string)
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
2019-03-19 05:42:08 -04:00
|
|
|
func (s *StepConfigureVNC) VNCAddress(ctx context.Context, vncBindAddress string, portMin, portMax uint) (string, uint, 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()
|
|
|
|
return s.l.Address, s.l.Port, nil
|
|
|
|
}
|
|
|
|
|
2016-08-19 06:49:23 -04:00
|
|
|
func VNCPassword(skipPassword bool) string {
|
|
|
|
if skipPassword {
|
|
|
|
return ""
|
|
|
|
}
|
2015-06-26 04:29:33 -04:00
|
|
|
length := int(8)
|
|
|
|
|
2017-02-02 07:03:46 -05:00
|
|
|
charSet := []byte("012345689abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
2015-06-26 04:29:33 -04:00
|
|
|
charSetLength := len(charSet)
|
|
|
|
|
|
|
|
password := make([]byte, length)
|
|
|
|
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
password[i] = charSet[rand.Intn(charSetLength)]
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(password)
|
|
|
|
}
|
|
|
|
|
2019-03-15 11:37:44 -04:00
|
|
|
func (s *StepConfigureVNC) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-10-09 20:12:33 -04:00
|
|
|
if !s.Enabled {
|
2017-09-20 23:07:21 -04:00
|
|
|
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)
|
2013-06-07 17:48:59 -04:00
|
|
|
|
2018-01-31 16:19:31 -05:00
|
|
|
vmxData, err := ReadVMX(vmxPath)
|
2013-06-07 17:48:59 -04:00
|
|
|
if err != nil {
|
2018-01-31 16:19:31 -05:00
|
|
|
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())
|
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
|
|
|
|
}
|
2019-03-15 11:37:44 -04:00
|
|
|
|
2014-09-05 15:10:40 -04:00
|
|
|
log.Printf("Looking for available port between %d and %d", s.VNCPortMin, s.VNCPortMax)
|
2019-03-19 05:42:08 -04:00
|
|
|
vncBindAddress, vncPort, err := vncFinder.VNCAddress(ctx, s.VNCBindAddress, s.VNCPortMin, s.VNCPortMax)
|
2019-03-15 11:37:44 -04:00
|
|
|
|
2014-08-18 21:28:25 -04:00
|
|
|
if err != nil {
|
2013-09-19 20:07:04 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2013-06-07 17:48:59 -04:00
|
|
|
}
|
2019-03-15 11:37:44 -04:00
|
|
|
s.l.Listener.Close() // free port, but don't unlock lock file
|
2013-06-07 17:48:59 -04:00
|
|
|
|
2016-08-19 06:49:23 -04:00
|
|
|
vncPassword := VNCPassword(s.VNCDisablePassword)
|
2015-06-26 04:29:33 -04:00
|
|
|
|
2019-03-15 11:37:44 -04:00
|
|
|
log.Printf("Found available VNC port: %v", s.l)
|
2013-06-07 17:50:07 -04:00
|
|
|
|
2019-03-19 05:42:08 -04:00
|
|
|
vncFinder.UpdateVMX(vncBindAddress, vncPassword, vncPort, vmxData)
|
2013-06-07 17:48:59 -04:00
|
|
|
|
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())
|
2013-06-07 17:48:59 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2019-03-18 12:19:03 -04:00
|
|
|
state.Put("vnc_port", s.l.Port)
|
|
|
|
state.Put("vnc_ip", s.l.Address)
|
2015-06-26 04:29:33 -04:00
|
|
|
state.Put("vnc_password", vncPassword)
|
2013-06-07 17:48:59 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-03-18 12:19:03 -04:00
|
|
|
func (StepConfigureVNC) UpdateVMX(address, password string, port uint, data map[string]string) {
|
2016-07-21 12:14:02 -04:00
|
|
|
data["remotedisplay.vnc.enabled"] = "TRUE"
|
|
|
|
data["remotedisplay.vnc.port"] = fmt.Sprintf("%d", port)
|
|
|
|
data["remotedisplay.vnc.ip"] = address
|
2016-08-19 06:49:23 -04:00
|
|
|
if len(password) > 0 {
|
|
|
|
data["remotedisplay.vnc.password"] = password
|
|
|
|
}
|
2016-07-21 12:14:02 -04:00
|
|
|
}
|
|
|
|
|
2019-03-15 11:37:44 -04:00
|
|
|
func (s *StepConfigureVNC) Cleanup(multistep.StateBag) {
|
2019-03-19 05:42:08 -04:00
|
|
|
if s.l != nil {
|
|
|
|
if err := s.l.Close(); err != nil {
|
|
|
|
log.Printf("failed to unlock port lockfile: %v", err)
|
|
|
|
}
|
2019-03-15 11:37:44 -04:00
|
|
|
}
|
2013-06-07 17:48:59 -04:00
|
|
|
}
|