-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
64 lines (54 loc) · 1.49 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
var elasticsearch = require('elasticsearch');
var moment = require('moment');
var endpoint = process.env.ENDPOINT;
var excludedIndices = (process.env.EXCLUDED_INDICES || '.kibana').split(/[ ,]/);
var indexDate = moment.utc().subtract(+(process.env.MAX_INDEX_AGE || 14), 'days');
exports.handler = function(event, context) {
var client = new elasticsearch.Client({
host: endpoint,
});
getIndices(client)
.then(extractIndices)
.then(filterIndices)
.then(deleteIndices(client))
.then(report(context.succeed), context.fail);
}
function getIndices(client) {
return client.indices.getAliases();
}
function extractIndices(results) {
return Object.keys(results);
}
function filterIndices(indices) {
return indices.filter(function(index) {
return !isExcluded(index) && isTooOld(index);
});
}
function deleteIndices(client) {
return function(indices) {
if (indices.length > 0) {
return client.indices.delete({index: indices}).then(function() {
return indices;
});
} else {
return indices;
}
};
}
function report(cb) {
return function(indices) {
var len = indices.length;
if (len > 0) {
cb('Successfully deleted ' + len + ' indices: ' + indices.join(', '));
} else {
cb('There were no indices to delete.');
}
};
}
function isExcluded(indexName) {
return excludedIndices.indexOf(indexName) !== -1;
}
function isTooOld(indexName) {
var m = moment.utc(indexName, 'YYYY.MM.DD');
return m.isBefore(indexDate);
}