From c2975140cf6cf2d4fd5d73b2c6b736961ae24105 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 25 Jun 2020 09:36:48 +0200 Subject: [PATCH] HCL2: allow to use keep_input_artifact in post processors (#9477) * HCL2: allow to use keep_input_artifact in post processors * add basic test * add docs --- hcl2template/testdata/complete/build.pkr.hcl | 5 +-- hcl2template/types.build.post-processor.go | 25 ++++++++------- hcl2template/types.packer_config.go | 7 ++-- hcl2template/types.packer_config_test.go | 24 +++++++++----- packer/build.go | 8 ++--- packer/core.go | 2 +- .../from-1.5/blocks/build/post-processor.mdx | 32 +++++++++++++++---- 7 files changed, 68 insertions(+), 35 deletions(-) diff --git a/hcl2template/testdata/complete/build.pkr.hcl b/hcl2template/testdata/complete/build.pkr.hcl index 327d2399f..d26ecb4ed 100644 --- a/hcl2template/testdata/complete/build.pkr.hcl +++ b/hcl2template/testdata/complete/build.pkr.hcl @@ -116,7 +116,7 @@ build { } } - post-processor "amazon-import" { + post-processor "amazon-import" { name = "something" string = "string" int = 42 @@ -124,6 +124,7 @@ build { bool = true trilean = true duration = "10s" + keep_input_artifact = true map_string_string = { a = "b" c = "d" @@ -164,7 +165,7 @@ build { } } - post-processor "amazon-import" { + post-processor "amazon-import" { string = "string" int = 42 int64 = 43 diff --git a/hcl2template/types.build.post-processor.go b/hcl2template/types.build.post-processor.go index 721a4def9..b45b682f5 100644 --- a/hcl2template/types.build.post-processor.go +++ b/hcl2template/types.build.post-processor.go @@ -10,9 +10,10 @@ import ( // ProvisionerBlock references a detected but unparsed post processor type PostProcessorBlock struct { - PType string - PName string - OnlyExcept OnlyExcept + PType string + PName string + OnlyExcept OnlyExcept + KeepInputArtifact *bool HCL2Ref } @@ -23,10 +24,11 @@ func (p *PostProcessorBlock) String() string { func (p *Parser) decodePostProcessor(block *hcl.Block) (*PostProcessorBlock, hcl.Diagnostics) { var b struct { - Name string `hcl:"name,optional"` - Only []string `hcl:"only,optional"` - Except []string `hcl:"except,optional"` - Rest hcl.Body `hcl:",remain"` + Name string `hcl:"name,optional"` + Only []string `hcl:"only,optional"` + Except []string `hcl:"except,optional"` + KeepInputArtifact *bool `hcl:"keep_input_artifact,optional"` + Rest hcl.Body `hcl:",remain"` } diags := gohcl.DecodeBody(block.Body, nil, &b) if diags.HasErrors() { @@ -34,10 +36,11 @@ func (p *Parser) decodePostProcessor(block *hcl.Block) (*PostProcessorBlock, hcl } postProcessor := &PostProcessorBlock{ - PType: block.Labels[0], - PName: b.Name, - OnlyExcept: OnlyExcept{Only: b.Only, Except: b.Except}, - HCL2Ref: newHCL2Ref(block, b.Rest), + PType: block.Labels[0], + PName: b.Name, + OnlyExcept: OnlyExcept{Only: b.Only, Except: b.Except}, + HCL2Ref: newHCL2Ref(block, b.Rest), + KeepInputArtifact: b.KeepInputArtifact, } diags = diags.Extend(postProcessor.OnlyExcept.Validate()) diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index fd692fb9a..b9287845f 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -271,9 +271,10 @@ func (cfg *PackerConfig) getCoreBuildPostProcessors(source SourceBlock, blocks [ continue } res = append(res, packer.CoreBuildPostProcessor{ - PostProcessor: postProcessor, - PName: ppb.PName, - PType: ppb.PType, + PostProcessor: postProcessor, + PName: ppb.PName, + PType: ppb.PType, + KeepInputArtifact: ppb.KeepInputArtifact, }) } diff --git a/hcl2template/types.packer_config_test.go b/hcl2template/types.packer_config_test.go index 74f688bb6..74df297e4 100644 --- a/hcl2template/types.packer_config_test.go +++ b/hcl2template/types.packer_config_test.go @@ -11,6 +11,7 @@ import ( var ( refVBIsoUbuntu1204 = SourceRef{Type: "virtualbox-iso", Name: "ubuntu-1204"} refAWSEBSUbuntu1604 = SourceRef{Type: "amazon-ebs", Name: "ubuntu-1604"} + pTrue = pointerToBool(true) ) func TestParser_complete(t *testing.T) { @@ -90,8 +91,9 @@ func TestParser_complete(t *testing.T) { }, PostProcessors: []*PostProcessorBlock{ { - PType: "amazon-import", - PName: "something", + PType: "amazon-import", + PName: "something", + KeepInputArtifact: pTrue, }, { PType: "amazon-import", @@ -117,9 +119,10 @@ func TestParser_complete(t *testing.T) { PostProcessors: [][]packer.CoreBuildPostProcessor{ { { - PType: "amazon-import", - PName: "something", - PostProcessor: basicMockPostProcessor, + PType: "amazon-import", + PName: "something", + PostProcessor: basicMockPostProcessor, + KeepInputArtifact: pTrue, }, { PType: "amazon-import", @@ -152,9 +155,10 @@ func TestParser_complete(t *testing.T) { PostProcessors: [][]packer.CoreBuildPostProcessor{ { { - PType: "amazon-import", - PName: "something", - PostProcessor: basicMockPostProcessor, + PType: "amazon-import", + PName: "something", + PostProcessor: basicMockPostProcessor, + KeepInputArtifact: pTrue, }, { PType: "amazon-import", @@ -191,3 +195,7 @@ func TestParser_ValidateFilterOption(t *testing.T) { }) } } + +func pointerToBool(b bool) *bool { + return &b +} diff --git a/packer/build.go b/packer/build.go index d3bb2e806..ca900797f 100644 --- a/packer/build.go +++ b/packer/build.go @@ -115,7 +115,7 @@ type CoreBuildPostProcessor struct { PType string PName string config map[string]interface{} - keepInputArtifact *bool + KeepInputArtifact *bool } // CoreBuildProvisioner keeps track of the provisioner and the configuration of @@ -341,15 +341,15 @@ PostProcessorRunSeqLoop: // Exception: for postprocessors that will fail/become // useless if keep isn't true, heed forceOverride and keep the // input artifact regardless of user preference. - if corePP.keepInputArtifact != nil { - if defaultKeep && *corePP.keepInputArtifact == false && forceOverride { + if corePP.KeepInputArtifact != nil { + if defaultKeep && *corePP.KeepInputArtifact == false && forceOverride { log.Printf("The %s post-processor forces "+ "keep_input_artifact=true to preserve integrity of the"+ "build chain. User-set keep_input_artifact=false will be"+ "ignored.", corePP.PType) } else { // User overrides default. - keep = *corePP.keepInputArtifact + keep = *corePP.KeepInputArtifact } } if i == 0 { diff --git a/packer/core.go b/packer/core.go index 262800a69..89ff7068e 100644 --- a/packer/core.go +++ b/packer/core.go @@ -341,7 +341,7 @@ func (c *Core) Build(n string) (Build, error) { PType: rawP.Type, PName: rawP.Name, config: rawP.Config, - keepInputArtifact: rawP.KeepInputArtifact, + KeepInputArtifact: rawP.KeepInputArtifact, }) } diff --git a/website/pages/docs/from-1.5/blocks/build/post-processor.mdx b/website/pages/docs/from-1.5/blocks/build/post-processor.mdx index ee20aca16..47fccaabb 100644 --- a/website/pages/docs/from-1.5/blocks/build/post-processor.mdx +++ b/website/pages/docs/from-1.5/blocks/build/post-processor.mdx @@ -17,8 +17,8 @@ The `post-processor` block defines how a post-processor is configured. build { # ... post-processor "checksum" { - checksum_types = [ "md5", "sha512" ] - keep_input_artifact = true + checksum_types = [ "md5", "sha512" ] # checksum the artifact + keep_input_artifact = true # keep the artifact } } ``` @@ -28,12 +28,32 @@ the provisioner(s). Post-processors are optional, and they can be used to upload artifacts, re-package, or more. The list of available post-processors can be found in the [post-processors](/docs/post-processors) section. +-> Note: The input 'artifact' received by a post-processor will be automatically + deleted. + +# Keep an input artifact + +To prevent an input artifact from being deleted, you can set the +`keep_input_artifact` field to true to make Packer keep both artifacts. For +example if we want to checksum an artifact and keep the artifact: + +```hcl +# builds.pkr.hcl +build { + # ... + post-processor "checksum" { + checksum_types = [ "md5", "sha512" ] + keep_input_artifact = true + } +} +``` + # Run on Specific Builds You can use the `only` or `except` configurations to run a post-processor only -with specific builds. These two configurations do what you expect: `only` will -only run the post-processor on the specified builds and `except` will run the -post-processor on anything other than the specified builds. +with specific sources. These two configurations do what you expect: `only` will +only run the post-processor on the specified sources and `except` will run the +post-processor on anything other than the specified sources. An example of `only` being used is shown below, but the usage of `except` is effectively the same: @@ -50,4 +70,4 @@ build { } ``` -The values within `only` or `except` are _build names_, not builder types. +The values within `only` or `except` are _source names_, not builder types.