opentofu/command/init.go

447 lines
14 KiB
Go
Raw Normal View History

2014-09-26 18:03:39 -05:00
package command
import (
"fmt"
"log"
2014-09-26 18:03:39 -05:00
"os"
"sort"
2014-09-26 18:03:39 -05:00
"strings"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform/backend"
2014-09-26 18:03:39 -05:00
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/helper/variables"
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/plugin/discovery"
"github.com/hashicorp/terraform/terraform"
2014-09-26 18:03:39 -05:00
)
// InitCommand is a Command implementation that takes a Terraform
// module and clones it to the working directory.
type InitCommand struct {
Meta
2017-05-04 12:01:05 -05:00
// getPlugins is for the -get-plugins flag
getPlugins bool
// providerInstaller is used to download and install providers that
// aren't found locally. This uses a discovery.ProviderInstaller instance
// by default, but it can be overridden here as a way to mock fetching
// providers for tests.
providerInstaller discovery.Installer
2014-09-26 18:03:39 -05:00
}
func (c *InitCommand) Run(args []string) int {
var flagBackend, flagGet, flagUpgrade bool
var flagConfigExtra map[string]interface{}
var flagPluginPath FlagStringSlice
2014-09-26 18:03:39 -05:00
args = c.Meta.process(args, false)
2017-01-18 22:50:45 -06:00
cmdFlags := c.flagSet("init")
cmdFlags.BoolVar(&flagBackend, "backend", true, "")
cmdFlags.Var((*variables.FlagAny)(&flagConfigExtra), "backend-config", "")
2017-01-18 22:50:45 -06:00
cmdFlags.BoolVar(&flagGet, "get", true, "")
cmdFlags.BoolVar(&c.getPlugins, "get-plugins", true, "")
cmdFlags.BoolVar(&c.forceInitCopy, "force-copy", false, "suppress prompts about copying state data")
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
cmdFlags.BoolVar(&c.reconfigure, "reconfigure", false, "reconfigure")
cmdFlags.BoolVar(&flagUpgrade, "upgrade", false, "")
cmdFlags.Var(&flagPluginPath, "plugin-dir", "plugin directory")
2014-09-26 18:03:39 -05:00
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
return 1
}
if len(flagPluginPath) > 0 {
c.pluginPath = flagPluginPath
c.getPlugins = false
}
2017-05-04 12:01:05 -05:00
// set getProvider if we don't have a test version already
if c.providerInstaller == nil {
c.providerInstaller = &discovery.ProviderInstaller{
Dir: c.pluginDir(),
PluginProtocolVersion: plugin.Handshake.ProtocolVersion,
}
2017-05-04 12:01:05 -05:00
}
2017-01-18 22:50:45 -06:00
// Validate the arg count
2014-09-26 18:03:39 -05:00
args = cmdFlags.Args()
if len(args) > 1 {
c.Ui.Error("The init command expects at most one argument.\n")
2014-09-26 18:03:39 -05:00
cmdFlags.Usage()
return 1
2017-01-18 22:50:45 -06:00
}
if err := c.storePluginPath(c.pluginPath); err != nil {
c.Ui.Error(fmt.Sprintf("Error saving -plugin-path values: %s", err))
return 1
}
2017-01-18 22:50:45 -06:00
// Get our pwd. We don't always need it but always getting it is easier
// than the logic to determine if it is or isn't needed.
pwd, err := os.Getwd()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err))
2014-09-26 18:03:39 -05:00
return 1
}
2017-01-18 22:50:45 -06:00
// Get the path and source module to copy
path := pwd
if len(args) == 1 {
path = args[0]
2014-09-26 18:03:39 -05:00
}
2017-01-18 22:50:45 -06:00
// This will track whether we outputted anything so that we know whether
// to output a newline before the success message
var header bool
// If our directory is empty, then we're done. We can't get or setup
// the backend with an empty directory.
2014-09-26 18:03:39 -05:00
if empty, err := config.IsEmptyDir(path); err != nil {
c.Ui.Error(fmt.Sprintf(
2017-01-18 22:50:45 -06:00
"Error checking configuration: %s", err))
2014-09-26 18:03:39 -05:00
return 1
2017-01-18 22:50:45 -06:00
} else if empty {
c.Ui.Output(c.Colorize().Color(strings.TrimSpace(outputInitEmpty)))
return 0
2014-09-26 18:03:39 -05:00
}
var back backend.Backend
2017-01-18 22:50:45 -06:00
// If we're performing a get or loading the backend, then we perform
// some extra tasks.
if flagGet || flagBackend {
conf, err := c.Config(path)
if err != nil {
2017-01-18 22:50:45 -06:00
c.Ui.Error(fmt.Sprintf(
"Error loading configuration: %s", err))
return 1
}
2017-01-18 22:50:45 -06:00
// If we requested downloading modules and have modules in the config
if flagGet && len(conf.Modules) > 0 {
header = true
getMode := module.GetModeGet
if flagUpgrade {
getMode = module.GetModeUpdate
c.Ui.Output(c.Colorize().Color(fmt.Sprintf(
"[reset][bold]Upgrading modules...")))
} else {
c.Ui.Output(c.Colorize().Color(fmt.Sprintf(
"[reset][bold]Downloading modules...")))
}
if err := getModules(&c.Meta, path, getMode); err != nil {
c.Ui.Error(fmt.Sprintf(
2017-01-18 22:50:45 -06:00
"Error downloading modules: %s", err))
return 1
}
2017-01-18 22:50:45 -06:00
}
// If we're requesting backend configuration or looking for required
// plugins, load the backend
if flagBackend {
2017-01-18 22:50:45 -06:00
header = true
// Only output that we're initializing a backend if we have
// something in the config. We can be UNSETTING a backend as well
// in which case we choose not to show this.
if conf.Terraform != nil && conf.Terraform.Backend != nil {
c.Ui.Output(c.Colorize().Color(fmt.Sprintf(
"\n[reset][bold]Initializing the backend...")))
}
2017-01-18 22:50:45 -06:00
opts := &BackendOpts{
Config: conf,
ConfigExtra: flagConfigExtra,
Init: true,
2017-01-18 22:50:45 -06:00
}
if back, err = c.Backend(opts); err != nil {
2017-01-18 22:50:45 -06:00
c.Ui.Error(err.Error())
return 1
}
}
2017-01-18 22:50:45 -06:00
}
// Now that we have loaded all modules, check the module tree for missing providers.
sMgr, err := back.State(c.Workspace())
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error loading state: %s", err))
return 1
}
if err := sMgr.RefreshState(); err != nil {
c.Ui.Error(fmt.Sprintf(
"Error refreshing state: %s", err))
return 1
}
err = c.getProviders(path, sMgr.State(), flagUpgrade)
if err != nil {
// this function provides its own output
log.Printf("[ERROR] %s", err)
return 1
}
2017-01-18 22:50:45 -06:00
// If we outputted information, then we need to output a newline
// so that our success message is nicely spaced out from prior text.
if header {
c.Ui.Output("")
}
2017-01-18 22:50:45 -06:00
c.Ui.Output(c.Colorize().Color(strings.TrimSpace(outputInitSuccess)))
2014-09-26 18:03:39 -05:00
return 0
}
// Load the complete module tree, and fetch any missing providers.
// This method outputs its own Ui.
func (c *InitCommand) getProviders(path string, state *terraform.State, upgrade bool) error {
mod, err := c.Module(path)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting plugins: %s", err))
return err
}
if err := mod.Validate(); err != nil {
c.Ui.Error(fmt.Sprintf("Error getting plugins: %s", err))
return err
}
var available discovery.PluginMetaSet
if upgrade {
// If we're in upgrade mode, we ignore any auto-installed plugins
// in "available", causing us to reinstall and possibly upgrade them.
available = c.providerPluginManuallyInstalledSet()
} else {
available = c.providerPluginSet()
}
requirements := terraform.ModuleTreeDependencies(mod, state).AllPluginRequirements()
if len(requirements) == 0 {
// nothing to initialize
return nil
}
c.Ui.Output(c.Colorize().Color(
"\n[reset][bold]Initializing provider plugins...",
))
missing := c.missingPlugins(available, requirements)
var errs error
if c.getPlugins {
for provider, reqd := range missing {
c.Ui.Output(fmt.Sprintf("- downloading plugin for provider %q...", provider))
_, err := c.providerInstaller.Get(provider, reqd.Versions)
if err != nil {
c.Ui.Error(fmt.Sprintf(errProviderNotFound, err, provider, reqd.Versions))
errs = multierror.Append(errs, err)
}
}
if errs != nil {
return errs
}
} else if len(missing) > 0 {
// we have missing providers, but aren't going to try and download them
for provider, reqd := range missing {
c.Ui.Error(fmt.Sprintf(errProviderNotFound, err, provider, reqd.Versions))
errs = multierror.Append(errs, fmt.Errorf("missing provider %q", provider))
}
return errs
}
// With all the providers downloaded, we'll generate our lock file
// that ensures the provider binaries remain unchanged until we init
// again. If anything changes, other commands that use providers will
// fail with an error instructing the user to re-run this command.
available = c.providerPluginSet() // re-discover to see newly-installed plugins
chosen := choosePlugins(available, requirements)
digests := map[string][]byte{}
for name, meta := range chosen {
digest, err := meta.SHA256()
if err != nil {
c.Ui.Error(fmt.Sprintf("failed to read provider plugin %s: %s", meta.Path, err))
return err
}
digests[name] = digest
}
err = c.providerPluginsLock().Write(digests)
if err != nil {
c.Ui.Error(fmt.Sprintf("failed to save provider manifest: %s", err))
return err
}
if upgrade {
// Purge any auto-installed plugins that aren't being used.
purged, err := c.providerInstaller.PurgeUnused(chosen)
if err != nil {
// Failure to purge old plugins is not a fatal error
c.Ui.Warn(fmt.Sprintf("failed to purge unused plugins: %s", err))
}
if purged != nil {
for meta := range purged {
log.Printf("[DEBUG] Purged unused %s plugin %s", meta.Name, meta.Path)
}
}
}
// If any providers have "floating" versions (completely unconstrained)
// we'll suggest the user constrain with a pessimistic constraint to
// avoid implicitly adopting a later major release.
constraintSuggestions := make(map[string]discovery.ConstraintStr)
for name, meta := range chosen {
req := requirements[name]
if req == nil {
// should never happen, but we don't want to crash here, so we'll
// be cautious.
continue
}
if req.Versions.Unconstrained() && meta.Version != discovery.VersionZero {
// meta.Version.MustParse is safe here because our "chosen" metas
// were already filtered for validity of versions.
constraintSuggestions[name] = meta.Version.MustParse().MinorUpgradeConstraintStr()
}
}
if len(constraintSuggestions) != 0 {
names := make([]string, 0, len(constraintSuggestions))
for name := range constraintSuggestions {
names = append(names, name)
}
sort.Strings(names)
c.Ui.Output(outputInitProvidersUnconstrained)
for _, name := range names {
c.Ui.Output(fmt.Sprintf("* provider.%s: version = %q", name, constraintSuggestions[name]))
}
}
return nil
}
2014-09-26 18:03:39 -05:00
func (c *InitCommand) Help() string {
helpText := `
Usage: terraform init [options] [DIR]
2017-01-18 22:50:45 -06:00
Initialize a new or existing Terraform working directory by creating
2017-01-18 22:50:45 -06:00
initial files, loading any remote state, downloading modules, etc.
This is the first command that should be run for any new or existing
Terraform configuration per machine. This sets up all the local data
2017-04-26 09:10:04 -05:00
necessary to run Terraform that is typically not committed to version
2017-01-18 22:50:45 -06:00
control.
This command is always safe to run multiple times. Though subsequent runs
may give errors, this command will never delete your configuration or
state. Even so, if you have important information, please back it up prior
to running this command, just in case.
2014-09-26 18:03:39 -05:00
2017-01-18 22:50:45 -06:00
If no arguments are given, the configuration in this working directory
is initialized.
2014-09-26 18:03:39 -05:00
Options:
-backend=true Configure the backend for this configuration.
-backend-config=path This can be either a path to an HCL file with key/value
assignments (same format as terraform.tfvars) or a
'key=value' format. This is merged with what is in the
configuration file. This can be specified multiple
times. The backend type must be in the configuration
itself.
-force-copy Suppress prompts about copying state data. This is
equivalent to providing a "yes" to all confirmation
prompts.
2017-01-18 22:50:45 -06:00
-get=true Download any modules for this configuration.
-get-plugins=true Download any missing plugins for this configuration.
2017-01-18 22:50:45 -06:00
-input=true Ask for input if necessary. If false, will error if
input was required.
-lock=true Lock the state file when locking is supported.
-lock-timeout=0s Duration to retry a state lock.
2017-01-18 22:50:45 -06:00
-no-color If specified, output won't contain any color.
2015-06-22 07:14:01 -05:00
-plugin-dir Directory containing plugin binaries. This overrides all
default search paths for plugins, and prevents the
automatic installation of plugins. This flag can be used
multiple times.
-reconfigure Reconfigure the backend, ignoring any saved configuration.
-upgrade=false If installing modules (-get) or plugins (-get-plugins),
ignore previously-downloaded objects and install the
latest version allowed within configured constraints.
2014-09-26 18:03:39 -05:00
`
return strings.TrimSpace(helpText)
}
func (c *InitCommand) Synopsis() string {
2017-01-18 22:50:45 -06:00
return "Initialize a new or existing Terraform configuration"
2014-09-26 18:03:39 -05:00
}
2017-01-18 22:50:45 -06:00
const errInitCopyNotEmpty = `
The destination path contains Terraform configuration files. The init command
with a SOURCE parameter can only be used on a directory without existing
Terraform files.
Please resolve this issue and try again.
`
const outputInitEmpty = `
[reset][bold]Terraform initialized in an empty directory![reset]
The directory has no Terraform configuration files. You may begin working
with Terraform immediately by creating Terraform configuration files.
`
const outputInitSuccess = `
[reset][bold][green]Terraform has been successfully initialized![reset][green]
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
2017-01-18 22:50:45 -06:00
commands will detect it and remind you to do so if necessary.
`
const outputInitProvidersUnconstrained = `
The following providers do not have any version constraints in configuration,
so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.
`
const errProviderNotFound = `
[reset][red]%[1]s
[reset][bold][red]Error: Satisfying %[2]q, provider not found
[reset][red]A version of the %[2]q provider that satisfies all version
constraints could not be found. The requested version
constraints are shown below.
%[2]s = %[3]q[reset]
`