-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbootstrap.js
292 lines (242 loc) · 8.54 KB
/
bootstrap.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
'use strict';
const assert = require('assert');
const path = require('path');
const pkg = require('@cfn/pkg');
const diff = require('@cfn/diff');
const { printChanges, printStackPolicy } = require('./print');
module.exports = class BootstrapPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = serverless.getProvider('aws');
this.commands = {
bootstrap: {
usage: 'Manages a CloudFormation Stack that complements the main Serverless Stack',
options: {
execute: {
usage: 'Auto-execute changes and wait for them to complete',
type: 'boolean'
},
},
lifecycleEvents: ['bootstrap'],
commands: {
remove: {
usage: 'Remove the bootstrap stack',
options: {
wait: {
usage: 'Wait for the stack to remove',
type: 'boolean',
}
},
lifecycleEvents: ['remove']
},
execute: {
usage: 'Execute a ChangeSet',
options: {
['change-set']: {
usage: 'The name of the ChangeSet to execute',
required: true,
type: 'string',
},
wait: {
usage: 'Wait for the change set to execute',
type: 'boolean',
}
},
lifecycleEvents: ['execute']
},
policy: {
usage: 'Applies a policy to the bootstrap stack',
lifecycleEvents: ['policy']
}
}
},
deploy: {
options: {
bootstrap: {
usage: 'Whether or not to check the bootstrap stack for changes (default to true, use --no-bootstrap to disable)',
type: 'boolean'
},
execute: {
usage: 'Auto-execute changes and wait for them to complete',
type: 'boolean'
},
}
}
};
this.hooks = {
'bootstrap:bootstrap': () => this.bootstrap(),
'bootstrap:remove:remove': () => this.remove(),
'bootstrap:execute:execute': () => this.execute(),
'bootstrap:policy:policy': () => this.updateStackPolicy(),
'before:deploy:deploy': () => this.check()
};
}
execute() {
const changeSetName = this.options['change-set'];
const { wait } = this.getConfig()
if (typeof changeSetName === 'string') {
return this.executeChangeSet(changeSetName, wait)
}
throw new Error('Bootstrap: You must specify a ChangeSet name (serverless bootstrap execute --change-set {{changeSetName}})');
}
async remove() {
const { provider, serverless } = this;
const stackName = this.getStackName();
await provider.request('CloudFormation', 'deleteStack', {
StackName: stackName,
})
const { wait = true } = this.getConfig()
if (wait) {
try {
serverless.cli.log('[serverless-plugin-bootstrap]: Waiting for stack to delete..');
await this.waitForStack()
} catch (err) {
if (err.message.indexOf('does not exist') === -1) {
throw err;
}
}
serverless.cli.log('[serverless-plugin-bootstrap]: Delete complete.');
}
}
check() {
const { provider, serverless } = this;
const { bootstrap = true } = this.options;
const { execute } = this.getConfig()
if (bootstrap === false) {
serverless.cli.log('WARNING: Skipping bootstrap check - your infrastructure might not match your code!');
return Promise.resolve();
}
const stackName = this.getStackName();
return this.getChanges(stackName)
.then(({ changeSetName, changes }) => {
if (changes.length > 0) {
if (execute) {
return this.executeChangeSet(changeSetName, true)
}
throw new Error(`The ${stackName} stack does not match the local template. Use the 'serverless bootstrap' command to view the diff and either update your source code or apply the changes`);
}
return provider.request('CloudFormation', 'deleteChangeSet', {
StackName: stackName,
ChangeSetName: changeSetName
});
});
}
bootstrap() {
const { provider, serverless } = this;
const stackName = this.getStackName();
const { execute } = this.getConfig()
return this.getChanges(stackName, true)
.then(({ changeSetName, changes }) => {
if (changes.length > 0) {
if (execute) {
return this.executeChangeSet(changeSetName, true)
} else {
return printChanges(serverless, stackName, changeSetName, changes);
}
} else {
serverless.cli.log('[serverless-plugin-bootstrap]: No changes.');
return provider.request('CloudFormation', 'deleteChangeSet', {
StackName: stackName,
ChangeSetName: changeSetName
})
}
});
}
async executeChangeSet(changeSetName, waitForComplete = false) {
const { provider, serverless } = this;
const stackName = this.getStackName();
await provider.request('CloudFormation', 'executeChangeSet', {
StackName: stackName,
ChangeSetName: changeSetName
})
if (waitForComplete) {
serverless.cli.log('[serverless-plugin-bootstrap]: Waiting for change set to execute..');
await this.waitForStack();
}
serverless.cli.log('[serverless-plugin-bootstrap]: Execute complete.');
}
async waitForStack() {
const { provider } = this;
const stackName = this.getStackName();
let status = ''
while (status.indexOf('COMPLETE') === -1) {
await new Promise(resolve => {
setTimeout(resolve, 5000)
})
const { Stacks } = await provider.request('CloudFormation', 'describeStacks', {
StackName: stackName,
})
if (Stacks.length > 0) {
status = Stacks[0].StackStatus
}
}
if (status.indexOf('ROLLBACK') !== -1) {
throw new Error(`Stack ${stackName} status is in rollback: ${status}`)
}
}
updateStackPolicy() {
const { provider, serverless } = this;
const { service } = serverless;
const { custom = {} } = service;
const { bootstrap = {} } = custom;
const { stackPolicy = null } = bootstrap;
const stackName = this.getStackName();
if (!stackPolicy) {
throw new Error('Must add \'stackPolicy\' to the bootstrap configuration.');
}
const statement = [].concat(stackPolicy);
printStackPolicy(serverless, stackName, statement);
return provider.request('CloudFormation', 'setStackPolicy', {
StackName: stackName,
StackPolicyBody: JSON.stringify({ Statement: statement })
});
}
getChanges(stackName, detailed = false) {
const { provider, serverless } = this;
const { service } = serverless;
const { custom = {} } = service;
const { bootstrap = {} } = custom;
const { bucket, file, parameters, capabilities = [] } = bootstrap;
const roleArn = serverless.service.provider.cfnRole;
const prefix = 'serverless-bootstrap-';
const description = 'Created by the Serverless Bootstrap plugin';
const template = serverless.utils.readFileSync(file);
const credentials = this.getCredentials(provider);
const bucketName = bucket || `${stackName}-resources`;
const basedir = path.dirname(file);
const { deploymentBucketObject = {} } = service.provider;
const { blockPublicAccess } = deploymentBucketObject;
return pkg({ credentials, template, basedir, bucketName, blockPublicAccess })
.then(() => {
const options = {
credentials, prefix, description, capabilities, stackName, roleArn, template, parameters, detailed
};
return diff(options);
});
}
getCredentials(provider) {
const credentials = provider.getCredentials();
const region = provider.getRegion();
credentials.region = region;
return credentials;
}
getStackName() {
const { serverless } = this;
const { service } = serverless;
const { custom = {} } = service;
const { bootstrap = {} } = custom;
const { stack, file } = bootstrap;
assert(file, 'serverless-plugin-bootstrap: must specify custom.bootstrap.file');
const fileName = path.basename(file, path.extname(file));
const serviceName = service.service;
return stack || `${serviceName}-${fileName}`;
}
getConfig() {
const { serverless } = this;
const { service } = serverless;
const { custom = {} } = service;
const { bootstrap = {} } = custom;
return Object.assign(bootstrap, this.options)
}
};