From 6722a6ba0cbc1acb681389ae8745968e27fe7b0a Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Wed, 19 Apr 2017 16:37:28 -0700 Subject: [PATCH 1/4] post-processor/checksum: fix output template --- post-processor/checksum/post-processor.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/post-processor/checksum/post-processor.go b/post-processor/checksum/post-processor.go index 8da32d5d9..9f95755c6 100644 --- a/post-processor/checksum/post-processor.go +++ b/post-processor/checksum/post-processor.go @@ -30,6 +30,13 @@ type PostProcessor struct { config Config } +type outputPathTemplate struct { + BuildName string + BuilderType string + ArtifactID string + HashType string +} + func (p *PostProcessor) Configure(raws ...interface{}) error { err := config.Decode(&p.config, &config.DecodeOpts{ Interpolate: true, @@ -46,7 +53,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { } if p.config.OutputPath == "" { - p.config.OutputPath = "packer_{{.BuildName}}_{{.BuilderType}}" + ".checksum" + p.config.OutputPath = "packer_{{.BuildName}}_{{.BuilderType}}_{{.HashType}}.checksum" } errs := new(packer.MultiError) @@ -85,15 +92,24 @@ func getHash(t string) hash.Hash { 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, + ArtifactID: artifact.Id(), + } for _, ct := range p.config.ChecksumTypes { h = getHash(ct) + opTpl.HashType = 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) From fbd621d2279d809db2206cb2f19fc570d9ff2829 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Wed, 19 Apr 2017 21:20:47 -0700 Subject: [PATCH 2/4] fail prepare if hash not found --- post-processor/checksum/post-processor.go | 43 +++++++++++------------ 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/post-processor/checksum/post-processor.go b/post-processor/checksum/post-processor.go index 9f95755c6..81bc2d460 100644 --- a/post-processor/checksum/post-processor.go +++ b/post-processor/checksum/post-processor.go @@ -37,6 +37,17 @@ type outputPathTemplate struct { HashType string } +func getHashMap() map[string]func() hash.Hash { + return map[string]func() hash.Hash{ + "md5": func() hash.Hash { return md5.New() }, + "sha1": func() hash.Hash { return sha1.New() }, + "sha224": func() hash.Hash { return sha256.New224() }, + "sha256": func() hash.Hash { return sha256.New() }, + "sha384": func() hash.Hash { return sha512.New384() }, + "sha512": func() hash.Hash { return sha512.New() }, + } +} + func (p *PostProcessor) Configure(raws ...interface{}) error { err := config.Decode(&p.config, &config.DecodeOpts{ Interpolate: true, @@ -47,17 +58,24 @@ 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"} } + hashMap := getHashMap() + for _, k := range p.config.ChecksumTypes { + if _, ok := hashMap[k]; !ok { + errs = packer.MultiErrorAppend(errs, + fmt.Errorf("Unrecognized checksum type: %s", k)) + } + } + if p.config.OutputPath == "" { p.config.OutputPath = "packer_{{.BuildName}}_{{.BuilderType}}_{{.HashType}}.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)) @@ -70,25 +88,6 @@ 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 @@ -101,7 +100,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac } for _, ct := range p.config.ChecksumTypes { - h = getHash(ct) + h = getHashMap()[ct]() opTpl.HashType = ct p.config.ctx.Data = &opTpl From 0b4cd4d3948ea06ed6c61acee6a962df5a47d4e8 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Wed, 19 Apr 2017 21:23:12 -0700 Subject: [PATCH 3/4] use the old switch --- post-processor/checksum/post-processor.go | 29 ++++++++++++++--------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/post-processor/checksum/post-processor.go b/post-processor/checksum/post-processor.go index 81bc2d460..7021af061 100644 --- a/post-processor/checksum/post-processor.go +++ b/post-processor/checksum/post-processor.go @@ -37,15 +37,23 @@ type outputPathTemplate struct { HashType string } -func getHashMap() map[string]func() hash.Hash { - return map[string]func() hash.Hash{ - "md5": func() hash.Hash { return md5.New() }, - "sha1": func() hash.Hash { return sha1.New() }, - "sha224": func() hash.Hash { return sha256.New224() }, - "sha256": func() hash.Hash { return sha256.New() }, - "sha384": func() hash.Hash { return sha512.New384() }, - "sha512": func() hash.Hash { return sha512.New() }, +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 { @@ -64,9 +72,8 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { p.config.ChecksumTypes = []string{"md5"} } - hashMap := getHashMap() for _, k := range p.config.ChecksumTypes { - if _, ok := hashMap[k]; !ok { + if h := getHash(k); h == nil { errs = packer.MultiErrorAppend(errs, fmt.Errorf("Unrecognized checksum type: %s", k)) } @@ -100,7 +107,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac } for _, ct := range p.config.ChecksumTypes { - h = getHashMap()[ct]() + h = getHash(ct) opTpl.HashType = ct p.config.ctx.Data = &opTpl From e8cada8bf7faf575590390cf25738ec7a5983dcf Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Wed, 19 Apr 2017 23:26:49 -0700 Subject: [PATCH 4/4] document and remove artifactID --- post-processor/checksum/post-processor.go | 12 +++++------- website/source/docs/post-processors/checksum.html.md | 11 ++++++++++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/post-processor/checksum/post-processor.go b/post-processor/checksum/post-processor.go index 7021af061..2ae6da164 100644 --- a/post-processor/checksum/post-processor.go +++ b/post-processor/checksum/post-processor.go @@ -31,10 +31,9 @@ type PostProcessor struct { } type outputPathTemplate struct { - BuildName string - BuilderType string - ArtifactID string - HashType string + BuildName string + BuilderType string + ChecksumType string } func getHash(t string) hash.Hash { @@ -80,7 +79,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { } if p.config.OutputPath == "" { - p.config.OutputPath = "packer_{{.BuildName}}_{{.BuilderType}}_{{.HashType}}.checksum" + p.config.OutputPath = "packer_{{.BuildName}}_{{.BuilderType}}_{{.ChecksumType}}.checksum" } if err = interpolate.Validate(p.config.OutputPath, &p.config.ctx); err != nil { @@ -103,12 +102,11 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac opTpl := &outputPathTemplate{ BuildName: p.config.PackerBuildName, BuilderType: p.config.PackerBuilderType, - ArtifactID: artifact.Id(), } for _, ct := range p.config.ChecksumTypes { h = getHash(ct) - opTpl.HashType = ct + opTpl.ChecksumType = ct p.config.ctx.Data = &opTpl for _, art := range files { diff --git a/website/source/docs/post-processors/checksum.html.md b/website/source/docs/post-processors/checksum.html.md index 855a8c375..7e72a0937 100644 --- a/website/source/docs/post-processors/checksum.html.md +++ b/website/source/docs/post-processors/checksum.html.md @@ -44,4 +44,13 @@ Optional parameters: - `checksum_types` (array of strings) - An array of strings of checksum types 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`.