2013-08-15 14:05:36 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-08 23:41:39 -04:00
|
|
|
"crypto/sha1"
|
2013-08-15 14:05:36 -04:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
2014-10-27 23:51:34 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-04-04 22:24:21 -04:00
|
|
|
"github.com/hashicorp/packer/helper/useragent"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-08-15 14:05:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepDownload downloads a remote file using the download client within
|
|
|
|
// this package. This step handles setting up the download configuration,
|
|
|
|
// progress reporting, interrupt handling, etc.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// cache packer.Cache
|
|
|
|
// ui packer.Ui
|
|
|
|
type StepDownload struct {
|
|
|
|
// The checksum and the type of the checksum for the download
|
|
|
|
Checksum string
|
|
|
|
ChecksumType string
|
|
|
|
|
|
|
|
// A short description of the type of download being done. Example:
|
|
|
|
// "ISO" or "Guest Additions"
|
|
|
|
Description string
|
|
|
|
|
|
|
|
// The name of the key where the final path of the ISO will be put
|
|
|
|
// into the state.
|
|
|
|
ResultKey string
|
|
|
|
|
2013-08-15 14:15:32 -04:00
|
|
|
// The path where the result should go, otherwise it goes to the
|
|
|
|
// cache directory.
|
|
|
|
TargetPath string
|
|
|
|
|
2013-08-15 14:05:36 -04:00
|
|
|
// A list of URLs to attempt to download this thing.
|
|
|
|
Url []string
|
2015-06-08 23:41:39 -04:00
|
|
|
|
|
|
|
// Extension is the extension to force for the file that is downloaded.
|
|
|
|
// Some systems require a certain extension. If this isn't set, the
|
|
|
|
// extension on the URL is used. Otherwise, this will be forced
|
|
|
|
// on the downloaded file for every URL.
|
|
|
|
Extension string
|
2013-08-15 14:05:36 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepDownload) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:17:59 -04:00
|
|
|
cache := state.Get("cache").(packer.Cache)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-08-15 14:05:36 -04:00
|
|
|
|
2013-08-15 14:15:32 -04:00
|
|
|
var checksum []byte
|
|
|
|
if s.Checksum != "" {
|
|
|
|
var err error
|
|
|
|
checksum, err = hex.DecodeString(s.Checksum)
|
|
|
|
if err != nil {
|
2013-08-31 15:17:59 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error parsing checksum: %s", err))
|
2013-08-15 14:15:32 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-08-15 14:05:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Downloading or copying %s", s.Description))
|
|
|
|
|
2017-11-25 16:02:54 -05:00
|
|
|
// First try to use any already downloaded file
|
2018-03-13 22:19:10 -04:00
|
|
|
// If it fails, proceed to regular download logic
|
2013-08-15 14:15:32 -04:00
|
|
|
|
2017-11-25 16:02:54 -05:00
|
|
|
var downloadConfigs = make([]*DownloadConfig, len(s.Url))
|
|
|
|
var finalPath string
|
|
|
|
for i, url := range s.Url {
|
2013-08-15 14:15:32 -04:00
|
|
|
targetPath := s.TargetPath
|
|
|
|
if targetPath == "" {
|
2015-06-08 23:41:39 -04:00
|
|
|
// Determine a cache key. This is normally just the URL but
|
|
|
|
// if we force a certain extension we hash the URL and add
|
|
|
|
// the extension to force it.
|
|
|
|
cacheKey := url
|
|
|
|
if s.Extension != "" {
|
|
|
|
hash := sha1.Sum([]byte(url))
|
|
|
|
cacheKey = fmt.Sprintf(
|
|
|
|
"%s.%s", hex.EncodeToString(hash[:]), s.Extension)
|
|
|
|
}
|
|
|
|
|
2013-08-15 14:15:32 -04:00
|
|
|
log.Printf("Acquiring lock to download: %s", url)
|
2015-06-08 23:41:39 -04:00
|
|
|
targetPath = cache.Lock(cacheKey)
|
|
|
|
defer cache.Unlock(cacheKey)
|
2013-08-15 14:15:32 -04:00
|
|
|
}
|
2013-08-15 14:05:36 -04:00
|
|
|
|
|
|
|
config := &DownloadConfig{
|
|
|
|
Url: url,
|
2013-08-15 14:15:32 -04:00
|
|
|
TargetPath: targetPath,
|
2013-08-15 14:05:36 -04:00
|
|
|
CopyFile: false,
|
|
|
|
Hash: HashForType(s.ChecksumType),
|
|
|
|
Checksum: checksum,
|
2018-04-04 22:24:21 -04:00
|
|
|
UserAgent: useragent.String(),
|
2013-08-15 14:05:36 -04:00
|
|
|
}
|
2017-11-25 16:02:54 -05:00
|
|
|
downloadConfigs[i] = config
|
2013-08-15 14:05:36 -04:00
|
|
|
|
2017-11-25 16:02:54 -05:00
|
|
|
if match, _ := NewDownloadClient(config).VerifyChecksum(config.TargetPath); match {
|
|
|
|
ui.Message(fmt.Sprintf("Found already downloaded, initial checksum matched, no download needed: %s", url))
|
|
|
|
finalPath = config.TargetPath
|
|
|
|
break
|
2013-08-15 14:05:36 -04:00
|
|
|
}
|
2017-11-25 16:02:54 -05:00
|
|
|
}
|
2013-08-15 14:05:36 -04:00
|
|
|
|
2017-11-25 16:02:54 -05:00
|
|
|
if finalPath == "" {
|
|
|
|
for i, url := range s.Url {
|
|
|
|
ui.Message(fmt.Sprintf("Downloading or copying: %s", url))
|
2013-08-15 14:05:36 -04:00
|
|
|
|
2017-11-25 16:02:54 -05:00
|
|
|
config := downloadConfigs[i]
|
|
|
|
|
|
|
|
path, err, retry := s.download(config, state)
|
|
|
|
if err != nil {
|
|
|
|
ui.Message(fmt.Sprintf("Error downloading: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !retry {
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
finalPath = path
|
|
|
|
break
|
|
|
|
}
|
2013-08-15 14:05:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if finalPath == "" {
|
|
|
|
err := fmt.Errorf("%s download failed.", s.Description)
|
2013-08-31 15:17:59 -04:00
|
|
|
state.Put("error", err)
|
2013-08-15 14:05:36 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:17:59 -04:00
|
|
|
state.Put(s.ResultKey, finalPath)
|
2013-08-15 14:05:36 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:17:59 -04:00
|
|
|
func (s *StepDownload) Cleanup(multistep.StateBag) {}
|
2013-08-15 14:05:36 -04:00
|
|
|
|
2013-08-31 15:17:59 -04:00
|
|
|
func (s *StepDownload) download(config *DownloadConfig, state multistep.StateBag) (string, error, bool) {
|
2013-08-15 14:05:36 -04:00
|
|
|
var path string
|
2013-08-31 15:17:59 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-08-15 14:05:36 -04:00
|
|
|
download := NewDownloadClient(config)
|
|
|
|
|
|
|
|
downloadCompleteCh := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
var err error
|
|
|
|
path, err = download.Get()
|
|
|
|
downloadCompleteCh <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
progressTicker := time.NewTicker(5 * time.Second)
|
|
|
|
defer progressTicker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-downloadCompleteCh:
|
|
|
|
if err != nil {
|
|
|
|
return "", err, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return path, nil, true
|
|
|
|
case <-progressTicker.C:
|
|
|
|
progress := download.PercentProgress()
|
|
|
|
if progress >= 0 {
|
|
|
|
ui.Message(fmt.Sprintf("Download progress: %d%%", progress))
|
|
|
|
}
|
|
|
|
case <-time.After(1 * time.Second):
|
2013-08-31 15:17:59 -04:00
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
2013-08-15 14:05:36 -04:00
|
|
|
ui.Say("Interrupt received. Cancelling download...")
|
|
|
|
return "", nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|