-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
87 lines (73 loc) · 2.98 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
const { EventEmitter } = require('events');
const AWS = require('aws-sdk');
const loadTemplateFile = require('./libs/loadTemplateFile');
const parseParameters = require('./libs/parseParameters');
const parseTags = require('./libs/parseTags');
const validateTemplate = require('./libs/cloudformation/validateTemplate');
const describeStack = require('./libs/cloudformation/describeStack');
const validateStackState = require('./libs/cloudformation/validateStackState');
const createChangeSet = require('./libs/cloudformation/createChangeSet');
const executeChangeSet = require('./libs/cloudformation/executeChangeSet');
const deleteChangeSet = require('./libs/cloudformation/deleteChangeSet');
module.exports = (args) => {
const events = new EventEmitter();
let templateString = '';
let paramsObj = {};
let tagsObj = {};
let stackData = {};
let changeSetData = {};
// Set AWS config
if (args.profile) {
const creds = new AWS.SharedIniFileCredentials({ profile: args.profile });
AWS.config.credentials = creds;
}
AWS.config.update({
region: args.region,
accessKeyId: args.accesskey,
secretAccessKey: args.secretkey,
});
// Start with empty promise so that there is no immediate call and event emitter returns first
new Promise((resolve) => resolve())
// Load files
.then(() => events.emit('LOADING_FILES'))
.then(() => {
if (args.templateString && args.templateString.length) {
return Promise.resolve(args.templateString);
}
return loadTemplateFile(args.template);
})
.then((newTemplateString) => { templateString = newTemplateString; })
// Parse params & tags
.then(() => parseParameters(args.parameters))
.then((newParamsObj) => { paramsObj = newParamsObj; })
.then(() => parseTags(args.tags))
.then((newTagsObj) => { tagsObj = newTagsObj; })
// Validate template
.then(() => events.emit('VALIDATING_TEMPLATE'))
.then(() => validateTemplate(templateString))
// Validate stack state
.then(() => events.emit('VALIDATING_STACKSTATE'))
.then(() => validateStackState(args.stackname))
.then((newStackData) => { stackData = newStackData; })
.then(() => deleteChangeSet(args.stackname, 'cfn-deploy'))
// Create changeset
.then(() => events.emit('CREATING_CHANGESET', { type: (stackData ? 'UPDATE' : 'CREATE') }))
.then(() => createChangeSet(args, templateString, paramsObj, tagsObj))
.then((newChangeSetData) => { changeSetData = newChangeSetData; })
// Execute changeset
.then(() => events.emit('EXECUTING_CHANGESET', { type: (stackData ? 'UPDATE' : 'CREATE') }))
.then(() => executeChangeSet(args.stackname, changeSetData.ChangeSetId))
// Get new stack status
.then(() => describeStack(args.stackname))
// Done
.then((newStackData) => {
events.emit('COMPLETE', newStackData);
events.emit('FINALLY');
})
// Handle errors
.catch((err) => {
events.emit('ERROR', err);
events.emit('FINALLY');
});
return events;
};