virtualbox: merge ExportOpts and ExportConfig structs

This commit is contained in:
Adrien Delorme 2019-06-19 16:34:14 +02:00
parent d4c400aa71
commit 01eff9472a
8 changed files with 105 additions and 52 deletions

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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,

View File

@ -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,
},

View File

@ -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)...)

View File

@ -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"
```