-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
332 lines (256 loc) · 9.04 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"fmt"
"os"
"github.com/mergermarket/cdflow2/command"
"github.com/mergermarket/cdflow2/deploy"
"github.com/mergermarket/cdflow2/destroy"
cinit "github.com/mergermarket/cdflow2/init"
release "github.com/mergermarket/cdflow2/release/command"
"github.com/mergermarket/cdflow2/setup"
"github.com/mergermarket/cdflow2/shell"
"github.com/mergermarket/cdflow2/util"
)
var version = "undefined"
const globalOptions = `Global options:
--component COMPONENT_NAME - override component name (inferred from git by default).
--commit GIT_COMMIT - override the git commit (inferred from git by default).
--no-pull-config - don't pull the config container (must exist).
--no-pull-release - don't pull the release container (must exist).
--no-pull-terraform - don't pull the terraform container (must exist).
--version - print the version number and exit.
--help - print the help message and exit.`
const help = `
Usage:
cdflow2 [ GLOBALOPTS ] COMMAND [ ARGS ]
Commands:
setup - configure your pipeline
init [ OPTS ] - initialize a new project
release [ OPTS ] VERSION - build and publish a new software artifact
deploy [ OPTS ] ENV VERSION - create & update infrastructure using software artifact
destroy [ OPTS ] ENV VERSION - destroy all Terraform managed infrastructure in ENV
shell ENV [ OPTS ] [ SHELLARGS ] - access terraform for debugging and tf state manipulation
help [ COMMAND ] - display detailed help and usage information for a command
` + globalOptions
const releaseHelp = `
Usage:
cdflow2 [ GLOBALOPTS ] release [ OPTS ] VERSION
Args:
VERSION - the version being released. We recommend using evergreen version numbers (i.e. simple incrementing integers,
probably from your CI service), combined with something to identify the commit - e.g. "34-a5dbc4a7".
Options:
--release-data | -r - add key/value to release metadata (i.e. --release-data foo=bar).
--terraform-log-level | -t - set Terraform log level (TF_LOG), useful for debugging.
` + globalOptions
const deployHelp = `
Usage:
cdflow2 [ GLOBALOPTS ] deploy [ OPTS ] ENV VERSION
Args:
ENV - the environment being deployed to.
VERSION - the version being deployed (must match what was released).
Options:
--plan-only | -p - create the terraform plan only, don't apply.
--new-state | -n - allow run without a pre-existing tfstate file.
--error-on-destroy | -e - fail if a plan return any resources to destroy.
--terraform-log-level | -t - set Terraform log level (TF_LOG), useful for debugging.
` + globalOptions
const setupHelp = `
Usage:
cdflow2 [ GLOBALARGS ] setup
` + globalOptions
const shellHelp = `
Usage:
cdflow2 [ GLOBALOPTS ] shell ENV [ OPTS ] [ SHELLARGS ]
Args:
ENV - the environment containing the deployment.
Options:
--version | -v - followed by the name of which version to interract with (must match a pre-existing release).
--terraform-log-level | -t - set Terraform log level (TF_LOG), useful for debugging.
Shell Arguments:
The shell arguments are passed to shell
ex: (cdflow2 shell demo test.sh)
(cdflow2 shell demo -v v1.0 -- -c "echo test")
` + globalOptions
const destroyHelp = `
Usage:
cdflow2 [ GLOBALOPTS ] destroy [ OPTS ] ENV VERSION
Args:
ENV - the environment containing the infrastructure being destroyed.
VERSION - the version to destroy (must match a pre-existing release).
Options:
--plan-only | -p - generate an execution plan only, don't destroy.
--terraform-log-level | -t - set Terraform log level (TF_LOG), useful for debugging.
` + globalOptions
const initHelp = `
Usage:
cdflow2 [ GLOBALOPTS ] init [ OPTS ]
Options:
--name | -n - Name of the new project repository
--boilerplate | -b - git URL of the git repo to copy as boilerplate. To use a specific branch (or any valid git refspec), add "?ref=branch-name" to the end of the URL.
--{boilerplate arguments} - Dynamic arguments for the templates files. E.g.: "--domain name --account test"
Boilerplate Templates:
The boilerplate might include variable placeholders in any file in the repo
with the format: %{name}
The 'name' variable is predefined, using the value passed by --name.
You can specify additional variables by passing arguments like:
--domain name
--account test
` + globalOptions
func usage(subcommand string) {
if subcommand == "release" {
fmt.Println(releaseHelp)
} else if subcommand == "deploy" {
fmt.Println(deployHelp)
} else if subcommand == "shell" {
fmt.Println(shellHelp)
} else if subcommand == "setup" {
fmt.Println(setupHelp)
} else if subcommand == "destroy" {
fmt.Println(destroyHelp)
} else if subcommand == "init" {
fmt.Println(initHelp)
} else {
fmt.Println(help)
}
}
var globalOptionErrorFormat = `
Error in global options:
%v
For usage run:
cdflow --help
`
func main() {
os.Exit(runCommand())
}
func runCommand() (status int) {
globalArgs, remainingArgs, err := command.ParseArgs(os.Args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, globalOptionErrorFormat, err)
return 2
}
if globalArgs.Command == "" {
usage("")
return 2
} else if globalArgs.Command == "help" {
subcommand := ""
if len(remainingArgs) > 0 {
subcommand = remainingArgs[0]
}
usage(subcommand)
return 0
} else if globalArgs.Command == "version" {
fmt.Println(version)
return 0
}
state, err := command.GetGlobalState(globalArgs, globalArgs.Command != "init")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
defer func() {
if globalArgs.Command == "init" || status == 2 {
return
}
state.MonitoringClient.Command = globalArgs.Command
state.MonitoringClient.Project = state.Component
state.MonitoringClient.Version = version
state.MonitoringClient.StatusCode = status
state.MonitoringClient.SubmitEvent()
}()
env := util.GetEnv(os.Environ())
if globalArgs.Command == "release" {
releaseArgs, err := release.ParseArgs(remainingArgs)
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf("Error: %s", err))
usage("release")
return 2
}
state.MonitoringClient.ReleaseVersion = releaseArgs.Version
if err := release.RunCommand(state, *releaseArgs, env); err != nil {
if status, ok := err.(command.Failure); ok {
return int(status)
}
fmt.Fprintln(os.Stderr, "\n"+err.Error())
return 1
}
} else if globalArgs.Command == "deploy" {
deployArgs, err := deploy.ParseArgs(remainingArgs)
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf("Error: %s", err))
usage("deploy")
return 2
}
state.MonitoringClient.Environment = deployArgs.EnvName
state.MonitoringClient.ReleaseVersion = deployArgs.Version
if err := deploy.RunCommand(state, deployArgs, env); err != nil {
if status, ok := err.(command.Failure); ok {
return int(status)
}
fmt.Fprintln(os.Stderr, err)
return 1
}
} else if globalArgs.Command == "shell" {
shellArgs, err := shell.ParseArgs(remainingArgs)
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf("Error: %s", err))
usage("shell")
return 2
}
state.MonitoringClient.Environment = shellArgs.EnvName
state.MonitoringClient.ReleaseVersion = shellArgs.Version
if err := shell.RunCommand(state, shellArgs, env); err != nil {
if status, ok := err.(command.Failure); ok {
return int(status)
}
fmt.Fprintln(os.Stderr, err)
return 1
}
} else if globalArgs.Command == "setup" {
if len(remainingArgs) != 0 {
fmt.Fprintln(os.Stderr, "Error: setup has no arguments")
usage("setup")
return 2
}
if err := setup.RunCommand(state, env); err != nil {
if status, ok := err.(command.Failure); ok {
return int(status)
}
fmt.Fprintln(os.Stderr, err)
return 1
}
} else if globalArgs.Command == "destroy" {
destroyArgs, err := destroy.ParseArgs(remainingArgs)
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf("Error: %s", err))
usage("destroy")
return 2
}
state.MonitoringClient.Environment = destroyArgs.EnvName
state.MonitoringClient.ReleaseVersion = destroyArgs.Version
if err := destroy.RunCommand(state, destroyArgs, env); err != nil {
if status, ok := err.(command.Failure); ok {
return int(status)
}
fmt.Fprintln(os.Stderr, err)
return 1
}
} else if globalArgs.Command == "init" {
initArgs, err := cinit.ParseArgs(remainingArgs)
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf("Error: %s", err))
usage("init")
return 2
}
if err := cinit.RunCommand(state, initArgs, env); err != nil {
if status, ok := err.(command.Failure); ok {
return int(status)
}
fmt.Fprintln(os.Stderr, err)
return 1
}
} else {
usage("")
return 2
}
return 0
}