Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the use of build.name for naming provisioners and post-processors #11432

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
build {
name = "test-build"
sources = [ "source.virtualbox-iso.ubuntu-1204" ]

post-processor "manifest" {
name = build.name
slice_string = ["${packer.version}", "${build.name}"]
}

}

source "virtualbox-iso" "ubuntu-1204" {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

// starts resources to provision them.
build {
name = "build-name-test"
sources = [
"source.virtualbox-iso.ubuntu-1204",
]

provisioner "shell" {
name = build.name
slice_string = ["${build.name}"]
}
}

source "virtualbox-iso" "ubuntu-1204" {
}

15 changes: 11 additions & 4 deletions hcl2template/types.build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/hcl/v2/hclsyntax"
packerregistry "github.com/hashicorp/packer/internal/registry"
"github.com/hashicorp/packer/internal/registry/env"
"github.com/zclconf/go-cty/cty"
)

const (
Expand Down Expand Up @@ -105,6 +106,12 @@ func (p *Parser) decodeBuildConfig(block *hcl.Block, cfg *PackerConfig) (*BuildB
build.Name = b.Name
build.Description = b.Description

// Expose build.name during parsing of pps and provisioners
ectx := cfg.EvalContext(BuildContext, nil)
ectx.Variables[buildAccessor] = cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal(b.Name),
})
Comment on lines +110 to +113
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could have also passed the extra variables as argument here, like:

Suggested change
ectx := cfg.EvalContext(BuildContext, nil)
ectx.Variables[buildAccessor] = cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal(b.Name),
})
variables := map[string]cty.Value{
buildAccessor: cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal(b.Name),
}),
}
ectx := cfg.EvalContext(BuildContext, variables)

It looks bigger, just another possibility. Don't feel pressured to accept the changes. Yours looks great!


