terraform/internal/initwd/version_required.go

84 lines
2.9 KiB
Go
Raw Normal View History

command: "terraform init" can partially initialize for 0.12upgrade There are a few constructs from 0.11 and prior that cause 0.12 parsing to fail altogether, which previously created a chicken/egg problem because we need to install the providers in order to run "terraform 0.12upgrade" and thus fix the problem. This changes "terraform init" to use the new "early configuration" loader for module and provider installation. This is built on the more permissive parser in the terraform-config-inspect package, and so it allows us to read out the top-level blocks from the configuration while accepting legacy HCL syntax. In the long run this will let us do version compatibility detection before attempting a "real" config load, giving us better error messages for any future syntax additions, but in the short term the key thing is that it allows us to install the dependencies even if the configuration isn't fully valid. Because backend init still requires full configuration, this introduces a new mode of terraform init where it detects heuristically if it seems like we need to do a configuration upgrade and does a partial init if so, before finally directing the user to run "terraform 0.12upgrade" before running any other commands. The heuristic here is based on two assumptions: - If the "early" loader finds no errors but the normal loader does, the configuration is likely to be valid for Terraform 0.11 but not 0.12. - If there's already a version constraint in the configuration that excludes Terraform versions prior to v0.12 then the configuration is probably _already_ upgraded and so it's just a normal syntax error, even if the early loader didn't detect it. Once the upgrade process is removed in 0.13.0 (users will be required to go stepwise 0.11 -> 0.12 -> 0.13 to upgrade after that), some of this can be simplified to remove that special mode, but the idea of doing the dependency version checks against the liberal parser will remain valuable to increase our chances of reporting version-based incompatibilities rather than syntax errors as we add new features in future.
2019-01-14 20:11:00 +01:00
package initwd
import (
"fmt"
version "github.com/hashicorp/go-version"
"github.com/hashicorp/terraform/internal/earlyconfig"
"github.com/hashicorp/terraform/tfdiags"
tfversion "github.com/hashicorp/terraform/version"
)
// CheckCoreVersionRequirements visits each of the modules in the given
// early configuration tree and verifies that any given Core version constraints
// match with the version of Terraform Core that is being used.
//
// 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 CheckCoreVersionRequirements(earlyConfig *earlyconfig.Config) tfdiags.Diagnostics {
if earlyConfig == nil {
return nil
}
var diags tfdiags.Diagnostics
module := earlyConfig.Module
var constraints version.Constraints
for _, constraintStr := range module.RequiredCore {
constraint, err := version.NewConstraint(constraintStr)
if err != nil {
// Unfortunately the early config parser doesn't preserve a source
// location for this, so we're unable to indicate a specific
// location where this constraint came from, but we can at least
// say which module set it.
switch {
case len(earlyConfig.Path) == 0:
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Invalid provider version constraint",
fmt.Sprintf("Invalid version core constraint %q in the root module.", constraintStr),
))
default:
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Invalid provider version constraint",
fmt.Sprintf("Invalid version core constraint %q in %s.", constraintStr, earlyConfig.Path),
))
}
continue
}
constraints = append(constraints, constraint...)
}
if !constraints.Check(tfversion.SemVer) {
switch {
case len(earlyConfig.Path) == 0:
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Unsupported Terraform Core version",
fmt.Sprintf(
"This configuration does not support Terraform version %s. To proceed, either choose another supported Terraform version or update the root module's version constraint. Version constraints are normally set for good reason, so updating the constraint may lead to other errors or unexpected behavior.",
tfversion.String(),
),
))
default:
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Unsupported Terraform Core version",
fmt.Sprintf(
"Module %s (from %q) does not support Terraform version %s. To proceed, either choose another supported Terraform version or update the module's version constraint. Version constraints are normally set for good reason, so updating the constraint may lead to other errors or unexpected behavior.",
earlyConfig.Path, earlyConfig.SourceAddr, tfversion.String(),
),
))
}
}
for _, c := range earlyConfig.Children {
childDiags := CheckCoreVersionRequirements(c)
diags = diags.Append(childDiags)
}
return diags
}