Verify remote cache for ESXi

This commit is contained in:
bugbuilder 2017-07-23 01:31:46 -04:00
parent 54de455729
commit 4023b618b4
3 changed files with 98 additions and 19 deletions

View file

@ -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,
},

View file

@ -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) {}

View file

@ -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
}