mirror of https://github.com/hashicorp/packer
parent
db6c3adbba
commit
39261f3671
@ -0,0 +1,68 @@
|
||||
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 HCL2PostProcessor struct {
|
||||
PostProcessor packer.PostProcessor
|
||||
postProcessorBlock *PostProcessorBlock
|
||||
evalContext *hcl.EvalContext
|
||||
builderVariables map[string]string
|
||||
}
|
||||
|
||||
func (p *HCL2PostProcessor) ConfigSpec() hcldec.ObjectSpec {
|
||||
return p.PostProcessor.ConfigSpec()
|
||||
}
|
||||
|
||||
func (p *HCL2PostProcessor) 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),
|
||||
}
|
||||
}
|
||||
|
||||
flatPostProcessorCfg, moreDiags := decodeHCL2Spec(p.postProcessorBlock.HCL2Ref.Rest, ectx, p.PostProcessor)
|
||||
diags = append(diags, moreDiags...)
|
||||
if diags.HasErrors() {
|
||||
return diags
|
||||
}
|
||||
return p.PostProcessor.Configure(p.builderVariables, flatPostProcessorCfg)
|
||||
}
|
||||
|
||||
func (p *HCL2PostProcessor) Configure(args ...interface{}) error {
|
||||
return p.PostProcessor.Configure(args...)
|
||||
}
|
||||
|
||||
func (p *HCL2PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
|
||||
generatedData := make(map[string]interface{})
|
||||
if artifactStateData, ok := artifact.State("generated_data").(map[interface{}]interface{}); ok {
|
||||
for k, v := range artifactStateData {
|
||||
generatedData[k.(string)] = v
|
||||
}
|
||||
}
|
||||
|
||||
err := p.HCL2Prepare(generatedData)
|
||||
if err != nil {
|
||||
return nil, false, false, err
|
||||
}
|
||||
return p.PostProcessor.PostProcess(ctx, ui, artifact)
|
||||
}
|
||||
Loading…
Reference in new issue