for _, buildFrom := range b.FromSources {
ref := sourceRefFromString(buildFrom)

Expand Down Expand Up @@ -158,7 +165,7 @@ func (p *Parser) decodeBuildConfig(block *hcl.Block, cfg *PackerConfig) (*BuildB
}
build.Sources = append(build.Sources, ref)
case buildProvisionerLabel:
p, moreDiags := p.decodeProvisioner(block, cfg)
p, moreDiags := p.decodeProvisioner(block, ectx)
diags = append(diags, moreDiags...)
if moreDiags.HasErrors() {
continue
Expand All @@ -173,14 +180,14 @@ func (p *Parser) decodeBuildConfig(block *hcl.Block, cfg *PackerConfig) (*BuildB
})
continue
}
p, moreDiags := p.decodeProvisioner(block, cfg)
p, moreDiags := p.decodeProvisioner(block, ectx)
diags = append(diags, moreDiags...)
if moreDiags.HasErrors() {
continue
}
build.ErrorCleanupProvisionerBlock = p
case buildPostProcessorLabel:
pp, moreDiags := p.decodePostProcessor(block, cfg)
pp, moreDiags := p.decodePostProcessor(block, ectx)
diags = append(diags, moreDiags...)
if moreDiags.HasErrors() {
continue
Expand All @@ -197,7 +204,7 @@ func (p *Parser) decodeBuildConfig(block *hcl.Block, cfg *PackerConfig) (*BuildB
errored := false
postProcessors := []*PostProcessorBlock{}
for _, block := range content.Blocks {
pp, moreDiags := p.decodePostProcessor(block, cfg)
pp, moreDiags := p.decodePostProcessor(block, ectx)
diags = append(diags, moreDiags...)
if moreDiags.HasErrors() {
errored = true
Expand Down
5 changes: 3 additions & 2 deletions hcl2template/types.build.post-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ func (p *PostProcessorBlock) String() string {
return fmt.Sprintf(buildPostProcessorLabel+"-block %q %q", p.PType, p.PName)
}

func (p *Parser) decodePostProcessor(block *hcl.Block, cfg *PackerConfig) (*PostProcessorBlock, hcl.Diagnostics) {
func (p *Parser) decodePostProcessor(block *hcl.Block, ectx *hcl.EvalContext) (*PostProcessorBlock, hcl.Diagnostics) {
var b struct {
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, cfg.EvalContext(BuildContext, nil), &b)

diags := gohcl.DecodeBody(block.Body, ectx, &b)
if diags.HasErrors() {
return nil, diags
}
Expand Down
4 changes: 2 additions & 2 deletions hcl2template/types.build.provisioners.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (p *ProvisionerBlock) String() string {
return fmt.Sprintf(buildProvisionerLabel+"-block %q %q", p.PType, p.PName)
}

func (p *Parser) decodeProvisioner(block *hcl.Block, cfg *PackerConfig) (*ProvisionerBlock, hcl.Diagnostics) {
func (p *Parser) decodeProvisioner(block *hcl.Block, ectx *hcl.EvalContext) (*ProvisionerBlock, hcl.Diagnostics) {
var b struct {
Name string `hcl:"name,optional"`
PauseBefore string `hcl:"pause_before,optional"`
Expand All @@ -85,7 +85,7 @@ func (p *Parser) decodeProvisioner(block *hcl.Block, cfg *PackerConfig) (*Provis
Override cty.Value `hcl:"override,optional"`
Rest hcl.Body `hcl:",remain"`
}
diags := gohcl.DecodeBody(block.Body, cfg.EvalContext(BuildContext, nil), &b)
diags := gohcl.DecodeBody(block.Body, ectx, &b)
if diags.HasErrors() {
return nil, diags
}
Expand Down
114 changes: 114 additions & 0 deletions hcl2template/types.build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,120 @@ func TestParse_build(t *testing.T) {
[]packersdk.Build{},
false,
},
{"use build.name in post-processor block",
defaultParser,
parseTestArgs{"testdata/build/post-processor_build_name_interpolation.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "build"),
Sources: map[SourceRef]SourceBlock{
refVBIsoUbuntu1204: {Type: "virtualbox-iso", Name: "ubuntu-1204"},
},
Builds: Builds{
&BuildBlock{
Name: "test-build",
Sources: []SourceUseBlock{
{
SourceRef: refVBIsoUbuntu1204,
},
},
PostProcessorsLists: [][]*PostProcessorBlock{
{
{
PName: "test-build",
PType: "manifest",
},
},
},
},
},
},
false, false,
[]packersdk.Build{
&packer.CoreBuild{
BuildName: "test-build",
Type: "virtualbox-iso.ubuntu-1204",
Prepared: true,
Builder: emptyMockBuilder,
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{
{
{
PName: "test-build",
PType: "manifest",
PostProcessor: &HCL2PostProcessor{
PostProcessor: &MockPostProcessor{
Config: MockConfig{
NestedMockConfig: NestedMockConfig{
Tags: []MockTag{},
SliceString: []string{lockedVersion, "test-build"},
},
NestedSlice: []NestedMockConfig{},
},
},
},
},
},
},
},
},
false,
},
{"use build.name in provisioner block",
defaultParser,
parseTestArgs{"testdata/build/provisioner_build_name_interpolation.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "build"),
Sources: map[SourceRef]SourceBlock{
refVBIsoUbuntu1204: {Type: "virtualbox-iso", Name: "ubuntu-1204"},
},
Builds: Builds{
&BuildBlock{
Name: "build-name-test",
Sources: []SourceUseBlock{
{
SourceRef: refVBIsoUbuntu1204,
},
},
ProvisionerBlocks: []*ProvisionerBlock{
{
PName: "build-name-test",
PType: "shell",
},
},
},
},
},
false, false,
[]packersdk.Build{
&packer.CoreBuild{
BuildName: "build-name-test",
Type: "virtualbox-iso.ubuntu-1204",
Prepared: true,
Builder: emptyMockBuilder,
Provisioners: []packer.CoreBuildProvisioner{
{
PName: "build-name-test",
PType: "shell",
Provisioner: &HCL2Provisioner{
Provisioner: &MockProvisioner{
Config: MockConfig{
NestedMockConfig: NestedMockConfig{
Tags: []MockTag{},
SliceString: []string{"build-name-test"},
},
NestedSlice: []NestedMockConfig{},
},
},
},
},
},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
},
},
false,
},
}
testParse(t, tests)
}