2013-06-24 02:05:32 -04:00
|
|
|
package virtualbox
|
|
|
|
|
|
|
|
import (
|
2013-06-24 02:44:03 -04:00
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2013-06-24 02:05:32 -04:00
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/builder/common"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-24 02:44:03 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2013-06-24 02:05:32 -04:00
|
|
|
"log"
|
2013-06-24 02:44:03 -04:00
|
|
|
"os"
|
|
|
|
"strings"
|
2013-06-24 02:05:32 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-06-25 15:31:06 -04:00
|
|
|
var additionsVersionMap = map[string]string{
|
|
|
|
"4.2.1": "4.2.0",
|
2013-06-24 12:24:16 -04:00
|
|
|
"4.1.23": "4.1.22",
|
|
|
|
}
|
|
|
|
|
2013-06-24 02:05:32 -04:00
|
|
|
// 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 {
|
2013-06-24 02:44:03 -04:00
|
|
|
var action multistep.StepAction
|
2013-06-24 02:05:32 -04:00
|
|
|
cache := state["cache"].(packer.Cache)
|
|
|
|
driver := state["driver"].(Driver)
|
|
|
|
ui := state["ui"].(packer.Ui)
|
2013-07-06 05:28:56 -04:00
|
|
|
config := state["config"].(*config)
|
2013-06-24 02:05:32 -04:00
|
|
|
|
2013-07-07 12:14:41 -04:00
|
|
|
// Get VBox version
|
2013-06-24 02:05:32 -04:00
|
|
|
version, err := driver.Version()
|
|
|
|
if err != nil {
|
|
|
|
state["error"] = fmt.Errorf("Error reading version for guest additions download: %s", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-06-24 12:24:16 -04:00
|
|
|
if newVersion, ok := additionsVersionMap[version]; ok {
|
|
|
|
log.Printf("Rewriting guest additions version: %s to %s", version, newVersion)
|
|
|
|
version = newVersion
|
|
|
|
}
|
|
|
|
|
2013-06-24 02:44:03 -04:00
|
|
|
additionsName := fmt.Sprintf("VBoxGuestAdditions_%s.iso", version)
|
|
|
|
|
2013-07-07 12:14:41 -04:00
|
|
|
// Use provided version or get it from virtualbox.org
|
|
|
|
var checksum string
|
2013-06-24 02:44:03 -04:00
|
|
|
|
2013-07-07 12:14:41 -04:00
|
|
|
if config.GuestAdditionsSHA256 != "" {
|
|
|
|
checksum = config.GuestAdditionsSHA256
|
|
|
|
} else {
|
|
|
|
checksum, action = s.downloadAdditionsSHA256(state, version, additionsName)
|
|
|
|
if action != multistep.ActionContinue {
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 02:44:03 -04:00
|
|
|
|
|
|
|
checksumBytes, err := hex.DecodeString(checksum)
|
|
|
|
if err != nil {
|
|
|
|
state["error"] = fmt.Errorf("Couldn't decode checksum into bytes: %s", checksum)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-07-07 12:14:41 -04:00
|
|
|
// Use the provided source (URL or file path) or generate it
|
|
|
|
url := config.GuestAdditionsURL
|
|
|
|
if url == "" {
|
|
|
|
url = fmt.Sprintf(
|
|
|
|
"http://download.virtualbox.org/virtualbox/%s/%s",
|
|
|
|
version,
|
|
|
|
additionsName)
|
|
|
|
}
|
2013-07-06 05:28:56 -04:00
|
|
|
|
2013-06-24 02:05:32 -04:00
|
|
|
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)
|
|
|
|
|
2013-07-06 05:28:56 -04:00
|
|
|
downloadConfig := &common.DownloadConfig{
|
2013-06-24 02:05:32 -04:00
|
|
|
Url: url,
|
|
|
|
TargetPath: cachePath,
|
2013-06-24 02:44:03 -04:00
|
|
|
Hash: sha256.New(),
|
|
|
|
Checksum: checksumBytes,
|
2013-06-24 02:05:32 -04:00
|
|
|
}
|
|
|
|
|
2013-07-06 05:28:56 -04:00
|
|
|
download := common.NewDownloadClient(downloadConfig)
|
2013-06-24 02:44:03 -04:00
|
|
|
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) progressDownload(c *common.DownloadClient, state map[string]interface{}) (string, multistep.StepAction) {
|
|
|
|
ui := state["ui"].(packer.Ui)
|
2013-06-24 02:05:32 -04:00
|
|
|
|
2013-06-24 02:44:03 -04:00
|
|
|
var result string
|
2013-06-24 02:05:32 -04:00
|
|
|
downloadCompleteCh := make(chan error, 1)
|
2013-06-24 02:44:03 -04:00
|
|
|
|
|
|
|
// Start a goroutine to actually do the download...
|
2013-06-24 02:05:32 -04:00
|
|
|
go func() {
|
2013-06-24 02:44:03 -04:00
|
|
|
var err error
|
|
|
|
result, err = c.Get()
|
2013-06-24 02:05:32 -04:00
|
|
|
downloadCompleteCh <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
progressTicker := time.NewTicker(5 * time.Second)
|
|
|
|
defer progressTicker.Stop()
|
|
|
|
|
2013-06-24 02:44:03 -04:00
|
|
|
// A loop that handles showing progress as well as timing out and handling
|
|
|
|
// interrupts and all that.
|
2013-06-24 02:05:32 -04:00
|
|
|
DownloadWaitLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-downloadCompleteCh:
|
|
|
|
if err != nil {
|
2013-06-24 02:44:03 -04:00
|
|
|
state["error"] = fmt.Errorf("Error downloading: %s", err)
|
|
|
|
return "", multistep.ActionHalt
|
2013-06-24 02:05:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break DownloadWaitLoop
|
|
|
|
case <-progressTicker.C:
|
2013-06-24 02:44:03 -04:00
|
|
|
ui.Message(fmt.Sprintf("Download progress: %d%%", c.PercentProgress()))
|
2013-06-24 02:05:32 -04:00
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
if _, ok := state[multistep.StateCancelled]; ok {
|
|
|
|
ui.Say("Interrupt received. Cancelling download...")
|
2013-06-24 02:44:03 -04:00
|
|
|
return "", multistep.ActionHalt
|
2013-06-24 02:05:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-24 02:44:03 -04:00
|
|
|
return result, multistep.ActionContinue
|
2013-06-24 02:05:32 -04:00
|
|
|
}
|
2013-07-06 05:28:56 -04:00
|
|
|
|
2013-07-07 12:14:41 -04:00
|
|
|
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
|
2013-07-06 05:28:56 -04:00
|
|
|
// for this version.
|
|
|
|
checksumsUrl := fmt.Sprintf("http://download.virtualbox.org/virtualbox/%s/SHA256SUMS", additionsVersion)
|
2013-07-07 12:14:41 -04:00
|
|
|
|
2013-07-07 12:17:27 -04:00
|
|
|
checksumsFile, err := ioutil.TempFile("", "packer")
|
2013-07-06 05:28:56 -04:00
|
|
|
if err != nil {
|
|
|
|
state["error"] = fmt.Errorf(
|
|
|
|
"Failed creating temporary file to store guest addition checksums: %s",
|
|
|
|
err)
|
|
|
|
return "", multistep.ActionHalt
|
|
|
|
}
|
2013-07-07 12:17:27 -04:00
|
|
|
defer os.Remove(checksumsFile.Name())
|
2013-07-06 05:28:56 -04:00
|
|
|
|
|
|
|
checksumsFile.Close()
|
|
|
|
|
|
|
|
downloadConfig := &common.DownloadConfig{
|
|
|
|
Url: checksumsUrl,
|
|
|
|
TargetPath: checksumsFile.Name(),
|
|
|
|
Hash: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Downloading guest addition checksums: %s", checksumsUrl)
|
|
|
|
download := common.NewDownloadClient(downloadConfig)
|
|
|
|
checksumsPath, action := s.progressDownload(download, state)
|
|
|
|
if action != multistep.ActionContinue {
|
|
|
|
return "", action
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we find the checksum for the file we're looking to download.
|
|
|
|
// It is an error if the checksum cannot be found.
|
|
|
|
checksumsF, err := os.Open(checksumsPath)
|
|
|
|
if err != nil {
|
|
|
|
state["error"] = fmt.Errorf("Error opening guest addition checksums: %s", err)
|
|
|
|
return "", multistep.ActionHalt
|
|
|
|
}
|
|
|
|
defer checksumsF.Close()
|
|
|
|
|
|
|
|
// We copy the contents of the file into memory. In general this file
|
|
|
|
// is quite small so that is okay. In the future, we probably want to
|
|
|
|
// use bufio and iterate line by line.
|
|
|
|
var contents bytes.Buffer
|
|
|
|
io.Copy(&contents, checksumsF)
|
|
|
|
|
|
|
|
checksum := ""
|
|
|
|
for _, line := range strings.Split(contents.String(), "\n") {
|
|
|
|
parts := strings.Fields(line)
|
|
|
|
log.Printf("Checksum file parts: %#v", parts)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
// Bogus line
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(parts[1], additionsName) {
|
|
|
|
checksum = parts[0]
|
|
|
|
log.Printf("Guest additions checksum: %s", checksum)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if checksum == "" {
|
|
|
|
state["error"] = fmt.Errorf("The checksum for the file '%s' could not be found.", additionsName)
|
|
|
|
return "", multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-07-07 12:14:41 -04:00
|
|
|
return checksum, multistep.ActionContinue
|
2013-07-06 05:28:56 -04:00
|
|
|
|
|
|
|
}
|