-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
127 lines (113 loc) · 3.26 KB
/
server.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
var production = process.env.NODE_ENV == 'production'
// amazon
var AWS = require('aws-sdk');
var region = 'eu-west-1';
AWS.config.update({
region: region
});
var ECS = new AWS.ECS();
var EC2 = new AWS.EC2();
var cloudwatch = new AWS.CloudWatch();
// application
var jwt = require('express-jwt');
var express = require('express');
var requestify = require('requestify');
var bodyParser = require('body-parser');
var compression = require('compression');
var app = express();
app.use(compression());
app.use(bodyParser.json());
app.use(express.static(__dirname + (production ? '/dist' : '/public')));
/*
app.use(jwt({secret: fs.readFileSync('jwt-public.pem')}).unless({path: ['/favicon.ico']}));
app.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') {
res.status(401).send('invalid token');
} else {
next();
}
});
*/
var error = function (res, err) {
if (err) {
console.log(err);
res.error(err).end();
}
return !!err;
};
app.post('/ec2/instances', function (req, res) {
var ids = req.body.InstanceIds;
if (ids && ids.length > 0) {
EC2.describeInstances({InstanceIds: ids}, function (err, data) {
if (!error(res, err)) {
res.json(data.Reservations.map(function (reservation) {
return reservation.Instances[0];
}));
}
})
} else {
res.json([]);
}
});
app.get('/ec2/instances/:id/cpu', function (req, res) {
var end = new Date();
var start = new Date();
start.setHours(end.getHours() - 1);
var params = {
EndTime: end,
MetricName: 'CPUUtilization',
Namespace: 'AWS/EC2',
Period: 60,
StartTime: start,
Statistics: ['Average'],
Dimensions: [{Name: 'InstanceId', Value: req.params.id}],
Unit: 'Percent'
};
cloudwatch.getMetricStatistics(params, function (err, data) {
if (!error(res, err)) {
data.Datapoints.sort(function (a, b) {
return new Date(a.Timestamp) - new Date(b.Timestamp);
});
res.json(data);
}
});
});
app.get('/ecs/clusters', function (req, res) {
ECS.listClusters({}, function (err, data) {
res.json(data.clusterArns.map(function(clusterArn) {
var split = clusterArn.split('/');
return split.length > 1 ? split[1] : '';
}));
});
});
app.get('/ecs/clusters/:cluster/instances', function (req, res) {
ECS.listContainerInstances({cluster: req.params.cluster}, function (err, data) {
if (!error(res, err) && data.containerInstanceArns.length) {
ECS.describeContainerInstances({cluster: req.params.cluster, containerInstances: data.containerInstanceArns},
function (err, descCI) {
if (!error(res, err)) {
res.json(descCI.containerInstances);
}
});
} else {
res.json([]);
}
});
});
app.get('/ecs/clusters/:cluster/tasks', function (req, res) {
ECS.listTasks({cluster: req.params.cluster}, function (err, data) {
if (!error(res, err) && data.taskArns.length) {
ECS.describeTasks({cluster: req.params.cluster, tasks: data.taskArns}, function (err, data) {
res.json(data.tasks);
});
} else {
res.json([]);
}
});
});
app.get('/ecs/taskDefinition', function (req, res) {
ECS.describeTaskDefinition({taskDefinition: req.query.arn}, function (err, data) {
res.json(data);
})
});
app.listen(3000);