forked from hashicorp/otto
/
deploy.go
414 lines (365 loc) · 11.4 KB
/
deploy.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package terraform
import (
"fmt"
"path/filepath"
"strings"
"github.com/hashicorp/otto/app"
"github.com/hashicorp/otto/directory"
"github.com/hashicorp/otto/foundation"
"github.com/hashicorp/otto/helper/router"
)
type DeployOptions struct {
// Dir is the directory where Terraform is run. If this isn't set, it'll
// default to "#{ctx.Dir}/deploy".
Dir string
// DisableBuild, if true, will not load a build associated with this
// appfile and attempt to extract the artifact from it. In this case,
// AritfactExtractors is also useless.
DisableBuild bool
// ArtifactExtractors is a mapping of artifact extractors. The
// built-in artifact extractors will populate this if a key isn't set.
ArtifactExtractors map[string]DeployArtifactExtractor
// InfraOutputMap is a map to change the key of an infra output
// to a different key for a Terraform variable. The key of this map
// is the infra output key, and teh value is the Terraform variable name.
InfraOutputMap map[string]string
}
// Deploy can be used as an implementation of app.App.Deploy to handle calling
// out to terraform w/ the configured config to get an app deployed to an
// infrastructure.
//
// This will verify the infrastructure is created and a build is available,
// and use that information to run Terraform. Any edge cases around Terraform
// failures is handled and state storage is automatic as well.
//
// This function implements app.App.Deploy.
func Deploy(opts *DeployOptions) *router.Router {
return &router.Router{
Actions: map[string]router.Action{
"": &router.SimpleAction{
ExecuteFunc: opts.actionDeploy,
SynopsisText: actionDeploySyn,
HelpText: strings.TrimSpace(actionDeployHelp),
},
"destroy": &router.SimpleAction{
ExecuteFunc: opts.actionDestroy,
SynopsisText: actionDestroySyn,
HelpText: strings.TrimSpace(actionDestroyHelp),
},
"info": &router.SimpleAction{
ExecuteFunc: opts.actionInfo,
SynopsisText: actionInfoSyn,
HelpText: strings.TrimSpace(actionInfoHelp),
},
},
}
}
func (opts *DeployOptions) actionDeploy(rctx router.Context) error {
ctx := rctx.(*app.Context)
project, err := Project(&ctx.Shared)
if err != nil {
return err
}
vars := make(map[string]string)
infra, infraVars, err := opts.lookupInfraVars(ctx)
if err != nil {
return err
}
if infra == nil {
return fmt.Errorf(
"Infrastructure for this application hasn't been built yet.\n" +
"The deploy step requires this because the target infrastructure\n" +
"as well as its final properties can affect the deploy process.\n" +
"Please run `otto infra` to build the underlying infrastructure,\n" +
"then run `otto deploy` again.")
}
for k, v := range infraVars {
vars[k] = v
}
if !opts.DisableBuild {
buildVars, err := opts.lookupBuildVars(ctx, infra)
if err != nil {
return err
}
if buildVars == nil {
return fmt.Errorf(
"This application hasn't been built yet. Please run `otto build`\n" +
"first so that the deploy step has an artifact to deploy.")
}
for k, v := range buildVars {
vars[k] = v
}
}
// Setup the vars
if err := foundation.WriteVars(&ctx.Shared); err != nil {
return fmt.Errorf("Error preparing deploy: %s", err)
}
// Get our old deploy to populate the old state data if we have it.
// This step is critical to make sure that Terraform remains idempotent
// and that it handles migrations properly.
deploy, err := opts.lookupDeploy(ctx)
if err != nil {
return err
}
// Run Terraform!
tf := &Terraform{
Path: project.Path(),
Dir: opts.tfDir(ctx),
Ui: ctx.Ui,
Variables: vars,
Directory: ctx.Directory,
StateId: deploy.ID,
}
if err := tf.Execute("apply"); err != nil {
deploy.MarkFailed()
if putErr := ctx.Directory.PutDeploy(deploy); putErr != nil {
return fmt.Errorf("The deploy failed with err: %s\n\n"+
"And then there was an error storing it in the directory: %s\n"+
"This second error is a bug and should be reported.", err, putErr)
}
return terraformError(err)
}
deploy.MarkSuccessful()
if err := ctx.Directory.PutDeploy(deploy); err != nil {
return err
}
return nil
}
func (opts *DeployOptions) actionDestroy(rctx router.Context) error {
ctx := rctx.(*app.Context)
project, err := Project(&ctx.Shared)
if err != nil {
return err
}
vars := make(map[string]string)
infra, infraVars, err := opts.lookupInfraVars(ctx)
if err != nil {
return err
}
if infra == nil {
return fmt.Errorf(
"Infrastructure for this application hasn't been built yet.\n" +
"Nothing to destroy.")
}
for k, v := range infraVars {
vars[k] = v
}
if !opts.DisableBuild {
buildVars, err := opts.lookupBuildVars(ctx, infra)
if err != nil {
return err
}
if buildVars == nil {
return fmt.Errorf(
"This application hasn't been built yet. Nothing to destroy.")
}
for k, v := range buildVars {
vars[k] = v
}
}
deploy, err := opts.lookupDeploy(ctx)
if err != nil {
return err
}
if deploy.IsNew() {
return fmt.Errorf(
"This application hasn't been deployed yet. Nothing to destroy.")
}
// Get the directory
// Run Terraform!
tf := &Terraform{
Path: project.Path(),
Dir: opts.tfDir(ctx),
Ui: ctx.Ui,
Variables: vars,
Directory: ctx.Directory,
StateId: deploy.ID,
}
if err := tf.Execute("destroy", "-force"); err != nil {
deploy.MarkFailed()
if putErr := ctx.Directory.PutDeploy(deploy); putErr != nil {
return fmt.Errorf("The destroy failed with err: %s\n\n"+
"And then there was an error storing it in the directory: %s\n"+
"This second error is a bug and should be reported.", err, putErr)
}
return terraformError(err)
}
deploy.MarkGone()
if err := ctx.Directory.PutDeploy(deploy); err != nil {
return err
}
return nil
}
func (opts *DeployOptions) actionInfo(rctx router.Context) error {
ctx := rctx.(*app.Context)
project, err := Project(&ctx.Shared)
if err != nil {
return err
}
deploy, err := opts.lookupDeploy(ctx)
if err != nil {
return err
}
if deploy.IsNew() {
return fmt.Errorf(
"This application hasn't been deployed yet. Nothing to show.")
}
// Get the directory
// Run Terraform!
tf := &Terraform{
Path: project.Path(),
Dir: opts.tfDir(ctx),
Ui: ctx.Ui,
Directory: ctx.Directory,
StateId: deploy.ID,
}
args := make([]string, len(ctx.ActionArgs)+1)
args[0] = "output"
copy(args[1:], ctx.ActionArgs)
if err := tf.Execute(args...); err != nil {
return terraformError(err)
}
return nil
}
// lookupInfraVars collects information about the result of `otto infra` and
// yields a set of variables that can be used by the deploy to reference
// resources in the infrastructure. It returns `nil` if the infrastructure has
// not been created successfully yet.
func (opts *DeployOptions) lookupInfraVars(
ctx *app.Context) (*directory.Infra, map[string]string, error) {
infra, err := ctx.Directory.GetInfra(&directory.Infra{
Lookup: directory.Lookup{
Infra: ctx.Appfile.ActiveInfrastructure().Name}})
if err != nil {
return nil, nil, err
}
if !infra.IsReady() {
return nil, nil, nil
}
vars := make(map[string]string)
for k, v := range infra.Outputs {
if opts.InfraOutputMap != nil {
if nk, ok := opts.InfraOutputMap[k]; ok {
k = nk
}
}
vars[k] = v
}
for k, v := range ctx.InfraCreds {
vars[k] = v
}
return infra, vars, nil
}
// lookupBuildVars collects information about the result of `otto build` and
// yields a set of variables that can be used by the deploy to reference the
// built artifact. It returns nil if `otto build` has not yet been run.
func (opts *DeployOptions) lookupBuildVars(
ctx *app.Context, infra *directory.Infra) (map[string]string, error) {
build, err := ctx.Directory.GetBuild(&directory.Build{
Lookup: directory.Lookup{
AppID: ctx.Appfile.ID,
Infra: ctx.Tuple.Infra,
InfraFlavor: ctx.Tuple.InfraFlavor,
},
})
if err != nil {
return nil, err
}
if build == nil {
return nil, nil
}
// Extract the artifact from the build. We do this based on the
// infrastructure type.
if opts.ArtifactExtractors == nil {
opts.ArtifactExtractors = make(map[string]DeployArtifactExtractor)
}
for k, v := range deployArtifactExtractors {
if _, ok := opts.ArtifactExtractors[k]; !ok {
opts.ArtifactExtractors[k] = v
}
}
ext, ok := opts.ArtifactExtractors[ctx.Tuple.Infra]
if !ok {
return nil, fmt.Errorf(
"Unknown deployment target infrastructure: %s\n\n"+
"This app currently doesn't know how to deploy to this infrastructure.\n"+
"Please report this to the project.",
ctx.Tuple.Infra)
}
return ext(ctx, build, infra)
}
// lookupDeploy returns any previously deploy made by Otto so we have the state
// necessary to update it.
//
// If we don't have a prior deploy, that is okay, we just create one
// now (with the DeployStateNew to note that we've never deployed). This
// gives us the UUID we can use for the state storage.
func (opts *DeployOptions) lookupDeploy(
ctx *app.Context) (*directory.Deploy, error) {
deployLookup := directory.Lookup{
AppID: ctx.Appfile.ID,
Infra: ctx.Tuple.Infra,
InfraFlavor: ctx.Tuple.InfraFlavor,
}
deploy, err := ctx.Directory.GetDeploy(&directory.Deploy{Lookup: deployLookup})
if err != nil {
return nil, err
}
if deploy == nil {
// If we have no deploy, put in a temporary one
deploy = &directory.Deploy{Lookup: deployLookup}
deploy.State = directory.DeployStateNew
// Write the temporary deploy so we have an ID to use for the state
if err := ctx.Directory.PutDeploy(deploy); err != nil {
return nil, err
}
}
return deploy, nil
}
// tfDir returns the appropriate terraform working dir
func (opts *DeployOptions) tfDir(ctx *app.Context) string {
tfDir := opts.Dir
if tfDir == "" {
tfDir = filepath.Join(ctx.Dir, "deploy")
}
return tfDir
}
// terraformError wraps an error from Terraform in a friendlier message.
func terraformError(err error) error {
return fmt.Errorf(
"Error running Terraform: %s\n\n"+
"Terraform usually has helpful error messages. Please read the error\n"+
"messages above and resolve them. Sometimes simply running `otto deploy`\n"+
"again will work.",
err)
}
// Synopsis text for actions
const (
actionDeploySyn = "Deploy the latest built artifact into your infrastructure"
actionDestroySyn = "Destroy all deployed resources for this application"
actionInfoSyn = "Display information about this application's deploy"
)
// Help text for actions
const actionDeployHelp = `
Usage: otto deploy
Deploys a built artifact into your infrastructure.
This command will take the latest built artifact and deploy it into your
infrastructure. Otto will create or replace any necessary resources required
to run your app.
`
const actionDestroyHelp = `
Usage: otto deploy destroy [-force]
Destroys any deployed resources associated with this application.
This command will remove any previously-deployed resources from your
infrastructure. This must be run for all of apps in an infrastructure before
'otto infra destroy' will work.
Otto will ask for confirmation to protect against an accidental destroy. You
can provide the -force flag to skip this check.
`
const actionInfoHelp = `
Usage: otto deploy info [NAME]
Displays information about this application's deploy.
This command will show any variables the deploy has specified as outputs. If
no NAME is specified, all outputs will be listed. If NAME is specified, just
the contents of that output will be printed.
`