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"
|
|
|
|
"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)
|
2020-09-16 05:02:46 -04:00
|
|
|
conn := state.Get("vnc_conn").(*vnc.ClientConn)
|
|
|
|
defer conn.Close()
|
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)
|
|
|
|
}
|
|
|
|
|
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-16 05:02:46 -04:00
|
|
|
d := bootcommand.NewVNCDriver(conn, 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) {}
|