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