hcl2template: let PackerConfig actually start a builder/provisioner/post-processor

This commit is contained in:
Adrien Delorme 2020-04-29 16:15:42 +02:00
parent a3343c1848
commit 2df21496b3
6 changed files with 27 additions and 17 deletions

View File

@ -68,6 +68,7 @@ func testParse(t *testing.T, tests []parseTest) {
} }
if diff := cmp.Diff(tt.parseWantCfg, gotCfg, if diff := cmp.Diff(tt.parseWantCfg, gotCfg,
cmpopts.IgnoreUnexported( cmpopts.IgnoreUnexported(
PackerConfig{},
cty.Value{}, cty.Value{},
cty.Type{}, cty.Type{},
Variable{}, Variable{},
@ -115,7 +116,7 @@ func testParse(t *testing.T, tests []parseTest) {
return return
} }
gotBuilds, gotDiags := tt.parser.getBuilds(gotCfg, nil, nil) gotBuilds, gotDiags := gotCfg.getBuilds(nil, nil)
if tt.getBuildsWantDiags == (gotDiags == nil) { if tt.getBuildsWantDiags == (gotDiags == nil) {
t.Fatalf("Parser.getBuilds() unexpected diagnostics. %s", gotDiags) t.Fatalf("Parser.getBuilds() unexpected diagnostics. %s", gotDiags)
} }

View File

@ -90,6 +90,9 @@ func (p *Parser) parse(filename string, varFiles []string, argVars map[string]st
} }
cfg := &PackerConfig{ cfg := &PackerConfig{
Basedir: basedir, Basedir: basedir,
builderSchemas: p.BuilderSchemas,
provisionersSchemas: p.ProvisionersSchemas,
postProcessorsSchemas: p.PostProcessorsSchemas,
} }
// Decode variable blocks so that they are available later on. Here locals // Decode variable blocks so that they are available later on. Here locals

View File

@ -49,11 +49,11 @@ func (p *Parser) decodePostProcessor(block *hcl.Block) (*PostProcessorBlock, hcl
return postProcessor, diags return postProcessor, diags
} }
func (p *Parser) startPostProcessor(source *SourceBlock, pp *PostProcessorBlock, ectx *hcl.EvalContext, generatedVars map[string]string) (packer.PostProcessor, hcl.Diagnostics) { func (cfg *PackerConfig) startPostProcessor(source *SourceBlock, pp *PostProcessorBlock, ectx *hcl.EvalContext, generatedVars map[string]string) (packer.PostProcessor, hcl.Diagnostics) {
// ProvisionerBlock represents a detected but unparsed provisioner // ProvisionerBlock represents a detected but unparsed provisioner
var diags hcl.Diagnostics var diags hcl.Diagnostics
postProcessor, err := p.PostProcessorsSchemas.Start(pp.PType) postProcessor, err := cfg.postProcessorsSchemas.Start(pp.PType)
if err != nil { if err != nil {
diags = append(diags, &hcl.Diagnostic{ diags = append(diags, &hcl.Diagnostic{
Summary: fmt.Sprintf("Failed loading %s", pp.PType), Summary: fmt.Sprintf("Failed loading %s", pp.PType),

View File

@ -77,10 +77,10 @@ func (p *Parser) decodeProvisioner(block *hcl.Block) (*ProvisionerBlock, hcl.Dia
return provisioner, diags return provisioner, diags
} }
func (p *Parser) startProvisioner(source *SourceBlock, pb *ProvisionerBlock, ectx *hcl.EvalContext, generatedVars map[string]string) (packer.Provisioner, hcl.Diagnostics) { func (cfg *PackerConfig) startProvisioner(source *SourceBlock, pb *ProvisionerBlock, ectx *hcl.EvalContext, generatedVars map[string]string) (packer.Provisioner, hcl.Diagnostics) {
var diags hcl.Diagnostics var diags hcl.Diagnostics
provisioner, err := p.ProvisionersSchemas.Start(pb.PType) provisioner, err := cfg.provisionersSchemas.Start(pb.PType)
if err != nil { if err != nil {
diags = append(diags, &hcl.Diagnostic{ diags = append(diags, &hcl.Diagnostic{
Summary: fmt.Sprintf("failed loading %s", pb.PType), Summary: fmt.Sprintf("failed loading %s", pb.PType),

View File

@ -31,6 +31,12 @@ type PackerConfig struct {
// Builds is the list of Build blocks defined in the config files. // Builds is the list of Build blocks defined in the config files.
Builds Builds Builds Builds
builderSchemas packer.BuilderStore
provisionersSchemas packer.ProvisionerStore
postProcessorsSchemas packer.PostProcessorStore
} }
type ValidationOptions struct { type ValidationOptions struct {
@ -188,11 +194,11 @@ func (c *PackerConfig) evaluateLocalVariable(local *Local) hcl.Diagnostics {
// getCoreBuildProvisioners takes a list of provisioner block, starts according // getCoreBuildProvisioners takes a list of provisioner block, starts according
// provisioners and sends parsed HCL2 over to it. // provisioners and sends parsed HCL2 over to it.
func (p *Parser) getCoreBuildProvisioners(source *SourceBlock, blocks []*ProvisionerBlock, ectx *hcl.EvalContext, generatedVars map[string]string) ([]packer.CoreBuildProvisioner, hcl.Diagnostics) { func (cfg *PackerConfig) getCoreBuildProvisioners(source *SourceBlock, blocks []*ProvisionerBlock, ectx *hcl.EvalContext, generatedVars map[string]string) ([]packer.CoreBuildProvisioner, hcl.Diagnostics) {
var diags hcl.Diagnostics var diags hcl.Diagnostics
res := []packer.CoreBuildProvisioner{} res := []packer.CoreBuildProvisioner{}
for _, pb := range blocks { for _, pb := range blocks {
provisioner, moreDiags := p.startProvisioner(source, pb, ectx, generatedVars) provisioner, moreDiags := cfg.startProvisioner(source, pb, ectx, generatedVars)
diags = append(diags, moreDiags...) diags = append(diags, moreDiags...)
if moreDiags.HasErrors() { if moreDiags.HasErrors() {
continue continue
@ -228,11 +234,11 @@ func (p *Parser) getCoreBuildProvisioners(source *SourceBlock, blocks []*Provisi
// getCoreBuildProvisioners takes a list of post processor block, starts // getCoreBuildProvisioners takes a list of post processor block, starts
// according provisioners and sends parsed HCL2 over to it. // according provisioners and sends parsed HCL2 over to it.
func (p *Parser) getCoreBuildPostProcessors(source *SourceBlock, blocks []*PostProcessorBlock, ectx *hcl.EvalContext, generatedVars map[string]string) ([]packer.CoreBuildPostProcessor, hcl.Diagnostics) { func (cfg *PackerConfig) getCoreBuildPostProcessors(source *SourceBlock, blocks []*PostProcessorBlock, ectx *hcl.EvalContext, generatedVars map[string]string) ([]packer.CoreBuildPostProcessor, hcl.Diagnostics) {
var diags hcl.Diagnostics var diags hcl.Diagnostics
res := []packer.CoreBuildPostProcessor{} res := []packer.CoreBuildPostProcessor{}
for _, ppb := range blocks { for _, ppb := range blocks {
postProcessor, moreDiags := p.startPostProcessor(source, ppb, ectx, generatedVars) postProcessor, moreDiags := cfg.startPostProcessor(source, ppb, ectx, generatedVars)
diags = append(diags, moreDiags...) diags = append(diags, moreDiags...)
if moreDiags.HasErrors() { if moreDiags.HasErrors() {
continue continue
@ -250,7 +256,7 @@ func (p *Parser) getCoreBuildPostProcessors(source *SourceBlock, blocks []*PostP
// getBuilds will return a list of packer Build based on the HCL2 parsed build // getBuilds will return a list of packer Build based on the HCL2 parsed build
// blocks. All Builders, Provisioners and Post Processors will be started and // blocks. All Builders, Provisioners and Post Processors will be started and
// configured. // configured.
func (p *Parser) getBuilds(cfg *PackerConfig, onlyGlobs []glob.Glob, exceptGlobs []glob.Glob) ([]packer.Build, hcl.Diagnostics) { func (cfg *PackerConfig) getBuilds(onlyGlobs []glob.Glob, exceptGlobs []glob.Glob) ([]packer.Build, hcl.Diagnostics) {
res := []packer.Build{} res := []packer.Build{}
var diags hcl.Diagnostics var diags hcl.Diagnostics
@ -297,7 +303,7 @@ func (p *Parser) getBuilds(cfg *PackerConfig, onlyGlobs []glob.Glob, exceptGlobs
} }
} }
builder, moreDiags, generatedVars := p.startBuilder(src, cfg.EvalContext(nil)) builder, moreDiags, generatedVars := cfg.startBuilder(src, cfg.EvalContext(nil))
diags = append(diags, moreDiags...) diags = append(diags, moreDiags...)
if moreDiags.HasErrors() { if moreDiags.HasErrors() {
continue continue
@ -323,12 +329,12 @@ func (p *Parser) getBuilds(cfg *PackerConfig, onlyGlobs []glob.Glob, exceptGlobs
} }
} }
provisioners, moreDiags := p.getCoreBuildProvisioners(src, build.ProvisionerBlocks, cfg.EvalContext(variables), generatedPlaceholderMap) provisioners, moreDiags := cfg.getCoreBuildProvisioners(src, build.ProvisionerBlocks, cfg.EvalContext(variables), generatedPlaceholderMap)
diags = append(diags, moreDiags...) diags = append(diags, moreDiags...)
if moreDiags.HasErrors() { if moreDiags.HasErrors() {
continue continue
} }
postProcessors, moreDiags := p.getCoreBuildPostProcessors(src, build.PostProcessors, cfg.EvalContext(variables), generatedPlaceholderMap) postProcessors, moreDiags := cfg.getCoreBuildPostProcessors(src, build.PostProcessors, cfg.EvalContext(variables), generatedPlaceholderMap)
pps := [][]packer.CoreBuildPostProcessor{} pps := [][]packer.CoreBuildPostProcessor{}
if len(postProcessors) > 0 { if len(postProcessors) > 0 {
pps = [][]packer.CoreBuildPostProcessor{postProcessors} pps = [][]packer.CoreBuildPostProcessor{postProcessors}
@ -404,6 +410,6 @@ func (p *Parser) Parse(path string, varFiles []string, argVars map[string]string
return nil, diags return nil, diags
} }
builds, moreDiags := p.getBuilds(cfg, onlyGlobs, exceptGlobs) builds, moreDiags := cfg.getBuilds(onlyGlobs, exceptGlobs)
return builds, append(diags, moreDiags...) return builds, append(diags, moreDiags...)
} }

View File

@ -38,10 +38,10 @@ func (p *Parser) decodeSource(block *hcl.Block) (*SourceBlock, hcl.Diagnostics)
return source, diags return source, diags
} }
func (p *Parser) startBuilder(source *SourceBlock, ectx *hcl.EvalContext) (packer.Builder, hcl.Diagnostics, []string) { func (cfg *PackerConfig) startBuilder(source *SourceBlock, ectx *hcl.EvalContext) (packer.Builder, hcl.Diagnostics, []string) {
var diags hcl.Diagnostics var diags hcl.Diagnostics
builder, err := p.BuilderSchemas.Start(source.Type) builder, err := cfg.builderSchemas.Start(source.Type)
if err != nil { if err != nil {
diags = append(diags, &hcl.Diagnostic{ diags = append(diags, &hcl.Diagnostic{
Summary: "Failed to load " + sourceLabel + " type", Summary: "Failed to load " + sourceLabel + " type",