-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
528 lines (460 loc) · 14.6 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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/**
* Cloud Formation Module. Handles stack creation, deletion, and updating. Adds
* periodic polling to check stack status. So that stack operations are
* synchronous.
*/
const filesystem = require('fs')
const Promise = require('bluebird')
const YAML = require('yamljs')
const AWS = require('aws-sdk')
const _ = require('lodash')
const sprintf = require('sprintf')
const moment = require('moment')
const flow = require('lodash/fp/flow')
const keyBy = require('lodash/fp/keyBy')
const get = require('lodash/fp/get')
const mapValues = require('lodash/fp/mapValues')
const merge = require('lodash/merge')
const chalk = require('chalk')
const HttpsProxyAgent = require('https-proxy-agent')
const fs = Promise.promisifyAll(filesystem)
AWS.config.setPromisesDependency(Promise)
const PROXY = process.env.PROXY || process.env.https_proxy || process.env.http_proxy
const ONE_MINUTE = 60000
const success = [
'CREATE_COMPLETE',
'DELETE_COMPLETE',
'UPDATE_COMPLETE'
]
const failed = [
'ROLLBACK_FAILED',
'ROLLBACK_IN_PROGRESS',
'ROLLBACK_COMPLETE',
'UPDATE_ROLLBACK_IN_PROGRESS',
'UPDATE_ROLLBACK_COMPLETE',
'UPDATE_FAILED',
'DELETE_FAILED'
]
const exists = [
'CREATE_COMPLETE',
'UPDATE_COMPLETE',
'ROLLBACK_COMPLETE',
'UPDATE_ROLLBACK_COMPLETE'
]
const colorMap = {
CREATE_IN_PROGRESS: 'gray',
CREATE_COMPLETE: 'green',
CREATE_FAILED: 'red',
DELETE_IN_PROGRESS: 'gray',
DELETE_COMPLETE: 'green',
DELETE_FAILED: 'red',
ROLLBACK_FAILED: 'red',
ROLLBACK_IN_PROGRESS: 'yellow',
ROLLBACK_COMPLETE: 'red',
UPDATE_IN_PROGRESS: 'gray',
UPDATE_COMPLETE: 'green',
UPDATE_COMPLETE_CLEANUP_IN_PROGRESS: 'green',
UPDATE_ROLLBACK_IN_PROGRESS: 'yellow',
UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS: 'yellow',
UPDATE_ROLLBACK_FAILED: 'red',
UPDATE_ROLLBACK_COMPLETE: 'red',
UPDATE_FAILED: 'red'
}
const ings = {
create: 'Creating',
delete: 'Deleting',
update: 'Updating'
}
let _config = {
checkStackInterval: 5000
}
function Cfn (name, template) {
let log = console.log
let opts = _.isPlainObject(name) ? name : {}
let awsOpts = {}
let startedAt = Date.now()
let params = opts.params
let cfParams = opts.cfParams || {}
let awsConfig = opts.awsConfig
let capabilities = opts.capabilities || ['CAPABILITY_IAM', 'CAPABILITY_NAMED_IAM']
let tags = opts.tags || {}
let async = opts.async
let checkStackInterval = opts.checkStackInterval || _config.checkStackInterval
if (PROXY) {
awsOpts.httpOptions = {
agent: new HttpsProxyAgent(PROXY)
}
}
if (awsConfig) {
_.merge(awsOpts, awsConfig)
}
// initialize cf
let cf = new AWS.CloudFormation(awsOpts)
name = opts.name || name
template = opts.template || template
function checkStack (action, name) {
const logPrefix = name + ' ' + action.toUpperCase()
const notExists = /ValidationError:\s+Stack\s+\[?.+]?\s+does not exist/
const throttling = /Throttling:\s+Rate\s+exceeded/
let displayedEvents = {}
return new Promise(function (resolve, reject) {
let interval
let running = false
// on success:
// 1. clear interval
// 2. return resolved promise
function _success () {
clearInterval(interval)
return resolve()
}
// on fail:
// 1. build fail message
// 2. clear interval
// 3. return rejected promise with failed message
function _failure (msg) {
const fullMsg = logPrefix + ' Failed' + (msg ? ': ' + msg : '')
clearInterval(interval)
return reject(new Error(fullMsg))
}
function _processEvents (events) {
events = _.sortBy(events, 'Timestamp')
_.forEach(events, function (event) {
displayedEvents[event.EventId] = true
if (moment(event.Timestamp).valueOf() >= startedAt) {
log(sprintf('[%s] %s %s: %s - %s %s %s',
chalk.gray(moment(event.Timestamp).format('HH:mm:ss')),
ings[action],
chalk.cyan(name),
event.ResourceType,
event.LogicalResourceId,
chalk[colorMap[event.ResourceStatus]](event.ResourceStatus),
event.ResourceStatusReason || ''
))
}
})
const lastEvent = _.last(events) || {}
const timestamp = moment(lastEvent.Timestamp).valueOf()
const resourceType = lastEvent.ResourceType
const status = lastEvent.ResourceStatus
const statusReason = lastEvent.ResourceStatusReason
// Only fail/succeed on cloud formation stack resource
// and ensure we are not finishing on a nested template
if (resourceType === 'AWS::CloudFormation::Stack' && lastEvent.LogicalResourceId === name) {
// if cf stack status indicates failure AND the failed event occurred during this update, notify of failure
// if cf stack status indicates success, OR it failed before this current update, notify of success
if (_.includes(failed, status) && (timestamp >= startedAt)) {
_failure(statusReason)
} else if (_.includes(success, status) || (_.includes(failed, status) && (timestamp < startedAt))) {
_success()
}
}
running = false
}
// provides all pagination
function getAllStackEvents (stackName) {
let next
let allEvents = []
function getStackEvents () {
return cf.describeStackEvents({
StackName: stackName,
NextToken: next
})
.promise()
.then(function (data) {
next = (data || {}).NextToken
allEvents = allEvents.concat(data.StackEvents)
return next && !allRelevantEventsLoaded(data.StackEvents) ? getStackEvents() : Promise.resolve()
})
}
return getStackEvents().then(function () {
return allEvents
})
}
// events are loaded sorted by timestamp desc (newest events first)
// if we try to load all events we can easily run into throttling by aws api
function allRelevantEventsLoaded (events) {
return events.some(event => moment(event.Timestamp).valueOf() < startedAt)
}
function outputNewStackEvents () {
let events = []
if (running) {
return
}
running = true
return getAllStackEvents(name)
.then(function (allEvents) {
running = false
_.forEach(allEvents, function (event) {
// if event has already been seen, don't add to events to process list
if (displayedEvents[event.EventId]) {
return
}
events.push(event)
})
return _processEvents(events)
}).catch(function (err) {
// if stack does not exist, notify success
if (err && notExists.test(err)) {
return _success()
}
// if throttling has occurred, process events again
if (err && throttling.test(err)) {
log('AWS api calls are throttling')
return _processEvents(events)
}
// otherwise, notify of failure
if (err) {
return _failure(err)
}
})
}
// call once and repeat in intervals
outputNewStackEvents()
interval = setInterval(outputNewStackEvents, checkStackInterval)
})
}
function processCfStack (action, cfparms) {
startedAt = Date.now()
if (action === 'update') {
return cf.updateStack(cfparms).promise()
.catch(function (err) {
if (!/No updates are to be performed/.test(err)) {
throw err
} else {
log('No updates are to be performed.')
}
})
}
return cf.createStack(cfparms).promise()
}
function loadJs (path) {
let tmpl = require(path)
let fn = _.isFunction(tmpl) ? tmpl : function () {
return tmpl
}
return Promise.resolve(JSON.stringify(fn(params)))
}
function normalizeParams (templateObject, params) {
if (!params) return Promise.resolve([])
if (!_.isPlainObject(params)) return Promise.resolve([])
if (_.keys(params).length <= 0) return Promise.resolve([])
// mutate params
_.keys(params).forEach(k => {
params[_.toLower(k)] = params[k]
})
return cf.getTemplateSummary(templateObject).promise()
.then(data => {
let templateParams = data.Parameters || []
return templateParams.map(p => {
let k = _.toLower(p.ParameterKey)
let v = params[k]
return {
ParameterKey: p.ParameterKey,
ParameterValue: v || p.DefaultValue
}
})
})
}
function convertTags () {
if (!_.isPlainObject(tags)) return []
return (Object.keys(tags)).map(function (key) {
return {
Key: key,
Value: tags[key]
}
})
}
function isStringOfType (type, str) {
let result = true
try {
type.parse(str)
} catch (ignore) {
result = false
}
return result
}
function isJSONString (str) {
return isStringOfType(JSON, str)
}
function isYAMLString (str) {
return isStringOfType(YAML, str) && str.split(/\r\n|\r|\n/).length > 1
}
function isValidTemplateString (str) {
return isJSONString(str) || isYAMLString(str)
}
function templateBodyObject (template) {
return {
TemplateBody: template
}
}
function processTemplate (template) {
// Check if template is located in S3
if (isUriTemplate(template)) return Promise.resolve({TemplateURL: template})
// Check if template if a `js` file
if (_.endsWith(template, '.js')) return loadJs(template).then(templateBodyObject)
// Check if template is an object, assume this is JSON good to go
if (_.isPlainObject(template)) return Promise.resolve(template).then(flow(JSON.stringify, templateBodyObject))
// Check if template is a valid string, serialised json or yaml
if (isValidTemplateString(template)) return Promise.resolve(templateBodyObject(template))
// Default to loading template from file.
return fs.readFileAsync(template, 'utf8').then(templateBodyObject)
}
function isUriTemplate (template) {
const httpsUri = /https:\/\/s3.+amazonaws.com/
return httpsUri.test(template)
}
function processStack (action, name, template) {
return processTemplate(template)
.then(templateObject => {
return normalizeParams(templateObject, cfParams)
.then(noramlizedParams => {
return processCfStack(action, merge({
StackName: name,
Capabilities: capabilities,
Parameters: noramlizedParams,
Tags: convertTags()
}, templateObject))
})
})
.then(function () {
return async ? Promise.resolve() : checkStack(action, name)
})
}
this.stackExists = function (overrideName) {
return cf.describeStacks({ StackName: overrideName || name }).promise()
.then(function (data) {
return _.includes(exists, data.Stacks[0].StackStatus)
})
.catch(function () {
return false
})
}
this.createOrUpdate = function () {
return this.stackExists()
.then(function (exists) {
return processStack(exists ? 'update' : 'create', name, template)
})
}
this.delete = function (overrideName) {
startedAt = Date.now()
return cf.deleteStack({ StackName: overrideName || name }).promise()
.then(function () {
return async ? Promise.resolve() : checkStack('delete', overrideName || name)
})
}
this.validate = function () {
return processTemplate(template)
.then(function (templateObject) {
return cf.validateTemplate(templateObject).promise()
})
}
this.outputs = function () {
return cf.describeStacks({ StackName: name }).promise()
.then(function (data) {
return flow(
get('Stacks[0].Outputs'),
keyBy('OutputKey'),
mapValues('OutputValue')
)(data)
})
}
this.output = function (field) {
return this.outputs()
.then(function (data) {
return data[field] || ''
})
}
this.cleanup = function (opts) {
const self = this
let regex = opts.regex
let minutesOld = opts.minutesOld
let dryRun = opts.dryRun
let async = opts.async
let limit = opts.limit
let next
let done = false
let stacks = []
startedAt = Date.now()
return (function loop () {
if (!done) {
return cf.listStacks({
NextToken: next,
StackStatusFilter: [
'CREATE_COMPLETE',
'CREATE_FAILED',
'DELETE_FAILED',
'ROLLBACK_COMPLETE',
'UPDATE_COMPLETE'
]
}).promise()
.then(function (data) {
next = data.NextToken
done = !next
return data.StackSummaries
})
.each(function (stack) {
var millisOld = Date.now() - ((minutesOld || 0) * ONE_MINUTE)
if (regex.test(stack.StackName) && moment(stack.CreationTime).valueOf() < millisOld) {
stacks.push(stack)
}
})
.then(function () {
return loop()
})
}
return Promise.resolve()
})()
.then(function () {
var filteredStacks = _.sortBy(stacks, ['CreationTime'])
if (limit) {
filteredStacks = _.take(filteredStacks, limit)
}
_.forEach(filteredStacks, function (stack) {
if (dryRun) {
log('Will clean up ' + stack.StackName + ' Created ' + stack.CreationTime)
} else {
log('Cleaning up ' + stack.StackName + ' Created ' + stack.CreationTime)
return self.delete(stack.StackName, async)
.catch(function (err) {
log('DELETE ERROR: ', err)
})
}
})
})
}
}
var cfn = function (name, template) {
return new Cfn(name, template).createOrUpdate()
}
cfn.stackExists = function (name) {
return new Cfn(name).stackExists()
}
cfn.create = function (name, template) {
return new Cfn(name, template).create()
}
cfn.delete = function (name) {
return new Cfn(name).delete()
}
cfn.validate = function (region, template, params) {
return new Cfn({
template: template,
params: params,
awsConfig: {
region: region
}
}).validate()
}
cfn.outputs = function (name) {
return new Cfn(name).outputs()
}
cfn.output = function (name, field) {
return new Cfn(name).output(field)
}
cfn.cleanup = function (regex, daysOld, dryRun) {
return new Cfn().cleanup(regex, daysOld, dryRun)
}
cfn.configure = function (cfg) {
_config = cfg
}
cfn.class = Cfn
module.exports = cfn