From c2013bf680008e2d43a4c7d533b264ac799a2bcd Mon Sep 17 00:00:00 2001 From: Rickard von Essen Date: Sun, 4 May 2014 17:56:57 +0200 Subject: [PATCH] virtualbox-ovf support for guest_additions_mode Fixes #1035 "virtualbox-ovf" builder doesn't support "guest_additions_mode" option --- .../virtualbox/common/guest_addition_modes.go | 9 +++ .../step_attach_guest_additions.go | 19 +++--- .../step_download_guest_additions.go | 29 +++++---- .../step_upload_guest_additions.go | 22 ++++--- builder/virtualbox/iso/builder.go | 31 +++++----- builder/virtualbox/iso/builder_test.go | 5 +- builder/virtualbox/ovf/builder.go | 21 +++++-- builder/virtualbox/ovf/config.go | 61 +++++++++++++++++-- 8 files changed, 136 insertions(+), 61 deletions(-) create mode 100644 builder/virtualbox/common/guest_addition_modes.go rename builder/virtualbox/{iso => common}/step_attach_guest_additions.go (79%) rename builder/virtualbox/{iso => common}/step_download_guest_additions.go (87%) rename builder/virtualbox/{iso => common}/step_upload_guest_additions.go (71%) diff --git a/builder/virtualbox/common/guest_addition_modes.go b/builder/virtualbox/common/guest_addition_modes.go new file mode 100644 index 000000000..b713df7c8 --- /dev/null +++ b/builder/virtualbox/common/guest_addition_modes.go @@ -0,0 +1,9 @@ +package common + +// These are the different valid mode values for "guest_additions_mode" which +// determine how guest additions are delivered to the guest. +const ( + GuestAdditionsModeDisable string = "disable" + GuestAdditionsModeAttach = "attach" + GuestAdditionsModeUpload = "upload" +) diff --git a/builder/virtualbox/iso/step_attach_guest_additions.go b/builder/virtualbox/common/step_attach_guest_additions.go similarity index 79% rename from builder/virtualbox/iso/step_attach_guest_additions.go rename to builder/virtualbox/common/step_attach_guest_additions.go index f35710259..bb047ed9a 100644 --- a/builder/virtualbox/iso/step_attach_guest_additions.go +++ b/builder/virtualbox/common/step_attach_guest_additions.go @@ -1,9 +1,8 @@ -package iso +package common import ( "fmt" "github.com/mitchellh/multistep" - vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" "github.com/mitchellh/packer/packer" "log" ) @@ -19,18 +18,18 @@ import ( // vmName string // // Produces: -type stepAttachGuestAdditions struct { - attachedPath string +type StepAttachGuestAdditions struct { + attachedPath string + GuestAdditionsMode string } -func (s *stepAttachGuestAdditions) Run(state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(*config) - driver := state.Get("driver").(vboxcommon.Driver) +func (s *StepAttachGuestAdditions) Run(state multistep.StateBag) multistep.StepAction { + driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) vmName := state.Get("vmName").(string) // If we're not attaching the guest additions then just return - if config.GuestAdditionsMode != GuestAdditionsModeAttach { + if s.GuestAdditionsMode != GuestAdditionsModeAttach { log.Println("Not attaching guest additions since we're uploading.") return multistep.ActionContinue } @@ -61,12 +60,12 @@ func (s *stepAttachGuestAdditions) Run(state multistep.StateBag) multistep.StepA return multistep.ActionContinue } -func (s *stepAttachGuestAdditions) Cleanup(state multistep.StateBag) { +func (s *StepAttachGuestAdditions) Cleanup(state multistep.StateBag) { if s.attachedPath == "" { return } - driver := state.Get("driver").(vboxcommon.Driver) + driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) vmName := state.Get("vmName").(string) diff --git a/builder/virtualbox/iso/step_download_guest_additions.go b/builder/virtualbox/common/step_download_guest_additions.go similarity index 87% rename from builder/virtualbox/iso/step_download_guest_additions.go rename to builder/virtualbox/common/step_download_guest_additions.go index dbb8b9d3c..9ee8a6f11 100644 --- a/builder/virtualbox/iso/step_download_guest_additions.go +++ b/builder/virtualbox/common/step_download_guest_additions.go @@ -1,10 +1,9 @@ -package iso +package common import ( "bytes" "fmt" "github.com/mitchellh/multistep" - vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" "github.com/mitchellh/packer/common" "github.com/mitchellh/packer/packer" "io" @@ -28,16 +27,20 @@ type guestAdditionsUrlTemplate struct { // // Produces: // guest_additions_path string - Path to the guest additions. -type stepDownloadGuestAdditions struct{} +type StepDownloadGuestAdditions struct { + GuestAdditionsMode string + GuestAdditionsURL string + GuestAdditionsSHA256 string + Tpl *packer.ConfigTemplate +} -func (s *stepDownloadGuestAdditions) Run(state multistep.StateBag) multistep.StepAction { +func (s *StepDownloadGuestAdditions) Run(state multistep.StateBag) multistep.StepAction { var action multistep.StepAction - driver := state.Get("driver").(vboxcommon.Driver) + driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) - config := state.Get("config").(*config) // If we've disabled guest additions, don't download - if config.GuestAdditionsMode == GuestAdditionsModeDisable { + if s.GuestAdditionsMode == GuestAdditionsModeDisable { log.Println("Not downloading guest additions since it is disabled.") return multistep.ActionContinue } @@ -59,8 +62,8 @@ func (s *stepDownloadGuestAdditions) Run(state multistep.StateBag) multistep.Ste // Use provided version or get it from virtualbox.org var checksum string - if config.GuestAdditionsSHA256 != "" { - checksum = config.GuestAdditionsSHA256 + if s.GuestAdditionsSHA256 != "" { + checksum = s.GuestAdditionsSHA256 } else { checksum, action = s.downloadAdditionsSHA256(state, version, additionsName) if action != multistep.ActionContinue { @@ -69,13 +72,13 @@ func (s *stepDownloadGuestAdditions) Run(state multistep.StateBag) multistep.Ste } // Use the provided source (URL or file path) or generate it - url := config.GuestAdditionsURL + url := s.GuestAdditionsURL if url != "" { tplData := &guestAdditionsUrlTemplate{ Version: version, } - url, err = config.tpl.Process(url, tplData) + url, err = s.Tpl.Process(url, tplData) if err != nil { err := fmt.Errorf("Error preparing guest additions url: %s", err) state.Put("error", err) @@ -110,9 +113,9 @@ func (s *stepDownloadGuestAdditions) Run(state multistep.StateBag) multistep.Ste return downStep.Run(state) } -func (s *stepDownloadGuestAdditions) Cleanup(state multistep.StateBag) {} +func (s *StepDownloadGuestAdditions) Cleanup(state multistep.StateBag) {} -func (s *stepDownloadGuestAdditions) downloadAdditionsSHA256(state multistep.StateBag, additionsVersion string, additionsName string) (string, multistep.StepAction) { +func (s *StepDownloadGuestAdditions) downloadAdditionsSHA256(state multistep.StateBag, additionsVersion string, additionsName string) (string, multistep.StepAction) { // First things first, we get the list of checksums for the files available // for this version. checksumsUrl := fmt.Sprintf( diff --git a/builder/virtualbox/iso/step_upload_guest_additions.go b/builder/virtualbox/common/step_upload_guest_additions.go similarity index 71% rename from builder/virtualbox/iso/step_upload_guest_additions.go rename to builder/virtualbox/common/step_upload_guest_additions.go index 0ae72c000..5e8b6a86c 100644 --- a/builder/virtualbox/iso/step_upload_guest_additions.go +++ b/builder/virtualbox/common/step_upload_guest_additions.go @@ -1,9 +1,8 @@ -package iso +package common import ( "fmt" "github.com/mitchellh/multistep" - vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" "github.com/mitchellh/packer/packer" "log" "os" @@ -14,16 +13,19 @@ type guestAdditionsPathTemplate struct { } // This step uploads the guest additions ISO to the VM. -type stepUploadGuestAdditions struct{} +type StepUploadGuestAdditions struct { + GuestAdditionsMode string + GuestAdditionsPath string + Tpl *packer.ConfigTemplate +} -func (s *stepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepAction { +func (s *StepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepAction { comm := state.Get("communicator").(packer.Communicator) - config := state.Get("config").(*config) - driver := state.Get("driver").(vboxcommon.Driver) + driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) // If we're attaching then don't do this, since we attached. - if config.GuestAdditionsMode != GuestAdditionsModeUpload { + if s.GuestAdditionsMode != GuestAdditionsModeUpload { log.Println("Not uploading guest additions since mode is not upload") return multistep.ActionContinue } @@ -47,7 +49,7 @@ func (s *stepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepA Version: version, } - config.GuestAdditionsPath, err = config.tpl.Process(config.GuestAdditionsPath, tplData) + s.GuestAdditionsPath, err = s.Tpl.Process(s.GuestAdditionsPath, tplData) if err != nil { err := fmt.Errorf("Error preparing guest additions path: %s", err) state.Put("error", err) @@ -56,7 +58,7 @@ func (s *stepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepA } ui.Say("Uploading VirtualBox guest additions ISO...") - if err := comm.Upload(config.GuestAdditionsPath, f); err != nil { + if err := comm.Upload(s.GuestAdditionsPath, f); err != nil { state.Put("error", fmt.Errorf("Error uploading guest additions: %s", err)) return multistep.ActionHalt } @@ -64,4 +66,4 @@ func (s *stepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepA return multistep.ActionContinue } -func (s *stepUploadGuestAdditions) Cleanup(state multistep.StateBag) {} +func (s *StepUploadGuestAdditions) Cleanup(state multistep.StateBag) {} diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index 7861866b2..f41118a45 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -13,14 +13,6 @@ import ( const BuilderId = "mitchellh.virtualbox" -// These are the different valid mode values for "guest_additions_mode" which -// determine how guest additions are delivered to the guest. -const ( - GuestAdditionsModeDisable string = "disable" - GuestAdditionsModeAttach = "attach" - GuestAdditionsModeUpload = "upload" -) - type Builder struct { config config runner multistep.Runner @@ -220,9 +212,9 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { validMode := false validModes := []string{ - GuestAdditionsModeDisable, - GuestAdditionsModeAttach, - GuestAdditionsModeUpload, + vboxcommon.GuestAdditionsModeDisable, + vboxcommon.GuestAdditionsModeAttach, + vboxcommon.GuestAdditionsModeUpload, } for _, mode := range validModes { @@ -269,7 +261,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe } steps := []multistep.Step{ - new(stepDownloadGuestAdditions), + &vboxcommon.StepDownloadGuestAdditions{ + GuestAdditionsMode: b.config.GuestAdditionsMode, + GuestAdditionsURL: b.config.GuestAdditionsURL, + GuestAdditionsSHA256: b.config.GuestAdditionsSHA256, + Tpl: b.config.tpl, + }, &common.StepDownload{ Checksum: b.config.ISOChecksum, ChecksumType: b.config.ISOChecksumType, @@ -289,7 +286,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe new(stepCreateVM), new(stepCreateDisk), new(stepAttachISO), - new(stepAttachGuestAdditions), + &vboxcommon.StepAttachGuestAdditions{ + GuestAdditionsMode: b.config.GuestAdditionsMode, + }, new(vboxcommon.StepAttachFloppy), &vboxcommon.StepForwardSSH{ GuestPort: b.config.SSHPort, @@ -313,7 +312,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &vboxcommon.StepUploadVersion{ Path: b.config.VBoxVersionFile, }, - new(stepUploadGuestAdditions), + &vboxcommon.StepUploadGuestAdditions{ + GuestAdditionsMode: b.config.GuestAdditionsMode, + GuestAdditionsPath: b.config.GuestAdditionsPath, + Tpl: b.config.tpl, + }, new(common.StepProvision), &vboxcommon.StepShutdown{ Command: b.config.ShutdownCommand, diff --git a/builder/virtualbox/iso/builder_test.go b/builder/virtualbox/iso/builder_test.go index ddd562f15..c9d5414dd 100644 --- a/builder/virtualbox/iso/builder_test.go +++ b/builder/virtualbox/iso/builder_test.go @@ -1,6 +1,7 @@ package iso import ( + "github.com/mitchellh/packer/builder/virtualbox/common" "github.com/mitchellh/packer/packer" "reflect" "testing" @@ -37,7 +38,7 @@ func TestBuilderPrepare_Defaults(t *testing.T) { t.Fatalf("should not have error: %s", err) } - if b.config.GuestAdditionsMode != GuestAdditionsModeUpload { + if b.config.GuestAdditionsMode != common.GuestAdditionsModeUpload { t.Errorf("bad guest additions mode: %s", b.config.GuestAdditionsMode) } @@ -111,7 +112,7 @@ func TestBuilderPrepare_GuestAdditionsMode(t *testing.T) { t.Fatalf("should not have error: %s", err) } - if b.config.GuestAdditionsMode != GuestAdditionsModeAttach { + if b.config.GuestAdditionsMode != common.GuestAdditionsModeAttach { t.Fatalf("bad: %s", b.config.GuestAdditionsMode) } diff --git a/builder/virtualbox/ovf/builder.go b/builder/virtualbox/ovf/builder.go index ffc1a0d1b..312fc4701 100644 --- a/builder/virtualbox/ovf/builder.go +++ b/builder/virtualbox/ovf/builder.go @@ -42,6 +42,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe state := new(multistep.BasicStateBag) state.Put("config", b.config) state.Put("driver", driver) + state.Put("cache", cache) state.Put("hook", hook) state.Put("ui", ui) @@ -55,14 +56,20 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &common.StepCreateFloppy{ Files: b.config.FloppyFiles, }, + &vboxcommon.StepDownloadGuestAdditions{ + GuestAdditionsMode: b.config.GuestAdditionsMode, + GuestAdditionsURL: b.config.GuestAdditionsURL, + GuestAdditionsSHA256: b.config.GuestAdditionsSHA256, + Tpl: b.config.tpl, + }, &StepImport{ Name: b.config.VMName, SourcePath: b.config.SourcePath, ImportOpts: b.config.ImportOpts, }, - /* - new(stepAttachGuestAdditions), - */ + &vboxcommon.StepAttachGuestAdditions{ + GuestAdditionsMode: b.config.GuestAdditionsMode, + }, new(vboxcommon.StepAttachFloppy), &vboxcommon.StepForwardSSH{ GuestPort: b.config.SSHPort, @@ -85,9 +92,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &vboxcommon.StepUploadVersion{ Path: b.config.VBoxVersionFile, }, - /* - new(stepUploadGuestAdditions), - */ + &vboxcommon.StepUploadGuestAdditions{ + GuestAdditionsMode: b.config.GuestAdditionsMode, + GuestAdditionsPath: b.config.GuestAdditionsPath, + Tpl: b.config.tpl, + }, new(common.StepProvision), &vboxcommon.StepShutdown{ Command: b.config.ShutdownCommand, diff --git a/builder/virtualbox/ovf/config.go b/builder/virtualbox/ovf/config.go index 4b0c9111a..a8df48791 100644 --- a/builder/virtualbox/ovf/config.go +++ b/builder/virtualbox/ovf/config.go @@ -3,6 +3,7 @@ package ovf import ( "fmt" "os" + "strings" vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" "github.com/mitchellh/packer/common" @@ -23,9 +24,13 @@ type Config struct { vboxcommon.VBoxManagePostConfig `mapstructure:",squash"` vboxcommon.VBoxVersionConfig `mapstructure:",squash"` - SourcePath string `mapstructure:"source_path"` - VMName string `mapstructure:"vm_name"` - ImportOpts string `mapstructure:"import_opts"` + SourcePath string `mapstructure:"source_path"` + GuestAdditionsMode string `mapstructure:"guest_additions_mode"` + GuestAdditionsPath string `mapstructure:"guest_additions_path"` + GuestAdditionsURL string `mapstructure:"guest_additions_url"` + GuestAdditionsSHA256 string `mapstructure:"guest_additions_sha256"` + VMName string `mapstructure:"vm_name"` + ImportOpts string `mapstructure:"import_opts"` tpl *packer.ConfigTemplate } @@ -44,6 +49,13 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { c.tpl.UserVars = c.PackerUserVars // Defaults + if c.GuestAdditionsMode == "" { + c.GuestAdditionsMode = "upload" + } + + if c.GuestAdditionsPath == "" { + c.GuestAdditionsPath = "VBoxGuestAdditions.iso" + } if c.VMName == "" { c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", c.PackerBuildName) } @@ -62,9 +74,11 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { errs = packer.MultiErrorAppend(errs, c.VBoxVersionConfig.Prepare(c.tpl)...) templates := map[string]*string{ - "source_path": &c.SourcePath, - "vm_name": &c.VMName, - "import_opts": &c.ImportOpts, + "guest_additions_mode": &c.GuestAdditionsMode, + "guest_additions_sha256": &c.GuestAdditionsSHA256, + "source_path": &c.SourcePath, + "vm_name": &c.VMName, + "import_opts": &c.ImportOpts, } for n, ptr := range templates { @@ -85,6 +99,41 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { } } + validates := map[string]*string{ + "guest_additions_path": &c.GuestAdditionsPath, + "guest_additions_url": &c.GuestAdditionsURL, + } + + for n, ptr := range validates { + if err := c.tpl.Validate(*ptr); err != nil { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("Error parsing %s: %s", n, err)) + } + } + + validMode := false + validModes := []string{ + vboxcommon.GuestAdditionsModeDisable, + vboxcommon.GuestAdditionsModeAttach, + vboxcommon.GuestAdditionsModeUpload, + } + + for _, mode := range validModes { + if c.GuestAdditionsMode == mode { + validMode = true + break + } + } + + if !validMode { + errs = packer.MultiErrorAppend(errs, + fmt.Errorf("guest_additions_mode is invalid. Must be one of: %v", validModes)) + } + + if c.GuestAdditionsSHA256 != "" { + c.GuestAdditionsSHA256 = strings.ToLower(c.GuestAdditionsSHA256) + } + // Warnings var warnings []string if c.ShutdownCommand == "" {