/
step_configure_vnc.go
142 lines (115 loc) · 3.53 KB
/
step_configure_vnc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package common
import (
"context"
"fmt"
"log"
"math/rand"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/net"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)
// This step configures the VM to enable the VNC server.
//
// Uses:
// ui packersdk.Ui
// vmx_path string
//
// Produces:
// vnc_port int - The port that VNC is configured to listen on.
type StepConfigureVNC struct {
Enabled bool
VNCBindAddress string
VNCPortMin int
VNCPortMax int
VNCDisablePassword bool
l *net.Listener
}
type VNCAddressFinder interface {
VNCAddress(context.Context, string, int, int) (string, int, error)
// UpdateVMX, sets driver specific VNC values to VMX data.
UpdateVMX(vncAddress, vncPassword string, vncPort int, vmxData map[string]string)
}
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 {
if !s.Enabled {
log.Println("Skipping VNC configuration step...")
return multistep.ActionContinue
}
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packersdk.Ui)
vmxPath := state.Get("vmx_path").(string)
vmxData, err := ReadVMX(vmxPath)
if err != nil {
err := fmt.Errorf("Error reading VMX file: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
var vncFinder VNCAddressFinder
if finder, ok := driver.(VNCAddressFinder); ok {
vncFinder = finder
} else {
vncFinder = s
}
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)
vncFinder.UpdateVMX(vncBindAddress, vncPassword, vncPort, vmxData)
if err := WriteVMX(vmxPath, vmxData); err != nil {
err := fmt.Errorf("Error writing VMX data: %s", err)
state.Put("error", err)
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
}
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)
}
}
}