2019-10-14 11:02:53 -04:00
|
|
|
package hcl2template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2019-10-14 11:02:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// PackerConfig represents a loaded packer config
|
|
|
|
type PackerConfig struct {
|
|
|
|
Sources map[SourceRef]*Source
|
|
|
|
|
|
|
|
Variables PackerV1Variables
|
|
|
|
|
|
|
|
Builds Builds
|
|
|
|
}
|
|
|
|
|
2019-12-17 12:42:15 -05:00
|
|
|
func (p *Parser) CoreBuildProvisioners(blocks []*ProvisionerBlock, generatedVars []string) ([]packer.CoreBuildProvisioner, hcl.Diagnostics) {
|
2019-12-17 05:25:56 -05:00
|
|
|
var diags hcl.Diagnostics
|
|
|
|
res := []packer.CoreBuildProvisioner{}
|
|
|
|
for _, pb := range blocks {
|
2019-12-17 12:42:15 -05:00
|
|
|
provisioner, moreDiags := p.StartProvisioner(pb, generatedVars)
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res = append(res, packer.CoreBuildProvisioner{
|
|
|
|
PType: pb.PType,
|
2020-01-16 06:08:39 -05:00
|
|
|
PName: pb.PName,
|
2019-12-17 05:25:56 -05:00
|
|
|
Provisioner: provisioner,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return res, diags
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *Parser) CoreBuildPostProcessors(blocks []*PostProcessorBlock) ([]packer.CoreBuildPostProcessor, hcl.Diagnostics) {
|
2019-10-14 11:02:53 -04:00
|
|
|
var diags hcl.Diagnostics
|
2019-12-17 05:25:56 -05:00
|
|
|
res := []packer.CoreBuildPostProcessor{}
|
2020-01-16 06:08:39 -05:00
|
|
|
for _, ppb := range blocks {
|
|
|
|
postProcessor, moreDiags := p.StartPostProcessor(ppb)
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res = append(res, packer.CoreBuildPostProcessor{
|
|
|
|
PostProcessor: postProcessor,
|
2020-01-16 06:08:39 -05:00
|
|
|
PName: ppb.PName,
|
|
|
|
PType: ppb.PType,
|
2019-12-17 05:25:56 -05:00
|
|
|
})
|
|
|
|
}
|
2019-12-17 19:13:21 -05:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
return res, diags
|
|
|
|
}
|
2019-10-14 11:02:53 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *Parser) getBuilds(cfg *PackerConfig) ([]packer.Build, hcl.Diagnostics) {
|
|
|
|
res := []packer.Build{}
|
|
|
|
var diags hcl.Diagnostics
|
2019-10-14 11:02:53 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
for _, build := range cfg.Builds {
|
2019-10-14 11:02:53 -04:00
|
|
|
for _, from := range build.Froms {
|
2019-12-17 05:25:56 -05:00
|
|
|
src, found := cfg.Sources[from]
|
2019-10-14 11:02:53 -04:00
|
|
|
if !found {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
2019-12-17 05:25:56 -05:00
|
|
|
Summary: "Unknown " + sourceLabel + " " + from.String(),
|
2020-01-16 06:08:39 -05:00
|
|
|
Subject: build.HCL2Ref.DefRange.Ptr(),
|
2019-10-14 11:02:53 -04:00
|
|
|
Severity: hcl.DiagError,
|
|
|
|
})
|
2019-12-17 05:25:56 -05:00
|
|
|
continue
|
|
|
|
}
|
2019-12-17 12:42:15 -05:00
|
|
|
builder, moreDiags, generatedVars := p.StartBuilder(src)
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-17 12:42:15 -05:00
|
|
|
provisioners, moreDiags := p.CoreBuildProvisioners(build.ProvisionerBlocks, generatedVars)
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
postProcessors, moreDiags := p.CoreBuildPostProcessors(build.PostProcessors)
|
2019-12-17 19:13:21 -05:00
|
|
|
pps := [][]packer.CoreBuildPostProcessor{}
|
|
|
|
if len(postProcessors) > 0 {
|
|
|
|
pps = [][]packer.CoreBuildPostProcessor{postProcessors}
|
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
2019-10-14 11:02:53 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
pcb := &packer.CoreBuild{
|
|
|
|
Type: src.Type,
|
|
|
|
Builder: builder,
|
|
|
|
Provisioners: provisioners,
|
2019-12-17 19:13:21 -05:00
|
|
|
PostProcessors: pps,
|
2019-12-17 05:25:56 -05:00
|
|
|
Variables: cfg.Variables,
|
|
|
|
}
|
|
|
|
res = append(res, pcb)
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
return res, diags
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *Parser) Parse(path string) ([]packer.Build, hcl.Diagnostics) {
|
|
|
|
cfg, diags := p.parse(path)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
return nil, diags
|
|
|
|
}
|
2019-10-14 11:02:53 -04:00
|
|
|
|
2020-01-06 08:31:15 -05:00
|
|
|
builds, moreDiags := p.getBuilds(cfg)
|
|
|
|
return builds, append(diags, moreDiags...)
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|