mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-21 23:41:41 -04:00
In previous versions of Packer, the parser would load a HCL file (in either HCL or JSON format), and process it in phases. This process meant that if the file was not valid in the first place, we'd have some duplicate errors first. Another thing is that sources and build blocks were not being parsed at the beginning of the parsing phase at all, but were discovered later in the process, when the data sources have been executed and variables have been evaluated. This commit changes the parsing to happen all at once, so we parse the files with the parser, which will fail should one file be malformatted, then we visit the body once using the top-level schema for a Packer build template, handling misplaced blocks once in the process. Then, since the builds and sources may contain some dynamic blocks that need to be expanded once their context is ready (i.e. the variables/datasources they depend on), we can expand the build block for them, and populate their respective objects. This last step should also be adapted for datasources later, as they may need to support dynamic blocks, and be expanded lazily.
160 lines
4.8 KiB
Go
160 lines
4.8 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package hcl2template
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strconv"
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
hcl2shim "github.com/hashicorp/packer/hcl2template/shim"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
// SourceBlock references an HCL 'source' block to be used in a build for
|
|
// example.
|
|
type SourceBlock struct {
|
|
// Type of source; ex: virtualbox-iso
|
|
Type string
|
|
// Given name; if any
|
|
Name string
|
|
|
|
// ready signals that the source block is ready for use, i.e. it does not
|
|
// need some dynamic expansion before being used.
|
|
Ready bool
|
|
|
|
block *hcl.Block
|
|
|
|
// LocalName can be set in a singular source block from a build block, it
|
|
// allows to give a special name to a build in the logs.
|
|
LocalName string
|
|
}
|
|
|
|
// SourceUseBlock is a SourceBlock 'usage' from a config stand point.
|
|
// For example when one uses `build.sources = ["..."]` or
|
|
// `build.source "..." {...}`.
|
|
type SourceUseBlock struct {
|
|
// reference to an actual source block definition, or SourceBlock.
|
|
SourceRef
|
|
|
|
// LocalName can be set in a singular source block from a build block, it
|
|
// allows to give a special name to a build in the logs.
|
|
LocalName string
|
|
|
|
// Rest of the body, in case the build.source block has more specific
|
|
// content
|
|
// Body can be expanded by a dynamic tag.
|
|
Body hcl.Body
|
|
}
|
|
|
|
func (b *SourceUseBlock) name() string {
|
|
if b.LocalName != "" {
|
|
return b.LocalName
|
|
}
|
|
return b.Name
|
|
}
|
|
|
|
func (b *SourceUseBlock) String() string {
|
|
return fmt.Sprintf("%s.%s", b.Type, b.name())
|
|
}
|
|
|
|
// EvalContext adds the values of the source to the passed eval context.
|
|
func (b *SourceUseBlock) ctyValues() map[string]cty.Value {
|
|
return map[string]cty.Value{
|
|
"type": cty.StringVal(b.Type),
|
|
"name": cty.StringVal(b.name()),
|
|
}
|
|
}
|
|
|
|
func (cfg *PackerConfig) startBuilder(source SourceUseBlock, ectx *hcl.EvalContext) (packersdk.Builder, hcl.Diagnostics, []string) {
|
|
var diags hcl.Diagnostics
|
|
|
|
builder, err := cfg.parser.PluginConfig.Builders.Start(source.Type)
|
|
if err != nil {
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
Severity: hcl.DiagError,
|
|
Summary: "Failed to load " + sourceLabel + " type",
|
|
Detail: err.Error(),
|
|
})
|
|
return builder, diags, nil
|
|
}
|
|
|
|
body := source.Body
|
|
// Add known values to source accessor in eval context.
|
|
ectx.Variables[sourcesAccessor] = cty.ObjectVal(source.ctyValues())
|
|
|
|
decoded, moreDiags := decodeHCL2Spec(body, ectx, builder)
|
|
diags = append(diags, moreDiags...)
|
|
if moreDiags.HasErrors() {
|
|
return builder, diags, nil
|
|
}
|
|
|
|
// In case of cty.Unknown values, this will write a equivalent placeholder of the same type
|
|
// Unknown types are not recognized by the json marshal during the RPC call and we have to do this here
|
|
// to avoid json parsing failures when running the validate command.
|
|
// We don't do this before so we can validate if variable types matches correctly on decodeHCL2Spec.
|
|
decoded = hcl2shim.WriteUnknownPlaceholderValues(decoded)
|
|
|
|
// 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_core_version"] = cfg.CorePackerVersionString
|
|
builderVars["packer_debug"] = strconv.FormatBool(cfg.debug)
|
|
builderVars["packer_force"] = strconv.FormatBool(cfg.force)
|
|
builderVars["packer_on_error"] = cfg.onError
|
|
|
|
generatedVars, warning, err := builder.Prepare(builderVars, decoded)
|
|
moreDiags = warningErrorsToDiags(cfg.Sources[source.SourceRef].block, warning, err)
|
|
diags = append(diags, moreDiags...)
|
|
return builder, diags, generatedVars
|
|
}
|
|
|
|
// These variables will populate the PackerConfig inside of the builders.
|
|
func (source *SourceUseBlock) builderVariables() map[string]string {
|
|
return map[string]string{
|
|
"packer_build_name": source.Name,
|
|
"packer_builder_type": source.Type,
|
|
}
|
|
}
|
|
|
|
func (source *SourceBlock) Ref() SourceRef {
|
|
return SourceRef{
|
|
Type: source.Type,
|
|
Name: source.Name,
|
|
}
|
|
}
|
|
|
|
// SourceRef is a nice way to put `virtualbox-iso.source_name`
|
|
type SourceRef struct {
|
|
// Type of the source, for example `virtualbox-iso`
|
|
Type string
|
|
// Name of the source, for example `source_name`
|
|
Name string
|
|
|
|
// No other field should be added to the SourceRef because we used that
|
|
// struct as a map accessor in many places.
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
func listAvailableSourceNames(srcs map[SourceRef]SourceBlock) []string {
|
|
res := make([]string, 0, len(srcs))
|
|
for k := range srcs {
|
|
res = append(res, k.String())
|
|
}
|
|
sort.Strings(res)
|
|
return res
|
|
}
|