From b50e279d8abfec34350ccd86e55a99973a53610c Mon Sep 17 00:00:00 2001 From: bugbuilder Date: Mon, 24 Jul 2017 00:11:30 -0400 Subject: [PATCH] Making visible verify cache step --- builder/vmware/iso/builder.go | 34 +++++++++++-------- builder/vmware/iso/step_register.go | 2 +- builder/vmware/iso/step_remote_upload.go | 34 ++++++++++--------- ...{step_download.go => step_verify_cache.go} | 28 ++++++++------- 4 files changed, 54 insertions(+), 44 deletions(-) rename builder/vmware/iso/{step_download.go => step_verify_cache.go} (63%) diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 5dba8de4e..7d3d6074e 100644 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -215,21 +215,28 @@ 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{ + 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, + }, + remoteUpload: &stepRemoteUpload{ + Key: "iso_path", + Message: "Uploading ISO to remoteUpload machine...", + }, + } + steps := []multistep.Step{ &vmwcommon.StepPrepareTools{ RemoteType: b.config.RemoteType, ToolsUploadFlavor: b.config.ToolsUploadFlavor, }, - &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, - }}, + stepVerifyCache, &vmwcommon.StepOutputDir{ Force: b.config.PackerForce, }, @@ -239,12 +246,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, &stepRemoteUpload{ Key: "floppy_path", - Message: "Uploading Floppy to remote machine...", - }, - &stepRemoteUpload{ - Key: "iso_path", - Message: "Uploading ISO to remote machine...", + Message: "Uploading Floppy to remoteUpload machine...", }, + stepVerifyCache.remoteUpload, &stepCreateDisk{}, &stepCreateVMX{}, &vmwcommon.StepConfigureVMX{ diff --git a/builder/vmware/iso/step_register.go b/builder/vmware/iso/step_register.go index a90de5fa2..524e3006d 100644 --- a/builder/vmware/iso/step_register.go +++ b/builder/vmware/iso/step_register.go @@ -20,7 +20,7 @@ func (s *StepRegister) Run(state multistep.StateBag) multistep.StepAction { vmxPath := state.Get("vmx_path").(string) if remoteDriver, ok := driver.(RemoteDriver); ok { - ui.Say("Registering remote VM...") + ui.Say("Registering remoteUpload VM...") if err := remoteDriver.Register(vmxPath); err != nil { err := fmt.Errorf("Error registering VM: %s", err) state.Put("error", err) diff --git a/builder/vmware/iso/step_remote_upload.go b/builder/vmware/iso/step_remote_upload.go index 2cb5003f5..56e1d0e58 100644 --- a/builder/vmware/iso/step_remote_upload.go +++ b/builder/vmware/iso/step_remote_upload.go @@ -3,24 +3,30 @@ package iso import ( "fmt" "log" - "strings" vmwcommon "github.com/hashicorp/packer/builder/vmware/common" "github.com/hashicorp/packer/packer" "github.com/mitchellh/multistep" ) -// stepRemoteUpload uploads some thing from the state bag to a remote driver -// (if it can) and stores that new remote path into the state bag. +// stepRemoteUpload uploads some thing from the state bag to a remoteUpload driver +// (if it can) and stores that new remoteUpload path into the state bag. type stepRemoteUpload struct { Key string Message string + + // Set this to true for skip + Skip bool } func (s *stepRemoteUpload) Run(state multistep.StateBag) multistep.StepAction { driver := state.Get("driver").(vmwcommon.Driver) ui := state.Get("ui").(packer.Ui) + if s.Skip { + return multistep.ActionContinue + } + remote, ok := driver.(RemoteDriver) if !ok { return multistep.ActionContinue @@ -35,20 +41,16 @@ func (s *stepRemoteUpload) Run(state multistep.StateBag) multistep.StepAction { checksum := config.ISOChecksum checksumType := config.ISOChecksumType - if !strings.HasPrefix(path, "skip_upload:") { - 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 - } - state.Put(s.Key, newPath) - } else { - state.Put(s.Key, strings.Split(path, "skip_upload:")[1]) + 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 } + state.Put(s.Key, newPath) return multistep.ActionContinue } diff --git a/builder/vmware/iso/step_download.go b/builder/vmware/iso/step_verify_cache.go similarity index 63% rename from builder/vmware/iso/step_download.go rename to builder/vmware/iso/step_verify_cache.go index d9a26fc17..01e90bbbf 100644 --- a/builder/vmware/iso/step_download.go +++ b/builder/vmware/iso/step_verify_cache.go @@ -13,20 +13,21 @@ import ( "runtime" ) -type stepDownload struct { - step *common.StepDownload +type stepVerifyCache struct { + download *common.StepDownload + remoteUpload *stepRemoteUpload } -func (s *stepDownload) Run(state multistep.StateBag) multistep.StepAction { +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") + ui.Say("Verifying remoteUpload cache") - for _, url := range s.step.Url { - targetPath := s.step.TargetPath + for _, url := range s.download.Url { + targetPath := s.download.TargetPath if u, err := neturl.Parse(url); err == nil { if u.Scheme == "file" { @@ -45,16 +46,19 @@ func (s *stepDownload) Run(state multistep.StateBag) multistep.StepAction { if targetPath == "" { hash := sha1.Sum([]byte(url)) - cacheKey := fmt.Sprintf("%s.%s", hex.EncodeToString(hash[:]), s.step.Extension) + 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.step.ChecksumType, s.step.Checksum, remotePath) { - state.Put(s.step.ResultKey, "skip_upload:"+remotePath) - ui.Message("Remote cache verified, skipping download step") + + if esx5.verifyChecksum(s.download.ChecksumType, s.download.Checksum, remotePath) { + ui.Message("Remote cache verified, skipping download/upload step") + + s.remoteUpload.Skip = true + state.Put(s.download.ResultKey, remotePath) return multistep.ActionContinue } @@ -62,7 +66,7 @@ func (s *stepDownload) Run(state multistep.StateBag) multistep.StepAction { } } - return s.step.Run(state) + return s.download.Run(state) } -func (s *stepDownload) Cleanup(multistep.StateBag) {} +func (s *stepVerifyCache) Cleanup(multistep.StateBag) {}