62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package hcl2template
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
|
"github.com/hashicorp/packer/packer"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
type HCL2Provisioner struct {
|
|
Provisioner packer.Provisioner
|
|
provisionerBlock *ProvisionerBlock
|
|
evalContext *hcl.EvalContext
|
|
builderVariables map[string]string
|
|
}
|
|
|
|
func (p *HCL2Provisioner) ConfigSpec() hcldec.ObjectSpec {
|
|
return p.Provisioner.ConfigSpec()
|
|
}
|
|
|
|
func (p *HCL2Provisioner) HCL2Prepare(buildVars map[string]interface{}) error {
|
|
var diags hcl.Diagnostics
|
|
ectx := p.evalContext
|
|
if len(buildVars) > 0 {
|
|
ectx = p.evalContext.NewChild()
|
|
buildValues := map[string]cty.Value{}
|
|
for k, v := range buildVars {
|
|
switch v := v.(type) {
|
|
case string:
|
|
buildValues[k] = cty.StringVal(v)
|
|
default:
|
|
return fmt.Errorf("unhandled builvar type: %T", v)
|
|
}
|
|
}
|
|
ectx.Variables = map[string]cty.Value{
|
|
buildAccessor: cty.ObjectVal(buildValues),
|
|
}
|
|
}
|
|
|
|
flatProvisionerCfg, moreDiags := decodeHCL2Spec(p.provisionerBlock.HCL2Ref.Rest, ectx, p.Provisioner)
|
|
diags = append(diags, moreDiags...)
|
|
if diags.HasErrors() {
|
|
return diags
|
|
}
|
|
return p.Provisioner.Prepare(p.builderVariables, flatProvisionerCfg)
|
|
}
|
|
|
|
func (p *HCL2Provisioner) Prepare(args ...interface{}) error {
|
|
return p.Provisioner.Prepare(args...)
|
|
}
|
|
|
|
func (p *HCL2Provisioner) Provision(ctx context.Context, ui packer.Ui, c packer.Communicator, vars map[string]interface{}) error {
|
|
err := p.HCL2Prepare(vars)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return p.Provisioner.Provision(ctx, ui, c, vars)
|
|
}
|