From 01eff9472a24007f4cfc634e3ad9a73322ecfa11 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 19 Jun 2019 16:34:14 +0200 Subject: [PATCH] virtualbox: merge ExportOpts and ExportConfig structs --- builder/virtualbox/common/export_config.go | 45 +++++++++++++++++++ .../virtualbox/common/export_config_test.go | 15 +++++++ builder/virtualbox/common/export_opts.go | 24 ---------- builder/virtualbox/common/export_opts_test.go | 22 --------- builder/virtualbox/iso/builder.go | 5 +-- builder/virtualbox/ovf/builder.go | 2 +- builder/virtualbox/ovf/config.go | 3 +- .../common/_ExportConfig-not-required.html.md | 41 +++++++++++++++++ 8 files changed, 105 insertions(+), 52 deletions(-) delete mode 100644 builder/virtualbox/common/export_opts.go delete mode 100644 builder/virtualbox/common/export_opts_test.go diff --git a/builder/virtualbox/common/export_config.go b/builder/virtualbox/common/export_config.go index 9a66626ce..6d10cad13 100644 --- a/builder/virtualbox/common/export_config.go +++ b/builder/virtualbox/common/export_config.go @@ -12,6 +12,47 @@ type ExportConfig struct { // Either ovf or ova, this specifies the output format // of the exported virtual machine. This defaults to ovf. Format string `mapstructure:"format" required:"false"` + // Additional options to pass to the [VBoxManage + // export](https://www.virtualbox.org/manual/ch09.html#vboxmanage-export). + // This can be useful for passing product information to include in the + // resulting appliance file. Packer JSON configuration file example: + // + // ``` json + // { + // "type": "virtualbox-iso", + // "export_opts": + // [ + // "--manifest", + // "--vsys", "0", + // "--description", "{{user `vm_description`}}", + // "--version", "{{user `vm_version`}}" + // ], + // "format": "ova", + // } + // ``` + // + // A VirtualBox [VM + // description](https://www.virtualbox.org/manual/ch09.html#vboxmanage-export-ovf) + // may contain arbitrary strings; the GUI interprets HTML formatting. However, + // the JSON format does not allow arbitrary newlines within a value. Add a + // multi-line description by preparing the string in the shell before the + // packer call like this (shell `>` continuation character snipped for easier + // copy & paste): + // + // ``` {.shell} + // + // vm_description='some + // multiline + // description' + // + // vm_version='0.2.0' + // + // packer build \ + // -var "vm_description=${vm_description}" \ + // -var "vm_version=${vm_version}" \ + // "packer_conf.json" + // ``` + ExportOpts []string `mapstructure:"export_opts" required:"false"` } func (c *ExportConfig) Prepare(ctx *interpolate.Context) []error { @@ -25,5 +66,9 @@ func (c *ExportConfig) Prepare(ctx *interpolate.Context) []error { errors.New("invalid format, only 'ovf' or 'ova' are allowed")) } + if c.ExportOpts == nil { + c.ExportOpts = make([]string, 0) + } + return errs } diff --git a/builder/virtualbox/common/export_config_test.go b/builder/virtualbox/common/export_config_test.go index 711fb3c99..fdf6ccc6d 100644 --- a/builder/virtualbox/common/export_config_test.go +++ b/builder/virtualbox/common/export_config_test.go @@ -34,3 +34,18 @@ func TestExportConfigPrepare_BootWait(t *testing.T) { t.Fatalf("should not have error: %s", errs) } } + +func TestExportConfigPrepare_Opts(t *testing.T) { + var c *ExportConfig + var errs []error + + // Good + c = new(ExportConfig) + c.ExportOpts = []string{ + "--options", + } + errs = c.Prepare(interpolate.NewContext()) + if len(errs) > 0 { + t.Fatalf("should not have error: %s", errs) + } +} diff --git a/builder/virtualbox/common/export_opts.go b/builder/virtualbox/common/export_opts.go deleted file mode 100644 index b5a22072c..000000000 --- a/builder/virtualbox/common/export_opts.go +++ /dev/null @@ -1,24 +0,0 @@ -//go:generate struct-markdown - -package common - -import ( - "github.com/hashicorp/packer/template/interpolate" -) - -type ExportOpts struct { - // Additional options to pass to the - // VBoxManage - // export. This - // can be useful for passing product information to include in the resulting - // appliance file. Packer JSON configuration file example: - ExportOpts []string `mapstructure:"export_opts" required:"false"` -} - -func (c *ExportOpts) Prepare(ctx *interpolate.Context) []error { - if c.ExportOpts == nil { - c.ExportOpts = make([]string, 0) - } - - return nil -} diff --git a/builder/virtualbox/common/export_opts_test.go b/builder/virtualbox/common/export_opts_test.go deleted file mode 100644 index 860558d7a..000000000 --- a/builder/virtualbox/common/export_opts_test.go +++ /dev/null @@ -1,22 +0,0 @@ -package common - -import ( - "testing" - - "github.com/hashicorp/packer/template/interpolate" -) - -func TestExportOptsPrepare_BootWait(t *testing.T) { - var c *ExportOpts - var errs []error - - // Good - c = new(ExportOpts) - c.ExportOpts = []string{ - "--options", - } - errs = c.Prepare(interpolate.NewContext()) - if len(errs) > 0 { - t.Fatalf("should not have error: %s", errs) - } -} diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index 7adc22338..71bdbae96 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -32,7 +32,6 @@ type Config struct { common.FloppyConfig `mapstructure:",squash"` bootcommand.BootConfig `mapstructure:",squash"` vboxcommon.ExportConfig `mapstructure:",squash"` - vboxcommon.ExportOpts `mapstructure:",squash"` vboxcommon.OutputConfig `mapstructure:",squash"` vboxcommon.RunConfig `mapstructure:",squash"` vboxcommon.ShutdownConfig `mapstructure:",squash"` @@ -146,7 +145,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, isoErrs...) errs = packer.MultiErrorAppend(errs, b.config.ExportConfig.Prepare(&b.config.ctx)...) - errs = packer.MultiErrorAppend(errs, b.config.ExportOpts.Prepare(&b.config.ctx)...) + errs = packer.MultiErrorAppend(errs, b.config.ExportConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend( errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...) @@ -362,7 +361,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &vboxcommon.StepExport{ Format: b.config.Format, OutputDir: b.config.OutputDir, - ExportOpts: b.config.ExportOpts.ExportOpts, + ExportOpts: b.config.ExportConfig.ExportOpts, Bundling: b.config.VBoxBundleConfig, SkipNatMapping: b.config.SSHSkipNatMapping, SkipExport: b.config.SkipExport, diff --git a/builder/virtualbox/ovf/builder.go b/builder/virtualbox/ovf/builder.go index aa3546c7b..de0791f2b 100644 --- a/builder/virtualbox/ovf/builder.go +++ b/builder/virtualbox/ovf/builder.go @@ -152,7 +152,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &vboxcommon.StepExport{ Format: b.config.Format, OutputDir: b.config.OutputDir, - ExportOpts: b.config.ExportOpts.ExportOpts, + ExportOpts: b.config.ExportConfig.ExportOpts, SkipNatMapping: b.config.SSHSkipNatMapping, SkipExport: b.config.SkipExport, }, diff --git a/builder/virtualbox/ovf/config.go b/builder/virtualbox/ovf/config.go index 586ae50ae..313493657 100644 --- a/builder/virtualbox/ovf/config.go +++ b/builder/virtualbox/ovf/config.go @@ -22,7 +22,6 @@ type Config struct { common.FloppyConfig `mapstructure:",squash"` bootcommand.BootConfig `mapstructure:",squash"` vboxcommon.ExportConfig `mapstructure:",squash"` - vboxcommon.ExportOpts `mapstructure:",squash"` vboxcommon.OutputConfig `mapstructure:",squash"` vboxcommon.RunConfig `mapstructure:",squash"` vboxcommon.SSHConfig `mapstructure:",squash"` @@ -145,7 +144,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { // Prepare the errors var errs *packer.MultiError errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(&c.ctx)...) - errs = packer.MultiErrorAppend(errs, c.ExportOpts.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.HTTPConfig.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(&c.ctx, &c.PackerConfig)...) diff --git a/website/source/partials/builder/virtualbox/common/_ExportConfig-not-required.html.md b/website/source/partials/builder/virtualbox/common/_ExportConfig-not-required.html.md index 9fe3971b2..b0be45beb 100644 --- a/website/source/partials/builder/virtualbox/common/_ExportConfig-not-required.html.md +++ b/website/source/partials/builder/virtualbox/common/_ExportConfig-not-required.html.md @@ -2,4 +2,45 @@ - `format` (string) - Either ovf or ova, this specifies the output format of the exported virtual machine. This defaults to ovf. + +- `export_opts` ([]string) - Additional options to pass to the [VBoxManage + export](https://www.virtualbox.org/manual/ch09.html#vboxmanage-export). + This can be useful for passing product information to include in the + resulting appliance file. Packer JSON configuration file example: + + ``` json + { + "type": "virtualbox-iso", + "export_opts": + [ + "--manifest", + "--vsys", "0", + "--description", "{{user `vm_description`}}", + "--version", "{{user `vm_version`}}" + ], + "format": "ova", + } + ``` + + A VirtualBox [VM + description](https://www.virtualbox.org/manual/ch09.html#vboxmanage-export-ovf) + may contain arbitrary strings; the GUI interprets HTML formatting. However, + the JSON format does not allow arbitrary newlines within a value. Add a + multi-line description by preparing the string in the shell before the + packer call like this (shell `>` continuation character snipped for easier + copy & paste): + + ``` {.shell} + + vm_description='some + multiline + description' + + vm_version='0.2.0' + + packer build \ + -var "vm_description=${vm_description}" \ + -var "vm_version=${vm_version}" \ + "packer_conf.json" + ``` \ No newline at end of file