packer-cn/builder/vmware/common/step_vnc_boot_command.go

107 lines
2.5 KiB
Go
Raw Normal View History

package common
2013-06-05 20:15:16 -04:00
import (
"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"
"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:
// 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>
type StepVNCBootCommand struct {
Config bootcommand.VNCConfig
VMName string
Ctx interpolate.Context
}
type VNCBootCommandTemplateData struct {
HTTPIP string
2019-03-19 09:47:21 -04:00
HTTPPort int
Name string
}
2013-06-05 20:15:16 -04:00
func (s *StepVNCBootCommand) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
if s.Config.DisableVNC {
log.Println("Skipping boot command step...")
return multistep.ActionContinue
}
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)
conn := state.Get("vnc_conn").(*vnc.ClientConn)
defer conn.Close()
2013-06-05 20:15:16 -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()))
select {
case <-time.After(s.Config.BootWait):
break
case <-ctx.Done():
return multistep.ActionHalt
}
}
var pauseFn multistep.DebugPauseFn
if debug {
pauseFn = state.Get("pauseFn").(multistep.DebugPauseFn)
}
hostIP := state.Get("http_ip").(string)
s.Ctx.Data = &VNCBootCommandTemplateData{
HTTPIP: hostIP,
HTTPPort: httpPort,
Name: s.VMName,
}
d := bootcommand.NewVNCDriver(conn, s.Config.BootKeyInterval)
2013-06-05 20:15:16 -04:00
ui.Say("Typing the boot command over VNC...")
flatBootCommand := s.Config.FlatBootCommand()
command, err := interpolate.Render(flatBootCommand, &s.Ctx)
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
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
}
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)
2013-06-05 20:15:16 -04:00
}
return multistep.ActionContinue
2013-06-05 20:15:16 -04:00
}
func (*StepVNCBootCommand) Cleanup(multistep.StateBag) {}