implement boot config struct for hyperv

This commit is contained in:
Matthew Hooker 2018-04-18 16:26:48 -07:00
parent a0c9ddb9ae
commit 1d36ef038c
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
7 changed files with 38 additions and 117 deletions

View File

@ -1,33 +0,0 @@
package common
import (
"fmt"
"time"
"github.com/hashicorp/packer/template/interpolate"
)
type RunConfig struct {
RawBootWait string `mapstructure:"boot_wait"`
BootWait time.Duration ``
}
func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
if c.RawBootWait == "" {
c.RawBootWait = "10s"
}
var errs []error
var err error
if c.RawBootWait != "" {
c.BootWait, err = time.ParseDuration(c.RawBootWait)
if err != nil {
errs = append(
errs, fmt.Errorf("Failed parsing boot_wait: %s", err))
}
}
return errs
}

View File

@ -1,37 +0,0 @@
package common
import (
"testing"
)
func TestRunConfigPrepare_BootWait(t *testing.T) {
var c *RunConfig
var errs []error
// Test a default boot_wait
c = new(RunConfig)
errs = c.Prepare(testConfigTemplate(t))
if len(errs) > 0 {
t.Fatalf("should not have error: %s", errs)
}
if c.RawBootWait != "10s" {
t.Fatalf("bad value: %s", c.RawBootWait)
}
// Test with a bad boot_wait
c = new(RunConfig)
c.RawBootWait = "this is not good"
errs = c.Prepare(testConfigTemplate(t))
if len(errs) == 0 {
t.Fatalf("bad: %#v", errs)
}
// Test with a good one
c = new(RunConfig)
c.RawBootWait = "5s"
errs = c.Prepare(testConfigTemplate(t))
if len(errs) > 0 {
t.Fatalf("should not have error: %s", errs)
}
}

View File

