2020-07-07 13:57:51 -04:00
|
|
|
//go:generate struct-markdown
|
2020-06-24 05:14:30 -04:00
|
|
|
package common
|
2018-02-17 21:13:56 -05:00
|
|
|
|
|
|
|
import (
|
2018-10-31 17:42:24 -04:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-06-12 08:10:46 -04:00
|
|
|
"time"
|
|
|
|
|
2020-02-14 11:42:29 -05:00
|
|
|
"github.com/hashicorp/packer/builder/vsphere/driver"
|
2020-06-11 12:37:32 -04:00
|
|
|
"github.com/hashicorp/packer/common/bootcommand"
|
2020-02-14 11:42:29 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
|
|
|
"golang.org/x/mobile/event/key"
|
2018-02-17 21:13:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type BootConfig struct {
|
2020-06-11 12:37:32 -04:00
|
|
|
bootcommand.BootConfig `mapstructure:",squash"`
|
2020-07-07 13:57:51 -04:00
|
|
|
// The IP address to use for the HTTP server started to serve the `http_directory`.
|
|
|
|
// If unset, Packer will automatically discover and assign an IP.
|
|
|
|
HTTPIP string `mapstructure:"http_ip"`
|
2018-02-17 21:13:56 -05:00
|
|
|
}
|
|
|
|
|
2018-12-22 18:13:31 -05:00
|
|
|
type bootCommandTemplateData struct {
|
|
|
|
HTTPIP string
|
2019-05-25 01:45:53 -04:00
|
|
|
HTTPPort int
|
2018-12-22 18:13:31 -05:00
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2020-06-11 12:37:32 -04:00
|
|
|
func (c *BootConfig) Prepare(ctx *interpolate.Context) []error {
|
2019-07-12 04:29:41 -04:00
|
|
|
if c.BootWait == 0 {
|
|
|
|
c.BootWait = 10 * time.Second
|
2018-05-06 10:46:40 -04:00
|
|
|
}
|
2020-06-12 08:22:00 -04:00
|
|
|
return c.BootConfig.Prepare(ctx)
|
2018-02-17 21:13:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type StepBootCommand struct {
|
|
|
|
Config *BootConfig
|
2018-12-22 18:13:31 -05:00
|
|
|
VMName string
|
|
|
|
Ctx interpolate.Context
|
2018-02-17 21:13:56 -05:00
|
|
|
}
|
|
|
|
|
2020-06-11 12:37:32 -04:00
|
|
|
func (s *StepBootCommand) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
debug := state.Get("debug").(bool)
|
2018-02-17 21:13:56 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vm := state.Get("vm").(*driver.VirtualMachine)
|
|
|
|
|
2018-05-06 10:46:40 -04:00
|
|
|
if s.Config.BootCommand == nil {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-06-11 12:37:32 -04:00
|
|
|
// Wait the for the vm to boot.
|
|
|
|
if int64(s.Config.BootWait) > 0 {
|
|
|
|
ui.Say(fmt.Sprintf("Waiting %s for boot...", s.Config.BootWait.String()))
|
2018-05-06 10:46:40 -04:00
|
|
|
select {
|
2020-06-11 12:37:32 -04:00
|
|
|
case <-time.After(s.Config.BootWait):
|
|
|
|
break
|
|
|
|
case <-ctx.Done():
|
|
|
|
return multistep.ActionHalt
|
2018-05-06 10:46:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 12:37:32 -04:00
|
|
|
var pauseFn multistep.DebugPauseFn
|
|
|
|
if debug {
|
|
|
|
pauseFn = state.Get("pauseFn").(multistep.DebugPauseFn)
|
|
|
|
}
|
|
|
|
|
2019-05-25 01:45:53 -04:00
|
|
|
port := state.Get("http_port").(int)
|
2018-12-23 11:49:38 -05:00
|
|
|
if port > 0 {
|
2020-06-11 12:37:32 -04:00
|
|
|
ip := state.Get("http_ip").(string)
|
2018-12-23 11:49:38 -05:00
|
|
|
s.Ctx.Data = &bootCommandTemplateData{
|
|
|
|
ip,
|
|
|
|
port,
|
|
|
|
s.VMName,
|
|
|
|
}
|
|
|
|
ui.Say(fmt.Sprintf("HTTP server is working at http://%v:%v/", ip, port))
|
2018-12-22 18:13:31 -05:00
|
|
|
}
|
2018-02-17 21:13:56 -05:00
|
|
|
|
2020-06-12 06:42:00 -04:00
|
|
|
sendCodes := func(code key.Code, down bool) error {
|
|
|
|
var keyAlt, keyCtrl, keyShift bool
|
|
|
|
|
|
|
|
switch code {
|
|
|
|
case key.CodeLeftAlt:
|
|
|
|
keyAlt = down
|
|
|
|
case key.CodeLeftControl:
|
|
|
|
keyCtrl = down
|
|
|
|
default:
|
|
|
|
keyShift = down
|
2018-02-17 21:13:56 -05:00
|
|
|
}
|
|
|
|
|
2020-06-12 06:42:00 -04:00
|
|
|
_, err := vm.TypeOnKeyboard(driver.KeyInput{
|
|
|
|
Scancode: code,
|
|
|
|
Ctrl: keyCtrl,
|
|
|
|
Alt: keyAlt,
|
|
|
|
Shift: keyShift,
|
|
|
|
})
|
2020-06-11 12:37:32 -04:00
|
|
|
if err != nil {
|
2020-07-29 09:06:45 -04:00
|
|
|
return fmt.Errorf("error typing a boot command (code, down) `%d, %t`: %w", code, down, err)
|
2018-12-22 19:00:30 -05:00
|
|
|
}
|
2020-06-11 12:37:32 -04:00
|
|
|
return nil
|
2018-12-22 18:13:31 -05:00
|
|
|
}
|
2020-06-11 12:37:32 -04:00
|
|
|
d := bootcommand.NewUSBDriver(sendCodes, s.Config.BootGroupInterval)
|
2018-12-22 18:13:31 -05:00
|
|
|
|
2020-06-11 12:37:32 -04:00
|
|
|
ui.Say("Typing boot command...")
|
|
|
|
flatBootCommand := s.Config.FlatBootCommand()
|
|
|
|
command, err := interpolate.Render(flatBootCommand, &s.Ctx)
|
2018-12-22 18:13:31 -05:00
|
|
|
if err != nil {
|
2020-06-11 12:37:32 -04:00
|
|
|
err := fmt.Errorf("Error preparing boot command: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2018-12-22 18:13:31 -05:00
|
|
|
}
|
|
|
|
|
2020-06-11 12:37:32 -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-12-22 18:13:31 -05:00
|
|
|
}
|
2020-06-11 12:37:32 -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
|
|
|
|
}
|
|
|
|
|
|
|
|
if pauseFn != nil {
|
|
|
|
pauseFn(multistep.DebugLocationAfterRun, fmt.Sprintf("boot_command: %s", command), state)
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
2018-12-22 18:13:31 -05:00
|
|
|
}
|
2020-06-11 12:37:32 -04:00
|
|
|
|
|
|
|
func (s *StepBootCommand) Cleanup(_ multistep.StateBag) {}
|