2019-10-14 11:02:53 -04:00
|
|
|
package hcl2template
|
|
|
|
|
|
|
|
import (
|
2019-12-17 05:25:56 -05:00
|
|
|
"fmt"
|
2021-02-05 04:57:14 -05:00
|
|
|
"strconv"
|
2020-04-16 05:58:54 -04:00
|
|
|
"time"
|
2020-01-16 06:08:39 -05:00
|
|
|
|
2019-10-14 11:02:53 -04:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2020-01-16 06:08:39 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/gohcl"
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2020-08-13 13:16:13 -04:00
|
|
|
hcl2shim "github.com/hashicorp/packer/hcl2template/shim"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
2019-10-14 11:02:53 -04:00
|
|
|
)
|
|
|
|
|
2020-06-05 05:51:23 -04:00
|
|
|
// OnlyExcept is a struct that is meant to be embedded that contains the
|
|
|
|
// logic required for "only" and "except" meta-parameters.
|
|
|
|
type OnlyExcept struct {
|
|
|
|
Only []string `json:"only,omitempty"`
|
|
|
|
Except []string `json:"except,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip says whether or not to skip the build with the given name.
|
|
|
|
func (o *OnlyExcept) Skip(n string) bool {
|
|
|
|
if len(o.Only) > 0 {
|
|
|
|
for _, v := range o.Only {
|
|
|
|
if v == n {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(o.Except) > 0 {
|
|
|
|
for _, v := range o.Except {
|
|
|
|
if v == n {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates that the OnlyExcept settings are correct for a thing.
|
|
|
|
func (o *OnlyExcept) Validate() hcl.Diagnostics {
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
|
|
|
if len(o.Only) > 0 && len(o.Except) > 0 {
|
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
|
|
Summary: "only one of 'only' or 'except' may be specified",
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
Hcl2 input variables, local variables and functions (#8588)
Mainly redefine or reused what Terraform did.
* allow to used `variables`, `variable` and `local` blocks
* import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
2020-02-06 05:49:21 -05:00
|
|
|
// ProvisionerBlock references a detected but unparsed provisioner
|
2019-12-17 05:25:56 -05:00
|
|
|
type ProvisionerBlock struct {
|
2020-04-16 05:58:54 -04:00
|
|
|
PType string
|
|
|
|
PName string
|
|
|
|
PauseBefore time.Duration
|
|
|
|
MaxRetries int
|
|
|
|
Timeout time.Duration
|
2020-08-13 13:16:13 -04:00
|
|
|
Override map[string]interface{}
|
2020-06-05 05:51:23 -04:00
|
|
|
OnlyExcept OnlyExcept
|
2020-01-16 06:08:39 -05:00
|
|
|
HCL2Ref
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProvisionerBlock) String() string {
|
|
|
|
return fmt.Sprintf(buildProvisionerLabel+"-block %q %q", p.PType, p.PName)
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
|
2020-08-05 11:41:20 -04:00
|
|
|
func (p *Parser) decodeProvisioner(block *hcl.Block, cfg *PackerConfig) (*ProvisionerBlock, hcl.Diagnostics) {
|
2020-01-16 06:08:39 -05:00
|
|
|
var b struct {
|
2020-08-13 13:16:13 -04:00
|
|
|
Name string `hcl:"name,optional"`
|
|
|
|
PauseBefore string `hcl:"pause_before,optional"`
|
|
|
|
MaxRetries int `hcl:"max_retries,optional"`
|
|
|
|
Timeout string `hcl:"timeout,optional"`
|
|
|
|
Only []string `hcl:"only,optional"`
|
|
|
|
Except []string `hcl:"except,optional"`
|
|
|
|
Override cty.Value `hcl:"override,optional"`
|
|
|
|
Rest hcl.Body `hcl:",remain"`
|
2020-01-16 06:08:39 -05:00
|
|
|
}
|
2021-02-04 05:25:44 -05:00
|
|
|
diags := gohcl.DecodeBody(block.Body, cfg.EvalContext(BuildContext, nil), &b)
|
2020-01-16 06:08:39 -05:00
|
|
|
if diags.HasErrors() {
|
|
|
|
return nil, diags
|
|
|
|
}
|
2020-04-16 05:58:54 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
provisioner := &ProvisionerBlock{
|
2020-04-16 05:58:54 -04:00
|
|
|
PType: block.Labels[0],
|
|
|
|
PName: b.Name,
|
|
|
|
MaxRetries: b.MaxRetries,
|
2020-06-05 05:51:23 -04:00
|
|
|
OnlyExcept: OnlyExcept{Only: b.Only, Except: b.Except},
|
2020-04-16 05:58:54 -04:00
|
|
|
HCL2Ref: newHCL2Ref(block, b.Rest),
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:51:23 -04:00
|
|
|
diags = diags.Extend(provisioner.OnlyExcept.Validate())
|
|
|
|
if diags.HasErrors() {
|
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
|
2020-08-13 13:16:13 -04:00
|
|
|
if !b.Override.IsNull() {
|
|
|
|
override := make(map[string]interface{})
|
|
|
|
for buildName, overrides := range b.Override.AsValueMap() {
|
|
|
|
buildOverrides := make(map[string]interface{})
|
|
|
|
for option, value := range overrides.AsValueMap() {
|
|
|
|
buildOverrides[option] = hcl2shim.ConfigValueFromHCL2(value)
|
|
|
|
}
|
|
|
|
override[buildName] = buildOverrides
|
|
|
|
}
|
|
|
|
provisioner.Override = override
|
|
|
|
}
|
|
|
|
|
2020-04-16 05:58:54 -04:00
|
|
|
if b.PauseBefore != "" {
|
|
|
|
pauseBefore, err := time.ParseDuration(b.PauseBefore)
|
|
|
|
if err != nil {
|
|
|
|
return nil, append(diags, &hcl.Diagnostic{
|
2021-04-26 11:37:24 -04:00
|
|
|
Summary: "Failed to parse pause_before duration",
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Detail: err.Error(),
|
2020-04-16 05:58:54 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
provisioner.PauseBefore = pauseBefore
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Timeout != "" {
|
|
|
|
timeout, err := time.ParseDuration(b.Timeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, append(diags, &hcl.Diagnostic{
|
|
|
|
Summary: "Failed to parse timeout duration",
|
|
|
|
Detail: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
provisioner.Timeout = timeout
|
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
|
|
|
|
2021-02-02 12:05:04 -05:00
|
|
|
func (cfg *PackerConfig) startProvisioner(source SourceUseBlock, pb *ProvisionerBlock, ectx *hcl.EvalContext) (packersdk.Provisioner, hcl.Diagnostics) {
|
2019-12-17 05:25:56 -05:00
|
|
|
var diags hcl.Diagnostics
|
2019-10-14 11:02:53 -04:00
|
|
|
|
2021-02-02 12:05:04 -05:00
|
|
|
provisioner, err := cfg.parser.PluginConfig.Provisioners.Start(pb.PType)
|
2019-12-17 05:25:56 -05:00
|
|
|
if err != nil {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
2021-02-05 14:06:52 -05:00
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: fmt.Sprintf("failed loading %s", pb.PType),
|
|
|
|
Subject: pb.HCL2Ref.LabelsRanges[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
|
|
|
}
|
2020-08-13 13:16:13 -04:00
|
|
|
|
2021-02-05 04:57:14 -05:00
|
|
|
builderVars := source.builderVariables()
|
|
|
|
builderVars["packer_debug"] = strconv.FormatBool(cfg.debug)
|
|
|
|
builderVars["packer_force"] = strconv.FormatBool(cfg.force)
|
|
|
|
builderVars["packer_on_error"] = cfg.onError
|
|
|
|
|
2020-07-06 04:32:39 -04:00
|
|
|
hclProvisioner := &HCL2Provisioner{
|
2020-07-02 12:02:19 -04:00
|
|
|
Provisioner: provisioner,
|
|
|
|
provisionerBlock: pb,
|
|
|
|
evalContext: ectx,
|
2021-02-05 04:57:14 -05:00
|
|
|
builderVariables: builderVars,
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
2020-08-13 13:16:13 -04:00
|
|
|
|
|
|
|
if pb.Override != nil {
|
|
|
|
if override, ok := pb.Override[source.name()]; ok {
|
|
|
|
hclProvisioner.override = override.(map[string]interface{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:14:17 -04:00
|
|
|
err = hclProvisioner.HCL2Prepare(nil)
|
2019-12-17 05:25:56 -05:00
|
|
|
if err != nil {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2020-01-16 06:08:39 -05:00
|
|
|
Summary: fmt.Sprintf("Failed preparing %s", pb),
|
2019-12-17 05:25:56 -05:00
|
|
|
Detail: err.Error(),
|
2020-01-16 06:08:39 -05:00
|
|
|
Subject: pb.HCL2Ref.DefRange.Ptr(),
|
2019-12-17 05:25:56 -05:00
|
|
|
})
|
|
|
|
return nil, diags
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
2020-07-06 04:32:39 -04:00
|
|
|
return hclProvisioner, diags
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|