2014-09-27 01:03:39 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/config/module"
|
2014-10-01 01:05:40 +02:00
|
|
|
"github.com/hashicorp/terraform/remote"
|
|
|
|
"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 {
|
2014-10-01 01:05:40 +02:00
|
|
|
var remoteConf terraform.RemoteState
|
2014-09-27 01:03:39 +02:00
|
|
|
args = c.Meta.process(args, false)
|
|
|
|
cmdFlags := flag.NewFlagSet("init", flag.ContinueOnError)
|
2014-10-01 01:05:40 +02:00
|
|
|
cmdFlags.StringVar(&remoteConf.Name, "remote", "", "")
|
|
|
|
cmdFlags.StringVar(&remoteConf.Server, "remote-server", "", "")
|
|
|
|
cmdFlags.StringVar(&remoteConf.AuthToken, "remote-auth", "", "")
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 01:05:40 +02:00
|
|
|
// Validate the remote configuration
|
|
|
|
if !remoteConf.Empty() {
|
|
|
|
if err := remote.ValidateConfig(&remoteConf); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("%s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
source, err = module.Detect(source, pwd)
|
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
|
|
|
|
if !remoteConf.Empty() {
|
2014-10-03 20:14:39 +02:00
|
|
|
change, err := remote.RefreshState(&remoteConf)
|
2014-10-01 01:05:40 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
2014-10-03 20:14:39 +02:00
|
|
|
"Failed to refresh from remote state: %v", err))
|
2014-10-01 01:05:40 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-10-03 20:14:39 +02:00
|
|
|
// Log the change that took place
|
|
|
|
c.Ui.Output(fmt.Sprintf("%s", change))
|
|
|
|
|
|
|
|
// Use an error exit code if the update was not a success
|
|
|
|
if !change.SuccessfulPull() {
|
2014-10-01 01:05:40 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
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:
|
|
|
|
|
|
|
|
-remote=name Name of the state file in the state storage server.
|
|
|
|
Optional, default does not use remote storage.
|
|
|
|
|
|
|
|
-remote-auth=token Authentication token for state storage server.
|
|
|
|
Optional, defaults to blank.
|
|
|
|
|
|
|
|
-remote-server=url URL of the remote storage server.
|
|
|
|
|
2014-09-27 01:03:39 +02:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InitCommand) Synopsis() string {
|
|
|
|
return "Initializes Terraform configuration from a module"
|
|
|
|
}
|