From be2afccb85b0d7ad020ef69bb6f6b5f10f1b5a6e Mon Sep 17 00:00:00 2001 From: bugbuilder Date: Fri, 10 Nov 2017 23:55:26 -0300 Subject: [PATCH] Revamped the process to verify remote cache. --- builder/vmware/iso/builder.go | 25 ++++---- builder/vmware/iso/step_remote_upload.go | 11 ++++ builder/vmware/iso/step_verify_cache.go | 72 ------------------------ 3 files changed, 21 insertions(+), 87 deletions(-) delete mode 100644 builder/vmware/iso/step_verify_cache.go diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 73a6a1c04..5fa15317f 100644 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -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{ diff --git a/builder/vmware/iso/step_remote_upload.go b/builder/vmware/iso/step_remote_upload.go index 691ebcb84..6aa963055 100644 --- a/builder/vmware/iso/step_remote_upload.go +++ b/builder/vmware/iso/step_remote_upload.go @@ -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) diff --git a/builder/vmware/iso/step_verify_cache.go b/builder/vmware/iso/step_verify_cache.go deleted file mode 100644 index f6f7d8b0d..000000000 --- a/builder/vmware/iso/step_verify_cache.go +++ /dev/null @@ -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) {}