-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdashboard.js
143 lines (115 loc) · 4.25 KB
/
dashboard.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
function createEc2dashboard(config) {
var showInstances = function (ec2, services, hcpath) {
var params = {
Filters: [
{
Name: 'tag:Name',
Values: services
},
],
};
ec2.describeInstances(params, function (err, data) {
if (err) {
console.log(err, err.stack);
} else {
console.log(data);
var $container = $('#ec2dashboard');
$('.date', $container).fadeOut(100, function () {
$(this).text((new Date()).toTimeString()).fadeIn(500);
});
$.each(data.Reservations, function (index, reservation) {
$.each(reservation.Instances, function (index, instance) {
renderInstnaceView(instance, $container, hcpath);
});
});
}
});
}
var renderInstnaceView = function (instance, $container, hcpath) {
var instanceId = instance.InstanceId;
var instanceName = instanceId;
var instanceIP = instance.PrivateIpAddress
if (typeof instanceIP == "undefined") {
return;
}
if (typeof instance.Tags[0] != "undefined") {
instanceName = instance.Tags[0].Value;
for (var i in instance.Tags) {
if (instance.Tags[i].Key == 'Name') {
instanceName = instance.Tags[i].Value
}
}
}
if ($('#' + instanceName, $container).length === 0) {
$('.instances', $container).append(
'<div id="' + instanceName + '" class="service">' +
'<div class="service-name">' + instanceName + '</div>' +
'</div>');
}
if ($('#' + instanceId, $container).length === 0) {
$('#' + instanceName, $container).append('<div id="' + instanceId + '" class="instance"></div>');
}
var url = 'http://' + instanceIP + '/' + hcpath;
checkInstance($('#' + instanceId), url);
}
var checkInstance = function checkInstance($elem, url) {
$elem.removeClass('blink');
$.ajax({
url: url,
timeout: 5000
}).done(function (data) {
var revision = '';
var hcResponse = '';
if (typeof data == 'object') {
$.each(data, function (key, value) {
if (key == 'revision') {
revision = value;
}
hcResponse += key + ': ' + value + '<br/>';
});
} else {
hcResponse = data;
}
if (revision) {
$elem.css('border-bottom', '12px solid #' + revision.substr(0, 6));
}
if ($elem.data('hash') != undefined && $elem.data('hash') != window.btoa(hcResponse)) {
$elem.addClass('blink');
}
$elem.removeClass('fail').addClass('ok').data('hash', window.btoa(hcResponse)).html('<a href="' + url + '" target="_blank">' + hcResponse + '</a>');
}).fail(function (data) {
$elem.removeClass('ok').addClass('fail').html('<a href="' + url + '" target="_blank">fail</a>');
});
}
if (!config.accessKeyId) {
alert('Specify accessKeyId');
}
if (!config.region) {
alert('Specify a region');
}
if (!config.services) {
alert('Specify a list of services');
}
if (!config.hcpath) {
alert('Specify healtcheck path');
}
var services = [];
if (config.services) {
services = config.services.split(',');
}
var secretPassPhrase = config.secretAccessKey;
if (!secretPassPhrase) {
secretPassPhrase = prompt('Enter the AWS secretAccessKey', '');
}
AWS.config = new AWS.Config({
accessKeyId: config.accessKeyId,
secretAccessKey: secretPassPhrase,
region: config.region
});
var ec2 = new AWS.EC2({apiVersion: '2016-11-15'});
showInstances(ec2, services, config.hcpath);
var recheckInterval = 10000;
setInterval(function () {
showInstances(ec2, services, config.hcpath);
}, recheckInterval);
}