2020-07-02 12:02:19 -04:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2020-07-06 05:38:12 -04:00
|
|
|
// HCL2Provisioner has a reference to the part of the HCL2 body where it is
|
|
|
|
// defined, allowing to completely reconfigure the Provisioner right before
|
|
|
|
// calling Provision: with contextual variables.
|
|
|
|
// This permits using "${build.ID}" values for example.
|
2020-07-02 12:02:19 -04:00
|
|
|
type HCL2Provisioner struct {
|
|
|
|
Provisioner packer.Provisioner
|
|
|
|
provisionerBlock *ProvisionerBlock
|
|
|
|
evalContext *hcl.EvalContext
|
|
|
|
builderVariables map[string]string
|
2020-08-13 13:16:13 -04:00
|
|
|
override map[string]interface{}
|
2020-07-02 12:02:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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{}
|
2020-10-16 04:57:42 -04:00
|
|
|
if !p.evalContext.Variables[buildAccessor].IsNull() {
|
|
|
|
buildValues = p.evalContext.Variables[buildAccessor].AsValueMap()
|
|
|
|
}
|
2020-07-02 12:02:19 -04:00
|
|
|
for k, v := range buildVars {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case string:
|
|
|
|
buildValues[k] = cty.StringVal(v)
|
2020-07-10 05:01:10 -04:00
|
|
|
case int64:
|
|
|
|
buildValues[k] = cty.NumberIntVal(v)
|
2020-07-13 06:36:02 -04:00
|
|
|
case uint64:
|
|
|
|
buildValues[k] = cty.NumberUIntVal(v)
|
2020-07-30 14:25:44 -04:00
|
|
|
case bool:
|
|
|
|
buildValues[k] = cty.BoolVal(v)
|
2020-07-02 12:02:19 -04:00
|
|
|
default:
|
2020-07-07 10:50:15 -04:00
|
|
|
return fmt.Errorf("unhandled buildvar type: %T", v)
|
2020-07-02 12:02:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2020-08-13 13:16:13 -04:00
|
|
|
return p.Provisioner.Prepare(p.builderVariables, flatProvisionerCfg, p.override)
|
2020-07-02 12:02:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|