2014-05-12 22:02:30 -04:00
|
|
|
package common
|
2013-06-05 20:15:16 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-06-05 20:15:16 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"time"
|
2014-09-05 15:01:07 -04:00
|
|
|
|
2018-04-18 17:53:59 -04:00
|
|
|
"github.com/hashicorp/packer/common/bootcommand"
|
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"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2014-09-05 15:01:07 -04:00
|
|
|
"github.com/mitchellh/go-vnc"
|
2013-06-05 20:15:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step "types" the boot command into the VM over VNC.
|
|
|
|
//
|
|
|
|
// Uses:
|
2013-06-05 20:32:57 -04:00
|
|
|
// http_port int
|
2013-06-05 20:15:16 -04:00
|
|
|
// ui packer.Ui
|
2019-03-19 09:47:21 -04:00
|
|
|
// vnc_port int
|
2013-06-05 20:15:16 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
2020-09-11 10:46:33 -04:00
|
|
|
type StepVNCBootCommand struct {
|
|
|
|
Config bootcommand.VNCConfig
|
|
|
|
VMName string
|
|
|
|
Ctx interpolate.Context
|
2014-05-12 22:02:30 -04:00
|
|
|
}
|
2020-09-11 10:46:33 -04:00
|
|
|
|
|
|
|
type VNCBootCommandTemplateData struct {
|
2018-04-11 02:05:46 -04:00
|
|
|
HTTPIP string
|
2019-03-19 09:47:21 -04:00
|
|
|
HTTPPort int
|
2018-04-11 02:05:46 -04:00
|
|
|
Name string
|
|
|
|
}
|
2013-06-05 20:15:16 -04:00
|
|
|
|
2020-09-11 10:46:33 -04:00
|
|
|
func (s *StepVNCBootCommand) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
if s.Config.DisableVNC {
|
2017-09-20 23:07:21 -04:00
|
|
|
log.Println("Skipping boot command step...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2016-05-17 03:50:00 -04:00
|
|
|
debug := state.Get("debug").(bool)
|
2019-03-19 09:47:21 -04:00
|
|
|
httpPort := state.Get("http_port").(int)
|
2013-08-31 15:50:25 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-09-19 20:07:04 -04:00
|
|
|
vncIp := state.Get("vnc_ip").(string)
|
2019-03-19 09:47:21 -04:00
|
|
|
vncPort := state.Get("vnc_port").(int)
|
2015-06-26 04:29:33 -04:00
|
|
|
vncPassword := state.Get("vnc_password")
|
2013-06-05 20:15:16 -04:00
|
|
|
|
2018-04-11 02:05:46 -04:00
|
|
|
// Wait the for the vm to boot.
|
2020-09-11 10:46:33 -04:00
|
|
|
if int64(s.Config.BootWait) > 0 {
|
|
|
|
ui.Say(fmt.Sprintf("Waiting %s for boot...", s.Config.BootWait.String()))
|
2018-04-11 02:05:46 -04:00
|
|
|
select {
|
2020-09-11 10:46:33 -04:00
|
|
|
case <-time.After(s.Config.BootWait):
|
2018-04-11 02:05:46 -04:00
|
|
|
break
|
|
|
|
case <-ctx.Done():
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 03:50:00 -04:00
|
|
|
var pauseFn multistep.DebugPauseFn
|
|
|
|
if debug {
|
|
|
|
pauseFn = state.Get("pauseFn").(multistep.DebugPauseFn)
|
|
|
|
}
|
|
|
|
|
2013-06-05 23:40:39 -04:00
|
|
|
// Connect to VNC
|
2015-11-11 07:39:06 -05:00
|
|
|
ui.Say(fmt.Sprintf("Connecting to VM via VNC (%s:%d)", vncIp, vncPort))
|
2017-02-01 06:32:02 -05:00
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
nc, err := net.Dial("tcp", fmt.Sprintf("%s:%d", vncIp, vncPort))
|
2013-06-05 20:15:16 -04:00
|
|
|
if err != nil {
|
2013-06-20 00:20:48 -04:00
|
|
|
err := fmt.Errorf("Error connecting to VNC: %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-05 20:15:16 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
defer nc.Close()
|
|
|
|
|
2015-06-26 04:29:33 -04:00
|
|
|
var auth []vnc.ClientAuth
|
|
|
|
|
2016-08-19 06:49:23 -04:00
|
|
|
if vncPassword != nil && len(vncPassword.(string)) > 0 {
|
2015-06-26 04:29:33 -04:00
|
|
|
auth = []vnc.ClientAuth{&vnc.PasswordAuth{Password: vncPassword.(string)}}
|
|
|
|
} else {
|
2016-08-19 06:49:23 -04:00
|
|
|
auth = []vnc.ClientAuth{new(vnc.ClientAuthNone)}
|
2015-06-26 04:29:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
c, err := vnc.Client(nc, &vnc.ClientConfig{Auth: auth, Exclusive: true})
|
2013-06-05 20:15:16 -04:00
|
|
|
if err != nil {
|
2013-06-20 00:20:48 -04:00
|
|
|
err := fmt.Errorf("Error handshaking with VNC: %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-05 20:15:16 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
2013-06-05 20:32:57 -04:00
|
|
|
log.Printf("Connected to VNC desktop: %s", c.DesktopName)
|
|
|
|
|
2020-05-19 05:49:48 -04:00
|
|
|
hostIP := state.Get("http_ip").(string)
|
2020-09-11 10:46:33 -04:00
|
|
|
s.Ctx.Data = &VNCBootCommandTemplateData{
|
|
|
|
HTTPIP: hostIP,
|
|
|
|
HTTPPort: httpPort,
|
|
|
|
Name: s.VMName,
|
2013-06-05 20:32:57 -04:00
|
|
|
}
|
|
|
|
|
2020-09-11 10:46:33 -04:00
|
|
|
d := bootcommand.NewVNCDriver(c, s.Config.BootKeyInterval)
|
2018-04-11 02:05:46 -04:00
|
|
|
|
2013-06-05 20:15:16 -04:00
|
|
|
ui.Say("Typing the boot command over VNC...")
|
2020-09-11 10:46:33 -04:00
|
|
|
flatBootCommand := s.Config.FlatBootCommand()
|
|
|
|
command, err := interpolate.Render(flatBootCommand, &s.Ctx)
|
2018-04-18 17:10:28 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error preparing boot command: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-06-05 20:15:16 -04:00
|
|
|
|
2018-04-18 17:10:28 -04:00
|
|
|
seq, err := bootcommand.GenerateExpressionSequence(command)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error generating boot command: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-04-13 15:58:35 -04:00
|
|
|
|
2018-04-18 17:10:28 -04:00
|
|
|
if err := seq.Do(ctx, d); err != nil {
|
|
|
|
err := fmt.Errorf("Error running boot command: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-04-13 15:58:35 -04:00
|
|
|
|
2018-04-18 17:10:28 -04:00
|
|
|
if pauseFn != nil {
|
|
|
|
pauseFn(multistep.DebugLocationAfterRun,
|
|
|
|
fmt.Sprintf("boot_command: %s", command), state)
|
2013-06-05 20:15:16 -04:00
|
|
|
}
|
2018-04-11 02:05:46 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
2013-06-05 20:15:16 -04:00
|
|
|
}
|
2018-04-11 02:05:46 -04:00
|
|
|
|
2020-09-11 10:46:33 -04:00
|
|
|
func (*StepVNCBootCommand) Cleanup(multistep.StateBag) {}
|