-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
492 lines (453 loc) · 16.4 KB
/
index.js
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
'use strict'
const path = require('path')
const merge = require('lodash.merge')
class AdditionalStacksPlugin {
constructor(serverless, options) {
this.serverless = serverless
this.options = options
this.provider = this.serverless.getProvider('aws')
// Map CloudFormation status codes to either 'success', 'failure' or 'in_progress'
this.stackStatusCodes = {
CREATE_COMPLETE: 'success',
CREATE_IN_PROGRESS: 'in_progress',
CREATE_FAILED: 'failure',
DELETE_COMPLETE: 'success',
DELETE_FAILED: 'failure',
DELETE_IN_PROGRESS: 'in_progress',
REVIEW_IN_PROGRESS: 'in_progress',
ROLLBACK_COMPLETE: 'failure',
ROLLBACK_FAILED: 'failure',
ROLLBACK_IN_PROGRESS: 'in_progress',
UPDATE_COMPLETE: 'success',
UPDATE_COMPLETE_CLEANUP_IN_PROGRESS: 'in_progress',
UPDATE_IN_PROGRESS: 'in_progress',
UPDATE_ROLLBACK_COMPLETE: 'failure',
UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS: 'in_progress',
UPDATE_ROLLBACK_FAILED: 'failure',
UPDATE_ROLLBACK_IN_PROGRESS: 'in_progress',
}
this.phrases = {
create: {
success: 'created successfully',
failure: 'create failed',
},
update: {
success: 'updated successfully',
failure: 'updated failed',
},
delete: {
success: 'removed successfully',
failure: 'remove failed',
},
}
this.commands = {
deploy: {
commands: {
additionalstacks: {
usage: 'Deploy additional stacks',
lifecycleEvents: [
'deploy',
],
options: {
stack: {
usage: 'Additional stack name to deploy',
shortcut: 'k',
required: false,
type: "string",
},
},
},
},
options: {
'skip-additionalstacks': {
usage: 'Skip deploying additional stacks',
required: false,
type: "boolean",
},
},
},
remove: {
commands: {
additionalstacks: {
usage: 'Remove additional stacks',
lifecycleEvents: [
'remove',
],
options: {
stack: {
usage: 'Additional stack name to remove',
shortcut: 'k',
required: false,
type: "string",
},
},
},
},
},
}
this.hooks = {
'before:deploy:deploy': this.beforeDeployGlobal.bind(this),
'after:deploy:deploy': this.afterDeployGlobal.bind(this),
'after:info:info': this.afterInfoGlobal.bind(this),
'deploy:additionalstacks:deploy': this.deployAdditionalStacksDeploy.bind(this),
'remove:additionalstacks:remove': this.removeAdditionalStacksRemove.bind(this),
}
}
mergeAdditionalStacks() {
// For each key in the additionalStacks, check if it's an array or not.
// If it is an array, merge the keys together. Else, return the original value.
return Object.fromEntries(Object.entries(this.serverless.service.custom.additionalStacks).map(([k, v], i) => {
if (Array.isArray(v)) {
return [k, v.reduce((memo, value) => {
if (value) {
if (typeof value === 'object') {
return merge(memo, value);
}
throw new Error(`Non-object value specified in ${key} array: ${value}`);
}
return memo;
}, {})];
} else {
return [k, v];
}
}));
}
getAdditionalStacks() {
return (this.serverless.service.custom && this.serverless.service.custom.additionalStacks && this.mergeAdditionalStacks()) || {}
}
getAdditionalBeforeStacks() {
const beforeStacks = {}
const stacks = this.getAdditionalStacks()
Object.keys(stacks).map(stackName => {
if (!stacks[stackName].Deploy || stacks[stackName].Deploy.toLowerCase() === 'before') {
beforeStacks[stackName] = stacks[stackName]
}
})
return beforeStacks
}
getAdditionalAfterStacks() {
const afterStacks = {}
const stacks = this.getAdditionalStacks()
Object.keys(stacks).map(stackName => {
if (stacks[stackName].Deploy && stacks[stackName].Deploy.toLowerCase() === 'after') {
afterStacks[stackName] = stacks[stackName]
}
})
return afterStacks
}
// Deploy additional stacks befpre deploying the main stack
// These are stacks with Deploy: Before, which is the default
beforeDeployGlobal() {
if (this.options['skip-additionalstacks'])
return
const stacks = this.getAdditionalBeforeStacks()
if (Object.keys(stacks).length > 0) {
this.serverless.cli.log('Deploying additional stacks...')
return this.deployStacks(stacks)
}
}
// Deploy additional stacks after deploying the main stack
// These are stacks with Deploy: After
afterDeployGlobal() {
if (this.options['skip-additionalstacks'])
return
const stacks = this.getAdditionalAfterStacks()
if (Object.keys(stacks).length > 0) {
this.serverless.cli.log('Deploying additional stacks...')
return this.deployStacks(stacks)
}
}
// Show additional stack info after normal info
afterInfoGlobal() {
if (this.options['skip-additionalstacks'])
return
const stacks = this.getAdditionalStacks()
if (Object.keys(stacks).length > 0) {
this.serverless.cli.consoleLog('additional stacks:')
return this.infoStacks(stacks)
}
}
// Deploy additional stacks specified with sls deploy additionalstack [name]
deployAdditionalStacksDeploy() {
const stacks = this.getAdditionalStacks()
if (this.options.stack) {
const stack = stacks[this.options.stack]
if (stack) {
this.serverless.cli.log('Deploying additional stack ' + this.options.stack + '...')
return this.deployStack(this.options.stack, stack)
} else {
return Promise.reject(new Error('Additional stack not found: ' + this.options.stack))
}
} else {
// Deploy all additional stacks
if (Object.keys(stacks).length > 0) {
this.serverless.cli.log('Deploying all additional stacks...')
return this.deployStacks(stacks)
} else {
this.serverless.cli.log('No additional stacks defined. Add a custom.additionalStacks section to serverless.yml.')
return Promise.resolve()
}
}
}
// Remove additional stacks specified with sls remove additionalstack [name]
removeAdditionalStacksRemove() {
const stacks = this.getAdditionalStacks()
if (this.options.stack) {
const stack = stacks[this.options.stack]
if (stack) {
return this.deleteStack(this.options.stack, stack)
} else {
return Promise.reject(new Error('Additional stack not found: ' + this.options.stack))
}
} else {
// Remove all additional stacks
if (Object.keys(stacks).length > 0) {
this.serverless.cli.log('Removing all additional stacks...')
return this.deleteStacks(stacks)
} else {
this.serverless.cli.log('No additional stacks defined. Add a custom.additionalStacks section to serverless.yml.')
return Promise.resolve()
}
}
}
// Generate a full name for an additional stack (used in AWS)
getFullStackName(stackName, stack) {
const defaultName = this.provider.naming.getStackName() + '-' + stackName
return stack.StackName || defaultName
}
// This deploys all the specified stacks
deployStacks(stacks) {
let promise = Promise.resolve()
Object.keys(stacks).map(stackName => {
promise = promise
.then(() => {
return this.deployStack(stackName, stacks[stackName])
})
})
return promise
}
// This is where we actually handle the deployment to AWS
deployStack(stackName, stack) {
// Generate the CloudFomation template
const compiledCloudFormationTemplate = {
"AWSTemplateFormatVersion": "2010-09-09",
"Description": stack.Description || "Additional AWS CloudFormation template for this Serverless application",
"Metadata": stack.Metadata || undefined,
"Parameters": stack.Parameters || undefined,
"Mappings": stack.Mappings || undefined,
"Conditions": stack.Conditions || undefined,
"Transform": stack.Transform || undefined,
"Resources": stack.Resources || undefined,
"Outputs": stack.Outputs || undefined,
}
// Generate tags
const stackTags = {
STAGE: this.options.stage || this.serverless.service.provider.stage
}
if (typeof stack.Tags === 'object') {
// Add custom tags specified only for this stack
Object.assign(stackTags, stack.Tags)
} else if (typeof this.serverless.service.provider.stackTags === 'object') {
// Add stackTags from Serverless main provider config
Object.assign(stackTags, this.serverless.service.provider.stackTags)
}
// Generate full stack name
const fullStackName = this.getFullStackName(stackName, stack)
// Stack deploy parameters (optional)
const deployParameters = stack.DeployParameters || []
return this.writeUpdateTemplateToDisk(stackName, compiledCloudFormationTemplate)
.then(() => { return this.describeStack(fullStackName) })
.then(stackStatus => {
if (this.options.noDeploy) {
this.serverless.cli.log('Did not deploy ' + fullStackName + ' due to --noDeploy')
return Promise.resolve()
} else {
if (!stackStatus) {
// Create stack
return this.createStack(stackName, fullStackName, compiledCloudFormationTemplate, stackTags, deployParameters)
} else {
// Update stack
return this.updateStack(stackName, fullStackName, compiledCloudFormationTemplate, stackTags, deployParameters)
}
}
})
}
// This deletes all the specified stacks in reverse order
deleteStacks(stacks) {
let promise = Promise.resolve()
Object.keys(stacks).reverse().map(stackName => {
promise = promise
.then(() => {
return this.deleteStack(stackName, stacks[stackName])
})
})
return promise
}
// This shows the info on all specified stacks
infoStacks(stacks) {
let promise = Promise.resolve()
Object.keys(stacks).map(stackName => {
promise = promise
.then(() => {
return this.infoStack(stackName, stacks[stackName])
})
})
return promise
}
describeStack(fullStackName) {
return this.provider.request(
'CloudFormation',
'describeStacks', {
StackName: fullStackName,
},
this.options.stage,
this.options.region
)
.then(response => {
return response.Stacks && response.Stacks[0]
})
.then(null, err => {
if (err.message && err.message.match(/does not exist$/)) {
// Stack doesn't exist yet
return null
} else {
// Some other error, let it throw
return Promise.reject(err)
}
})
}
createStack(stackName, fullStackName, compiledCloudFormationTemplate, stackTags, deployParameters) {
// These are the same parameters that Serverless uses in https://github.com/serverless/serverless/blob/master/lib/plugins/aws/deploy/lib/createStack.js
const params = {
StackName: fullStackName,
OnFailure: 'ROLLBACK',
Capabilities: [
'CAPABILITY_IAM',
'CAPABILITY_NAMED_IAM',
],
Parameters: deployParameters || [],
TemplateBody: JSON.stringify(compiledCloudFormationTemplate),
Tags: Object.keys(stackTags).map((key) => ({ Key: key, Value: stackTags[key] })),
}
// If the CloudFormation Template has the Transform tag, an additional capability is needed.
if (compiledCloudFormationTemplate.Transform) {
params.Capabilities.push("CAPABILITY_AUTO_EXPAND")
}
this.serverless.cli.log('Creating additional stack ' + stackName + '...')
return this.provider.request(
'CloudFormation',
'createStack',
params,
this.options.stage,
this.options.region
)
.then(() => {
return this.waitForStack(stackName, fullStackName, 'create')
})
}
writeUpdateTemplateToDisk(stackName, stack) {
this.serverless.cli.log('Writing template for additional stack ' + stackName + '...')
const updateOrCreate = this.createLater ? 'create' : 'update';
const cfTemplateFilePath = path.join(this.serverless.config.servicePath,
'.serverless', `cloudformation-template-${updateOrCreate}-stack-${stackName}.json`);
this.serverless.utils.writeFileSync(cfTemplateFilePath, stack);
return Promise.resolve();
}
updateStack(stackName, fullStackName, compiledCloudFormationTemplate, stackTags, deployParameters) {
// These are the same parameters that Serverless uses in https://github.com/serverless/serverless/blob/master/lib/plugins/aws/lib/updateStack.js
const params = {
StackName: fullStackName,
Capabilities: [
'CAPABILITY_IAM',
'CAPABILITY_NAMED_IAM',
],
Parameters: deployParameters || [],
TemplateBody: JSON.stringify(compiledCloudFormationTemplate),
Tags: Object.keys(stackTags).map((key) => ({ Key: key, Value: stackTags[key] })),
}
// If the CloudFormation Template has the Transform tag, an additional capability is needed.
if (compiledCloudFormationTemplate.Transform) {
params.Capabilities.push("CAPABILITY_AUTO_EXPAND")
}
return this.provider.request(
'CloudFormation',
'updateStack',
params,
this.options.stage,
this.options.region
)
.then(() => {
this.serverless.cli.log('Updating additional stack ' + stackName + '...')
return this.waitForStack(stackName, fullStackName, 'update')
})
.then(null, err => {
if (err.message && err.message.match(/^No updates/)) {
// Stack is unchanged, ignore error
this.serverless.cli.log('Additional stack ' + stackName + ' has not changed.')
return Promise.resolve()
} else {
return Promise.reject(err)
}
})
}
// This is where we actually handle the stack deletion from AWS
deleteStack(stackName, stack) {
// Generate full stack name
const fullStackName = this.getFullStackName(stackName, stack)
this.serverless.cli.log('Removing additional stack ' + stackName + '...')
return this.provider.request(
'CloudFormation',
'deleteStack', {
StackName: fullStackName,
},
this.options.stage,
this.options.region
)
.then(() => {
return this.waitForStack(stackName, fullStackName, 'delete')
})
}
// This is where we actually show information about the CloudFormation stack in AWS
infoStack(stackName, stack) {
// Generate full stack name
const fullStackName = this.getFullStackName(stackName, stack)
return this.describeStack(fullStackName)
.then(status => {
if (!status) {
this.serverless.cli.consoleLog(' ' + stackName + ': does not exist')
} else {
this.serverless.cli.consoleLog(' ' + stackName + ': ' + status.StackStatus)
}
})
}
waitForStack(stackName, fullStackName, operation) {
let dots = 0
const readMore = () => {
return this.describeStack(fullStackName)
.then(response => {
if (!response) {
// Stack does not exist
if (dots) this.serverless.cli.consoleLog('')
this.serverless.cli.log('Additional stack ' + stackName + ' removed successfully.')
return
}
const state = this.stackStatusCodes[response.StackStatus]
if (state === 'in_progress') {
// Continue until no longer in progress
this.serverless.cli.printDot()
dots += 1
return new Promise((resolve, reject) => setTimeout(resolve, 5000)).then(readMore)
} else {
if (dots) this.serverless.cli.consoleLog('')
this.serverless.cli.log('Additional stack ' + stackName + ' ' + this.phrases[operation][state] + ' (' + response.StackStatus + ').')
if (this.stackStatusCodes[response.StackStatus] === 'failure') {
// The operation failed, so return an error to Serverless
return Promise.reject(new Error('Additional stack ' + stackName + ' ' + this.phrases[operation][state] + ' (' + response.StackStatus + ')'))
}
}
})
}
return readMore()
}
}
module.exports = AdditionalStacksPlugin