2014-09-27 01:03:39 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-01-20 02:13:19 +01:00
|
|
|
"path/filepath"
|
2014-09-27 01:03:39 +02:00
|
|
|
"strings"
|
|
|
|
|
2015-10-15 22:48:58 +02:00
|
|
|
"github.com/hashicorp/go-getter"
|
2014-09-27 01:03:39 +02:00
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/config/module"
|
2014-10-01 01:05:40 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-09-27 01:03:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// InitCommand is a Command implementation that takes a Terraform
|
|
|
|
// module and clones it to the working directory.
|
|
|
|
type InitCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InitCommand) Run(args []string) int {
|
2015-02-23 19:56:27 +01:00
|
|
|
var remoteBackend string
|
2014-09-27 01:03:39 +02:00
|
|
|
args = c.Meta.process(args, false)
|
2015-02-23 19:56:27 +01:00
|
|
|
remoteConfig := make(map[string]string)
|
2014-09-27 01:03:39 +02:00
|
|
|
cmdFlags := flag.NewFlagSet("init", flag.ContinueOnError)
|
2015-02-23 19:56:27 +01:00
|
|
|
cmdFlags.StringVar(&remoteBackend, "backend", "", "")
|
core: Allow lists and maps as variable overrides
Terraform 0.7 introduces lists and maps as first-class values for
variables, in addition to string values which were previously available.
However, there was previously no way to override the default value of a
list or map, and the functionality for overriding specific map keys was
broken.
Using the environment variable method for setting variable values, there
was previously no way to give a variable a value of a list or map. These
now support HCL for individual values - specifying:
TF_VAR_test='["Hello", "World"]'
will set the variable `test` to a two-element list containing "Hello"
and "World". Specifying
TF_VAR_test_map='{"Hello = "World", "Foo" = "bar"}'
will set the variable `test_map` to a two-element map with keys "Hello"
and "Foo", and values "World" and "bar" respectively.
The same logic is applied to `-var` flags, and the file parsed by
`-var-files` ("autoVariables").
Note that care must be taken to not run into shell expansion for `-var-`
flags and environment variables.
We also merge map keys where appropriate. The override syntax has
changed (to be noted in CHANGELOG as a breaking change), so several
tests needed their syntax updating from the old `amis.us-east-1 =
"newValue"` style to `amis = "{ "us-east-1" = "newValue"}"` style as
defined in TF-002.
In order to continue supporting the `-var "foo=bar"` type of variable
flag (which is not valid HCL), a special case error is checked after HCL
parsing fails, and the old code path runs instead.
2016-07-21 03:38:26 +02:00
|
|
|
cmdFlags.Var((*FlagStringKV)(&remoteConfig), "backend-config", "config")
|
2014-09-27 01:03:39 +02:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:26:12 +01:00
|
|
|
remoteBackend = strings.ToLower(remoteBackend)
|
|
|
|
|
2014-09-27 01:03:39 +02:00
|
|
|
var path string
|
|
|
|
args = cmdFlags.Args()
|
|
|
|
if len(args) > 2 {
|
|
|
|
c.Ui.Error("The init command expects at most two arguments.\n")
|
|
|
|
cmdFlags.Usage()
|
|
|
|
return 1
|
|
|
|
} else if len(args) < 1 {
|
|
|
|
c.Ui.Error("The init command expects at least one arguments.\n")
|
|
|
|
cmdFlags.Usage()
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 2 {
|
|
|
|
path = args[1]
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
path, err = os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 02:13:19 +01: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-21 00:55:05 +02:00
|
|
|
c.Meta.dataDir = filepath.Join(path, DefaultDataDir)
|
2016-01-20 02:13:19 +01:00
|
|
|
|
2014-09-27 01:03:39 +02:00
|
|
|
source := args[0]
|
|
|
|
|
2014-09-27 01:30:49 +02:00
|
|
|
// Get our pwd since we need it
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error reading working directory: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-09-27 01:03:39 +02:00
|
|
|
// Verify the directory is empty
|
|
|
|
if empty, err := config.IsEmptyDir(path); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error checking on destination path: %s", err))
|
|
|
|
return 1
|
|
|
|
} else if !empty {
|
|
|
|
c.Ui.Error(
|
|
|
|
"The destination path has Terraform configuration files. The\n" +
|
|
|
|
"init command can only be used on a directory without existing Terraform\n" +
|
|
|
|
"files.")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-09-27 01:30:49 +02:00
|
|
|
// Detect
|
2015-10-15 22:48:58 +02:00
|
|
|
source, err = getter.Detect(source, pwd, getter.Detectors)
|
2014-09-27 01:03:39 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
2014-09-27 01:30:49 +02:00
|
|
|
"Error with module source: %s", err))
|
2014-09-27 01:03:39 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get it!
|
2014-09-27 01:30:49 +02:00
|
|
|
if err := module.GetCopy(path, source); err != nil {
|
2014-09-27 01:03:39 +02:00
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-10-01 01:05:40 +02:00
|
|
|
// Handle remote state if configured
|
2015-02-23 19:56:27 +01:00
|
|
|
if remoteBackend != "" {
|
2014-12-05 04:06:47 +01:00
|
|
|
var remoteConf terraform.RemoteState
|
|
|
|
remoteConf.Type = remoteBackend
|
2015-02-23 19:56:27 +01:00
|
|
|
remoteConf.Config = remoteConfig
|
2014-12-05 04:06:47 +01:00
|
|
|
|
2015-02-22 19:49:31 +01:00
|
|
|
state, err := c.State()
|
2014-10-01 01:05:40 +02:00
|
|
|
if err != nil {
|
2015-02-22 19:49:31 +01:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error checking for state: %s", err))
|
2014-10-10 02:16:17 +02:00
|
|
|
return 1
|
|
|
|
}
|
2015-02-22 19:49:31 +01:00
|
|
|
if state != nil {
|
|
|
|
s := state.State()
|
|
|
|
if !s.Empty() {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"State file already exists and is not empty! Please remove this\n" +
|
|
|
|
"state file before initializing. Note that removing the state file\n" +
|
|
|
|
"may result in a loss of information since Terraform uses this\n" +
|
|
|
|
"to track your infrastructure."))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if s.IsRemote() {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"State file already exists with remote state enabled! Please remove this\n" +
|
|
|
|
"state file before initializing. Note that removing the state file\n" +
|
|
|
|
"may result in a loss of information since Terraform uses this\n" +
|
|
|
|
"to track your infrastructure."))
|
|
|
|
return 1
|
|
|
|
}
|
2014-10-01 01:05:40 +02:00
|
|
|
}
|
2014-10-10 02:16:17 +02:00
|
|
|
|
|
|
|
// Initialize a blank state file with remote enabled
|
2015-03-05 01:17:30 +01:00
|
|
|
remoteCmd := &RemoteConfigCommand{
|
2014-10-10 02:16:17 +02:00
|
|
|
Meta: c.Meta,
|
|
|
|
remoteConf: remoteConf,
|
|
|
|
}
|
|
|
|
return remoteCmd.initBlankState()
|
2014-10-01 01:05:40 +02:00
|
|
|
}
|
2014-09-27 01:03:39 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InitCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: terraform init [options] SOURCE [PATH]
|
|
|
|
|
|
|
|
Downloads the module given by SOURCE into the PATH. The PATH defaults
|
|
|
|
to the working directory. PATH must be empty of any Terraform files.
|
|
|
|
Any conflicting non-Terraform files will be overwritten.
|
|
|
|
|
|
|
|
The module downloaded is a copy. If you're downloading a module from
|
|
|
|
Git, it will not preserve the Git history, it will only copy the
|
|
|
|
latest files.
|
|
|
|
|
2014-10-01 01:05:40 +02:00
|
|
|
Options:
|
|
|
|
|
2015-02-23 19:58:39 +01:00
|
|
|
-backend=atlas Specifies the type of remote backend. If not
|
|
|
|
specified, local storage will be used.
|
2014-12-05 04:06:47 +01:00
|
|
|
|
2015-02-23 19:58:39 +01:00
|
|
|
-backend-config="k=v" Specifies configuration for the remote storage
|
|
|
|
backend. This can be specified multiple times.
|
2014-10-01 01:05:40 +02:00
|
|
|
|
2015-06-22 14:14:01 +02:00
|
|
|
-no-color If specified, output won't contain any color.
|
|
|
|
|
2014-09-27 01:03:39 +02:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InitCommand) Synopsis() string {
|
|
|
|
return "Initializes Terraform configuration from a module"
|
|
|
|
}
|