Merge pull request #4812 from hashicorp/checksumfix
post-processor/checksum: fix crash when invalid checksum is used
This commit is contained in:
commit
b0a6bb24e1
|
@ -30,37 +30,10 @@ type PostProcessor struct {
|
||||||
config Config
|
config Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
type outputPathTemplate struct {
|
||||||
err := config.Decode(&p.config, &config.DecodeOpts{
|
BuildName string
|
||||||
Interpolate: true,
|
BuilderType string
|
||||||
InterpolateFilter: &interpolate.RenderFilter{
|
ChecksumType string
|
||||||
Exclude: []string{},
|
|
||||||
},
|
|
||||||
}, raws...)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.config.ChecksumTypes == nil {
|
|
||||||
p.config.ChecksumTypes = []string{"md5"}
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.config.OutputPath == "" {
|
|
||||||
p.config.OutputPath = "packer_{{.BuildName}}_{{.BuilderType}}" + ".checksum"
|
|
||||||
}
|
|
||||||
|
|
||||||
errs := new(packer.MultiError)
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getHash(t string) hash.Hash {
|
func getHash(t string) hash.Hash {
|
||||||
|
@ -82,18 +55,65 @@ func getHash(t string) hash.Hash {
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
||||||
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
||||||
|
Interpolate: true,
|
||||||
|
InterpolateFilter: &interpolate.RenderFilter{
|
||||||
|
Exclude: []string{},
|
||||||
|
},
|
||||||
|
}, raws...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
errs := new(packer.MultiError)
|
||||||
|
|
||||||
|
if p.config.ChecksumTypes == nil {
|
||||||
|
p.config.ChecksumTypes = []string{"md5"}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, k := range p.config.ChecksumTypes {
|
||||||
|
if h := getHash(k); h == nil {
|
||||||
|
errs = packer.MultiErrorAppend(errs,
|
||||||
|
fmt.Errorf("Unrecognized checksum type: %s", k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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(
|
||||||
|
errs, fmt.Errorf("Error parsing target template: %s", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(errs.Errors) > 0 {
|
||||||
|
return errs
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
|
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
|
||||||
files := artifact.Files()
|
files := artifact.Files()
|
||||||
var h hash.Hash
|
var h hash.Hash
|
||||||
var checksumFile string
|
|
||||||
|
|
||||||
newartifact := NewArtifact(artifact.Files())
|
newartifact := NewArtifact(artifact.Files())
|
||||||
|
opTpl := &outputPathTemplate{
|
||||||
|
BuildName: p.config.PackerBuildName,
|
||||||
|
BuilderType: p.config.PackerBuilderType,
|
||||||
|
}
|
||||||
|
|
||||||
for _, ct := range p.config.ChecksumTypes {
|
for _, ct := range p.config.ChecksumTypes {
|
||||||
h = getHash(ct)
|
h = getHash(ct)
|
||||||
|
opTpl.ChecksumType = ct
|
||||||
|
p.config.ctx.Data = &opTpl
|
||||||
|
|
||||||
for _, art := range files {
|
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 {
|
if _, err := os.Stat(checksumFile); err != nil {
|
||||||
newartifact.files = append(newartifact.files, checksumFile)
|
newartifact.files = append(newartifact.files, checksumFile)
|
||||||
|
|
|
@ -44,4 +44,13 @@ Optional parameters:
|
||||||
|
|
||||||
- `checksum_types` (array of strings) - An array of strings of checksum types
|
- `checksum_types` (array of strings) - An array of strings of checksum types
|
||||||
to compute. Allowed values are md5, sha1, sha224, sha256, sha384, sha512.
|
to compute. Allowed values are md5, sha1, sha224, sha256, sha384, sha512.
|
||||||
- `output` (string) - Specify filename to store checksums.
|
- `output` (string) - Specify filename to store checksums. This defaults to
|
||||||
|
`packer_{{.BuildName}}_{{.BuilderType}}_{{.ChecksumType}}.checksum`. For
|
||||||
|
example, if you had a builder named `database`, you might see the file
|
||||||
|
written as `packer_database_docker_md5.checksum`. The following variables are
|
||||||
|
available to use in the output template:
|
||||||
|
|
||||||
|
* `BuildName`: The name of the builder that produced the artifact.
|
||||||
|
* `BuilderType`: The type of builder used to produce the artifact.
|
||||||
|
* `ChecksumType`: The type of checksums the file contains. This should be
|
||||||
|
used if you have more than one value in `checksum_types`.
|
||||||
|
|
Loading…
Reference in New Issue