-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathstep_run.go
More file actions
479 lines (403 loc) · 12.7 KB
/
Copy pathstep_run.go
File metadata and controls
479 lines (403 loc) · 12.7 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package tart
import (
"bytes"
"context"
"errors"
"fmt"
"net"
"os"
"os/exec"
"regexp"
"strings"
"time"
"github.com/apparentlymart/go-cidr/cidr"
"github.com/hashicorp/packer-plugin-sdk/bootcommand"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
"github.com/mitchellh/go-vnc"
)
var ErrFailedToDetectHostIP = errors.New("failed to detect host IP")
var vncRegexp = regexp.MustCompile("vnc://.*:(.*)@(.*):([0-9]{1,5})")
type stepRun struct{}
type bootCommandTemplateData struct {
HTTPIP string
HTTPPort int
}
func (s *stepRun) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
ui := state.Get("ui").(packersdk.Ui)
ui.Say("Starting the virtual machine...")
runArgs := []string{"run", config.VMName}
if config.Headless {
runArgs = append(runArgs, "--no-graphics")
} else {
runArgs = append(runArgs, "--graphics")
}
if !config.DisableVNC {
runArgs = append(runArgs, "--vnc-experimental")
}
if config.Recovery {
runArgs = append(runArgs, "--recovery")
}
if config.Rosetta != "" {
runArgs = append(runArgs, fmt.Sprintf("--rosetta=%s", config.Rosetta))
}
for _, iso := range config.FromISO {
runArgs = append(runArgs, fmt.Sprintf("--disk=%s:ro", iso))
}
if len(config.RunExtraArgs) > 0 {
runArgs = append(runArgs, config.RunExtraArgs...)
}
cmd := exec.CommandContext(ctx, tartCommand, runArgs...)
stdout := bytes.NewBufferString("")
cmd.Stdout = stdout
cmd.Stderr = uiWriter{ui: ui}
// Prevent the Tart from opening the Screen Sharing
// window connected to the VNC server we're starting
if !config.DisableVNC {
cmd.Env = cmd.Environ()
cmd.Env = append(cmd.Env, "CI=true")
}
if err := cmd.Start(); err != nil {
err = fmt.Errorf("Error starting VM: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put("tart-cmd", cmd)
ui.Say("Successfully started the virtual machine...")
// Handle VM termination during boot commands or provisioning,
// in which case we want to cancel the build instead of hanging
// around in the packer plugin until timeout.
go func() {
err := cmd.Wait()
cancelBuild, ok := state.Get("cancel-build").(context.CancelFunc)
if !ok {
return // Don't treat our own cleanup as a failure
}
select {
case <-ctx.Done():
return
default:
if err != nil {
// Termination via e.g. kill -9
ui.Error(fmt.Sprintf("VM terminated with error: %s", err))
cancelBuild()
} else if strings.Contains(stdout.String(), "Stopping VM...") {
// User initiated termination by quitting Tart,
// for example via Cmd+Q or closing the window.
ui.Error("VM terminated by user action")
cancelBuild()
} else if strings.Contains(stdout.String(), //nolint:staticcheck
"guest has stopped the virtual machine") {
// Boot commands or provisioning initiated termination,
// for example by issuing 'shutdown -t now', or via UI,
// and then issued a wait, to let the OS fully shut down
// on its own, instead of the packer plugin tearing down
// the VM after boot commands were done, in which case
// we assume the user knows what they are doing.
}
}
}()
needsVNCSession := !config.DisableVNC && (len(config.BootCommand) > 0 || config.VNCRecordingDir != "")
var vncSession *vncSession
if needsVNCSession {
vncConnection, ok := connectToTartVNC(ctx, state, ui, stdout)
if !ok {
return multistep.ActionHalt
}
state.Put("vnc-connection", vncConnection)
vncSession = vncConnection.session
if config.VNCRecordingDir != "" {
if !startVNCRecording(ctx, state, config, ui, vncSession) {
return multistep.ActionHalt
}
}
}
if len(config.BootCommand) > 0 && !config.DisableVNC {
vncDriver := newCustomDriver(vncSession, config, ctx)
if !typeBootCommandOverVNC(ctx, state, config, ui, vncDriver) {
return multistep.ActionHalt
}
}
return multistep.ActionContinue
}
type uiWriter struct {
ui packersdk.Ui
}
func (u uiWriter) Write(p []byte) (n int, err error) {
u.ui.Error(strings.TrimSpace(string(p)))
return len(p), nil
}
type vncConnection struct {
session *vncSession
netConn net.Conn
}
func (c *vncConnection) Close() {
c.session.vncClient.Close()
_ = c.netConn.Close()
}
// Cleanup stops the VM.
func (s *stepRun) Cleanup(state multistep.StateBag) {
config := state.Get("config").(*Config)
ui := state.Get("ui").(packersdk.Ui)
cmd := state.Get("tart-cmd").(*exec.Cmd)
if cmd == nil || cmd.ProcessState != nil {
cleanupVNCResources(state, ui)
return // Nothing to shut down
}
// Avoid cancellation logic when we explicitly
// shut down the VM.
state.Put("cancel-build", nil)
communicator := state.Get("communicator")
if communicator != nil {
ui.Say("Gracefully shutting down the VM...")
shutdownCmd := packersdk.RemoteCmd{
Command: fmt.Sprintf("echo %s | sudo -S -p '' shutdown -h now", config.CommunicatorConfig.Password()),
}
err := shutdownCmd.RunWithUi(context.Background(), communicator.(packersdk.Communicator), ui)
if err != nil {
ui.Say("Failed to gracefully shutdown VM...")
ui.Error(err.Error())
}
} else {
ui.Say("Shutting down the VM...")
err := cmd.Process.Signal(os.Interrupt)
if err != nil {
ui.Say("Failed to shutdown VM...")
ui.Error(err.Error())
}
}
// Always wait, even if we didn't initiate shutdown,
// so that we properly read and close stdout/stderr.
ui.Say("Waiting for the tart process to exit...")
_, _ = cmd.Process.Wait()
cleanupVNCResources(state, ui)
}
func connectToTartVNC(
ctx context.Context,
state multistep.StateBag,
ui packersdk.Ui,
tartRunStdout *bytes.Buffer,
) (*vncConnection, bool) {
ui.Say("Waiting for VNC server credentials from Tart...")
vncCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
var vncPassword string
var vncHost string
var vncPort string
for {
matches := vncRegexp.FindStringSubmatch(tartRunStdout.String())
if len(matches) == 1+vncRegexp.NumSubexp() {
vncPassword = matches[1]
vncHost = matches[2]
vncPort = matches[3]
break
}
select {
case <-vncCtx.Done():
return nil, false
case <-time.After(time.Second):
// continue
}
}
ui.Say("Retrieved VNC credentials, connecting...")
ui.Sayf("If you want to view the screen of the VM, connect via VNC with the password \"%s\" to\n"+
"vnc://%s:%s", vncPassword, vncHost, vncPort)
dialer := net.Dialer{}
netConn, err := dialer.DialContext(ctx, "tcp", fmt.Sprintf("%s:%s", vncHost, vncPort))
if err != nil {
err := fmt.Errorf("Failed to connect to Tart's VNC server: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return nil, false
}
serverMessageChannel := make(chan vnc.ServerMessage)
vncClient, err := vnc.Client(netConn, &vnc.ClientConfig{
Auth: []vnc.ClientAuth{
&vnc.PasswordAuth{Password: vncPassword},
},
ServerMessageCh: serverMessageChannel,
})
if err != nil {
err := fmt.Errorf("Failed to connect to Tart's VNC server: %s", err)
state.Put("error", err)
ui.Error(err.Error())
_ = netConn.Close()
return nil, false
}
ui.Say("Connected to VNC server!")
err = vncClient.SetEncodings([]vnc.Encoding{
&vnc.RawEncoding{},
&DesktopSizePseudoEncoding{},
})
if err != nil {
err := fmt.Errorf("Failed to set VNC encoding: %s", err)
state.Put("error", err)
ui.Error(err.Error())
vncClient.Close()
_ = netConn.Close()
return nil, false
}
return &vncConnection{
session: newVNCSession(vncClient, serverMessageChannel),
netConn: netConn,
}, true
}
func startVNCRecording(
ctx context.Context,
state multistep.StateBag,
config *Config,
ui packersdk.Ui,
session *vncSession,
) bool {
recorder := newVNCRecorder(session, config.VNCRecordingDir, config.VNCRecordingInterval)
if err := recorder.Prepare(); err != nil {
state.Put("error", err)
ui.Error(err.Error())
return false
}
ui.Sayf("Recording VNC snapshots to %s every %s...",
config.VNCRecordingDir, config.VNCRecordingInterval)
handle := startVNCRecorder(ctx, recorder)
state.Put("vnc-recorder", handle)
go func() {
<-handle.done
if err := handle.Err(); err != nil {
ui.Error(fmt.Sprintf("VNC recording failed: %s", err))
state.Put("error", err)
cancelBuild, ok := state.Get("cancel-build").(context.CancelFunc)
if ok && cancelBuild != nil {
cancelBuild()
}
}
}()
return true
}
func cleanupVNCResources(state multistep.StateBag, ui packersdk.Ui) {
if rawRecorder, ok := state.GetOk("vnc-recorder"); ok {
if err := rawRecorder.(*vncRecorderHandle).Stop(); err != nil {
ui.Error(fmt.Sprintf("VNC recording failed: %s", err))
state.Put("error", err)
}
}
if rawConnection, ok := state.GetOk("vnc-connection"); ok {
rawConnection.(*vncConnection).Close()
}
}
func typeBootCommandOverVNC(
ctx context.Context,
state multistep.StateBag,
config *Config,
ui packersdk.Ui,
vncDriver *customDriver,
) bool {
ui.Say("Typing boot commands over VNC...")
if config.HTTPDir != "" || len(config.HTTPContent) != 0 {
ui.Say("Detecting host IP...")
hostIP, err := detectHostIP(ctx, config)
if err != nil {
err := fmt.Errorf("Failed to detect the host IP address: %v", err)
state.Put("error", err)
ui.Error(err.Error())
return false
}
ui.Say(fmt.Sprintf("Host IP is assumed to be %s", hostIP))
state.Put("http_ip", hostIP)
// Should be already filled by the Packer's commonsteps.StepHTTPServer
httpPort := state.Get("http_port").(int)
config.ctx.Data = &bootCommandTemplateData{
HTTPIP: hostIP,
HTTPPort: httpPort,
}
}
if config.VNCConfig.BootWait > 0 {
message := fmt.Sprintf("Waiting %v after the VM has booted...", config.VNCConfig.BootWait)
ui.Say(message)
time.Sleep(config.VNCConfig.BootWait)
}
message := fmt.Sprintf("Typing commands with key interval %v...", vncDriver.KeyInterval())
ui.Say(message)
command, err := interpolate.Render(config.VNCConfig.FlatBootCommand(), &config.ctx)
if err != nil {
err := fmt.Errorf("Failed to render the boot command: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return false
}
stringWaitRegex := regexp.MustCompile(`<wait\s*'(.+?)'>`)
command = stringWaitRegex.ReplaceAllString(command,
fmt.Sprintf(`%c${1}%c`, WaitForStringStart, WaitForStringEnd))
stringClickRegex := regexp.MustCompile(`<click\s*'(.+?)'>`)
command = stringClickRegex.ReplaceAllString(command,
fmt.Sprintf(`%c${1}%c`, ClickStringStart, ClickStringEnd))
// Waiting for https://github.com/hashicorp/packer-plugin-sdk/pull/293
leftCommandRegex := regexp.MustCompile(`<leftCommand(On|Off)?>`)
command = leftCommandRegex.ReplaceAllString(command,
fmt.Sprintf(`<%c${1}>`, LeftCommand))
rightCommandRegex := regexp.MustCompile(`<rightCommand(On|Off)?>`)
command = rightCommandRegex.ReplaceAllString(command,
fmt.Sprintf(`<%c${1}>`, RightCommand))
leftOptionRegex := regexp.MustCompile(`<leftOption(On|Off)?>`)
command = leftOptionRegex.ReplaceAllString(command,
fmt.Sprintf(`<%c${1}>`, LeftOption))
rightOptionRegex := regexp.MustCompile(`<rightOption(On|Off)?>`)
command = rightOptionRegex.ReplaceAllString(command,
fmt.Sprintf(`<%c${1}>`, RightOption))
seq, err := bootcommand.GenerateExpressionSequence(command)
if err != nil {
err := fmt.Errorf("Failed to parse the boot command: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return false
}
if err := seq.Do(ctx, vncDriver); err != nil {
err := fmt.Errorf("Failed to run the boot command: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return false
}
ui.Say("Done typing commands!")
return true
}
func detectHostIP(ctx context.Context, config *Config) (string, error) {
if config.HTTPAddress != "0.0.0.0" {
return config.HTTPAddress, nil
}
vmIPRaw, err := TartMachineIP(ctx, config.VMName, config.IpExtraArgs)
if err != nil {
return "", fmt.Errorf("%w: while running \"tart ip\": %v",
ErrFailedToDetectHostIP, err)
}
vmIP := net.ParseIP(vmIPRaw)
// Find the interface that has this IP
interfaces, err := net.Interfaces()
if err != nil {
return "", fmt.Errorf("%w: while retrieving interfaces: %v",
ErrFailedToDetectHostIP, err)
}
for _, iface := range interfaces {
addrs, err := iface.Addrs()
if err != nil {
return "", fmt.Errorf("%w: while retrieving interface addresses: %v",
ErrFailedToDetectHostIP, err)
}
for _, addr := range addrs {
_, net, err := net.ParseCIDR(addr.String())
if err != nil {
return "", fmt.Errorf("%w: while parsing interface CIDR: %v",
ErrFailedToDetectHostIP, err)
}
if net.Contains(vmIP) {
gatewayIP, err := cidr.Host(net, 1)
if err != nil {
return "", fmt.Errorf("%w: while calculating gateway IP: %v",
ErrFailedToDetectHostIP, err)
}
return gatewayIP.String(), nil
}
}
}
return "", fmt.Errorf("%w: no suitable interface found", ErrFailedToDetectHostIP)
}