|
|
|
|
@ -30,6 +30,31 @@ type PostProcessor struct {
|
|
|
|
|
config Config
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type outputPathTemplate struct {
|
|
|
|
|
BuildName string
|
|
|
|
|
BuilderType string
|
|
|
|
|
ChecksumType string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getHash(t string) hash.Hash {
|
|
|
|
|
var h hash.Hash
|
|
|
|
|
switch t {
|
|
|
|
|
case "md5":
|
|
|
|
|
h = md5.New()
|
|
|
|
|
case "sha1":
|
|
|
|
|
h = sha1.New()
|
|
|
|
|
case "sha224":
|
|
|
|
|
h = sha256.New224()
|
|
|
|
|
case "sha256":
|
|
|
|
|
h = sha256.New()
|
|
|
|
|
case "sha384":
|
|
|
|
|
h = sha512.New384()
|
|
|
|
|
case "sha512":
|
|
|
|
|
h = sha512.New()
|
|
|
|
|
}
|
|
|
|
|
return h
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
|
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
|
|
|
|
Interpolate: true,
|
|
|
|
|
@ -40,16 +65,22 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
errs := new(packer.MultiError)
|
|
|
|
|
|
|
|
|
|
if p.config.ChecksumTypes == nil {
|
|
|
|
|
p.config.ChecksumTypes = []string{"md5"}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if p.config.OutputPath == "" {
|
|
|
|
|
p.config.OutputPath = "packer_{{.BuildName}}_{{.BuilderType}}" + ".checksum"
|
|
|
|
|
for _, k := range p.config.ChecksumTypes {
|
|
|
|
|
if h := getHash(k); h == nil {
|
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
|
fmt.Errorf("Unrecognized checksum type: %s", k))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
errs := new(packer.MultiError)
|
|
|
|
|
if p.config.OutputPath == "" {
|
|
|
|
|
p.config.OutputPath = "packer_{{.BuildName}}_{{.BuilderType}}_{{.ChecksumType}}.checksum"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = interpolate.Validate(p.config.OutputPath, &p.config.ctx); err != nil {
|
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
|
@ -63,37 +94,26 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getHash(t string) hash.Hash {
|
|
|
|
|
var h hash.Hash
|
|
|
|
|
switch t {
|
|
|
|
|
case "md5":
|
|
|
|
|
h = md5.New()
|
|
|
|
|
case "sha1":
|
|
|
|
|
h = sha1.New()
|
|
|
|
|
case "sha224":
|
|
|
|
|
h = sha256.New224()
|
|
|
|
|
case "sha256":
|
|
|
|
|
h = sha256.New()
|
|
|
|
|
case "sha384":
|
|
|
|
|
h = sha512.New384()
|
|
|
|
|
case "sha512":
|
|
|
|
|
h = sha512.New()
|
|
|
|
|
}
|
|
|
|
|
return h
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
|
|
|
|
|
files := artifact.Files()
|
|
|
|
|
var h hash.Hash
|
|
|
|
|
var checksumFile string
|
|
|
|
|
|
|
|
|
|
newartifact := NewArtifact(artifact.Files())
|
|
|
|
|
opTpl := &outputPathTemplate{
|
|
|
|
|
BuildName: p.config.PackerBuildName,
|
|
|
|
|
BuilderType: p.config.PackerBuilderType,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, ct := range p.config.ChecksumTypes {
|
|
|
|
|
h = getHash(ct)
|
|
|
|
|
opTpl.ChecksumType = ct
|
|
|
|
|
p.config.ctx.Data = &opTpl
|
|
|
|
|
|
|
|
|
|
for _, art := range files {
|
|
|
|
|
checksumFile = p.config.OutputPath
|
|
|
|
|
checksumFile, err := interpolate.Render(p.config.OutputPath, &p.config.ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := os.Stat(checksumFile); err != nil {
|
|
|
|
|
newartifact.files = append(newartifact.files, checksumFile)
|
|
|
|
|
|