From 58fd5aec61f739bd99e1618630c91cbca9d65302 Mon Sep 17 00:00:00 2001 From: Ross Smith II Date: Tue, 6 May 2014 15:37:49 -0700 Subject: [PATCH] virtualbox: use local VBoxGuestAdditions.iso by default fixes #1123 --- builder/virtualbox/common/driver.go | 3 ++ builder/virtualbox/common/driver_4_2.go | 29 +++++++++++++++ builder/virtualbox/common/driver_mock.go | 8 ++++ .../common/step_download_guest_additions.go | 37 ++++++++++++------- 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/builder/virtualbox/common/driver.go b/builder/virtualbox/common/driver.go index 8c389d419..08d86eb4a 100644 --- a/builder/virtualbox/common/driver.go +++ b/builder/virtualbox/common/driver.go @@ -25,6 +25,9 @@ type Driver interface { // Import a VM Import(string, string, string) error + // The complete path to the Guest Additions ISO + Iso() (string, error) + // Checks if the VM with the given name is running. IsRunning(string) (bool, error) diff --git a/builder/virtualbox/common/driver_4_2.go b/builder/virtualbox/common/driver_4_2.go index 49609d059..fc383c5b8 100644 --- a/builder/virtualbox/common/driver_4_2.go +++ b/builder/virtualbox/common/driver_4_2.go @@ -40,6 +40,35 @@ func (d *VBox42Driver) Delete(name string) error { return d.VBoxManage("unregistervm", name, "--delete") } +func (d *VBox42Driver) Iso() (string, error) { + var stdout bytes.Buffer + + cmd := exec.Command(d.VBoxManagePath, "list", "systemproperties") + cmd.Stdout = &stdout + if err := cmd.Run(); err != nil { + return "", err + } + + DefaultGuestAdditionsRe := regexp.MustCompile("Default Guest Additions ISO:(.*)") + + for _, line := range strings.Split(stdout.String(), "\n") { + // Need to trim off CR character when running in windows + line = strings.TrimRight(line, "\r") + + matches := DefaultGuestAdditionsRe.FindStringSubmatch(line) + if matches == nil { + continue + } + + isoname := strings.Trim(matches[1], " \r\n") + log.Printf("Found Default Guest Additions ISO: %s", isoname) + + return isoname, nil + } + + return "", fmt.Errorf("Cannot find \"Default Guest Additions ISO\" in vboxmanage output") +} + func (d *VBox42Driver) Import(name, path, opts string) error { args := []string{ "import", path, diff --git a/builder/virtualbox/common/driver_mock.go b/builder/virtualbox/common/driver_mock.go index 39d0ebb5d..1f7e18487 100644 --- a/builder/virtualbox/common/driver_mock.go +++ b/builder/virtualbox/common/driver_mock.go @@ -19,6 +19,9 @@ type DriverMock struct { ImportOpts string ImportErr error + IsoCalled bool + IsoErr error + IsRunningName string IsRunningReturn bool IsRunningErr error @@ -60,6 +63,11 @@ func (d *DriverMock) Import(name, path, opts string) error { return d.ImportErr } +func (d *DriverMock) Iso() (string, error) { + d.IsoCalled = true + return "", d.IsoErr +} + func (d *DriverMock) IsRunning(name string) (bool, error) { d.Lock() defer d.Unlock() diff --git a/builder/virtualbox/common/step_download_guest_additions.go b/builder/virtualbox/common/step_download_guest_additions.go index 9ee8a6f11..9a5a46bf2 100644 --- a/builder/virtualbox/common/step_download_guest_additions.go +++ b/builder/virtualbox/common/step_download_guest_additions.go @@ -62,14 +62,7 @@ func (s *StepDownloadGuestAdditions) Run(state multistep.StateBag) multistep.Ste // Use provided version or get it from virtualbox.org var checksum string - if s.GuestAdditionsSHA256 != "" { - checksum = s.GuestAdditionsSHA256 - } else { - checksum, action = s.downloadAdditionsSHA256(state, version, additionsName) - if action != multistep.ActionContinue { - return action - } - } + checksumType := "sha256" // Use the provided source (URL or file path) or generate it url := s.GuestAdditionsURL @@ -86,10 +79,28 @@ func (s *StepDownloadGuestAdditions) Run(state multistep.StateBag) multistep.Ste return multistep.ActionHalt } } else { - url = fmt.Sprintf( - "http://download.virtualbox.org/virtualbox/%s/%s", - version, - additionsName) + url, err = driver.Iso() + + if err == nil { + checksumType = "none" + } else { + ui.Error(err.Error()) + url = fmt.Sprintf( + "http://download.virtualbox.org/virtualbox/%s/%s", + version, + additionsName) + } + } + + if checksumType != "none" { + if s.GuestAdditionsSHA256 != "" { + checksum = s.GuestAdditionsSHA256 + } else { + checksum, action = s.downloadAdditionsSHA256(state, version, additionsName) + if action != multistep.ActionContinue { + return action + } + } } url, err = common.DownloadableURL(url) @@ -104,7 +115,7 @@ func (s *StepDownloadGuestAdditions) Run(state multistep.StateBag) multistep.Ste downStep := &common.StepDownload{ Checksum: checksum, - ChecksumType: "sha256", + ChecksumType: checksumType, Description: "Guest additions", ResultKey: "guest_additions_path", Url: []string{url},