forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
160 lines (137 loc) · 4.16 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package command
import (
"flag"
"fmt"
"os"
"strings"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/terraform"
)
// 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 {
var remoteBackend string
args = c.Meta.process(args, false)
remoteConfig := make(map[string]string)
cmdFlags := flag.NewFlagSet("init", flag.ContinueOnError)
cmdFlags.StringVar(&remoteBackend, "backend", "", "")
cmdFlags.Var((*FlagKV)(&remoteConfig), "backend-config", "config")
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))
}
}
source := args[0]
// 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
}
// 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
}
// Detect
source, err = module.Detect(source, pwd)
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error with module source: %s", err))
return 1
}
// Get it!
if err := module.GetCopy(path, source); err != nil {
c.Ui.Error(err.Error())
return 1
}
// Handle remote state if configured
if remoteBackend != "" {
var remoteConf terraform.RemoteState
remoteConf.Type = remoteBackend
remoteConf.Config = remoteConfig
state, err := c.State()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error checking for state: %s", err))
return 1
}
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
}
}
// Initialize a blank state file with remote enabled
remoteCmd := &RemoteConfigCommand{
Meta: c.Meta,
remoteConf: remoteConf,
}
return remoteCmd.initBlankState()
}
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.
Options:
-backend=atlas Specifies the type of remote backend. If not
specified, local storage will be used.
-backend-config="k=v" Specifies configuration for the remote storage
backend. This can be specified multiple times.
-no-color If specified, output won't contain any color.
`
return strings.TrimSpace(helpText)
}
func (c *InitCommand) Synopsis() string {
return "Initializes Terraform configuration from a module"
}