2019-10-14 11:02:53 -04:00
|
|
|
package hcl2template
|
|
|
|
|
|
|
|
import (
|
2020-02-14 11:39:32 -05:00
|
|
|
"fmt"
|
2020-03-04 06:30:05 -05:00
|
|
|
|
2019-10-14 11:02:53 -04:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2020-02-14 11:39:32 -05: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
|
|
|
|
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-03-09 11:16:59 -04:00
|
|
|
type ValidationOptions struct {
|
|
|
|
Strict bool
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (cfg *PackerConfig) EvalContext() *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-03-09 12:25:56 -04:00
|
|
|
"var": cty.ObjectVal(inputVariables),
|
|
|
|
"local": cty.ObjectVal(localVariables),
|
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
|
|
|
|
|
|
|
|
value, moreDiags := local.Expr.Value(c.EvalContext())
|
|
|
|
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-02-14 11:39:32 -05:00
|
|
|
func (p *Parser) getCoreBuildProvisioners(blocks []*ProvisionerBlock, ectx *hcl.EvalContext, generatedVars map[string]string) ([]packer.CoreBuildProvisioner, hcl.Diagnostics) {
|
2019-12-17 05:25:56 -05:00
|
|
|
var diags hcl.Diagnostics
|
|
|
|
res := []packer.CoreBuildProvisioner{}
|
|
|
|
for _, pb := range blocks {
|
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
|
|
|
provisioner, moreDiags := p.startProvisioner(pb, ectx, generatedVars)
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
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-02-14 11:39:32 -05:00
|
|
|
func (p *Parser) getCoreBuildPostProcessors(blocks []*PostProcessorBlock, ectx *hcl.EvalContext, generatedVars map[string]string) ([]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-02-14 11:39:32 -05:00
|
|
|
postProcessor, moreDiags := p.startPostProcessor(ppb, ectx, generatedVars)
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res = append(res, packer.CoreBuildPostProcessor{
|
|
|
|
PostProcessor: postProcessor,
|
2020-01-16 06:08:39 -05:00
|
|
|
PName: ppb.PName,
|
|
|
|
PType: ppb.PType,
|
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
|
|
|
|
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
|
|
|
// getBuilds will return a list of packer Build based on the HCL2 parsed build
|
|
|
|
// blocks. All Builders, Provisioners and Post Processors will be started and
|
|
|
|
// configured.
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *Parser) getBuilds(cfg *PackerConfig) ([]packer.Build, hcl.Diagnostics) {
|
|
|
|
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 {
|
2019-12-17 05:25:56 -05:00
|
|
|
src, found := cfg.Sources[from]
|
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,
|
|
|
|
})
|
2019-12-17 05:25:56 -05:00
|
|
|
continue
|
|
|
|
}
|
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
|
|
|
builder, moreDiags, generatedVars := p.startBuilder(src, cfg.EvalContext())
|
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()
|
|
|
|
if generatedVars != nil {
|
|
|
|
for _, k := range generatedVars {
|
|
|
|
generatedPlaceholderMap[k] = fmt.Sprintf("Build_%s. "+
|
|
|
|
common.PlaceholderMsg, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
provisioners, moreDiags := p.getCoreBuildProvisioners(build.ProvisionerBlocks, cfg.EvalContext(), generatedPlaceholderMap)
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-14 11:39:32 -05:00
|
|
|
postProcessors, moreDiags := p.getCoreBuildPostProcessors(build.PostProcessors, cfg.EvalContext(), generatedPlaceholderMap)
|
2019-12-17 19:13:21 -05:00
|
|
|
pps := [][]packer.CoreBuildPostProcessor{}
|
|
|
|
if len(postProcessors) > 0 {
|
|
|
|
pps = [][]packer.CoreBuildPostProcessor{postProcessors}
|
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
2019-10-14 11:02:53 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
pcb := &packer.CoreBuild{
|
|
|
|
Type: src.Type,
|
|
|
|
Builder: builder,
|
|
|
|
Provisioners: provisioners,
|
2019-12-17 19:13:21 -05:00
|
|
|
PostProcessors: pps,
|
2020-02-14 11:39:32 -05:00
|
|
|
Prepared: true,
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
// Parse will parse HCL file(s) in path. Path can be a folder or a file.
|
|
|
|
//
|
|
|
|
// Parse will first parse variables and then the rest; so that interpolation
|
|
|
|
// can happen.
|
|
|
|
//
|
|
|
|
// For each build block a packer.Build will be started, and for each builder,
|
|
|
|
// all provisioners and post-processors will be started.
|
|
|
|
//
|
|
|
|
// Parse then return a slice of packer.Builds; which are what packer core uses
|
|
|
|
// to run builds.
|
2020-03-12 09:27:56 -04:00
|
|
|
func (p *Parser) Parse(path string, varFiles []string, argVars map[string]string) ([]packer.Build, hcl.Diagnostics) {
|
|
|
|
cfg, diags := p.parse(path, varFiles, argVars)
|
2019-12-17 05:25:56 -05:00
|
|
|
if diags.HasErrors() {
|
|
|
|
return nil, diags
|
|
|
|
}
|
2019-10-14 11:02:53 -04:00
|
|
|
|
2020-01-06 08:31:15 -05:00
|
|
|
builds, moreDiags := p.getBuilds(cfg)
|
|
|
|
return builds, append(diags, moreDiags...)
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|