This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
checkers.js
287 lines (270 loc) · 8.66 KB
/
checkers.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
// Copyright 2018 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE.txt file for details.
'use strict';
const {processDeltas} = require('@canonical/maraca/lib/delta-handlers');
const {Status, Terminal} = require('@canonical/juju-react-components');
const React = require('react');
/**
Check that the model agent is up and running.
*/
async function checkModel(connect, status, ui) {
const model = status.model;
const modelStatus = model.modelStatus.status;
if (modelStatus !== 'available') {
ui.error(`model ${model.name} - status is ${modelStatus}`);
ui.addLink('File RT', `http://rt.admin.canonical.com/`);
}
}
/**
Check that there are no units in error.
Provide the ability to retry units in error state.
*/
async function checkUnits(connect, status, ui) {
const refresh = () => {
setTimeout(async () => {
const {conn, logout} = await connect();
try {
status = await conn.facades.client.fullStatus();
} finally {
logout();
}
ui.refresh();
checkUnits(connect, status, ui);
}, 3000);
};
for (let app in status.applications) {
const units = status.applications[app].units;
for (let unit in units) {
const workloadStatus = units[unit].workloadStatus;
if (workloadStatus.status === 'error') {
ui.error(
`model ${status.model.name} - unit ${unit} is in ${
workloadStatus.status
} state: ${workloadStatus.info}`
);
ui.addAction('Retry', async write => {
const {conn, logout} = await connect();
try {
write(`retrying unit ${unit}`, {autoclose: true});
await conn.facades.client.resolved({unitName: unit});
} finally {
logout();
}
refresh();
});
const machine = units[unit].machine;
if (unitsInMachine(status, machine).length <= 1) {
ui.addAction('Replace', async write => {
const {conn, logout} = await connect();
try {
write(`replacing unit ${unit}`, {autoclose: true});
ui.log(`destroying machine ${machine}`);
await conn.facades.client.destroyMachines({
machineNames: machine,
force: true
});
ui.log(`adding another unit to ${app}`);
await conn.facades.application.addUnits({
application: app,
numUnits: 1
});
} finally {
logout();
}
refresh();
});
}
ui.addAction('Show Status', async write => {
const {conn, logout} = await connect();
let handle;
handle = conn.facades.client.watch((err, delta) => {
if (err) {
ui.error(err);
return;
}
handle.stop();
logout();
const data = processDeltas(delta.deltas).changed;
const model = status.model;
const modelTag = conn.info.modelTag.split('-');
modelTag.shift();
write(
<Status
generateApplicationOnClick={() => {}}
generateApplicationURL={() => {}}
generateCharmURL={() => {}}
generateMachineOnClick={() => {}}
generateMachineURL={() => {}}
generateUnitOnClick={() => {}}
generateUnitURL={() => {}}
getIconPath={() => {}}
model={{
cloud: model.cloudTag.split('-')[1],
environmentName: model.name,
modelUUID: modelTag.join('-'),
region: model.region,
sla: model.sla,
version: model.version
}}
navigateToApplication={() => {}}
navigateToCharm={() => {}}
navigateToMachine={() => {}}
valueStore={{
applications: data.applications || {},
machines: data.machines || {},
relations: data.relations || {},
remoteApplications: data.remoteApplications || {},
units: data.units || {}
}}
/>
);
});
});
const {conn, logout} = await connect();
try {
const info = await conn.facades.client.modelInfo();
const user = info.ownerTag.split('@')[0].slice(5);
ui.addLink(
'Open GUI',
`https://jujucharms.com/u/${user}/${status.model.name}`
);
} finally {
logout();
}
}
}
}
}
/**
Return a list of units located in the given machine.
@param {Object} status The model status.
@param {String} machine The machine id.
@returns {Array} A list of names for units placed in the given machine.
*/
function unitsInMachine(status, machine) {
const result = [];
for (let app in status.applications) {
const units = status.applications[app].units;
for (let unit in units) {
if (units[unit].machine === machine) {
result.push(unit);
}
}
}
return result;
}
/**
Check jujushell errors.
*/
async function checkJujushell(connect, status, ui) {
const {conn, logout, options, url} = await connect();
const application = conn.facades.application;
try {
for (let app in status.applications) {
const info = status.applications[app];
if (!info.charm.startsWith('cs:~juju-gui/jujushell')) {
continue;
}
const result = await application.get({application: app});
const dnsName = result.config['dns-name'].value;
const target = `https://${dnsName}/metrics`;
ui.log(`making a GET request to ${target}`);
const resp = await makeRequest(
'GET',
`https://cors-anywhere.herokuapp.com/${target}`
);
let numErrors = 0;
const errors = {};
resp.split('\n').forEach(line => {
if (line.startsWith('jujushell_errors_count')) {
const message = line.slice(
line.indexOf('"') + 1,
line.lastIndexOf('"')
);
const num = parseInt(line.split(' ').reverse()[0], 10);
numErrors += num;
errors[message] = num;
}
});
if (numErrors > 0) {
ui.error(
`model ${
status.model.name
} - app ${app} exposed at ${dnsName} has ${numErrors} errors`
);
ui.addAction('Show Errors', async write => {
const rows = [];
for (let message in errors) {
rows.push(
<tr key={message}>
<td key="message">{message}</td>
<td key="#">{errors[message]}</td>
</tr>
);
}
write(
<table>
<thead>
<tr>
<th key="message">message</th>
<th key="#">#</th>
</tr>
</thead>
<tbody>{rows}</tbody>
</table>
);
});
// If the terminal is connected to JAAS, and we are also connected to
// JAAS, then we can also offer the ability to actually open the shell.
const host = url.split('/')[2];
if (result.config['juju-addrs'].value === host) {
ui.addAction('Open Terminal', async write => {
const storage = options.bakery.storage;
const token = JSON.parse(atob(storage.get('identity')));
const macaroons = {'https://api.jujucharms.com/identity': token};
write(
<Terminal
WebSocket={WebSocket}
addNotification={() => {}}
address={`wss://${dnsName}/ws/`}
changeState={() => {}}
creds={{macaroons: macaroons}}
/>
);
});
}
}
}
} finally {
logout();
}
}
/**
Send a XHR request using promises.
@param {String} method The HTTP method.
@param {String} url The URL to use for the request.
@returns {Promise} Resolved when a good response is returned, rejected when
a bad response (>300) is returned.
*/
function makeRequest(method, url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.onload = function() {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
return;
}
reject({status: this.status, statusText: xhr.statusText});
};
xhr.onerror = function() {
reject({status: this.status, statusText: xhr.statusText});
};
xhr.send();
});
}
module.exports = {
checkModel,
checkUnits,
checkJujushell
};