From bc85854a53b48b44c5e612b79fb212356acd3cb4 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 9 Nov 2020 12:29:53 -0800 Subject: [PATCH] refactor packer version out of hcltemplate code. --- command/build.go | 11 +++++++---- common/packer_config.go | 1 + hcl2template/parser.go | 22 ++++++++++++++-------- hcl2template/types.packer_config.go | 7 +++++-- hcl2template/version_required.go | 8 ++++---- packer/build.go | 7 +++++++ 6 files changed, 38 insertions(+), 18 deletions(-) diff --git a/command/build.go b/command/build.go index 38901701e..ce1421315 100644 --- a/command/build.go +++ b/command/build.go @@ -16,6 +16,7 @@ import ( "github.com/hashicorp/packer/hcl2template" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template" + "github.com/hashicorp/packer/version" "golang.org/x/sync/semaphore" "github.com/hako/durafmt" @@ -62,10 +63,12 @@ func (c *BuildCommand) ParseArgs(args []string) (*BuildArgs, int) { func (m *Meta) GetConfigFromHCL(cla *MetaArgs) (*hcl2template.PackerConfig, int) { parser := &hcl2template.Parser{ - Parser: hclparse.NewParser(), - BuilderSchemas: m.CoreConfig.Components.BuilderStore, - ProvisionersSchemas: m.CoreConfig.Components.ProvisionerStore, - PostProcessorsSchemas: m.CoreConfig.Components.PostProcessorStore, + CorePackerVersion: version.SemVer, + CorePackerVersionString: version.FormattedVersion(), + Parser: hclparse.NewParser(), + BuilderSchemas: m.CoreConfig.Components.BuilderStore, + ProvisionersSchemas: m.CoreConfig.Components.ProvisionerStore, + PostProcessorsSchemas: m.CoreConfig.Components.PostProcessorStore, } cfg, diags := parser.Parse(cla.Path, cla.VarFiles, cla.Vars) return cfg, writeDiags(m.Ui, parser.Files(), diags) diff --git a/common/packer_config.go b/common/packer_config.go index dcf77f9fb..3e2c87225 100644 --- a/common/packer_config.go +++ b/common/packer_config.go @@ -6,6 +6,7 @@ package common type PackerConfig struct { PackerBuildName string `mapstructure:"packer_build_name"` PackerBuilderType string `mapstructure:"packer_builder_type"` + PackerCoreVersion bool `mapstructure:"packer_core_version"` PackerDebug bool `mapstructure:"packer_debug"` PackerForce bool `mapstructure:"packer_force"` PackerOnError string `mapstructure:"packer_on_error"` diff --git a/hcl2template/parser.go b/hcl2template/parser.go index 409157f02..2a856a16f 100644 --- a/hcl2template/parser.go +++ b/hcl2template/parser.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" + "github.com/hashicorp/go-version" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/ext/dynblock" "github.com/hashicorp/hcl/v2/hclparse" @@ -47,6 +48,10 @@ var packerBlockSchema = &hcl.BodySchema{ // the parsed HCL and then return a []packer.Build. Packer will use that list // of Builds to run everything in order. type Parser struct { + CorePackerVersion *version.Version + + CorePackerVersionString string + *hclparse.Parser BuilderSchemas packer.BuilderStore @@ -115,13 +120,14 @@ func (p *Parser) Parse(filename string, varFiles []string, argVars map[string]st }) } cfg := &PackerConfig{ - Basedir: basedir, - Cwd: wd, - builderSchemas: p.BuilderSchemas, - provisionersSchemas: p.ProvisionersSchemas, - postProcessorsSchemas: p.PostProcessorsSchemas, - parser: p, - files: files, + Basedir: basedir, + Cwd: wd, + CorePackerVersionString: p.CorePackerVersionString, + builderSchemas: p.BuilderSchemas, + provisionersSchemas: p.ProvisionersSchemas, + postProcessorsSchemas: p.PostProcessorsSchemas, + parser: p, + files: files, } for _, file := range files { @@ -133,7 +139,7 @@ func (p *Parser) Parse(filename string, varFiles []string, argVars map[string]st // Before we go further, we'll check to make sure this version can read // that file, so we can produce a version-related error message rather than // potentially-confusing downstream errors. - versionDiags := cfg.CheckCoreVersionRequirements() + versionDiags := cfg.CheckCoreVersionRequirements(p.CorePackerVersion) diags = append(diags, versionDiags...) if versionDiags.HasErrors() { return cfg, diags diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index daf80a209..58938faf6 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -9,7 +9,6 @@ import ( "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/hclsyntax" "github.com/hashicorp/packer/packer" - "github.com/hashicorp/packer/version" "github.com/zclconf/go-cty/cty" ) @@ -21,6 +20,10 @@ type PackerConfig struct { } // Directory where the config files are defined Basedir string + + // Core Packer version, for reference by plugins and template functions. + CorePackerVersionString string + // directory Packer was called from Cwd string @@ -84,7 +87,7 @@ func (cfg *PackerConfig) EvalContext(variables map[string]cty.Value) *hcl.EvalCo }), buildAccessor: cty.UnknownVal(cty.EmptyObject), packerAccessor: cty.ObjectVal(map[string]cty.Value{ - "version": cty.StringVal(version.FormattedVersion()), + "version": cty.StringVal(cfg.CorePackerVersionString), }), pathVariablesAccessor: cty.ObjectVal(map[string]cty.Value{ "cwd": cty.StringVal(strings.ReplaceAll(cfg.Cwd, `\`, `/`)), diff --git a/hcl2template/version_required.go b/hcl2template/version_required.go index 516d84823..10d50bb82 100644 --- a/hcl2template/version_required.go +++ b/hcl2template/version_required.go @@ -3,8 +3,8 @@ package hcl2template import ( "fmt" + "github.com/hashicorp/go-version" "github.com/hashicorp/hcl/v2" - pkrversion "github.com/hashicorp/packer/version" ) // CheckCoreVersionRequirements visits each of the block in the given @@ -14,7 +14,7 @@ import ( // The returned diagnostics will contain errors if any constraints do not match. // The returned diagnostics might also return warnings, which should be // displayed to the user. -func (cfg *PackerConfig) CheckCoreVersionRequirements() hcl.Diagnostics { +func (cfg *PackerConfig) CheckCoreVersionRequirements(coreVersion *version.Version) hcl.Diagnostics { if cfg == nil { return nil } @@ -22,13 +22,13 @@ func (cfg *PackerConfig) CheckCoreVersionRequirements() hcl.Diagnostics { var diags hcl.Diagnostics for _, constraint := range cfg.Packer.VersionConstraints { - if !constraint.Required.Check(pkrversion.SemVer) { + if !constraint.Required.Check(coreVersion) { diags = diags.Append(&hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Unsupported Packer Core version", Detail: fmt.Sprintf( "This configuration does not support Packer version %s. To proceed, either choose another supported Packer version or update this version constraint. Version constraints are normally set for good reason, so updating the constraint may lead to other errors or unexpected behavior.", - pkrversion.String(), + coreVersion.String(), ), Subject: constraint.DeclRange.Ptr(), }) diff --git a/packer/build.go b/packer/build.go index 75d978e23..e054d2b55 100644 --- a/packer/build.go +++ b/packer/build.go @@ -7,6 +7,7 @@ import ( "sync" "github.com/hashicorp/packer/common/packerbuilderdata" + "github.com/hashicorp/packer/version" ) const ( @@ -19,6 +20,11 @@ const ( // such who want to make use of this. BuilderTypeConfigKey = "packer_builder_type" + // this is the key in the configuration that is set to the version of the + // Packer Core. This can be used by plugins to set user agents, etc, without + // having to import the Core to find out the Packer version. + CoreVersionConfigKey = "packer_core_version" + // This is the key in configurations that is set to "true" when Packer // debugging is enabled. DebugConfigKey = "packer_debug" @@ -160,6 +166,7 @@ func (b *CoreBuild) Prepare() (warn []string, err error) { packerConfig := map[string]interface{}{ BuildNameConfigKey: b.Type, BuilderTypeConfigKey: b.BuilderType, + CoreVersionConfigKey: version.Version, DebugConfigKey: b.debug, ForceConfigKey: b.force, OnErrorConfigKey: b.onError,