Merge pull request #1120 from rickard-von-essen/vbox-ovf_guest_add

virtualbox-ovf support for guest_additions_mode
This commit is contained in:
Rickard von Essen 2014-05-04 22:58:04 +02:00
commit 83cf8b23a1
8 changed files with 136 additions and 61 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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 == "" {