From d731dcd8f752705d07f0f6fa2d765750625cc072 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 22 Dec 2013 07:58:07 -0800 Subject: [PATCH] builder/virtualbox: floppy files config --- builder/virtualbox/common/floppy_config.go | 31 +++++++++++++++++++ .../virtualbox/common/floppy_config_test.go | 18 +++++++++++ builder/virtualbox/iso/builder.go | 16 +--------- builder/virtualbox/ovf/builder.go | 6 ++-- builder/virtualbox/ovf/config.go | 1 + 5 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 builder/virtualbox/common/floppy_config.go create mode 100644 builder/virtualbox/common/floppy_config_test.go diff --git a/builder/virtualbox/common/floppy_config.go b/builder/virtualbox/common/floppy_config.go new file mode 100644 index 000000000..35cd7aca4 --- /dev/null +++ b/builder/virtualbox/common/floppy_config.go @@ -0,0 +1,31 @@ +package common + +import ( + "fmt" + + "github.com/mitchellh/packer/packer" +) + +// FloppyConfig is configuration related to created floppy disks and attaching +// them to a VirtualBox machine. +type FloppyConfig struct { + FloppyFiles []string `mapstructure:"floppy_files"` +} + +func (c *FloppyConfig) Prepare(t *packer.ConfigTemplate) []error { + if c.FloppyFiles == nil { + c.FloppyFiles = make([]string, 0) + } + + errs := make([]error, 0) + for i, file := range c.FloppyFiles { + var err error + c.FloppyFiles[i], err = t.Process(file, nil) + if err != nil { + errs = append(errs, fmt.Errorf( + "Error processing floppy_files[%d]: %s", i, err)) + } + } + + return errs +} diff --git a/builder/virtualbox/common/floppy_config_test.go b/builder/virtualbox/common/floppy_config_test.go new file mode 100644 index 000000000..3e4fc55db --- /dev/null +++ b/builder/virtualbox/common/floppy_config_test.go @@ -0,0 +1,18 @@ +package common + +import ( + "testing" +) + +func TestFloppyConfigPrepare(t *testing.T) { + c := new(FloppyConfig) + + errs := c.Prepare(testConfigTemplate(t)) + if len(errs) > 0 { + t.Fatalf("err: %#v", errs) + } + + if len(c.FloppyFiles) > 0 { + t.Fatal("should not have floppy files") + } +} diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index fddd36b6d..f4948cb04 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -30,11 +30,11 @@ type Builder struct { type config struct { common.PackerConfig `mapstructure:",squash"` + vboxcommon.FloppyConfig `mapstructure:",squash"` vboxcommon.OutputConfig `mapstructure:",squash"` BootCommand []string `mapstructure:"boot_command"` DiskSize uint `mapstructure:"disk_size"` - FloppyFiles []string `mapstructure:"floppy_files"` Format string `mapstructure:"format"` GuestAdditionsMode string `mapstructure:"guest_additions_mode"` GuestAdditionsPath string `mapstructure:"guest_additions_path"` @@ -93,10 +93,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { b.config.DiskSize = 40000 } - if b.config.FloppyFiles == nil { - b.config.FloppyFiles = make([]string, 0) - } - if b.config.GuestAdditionsMode == "" { b.config.GuestAdditionsMode = "upload" } @@ -212,16 +208,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } } - for i, file := range b.config.FloppyFiles { - var err error - b.config.FloppyFiles[i], err = b.config.tpl.Process(file, nil) - if err != nil { - errs = packer.MultiErrorAppend(errs, - fmt.Errorf("Error processing floppy_files[%d]: %s", - i, err)) - } - } - if !(b.config.Format == "ovf" || b.config.Format == "ova") { errs = packer.MultiErrorAppend( errs, errors.New("invalid format, only 'ovf' or 'ova' are allowed")) diff --git a/builder/virtualbox/ovf/builder.go b/builder/virtualbox/ovf/builder.go index 18195644a..1d7c088d9 100644 --- a/builder/virtualbox/ovf/builder.go +++ b/builder/virtualbox/ovf/builder.go @@ -47,10 +47,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Path: b.config.OutputDir, }, new(vboxcommon.StepSuppressMessages), + &common.StepCreateFloppy{ + Files: b.config.FloppyFiles, + }, /* - &common.StepCreateFloppy{ - Files: b.config.FloppyFiles, - }, new(stepAttachGuestAdditions), new(stepAttachFloppy), new(stepForwardSSH), diff --git a/builder/virtualbox/ovf/config.go b/builder/virtualbox/ovf/config.go index 3064365e6..7dcf0da50 100644 --- a/builder/virtualbox/ovf/config.go +++ b/builder/virtualbox/ovf/config.go @@ -9,6 +9,7 @@ import ( // Config is the configuration structure for the builder. type Config struct { common.PackerConfig `mapstructure:",squash"` + vboxcommon.FloppyConfig `mapstructure:",squash"` vboxcommon.OutputConfig `mapstructure:",squash"` tpl *packer.ConfigTemplate