-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.js
executable file
·367 lines (326 loc) · 13.2 KB
/
aws.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
#!/usr/bin/env node
'use strict';
var fs = require('fs'),
http = require('http'),
aws2js = require('aws2js'),
program = require('commander'),
async = require('async'),
config = require('./config/default.json'),
pack = require('./package'),
command_given = false;
var dumpInfo = require('./commands/dumpInfo');
program
.version(pack.version);
program
.command('start [num] [region]')
.description('Add <num> AWS instances. Default = 1.')
.action(function(num, region) {
command_given = true;
var nInstances = +(num || 1);
var regions = region ? [region] : config.regions;
for (var i = 0; i < nInstances; i++) {
// Start equally in all regions.
startInstance(getClient(regions[i%regions.length]));
}
});
program
.command('dump')
.description('Dump all casperJS data to your computer.')
.action(function() {
command_given = true;
var regions = config.regions;
async.map(regions.map(getClient), getInstances, function(err, res) {
if (err) {
throw(err);
}
dumpInfo(res);
});
});
program
.command('stop [num] [region]')
.description('Remove <num> AWS instances. Default = 1. Accepts \'all\'.')
.action(function(num, region) {
command_given = true;
var nInstances = (num === "all") ? 10000 : +(num || 1);
var regions = region ? [region] : config.regions;
async.map(regions.map(getClient), getInstances, function(err, res) {
var instanceCount = res.map(function(r) {
return r.length;
}).reduce(function(a,b) {
return a+b;
}, 0);
nInstances = Math.min(nInstances, instanceCount);
var regionId = -1;
var instancesToTerminate = regions.map(function() {return [];});
for (var i = 0; i < nInstances; i++) {
regionId = (regionId+1) % regions.length;
while (res[regionId].length === 0) {
regionId = (regionId+1) % regions.length;
}
instancesToTerminate[regionId].push(res[regionId].shift().instanceId);
}
for (i = 0; i < regions.length; i++) {
stopInstances(getClient(regions[i]), instancesToTerminate[i]);
}
});
});
program
.command('status [region]')
.description('Top-like automatically updating status of instances in all regions.')
.action(function(region) {
command_given = true;
var regions = region ? [region] : config.regions;
updateInstances(regions);
setInterval(printStatus, 500);
});
program
.command('restart')
.description('Restart all instances')
.action(function() {
command_given = true;
var regions = config.regions;
regions.map(getClient).forEach(function(client) {
getInstances(client, function(err, instances) {
for (var i = 0; i < instances.length; i++) {
sendParam(client, instances[i], 'restart', 1);
}
});
});
});
program
.command('set <param> <value>')
.description('Set a parameter to given value in all current instances.')
.action(function(param, val) {
command_given = true;
var regions = config.regions;
regions.map(getClient).forEach(function(client) {
getInstances(client, function(err, instances) {
for (var i = 0; i < instances.length; i++) {
sendParam(client, instances[i], param, val);
}
});
});
});
// ====== Working with instances ===============================================
function normalizeResponse(obj, key) { // Handles quirks of xml-to-js transformation ('item', empty objects)
if (typeof obj == 'object') {
var keys = Object.keys(obj);
if (keys.length === 0) {
if (key && (key.slice(-3) === "Set" || key.slice(-1) === "s")) // Heuristic to determine empty arrays from empty strings.
return [];
return "";
}
if (keys.length === 1 && keys[0] === 'item') {
if (!Array.isArray(obj.item)) obj.item = [obj.item];
return normalizeResponse(obj.item);
}
for (var i = 0; i < keys.length; i++)
obj[keys[i]] = normalizeResponse(obj[keys[i]], keys[i]);
}
return obj;
}
var regionInstances = {
// We are interested in Ubuntu Server 16.04 LTS.
"eu-west-1": "ami-045a58975c6b7ef82"
};
var clients = {}; // {<region>: <ec2 client>}
function getClient(region) {
region = region || config.regions[0] || "us-east-1";
if (!clients[region]) {
var client = clients[region] = aws2js.load('ec2');
client.region = region;
if (!(region in regionInstances)) {
throw new Error("Unknown AWS region: "+region+". Must be one of: "+Object.keys(regionInstances));
}
client.setRegion(region);
if (typeof config.accessKeyId !== 'string' || config.accessKeyId.length !== 20 ||
typeof config.accessKeySecret != 'string' || config.accessKeySecret.length !== 40)
throw new Error("Please provide AWS Access Keys in 'aws-config.json' file.")
client.setCredentials(config.accessKeyId, config.accessKeySecret);
}
return clients[region];
}
function startInstance(client) {
// Prepare userdata and do a basic templating (replace <%=filename.ext%> to contents of filename.ext).
var userdata = fs.readFileSync(config.userDataFile, "utf8")
.replace(/<%=(.*)%>/g, function(_, name) {
return fs.readFileSync(name, "utf8");
});
// We use Cloud Init, see https://help.ubuntu.com/community/CloudInit
var params = {
InstanceType: (config.instanceType || "t1.micro"),
ImageId: regionInstances[client.region],
MinCount: 1,
MaxCount: 1,
UserData: new Buffer(userdata).toString("base64")
};
if (config.keyName) {
// To gain ssh access to instances, you should either upload a key to
// all given EC2 regions and use it here, or use Cloud Init to write them manually.
params.KeyName = config.keyName;
}
// http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-RunInstances.html
client.request("RunInstances", params, function (err, resp) {
if (err) return console.error("RunInstances error: ", JSON.stringify(err));
resp = normalizeResponse(resp);
var instanceId = resp.instancesSet[0].instanceId;
params = {
"ResourceId.1": instanceId,
};
var keys = Object.keys(config.instanceTags);
for (var i = 0; i < keys.length; i++) {
params["Tag."+(i+1)+".Key"] = keys[i];
params["Tag."+(i+1)+".Value"] = config.instanceTags[keys[i]];
};
if (keys.length > 0) {
client.request("CreateTags", params, function(err, resp) {
if (err) return console.error("CreateTags error: ", JSON.stringify(err));
console.log("Instance " + instanceId + " started in region " + client.region);
})
} else {
console.log("Instance " + instanceId + " started in region " + client.region);
}
});
}
function stopInstances(client, instanceIdArray) {
if (instanceIdArray.length === 0) return; // Nothing to do.
var params = {};
for (var i = 0; i < instanceIdArray.length; i++)
params["InstanceId."+(i+1)] = instanceIdArray[i];
client.request("TerminateInstances", params, function(err, resp) {
if (err) return console.error("TerminateInstances error: ", JSON.stringify(err));
console.log("Instances " + instanceIdArray.join(", ") + " terminated in region " + client.region);
});
}
function getInstances(client, callback) {
client.request("DescribeInstances", function(err, resp) {
try {
if (err) return callback(err);
}
catch (e) {
console.log(err)
}
resp = normalizeResponse(resp);
var instances = [];
resp.reservationSet.forEach(function(reservation) {
reservation.instancesSet.forEach(function(instance) {
// Check instance is in good state.
// Possible instance states: pending | running | shutting-down | terminated | stopping | stopped
var goodStates = ["pending", "running", "shutting-down"];
if (goodStates.indexOf(instance.instanceState.name) < 0)
return;
// Check instance tags. All tags given in config must be the same.
if (config.instanceTags && instance.tagSet) {
var tags = {};
instance.tagSet.forEach(function(item) {tags[item.key] = item.value;});
for (var key in config.instanceTags)
if (config.instanceTags[key] !== tags[key])
return;
}
// All checks successful, add instance to the resulting array.
instances.push(instance);
});
});
try {
callback(null, instances);
}
catch (e) {
// Hopefully fine.
}
});
}
function pad(str, width) { while(str.length < width) str=" "+str; return str; }
var status = [], statusRegions = []; // Array of arrays of instances
var updaters = {}; // instance-id -> {lastUpdateTime: .., updateReq: ..., updateRes: {}}
function printStatus() {
process.stdout.write('\u001B[2J\u001B[0;0f');
status.forEach(function(regionInstances, i) {
console.log(statusRegions[i] + ": ");
var num = 0;
regionInstances.forEach(function(inst) {
var message = "Unknown";
var u = updaters[inst.instanceId];
if (u) {
var t1 = (u.lastUpdateTime>u.lastReqTime) ? ((u.lastUpdateTime-u.lastReqTime)/100).toFixed(0) : " ";
var t2 = ((Date.now()-u.lastReqTime)/100).toFixed(0);
message = "("+pad(t1, 2)+"/"+pad(t2,2)+") "+JSON.stringify(u.updateRes).replace(/[{}"]/g, "");
if (u.updateErr) message += " ["+u.updateErr.code+"]";
}
console.log(" "+inst.instanceId+"["+inst.instanceState.name+"]: "+message);
try {
var d = u.updateRes;
if (d && d.lastStatus && d.lastStatus.errors_req) {
num = num + parseInt(d.lastStatus.errors_req, 10);
}
}
catch (err) {
// Meh.
}
if (!u) updaters[inst.instanceId] = u = {lastUpdateTime: Date.now(), lastReqTime: 0, updateRes: {}};
var rt = Date.now()-u.lastReqTime;
if (rt > 2000) {
//if (u.updateReq) {u.updateReq.abort(); delete u.updateReq;}
if (inst.dnsName != "") {
u.lastReqTime = Date.now();
var req = http.request({host: inst.dnsName, port: 8889});
req.setHeader("Connection", "keep-alive");
u.updateReq = req;
req.on('response', function(res) {
var text = "";
res.setEncoding("utf8");
res.on('data', function(data) {text = text+data});
res.on('end', function() {
u.lastUpdateTime = Date.now();
u.updateRes = JSON.parse(text);
delete u.updateErr;
delete u.updateReq;
});
});
req.on('error', function(err) {
u.updateErr = err;
delete u.updateReq;
});
req.end();
}
}
});
console.log('Total errors in region on last completed run: %d', num);
});
}
function updateInstances(regions) {
var clients = regions.map(getClient);
async.map(clients, getInstances, function(err, res) {
if (err) return console.error("DescribeInstances error: "+JSON.stringify(err));
status = res;
statusRegions = regions;
updateInstances(regions);
});
}
function sendParam(client, inst, param, val) {
if (!inst.dnsName) {
return console.error("Instance "+inst.instanceId+" has no dnsName.");
}
var p = {};
p[param] = val;
var query = require('querystring').stringify(p);
var path = (query == 'restart=1') ? '/restart' : ('/set?'+query);
var req = http.request({host: inst.dnsName, port: config.controlPort, path: path});
req.on('response', function(res) {
if (res.statusCode !== 200)
return console.error("Instance "+inst.instanceId+ " status code = " + res.statusCode);
var text = "";
res.setEncoding("utf8");
res.on('data', function(data) {text += data});
res.on('end', function() {
console.log("Instance "+inst.instanceId+ " OK: "+text);
});
});
req.on('error', function(err) {
console.error("Instance "+inst.instanceId+" error:"+err);
});
req.end();
}
// == Process command line arguments ===========================================
program.parse(process.argv);
if (!command_given)
program.parse(process.argv.slice(0,2).concat(['-h'])); // Print help.