From a19bd564d3ab904fc1e91cdb4750e11f2ea4b833 Mon Sep 17 00:00:00 2001 From: Johan Siebens Date: Mon, 19 Aug 2013 20:21:36 +0200 Subject: [PATCH] builder/virtualbox: export to ovf or ova (default ovf) --- builder/virtualbox/builder.go | 11 ++++++++++ builder/virtualbox/builder_test.go | 32 ++++++++++++++++++++++++++++++ builder/virtualbox/step_export.go | 2 +- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/builder/virtualbox/builder.go b/builder/virtualbox/builder.go index dcb4867b8..6c620a357 100644 --- a/builder/virtualbox/builder.go +++ b/builder/virtualbox/builder.go @@ -48,6 +48,7 @@ type config struct { VBoxVersionFile string `mapstructure:"virtualbox_version_file"` VBoxManage [][]string `mapstructure:"vboxmanage"` VMName string `mapstructure:"vm_name"` + Format string `mapstructure:"format"` RawBootWait string `mapstructure:"boot_wait"` RawSingleISOUrl string `mapstructure:"iso_url"` @@ -131,6 +132,10 @@ func (b *Builder) Prepare(raws ...interface{}) error { b.config.VMName = fmt.Sprintf("packer-%s", b.config.PackerBuildName) } + if b.config.Format == "" { + b.config.Format = "ovf" + } + // Errors templates := map[string]*string{ "guest_additions_sha256": &b.config.GuestAdditionsSHA256, @@ -145,6 +150,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { "ssh_username": &b.config.SSHUser, "virtualbox_version_file": &b.config.VBoxVersionFile, "vm_name": &b.config.VMName, + "format": &b.config.Format, "boot_wait": &b.config.RawBootWait, "shutdown_timeout": &b.config.RawShutdownTimeout, "ssh_wait_timeout": &b.config.RawSSHWaitTimeout, @@ -197,6 +203,11 @@ func (b *Builder) Prepare(raws ...interface{}) error { } } + if !(b.config.Format == "ovf" || b.config.Format == "ova") { + errs = packer.MultiErrorAppend( + errs, errors.New("invalid format, only 'ovf' or 'ova' are allowed")) + } + if b.config.HTTPPortMin > b.config.HTTPPortMax { errs = packer.MultiErrorAppend( errs, errors.New("http_port_min must be less than http_port_max")) diff --git a/builder/virtualbox/builder_test.go b/builder/virtualbox/builder_test.go index 035cb87c8..f3afdd6dd 100644 --- a/builder/virtualbox/builder_test.go +++ b/builder/virtualbox/builder_test.go @@ -58,6 +58,10 @@ func TestBuilderPrepare_Defaults(t *testing.T) { if b.config.VMName != "packer-foo" { t.Errorf("bad vm name: %s", b.config.VMName) } + + if b.config.Format != "ovf" { + t.Errorf("bad format: %s", b.config.Format) + } } func TestBuilderPrepare_BootWait(t *testing.T) { @@ -248,6 +252,34 @@ func TestBuilderPrepare_HTTPPort(t *testing.T) { } } +func TestBuilderPrepare_Format(t *testing.T) { + var b Builder + config := testConfig() + + // Bad + config["format"] = "illegal value" + err := b.Prepare(config) + if err == nil { + t.Fatal("should have error") + } + + // Good + config["format"] = "ova" + b = Builder{} + err = b.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + // Good + config["format"] = "ovf" + b = Builder{} + err = b.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } +} + func TestBuilderPrepare_InvalidKey(t *testing.T) { var b Builder config := testConfig() diff --git a/builder/virtualbox/step_export.go b/builder/virtualbox/step_export.go index c15fc1a0a..b6ee82e00 100644 --- a/builder/virtualbox/step_export.go +++ b/builder/virtualbox/step_export.go @@ -50,7 +50,7 @@ func (s *stepExport) Run(state map[string]interface{}) multistep.StepAction { } // Export the VM to an OVF - outputPath := filepath.Join(config.OutputDir, "packer.ovf") + outputPath := filepath.Join(config.OutputDir, "packer." + config.Format) command = []string{ "export",