2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2016-05-18 20:31:16 -04:00
|
|
|
package checksum
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:56:02 -04:00
|
|
|
"context"
|
2016-05-18 20:31:16 -04:00
|
|
|
"crypto/md5"
|
|
|
|
"crypto/sha1"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/sha512"
|
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2016-05-18 20:31:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
|
|
|
Keep bool `mapstructure:"keep_input_artifact"`
|
|
|
|
ChecksumTypes []string `mapstructure:"checksum_types"`
|
|
|
|
OutputPath string `mapstructure:"output"`
|
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
type PostProcessor struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
2017-04-19 19:37:28 -04:00
|
|
|
type outputPathTemplate struct {
|
2017-04-20 02:26:49 -04:00
|
|
|
BuildName string
|
|
|
|
BuilderType string
|
|
|
|
ChecksumType string
|
2017-04-19 19:37:28 -04:00
|
|
|
}
|
|
|
|
|
2017-04-20 00:23:12 -04:00
|
|
|
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()
|
2017-04-20 00:20:47 -04:00
|
|
|
}
|
2017-04-20 00:23:12 -04:00
|
|
|
return h
|
2017-04-20 00:20:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-18 20:31:16 -04:00
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
2017-07-09 15:50:20 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
2016-05-18 20:31:16 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
2017-07-09 15:50:20 -04:00
|
|
|
Exclude: []string{"output"},
|
2016-05-18 20:31:16 -04:00
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-20 00:20:47 -04:00
|
|
|
errs := new(packer.MultiError)
|
2016-05-18 20:31:16 -04:00
|
|
|
|
|
|
|
if p.config.ChecksumTypes == nil {
|
|
|
|
p.config.ChecksumTypes = []string{"md5"}
|
|
|
|
}
|
|
|
|
|
2017-04-20 00:20:47 -04:00
|
|
|
for _, k := range p.config.ChecksumTypes {
|
2017-04-20 00:23:12 -04:00
|
|
|
if h := getHash(k); h == nil {
|
2017-04-20 00:20:47 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Unrecognized checksum type: %s", k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 20:31:16 -04:00
|
|
|
if p.config.OutputPath == "" {
|
2017-04-20 02:26:49 -04:00
|
|
|
p.config.OutputPath = "packer_{{.BuildName}}_{{.BuilderType}}_{{.ChecksumType}}.checksum"
|
2016-05-18 20:31:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = interpolate.Validate(p.config.OutputPath, &p.config.ctx); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error parsing target template: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs.Errors) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-08 13:59:42 -04:00
|
|
|
func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
|
2016-05-18 20:31:16 -04:00
|
|
|
files := artifact.Files()
|
|
|
|
var h hash.Hash
|
|
|
|
|
|
|
|
newartifact := NewArtifact(artifact.Files())
|
2017-04-19 19:37:28 -04:00
|
|
|
opTpl := &outputPathTemplate{
|
|
|
|
BuildName: p.config.PackerBuildName,
|
|
|
|
BuilderType: p.config.PackerBuilderType,
|
|
|
|
}
|
2016-05-18 20:31:16 -04:00
|
|
|
|
|
|
|
for _, ct := range p.config.ChecksumTypes {
|
2017-04-20 00:23:12 -04:00
|
|
|
h = getHash(ct)
|
2017-04-20 02:26:49 -04:00
|
|
|
opTpl.ChecksumType = ct
|
2017-04-19 19:37:28 -04:00
|
|
|
p.config.ctx.Data = &opTpl
|
2016-05-18 20:31:16 -04:00
|
|
|
|
|
|
|
for _, art := range files {
|
2017-04-19 19:37:28 -04:00
|
|
|
checksumFile, err := interpolate.Render(p.config.OutputPath, &p.config.ctx)
|
|
|
|
if err != nil {
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, true, err
|
2017-04-19 19:37:28 -04:00
|
|
|
}
|
2016-11-26 17:39:29 -05:00
|
|
|
|
2016-05-18 20:31:16 -04:00
|
|
|
if _, err := os.Stat(checksumFile); err != nil {
|
|
|
|
newartifact.files = append(newartifact.files, checksumFile)
|
|
|
|
}
|
2016-07-02 11:17:39 -04:00
|
|
|
if err := os.MkdirAll(filepath.Dir(checksumFile), os.FileMode(0755)); err != nil {
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, true, fmt.Errorf("unable to create dir: %s", err.Error())
|
2016-07-02 11:17:39 -04:00
|
|
|
}
|
2016-05-18 20:31:16 -04:00
|
|
|
fw, err := os.OpenFile(checksumFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(0644))
|
|
|
|
if err != nil {
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, true, fmt.Errorf("unable to create file %s: %s", checksumFile, err.Error())
|
2016-05-18 20:31:16 -04:00
|
|
|
}
|
|
|
|
fr, err := os.Open(art)
|
|
|
|
if err != nil {
|
|
|
|
fw.Close()
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, true, fmt.Errorf("unable to open file %s: %s", art, err.Error())
|
2016-05-18 20:31:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = io.Copy(h, fr); err != nil {
|
|
|
|
fr.Close()
|
|
|
|
fw.Close()
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, true, fmt.Errorf("unable to compute %s hash for %s", ct, art)
|
2016-05-18 20:31:16 -04:00
|
|
|
}
|
|
|
|
fr.Close()
|
|
|
|
fw.WriteString(fmt.Sprintf("%x\t%s\n", h.Sum(nil), filepath.Base(art)))
|
|
|
|
fw.Close()
|
2016-11-26 17:39:29 -05:00
|
|
|
h.Reset()
|
2016-05-18 20:31:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 19:51:58 -04:00
|
|
|
// sets keep and forceOverride to true because we don't want to accidentally
|
|
|
|
// delete the very artifact we're checksumming.
|
|
|
|
return newartifact, true, true, nil
|
2016-05-18 20:31:16 -04:00
|
|
|
}
|