2019-10-14 11:02:53 -04:00
|
|
|
package hcl2template
|
|
|
|
|
|
|
|
import (
|
2019-12-17 05:25:56 -05:00
|
|
|
"fmt"
|
2019-10-14 11:02:53 -04:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
// ProvisionerBlock represents a parsed provisioner
|
|
|
|
type ProvisionerBlock struct {
|
|
|
|
PType string
|
|
|
|
block *hcl.Block
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *Parser) decodeProvisioner(block *hcl.Block) (*ProvisionerBlock, hcl.Diagnostics) {
|
|
|
|
provisioner := &ProvisionerBlock{
|
|
|
|
PType: block.Labels[0],
|
|
|
|
block: block,
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
|
|
|
if !p.ProvisionersSchemas.Has(provisioner.PType) {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Summary: "Unknown " + buildProvisionerLabel + " type " + provisioner.PType,
|
|
|
|
Subject: block.LabelRanges[0].Ptr(),
|
|
|
|
Detail: fmt.Sprintf("known provisioners: %v", p.ProvisionersSchemas.List()),
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
})
|
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
return provisioner, diags
|
|
|
|
}
|
2019-10-14 11:02:53 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *Parser) StartProvisioner(pb *ProvisionerBlock) (packer.Provisioner, hcl.Diagnostics) {
|
|
|
|
var diags hcl.Diagnostics
|
2019-10-14 11:02:53 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
provisioner, err := p.ProvisionersSchemas.Start(pb.PType)
|
|
|
|
if err != nil {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Summary: "Failed loading " + pb.block.Type,
|
|
|
|
Subject: pb.block.LabelRanges[0].Ptr(),
|
|
|
|
Detail: err.Error(),
|
2019-10-14 11:02:53 -04:00
|
|
|
})
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, diags
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
flatProvisinerCfg, moreDiags := decodeHCL2Spec(pb.block, nil, provisioner)
|
2019-10-14 11:02:53 -04:00
|
|
|
diags = append(diags, moreDiags...)
|
2019-12-17 05:25:56 -05:00
|
|
|
if diags.HasErrors() {
|
|
|
|
return nil, diags
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
err = provisioner.Prepare(flatProvisinerCfg)
|
|
|
|
if err != nil {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Failed preparing " + pb.block.Type,
|
|
|
|
Detail: err.Error(),
|
|
|
|
Subject: pb.block.DefRange.Ptr(),
|
|
|
|
})
|
|
|
|
return nil, diags
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
return provisioner, diags
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|