-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
145 lines (125 loc) · 4.42 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
'use strict';
module.exports = function(S) {
const path = require( 'path' ),
SUtils = require( S.getServerlessPath( 'utils' ) ),
context = require( S.getServerlessPath( 'utils/context' ) ),
SCli = require( S.getServerlessPath( 'utils/cli' ) ),
AWS = require( 'aws-sdk' ),
BbPromise = require( 'bluebird' );
class LambdaPrune extends S.classes.Plugin {
constructor() {
super();
this.name = 'prune';
}
static getName() {
return 'net.nopik.' + LambdaPrune.name;
}
registerActions() {
S.addAction(this.prune.bind(this), {
handler: 'prune',
description: `Delete old/unused lambda versions from your AWS account`,
context: 'function',
contextAction: 'prune',
options: [
{
option: 'region',
shortcut: 'r',
description: 'region you want to list env vars for'
},
{
option: 'number',
shortcut: 'n',
description: 'Keep last N versions (default: 5)'
}
]
});
return BbPromise.resolve();
}
registerHooks() {
return Promise.resolve();
}
_slowdownRequests( f ){
return new BbPromise( function( resolve, reject ){
let doCall = function(){
f()
.then(resolve)
.catch(function(error) {
if( error.message == 'Rate Exceeded.' ) {
SCli.log("'Too many requests' received, sleeping 5 seconds");
setTimeout( doCall, 5000 );
} else
reject( error );
});
};
doCall();
});
}
_listLambdas(evt){
let _this = this;
let params = {};
if( process.env.AWS_REGION ) {
params.region = process.env.AWS_REGION;
} else {
params.region = evt.region
}
let Lambda = new AWS.Lambda( params );
BbPromise.promisifyAll( Object.getPrototypeOf( Lambda ), { suffix: 'Asynchronously' } );
//TODO: handle marker for pagination?
return _this._slowdownRequests( function(){ return Lambda.listFunctionsAsynchronously({}); } ).then(function(functions){
BbPromise.map(functions.Functions, function(f){
return BbPromise.all([
_this._slowdownRequests( function(){ return Lambda.listAliasesAsynchronously({ FunctionName: f.FunctionName }); } ),
_this._slowdownRequests( function(){ return Lambda.listVersionsByFunctionAsynchronously({ FunctionName: f.FunctionName }); } )
]).spread(function( aliases, versions ){
SCli.log( `Pruning ${f.FunctionName}, found ${aliases.Aliases.length} aliases and ${versions.Versions.length} versions` );
let keepVersions = aliases.Aliases.map(function(a){
return a.FunctionVersion;
});
keepVersions.push( '$LATEST' );
let vs = versions.Versions.sort(function( v1, v2 ){
if( v1.LastModified < v2.LastModified ) {
return 1;
} else {
if( v1.LastModified > v2.LastModified ) {
return -1;
} else {
return 0;
}
}
});
let toKeep = evt.number;
vs.forEach(function( v ){
if( (toKeep > 0) && (v.Version != '$LATEST') && (keepVersions.indexOf( v.Version ) < 0) ) {
keepVersions.push( v.Version );
toKeep--;
}
});
return BbPromise.map(versions.Versions, function (v){
if( keepVersions.indexOf( v.Version ) < 0 ) {
SCli.log( `Deleting version ${v.Version} of ${f.FunctionName} function` );
return _this._slowdownRequests( function(){ return Lambda.deleteFunctionAsynchronously({
FunctionName: f.FunctionName,
Qualifier: v.Version
}); });
}
}, { concurrency: 3 });
});
}, { concurrency: 3 });
return evt;
});
}
prune(evt) {
let _this = this;
if (S.cli) {
evt = JSON.parse(JSON.stringify(S.cli.options));
if (S.cli.options.nonInteractive) S._interactive = false
}
_this.evt = evt;
if( !_this.evt.number ){
_this.evt.number = 5;
}
return _this._listLambdas(_this.evt);
}
}
return LambdaPrune;
};