Commit 921770b6 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/virtualbox: Download guest additions for the VM

parent e91421b1
...@@ -25,27 +25,28 @@ type Builder struct { ...@@ -25,27 +25,28 @@ type Builder struct {
} }
type config struct { type config struct {
BootCommand []string `mapstructure:"boot_command"` BootCommand []string `mapstructure:"boot_command"`
BootWait time.Duration `` BootWait time.Duration ``
DiskSize uint `mapstructure:"disk_size"` DiskSize uint `mapstructure:"disk_size"`
GuestOSType string `mapstructure:"guest_os_type"` GuestAdditionsPath string `mapstructure:"guest_additions_path"`
HTTPDir string `mapstructure:"http_directory"` GuestOSType string `mapstructure:"guest_os_type"`
HTTPPortMin uint `mapstructure:"http_port_min"` HTTPDir string `mapstructure:"http_directory"`
HTTPPortMax uint `mapstructure:"http_port_max"` HTTPPortMin uint `mapstructure:"http_port_min"`
ISOMD5 string `mapstructure:"iso_md5"` HTTPPortMax uint `mapstructure:"http_port_max"`
ISOUrl string `mapstructure:"iso_url"` ISOMD5 string `mapstructure:"iso_md5"`
OutputDir string `mapstructure:"output_directory"` ISOUrl string `mapstructure:"iso_url"`
ShutdownCommand string `mapstructure:"shutdown_command"` OutputDir string `mapstructure:"output_directory"`
ShutdownTimeout time.Duration `` ShutdownCommand string `mapstructure:"shutdown_command"`
SSHHostPortMin uint `mapstructure:"ssh_host_port_min"` ShutdownTimeout time.Duration ``
SSHHostPortMax uint `mapstructure:"ssh_host_port_max"` SSHHostPortMin uint `mapstructure:"ssh_host_port_min"`
SSHPassword string `mapstructure:"ssh_password"` SSHHostPortMax uint `mapstructure:"ssh_host_port_max"`
SSHPort uint `mapstructure:"ssh_port"` SSHPassword string `mapstructure:"ssh_password"`
SSHUser string `mapstructure:"ssh_username"` SSHPort uint `mapstructure:"ssh_port"`
SSHWaitTimeout time.Duration `` SSHUser string `mapstructure:"ssh_username"`
VBoxVersionFile string `mapstructure:"virtualbox_version_file"` SSHWaitTimeout time.Duration ``
VBoxManage [][]string `mapstructure:"vboxmanage"` VBoxVersionFile string `mapstructure:"virtualbox_version_file"`
VMName string `mapstructure:"vm_name"` VBoxManage [][]string `mapstructure:"vboxmanage"`
VMName string `mapstructure:"vm_name"`
PackerDebug bool `mapstructure:"packer_debug"` PackerDebug bool `mapstructure:"packer_debug"`
...@@ -68,6 +69,10 @@ func (b *Builder) Prepare(raws ...interface{}) error { ...@@ -68,6 +69,10 @@ func (b *Builder) Prepare(raws ...interface{}) error {
b.config.DiskSize = 40000 b.config.DiskSize = 40000
} }
if b.config.GuestAdditionsPath == "" {
b.config.GuestAdditionsPath = "VBoxGuestAdditions.iso"
}
if b.config.GuestOSType == "" { if b.config.GuestOSType == "" {
b.config.GuestOSType = "Other" b.config.GuestOSType = "Other"
} }
...@@ -206,6 +211,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { ...@@ -206,6 +211,7 @@ func (b *Builder) Prepare(raws ...interface{}) error {
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
steps := []multistep.Step{ steps := []multistep.Step{
new(stepDownloadGuestAdditions),
new(stepDownloadISO), new(stepDownloadISO),
new(stepPrepareOutputDir), new(stepPrepareOutputDir),
new(stepHTTPServer), new(stepHTTPServer),
......
...@@ -102,6 +102,32 @@ func TestBuilderPrepare_DiskSize(t *testing.T) { ...@@ -102,6 +102,32 @@ func TestBuilderPrepare_DiskSize(t *testing.T) {
} }
} }
func TestBuilderPrepare_GuestAdditionsPath(t *testing.T) {
var b Builder
config := testConfig()
delete(config, "disk_size")
err := b.Prepare(config)
if err != nil {
t.Fatalf("bad err: %s", err)
}
if b.config.GuestAdditionsPath != "VBoxGuestAdditions.iso" {
t.Fatalf("bad: %s", b.config.GuestAdditionsPath)
}
config["guest_additions_path"] = "foo"
b = Builder{}
err = b.Prepare(config)
if err != nil {
t.Fatalf("should not have error: %s", err)
}
if b.config.GuestAdditionsPath != "foo" {
t.Fatalf("bad size: %s", b.config.GuestAdditionsPath)
}
}
func TestBuilderPrepare_HTTPPort(t *testing.T) { func TestBuilderPrepare_HTTPPort(t *testing.T) {
var b Builder var b Builder
config := testConfig() config := testConfig()
......
package virtualbox
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/builder/common"
"github.com/mitchellh/packer/packer"
"log"
"time"
)
// This step uploads a file containing the VirtualBox version, which
// can be useful for various provisioning reasons.
//
// Produces:
// guest_additions_path string - Path to the guest additions.
type stepDownloadGuestAdditions struct{}
func (s *stepDownloadGuestAdditions) Run(state map[string]interface{}) multistep.StepAction {
cache := state["cache"].(packer.Cache)
driver := state["driver"].(Driver)
ui := state["ui"].(packer.Ui)
version, err := driver.Version()
if err != nil {
state["error"] = fmt.Errorf("Error reading version for guest additions download: %s", err)
return multistep.ActionHalt
}
url := fmt.Sprintf(
"http://download.virtualbox.org/virtualbox/%s/VBoxGuestAdditions_%s.iso",
version, version)
log.Printf("Guest additions URL: %s", url)
log.Printf("Acquiring lock to download the guest additions ISO.")
cachePath := cache.Lock(url)
defer cache.Unlock(url)
downloadConfig := &common.DownloadConfig{
Url: url,
TargetPath: cachePath,
Hash: nil,
}
download := common.NewDownloadClient(downloadConfig)
downloadCompleteCh := make(chan error, 1)
go func() {
ui.Say("Downloading VirtualBox guest additions. Progress will be shown periodically.")
cachePath, err = download.Get()
downloadCompleteCh <- err
}()
progressTicker := time.NewTicker(5 * time.Second)
defer progressTicker.Stop()
DownloadWaitLoop:
for {
select {
case err := <-downloadCompleteCh:
if err != nil {
state["error"] = fmt.Errorf("Error downloading guest additions: %s", err)
return multistep.ActionHalt
}
break DownloadWaitLoop
case <-progressTicker.C:
ui.Message(fmt.Sprintf("Download progress: %d%%", download.PercentProgress()))
case <-time.After(1 * time.Second):
if _, ok := state[multistep.StateCancelled]; ok {
ui.Say("Interrupt received. Cancelling download...")
return multistep.ActionHalt
}
}
}
state["guest_additions_path"] = cachePath
return multistep.ActionContinue
}
func (s *stepDownloadGuestAdditions) Cleanup(state map[string]interface{}) {}
...@@ -78,7 +78,7 @@ DownloadWaitLoop: ...@@ -78,7 +78,7 @@ DownloadWaitLoop:
break DownloadWaitLoop break DownloadWaitLoop
case <-progressTicker.C: case <-progressTicker.C:
ui.Say(fmt.Sprintf("Download progress: %d%%", download.PercentProgress())) ui.Message(fmt.Sprintf("Download progress: %d%%", download.PercentProgress()))
case <-time.After(1 * time.Second): case <-time.After(1 * time.Second):
if _, ok := state[multistep.StateCancelled]; ok { if _, ok := state[multistep.StateCancelled]; ok {
ui.Say("Interrupt received. Cancelling download...") ui.Say("Interrupt received. Cancelling download...")
......
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