forked from Enase/serverless-aws-alias
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
352 lines (314 loc) · 8.73 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
'use strict';
/**
* Serverless AWS alias plugin
*/
const BbPromise = require('bluebird')
, _ = require('lodash')
, Path = require('path')
, validate = require('./lib/validate')
, configureAliasStack = require('./lib/configureAliasStack')
, createAliasStack = require('./lib/createAliasStack')
, updateAliasStack = require('./lib/updateAliasStack')
, aliasRestructureStack = require('./lib/aliasRestructureStack')
, stackInformation = require('./lib/stackInformation')
, listAliases = require('./lib/listAliases')
, removeAlias = require('./lib/removeAlias')
, logs = require('./lib/logs')
, collectUserResources = require('./lib/collectUserResources')
, uploadAliasArtifacts = require('./lib/uploadAliasArtifacts')
, updateFunctionAlias = require('./lib/updateFunctionAlias')
, deferredOutputs = require('./lib/deferredOutputs');
const aliasStageCommonOptions = {
type: 'object',
properties: {
'cacheDataEncrypted': { type: 'boolean' },
'cacheTtlInSeconds': { type: 'integer' },
'cachingEnabled': { type: 'boolean' },
'dataTraceEnabled': { type: 'boolean' },
'loggingLevel': { type: 'string' },
'metricsEnabled': { type: 'boolean' },
'throttlingBurstLimit': { type: 'integer' },
'throttlingRateLimit': { type: 'number' },
},
additionalProperties: false,
};
const aliasStageCustomOptions = {
type: 'object',
properties: {
'cacheDataEncrypted': { type: 'boolean' },
'cacheTtlInSeconds': { type: 'integer' },
'cachingEnabled': { type: 'boolean' },
'dataTraceEnabled': { type: 'boolean' },
'loggingLevel': { type: 'string' },
'metricsEnabled': { type: 'boolean' },
'throttlingBurstLimit': { type: 'integer' },
'throttlingRateLimit': { type: 'number' },
'cacheClusterEnabled': { type: 'boolean' },
'cacheClusterSize': { type: 'integer' },
},
additionalProperties: false,
};
class AwsAlias {
constructor(serverless, options) {
this._serverless = serverless;
this._options = options || {};
this._provider = this._serverless.getProvider('aws');
/**
* Set preliminary stage and alias. This is needed to enable the injection
* of the stage into the system variables. The values are overwritten by
* validate with their actually evaluated and substituted values.
*/
this._stage = this._provider.getStage();
this._alias = this._options.alias || this._stage;
/**
* Load stack helpers from Serverless installation.
*/
const monitorStack = require(
Path.join(this._serverless.config.serverlessPath,
'plugins',
'aws',
'lib',
'monitor-stack')
);
const setBucketName = require(
Path.join(this._serverless.config.serverlessPath,
'plugins',
'aws',
'lib',
'set-bucket-name')
);
_.assign(
this,
validate,
collectUserResources,
configureAliasStack,
createAliasStack,
updateAliasStack,
listAliases,
logs,
removeAlias,
aliasRestructureStack,
stackInformation,
uploadAliasArtifacts,
updateFunctionAlias,
setBucketName,
monitorStack,
deferredOutputs
);
this._commands = {
alias: {
commands: {
remove: {
usage: 'Remove a deployed alias',
lifecycleEvents: [
'remove'
],
options: {
alias: {
usage: 'Name of the alias',
shortcut: 'a',
required: true,
type: 'string',
},
verbose: {
usage: 'Enable verbose output',
shortcut: 'v',
required: false,
type: 'boolean',
}
}
}
}
},
package: {
options: {
alias: {
usage: 'Name of the alias',
shortcut: 'a',
required: false,
type: 'string'
},
}
},
deploy: {
options: {
alias: {
usage: 'Name of the alias',
shortcut: 'a',
required: false,
type: 'string'
},
}
},
s3deploy: {
options: {
alias: {
usage: 'Name of the alias',
shortcut: 'a',
required: false,
type: 'string'
},
}
},
};
this._serverless.configSchemaHandler.defineCustomProperties({
properties: { aliasStage: aliasStageCustomOptions },
});
this._serverless.configSchemaHandler.defineFunctionProperties('aws', {
properties: { aliasStage: aliasStageCommonOptions },
});
this._serverless.configSchemaHandler.defineFunctionEventProperties('aws', 'http', {
properties: { aliasStage: aliasStageCommonOptions },
});
this._hooks = {
'before:package:initialize': () => BbPromise.bind(this)
.then(this.validate),
'before:aws:package:finalize:mergeCustomProviderResources': () => BbPromise.bind(this)
.then(this.collectUserResources),
'before:deploy:deploy': () => BbPromise.bind(this)
.then(this.validate)
.then(this.configureAliasStack),
'before:aws:deploy:deploy:createStack': () => BbPromise.bind(this)
.then(this.aliasStackLoadCurrentCFStackAndDependencies)
.spread(this.aliasRestructureStack),
'after:aws:deploy:deploy:createStack': () => BbPromise.bind(this)
.then(this.createAliasStack),
'after:aws:deploy:deploy:uploadArtifacts': () => BbPromise.bind(this)
.then(() => BbPromise.resolve()),
'after:aws:deploy:deploy:updateStack': () => BbPromise.bind(this)
.then(this.setBucketName)
.then(this.uploadAliasArtifacts)
.then(this.updateAliasStack),
'before:deploy:function:initialize': () => BbPromise.bind(this)
.then(this.validate)
.then(() => {
// Force forced deploy
if (!this._options.force) {
return BbPromise.reject(new this.serverless.classes.Error("You must deploy single functions using --force with the alias plugin."));
}
return BbPromise.resolve();
}),
'after:deploy:function:deploy': () => BbPromise.bind(this)
.then(this.updateFunctionAlias),
'after:info:info': () => BbPromise.bind(this)
.then(this.validate)
.then(this.listAliases),
'before:remove:remove': () => {
if (!this._validated) {
return BbPromise.reject(new this._serverless.classes.Error(`Use "serverless alias remove --alias=${this._stage}" to remove the service.`));
}
return BbPromise.resolve();
},
// Override the logs command - must be, because the $LATEST filter
// in the original logs command is not easy to change without hacks.
'logs:logs': () => BbPromise.bind(this)
.then(this.validate)
.then(this.logsValidate)
.then(this.logsGetLogStreams)
.then(this.functionLogsShowLogs),
'logs:api:logs': () => BbPromise.bind(this)
.then(this.validate)
.then(this.apiLogsValidate)
.then(this.apiLogsGetLogStreams)
.then(this.apiLogsShowLogs),
'alias:remove:remove': () => BbPromise.bind(this)
.then(this.validate)
.then(this.aliasStackLoadCurrentCFStackAndDependencies)
.spread(this.removeAlias)
};
// Patch hooks to override our event replacements
const pluginManager = this.serverless.pluginManager;
const logHooks = pluginManager.hooks['logs:logs'];
_.pullAllWith(logHooks, [ 'AwsLogs' ], (a, b) => a.pluginName === b);
// Extend the logs command if available
try {
const logCommand = pluginManager.getCommand([ 'logs' ]);
logCommand.options.alias = {
usage: 'Alias'
};
logCommand.options.version = {
usage: 'Logs a specific version of the function'
};
logCommand.commands = _.assign({}, logCommand.commands, {
api: {
usage: 'Output the logs of a deployed APIG stage (alias)',
lifecycleEvents: [
'logs',
],
options: {
alias: {
usage: 'Alias'
},
stage: {
usage: 'Stage of the service',
shortcut: 's',
},
region: {
usage: 'Region of the service',
shortcut: 'r',
},
tail: {
usage: 'Tail the log output',
shortcut: 't',
},
startTime: {
usage: 'Logs before this time will not be displayed',
},
filter: {
usage: 'A filter pattern',
},
interval: {
usage: 'Tail polling interval in milliseconds. Default: `1000`',
shortcut: 'i',
},
},
key: 'logs:api',
pluginName: 'Logs',
commands: {},
}
});
} catch (e) {
// Do nothing
}
}
/**
* Expose the supported commands as read-only property.
*/
get commands() {
return this._commands;
}
/**
* Expose the supported hooks as read-only property.
*/
get hooks() {
return this._hooks;
}
/**
* Expose the options as read-only property.
*/
get options() {
return this._options;
}
/**
* Expose the supported provider as read-only property.
*/
get provider() {
return this._provider;
}
/**
* Expose the serverless object as read-only property.
*/
get serverless() {
return this._serverless;
}
/**
* Expose the stack name as read-only property.
*/
get stackName() {
return this._stackName;
}
_cleanup() {
this._serverless.cli.log('Cleanup !!!!');
}
}
module.exports = AwsAlias;