forked from claygregory/serverless-prune-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
387 lines (325 loc) · 11 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
'use strict';
const BbPromise = require('bluebird');
class Prune {
constructor(serverless, options, { log, progress } = {}) {
this.serverless = serverless;
this.options = options || {};
this.provider = this.serverless.getProvider('aws');
this.log = log || serverless.cli.log.bind(serverless.cli);
this.progress = progress;
this.pluginCustom = this.loadCustom(this.serverless.service.custom);
this.commands = {
prune: {
usage: 'Clean up deployed functions and/or layers by deleting older versions.',
lifecycleEvents: ['prune'],
options: {
number: {
usage: 'Number of previous versions to keep',
shortcut: 'n',
required: true,
type: 'string'
},
stage: {
usage: 'Stage of the service',
shortcut: 's',
type: 'string'
},
region: {
usage: 'Region of the service',
shortcut: 'r',
type: 'string'
},
function: {
usage: 'Function name. Limits cleanup to the specified function',
shortcut: 'f',
required: false,
type: 'string'
},
layer: {
usage: 'Layer name. Limits cleanup to the specified Lambda layer',
shortcut: 'l',
required: false,
type: 'string'
},
includeLayers: {
usage: 'Boolean flag. Includes the pruning of Lambda layers.',
shortcut: 'i',
required: false,
type: 'boolean'
},
dryRun: {
usage: 'Simulate pruning without executing delete actions. Deletion candidates are logged when used in conjunction with --verbose',
shortcut: 'd',
required: false,
type: 'boolean'
},
verbose: {
usage: 'Enable detailed output during plugin execution',
required: false,
type: 'boolean'
}
}
},
};
this.hooks = {
'prune:prune': this.cliPrune.bind(this),
'after:deploy:deploy': this.postDeploy.bind(this)
};
}
getNumber() {
return this.options.number || this.pluginCustom.number;
}
loadCustom(custom) {
const pluginCustom = {};
if (custom && custom.prune) {
if (custom.prune.number != null) {
const number = parseInt(custom.prune.number);
if (!isNaN(number)) pluginCustom.number = number;
}
if (typeof custom.prune.automatic === 'boolean') {
pluginCustom.automatic = custom.prune.automatic;
}
if (typeof custom.prune.includeLayers === 'boolean') {
pluginCustom.includeLayers = custom.prune.includeLayers;
}
}
return pluginCustom;
}
cliPrune() {
if (this.options.dryRun) {
this.logNotice('Dry-run enabled, no pruning actions will be performed.');
}
if(this.options.includeLayers) {
return BbPromise.all([
this.pruneFunctions(),
this.pruneLayers()
]);
}
if (this.options.layer && !this.options.function) {
return this.pruneLayers();
} else {
return this.pruneFunctions();
}
}
postDeploy() {
this.pluginCustom = this.loadCustom(this.serverless.service.custom);
if (this.options.noDeploy === true) {
return BbPromise.resolve();
}
if (this.pluginCustom.automatic &&
this.pluginCustom.number !== undefined && this.pluginCustom.number >= 0) {
if(this.pluginCustom.includeLayers) {
return BbPromise.all([
this.pruneFunctions(),
this.pruneLayers()
]);
}
return this.pruneFunctions();
} else {
return BbPromise.resolve();
}
}
pruneLayers() {
const selectedLayers = this.options.layer ? [this.options.layer] : this.serverless.service.getAllLayers();
const layerNames = selectedLayers.map(key => this.serverless.service.getLayer(key).name || key);
this.createProgress(
'prune-plugin-prune-layers',
'Pruning layer versions'
);
return BbPromise.mapSeries(layerNames, layerName => {
return BbPromise.join(
this.listVersionsForLayer(layerName),
(versions) => ({ name: layerName, versions: versions })
);
}).each(({ name, versions }) => {
if (!versions.length) {
return BbPromise.resolve();
}
const deletionCandidates = this.selectPruneVersionsForLayer(versions);
if (deletionCandidates.length > 0) {
this.updateProgress('prune-plugin-prune-layers', `Pruning layer versions (${name})`);
}
if (this.options.dryRun) {
this.printPruningCandidates(name, deletionCandidates);
return BbPromise.resolve();
} else {
return this.deleteVersionsForLayer(name, deletionCandidates);
}
}).then(() => {
this.clearProgress('prune-plugin-prune-layers');
this.logSuccess('Pruning of layers complete');
});
}
pruneFunctions() {
const selectedFunctions = this.options.function ? [this.options.function] : this.serverless.service.getAllFunctions();
const functionNames = selectedFunctions.map(key => this.serverless.service.getFunction(key).name);
this.createProgress(
'prune-plugin-prune-functions',
'Pruning function versions'
);
return BbPromise.mapSeries(functionNames, functionName => {
return BbPromise.join(
this.listVersionForFunction(functionName),
this.listAliasesForFunction(functionName),
(versions, aliases) => ( { name: functionName, versions: versions, aliases: aliases } )
);
}).each(({ name, versions, aliases }) => {
if (!versions.length) {
return BbPromise.resolve();
}
const deletionCandidates = this.selectPruneVersionsForFunction(versions, aliases);
if (deletionCandidates.length > 0) {
this.updateProgress('prune-plugin-prune-functions', `Pruning function versions (${name})`);
}
if (this.options.dryRun) {
this.printPruningCandidates(name, deletionCandidates);
return BbPromise.resolve();
} else {
return this.deleteVersionsForFunction(name, deletionCandidates);
}
}).then(() => {
this.clearProgress('prune-plugin-prune-functions');
this.logSuccess('Pruning of functions complete');
});
}
deleteVersionsForLayer(layerName, versions) {
return BbPromise.each(versions, version => {
this.logInfo(`Deleting layer version ${layerName}:${version}.`);
const params = {
LayerName: layerName,
VersionNumber: version
};
return BbPromise.resolve()
.then(() => this.provider.request('Lambda', 'deleteLayerVersion', params))
.catch(e => {
throw e;
});
});
}
deleteVersionsForFunction(functionName, versions) {
return BbPromise.each(versions, version => {
this.logInfo(`Deleting function version ${functionName}:${version}.`);
const params = {
FunctionName: functionName,
Qualifier: version
};
return BbPromise.resolve()
.then(() => this.provider.request('Lambda', 'deleteFunction', params))
.catch(e => {
//ignore if trying to delete replicated lambda edge function
if (e.providerError && e.providerError.statusCode === 400
&& e.providerError.message.startsWith('Lambda was unable to delete')
&& e.providerError.message.indexOf('because it is a replicated function.') > -1) {
this.logWarning(`Unable to delete replicated Lambda@Edge function version ${functionName}:${version}.`);
} else {
throw e;
}
});
});
}
listAliasesForFunction(functionName) {
const params = {
FunctionName: functionName
};
return this.makeLambdaRequest('listAliases', params, r => r.Aliases)
.catch(e => {
//ignore if function not deployed
if (e.providerError && e.providerError.statusCode === 404) return [];
else throw e;
});
}
listVersionForFunction(functionName) {
const params = {
FunctionName: functionName
};
return this.makeLambdaRequest('listVersionsByFunction', params, r => r.Versions)
.catch(e => {
//ignore if function not deployed
if (e.providerError && e.providerError.statusCode === 404) return [];
else throw e;
});
}
listVersionsForLayer(layerName) {
const params = {
LayerName: layerName
};
return this.makeLambdaRequest('listLayerVersions', params, r => r.LayerVersions)
.catch(e => {
// ignore if layer not deployed
if (e.providerError && e.providerError.statusCode === 404) return [];
else throw e;
});
}
makeLambdaRequest(action, params, responseMapping) {
const results = [];
const responseHandler = response => {
Array.prototype.push.apply(results, responseMapping(response));
if (response.NextMarker) {
return this.provider.request('Lambda', action, Object.assign({}, params, { Marker: response.NextMarker }))
.then(responseHandler);
} else {
return BbPromise.resolve(results);
}
};
return this.provider.request('Lambda', action, params)
.then(responseHandler);
}
selectPruneVersionsForFunction(versions, aliases) {
const aliasedVersion = aliases.map(a => a.FunctionVersion);
return versions
.map(f => f.Version)
.filter(v => v !== '$LATEST') //skip $LATEST
.filter(v => aliasedVersion.indexOf(v) === -1) //skip aliased versions
.sort((a, b) => parseInt(a) === parseInt(b) ? 0 : parseInt(a) > parseInt(b) ? -1 : 1)
.slice(this.getNumber());
}
selectPruneVersionsForLayer(versions) {
return versions
.map(f => f.Version)
.sort((a, b) => parseInt(a) === parseInt(b) ? 0 : parseInt(a) > parseInt(b) ? -1 : 1)
.slice(this.getNumber());
}
printPruningCandidates(name, deletionCandidates) {
deletionCandidates.forEach(version => this.logInfo(`${name}:${version} selected for deletion.`));
}
// -- Compatibility with both Framework 2.x and 3.x logging ---
logInfo(message) {
if (this.log.info) this.log.info(message);
else this.log(`Prune: ${message}`);
}
logNotice(message) {
if (this.log.notice) this.log.notice(message);
else this.log(`Prune: ${message}`);
}
logWarning(message) {
if (this.log.warning) this.log.warning(message);
else this.log(`Prune: ${message}`);
}
logSuccess(message) {
if (this.log.success) this.log.success(message);
else this.log(`Prune: ${message}`);
}
createProgress(name, message) {
if (!this.progress) {
this.log(`Prune: ${message}...`);
} else {
this.progress.create({
message,
name
});
}
}
updateProgress(name, message) {
if (!this.progress) {
this.log(`Prune: ${message}`);
} else {
this.progress.get(name).update(message);
}
}
clearProgress(name) {
if (this.progress) {
this.progress.get(name).remove();
}
}
}
module.exports = Prune;