2019-10-14 11:02:53 -04:00
|
|
|
package hcl2template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-05-14 19:22:51 -04:00
|
|
|
"strconv"
|
2019-10-14 11:02:53 -04:00
|
|
|
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
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
|
|
|
// SourceBlock references an HCL 'source' block.
|
|
|
|
type SourceBlock struct {
|
2019-10-14 11:02:53 -04:00
|
|
|
// Type of source; ex: virtualbox-iso
|
|
|
|
Type string
|
|
|
|
// Given name; if any
|
|
|
|
Name string
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
block *hcl.Block
|
2020-05-25 11:09:37 -04:00
|
|
|
|
2020-06-02 05:52:11 -04:00
|
|
|
// addition will be merged into block to allow user to override builder settings
|
2020-05-25 11:09:37 -04:00
|
|
|
// per build.source block.
|
|
|
|
addition hcl.Body
|
|
|
|
}
|
|
|
|
|
|
|
|
// decodeBuildSource reads a used source block from a build:
|
|
|
|
// build {
|
|
|
|
// source "type.example" {}
|
|
|
|
// }
|
|
|
|
func (p *Parser) decodeBuildSource(block *hcl.Block) (SourceRef, hcl.Diagnostics) {
|
|
|
|
ref := sourceRefFromString(block.Labels[0])
|
|
|
|
ref.addition = block.Body
|
|
|
|
return ref, nil
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
|
2020-05-25 11:09:37 -04:00
|
|
|
func (p *Parser) decodeSource(block *hcl.Block) (SourceBlock, hcl.Diagnostics) {
|
|
|
|
source := SourceBlock{
|
2019-12-17 05:25:56 -05:00
|
|
|
Type: block.Labels[0],
|
|
|
|
Name: block.Labels[1],
|
|
|
|
block: block,
|
|
|
|
}
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
|
|
|
if !p.BuilderSchemas.Has(source.Type) {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Summary: "Unknown " + buildSourceLabel + " type " + source.Type,
|
|
|
|
Subject: block.LabelRanges[0].Ptr(),
|
|
|
|
Detail: fmt.Sprintf("known builders: %v", p.BuilderSchemas.List()),
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
})
|
2020-05-25 11:09:37 -04:00
|
|
|
return source, diags
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
return source, diags
|
|
|
|
}
|
|
|
|
|
2020-05-25 11:09:37 -04:00
|
|
|
func (cfg *PackerConfig) startBuilder(source SourceBlock, ectx *hcl.EvalContext, opts packer.GetBuildsOptions) (packer.Builder, hcl.Diagnostics, []string) {
|
2019-10-14 11:02:53 -04:00
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
2020-04-29 10:15:42 -04:00
|
|
|
builder, err := cfg.builderSchemas.Start(source.Type)
|
2019-12-17 05:25:56 -05:00
|
|
|
if err != nil {
|
2019-10-14 11:02:53 -04:00
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
2019-12-17 05:25:56 -05:00
|
|
|
Summary: "Failed to load " + sourceLabel + " type",
|
|
|
|
Detail: err.Error(),
|
|
|
|
Subject: &source.block.LabelRanges[0],
|
2019-10-14 11:02:53 -04:00
|
|
|
})
|
2019-12-17 12:42:15 -05:00
|
|
|
return builder, diags, nil
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
|
2020-05-25 11:09:37 -04:00
|
|
|
body := source.block.Body
|
|
|
|
if source.addition != nil {
|
|
|
|
body = hcl.MergeBodies([]hcl.Body{source.block.Body, source.addition})
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded, moreDiags := decodeHCL2Spec(body, ectx, builder)
|
2019-10-14 11:02:53 -04:00
|
|
|
diags = append(diags, moreDiags...)
|
2019-12-17 05:25:56 -05:00
|
|
|
if moreDiags.HasErrors() {
|
2019-12-17 12:42:15 -05:00
|
|
|
return nil, diags, nil
|
2019-12-17 05:25:56 -05:00
|
|
|
}
|
2019-12-17 10:31:22 -05:00
|
|
|
|
2020-05-14 19:22:51 -04:00
|
|
|
// Note: HCL prepares inside of the Start func, but Json does not. Json
|
|
|
|
// builds are instead prepared only in command/build.go
|
|
|
|
// TODO: either make json prepare when plugins are loaded, or make HCL
|
|
|
|
// prepare at a later step, to make builds from different template types
|
|
|
|
// easier to reason about.
|
|
|
|
builderVars := source.builderVariables()
|
|
|
|
builderVars["packer_debug"] = strconv.FormatBool(opts.Debug)
|
|
|
|
builderVars["packer_force"] = strconv.FormatBool(opts.Force)
|
|
|
|
builderVars["packer_on_error"] = opts.OnError
|
|
|
|
|
|
|
|
generatedVars, warning, err := builder.Prepare(builderVars, decoded)
|
2019-12-17 05:25:56 -05:00
|
|
|
moreDiags = warningErrorsToDiags(source.block, warning, err)
|
|
|
|
diags = append(diags, moreDiags...)
|
2019-12-17 12:42:15 -05:00
|
|
|
return builder, diags, generatedVars
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
|
2020-05-14 19:22:51 -04:00
|
|
|
// These variables will populate the PackerConfig inside of the builders.
|
2020-04-09 05:14:37 -04:00
|
|
|
func (source *SourceBlock) builderVariables() map[string]string {
|
|
|
|
return map[string]string{
|
|
|
|
"packer_build_name": source.Name,
|
|
|
|
"packer_builder_type": source.Type,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (source *SourceBlock) Ref() SourceRef {
|
2019-10-14 11:02:53 -04:00
|
|
|
return SourceRef{
|
|
|
|
Type: source.Type,
|
|
|
|
Name: source.Name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SourceRef struct {
|
|
|
|
Type string
|
|
|
|
Name string
|
2020-05-25 11:09:37 -04:00
|
|
|
|
|
|
|
// The content of this body will be merged into a new block to allow to
|
|
|
|
// override builder settings per build section.
|
|
|
|
addition hcl.Body
|
|
|
|
}
|
|
|
|
|
2020-06-02 05:51:48 -04:00
|
|
|
// the 'addition' field makes of ref a different entry in the sources map, so
|
2020-05-25 11:09:37 -04:00
|
|
|
// Ref is here to make sure only one is returned.
|
|
|
|
func (r *SourceRef) Ref() SourceRef {
|
|
|
|
return SourceRef{
|
|
|
|
Type: r.Type,
|
|
|
|
Name: r.Name,
|
|
|
|
}
|
2019-10-14 11:02:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NoSource is the zero value of sourceRef, representing the absense of an
|
|
|
|
// source.
|
|
|
|
var NoSource SourceRef
|
|
|
|
|
|
|
|
func (r SourceRef) String() string {
|
|
|
|
return fmt.Sprintf("%s.%s", r.Type, r.Name)
|
|
|
|
}
|