2019-10-14 11:02:53 -04:00
|
|
|
package hcl2template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
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
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-10-14 11:02:53 -04:00
|
|
|
|
2020-11-09 15:29:53 -05:00
|
|
|
"github.com/hashicorp/go-version"
|
2019-10-14 11:02:53 -04:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2020-02-20 04:51:34 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/ext/dynblock"
|
2019-10-14 11:02:53 -04:00
|
|
|
"github.com/hashicorp/hcl/v2/hclparse"
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-10-02 04:17:07 -04:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2019-10-14 11:02:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-10-27 05:03:36 -04:00
|
|
|
packerLabel = "packer"
|
2019-10-14 11:02:53 -04:00
|
|
|
sourceLabel = "source"
|
|
|
|
variablesLabel = "variables"
|
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
|
|
|
variableLabel = "variable"
|
|
|
|
localsLabel = "locals"
|
2021-01-26 04:21:44 -05:00
|
|
|
localLabel = "local"
|
2021-01-20 04:37:16 -05:00
|
|
|
dataSourceLabel = "data"
|
2019-10-14 11:02:53 -04:00
|
|
|
buildLabel = "build"
|
|
|
|
communicatorLabel = "communicator"
|
|
|
|
)
|
|
|
|
|
|
|
|
var configSchema = &hcl.BodySchema{
|
|
|
|
Blocks: []hcl.BlockHeaderSchema{
|
2020-10-27 05:03:36 -04:00
|
|
|
{Type: packerLabel},
|
2019-10-14 11:02:53 -04:00
|
|
|
{Type: sourceLabel, LabelNames: []string{"type", "name"}},
|
|
|
|
{Type: variablesLabel},
|
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
|
|
|
{Type: variableLabel, LabelNames: []string{"name"}},
|
|
|
|
{Type: localsLabel},
|
2021-01-26 04:21:44 -05:00
|
|
|
{Type: localLabel, LabelNames: []string{"name"}},
|
2021-01-20 04:37:16 -05:00
|
|
|
{Type: dataSourceLabel, LabelNames: []string{"type", "name"}},
|
2019-10-14 11:02:53 -04:00
|
|
|
{Type: buildLabel},
|
|
|
|
{Type: communicatorLabel, LabelNames: []string{"type", "name"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-27 05:03:36 -04:00
|
|
|
// packerBlockSchema is the schema for a top-level "packer" block in
|
|
|
|
// a configuration file.
|
|
|
|
var packerBlockSchema = &hcl.BodySchema{
|
|
|
|
Attributes: []hcl.AttributeSchema{
|
|
|
|
{Name: "required_version"},
|
|
|
|
},
|
2021-02-02 12:05:04 -05:00
|
|
|
Blocks: []hcl.BlockHeaderSchema{
|
|
|
|
{Type: "required_plugins"},
|
|
|
|
},
|
2020-10-27 05:03:36 -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
|
|
|
// Parser helps you parse HCL folders. It will parse an hcl file or directory
|
|
|
|
// and start builders, provisioners and post-processors to configure them with
|
2020-12-09 06:39:54 -05:00
|
|
|
// the parsed HCL and then return a []packersdk.Build. Packer will use that list
|
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
|
|
|
// of Builds to run everything in order.
|
2019-10-14 11:02:53 -04:00
|
|
|
type Parser struct {
|
2020-11-09 15:29:53 -05:00
|
|
|
CorePackerVersion *version.Version
|
|
|
|
|
|
|
|
CorePackerVersionString string
|
|
|
|
|
2019-10-14 11:02:53 -04:00
|
|
|
*hclparse.Parser
|
|
|
|
|
2021-02-02 12:05:04 -05:00
|
|
|
PluginConfig *packer.PluginConfig
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 10:05:30 -05:00
|
|
|
const (
|
2020-12-14 10:29:58 -05:00
|
|
|
hcl2FileExt = ".pkr.hcl"
|
|
|
|
hcl2JsonFileExt = ".pkr.json"
|
|
|
|
hcl2VarFileExt = ".pkrvars.hcl"
|
|
|
|
hcl2VarJsonFileExt = ".pkrvars.json"
|
|
|
|
hcl2AutoVarFileExt = ".auto.pkrvars.hcl"
|
|
|
|
hcl2AutoVarJsonFileExt = ".auto.pkrvars.json"
|
2020-01-06 10:05:30 -05:00
|
|
|
)
|
2019-10-14 11:02:53 -04:00
|
|
|
|
2020-04-30 05:51:24 -04:00
|
|
|
// Parse will Parse all HCL files in filename. Path can be a folder or a file.
|
2020-04-29 10:36:40 -04:00
|
|
|
//
|
|
|
|
// Parse will first Parse variables and then the rest; so that interpolation
|
|
|
|
// can happen.
|
2020-04-30 05:51:24 -04:00
|
|
|
//
|
|
|
|
// Parse returns a PackerConfig that contains configuration layout of a packer
|
|
|
|
// build; sources(builders)/provisioners/posts-processors will not be started
|
|
|
|
// and their contents wont be verified; Most syntax errors will cause an error.
|
2020-04-29 10:36:40 -04:00
|
|
|
func (p *Parser) Parse(filename string, varFiles []string, argVars map[string]string) (*PackerConfig, hcl.Diagnostics) {
|
2019-10-14 11:02:53 -04:00
|
|
|
var files []*hcl.File
|
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
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
|
|
|
// parse config files
|
2020-06-05 11:23:54 -04:00
|
|
|
if filename != "" {
|
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
|
|
|
hclFiles, jsonFiles, moreDiags := GetHCL2Files(filename, hcl2FileExt, hcl2JsonFileExt)
|
2020-03-12 09:27:56 -04:00
|
|
|
diags = append(diags, moreDiags...)
|
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
|
|
|
if len(hclFiles)+len(jsonFiles) == 0 {
|
2020-07-23 03:25:07 -04:00
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
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
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Could not find any config file in " + filename,
|
|
|
|
Detail: "A config file must be suffixed with `.pkr.hcl` or " +
|
|
|
|
"`.pkr.json`. A folder can be referenced.",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, filename := range hclFiles {
|
|
|
|
f, moreDiags := p.ParseHCLFile(filename)
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
files = append(files, f)
|
|
|
|
}
|
|
|
|
for _, filename := range jsonFiles {
|
|
|
|
f, moreDiags := p.ParseJSONFile(filename)
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
files = append(files, f)
|
|
|
|
}
|
|
|
|
if diags.HasErrors() {
|
|
|
|
return nil, 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
|
|
|
|
|
|
|
basedir := filename
|
|
|
|
if isDir, err := isDir(basedir); err == nil && !isDir {
|
|
|
|
basedir = filepath.Dir(basedir)
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
2020-07-23 03:25:07 -04:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Could not find current working directory",
|
|
|
|
Detail: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
cfg := &PackerConfig{
|
2020-11-09 15:29:53 -05:00
|
|
|
Basedir: basedir,
|
|
|
|
Cwd: wd,
|
|
|
|
CorePackerVersionString: p.CorePackerVersionString,
|
|
|
|
parser: p,
|
|
|
|
files: files,
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
|
2020-10-27 05:03:36 -04:00
|
|
|
for _, file := range files {
|
|
|
|
coreVersionConstraints, moreDiags := sniffCoreVersionRequirements(file.Body)
|
|
|
|
cfg.Packer.VersionConstraints = append(cfg.Packer.VersionConstraints, coreVersionConstraints...)
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we go further, we'll check to make sure this version can read
|
2021-02-02 12:05:04 -05:00
|
|
|
// all files, so we can produce a version-related error message rather than
|
2020-10-27 05:03:36 -04:00
|
|
|
// potentially-confusing downstream errors.
|
2020-11-09 15:29:53 -05:00
|
|
|
versionDiags := cfg.CheckCoreVersionRequirements(p.CorePackerVersion)
|
2020-10-27 05:03:36 -04:00
|
|
|
diags = append(diags, versionDiags...)
|
|
|
|
if versionDiags.HasErrors() {
|
|
|
|
return cfg, diags
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:05:04 -05:00
|
|
|
// Decode required_plugins blocks and create implicit required_plugins
|
|
|
|
// blocks. Implicit required_plugins blocks happen when a builder or another
|
|
|
|
// plugin cannot be found, for example if one uses :
|
|
|
|
// source "amazon-ebs" "example" { ... }
|
|
|
|
// And no `amazon-ebs` builder can be found. This will then be the
|
|
|
|
// equivalent of having :
|
|
|
|
// packer {
|
|
|
|
// required_plugins {
|
2021-02-15 07:58:58 -05:00
|
|
|
// amazon = {
|
|
|
|
// version = "latest"
|
|
|
|
// source = "github.com/hashicorp/amazon"
|
2021-02-02 12:05:04 -05:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// Note: using `latest` ( or actually an empty string ) in a config file
|
|
|
|
// does not work and packer will ask you to pick a version
|
|
|
|
{
|
|
|
|
for _, file := range files {
|
|
|
|
diags = append(diags, cfg.decodeRequiredPluginsBlock(file)...)
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
diags = append(diags, cfg.decodeImplicitRequiredPluginsBlocks(file)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// Decode variable blocks so that they are available later on. Here locals
|
2021-02-02 12:05:04 -05:00
|
|
|
// can use input variables so we decode input variables first.
|
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 _, file := range files {
|
2020-02-21 06:12:30 -05:00
|
|
|
diags = append(diags, cfg.decodeInputVariables(file)...)
|
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-02-21 06:12:30 -05:00
|
|
|
|
2021-01-20 04:37:16 -05:00
|
|
|
for _, file := range files {
|
|
|
|
morediags := p.decodeDatasources(file, cfg)
|
|
|
|
diags = append(diags, morediags...)
|
|
|
|
}
|
|
|
|
|
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 _, file := range files {
|
2020-02-21 06:12:30 -05:00
|
|
|
moreLocals, morediags := cfg.parseLocalVariables(file)
|
|
|
|
diags = append(diags, morediags...)
|
2020-07-24 04:58:03 -04:00
|
|
|
cfg.LocalBlocks = append(cfg.LocalBlocks, moreLocals...)
|
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 var files
|
|
|
|
{
|
2020-12-14 10:29:58 -05:00
|
|
|
hclVarFiles, jsonVarFiles, moreDiags := GetHCL2Files(filename, hcl2AutoVarFileExt, hcl2AutoVarJsonFileExt)
|
2019-10-14 11:02:53 -04:00
|
|
|
diags = append(diags, moreDiags...)
|
2020-03-12 09:27:56 -04:00
|
|
|
for _, file := range varFiles {
|
|
|
|
switch filepath.Ext(file) {
|
|
|
|
case ".hcl":
|
|
|
|
hclVarFiles = append(hclVarFiles, file)
|
|
|
|
case ".json":
|
|
|
|
jsonVarFiles = append(jsonVarFiles, file)
|
|
|
|
default:
|
|
|
|
diags = append(moreDiags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Could not guess format of " + file,
|
|
|
|
Detail: "A var file must be suffixed with `.hcl` or `.json`.",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
var varFiles []*hcl.File
|
|
|
|
for _, filename := range hclVarFiles {
|
|
|
|
f, moreDiags := p.ParseHCLFile(filename)
|
|
|
|
diags = append(diags, moreDiags...)
|
2020-03-19 08:57:22 -04:00
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
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
|
|
|
varFiles = append(varFiles, f)
|
|
|
|
}
|
|
|
|
for _, filename := range jsonVarFiles {
|
|
|
|
f, moreDiags := p.ParseJSONFile(filename)
|
|
|
|
diags = append(diags, moreDiags...)
|
2020-03-19 08:57:22 -04:00
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
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
|
|
|
varFiles = append(varFiles, f)
|
|
|
|
}
|
|
|
|
|
2020-03-12 09:27:56 -04:00
|
|
|
diags = append(diags, cfg.collectInputVariableValues(os.Environ(), varFiles, argVars)...)
|
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-02 12:05:04 -05:00
|
|
|
|
|
|
|
// parse the actual content // rest
|
|
|
|
for _, file := range cfg.files {
|
|
|
|
diags = append(diags, cfg.parser.parseConfig(file, cfg)...)
|
|
|
|
}
|
2020-07-24 04:58:03 -04:00
|
|
|
return cfg, diags
|
|
|
|
}
|
|
|
|
|
2020-10-27 05:03:36 -04:00
|
|
|
// sniffCoreVersionRequirements does minimal parsing of the given body for
|
|
|
|
// "packer" blocks with "required_version" attributes, returning the
|
|
|
|
// requirements found.
|
|
|
|
//
|
|
|
|
// This is intended to maximize the chance that we'll be able to read the
|
|
|
|
// requirements (syntax errors notwithstanding) even if the config file contains
|
|
|
|
// constructs that might've been added in future versions
|
|
|
|
//
|
|
|
|
// This is a "best effort" sort of method which will return constraints it is
|
|
|
|
// able to find, but may return no constraints at all if the given body is
|
|
|
|
// so invalid that it cannot be decoded at all.
|
|
|
|
func sniffCoreVersionRequirements(body hcl.Body) ([]VersionConstraint, hcl.Diagnostics) {
|
|
|
|
|
|
|
|
var sniffRootSchema = &hcl.BodySchema{
|
|
|
|
Blocks: []hcl.BlockHeaderSchema{
|
|
|
|
{
|
|
|
|
Type: packerLabel,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
rootContent, _, diags := body.PartialContent(sniffRootSchema)
|
|
|
|
|
|
|
|
var constraints []VersionConstraint
|
|
|
|
|
|
|
|
for _, block := range rootContent.Blocks {
|
|
|
|
content, blockDiags := block.Body.Content(packerBlockSchema)
|
|
|
|
diags = append(diags, blockDiags...)
|
|
|
|
|
|
|
|
attr, exists := content.Attributes["required_version"]
|
|
|
|
if !exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
constraint, constraintDiags := decodeVersionConstraint(attr)
|
|
|
|
diags = append(diags, constraintDiags...)
|
|
|
|
if !constraintDiags.HasErrors() {
|
|
|
|
constraints = append(constraints, constraint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return constraints, diags
|
|
|
|
}
|
|
|
|
|
2021-01-26 04:21:44 -05:00
|
|
|
func filterVarsFromLogs(inputOrLocal Variables) {
|
|
|
|
for _, variable := range inputOrLocal {
|
2020-10-02 04:17:07 -04:00
|
|
|
if !variable.Sensitive {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
value, _ := variable.Value()
|
2020-10-02 04:53:17 -04:00
|
|
|
_ = cty.Walk(value, func(_ cty.Path, nested cty.Value) (bool, error) {
|
2020-10-02 04:17:07 -04:00
|
|
|
if nested.IsWhollyKnown() && !nested.IsNull() && nested.Type().Equals(cty.String) {
|
2020-11-19 17:03:11 -05:00
|
|
|
packersdk.LogSecretFilter.Set(nested.AsString())
|
2020-10-02 04:17:07 -04:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
}
|
2021-01-26 04:21:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *PackerConfig) Initialize(opts packer.InitializeOptions) hcl.Diagnostics {
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
2021-02-02 12:05:04 -05:00
|
|
|
// enable packer to start plugins requested in required_plugins.
|
|
|
|
moreDiags := cfg.detectPluginBinaries()
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
|
|
|
_, moreDiags = cfg.InputVariables.Values()
|
2021-01-26 04:21:44 -05:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
_, moreDiags = cfg.LocalVariables.Values()
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
diags = append(diags, cfg.evaluateDatasources(opts.SkipDatasourcesExecution)...)
|
|
|
|
diags = append(diags, cfg.evaluateLocalVariables(cfg.LocalBlocks)...)
|
|
|
|
|
|
|
|
filterVarsFromLogs(cfg.InputVariables)
|
|
|
|
filterVarsFromLogs(cfg.LocalVariables)
|
2020-10-02 04:17:07 -04:00
|
|
|
|
2021-02-02 12:05:04 -05:00
|
|
|
diags = append(diags, cfg.initializeBlocks()...)
|
2019-10-14 11:02:53 -04:00
|
|
|
|
2020-07-24 04:58:03 -04:00
|
|
|
return diags
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
|
2021-02-02 12:05:04 -05:00
|
|
|
// parseConfig looks in the found blocks for everything that is not a variable
|
|
|
|
// block.
|
|
|
|
func (p *Parser) parseConfig(f *hcl.File, cfg *PackerConfig) hcl.Diagnostics {
|
2019-10-14 11:02:53 -04:00
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
2021-02-02 12:05:04 -05:00
|
|
|
body := f.Body
|
2020-02-20 04:51:34 -05:00
|
|
|
content, moreDiags := body.Content(configSchema)
|
2019-10-14 11:02:53 -04:00
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
|
|
|
|
for _, block := range content.Blocks {
|
|
|
|
switch block.Type {
|
|
|
|
case sourceLabel:
|
2019-12-17 05:25:56 -05:00
|
|
|
source, moreDiags := p.decodeSource(block)
|
2019-10-14 11:02:53 -04:00
|
|
|
diags = append(diags, moreDiags...)
|
2019-12-17 05:25:56 -05:00
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-14 11:02:53 -04:00
|
|
|
|
|
|
|
ref := source.Ref()
|
2020-05-25 11:09:37 -04:00
|
|
|
if existing, found := cfg.Sources[ref]; found {
|
2019-10-14 11:02:53 -04:00
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Duplicate " + sourceLabel + " block",
|
|
|
|
Detail: fmt.Sprintf("This "+sourceLabel+" block has the "+
|
|
|
|
"same builder type and name as a previous block declared "+
|
|
|
|
"at %s. Each "+sourceLabel+" must have a unique name per builder type.",
|
2019-12-17 05:25:56 -05:00
|
|
|
existing.block.DefRange.Ptr()),
|
|
|
|
Subject: source.block.DefRange.Ptr(),
|
2019-10-14 11:02:53 -04:00
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
|
|
|
|
if cfg.Sources == nil {
|
2020-05-25 11:09:37 -04:00
|
|
|
cfg.Sources = map[SourceRef]SourceBlock{}
|
2019-12-17 05:25:56 -05:00
|
|
|
}
|
2019-10-14 11:02:53 -04:00
|
|
|
cfg.Sources[ref] = source
|
|
|
|
|
|
|
|
case buildLabel:
|
2020-08-05 11:41:20 -04:00
|
|
|
build, moreDiags := p.decodeBuildConfig(block, cfg)
|
2019-10-14 11:02:53 -04:00
|
|
|
diags = append(diags, moreDiags...)
|
2019-12-17 05:25:56 -05:00
|
|
|
if moreDiags.HasErrors() {
|
2019-10-14 11:02:53 -04:00
|
|
|
continue
|
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
cfg.Builds = append(cfg.Builds, build)
|
2019-10-14 11:02:53 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return diags
|
|
|
|
}
|
2021-01-20 04:37:16 -05:00
|
|
|
|
|
|
|
func (p *Parser) decodeDatasources(file *hcl.File, cfg *PackerConfig) hcl.Diagnostics {
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
2021-02-04 05:25:44 -05:00
|
|
|
body := dynblock.Expand(file.Body, cfg.EvalContext(DatasourceContext, nil))
|
2021-01-20 04:37:16 -05:00
|
|
|
content, moreDiags := body.Content(configSchema)
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
|
|
|
|
for _, block := range content.Blocks {
|
|
|
|
switch block.Type {
|
|
|
|
case dataSourceLabel:
|
|
|
|
datasource, moreDiags := p.decodeDataBlock(block)
|
|
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ref := datasource.Ref()
|
|
|
|
if existing, found := cfg.Datasources[ref]; found {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Duplicate " + dataSourceLabel + " block",
|
|
|
|
Detail: fmt.Sprintf("This "+dataSourceLabel+" block has the "+
|
|
|
|
"same data type and name as a previous block declared "+
|
|
|
|
"at %s. Each "+dataSourceLabel+" must have a unique name per builder type.",
|
|
|
|
existing.block.DefRange.Ptr()),
|
|
|
|
Subject: datasource.block.DefRange.Ptr(),
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cfg.Datasources == nil {
|
|
|
|
cfg.Datasources = Datasources{}
|
|
|
|
}
|
|
|
|
cfg.Datasources[ref] = *datasource
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return diags
|
|
|
|
}
|