@ -21,7 +21,7 @@ type bootCommandTemplateData struct {
// This step "types" the boot command into the VM via the Hyper-V virtual keyboard // This step "types" the boot command into the VM via the Hyper-V virtual keyboard
type StepTypeBootCommand struct { type StepTypeBootCommand struct {
BootCommand []string BootCommand string
BootWait time.Duration BootWait time.Duration
SwitchName string SwitchName string
Ctx interpolate.Context Ctx interpolate.Context
@ -62,32 +62,23 @@ func (s *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateBag)
vmName, vmName,
} }
ui.Say("Typing the boot command...")
// Flatten command so we send it all at once
commands := []string{}
for _, command := range s.BootCommand {
command, err := interpolate.Render(command, &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
}
commands = append(commands, command)
}
sendCodes := func(codes []string) error { sendCodes := func(codes []string) error {
scanCodesToSendString := strings.Join(codes, " ") scanCodesToSendString := strings.Join(codes, " ")
return driver.TypeScanCodes(vmName, scanCodesToSendString) return driver.TypeScanCodes(vmName, scanCodesToSendString)
} }
d := bootcommand.NewPCXTDriver(sendCodes, -1) d := bootcommand.NewPCXTDriver(sendCodes, -1)
flatCommands := strings.Join(commands, "") ui.Say("Typing the boot command...")
seq, err := bootcommand.GenerateExpressionSequence(flatCommands) command, err := interpolate.Render(s.BootCommand, &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
}
seq, err := bootcommand.GenerateExpressionSequence(command)
if err != nil { if err != nil {
err := fmt.Errorf("Error generating boot command: %s", err) err := fmt.Errorf("Error generating boot command: %s", err)
state.Put("error", err) state.Put("error", err)

View File

@ -10,6 +10,7 @@ import (
hypervcommon "github.com/hashicorp/packer/builder/hyperv/common" hypervcommon "github.com/hashicorp/packer/builder/hyperv/common"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/common/bootcommand"
powershell "github.com/hashicorp/packer/common/powershell" powershell "github.com/hashicorp/packer/common/powershell"
"github.com/hashicorp/packer/common/powershell/hyperv" "github.com/hashicorp/packer/common/powershell/hyperv"
"github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/communicator"
@ -51,9 +52,9 @@ type Config struct {
common.HTTPConfig `mapstructure:",squash"` common.HTTPConfig `mapstructure:",squash"`
common.ISOConfig `mapstructure:",squash"` common.ISOConfig `mapstructure:",squash"`
common.FloppyConfig `mapstructure:",squash"` common.FloppyConfig `mapstructure:",squash"`
bootcommand.BootConfig `mapstructure:",squash"`
hypervcommon.OutputConfig `mapstructure:",squash"` hypervcommon.OutputConfig `mapstructure:",squash"`
hypervcommon.SSHConfig `mapstructure:",squash"` hypervcommon.SSHConfig `mapstructure:",squash"`
hypervcommon.RunConfig `mapstructure:",squash"`
hypervcommon.ShutdownConfig `mapstructure:",squash"` hypervcommon.ShutdownConfig `mapstructure:",squash"`
// The size, in megabytes, of the hard disk to create for the VM. // The size, in megabytes, of the hard disk to create for the VM.
@ -81,18 +82,17 @@ type Config struct {
// By default this is "packer-BUILDNAME", where "BUILDNAME" is the name of the build. // By default this is "packer-BUILDNAME", where "BUILDNAME" is the name of the build.
VMName string `mapstructure:"vm_name"` VMName string `mapstructure:"vm_name"`
BootCommand []string `mapstructure:"boot_command"` SwitchName string `mapstructure:"switch_name"`
SwitchName string `mapstructure:"switch_name"` SwitchVlanId string `mapstructure:"switch_vlan_id"`
SwitchVlanId string `mapstructure:"switch_vlan_id"` MacAddress string `mapstructure:"mac_address"`
MacAddress string `mapstructure:"mac_address"` VlanId string `mapstructure:"vlan_id"`
VlanId string `mapstructure:"vlan_id"` Cpu uint `mapstructure:"cpu"`
Cpu uint `mapstructure:"cpu"` Generation uint `mapstructure:"generation"`
Generation uint `mapstructure:"generation"` EnableMacSpoofing bool `mapstructure:"enable_mac_spoofing"`
EnableMacSpoofing bool `mapstructure:"enable_mac_spoofing"` EnableDynamicMemory bool `mapstructure:"enable_dynamic_memory"`
EnableDynamicMemory bool `mapstructure:"enable_dynamic_memory"` EnableSecureBoot bool `mapstructure:"enable_secure_boot"`
EnableSecureBoot bool `mapstructure:"enable_secure_boot"` EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions"`
EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions"` TempPath string `mapstructure:"temp_path"`
TempPath string `mapstructure:"temp_path"`
// A separate path can be used for storing the VM's disk image. The purpose is to enable // A separate path can be used for storing the VM's disk image. The purpose is to enable
// reading and writing to take place on different physical disks (read from VHD temp path // reading and writing to take place on different physical disks (read from VHD temp path
@ -136,9 +136,9 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
warnings = append(warnings, isoWarnings...) warnings = append(warnings, isoWarnings...)
errs = packer.MultiErrorAppend(errs, isoErrs...) errs = packer.MultiErrorAppend(errs, isoErrs...)
errs = packer.MultiErrorAppend(errs, b.config.BootConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...) errs = packer.MultiErrorAppend(errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...)
errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...)
@ -407,7 +407,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&hypervcommon.StepRun{}, &hypervcommon.StepRun{},
&hypervcommon.StepTypeBootCommand{ &hypervcommon.StepTypeBootCommand{
BootCommand: b.config.BootCommand, BootCommand: b.config.FlatBootCommand(),
BootWait: b.config.BootWait, BootWait: b.config.BootWait,
SwitchName: b.config.SwitchName, SwitchName: b.config.SwitchName,
Ctx: b.config.ctx, Ctx: b.config.ctx,

View File

@ -562,7 +562,7 @@ func TestUserVariablesInBootCommand(t *testing.T) {
state.Put("vmName", "packer-foo") state.Put("vmName", "packer-foo")
step := &hypervcommon.StepTypeBootCommand{ step := &hypervcommon.StepTypeBootCommand{
BootCommand: b.config.BootCommand, BootCommand: b.config.FlatBootCommand(),
SwitchName: b.config.SwitchName, SwitchName: b.config.SwitchName,
Ctx: b.config.ctx, Ctx: b.config.ctx,
} }

View File

@ -9,6 +9,7 @@ import (
hypervcommon "github.com/hashicorp/packer/builder/hyperv/common" hypervcommon "github.com/hashicorp/packer/builder/hyperv/common"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/common/bootcommand"
powershell "github.com/hashicorp/packer/common/powershell" powershell "github.com/hashicorp/packer/common/powershell"
"github.com/hashicorp/packer/common/powershell/hyperv" "github.com/hashicorp/packer/common/powershell/hyperv"
"github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/communicator"
@ -42,9 +43,9 @@ type Config struct {
common.HTTPConfig `mapstructure:",squash"` common.HTTPConfig `mapstructure:",squash"`
common.ISOConfig `mapstructure:",squash"` common.ISOConfig `mapstructure:",squash"`
common.FloppyConfig `mapstructure:",squash"` common.FloppyConfig `mapstructure:",squash"`
bootcommand.BootConfig `mapstructure:",squash"`
hypervcommon.OutputConfig `mapstructure:",squash"` hypervcommon.OutputConfig `mapstructure:",squash"`
hypervcommon.SSHConfig `mapstructure:",squash"` hypervcommon.SSHConfig `mapstructure:",squash"`
hypervcommon.RunConfig `mapstructure:",squash"`
hypervcommon.ShutdownConfig `mapstructure:",squash"` hypervcommon.ShutdownConfig `mapstructure:",squash"`
// The size, in megabytes, of the computer memory in the VM. // The size, in megabytes, of the computer memory in the VM.
@ -79,12 +80,11 @@ type Config struct {
// Use differencing disk // Use differencing disk
DifferencingDisk bool `mapstructure:"differencing_disk"` DifferencingDisk bool `mapstructure:"differencing_disk"`
BootCommand []string `mapstructure:"boot_command"` SwitchName string `mapstructure:"switch_name"`
SwitchName string `mapstructure:"switch_name"` SwitchVlanId string `mapstructure:"switch_vlan_id"`
SwitchVlanId string `mapstructure:"switch_vlan_id"` MacAddress string `mapstructure:"mac_address"`
MacAddress string `mapstructure:"mac_address"` VlanId string `mapstructure:"vlan_id"`
VlanId string `mapstructure:"vlan_id"` Cpu uint `mapstructure:"cpu"`
Cpu uint `mapstructure:"cpu"`
Generation uint Generation uint
EnableMacSpoofing bool `mapstructure:"enable_mac_spoofing"` EnableMacSpoofing bool `mapstructure:"enable_mac_spoofing"`
EnableDynamicMemory bool `mapstructure:"enable_dynamic_memory"` EnableDynamicMemory bool `mapstructure:"enable_dynamic_memory"`
@ -125,9 +125,9 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
errs = packer.MultiErrorAppend(errs, isoErrs...) errs = packer.MultiErrorAppend(errs, isoErrs...)
} }
errs = packer.MultiErrorAppend(errs, b.config.BootConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...) errs = packer.MultiErrorAppend(errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...)
errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...)
@ -437,7 +437,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&hypervcommon.StepRun{}, &hypervcommon.StepRun{},
&hypervcommon.StepTypeBootCommand{ &hypervcommon.StepTypeBootCommand{
BootCommand: b.config.BootCommand, BootCommand: b.config.FlatBootCommand(),
BootWait: b.config.BootWait, BootWait: b.config.BootWait,
SwitchName: b.config.SwitchName, SwitchName: b.config.SwitchName,
Ctx: b.config.ctx, Ctx: b.config.ctx,

View File

@ -530,7 +530,7 @@ func TestUserVariablesInBootCommand(t *testing.T) {
state.Put("vmName", "packer-foo") state.Put("vmName", "packer-foo")
step := &hypervcommon.StepTypeBootCommand{ step := &hypervcommon.StepTypeBootCommand{
BootCommand: b.config.BootCommand, BootCommand: b.config.FlatBootCommand(),
SwitchName: b.config.SwitchName, SwitchName: b.config.SwitchName,
Ctx: b.config.ctx, Ctx: b.config.ctx,
} }