2013-06-10 00:50:15 -04:00
|
|
|
package vmware
|
|
|
|
|
|
|
|
import (
|
2013-06-10 02:00:07 -04:00
|
|
|
"crypto/md5"
|
2013-06-10 02:12:37 -04:00
|
|
|
"encoding/hex"
|
2013-06-10 01:44:35 -04:00
|
|
|
"fmt"
|
2013-06-10 00:50:15 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-06-12 20:41:44 -04:00
|
|
|
"github.com/mitchellh/packer/builder/common"
|
2013-06-10 01:44:35 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-10 00:50:15 -04:00
|
|
|
"log"
|
2013-06-10 01:44:35 -04:00
|
|
|
"time"
|
2013-06-10 00:50:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step downloads the ISO specified.
|
|
|
|
//
|
|
|
|
// Uses:
|
2013-06-10 01:44:35 -04:00
|
|
|
// cache packer.Cache
|
2013-06-10 00:50:15 -04:00
|
|
|
// config *config
|
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// iso_path string
|
|
|
|
type stepDownloadISO struct{}
|
|
|
|
|
2013-06-10 01:44:35 -04:00
|
|
|
func (s stepDownloadISO) Run(state map[string]interface{}) multistep.StepAction {
|
|
|
|
cache := state["cache"].(packer.Cache)
|
2013-06-10 00:50:15 -04:00
|
|
|
config := state["config"].(*config)
|
2013-06-10 01:44:35 -04:00
|
|
|
ui := state["ui"].(packer.Ui)
|
2013-06-10 00:50:15 -04:00
|
|
|
|
2013-06-12 20:41:44 -04:00
|
|
|
checksum, err := hex.DecodeString(config.ISOMD5)
|
|
|
|
if err != nil {
|
2013-06-20 00:20:48 -04:00
|
|
|
err := fmt.Errorf("Error parsing checksum: %s", err)
|
|
|
|
state["error"] = err
|
|
|
|
ui.Error(err.Error())
|
2013-06-12 20:41:44 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-06-10 00:50:15 -04:00
|
|
|
log.Printf("Acquiring lock to download the ISO.")
|
2013-06-10 01:44:35 -04:00
|
|
|
cachePath := cache.Lock(config.ISOUrl)
|
|
|
|
defer cache.Unlock(config.ISOUrl)
|
2013-06-10 00:50:15 -04:00
|
|
|
|
2013-06-12 20:41:44 -04:00
|
|
|
downloadConfig := &common.DownloadConfig{
|
|
|
|
Url: config.ISOUrl,
|
|
|
|
TargetPath: cachePath,
|
|
|
|
CopyFile: false,
|
|
|
|
Hash: md5.New(),
|
|
|
|
Checksum: checksum,
|
2013-06-10 01:44:35 -04:00
|
|
|
}
|
|
|
|
|
2013-06-12 20:41:44 -04:00
|
|
|
download := common.NewDownloadClient(downloadConfig)
|
2013-06-10 01:44:35 -04:00
|
|
|
|
2013-06-12 20:41:44 -04:00
|
|
|
downloadCompleteCh := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
ui.Say("Copying or downloading ISO. Progress will be reported periodically.")
|
|
|
|
cachePath, err = download.Get()
|
|
|
|
downloadCompleteCh <- err
|
|
|
|
}()
|
2013-06-10 02:00:07 -04:00
|
|
|
|
2013-06-12 20:41:44 -04:00
|
|
|
progressTicker := time.NewTicker(5 * time.Second)
|
|
|
|
defer progressTicker.Stop()
|
2013-06-10 02:00:07 -04:00
|
|
|
|
2013-06-12 20:41:44 -04:00
|
|
|
DownloadWaitLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-downloadCompleteCh:
|
|
|
|
if err != nil {
|
2013-06-20 00:20:48 -04:00
|
|
|
err := fmt.Errorf("Error downloading ISO: %s", err)
|
|
|
|
state["error"] = err
|
|
|
|
ui.Error(err.Error())
|
2013-06-23 21:23:00 -04:00
|
|
|
return multistep.ActionHalt
|
2013-06-10 01:44:35 -04:00
|
|
|
}
|
2013-06-10 02:00:07 -04:00
|
|
|
|
2013-06-12 20:41:44 -04:00
|
|
|
break DownloadWaitLoop
|
|
|
|
case <-progressTicker.C:
|
|
|
|
ui.Say(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
|
|
|
|
}
|
2013-06-10 02:00:07 -04:00
|
|
|
}
|
2013-06-10 01:44:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Path to ISO on disk: %s", cachePath)
|
|
|
|
state["iso_path"] = cachePath
|
2013-06-10 00:50:15 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stepDownloadISO) Cleanup(map[string]interface{}) {}
|