-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
98 lines (86 loc) · 3.01 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
'use strict';
/* eslint-disable no-console */
const CB = require('camda').CB;
const AWS = require('aws-sdk');
const gatherInfo = require('./lib/gatherInformation');
const scaleUp = require('./lib/scaleUp');
const scaleDown = require('./lib/scaleDown');
if(!process.env.AWS_REGION) {
console.error('AWS_REGION not set');
process.exit(1);
}
const as = new AWS.AutoScaling();
let ASGroupName = process.env.AS_GROUP_NAME;
let clusterName = process.env.CLUSTER_NAME;
let dryRun = process.env.DRY_RUN;
let interval = process.env.INTERVAL || 20000;
let loopsRemaining = process.env.MAX_LOOPS || 20;
if(!ASGroupName || !clusterName) {
console.error('AS_GROUP_NAME or CLUSTER_NAME missing');
process.exit(1);
}
//increaseASGroupCapacity :: CB (AutoscalingGroupName, currentCapacity) ()
const increaseASGroupCapacity = CB.create(params => ({
AutoScalingGroupName: params.ASGroupName,
DesiredCapacity: params.currentCapacity + 1,
HonorCooldown: true
}))
.compose(CB((asgRequest, cb) => {
if(dryRun) {
console.log('Dry run - will not increase capacity');
return cb(null, null);
}
console.log('Increasing desired capacity');
as.setDesiredCapacity(asgRequest, cb);
}));
//removeInstance :: CB instanceId ()
const removeInstance = CB.create(instanceId => ({
InstanceId: instanceId, ShouldDecrementDesiredCapacity: true
}))
.compose(CB((terminateInstanceRequest, cb) => {
if(dryRun) {
console.log('Dry run - will not remove instance: ' + terminateInstanceRequest.InstanceId);
return cb(null, null);
}
console.log('Removing instance: ' + terminateInstanceRequest.InstanceId);
as.terminateInstanceInAutoScalingGroup(terminateInstanceRequest, cb);
}));
const gc = () => {
if(global.gc) global.gc();
};
const run = () => {
if(loopsRemaining <= 0) {
process.exit(0);
return;
}
loopsRemaining--;
gatherInfo({
clusterName: clusterName,
ASGroupName: ASGroupName
}, (err, data) => {
const scaleDownInstance = scaleDown(data);
const scaleUpInstance = scaleUp(data);
if(data.containerInstances.length !== data.desiredCapacity) {
console.log('Still scaling up instances');
} else if(scaleUpInstance) {
return increaseASGroupCapacity({currentCapacity: data.desiredCapacity, ASGroupName: ASGroupName}, err => {
if(err) return console.error(err);
console.log('Capacity increased');
gc();
});
} else if(data.awaitingTasks.length !== 0) {
console.log('Still scaling up tasks');
} else if(scaleDownInstance) {
return removeInstance(scaleDownInstance, err => {
if(err) return console.error(err);
console.log('Capacity decreased');
gc();
});
} else {
console.log('No changes needed.');
}
gc();
});
};
run();
setInterval(run, interval);