Revamped the process to verify remote cache.

This commit is contained in:
bugbuilder 2017-11-10 23:55:26 -03:00
parent 463d87adcd
commit be2afccb85
3 changed files with 21 additions and 87 deletions

View File

@ -225,8 +225,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
state.Put("hook", hook)
state.Put("ui", ui)
stepVerifyCache := &stepVerifyCache{
download: &common.StepDownload{
steps := []multistep.Step{
&vmwcommon.StepPrepareTools{
RemoteType: b.config.RemoteType,
ToolsUploadFlavor: b.config.ToolsUploadFlavor,
},
&common.StepDownload{
Checksum: b.config.ISOChecksum,
ChecksumType: b.config.ISOChecksumType,
Description: "ISO",
@ -235,18 +239,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
TargetPath: b.config.TargetPath,
Url: b.config.ISOUrls,
},
remoteUpload: &stepRemoteUpload{
Key: "iso_path",
Message: "Uploading ISO to remote machine...",
},
}
steps := []multistep.Step{
&vmwcommon.StepPrepareTools{
RemoteType: b.config.RemoteType,
ToolsUploadFlavor: b.config.ToolsUploadFlavor,
},
stepVerifyCache,
&vmwcommon.StepOutputDir{
Force: b.config.PackerForce,
},
@ -258,7 +250,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Key: "floppy_path",
Message: "Uploading Floppy to remote machine...",
},
stepVerifyCache.remoteUpload,
&stepRemoteUpload{
Key: "iso_path",
Message: "Uploading ISO to remote machine...",
},
&stepCreateDisk{},
&stepCreateVMX{},
&vmwcommon.StepConfigureVMX{

View File

@ -41,6 +41,17 @@ func (s *stepRemoteUpload) Run(state multistep.StateBag) multistep.StepAction {
checksum := config.ISOChecksum
checksumType := config.ISOChecksumType
if esx5, ok := remote.(*ESX5Driver); ok {
remotePath := esx5.cachePath(path)
if esx5.verifyChecksum(checksumType, checksum, remotePath) {
ui.Say("Remote cache was verified skipping remote upload...")
state.Put(s.Key, remotePath)
return multistep.ActionContinue
}
}
ui.Say(s.Message)
log.Printf("Remote uploading: %s", path)
newPath, err := remote.UploadISO(path, checksum, checksumType)

View File

@ -1,72 +0,0 @@
package iso
import (
"crypto/sha1"
"encoding/hex"
"fmt"
neturl "net/url"
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
"runtime"
)
type stepVerifyCache struct {
download *common.StepDownload
remoteUpload *stepRemoteUpload
}
func (s *stepVerifyCache) Run(state multistep.StateBag) multistep.StepAction {
cache := state.Get("cache").(packer.Cache)
driver := state.Get("driver").(vmwcommon.Driver)
ui := state.Get("ui").(packer.Ui)
if esx5, ok := driver.(*ESX5Driver); ok {
ui.Say("Verifying remote cache")
for _, url := range s.download.Url {
targetPath := s.download.TargetPath
if u, err := neturl.Parse(url); err == nil {
if u.Scheme == "file" {
if u.Path != "" {
targetPath = u.Path
} else if u.Opaque != "" {
targetPath = u.Opaque
}
if runtime.GOOS == "windows" && len(targetPath) > 0 && targetPath[0] == '/' {
targetPath = targetPath[1:]
}
}
}
if targetPath == "" {
hash := sha1.Sum([]byte(url))
cacheKey := fmt.Sprintf("%s.%s", hex.EncodeToString(hash[:]), s.download.Extension)
targetPath = cache.Lock(cacheKey)
cache.Unlock(cacheKey)
}
remotePath := esx5.cachePath(targetPath)
ui.Message(remotePath)
if esx5.verifyChecksum(s.download.ChecksumType, s.download.Checksum, remotePath) {
ui.Message("Remote cache verified, skipping download/upload steps")
s.remoteUpload.Skip = true
state.Put(s.download.ResultKey, remotePath)
return multistep.ActionContinue
}
ui.Message("Remote cache couldn't be verified")
}
}
return s.download.Run(state)
}
func (s *stepVerifyCache) Cleanup(multistep.StateBag) {}