2019-10-14 11:02:53 -04:00
|
|
|
package hcl2template
|
|
|
|
|
|
|
|
import (
|
2020-02-14 11:39:32 -05:00
|
|
|
"fmt"
|
2020-09-01 09:27:01 -04:00
|
|
|
"sort"
|
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"
|
2021-01-20 04:37:16 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-06-05 11:23:54 -04:00
|
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2020-11-11 05:27:32 -05:00
|
|
|
pkrfunction "github.com/hashicorp/packer/hcl2template/function"
|
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"
|
2020-11-11 05:27:32 -05:00
|
|
|
"github.com/zclconf/go-cty/cty/function"
|
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 {
|
2020-10-27 05:03:36 -04:00
|
|
|
Packer struct {
|
|
|
|
VersionConstraints []VersionConstraint
|
2021-02-02 12:05:04 -05:00
|
|
|
RequiredPlugins []*RequiredPlugins
|
2020-10-27 05:03:36 -04:00
|
|
|
}
|
2021-02-02 12:05:04 -05: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
|
|
|
// Directory where the config files are defined
|
|
|
|
Basedir string
|
2020-11-09 15:29:53 -05:00
|
|
|
|
|
|
|
// Core Packer version, for reference by plugins and template functions.
|
|
|
|
CorePackerVersionString string
|
|
|
|
|
2020-07-23 03:25:07 -04:00
|
|
|
// directory Packer was called from
|
|
|
|
Cwd 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
|
|
|
|
|
2021-01-20 04:37:16 -05:00
|
|
|
Datasources Datasources
|
|
|
|
|
2020-07-24 04:58:03 -04:00
|
|
|
LocalBlocks []*LocalBlock
|
|
|
|
|
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
|
|
|
|
2020-07-24 04:58:03 -04:00
|
|
|
parser *Parser
|
|
|
|
files []*hcl.File
|
2021-02-05 04:57:14 -05:00
|
|
|
|
|
|
|
// Fields passed as command line flags
|
|
|
|
except []glob.Glob
|
|
|
|
only []glob.Glob
|
|
|
|
force bool
|
|
|
|
debug bool
|
|
|
|
onError string
|
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"
|
2020-07-23 03:25:07 -04:00
|
|
|
pathVariablesAccessor = "path"
|
2020-04-09 05:14:37 -04:00
|
|
|
sourcesAccessor = "source"
|
2020-07-02 09:56:49 -04:00
|
|
|
buildAccessor = "build"
|
2020-10-15 19:30:46 -04:00
|
|
|
packerAccessor = "packer"
|
2021-01-20 04:37:16 -05:00
|
|
|
dataAccessor = "data"
|
2020-04-09 05:14:37 -04:00
|
|
|
)
|
|
|
|
|
2021-02-04 05:25:44 -05:00
|
|
|
type BlockContext int
|
|
|
|
|
|
|
|
const (
|
|
|
|
InputVariableContext BlockContext = iota
|
|
|
|
LocalContext
|
|
|
|
BuildContext
|
|
|
|
DatasourceContext
|
|
|
|
NilContext
|
|
|
|
)
|
|
|
|
|
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.
|
2021-02-04 05:25:44 -05:00
|
|
|
func (cfg *PackerConfig) EvalContext(ctx BlockContext, 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),
|
|
|
|
}),
|
2020-07-06 10:07:46 -04:00
|
|
|
buildAccessor: cty.UnknownVal(cty.EmptyObject),
|
2020-10-15 19:30:46 -04:00
|
|
|
packerAccessor: cty.ObjectVal(map[string]cty.Value{
|
2020-11-09 15:29:53 -05:00
|
|
|
"version": cty.StringVal(cfg.CorePackerVersionString),
|
2020-10-15 19:30:46 -04:00
|
|
|
}),
|
2020-07-23 03:25:07 -04:00
|
|
|
pathVariablesAccessor: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"cwd": cty.StringVal(strings.ReplaceAll(cfg.Cwd, `\`, `/`)),
|
|
|
|
"root": cty.StringVal(strings.ReplaceAll(cfg.Basedir, `\`, `/`)),
|
|
|
|
}),
|
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
|
|
|
},
|
|
|
|
}
|
2021-02-04 05:25:44 -05:00
|
|
|
|
|
|
|
// Currently the places where you can make references to other blocks
|
|
|
|
// from one is very 'procedural', and in this specific case, we could make
|
|
|
|
// the data sources available to other datasources, but this would be
|
|
|
|
// order dependant, meaning that if you define two datasources in two
|
|
|
|
// different blocks, the second one can use the first one, but not the
|
|
|
|
// other way around; which would be totally confusing; so - for now -
|
|
|
|
// datasources can't use other datasources.
|
|
|
|
// In the future we'd like to load and execute HCL blocks using a graph
|
|
|
|
// dependency tree, so that any block can use any block whatever the
|
|
|
|
// order.
|
|
|
|
switch ctx {
|
|
|
|
case LocalContext, BuildContext:
|
|
|
|
datasourceVariables, _ := cfg.Datasources.Values()
|
|
|
|
ectx.Variables[dataAccessor] = cty.ObjectVal(datasourceVariables)
|
|
|
|
}
|
|
|
|
|
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...)
|
|
|
|
|
2020-11-11 05:27:32 -05:00
|
|
|
// for input variables we allow to use env in the default value section.
|
|
|
|
ectx := &hcl.EvalContext{
|
|
|
|
Functions: map[string]function.Function{
|
|
|
|
"env": pkrfunction.EnvFunc,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-21 06:12:30 -05:00
|
|
|
for _, block := range content.Blocks {
|
|
|
|
switch block.Type {
|
|
|
|
case variableLabel:
|
2020-11-11 05:27:32 -05:00
|
|
|
moreDiags := c.InputVariables.decodeVariableBlock(block, ectx)
|
2020-02-21 06:12:30 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
case variablesLabel:
|
|
|
|
attrs, moreDiags := block.Body.JustAttributes()
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
for key, attr := range attrs {
|
2020-11-11 05:27:32 -05:00
|
|
|
moreDiags = c.InputVariables.decodeVariable(key, attr, ectx)
|
2020-02-21 06:12:30 -05:00
|
|
|
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.
|
2020-07-24 04:58:03 -04:00
|
|
|
func (c *PackerConfig) parseLocalVariables(f *hcl.File) ([]*LocalBlock, hcl.Diagnostics) {
|
2020-02-21 06:12:30 -05:00
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
|
|
|
content, moreDiags := f.Body.Content(configSchema)
|
|
|
|
diags = append(diags, moreDiags...)
|
2021-01-26 04:21:44 -05:00
|
|
|
|
|
|
|
locals := c.LocalBlocks
|
2020-02-21 06:12:30 -05:00
|
|
|
|
|
|
|
for _, block := range content.Blocks {
|
|
|
|
switch block.Type {
|
2021-01-26 04:21:44 -05:00
|
|
|
case localLabel:
|
|
|
|
l, moreDiags := decodeLocalBlock(block, locals)
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if l != nil {
|
|
|
|
locals = append(locals, l)
|
|
|
|
}
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
return locals, diags
|
|
|
|
}
|
2020-02-21 06:12:30 -05:00
|
|
|
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(),
|
|
|
|
})
|
2021-01-26 04:21:44 -05:00
|
|
|
return locals, diags
|
2020-02-21 06:12:30 -05:00
|
|
|
}
|
2020-07-24 04:58:03 -04:00
|
|
|
locals = append(locals, &LocalBlock{
|
2020-02-21 06:12:30 -05:00
|
|
|
Name: name,
|
|
|
|
Expr: attr.Expr,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 04:21:44 -05:00
|
|
|
c.LocalBlocks = locals
|
2020-03-04 07:00:51 -05:00
|
|
|
return locals, diags
|
2020-02-21 06:12:30 -05:00
|
|
|
}
|
|
|
|
|
2020-07-24 04:58:03 -04:00
|
|
|
func (c *PackerConfig) evaluateLocalVariables(locals []*LocalBlock) hcl.Diagnostics {
|
2020-02-21 06:12:30 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-24 04:58:03 -04:00
|
|
|
func (c *PackerConfig) evaluateLocalVariable(local *LocalBlock) hcl.Diagnostics {
|
2020-02-21 06:12:30 -05:00
|
|
|
var diags hcl.Diagnostics
|
2021-02-04 05:25:44 -05:00
|
|
|
value, moreDiags := local.Expr.Value(c.EvalContext(LocalContext, nil))
|
2020-02-21 06:12:30 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
c.LocalVariables[local.Name] = &Variable{
|
2021-01-26 04:21:44 -05:00
|
|
|
Name: local.Name,
|
|
|
|
Sensitive: local.Sensitive,
|
2020-11-02 05:49:40 -05:00
|
|
|
Values: []VariableAssignment{{
|
|
|
|
Value: value,
|
|
|
|
Expr: local.Expr,
|
|
|
|
From: "default",
|
|
|
|
}},
|
|
|
|
Type: value.Type(),
|
2020-02-21 06:12:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
2021-01-20 04:37:16 -05:00
|
|
|
func (cfg *PackerConfig) evaluateDatasources(skipExecution bool) hcl.Diagnostics {
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
|
|
|
for ref, ds := range cfg.Datasources {
|
|
|
|
if ds.value != (cty.Value{}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:05:04 -05:00
|
|
|
datasource, startDiags := cfg.startDatasource(cfg.parser.PluginConfig.DataSources, ref)
|
2021-01-20 04:37:16 -05:00
|
|
|
diags = append(diags, startDiags...)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if skipExecution {
|
|
|
|
placeholderValue := cty.UnknownVal(hcldec.ImpliedType(datasource.OutputSpec()))
|
|
|
|
ds.value = placeholderValue
|
|
|
|
cfg.Datasources[ref] = ds
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
realValue, err := datasource.Execute()
|
|
|
|
if err != nil {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Summary: err.Error(),
|
|
|
|
Subject: &cfg.Datasources[ref].block.DefRange,
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ds.value = realValue
|
|
|
|
cfg.Datasources[ref] = ds
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2021-02-02 12:05:04 -05:00
|
|
|
func (cfg *PackerConfig) getCoreBuildProvisioners(source SourceUseBlock, blocks []*ProvisionerBlock, ectx *hcl.EvalContext) ([]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
|
|
|
|
}
|
2021-02-11 04:23:15 -05:00
|
|
|
|
|
|
|
coreBuildProv, moreDiags := cfg.getCoreBuildProvisioner(source, pb, ectx)
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-11 04:23:15 -05:00
|
|
|
res = append(res, coreBuildProv)
|
|
|
|
}
|
|
|
|
return res, diags
|
|
|
|
}
|
2020-07-02 09:07:29 -04:00
|
|
|
|
2021-02-11 04:23:15 -05:00
|
|
|
func (cfg *PackerConfig) getCoreBuildProvisioner(source SourceUseBlock, pb *ProvisionerBlock, ectx *hcl.EvalContext) (packer.CoreBuildProvisioner, hcl.Diagnostics) {
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
provisioner, moreDiags := cfg.startProvisioner(source, pb, ectx)
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
return packer.CoreBuildProvisioner{}, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're pausing, we wrap the provisioner in a special pauser.
|
|
|
|
if pb.PauseBefore != 0 {
|
|
|
|
provisioner = &packer.PausedProvisioner{
|
|
|
|
PauseBefore: pb.PauseBefore,
|
|
|
|
Provisioner: provisioner,
|
2020-04-16 05:58:54 -04:00
|
|
|
}
|
2021-02-11 04:23:15 -05:00
|
|
|
} else if pb.Timeout != 0 {
|
|
|
|
provisioner = &packer.TimeoutProvisioner{
|
|
|
|
Timeout: pb.Timeout,
|
|
|
|
Provisioner: provisioner,
|
2020-04-16 05:58:54 -04:00
|
|
|
}
|
2021-02-11 04:23:15 -05:00
|
|
|
}
|
|
|
|
if pb.MaxRetries != 0 {
|
|
|
|
provisioner = &packer.RetriedProvisioner{
|
|
|
|
MaxRetries: pb.MaxRetries,
|
2019-12-17 05:25:56 -05:00
|
|
|
Provisioner: provisioner,
|
2021-02-11 04:23:15 -05:00
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
}
|
2021-02-11 04:23:15 -05:00
|
|
|
|
|
|
|
return packer.CoreBuildProvisioner{
|
|
|
|
PType: pb.PType,
|
|
|
|
PName: pb.PName,
|
|
|
|
Provisioner: provisioner,
|
|
|
|
}, 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.
|
2021-02-02 12:05:04 -05:00
|
|
|
func (cfg *PackerConfig) getCoreBuildPostProcessors(source SourceUseBlock, blocksList [][]*PostProcessorBlock, ectx *hcl.EvalContext) ([][]packer.CoreBuildPostProcessor, hcl.Diagnostics) {
|
2019-10-14 11:02:53 -04:00
|
|
|
var diags hcl.Diagnostics
|
2020-07-28 04:02:37 -04:00
|
|
|
res := [][]packer.CoreBuildPostProcessor{}
|
|
|
|
for _, blocks := range blocksList {
|
|
|
|
pps := []packer.CoreBuildPostProcessor{}
|
|
|
|
for _, ppb := range blocks {
|
|
|
|
if ppb.OnlyExcept.Skip(source.String()) {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-09 09:23:29 -04:00
|
|
|
|
2020-07-28 04:02:37 -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 09:23:29 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-07-28 04:02:37 -04:00
|
|
|
postProcessor, moreDiags := cfg.startPostProcessor(source, ppb, ectx)
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pps = append(pps, packer.CoreBuildPostProcessor{
|
|
|
|
PostProcessor: postProcessor,
|
|
|
|
PName: ppb.PName,
|
|
|
|
PType: ppb.PType,
|
|
|
|
KeepInputArtifact: ppb.KeepInputArtifact,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if len(pps) > 0 {
|
|
|
|
res = append(res, pps)
|
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-12-09 06:39:54 -05:00
|
|
|
func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packersdk.Build, hcl.Diagnostics) {
|
|
|
|
res := []packersdk.Build{}
|
2019-12-17 05:25:56 -05:00
|
|
|
var diags hcl.Diagnostics
|
2019-10-14 11:02:53 -04:00
|
|
|
|
2021-02-05 04:57:14 -05:00
|
|
|
cfg.debug = opts.Debug
|
|
|
|
cfg.force = opts.Force
|
|
|
|
cfg.onError = opts.OnError
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
for _, build := range cfg.Builds {
|
2021-02-02 12:05:04 -05:00
|
|
|
for _, srcUsage := range build.Sources {
|
|
|
|
src, found := cfg.Sources[srcUsage.SourceRef]
|
2019-10-14 11:02:53 -04:00
|
|
|
if !found {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
2021-02-02 12:05:04 -05:00
|
|
|
Summary: "Unknown " + sourceLabel + " " + srcUsage.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-04-28 09:03:24 -04:00
|
|
|
|
2020-06-23 04:53:16 -04:00
|
|
|
pcb := &packer.CoreBuild{
|
|
|
|
BuildName: build.Name,
|
2021-02-02 12:05:04 -05:00
|
|
|
Type: srcUsage.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 04:57:14 -05:00
|
|
|
builder, moreDiags, generatedVars := cfg.startBuilder(srcUsage, cfg.EvalContext(BuildContext, nil))
|
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.
|
2020-07-02 09:56:49 -04:00
|
|
|
unknownBuildValues := map[string]cty.Value{}
|
2020-07-07 05:29:27 -04:00
|
|
|
for _, k := range append(packer.BuilderDataCommonKeys, generatedVars...) {
|
2020-07-02 12:02:19 -04:00
|
|
|
unknownBuildValues[k] = cty.StringVal("<unknown>")
|
2020-07-02 09:56:49 -04:00
|
|
|
}
|
2020-10-16 04:57:42 -04:00
|
|
|
unknownBuildValues["name"] = cty.StringVal(build.Name)
|
2020-07-02 09:56:49 -04:00
|
|
|
|
|
|
|
variables := map[string]cty.Value{
|
2021-02-02 12:05:04 -05:00
|
|
|
sourcesAccessor: cty.ObjectVal(srcUsage.ctyValues()),
|
2020-07-06 10:07:29 -04:00
|
|
|
buildAccessor: cty.ObjectVal(unknownBuildValues),
|
2020-02-14 11:39:32 -05:00
|
|
|
}
|
|
|
|
|
2021-02-04 05:25:44 -05:00
|
|
|
provisioners, moreDiags := cfg.getCoreBuildProvisioners(srcUsage, build.ProvisionerBlocks, cfg.EvalContext(BuildContext, variables))
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-04 05:25:44 -05:00
|
|
|
pps, moreDiags := cfg.getCoreBuildPostProcessors(srcUsage, build.PostProcessorsLists, cfg.EvalContext(BuildContext, variables))
|
2019-12-17 05:25:56 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
2019-10-14 11:02:53 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-11 04:23:15 -05:00
|
|
|
if build.ErrorCleanupProvisionerBlock != nil {
|
|
|
|
if !build.ErrorCleanupProvisionerBlock.OnlyExcept.Skip(srcUsage.String()) {
|
|
|
|
errorCleanupProv, moreDiags := cfg.getCoreBuildProvisioner(srcUsage, build.ErrorCleanupProvisionerBlock, cfg.EvalContext(BuildContext, variables))
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pcb.CleanupProvisioner = errorCleanupProv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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")
|
2020-09-01 09:27:01 -04:00
|
|
|
keys := p.InputVariables.Keys()
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, key := range keys {
|
|
|
|
v := p.InputVariables[key]
|
2020-06-23 05:58:57 -04:00
|
|
|
val, _ := v.Value()
|
2020-08-26 09:21:32 -04:00
|
|
|
fmt.Fprintf(out, "var.%s: %q\n", v.Name, PrintableCtyValue(val))
|
2020-06-23 05:58:57 -04:00
|
|
|
}
|
|
|
|
out.WriteString("\n> local-variables:\n\n")
|
2020-09-01 09:27:01 -04:00
|
|
|
keys = p.LocalVariables.Keys()
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, key := range keys {
|
|
|
|
v := p.LocalVariables[key]
|
2020-06-23 05:58:57 -04:00
|
|
|
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 {
|
2021-02-02 12:05:04 -05:00
|
|
|
fmt.Fprintf(out, "\n %s\n", source.String())
|
2020-06-23 09:40:54 -04:00
|
|
|
}
|
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)
|
|
|
|
}
|
2020-07-28 04:02:37 -04:00
|
|
|
fmt.Fprintf(out, "\n post-processors:\n")
|
|
|
|
if len(build.PostProcessorsLists) == 0 {
|
|
|
|
fmt.Fprintf(out, "\n <no post-processor>\n")
|
2020-06-23 05:58:57 -04:00
|
|
|
}
|
2020-07-28 04:02:37 -04:00
|
|
|
for i, ppList := range build.PostProcessorsLists {
|
|
|
|
fmt.Fprintf(out, "\n %d:\n", i)
|
|
|
|
for _, pp := range ppList {
|
|
|
|
str := pp.PType
|
|
|
|
if pp.PName != "" {
|
|
|
|
str = strings.Join([]string{pp.PType, pp.PName}, ".")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, " %s\n", str)
|
2020-06-23 05:58:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-04 05:25:44 -05:00
|
|
|
val, valueDiags := expr.Value(p.EvalContext(NilContext, nil))
|
2020-06-05 11:23:54 -04:00
|
|
|
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
|
|
|
|
}
|