2014-09-26 18:03:39 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-01-19 19:13:19 -06:00
|
|
|
"path/filepath"
|
2017-06-01 19:57:43 -05:00
|
|
|
"sort"
|
2014-09-26 18:03:39 -05:00
|
|
|
"strings"
|
|
|
|
|
2017-05-03 10:02:47 -05:00
|
|
|
getter "github.com/hashicorp/go-getter"
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
2014-09-26 18:03:39 -05:00
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/config/module"
|
2017-03-17 01:27:05 -05:00
|
|
|
"github.com/hashicorp/terraform/helper/variables"
|
2017-06-01 13:36:30 -05:00
|
|
|
"github.com/hashicorp/terraform/plugin"
|
2017-05-03 10:02:47 -05:00
|
|
|
"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
|
|
|
|
|
|
|
// getProvider fetches providers that aren't found locally, and unpacks
|
|
|
|
// them into the dst directory.
|
|
|
|
// This uses discovery.GetProvider by default, but it provided here as a
|
|
|
|
// way to mock fetching providers for tests.
|
2017-06-01 13:36:30 -05:00
|
|
|
getProvider func(dst, provider string, req discovery.Constraints, protoVersion uint) error
|
2014-09-26 18:03:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InitCommand) Run(args []string) int {
|
2017-05-03 10:02:47 -05:00
|
|
|
var flagBackend, flagGet, flagGetPlugins bool
|
2017-03-17 01:27:05 -05:00
|
|
|
var flagConfigExtra map[string]interface{}
|
2017-03-21 14:05:51 -05:00
|
|
|
|
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, "")
|
2017-03-17 01:27:05 -05:00
|
|
|
cmdFlags.Var((*variables.FlagAny)(&flagConfigExtra), "backend-config", "")
|
2017-01-18 22:50:45 -06:00
|
|
|
cmdFlags.BoolVar(&flagGet, "get", true, "")
|
2017-05-03 10:02:47 -05:00
|
|
|
cmdFlags.BoolVar(&flagGetPlugins, "get-plugins", true, "")
|
2017-03-21 14:05:51 -05:00
|
|
|
cmdFlags.BoolVar(&c.forceInitCopy, "force-copy", false, "suppress prompts about copying state data")
|
2017-04-01 15:19:59 -05:00
|
|
|
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
|
|
|
|
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
|
2017-04-20 16:26:50 -05:00
|
|
|
cmdFlags.BoolVar(&c.reconfigure, "reconfigure", false, "reconfigure")
|
2017-03-21 14:05:51 -05:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-04 12:01:05 -05:00
|
|
|
// set getProvider if we don't have a test version already
|
|
|
|
if c.getProvider == nil {
|
|
|
|
c.getProvider = discovery.GetProvider
|
|
|
|
}
|
|
|
|
|
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) > 2 {
|
|
|
|
c.Ui.Error("The init command expects at most two arguments.\n")
|
|
|
|
cmdFlags.Usage()
|
|
|
|
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
|
|
|
|
var path string
|
|
|
|
var source string
|
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
path = pwd
|
|
|
|
case 1:
|
|
|
|
path = pwd
|
|
|
|
source = args[0]
|
|
|
|
case 2:
|
|
|
|
source = args[0]
|
2014-09-26 18:03:39 -05:00
|
|
|
path = args[1]
|
2017-01-18 22:50:45 -06:00
|
|
|
default:
|
|
|
|
panic("assertion failed on arg count")
|
2014-09-26 18:03:39 -05:00
|
|
|
}
|
|
|
|
|
2016-01-19 19:13:19 -06:00
|
|
|
// Set the state out path to be the path requested for the module
|
|
|
|
// to be copied. This ensures any remote states gets setup in the
|
|
|
|
// proper directory.
|
2016-07-20 17:55:05 -05:00
|
|
|
c.Meta.dataDir = filepath.Join(path, DefaultDataDir)
|
2016-01-19 19:13:19 -06: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 we have a source, copy it
|
|
|
|
if source != "" {
|
|
|
|
c.Ui.Output(c.Colorize().Color(fmt.Sprintf(
|
|
|
|
"[reset][bold]"+
|
|
|
|
"Initializing configuration from: %q...", source)))
|
|
|
|
if err := c.copySource(path, source, pwd); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error copying source: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2014-09-26 18:03:39 -05:00
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
header = true
|
2014-09-26 18:30:49 -05:00
|
|
|
}
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2017-05-03 10:02:47 -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 {
|
2017-05-01 16:47:53 -05:00
|
|
|
conf, err := c.Config(path)
|
2014-09-30 18:05:40 -05:00
|
|
|
if err != nil {
|
2017-01-18 22:50:45 -06:00
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error loading configuration: %s", err))
|
2014-10-09 19:16:17 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
c.Ui.Output(c.Colorize().Color(fmt.Sprintf(
|
|
|
|
"[reset][bold]" +
|
|
|
|
"Downloading modules (if any)...")))
|
|
|
|
if err := getModules(&c.Meta, path, module.GetModeGet); err != nil {
|
2015-02-22 12:49:31 -06:00
|
|
|
c.Ui.Error(fmt.Sprintf(
|
2017-01-18 22:50:45 -06:00
|
|
|
"Error downloading modules: %s", err))
|
2015-02-22 12:49:31 -06:00
|
|
|
return 1
|
|
|
|
}
|
2017-05-03 10:02:47 -05:00
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
}
|
|
|
|
|
2017-05-03 10:02:47 -05:00
|
|
|
// If we're requesting backend configuration or looking for required
|
|
|
|
// plugins, load the backend
|
|
|
|
if flagBackend || flagGetPlugins {
|
2017-01-18 22:50:45 -06:00
|
|
|
header = true
|
|
|
|
|
2017-02-15 17:44:53 -06:00
|
|
|
// 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(
|
|
|
|
"[reset][bold]" +
|
|
|
|
"Initializing the backend...")))
|
|
|
|
}
|
2017-01-18 22:50:45 -06:00
|
|
|
|
|
|
|
opts := &BackendOpts{
|
2017-05-01 16:47:53 -05:00
|
|
|
Config: conf,
|
2017-03-17 01:27:05 -05:00
|
|
|
ConfigExtra: flagConfigExtra,
|
|
|
|
Init: true,
|
2017-01-18 22:50:45 -06:00
|
|
|
}
|
2017-05-03 10:02:47 -05:00
|
|
|
if back, err = c.Backend(opts); err != nil {
|
2017-01-18 22:50:45 -06:00
|
|
|
c.Ui.Error(err.Error())
|
2015-02-22 12:49:31 -06:00
|
|
|
return 1
|
|
|
|
}
|
2014-09-30 18:05:40 -05:00
|
|
|
}
|
2017-01-18 22:50:45 -06:00
|
|
|
}
|
2014-10-09 19:16:17 -05:00
|
|
|
|
2017-05-03 10:02:47 -05:00
|
|
|
// Now that we have loaded all modules, check the module tree for missing providers
|
|
|
|
if flagGetPlugins {
|
|
|
|
sMgr, err := back.State(c.Env())
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-06-01 19:57:43 -05:00
|
|
|
c.Ui.Output(c.Colorize().Color(
|
|
|
|
"[reset][bold]Initializing provider plugins...",
|
|
|
|
))
|
|
|
|
|
2017-05-03 10:02:47 -05:00
|
|
|
err = c.getProviders(path, sMgr.State())
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error getting plugins: %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("")
|
2014-09-30 18:05:40 -05:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-03 10:02:47 -05:00
|
|
|
// load the complete module tree, and fetch any missing providers
|
|
|
|
func (c *InitCommand) getProviders(path string, state *terraform.State) error {
|
|
|
|
mod, err := c.Module(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mod.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-24 19:35:46 -05:00
|
|
|
available := c.providerPluginSet()
|
2017-05-03 10:02:47 -05:00
|
|
|
requirements := terraform.ModuleTreeDependencies(mod, state).AllPluginRequirements()
|
2017-05-24 19:35:46 -05:00
|
|
|
missing := c.missingPlugins(available, requirements)
|
2017-05-03 10:02:47 -05:00
|
|
|
|
|
|
|
dst := c.pluginDir()
|
|
|
|
for provider, reqd := range missing {
|
2017-06-01 13:36:30 -05:00
|
|
|
err := c.getProvider(dst, provider, reqd.Versions, plugin.Handshake.ProtocolVersion)
|
2017-05-03 10:02:47 -05:00
|
|
|
// TODO: return all errors
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 19:35:46 -05:00
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return fmt.Errorf("failed to read provider plugin %s: %s", meta.Path, err)
|
|
|
|
}
|
|
|
|
digests[name] = digest
|
|
|
|
}
|
|
|
|
err = c.providerPluginsLock().Write(digests)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to save provider manifest: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-06-01 19:57:43 -05:00
|
|
|
// 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.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]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 10:02:47 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
func (c *InitCommand) copySource(dst, src, pwd string) error {
|
|
|
|
// Verify the directory is empty
|
|
|
|
if empty, err := config.IsEmptyDir(dst); err != nil {
|
|
|
|
return fmt.Errorf("Error checking on destination path: %s", err)
|
|
|
|
} else if !empty {
|
|
|
|
return fmt.Errorf(strings.TrimSpace(errInitCopyNotEmpty))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect
|
|
|
|
source, err := getter.Detect(src, pwd, getter.Detectors)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error with module source: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get it!
|
|
|
|
return module.GetCopy(dst, source)
|
|
|
|
}
|
|
|
|
|
2014-09-26 18:03:39 -05:00
|
|
|
func (c *InitCommand) Help() string {
|
|
|
|
helpText := `
|
2017-01-18 22:50:45 -06:00
|
|
|
Usage: terraform init [options] [SOURCE] [PATH]
|
|
|
|
|
|
|
|
Initialize a new or existing Terraform environment by creating
|
|
|
|
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 blow away your environment 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
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
If one or two arguments are given, the first is a SOURCE of a module to
|
|
|
|
download to the second argument PATH. After downloading the module to PATH,
|
|
|
|
the configuration will be initialized as if this command were called pointing
|
|
|
|
only to that PATH. PATH must be empty of any Terraform files. Any
|
|
|
|
conflicting non-Terraform files will be overwritten. The module download
|
|
|
|
is a copy. If you're downloading a module from Git, it will not preserve
|
|
|
|
Git history.
|
2014-09-26 18:03:39 -05:00
|
|
|
|
2014-09-30 18:05:40 -05:00
|
|
|
Options:
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
-backend=true Configure the backend for this environment.
|
2014-12-04 21:06:47 -06:00
|
|
|
|
2017-03-17 01:27:05 -05:00
|
|
|
-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.
|
2014-09-30 18:05:40 -05:00
|
|
|
|
2017-04-20 16:26:50 -05:00
|
|
|
-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.
|
|
|
|
|
2017-05-03 10:02:47 -05:00
|
|
|
-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.
|
|
|
|
|
2017-04-01 15:19:59 -05:00
|
|
|
-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
|
|
|
|
2017-04-20 16:26:50 -05:00
|
|
|
-reconfigure Reconfigure the backend, ignoring any saved configuration.
|
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 environment. If you forget, other
|
|
|
|
commands will detect it and remind you to do so if necessary.
|
|
|
|
`
|
2017-06-01 19:57:43 -05:00
|
|
|
|
|
|
|
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.
|
|
|
|
`
|