2019-10-14 11:02:53 -04:00
|
|
|
package hcl2template
|
|
|
|
|
|
|
|
import (
|
2020-02-14 11:39:32 -05:00
|
|
|
"fmt"
|
2020-06-05 11:23:54 -04:00
|
|
|
"strings"
|
2020-03-04 06:30:05 -05:00
|
|
|
|
2020-06-09 09:29:32 -04:00
|
|
|
"github.com/gobwas/glob"
|
2019-10-14 11:02:53 -04:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2020-06-05 11:23:54 -04:00
|
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
2020-07-02 09:07:29 -04:00
|
|
|
"github.com/hashicorp/packer/helper/common"
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
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
|
|
|
"github.com/zclconf/go-cty/cty"
|
2019-10-14 11:02:53 -04:00
|
|
|
)
|
|
|
|
|
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
|
|
|
// PackerConfig represents a loaded Packer HCL config. It will contain
|
|
|
|
// references to all possible blocks of the allowed configuration.
|
2019-10-14 11:02:53 -04:00
|
|
|
type PackerConfig struct {
|
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
|
|
|
// Directory where the config files are defined
|
|
|
|
Basedir string
|
2019-10-14 11:02:53 -04:00
|
|
|
|
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
|
|
|
// Available Source blocks
|
2020-05-25 11:09:37 -04:00
|
|
|
Sources map[SourceRef]SourceBlock
|
2019-10-14 11:02:53 -04:00
|
|
|
|
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
|
|
|
// InputVariables and LocalVariables are the list of defined input and
|
|
|
|
// local variables. They are of the same type but are not used in the same
|
|
|
|
// way. Local variables will not be decoded from any config file, env var,
|
|
|
|
// or ect. Like the Input variables will.
|
|
|
|
InputVariables Variables
|
|
|
|
LocalVariables Variables
|
|
|
|
|
2020-03-09 11:16:59 -04:00
|
|
|
ValidationOptions
|
|
|
|
|
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
|
|
|
// Builds is the list of Build blocks defined in the config files.
|
2019-10-14 11:02:53 -04:00
|
|
|
Builds Builds
|
2020-04-29 10:15:42 -04:00
|
|
|
|
|
|
|
builderSchemas packer.BuilderStore
|
|
|
|
|
|
|
|
provisionersSchemas packer.ProvisionerStore
|
|
|
|
|
|
|
|
postProcessorsSchemas packer.PostProcessorStore
|
2020-06-09 09:23:29 -04:00
|
|
|
|
|
|
|
except []glob.Glob
|
|
|
|
only []glob.Glob
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
|
2020-03-09 11:16:59 -04:00
|
|
|
type ValidationOptions struct {
|
|
|
|
Strict bool
|
|
|
|
}
|
|
|
|
|
2020-04-09 05:14:37 -04:00
|
|
|
const (
|
|
|
|
inputVariablesAccessor = "var"
|
|
|
|
localsAccessor = "local"
|
|
|
|
sourcesAccessor = "source"
|
2020-07-02 09:56:49 -04:00
|
|
|
buildAccessor = "build"
|
2020-04-09 05:14:37 -04:00
|
|
|
)
|
|
|
|
|
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
|
|
|
// EvalContext returns the *hcl.EvalContext that will be passed to an hcl
|
|
|
|
// decoder in order to tell what is the actual value of a var or a local and
|
|
|
|
// the list of defined functions.
|
2020-04-09 05:14:37 -04:00
|
|
|
func (cfg *PackerConfig) EvalContext(variables map[string]cty.Value) *hcl.EvalContext {
|
2020-03-09 12:25:56 -04:00
|
|
|
inputVariables, _ := cfg.InputVariables.Values()
|
|
|
|
localVariables, _ := cfg.LocalVariables.Values()
|
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
|
|
|
ectx := &hcl.EvalContext{
|
|
|
|
Functions: Functions(cfg.Basedir),
|
|
|
|
Variables: map[string]cty.Value{
|
2020-04-09 05:14:37 -04:00
|
|
|
inputVariablesAccessor: cty.ObjectVal(inputVariables),
|
|
|
|
localsAccessor: cty.ObjectVal(localVariables),
|
|
|
|
sourcesAccessor: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"type": cty.UnknownVal(cty.String),
|
|
|
|
"name": cty.UnknownVal(cty.String),
|
|
|
|
}),
|
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
|
|
|
},
|
|
|
|
}
|
2020-04-09 05:14:37 -04:00
|
|
|
for k, v := range variables {
|
|
|
|
ectx.Variables[k] = v
|
|
|
|
}
|
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
|
|
|
return ectx
|
|
|
|
}
|
|
|
|
|
2020-02-21 06:12:30 -05:00
|
|
|
// decodeInputVariables looks in the found blocks for 'variables' and
|
|
|
|
// 'variable' blocks. It should be called firsthand so that other blocks can
|
|
|
|
// use the variables.
|
|
|
|
func (c *PackerConfig) decodeInputVariables(f *hcl.File) hcl.Diagnostics {
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
|
|
|
content, moreDiags := f.Body.Content(configSchema)
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
|
|
|
|
for _, block := range content.Blocks {
|
|
|
|
switch block.Type {
|
|
|
|
case variableLabel:
|
|
|
|
moreDiags := c.InputVariables.decodeVariableBlock(block, nil)
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
case variablesLabel:
|
|
|
|
attrs, moreDiags := block.Body.JustAttributes()
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
for key, attr := range attrs {
|
|
|
|
moreDiags = c.InputVariables.decodeVariable(key, attr, nil)
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseLocalVariables looks in the found blocks for 'locals' blocks. It
|
|
|
|
// should be called after parsing input variables so that they can be
|
|
|
|
// referenced.
|
|
|
|
func (c *PackerConfig) parseLocalVariables(f *hcl.File) ([]*Local, hcl.Diagnostics) {
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
|
|
|
content, moreDiags := f.Body.Content(configSchema)
|
|
|
|
diags = append(diags, moreDiags...)
|
2020-03-04 07:00:51 -05:00
|
|
|
var locals []*Local
|
2020-02-21 06:12:30 -05:00
|
|
|
|
|
|
|
for _, block := range content.Blocks {
|
|
|
|
switch block.Type {
|
|
|
|
case localsLabel:
|
|
|
|
attrs, moreDiags := block.Body.JustAttributes()
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
for name, attr := range attrs {
|
|
|
|
if _, found := c.LocalVariables[name]; found {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2020-03-04 07:00:51 -05:00
|
|
|
Summary: "Duplicate value in " + localsLabel,
|
|
|
|
Detail: "Duplicate " + name + " definition found.",
|
2020-02-21 06:12:30 -05:00
|
|
|
Subject: attr.NameRange.Ptr(),
|
|
|
|
Context: block.DefRange.Ptr(),
|
|
|
|
})
|
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
locals = append(locals, &Local{
|
|
|
|
Name: name,
|
|
|
|
Expr: attr.Expr,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 07:00:51 -05:00
|
|
|
return locals, diags
|
2020-02-21 06:12:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PackerConfig) evaluateLocalVariables(locals []*Local) hcl.Diagnostics {
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
|
|
|
if len(locals) > 0 && c.LocalVariables == nil {
|
|
|
|
c.LocalVariables = Variables{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var retry, previousL int
|
|
|
|
for len(locals) > 0 {
|
|
|
|
local := locals[0]
|
|
|
|
moreDiags := c.evaluateLocalVariable(local)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
if len(locals) == 1 {
|
|
|
|
// If this is the only local left there's no need
|
|
|
|
// to try evaluating again
|
|
|
|
return append(diags, moreDiags...)
|
|
|
|
}
|
|
|
|
if previousL == len(locals) {
|
|
|
|
if retry == 100 {
|
|
|
|
// To get to this point, locals must have a circle dependency
|
|
|
|
return append(diags, moreDiags...)
|
|
|
|
}
|
|
|
|
retry++
|
|
|
|
}
|
|
|
|
previousL = len(locals)
|
|
|
|
|
|
|
|
// If local uses another local that has not been evaluated yet this could be the reason of errors
|
|
|
|
// Push local to the end of slice to be evaluated later
|
|
|
|
locals = append(locals, local)
|
|
|
|
} else {
|
|
|
|
retry = 0
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
}
|
|
|
|
// Remove local from slice
|
|
|
|
locals = append(locals[:0], locals[1:]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PackerConfig) evaluateLocalVariable(local *Local) hcl.Diagnostics {
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
2020-04-09 05:14:37 -04:00
|
|
|
value, moreDiags := local.Expr.Value(c.EvalContext(nil))
|
2020-02-21 06:12:30 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
c.LocalVariables[local.Name] = &Variable{
|
2020-03-04 07:01:04 -05:00
|
|
|
Name: local.Name,
|
2020-02-21 06:12:30 -05:00
|
|
|
DefaultValue: value,
|
|
|
|
Type: value.Type(),
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
// getCoreBuildProvisioners takes a list of provisioner block, starts according
|
|
|
|
// provisioners and sends parsed HCL2 over to it.
|
2020-07-02 12:02:19 -04:00
|
|
|
func (cfg *PackerConfig) getCoreBuildProvisioners(source SourceBlock, blocks []*ProvisionerBlock, ectx *hcl.EvalContext, generatedVars map[string]interface{}) ([]packer.CoreBuildProvisioner, hcl.Diagnostics) {
|
2019-12-17 05:25:56 -05:00
|
|
|
var diags hcl.Diagnostics
|
|
|
|
res := []packer.CoreBuildProvisioner{}
|
|
|
|
for _, pb := range blocks {
|
2020-07-02 05:07:59 -04:00
|
|
|
if pb.OnlyExcept.Skip(source.String()) {
|
2020-06-05 05:51:23 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-07-02 09:07:29 -04:00
|
|
|
provisioner, moreDiags := cfg.startProvisioner(source, pb, ectx, generatedVars)
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-02 09:07:29 -04:00
|
|
|
|
2020-04-16 05:58:54 -04:00
|
|
|
// If we're pausing, we wrap the provisioner in a special pauser.
|
|
|
|
if pb.PauseBefore != 0 {
|
|
|
|
provisioner = &packer.PausedProvisioner{
|
|
|
|
PauseBefore: pb.PauseBefore,
|
|
|
|
Provisioner: provisioner,
|
|
|
|
}
|
|
|
|
} else if pb.Timeout != 0 {
|
|
|
|
provisioner = &packer.TimeoutProvisioner{
|
|
|
|
Timeout: pb.Timeout,
|
|
|
|
Provisioner: provisioner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pb.MaxRetries != 0 {
|
|
|
|
provisioner = &packer.RetriedProvisioner{
|
|
|
|
MaxRetries: pb.MaxRetries,
|
|
|
|
Provisioner: provisioner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
res = append(res, packer.CoreBuildProvisioner{
|
|
|
|
PType: pb.PType,
|
2020-01-16 06:08:39 -05:00
|
|
|
PName: pb.PName,
|
2019-12-17 05:25:56 -05:00
|
|
|
Provisioner: provisioner,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return res, diags
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
// getCoreBuildProvisioners takes a list of post processor block, starts
|
|
|
|
// according provisioners and sends parsed HCL2 over to it.
|
2020-07-02 12:02:19 -04:00
|
|
|
func (cfg *PackerConfig) getCoreBuildPostProcessors(source SourceBlock, blocks []*PostProcessorBlock, ectx *hcl.EvalContext, generatedVars map[string]interface{}) ([]packer.CoreBuildPostProcessor, hcl.Diagnostics) {
|
2019-10-14 11:02:53 -04:00
|
|
|
var diags hcl.Diagnostics
|
2019-12-17 05:25:56 -05:00
|
|
|
res := []packer.CoreBuildPostProcessor{}
|
2020-01-16 06:08:39 -05:00
|
|
|
for _, ppb := range blocks {
|
2020-07-02 05:07:59 -04:00
|
|
|
if ppb.OnlyExcept.Skip(source.String()) {
|
2020-06-05 05:51:23 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-06-09 09:23:29 -04:00
|
|
|
|
|
|
|
name := ppb.PName
|
|
|
|
if name == "" {
|
|
|
|
name = ppb.PType
|
|
|
|
}
|
|
|
|
// -except
|
|
|
|
exclude := false
|
|
|
|
for _, exceptGlob := range cfg.except {
|
|
|
|
if exceptGlob.Match(name) {
|
|
|
|
exclude = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if exclude {
|
2020-06-09 11:35:53 -04:00
|
|
|
break
|
2020-06-09 09:23:29 -04:00
|
|
|
}
|
|
|
|
|
2020-07-02 09:07:29 -04:00
|
|
|
postProcessor, moreDiags := cfg.startPostProcessor(source, ppb, ectx, generatedVars)
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res = append(res, packer.CoreBuildPostProcessor{
|
2020-06-25 03:36:48 -04:00
|
|
|
PostProcessor: postProcessor,
|
|
|
|
PName: ppb.PName,
|
|
|
|
PType: ppb.PType,
|
|
|
|
KeepInputArtifact: ppb.KeepInputArtifact,
|
2019-12-17 05:25:56 -05:00
|
|
|
})
|
|
|
|
}
|
2019-12-17 19:13:21 -05:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
return res, diags
|
|
|
|
}
|
2019-10-14 11:02:53 -04:00
|
|
|
|
2020-04-29 10:36:40 -04:00
|
|
|
// GetBuilds returns a list of packer Build based on the HCL2 parsed build
|
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
|
|
|
// blocks. All Builders, Provisioners and Post Processors will be started and
|
|
|
|
// configured.
|
2020-05-08 10:41:47 -04:00
|
|
|
func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packer.Build, hcl.Diagnostics) {
|
2019-12-17 05:25:56 -05:00
|
|
|
res := []packer.Build{}
|
|
|
|
var diags hcl.Diagnostics
|
2019-10-14 11:02:53 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
for _, build := range cfg.Builds {
|
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
|
|
|
for _, from := range build.Sources {
|
2020-05-25 11:09:37 -04:00
|
|
|
src, found := cfg.Sources[from.Ref()]
|
2019-10-14 11:02:53 -04:00
|
|
|
if !found {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
2019-12-17 05:25:56 -05:00
|
|
|
Summary: "Unknown " + sourceLabel + " " + from.String(),
|
2020-01-16 06:08:39 -05:00
|
|
|
Subject: build.HCL2Ref.DefRange.Ptr(),
|
2019-10-14 11:02:53 -04:00
|
|
|
Severity: hcl.DiagError,
|
2020-05-25 11:09:37 -04:00
|
|
|
Detail: fmt.Sprintf("Known: %v", cfg.Sources),
|
2019-10-14 11:02:53 -04:00
|
|
|
})
|
2019-12-17 05:25:56 -05:00
|
|
|
continue
|
|
|
|
}
|
2020-05-25 11:09:37 -04:00
|
|
|
src.addition = from.addition
|
2020-07-02 05:07:59 -04:00
|
|
|
src.LocalName = from.LocalName
|
2020-04-28 09:03:24 -04:00
|
|
|
|
2020-06-23 04:53:16 -04:00
|
|
|
pcb := &packer.CoreBuild{
|
|
|
|
BuildName: build.Name,
|
2020-07-02 05:07:59 -04:00
|
|
|
Type: src.String(),
|
2020-06-23 04:53:16 -04:00
|
|
|
}
|
2020-04-28 09:03:24 -04:00
|
|
|
|
2020-06-23 04:53:16 -04:00
|
|
|
// Apply the -only and -except command-line options to exclude matching builds.
|
|
|
|
buildName := pcb.Name()
|
2020-04-28 09:03:24 -04:00
|
|
|
// -only
|
2020-05-08 10:41:47 -04:00
|
|
|
if len(opts.Only) > 0 {
|
|
|
|
onlyGlobs, diags := convertFilterOption(opts.Only, "only")
|
2020-04-29 10:36:40 -04:00
|
|
|
if diags.HasErrors() {
|
|
|
|
return nil, diags
|
|
|
|
}
|
2020-06-09 09:23:29 -04:00
|
|
|
cfg.only = onlyGlobs
|
2020-04-28 09:03:24 -04:00
|
|
|
include := false
|
|
|
|
for _, onlyGlob := range onlyGlobs {
|
|
|
|
if onlyGlob.Match(buildName) {
|
|
|
|
include = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !include {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -except
|
2020-05-08 10:41:47 -04:00
|
|
|
if len(opts.Except) > 0 {
|
|
|
|
exceptGlobs, diags := convertFilterOption(opts.Except, "except")
|
2020-04-29 10:36:40 -04:00
|
|
|
if diags.HasErrors() {
|
|
|
|
return nil, diags
|
|
|
|
}
|
2020-06-09 09:23:29 -04:00
|
|
|
cfg.except = exceptGlobs
|
2020-04-28 09:03:24 -04:00
|
|
|
exclude := false
|
|
|
|
for _, exceptGlob := range exceptGlobs {
|
|
|
|
if exceptGlob.Match(buildName) {
|
|
|
|
exclude = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if exclude {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 19:22:51 -04:00
|
|
|
builder, moreDiags, generatedVars := cfg.startBuilder(src, cfg.EvalContext(nil), opts)
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-14 11:39:32 -05:00
|
|
|
|
|
|
|
// If the builder has provided a list of to-be-generated variables that
|
|
|
|
// should be made accessible to provisioners, pass that list into
|
|
|
|
// the provisioner prepare() so that the provisioner can appropriately
|
|
|
|
// validate user input against what will become available. Otherwise,
|
|
|
|
// only pass the default variables, using the basic placeholder data.
|
|
|
|
generatedPlaceholderMap := packer.BasicPlaceholderData()
|
2020-07-02 09:56:49 -04:00
|
|
|
unknownBuildValues := map[string]cty.Value{}
|
|
|
|
for _, k := range generatedVars {
|
|
|
|
generatedPlaceholderMap[k] = fmt.Sprintf("Build_%s. "+
|
|
|
|
common.PlaceholderMsg, k)
|
2020-07-02 12:02:19 -04:00
|
|
|
unknownBuildValues[k] = cty.StringVal("<unknown>")
|
2020-07-02 09:56:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
variables := map[string]cty.Value{
|
|
|
|
sourcesAccessor: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"type": cty.StringVal(src.Type),
|
|
|
|
"name": cty.StringVal(src.Name),
|
|
|
|
}),
|
|
|
|
buildAccessor: cty.ObjectVal(unknownBuildValues),
|
2020-02-14 11:39:32 -05:00
|
|
|
}
|
|
|
|
|
2020-07-02 12:02:19 -04:00
|
|
|
provisioners, moreDiags := cfg.getCoreBuildProvisioners(src, build.ProvisionerBlocks, cfg.EvalContext(variables), nil)
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-02 12:02:19 -04:00
|
|
|
postProcessors, moreDiags := cfg.getCoreBuildPostProcessors(src, build.PostProcessors, cfg.EvalContext(variables), nil)
|
2020-07-02 09:07:29 -04:00
|
|
|
pps := [][]packer.CoreBuildPostProcessor{}
|
|
|
|
if len(postProcessors) > 0 {
|
|
|
|
pps = [][]packer.CoreBuildPostProcessor{postProcessors}
|
2020-07-02 12:02:19 -04:00
|
|
|
} // TODO(azr): remove this
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
2019-10-14 11:02:53 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:53:16 -04:00
|
|
|
pcb.Builder = builder
|
2020-07-02 09:07:29 -04:00
|
|
|
pcb.Provisioners = provisioners
|
|
|
|
pcb.PostProcessors = pps
|
2020-06-23 04:53:16 -04:00
|
|
|
pcb.Prepared = true
|
|
|
|
|
2020-05-28 04:43:58 -04:00
|
|
|
// Prepare just sets the "prepareCalled" flag on CoreBuild, since
|
|
|
|
// we did all the prep here.
|
|
|
|
_, err := pcb.Prepare()
|
|
|
|
if err != nil {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: fmt.Sprintf("Preparing packer core build %s failed", src.Ref().String()),
|
|
|
|
Detail: err.Error(),
|
|
|
|
Subject: build.HCL2Ref.DefRange.Ptr(),
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
res = append(res, pcb)
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
return res, diags
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
2020-06-05 11:23:54 -04:00
|
|
|
|
|
|
|
var PackerConsoleHelp = strings.TrimSpace(`
|
|
|
|
Packer console HCL2 Mode.
|
|
|
|
The Packer console allows you to experiment with Packer interpolations.
|
|
|
|
You may access variables and functions in the Packer config you called the
|
|
|
|
console with.
|
|
|
|
|
|
|
|
Type in the interpolation to test and hit <enter> to see the result.
|
|
|
|
|
|
|
|
"upper(var.foo.id)" would evaluate to the ID of "foo" and uppercase is, if it
|
|
|
|
exists in your config file.
|
|
|
|
|
|
|
|
"variables" will dump all available variables and their values.
|
|
|
|
|
|
|
|
To exit the console, type "exit" and hit <enter>, or use Control-C.
|
|
|
|
|
|
|
|
/!\ It is not possible to use go templating interpolation like "{{timestamp}}"
|
|
|
|
with in HCL2 mode.
|
|
|
|
`)
|
|
|
|
|
|
|
|
func (p *PackerConfig) EvaluateExpression(line string) (out string, exit bool, diags hcl.Diagnostics) {
|
|
|
|
switch {
|
|
|
|
case line == "":
|
|
|
|
return "", false, nil
|
|
|
|
case line == "exit":
|
|
|
|
return "", true, nil
|
|
|
|
case line == "help":
|
|
|
|
return PackerConsoleHelp, false, nil
|
|
|
|
case line == "variables":
|
2020-06-23 05:58:57 -04:00
|
|
|
return p.printVariables(), false, nil
|
2020-06-05 11:23:54 -04:00
|
|
|
default:
|
|
|
|
return p.handleEval(line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 05:58:57 -04:00
|
|
|
func (p *PackerConfig) printVariables() string {
|
|
|
|
out := &strings.Builder{}
|
|
|
|
out.WriteString("> input-variables:\n\n")
|
|
|
|
for _, v := range p.InputVariables {
|
|
|
|
val, _ := v.Value()
|
|
|
|
fmt.Fprintf(out, "var.%s: %q [debug: %#v]\n", v.Name, PrintableCtyValue(val), v)
|
|
|
|
}
|
|
|
|
out.WriteString("\n> local-variables:\n\n")
|
|
|
|
for _, v := range p.LocalVariables {
|
|
|
|
val, _ := v.Value()
|
|
|
|
fmt.Fprintf(out, "local.%s: %q\n", v.Name, PrintableCtyValue(val))
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PackerConfig) printBuilds() string {
|
|
|
|
out := &strings.Builder{}
|
|
|
|
out.WriteString("> builds:\n")
|
|
|
|
for i, build := range p.Builds {
|
|
|
|
name := build.Name
|
|
|
|
if name == "" {
|
|
|
|
name = fmt.Sprintf("<unnamed build %d>", i)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, "\n > %s:\n", name)
|
2020-06-23 09:40:54 -04:00
|
|
|
if build.Description != "" {
|
|
|
|
fmt.Fprintf(out, "\n > Description: %s\n", build.Description)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, "\n sources:\n")
|
|
|
|
if len(build.Sources) == 0 {
|
|
|
|
fmt.Fprintf(out, "\n <no source>\n")
|
|
|
|
}
|
|
|
|
for _, source := range build.Sources {
|
|
|
|
fmt.Fprintf(out, "\n %s\n", source)
|
|
|
|
}
|
2020-06-23 05:58:57 -04:00
|
|
|
fmt.Fprintf(out, "\n provisioners:\n\n")
|
|
|
|
if len(build.ProvisionerBlocks) == 0 {
|
|
|
|
fmt.Fprintf(out, " <no provisioner>\n")
|
|
|
|
}
|
|
|
|
for _, prov := range build.ProvisionerBlocks {
|
|
|
|
str := prov.PType
|
|
|
|
if prov.PName != "" {
|
|
|
|
str = strings.Join([]string{prov.PType, prov.PName}, ".")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, " %s\n", str)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, "\n post-processors:\n\n")
|
|
|
|
if len(build.PostProcessors) == 0 {
|
|
|
|
fmt.Fprintf(out, " <no post-processor>\n")
|
|
|
|
}
|
|
|
|
for _, pp := range build.PostProcessors {
|
2020-06-23 09:40:54 -04:00
|
|
|
str := pp.PType
|
2020-06-23 05:58:57 -04:00
|
|
|
if pp.PName != "" {
|
|
|
|
str = strings.Join([]string{pp.PType, pp.PName}, ".")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, " %s\n", str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
2020-06-05 11:23:54 -04:00
|
|
|
func (p *PackerConfig) handleEval(line string) (out string, exit bool, diags hcl.Diagnostics) {
|
|
|
|
|
|
|
|
// Parse the given line as an expression
|
|
|
|
expr, parseDiags := hclsyntax.ParseExpression([]byte(line), "<console-input>", hcl.Pos{Line: 1, Column: 1})
|
|
|
|
diags = append(diags, parseDiags...)
|
|
|
|
if parseDiags.HasErrors() {
|
|
|
|
return "", false, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
val, valueDiags := expr.Value(p.EvalContext(nil))
|
|
|
|
diags = append(diags, valueDiags...)
|
|
|
|
if valueDiags.HasErrors() {
|
|
|
|
return "", false, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
return PrintableCtyValue(val), false, diags
|
|
|
|
}
|
2020-06-02 14:58:33 -04:00
|
|
|
|
|
|
|
func (p *PackerConfig) FixConfig(_ packer.FixConfigOptions) (diags hcl.Diagnostics) {
|
|
|
|
// No Fixers exist for HCL2 configs so there is nothing to do here for now.
|
|
|
|
return
|
|
|
|
}
|
2020-06-23 05:58:57 -04:00
|
|
|
|
|
|
|
func (p *PackerConfig) InspectConfig(opts packer.InspectConfigOptions) int {
|
|
|
|
|
|
|
|
ui := opts.Ui
|
|
|
|
ui.Say("Packer Inspect: HCL2 mode\n")
|
|
|
|
ui.Say(p.printVariables())
|
|
|
|
ui.Say(p.printBuilds())
|
|
|
|
return 0
|
|
|
|
}
|