Commit c2013bf6 authored by Rickard von Essen's avatar Rickard von Essen

virtualbox-ovf support for guest_additions_mode

Fixes #1035 "virtualbox-ovf" builder doesn't support "guest_additions_mode" option
parent 40c66a04
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"
)
package iso package common
import ( import (
"fmt" "fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log" "log"
) )
...@@ -19,18 +18,18 @@ import ( ...@@ -19,18 +18,18 @@ import (
// vmName string // vmName string
// //
// Produces: // Produces:
type stepAttachGuestAdditions struct { type StepAttachGuestAdditions struct {
attachedPath string attachedPath string
GuestAdditionsMode string
} }
func (s *stepAttachGuestAdditions) Run(state multistep.StateBag) multistep.StepAction { func (s *StepAttachGuestAdditions) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*config) driver := state.Get("driver").(Driver)
driver := state.Get("driver").(vboxcommon.Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
vmName := state.Get("vmName").(string) vmName := state.Get("vmName").(string)
// If we're not attaching the guest additions then just return // 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.") log.Println("Not attaching guest additions since we're uploading.")
return multistep.ActionContinue return multistep.ActionContinue
} }
...@@ -61,12 +60,12 @@ func (s *stepAttachGuestAdditions) Run(state multistep.StateBag) multistep.StepA ...@@ -61,12 +60,12 @@ func (s *stepAttachGuestAdditions) Run(state multistep.StateBag) multistep.StepA
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *stepAttachGuestAdditions) Cleanup(state multistep.StateBag) { func (s *StepAttachGuestAdditions) Cleanup(state multistep.StateBag) {
if s.attachedPath == "" { if s.attachedPath == "" {
return return
} }
driver := state.Get("driver").(vboxcommon.Driver) driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
vmName := state.Get("vmName").(string) vmName := state.Get("vmName").(string)
......
package iso package common
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
"github.com/mitchellh/packer/common" "github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"io" "io"
...@@ -28,16 +27,20 @@ type guestAdditionsUrlTemplate struct { ...@@ -28,16 +27,20 @@ type guestAdditionsUrlTemplate struct {
// //
// Produces: // Produces:
// guest_additions_path string - Path to the guest additions. // 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 var action multistep.StepAction
driver := state.Get("driver").(vboxcommon.Driver) driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(*config)
// If we've disabled guest additions, don't download // 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.") log.Println("Not downloading guest additions since it is disabled.")
return multistep.ActionContinue return multistep.ActionContinue
} }
...@@ -59,8 +62,8 @@ func (s *stepDownloadGuestAdditions) Run(state multistep.StateBag) multistep.Ste ...@@ -59,8 +62,8 @@ func (s *stepDownloadGuestAdditions) Run(state multistep.StateBag) multistep.Ste
// Use provided version or get it from virtualbox.org // Use provided version or get it from virtualbox.org
var checksum string var checksum string
if config.GuestAdditionsSHA256 != "" { if s.GuestAdditionsSHA256 != "" {
checksum = config.GuestAdditionsSHA256 checksum = s.GuestAdditionsSHA256
} else { } else {
checksum, action = s.downloadAdditionsSHA256(state, version, additionsName) checksum, action = s.downloadAdditionsSHA256(state, version, additionsName)
if action != multistep.ActionContinue { if action != multistep.ActionContinue {
...@@ -69,13 +72,13 @@ func (s *stepDownloadGuestAdditions) Run(state multistep.StateBag) multistep.Ste ...@@ -69,13 +72,13 @@ func (s *stepDownloadGuestAdditions) Run(state multistep.StateBag) multistep.Ste
} }
// Use the provided source (URL or file path) or generate it // Use the provided source (URL or file path) or generate it
url := config.GuestAdditionsURL url := s.GuestAdditionsURL
if url != "" { if url != "" {
tplData := &guestAdditionsUrlTemplate{ tplData := &guestAdditionsUrlTemplate{
Version: version, Version: version,
} }
url, err = config.tpl.Process(url, tplData) url, err = s.Tpl.Process(url, tplData)
if err != nil { if err != nil {
err := fmt.Errorf("Error preparing guest additions url: %s", err) err := fmt.Errorf("Error preparing guest additions url: %s", err)
state.Put("error", err) state.Put("error", err)
...@@ -110,9 +113,9 @@ func (s *stepDownloadGuestAdditions) Run(state multistep.StateBag) multistep.Ste ...@@ -110,9 +113,9 @@ func (s *stepDownloadGuestAdditions) Run(state multistep.StateBag) multistep.Ste
return downStep.Run(state) 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 // First things first, we get the list of checksums for the files available
// for this version. // for this version.
checksumsUrl := fmt.Sprintf( checksumsUrl := fmt.Sprintf(
......
package iso package common
import ( import (
"fmt" "fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log" "log"
"os" "os"
...@@ -14,16 +13,19 @@ type guestAdditionsPathTemplate struct { ...@@ -14,16 +13,19 @@ type guestAdditionsPathTemplate struct {
} }
// This step uploads the guest additions ISO to the VM. // 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) comm := state.Get("communicator").(packer.Communicator)
config := state.Get("config").(*config) driver := state.Get("driver").(Driver)
driver := state.Get("driver").(vboxcommon.Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
// If we're attaching then don't do this, since we attached. // 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") log.Println("Not uploading guest additions since mode is not upload")
return multistep.ActionContinue return multistep.ActionContinue
} }
...@@ -47,7 +49,7 @@ func (s *stepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepA ...@@ -47,7 +49,7 @@ func (s *stepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepA
Version: version, Version: version,
} }
config.GuestAdditionsPath, err = config.tpl.Process(config.GuestAdditionsPath, tplData) s.GuestAdditionsPath, err = s.Tpl.Process(s.GuestAdditionsPath, tplData)
if err != nil { if err != nil {
err := fmt.Errorf("Error preparing guest additions path: %s", err) err := fmt.Errorf("Error preparing guest additions path: %s", err)
state.Put("error", err) state.Put("error", err)
...@@ -56,7 +58,7 @@ func (s *stepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepA ...@@ -56,7 +58,7 @@ func (s *stepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepA
} }
ui.Say("Uploading VirtualBox guest additions ISO...") 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)) state.Put("error", fmt.Errorf("Error uploading guest additions: %s", err))
return multistep.ActionHalt return multistep.ActionHalt
} }
...@@ -64,4 +66,4 @@ func (s *stepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepA ...@@ -64,4 +66,4 @@ func (s *stepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepA
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *stepUploadGuestAdditions) Cleanup(state multistep.StateBag) {} func (s *StepUploadGuestAdditions) Cleanup(state multistep.StateBag) {}
...@@ -13,14 +13,6 @@ import ( ...@@ -13,14 +13,6 @@ import (
const BuilderId = "mitchellh.virtualbox" 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 { type Builder struct {
config config config config
runner multistep.Runner runner multistep.Runner
...@@ -220,9 +212,9 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { ...@@ -220,9 +212,9 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
validMode := false validMode := false
validModes := []string{ validModes := []string{
GuestAdditionsModeDisable, vboxcommon.GuestAdditionsModeDisable,
GuestAdditionsModeAttach, vboxcommon.GuestAdditionsModeAttach,
GuestAdditionsModeUpload, vboxcommon.GuestAdditionsModeUpload,
} }
for _, mode := range validModes { for _, mode := range validModes {
...@@ -269,7 +261,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -269,7 +261,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
} }
steps := []multistep.Step{ 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{ &common.StepDownload{
Checksum: b.config.ISOChecksum, Checksum: b.config.ISOChecksum,
ChecksumType: b.config.ISOChecksumType, ChecksumType: b.config.ISOChecksumType,
...@@ -289,7 +286,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -289,7 +286,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
new(stepCreateVM), new(stepCreateVM),
new(stepCreateDisk), new(stepCreateDisk),
new(stepAttachISO), new(stepAttachISO),
new(stepAttachGuestAdditions), &vboxcommon.StepAttachGuestAdditions{
GuestAdditionsMode: b.config.GuestAdditionsMode,
},
new(vboxcommon.StepAttachFloppy), new(vboxcommon.StepAttachFloppy),
&vboxcommon.StepForwardSSH{ &vboxcommon.StepForwardSSH{
GuestPort: b.config.SSHPort, GuestPort: b.config.SSHPort,
...@@ -313,7 +312,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -313,7 +312,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&vboxcommon.StepUploadVersion{ &vboxcommon.StepUploadVersion{
Path: b.config.VBoxVersionFile, Path: b.config.VBoxVersionFile,
}, },
new(stepUploadGuestAdditions), &vboxcommon.StepUploadGuestAdditions{
GuestAdditionsMode: b.config.GuestAdditionsMode,
GuestAdditionsPath: b.config.GuestAdditionsPath,
Tpl: b.config.tpl,
},
new(common.StepProvision), new(common.StepProvision),
&vboxcommon.StepShutdown{ &vboxcommon.StepShutdown{
Command: b.config.ShutdownCommand, Command: b.config.ShutdownCommand,
......
package iso package iso
import ( import (
"github.com/mitchellh/packer/builder/virtualbox/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"reflect" "reflect"
"testing" "testing"
...@@ -37,7 +38,7 @@ func TestBuilderPrepare_Defaults(t *testing.T) { ...@@ -37,7 +38,7 @@ func TestBuilderPrepare_Defaults(t *testing.T) {
t.Fatalf("should not have error: %s", err) 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) t.Errorf("bad guest additions mode: %s", b.config.GuestAdditionsMode)
} }
...@@ -111,7 +112,7 @@ func TestBuilderPrepare_GuestAdditionsMode(t *testing.T) { ...@@ -111,7 +112,7 @@ func TestBuilderPrepare_GuestAdditionsMode(t *testing.T) {
t.Fatalf("should not have error: %s", err) 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) t.Fatalf("bad: %s", b.config.GuestAdditionsMode)
} }
......
...@@ -42,6 +42,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -42,6 +42,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
state := new(multistep.BasicStateBag) state := new(multistep.BasicStateBag)
state.Put("config", b.config) state.Put("config", b.config)
state.Put("driver", driver) state.Put("driver", driver)
state.Put("cache", cache)
state.Put("hook", hook) state.Put("hook", hook)
state.Put("ui", ui) state.Put("ui", ui)
...@@ -55,14 +56,20 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -55,14 +56,20 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&common.StepCreateFloppy{ &common.StepCreateFloppy{
Files: b.config.FloppyFiles, Files: b.config.FloppyFiles,
}, },
&vboxcommon.StepDownloadGuestAdditions{
GuestAdditionsMode: b.config.GuestAdditionsMode,
GuestAdditionsURL: b.config.GuestAdditionsURL,
GuestAdditionsSHA256: b.config.GuestAdditionsSHA256,
Tpl: b.config.tpl,
},
&StepImport{ &StepImport{
Name: b.config.VMName, Name: b.config.VMName,
SourcePath: b.config.SourcePath, SourcePath: b.config.SourcePath,
ImportOpts: b.config.ImportOpts, ImportOpts: b.config.ImportOpts,
}, },
/* &vboxcommon.StepAttachGuestAdditions{
new(stepAttachGuestAdditions), GuestAdditionsMode: b.config.GuestAdditionsMode,
*/ },
new(vboxcommon.StepAttachFloppy), new(vboxcommon.StepAttachFloppy),
&vboxcommon.StepForwardSSH{ &vboxcommon.StepForwardSSH{
GuestPort: b.config.SSHPort, GuestPort: b.config.SSHPort,
...@@ -85,9 +92,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -85,9 +92,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&vboxcommon.StepUploadVersion{ &vboxcommon.StepUploadVersion{
Path: b.config.VBoxVersionFile, Path: b.config.VBoxVersionFile,
}, },
/* &vboxcommon.StepUploadGuestAdditions{
new(stepUploadGuestAdditions), GuestAdditionsMode: b.config.GuestAdditionsMode,
*/ GuestAdditionsPath: b.config.GuestAdditionsPath,
Tpl: b.config.tpl,
},
new(common.StepProvision), new(common.StepProvision),
&vboxcommon.StepShutdown{ &vboxcommon.StepShutdown{
Command: b.config.ShutdownCommand, Command: b.config.ShutdownCommand,
......
...@@ -3,6 +3,7 @@ package ovf ...@@ -3,6 +3,7 @@ package ovf
import ( import (
"fmt" "fmt"
"os" "os"
"strings"
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
"github.com/mitchellh/packer/common" "github.com/mitchellh/packer/common"
...@@ -23,9 +24,13 @@ type Config struct { ...@@ -23,9 +24,13 @@ type Config struct {
vboxcommon.VBoxManagePostConfig `mapstructure:",squash"` vboxcommon.VBoxManagePostConfig `mapstructure:",squash"`
vboxcommon.VBoxVersionConfig `mapstructure:",squash"` vboxcommon.VBoxVersionConfig `mapstructure:",squash"`
SourcePath string `mapstructure:"source_path"` SourcePath string `mapstructure:"source_path"`
VMName string `mapstructure:"vm_name"` GuestAdditionsMode string `mapstructure:"guest_additions_mode"`
ImportOpts string `mapstructure:"import_opts"` 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 tpl *packer.ConfigTemplate
} }
...@@ -44,6 +49,13 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { ...@@ -44,6 +49,13 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
c.tpl.UserVars = c.PackerUserVars c.tpl.UserVars = c.PackerUserVars
// Defaults // Defaults
if c.GuestAdditionsMode == "" {
c.GuestAdditionsMode = "upload"
}
if c.GuestAdditionsPath == "" {
c.GuestAdditionsPath = "VBoxGuestAdditions.iso"
}
if c.VMName == "" { if c.VMName == "" {
c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", c.PackerBuildName) c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", c.PackerBuildName)
} }
...@@ -62,9 +74,11 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { ...@@ -62,9 +74,11 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
errs = packer.MultiErrorAppend(errs, c.VBoxVersionConfig.Prepare(c.tpl)...) errs = packer.MultiErrorAppend(errs, c.VBoxVersionConfig.Prepare(c.tpl)...)
templates := map[string]*string{ templates := map[string]*string{
"source_path": &c.SourcePath, "guest_additions_mode": &c.GuestAdditionsMode,
"vm_name": &c.VMName, "guest_additions_sha256": &c.GuestAdditionsSHA256,
"import_opts": &c.ImportOpts, "source_path": &c.SourcePath,
"vm_name": &c.VMName,
"import_opts": &c.ImportOpts,
} }
for n, ptr := range templates { for n, ptr := range templates {
...@@ -85,6 +99,41 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { ...@@ -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 // Warnings
var warnings []string var warnings []string
if c.ShutdownCommand == "" { if c.ShutdownCommand == "" {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment