refactor packer version out of hcltemplate code.
This commit is contained in:
parent
2bf912bddf
commit
bc85854a53
|
@ -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,6 +63,8 @@ func (c *BuildCommand) ParseArgs(args []string) (*BuildArgs, int) {
|
|||
|
||||
func (m *Meta) GetConfigFromHCL(cla *MetaArgs) (*hcl2template.PackerConfig, int) {
|
||||
parser := &hcl2template.Parser{
|
||||
CorePackerVersion: version.SemVer,
|
||||
CorePackerVersionString: version.FormattedVersion(),
|
||||
Parser: hclparse.NewParser(),
|
||||
BuilderSchemas: m.CoreConfig.Components.BuilderStore,
|
||||
ProvisionersSchemas: m.CoreConfig.Components.ProvisionerStore,
|
||||
|
|
|
@ -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"`
|
||||
|
|
|
@ -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
|
||||
|
@ -117,6 +122,7 @@ func (p *Parser) Parse(filename string, varFiles []string, argVars map[string]st
|
|||
cfg := &PackerConfig{
|
||||
Basedir: basedir,
|
||||
Cwd: wd,
|
||||
CorePackerVersionString: p.CorePackerVersionString,
|
||||
builderSchemas: p.BuilderSchemas,
|
||||
provisionersSchemas: p.ProvisionersSchemas,
|
||||
postProcessorsSchemas: p.PostProcessorsSchemas,
|
||||
|
@ -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
|
||||
|
|
|
@ -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, `\`, `/`)),
|
||||
|
|
|
@ -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(),
|
||||
})
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue