From 4023b618b4ad6c60fbb9ba1f55464ba47768db55 Mon Sep 17 00:00:00 2001 From: bugbuilder Date: Sun, 23 Jul 2017 01:31:46 -0400 Subject: [PATCH] Verify remote cache for ESXi --- builder/vmware/iso/builder.go | 19 ++++--- builder/vmware/iso/step_download.go | 72 ++++++++++++++++++++++++ builder/vmware/iso/step_remote_upload.go | 26 +++++---- 3 files changed, 98 insertions(+), 19 deletions(-) create mode 100644 builder/vmware/iso/step_download.go diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 42038b96a..5dba8de4e 100644 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -220,15 +220,16 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe RemoteType: b.config.RemoteType, ToolsUploadFlavor: b.config.ToolsUploadFlavor, }, - &common.StepDownload{ - Checksum: b.config.ISOChecksum, - ChecksumType: b.config.ISOChecksumType, - Description: "ISO", - Extension: b.config.TargetExtension, - ResultKey: "iso_path", - TargetPath: b.config.TargetPath, - Url: b.config.ISOUrls, - }, + &stepDownload{ + step: &common.StepDownload{ + Checksum: b.config.ISOChecksum, + ChecksumType: b.config.ISOChecksumType, + Description: "ISO", + Extension: b.config.TargetExtension, + ResultKey: "iso_path", + TargetPath: b.config.TargetPath, + Url: b.config.ISOUrls, + }}, &vmwcommon.StepOutputDir{ Force: b.config.PackerForce, }, diff --git a/builder/vmware/iso/step_download.go b/builder/vmware/iso/step_download.go new file mode 100644 index 000000000..874a2a26e --- /dev/null +++ b/builder/vmware/iso/step_download.go @@ -0,0 +1,72 @@ +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 stepDownload struct { + step *common.StepDownload +} + +func (s *stepDownload) 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") + + targetPath := "" + for _, url := range s.step.Url { + targetPath = s.step.TargetPath + + if 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.step.Extension) + targetPath = cache.Lock(cacheKey) + cache.Unlock(cacheKey) + } + } + + remotePath := esx5.cachePath(targetPath) + ui.Message(remotePath) + if esx5.verifyChecksum(s.step.ChecksumType, s.step.Checksum, remotePath) { + state.Put(s.step.ResultKey, "skip_upload:"+remotePath) + ui.Message("Remote cache verified, skipping download step") + return multistep.ActionContinue + } + + ui.Message("Remote cache couldn't be verified") + } + } + + return s.step.Run(state) +} + +func (s *stepDownload) Cleanup(multistep.StateBag) {} diff --git a/builder/vmware/iso/step_remote_upload.go b/builder/vmware/iso/step_remote_upload.go index 6ef27bce6..852369627 100644 --- a/builder/vmware/iso/step_remote_upload.go +++ b/builder/vmware/iso/step_remote_upload.go @@ -2,10 +2,12 @@ package iso import ( "fmt" + "log" + "strings" + vmwcommon "github.com/hashicorp/packer/builder/vmware/common" "github.com/hashicorp/packer/packer" "github.com/mitchellh/multistep" - "log" ) // stepRemoteUpload uploads some thing from the state bag to a remote driver @@ -33,17 +35,21 @@ func (s *stepRemoteUpload) Run(state multistep.StateBag) multistep.StepAction { checksum := config.ISOChecksum checksumType := config.ISOChecksumType - ui.Say(s.Message) - log.Printf("Remote uploading: %s", path) - newPath, err := remote.UploadISO(path, checksum, checksumType) - if err != nil { - err := fmt.Errorf("Error uploading file: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + if !strings.HasPrefix(path, "skip_upload:") { + log.Printf("Remote uploading: %s", path) + newPath, err := remote.UploadISO(path, checksum, checksumType) + + if err != nil { + err := fmt.Errorf("Error uploading file: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + state.Put(s.Key, newPath) + } else { + state.Put(s.Key, strings.Split("skip_upload:", path)[0]) } - state.Put(s.Key, newPath) return multistep.ActionContinue }