builder/virtualbox: switch download guest additions to use new common

This commit is contained in:
Mitchell Hashimoto 2013-08-15 14:15:32 -04:00
parent 2946d14edf
commit 4188e07fc2
2 changed files with 40 additions and 42 deletions

View File

@ -2,8 +2,6 @@ package virtualbox
import ( import (
"bytes" "bytes"
"crypto/sha256"
"encoding/hex"
"fmt" "fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/common" "github.com/mitchellh/packer/common"
@ -34,7 +32,6 @@ type stepDownloadGuestAdditions struct{}
func (s *stepDownloadGuestAdditions) Run(state map[string]interface{}) multistep.StepAction { func (s *stepDownloadGuestAdditions) Run(state map[string]interface{}) multistep.StepAction {
var action multistep.StepAction var action multistep.StepAction
cache := state["cache"].(packer.Cache)
driver := state["driver"].(Driver) driver := state["driver"].(Driver)
ui := state["ui"].(packer.Ui) ui := state["ui"].(packer.Ui)
config := state["config"].(*config) config := state["config"].(*config)
@ -65,12 +62,6 @@ func (s *stepDownloadGuestAdditions) Run(state map[string]interface{}) multistep
} }
} }
checksumBytes, err := hex.DecodeString(checksum)
if err != nil {
state["error"] = fmt.Errorf("Couldn't decode checksum into bytes: %s", checksum)
return multistep.ActionHalt
}
// 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 := config.GuestAdditionsURL
if url != "" { if url != "" {
@ -102,21 +93,15 @@ func (s *stepDownloadGuestAdditions) Run(state map[string]interface{}) multistep
log.Printf("Guest additions URL: %s", url) log.Printf("Guest additions URL: %s", url)
log.Printf("Acquiring lock to download the guest additions ISO.") downStep := &common.StepDownload{
cachePath := cache.Lock(url) Checksum: checksum,
defer cache.Unlock(url) ChecksumType: "sha256",
Description: "Guest additions",
downloadConfig := &common.DownloadConfig{ ResultKey: "guest_additions_path",
Url: url, Url: []string{url},
TargetPath: cachePath,
Hash: sha256.New(),
Checksum: checksumBytes,
} }
download := common.NewDownloadClient(downloadConfig) return downStep.Run(state)
ui.Say("Downloading VirtualBox guest additions. Progress will be shown periodically.")
state["guest_additions_path"], action = s.progressDownload(download, state)
return action
} }
func (s *stepDownloadGuestAdditions) Cleanup(state map[string]interface{}) {} func (s *stepDownloadGuestAdditions) Cleanup(state map[string]interface{}) {}
@ -165,7 +150,9 @@ DownloadWaitLoop:
func (s *stepDownloadGuestAdditions) downloadAdditionsSHA256(state map[string]interface{}, additionsVersion string, additionsName string) (string, multistep.StepAction) { func (s *stepDownloadGuestAdditions) downloadAdditionsSHA256(state map[string]interface{}, 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("http://download.virtualbox.org/virtualbox/%s/SHA256SUMS", additionsVersion) checksumsUrl := fmt.Sprintf(
"http://download.virtualbox.org/virtualbox/%s/SHA256SUMS",
additionsVersion)
checksumsFile, err := ioutil.TempFile("", "packer") checksumsFile, err := ioutil.TempFile("", "packer")
if err != nil { if err != nil {
@ -175,25 +162,23 @@ func (s *stepDownloadGuestAdditions) downloadAdditionsSHA256(state map[string]in
return "", multistep.ActionHalt return "", multistep.ActionHalt
} }
defer os.Remove(checksumsFile.Name()) defer os.Remove(checksumsFile.Name())
checksumsFile.Close() checksumsFile.Close()
downloadConfig := &common.DownloadConfig{ downStep := &common.StepDownload{
Url: checksumsUrl, Description: "Guest additions checksums",
TargetPath: checksumsFile.Name(), ResultKey: "guest_additions_checksums_path",
Hash: nil, TargetPath: checksumsFile.Name(),
Url: []string{checksumsUrl},
} }
log.Printf("Downloading guest addition checksums: %s", checksumsUrl) action := downStep.Run(state)
download := common.NewDownloadClient(downloadConfig) if action == multistep.ActionHalt {
checksumsPath, action := s.progressDownload(download, state)
if action != multistep.ActionContinue {
return "", action return "", action
} }
// Next, we find the checksum for the file we're looking to download. // Next, we find the checksum for the file we're looking to download.
// It is an error if the checksum cannot be found. // It is an error if the checksum cannot be found.
checksumsF, err := os.Open(checksumsPath) checksumsF, err := os.Open(state["guest_additions_checksums_path"].(string))
if err != nil { if err != nil {
state["error"] = fmt.Errorf("Error opening guest addition checksums: %s", err) state["error"] = fmt.Errorf("Error opening guest addition checksums: %s", err)
return "", multistep.ActionHalt return "", multistep.ActionHalt
@ -223,7 +208,8 @@ func (s *stepDownloadGuestAdditions) downloadAdditionsSHA256(state map[string]in
} }
if checksum == "" { if checksum == "" {
state["error"] = fmt.Errorf("The checksum for the file '%s' could not be found.", additionsName) state["error"] = fmt.Errorf(
"The checksum for the file '%s' could not be found.", additionsName)
return "", multistep.ActionHalt return "", multistep.ActionHalt
} }

View File

@ -29,6 +29,10 @@ type StepDownload struct {
// into the state. // into the state.
ResultKey string ResultKey string
// The path where the result should go, otherwise it goes to the
// cache directory.
TargetPath string
// A list of URLs to attempt to download this thing. // A list of URLs to attempt to download this thing.
Url []string Url []string
} }
@ -37,10 +41,14 @@ func (s *StepDownload) Run(state map[string]interface{}) multistep.StepAction {
cache := state["cache"].(packer.Cache) cache := state["cache"].(packer.Cache)
ui := state["ui"].(packer.Ui) ui := state["ui"].(packer.Ui)
checksum, err := hex.DecodeString(s.Checksum) var checksum []byte
if err != nil { if s.Checksum != "" {
state["error"] = fmt.Errorf("Error parsing checksum: %s", err) var err error
return multistep.ActionHalt checksum, err = hex.DecodeString(s.Checksum)
if err != nil {
state["error"] = fmt.Errorf("Error parsing checksum: %s", err)
return multistep.ActionHalt
}
} }
ui.Say(fmt.Sprintf("Downloading or copying %s", s.Description)) ui.Say(fmt.Sprintf("Downloading or copying %s", s.Description))
@ -48,13 +56,17 @@ func (s *StepDownload) Run(state map[string]interface{}) multistep.StepAction {
var finalPath string var finalPath string
for _, url := range s.Url { for _, url := range s.Url {
ui.Message(fmt.Sprintf("Downloading or copying: %s", url)) ui.Message(fmt.Sprintf("Downloading or copying: %s", url))
log.Printf("Acquiring lock to download: %s", url)
cachePath := cache.Lock(url) targetPath := s.TargetPath
defer cache.Unlock(url) if targetPath == "" {
log.Printf("Acquiring lock to download: %s", url)
targetPath = cache.Lock(url)
defer cache.Unlock(url)
}
config := &DownloadConfig{ config := &DownloadConfig{
Url: url, Url: url,
TargetPath: cachePath, TargetPath: targetPath,
CopyFile: false, CopyFile: false,
Hash: HashForType(s.ChecksumType), Hash: HashForType(s.ChecksumType),
Checksum: checksum, Checksum: checksum,