Add support of customizing boot order

This commit is contained in:
Andrei Tonkikh 2018-01-30 20:25:05 +03:00
parent ee6192f1c1
commit 19713ca20d
6 changed files with 55 additions and 17 deletions

View File

@ -47,7 +47,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
if b.config.Comm.Type != "none" { if b.config.Comm.Type != "none" {
steps = append(steps, steps = append(steps,
&common.StepRun{}, &common.StepRun{
Config: &b.config.RunConfig,
},
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.Comm, Config: &b.config.Comm,
Host: common.CommHost, Host: common.CommHost,

View File

@ -9,14 +9,15 @@ import (
) )
type Config struct { type Config struct {
packerCommon.PackerConfig `mapstructure:",squash"` packerCommon.PackerConfig `mapstructure:",squash"`
common.ConnectConfig `mapstructure:",squash"` common.ConnectConfig `mapstructure:",squash"`
CloneConfig `mapstructure:",squash"` common.RunConfig `mapstructure:",squash"`
common.HardwareConfig `mapstructure:",squash"` CloneConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"` common.HardwareConfig `mapstructure:",squash"`
common.ShutdownConfig `mapstructure:",squash"` Comm communicator.Config `mapstructure:",squash"`
CreateSnapshot bool `mapstructure:"create_snapshot"` common.ShutdownConfig `mapstructure:",squash"`
ConvertToTemplate bool `mapstructure:"convert_to_template"` CreateSnapshot bool `mapstructure:"create_snapshot"`
ConvertToTemplate bool `mapstructure:"convert_to_template"`
ctx interpolate.Context ctx interpolate.Context
} }
@ -29,6 +30,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
errs := new(packer.MultiError) errs := new(packer.MultiError)
errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.ConnectConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.ConnectConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.CloneConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.CloneConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...)

View File

@ -5,9 +5,19 @@ import (
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"fmt" "fmt"
"github.com/jetbrains-infra/packer-builder-vsphere/driver" "github.com/jetbrains-infra/packer-builder-vsphere/driver"
"strings"
) )
type RunConfig struct {
BootOrder string `mapstructure:"boot_order"` // example: "floppy,cdrom,ethernet,disk"
}
func (c *RunConfig) Prepare() []error {
return nil
}
type StepRun struct { type StepRun struct {
Config *RunConfig
} }
func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction { func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction {
@ -16,9 +26,16 @@ func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction {
ui.Say("Power on VM...") ui.Say("Power on VM...")
if s.Config.BootOrder != "" {
if err := vm.SetBootOrder(strings.Split(s.Config.BootOrder, ",")); err != nil {
state.Put("error", fmt.Errorf("error selecting boot order: %v", err))
return multistep.ActionHalt
}
}
err := vm.PowerOn() err := vm.PowerOn()
if err != nil { if err != nil {
state.Put("error", err) state.Put("error", fmt.Errorf("error powering on VM: %v", err))
return multistep.ActionHalt return multistep.ActionHalt
} }

View File

@ -463,6 +463,19 @@ func (vm *VirtualMachine) AddFloppy(imgPath string) error {
return vm.addDevice(floppy) return vm.addDevice(floppy)
} }
func (vm *VirtualMachine) SetBootOrder(order []string) error {
devices, err := vm.vm.Device(vm.driver.ctx)
if err != nil {
return err
}
bootOptions := types.VirtualMachineBootOptions{
BootOrder: devices.BootOrder(order),
}
return vm.vm.SetBootOptions(vm.driver.ctx, &bootOptions)
}
func (vm *VirtualMachine) addDevice(device types.BaseVirtualDevice) error { func (vm *VirtualMachine) addDevice(device types.BaseVirtualDevice) error {
newDevices := object.VirtualDeviceList{device} newDevices := object.VirtualDeviceList{device}
confSpec := types.VirtualMachineConfigSpec{} confSpec := types.VirtualMachineConfigSpec{}

View File

@ -49,7 +49,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
if b.config.Comm.Type != "none" { if b.config.Comm.Type != "none" {
steps = append(steps, steps = append(steps,
&common.StepRun{}, &common.StepRun{
Config: &b.config.RunConfig,
},
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.Comm, Config: &b.config.Comm,
Host: common.CommHost, Host: common.CommHost,

View File

@ -9,12 +9,13 @@ import (
) )
type Config struct { type Config struct {
packerCommon.PackerConfig `mapstructure:",squash"` packerCommon.PackerConfig `mapstructure:",squash"`
common.ConnectConfig `mapstructure:",squash"` common.RunConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"` common.ConnectConfig `mapstructure:",squash"`
common.ShutdownConfig `mapstructure:",squash"` Comm communicator.Config `mapstructure:",squash"`
CreateSnapshot bool `mapstructure:"create_snapshot"` common.ShutdownConfig `mapstructure:",squash"`
ConvertToTemplate bool `mapstructure:"convert_to_template"` CreateSnapshot bool `mapstructure:"create_snapshot"`
ConvertToTemplate bool `mapstructure:"convert_to_template"`
CreateConfig `mapstructure:",squash"` CreateConfig `mapstructure:",squash"`
CDRomConfig `mapstructure:",squash"` CDRomConfig `mapstructure:",squash"`
@ -31,6 +32,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
errs := new(packer.MultiError) errs := new(packer.MultiError)
errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.ConnectConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.ConnectConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare()...)