-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
445 lines (409 loc) · 13.6 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
const net = require('net')
const { WebClient } = require('@slack/client')
const { RTMClient } = require('@slack/client')
const { createMessageAdapter } = require('@slack/interactive-messages')
const uuidv4 = require('uuid/v4')
const { readFileSync } = require('fs')
const { exec } = require('child_process')
const { createServer } = require('http')
const { preview } = require('./preview')
const Koa = require('koa')
const koaRoute = require('koa-route')
const HostStatus = {
UNKNOWN: 0,
TESTING_REBOOT: 1,
NORMAL: 2,
DOWN: 3,
WAITING_REBOOT: 4
}
class TimedValue {
constructor (initialValue) {
this._value = initialValue
this._timestamp = Math.floor(new Date() / 1000)
}
get value () {
return this._value
}
set value (newValue) {
this._value = newValue
this._timestamp = Math.floor(new Date() / 1000)
}
get timestamp () {
return this._timestamp
}
get timeFormatting () {
return `<!date^${this.timestamp}^{date_pretty} {time_secs}|${this.timestamp}>`
}
}
class Host {
constructor (hostId, pingHost, ipmiHost, ipmiPassword) {
this.hostId = hostId
this.pingHost = pingHost
this.ipmiHost = ipmiHost
this.ipmiPassword = ipmiPassword
this.supervised = globalSupervised
this.status = new TimedValue(HostStatus.UNKNOWN)
this.pingFailures = 0
this.timeout = null
this.consolePreview = null
}
get consolePreviewNeeded () {
if (!this.ipmiHost) {
return false
}
return this.status.value === HostStatus.DOWN || this.status.value === HostStatus.TESTING_REBOOT || this.status.value === HostStatus.WAITING_REBOOT
}
async heartbeat () {
try {
await sshProbe(this.pingHost, pingConfig['timeout'])
this.pingFailures = 0
await this.post(await this.transition(HostStatus.NORMAL))
} catch (e) {
if (this.status.value === HostStatus.WAITING_REBOOT) {
await this.post(false)
return
}
console.error(e)
this.pingFailures++
if (this.pingFailures === pingConfig['num_trials_before_down']) {
await this.post(await this.transition(HostStatus.DOWN))
} else {
await this.post(false)
}
}
}
async post (forced) {
let willPost = forced
const messageCard = messageCards.tryGetByHostId(this.hostId)
if (messageCard === undefined) {
return
}
if (this.consolePreviewNeeded) {
if (messageCard.consolePreview !== this.consolePreview) {
messageCard.consolePreview = this.consolePreview
willPost = true
}
} else {
if (messageCard.consolePreview) {
messageCard.consolePreview = null
willPost = true
}
}
if (willPost) {
await messageCard.post()
}
}
async transition (newStatus) {
const oldStatus = this.status.value
const oldConsolePreviewNeeded = this.consolePreviewNeeded
if (oldStatus === newStatus) {
return false
}
this.status.value = newStatus
const newConsolePreviewNeeded = this.consolePreviewNeeded
if (!oldConsolePreviewNeeded && newConsolePreviewNeeded) {
consolePreviewUpdate(this).catch(error => console.error(error))
}
this.pingFailures = 0
if (this.timeout) {
clearTimeout(this.timeout)
this.timeout = null
}
const messageCard = messageCards.tryGetByHostId(this.hostId)
if (messageCard !== undefined) {
messageCard.status.value = newStatus
}
switch (newStatus) {
case HostStatus.TESTING_REBOOT: {
return false
}
case HostStatus.NORMAL: {
if (messageCard !== undefined) {
messageCard.recoverToAutomatic = this.supervised && !globalSupervised
}
this.supervised = globalSupervised
return true
}
case HostStatus.DOWN: {
const recurred = oldStatus === HostStatus.TESTING_REBOOT || oldStatus === HostStatus.WAITING_REBOOT
if (recurred) {
if (messageCard !== undefined) {
messageCard.dropToSupervised = !this.supervised && !globalSupervised
messageCard.consolePreview = null
await messageCard.post()
}
this.supervised = true
}
const newCard = messageCards.addByHost(this, recurred)
if (this.supervised || this.ipmiHost === null) {
return true
} else {
newCard.rebootRequested.value = true
const result = await this.transition(HostStatus.WAITING_REBOOT)
return result
}
}
case HostStatus.WAITING_REBOOT: {
try {
if (messageCard !== undefined) {
await messageCard.post()
}
await system(`ipmitool -I lanplus -H ${this.ipmiHost} -U elsabot -L OPERATOR -P ${this.ipmiPassword} power reset`)
await system(`ipmitool -I lanplus -H ${this.ipmiHost} -U elsabot -L OPERATOR -P ${this.ipmiPassword} power on`)
this.timeout = setTimeout(() => {
this.timeout = null
this.transition(HostStatus.TESTING_REBOOT).catch(reason => console.error(reason))
}, pingConfig['reboot_wait'] * 1000)
return false
} catch (e) {
console.error(e)
if (messageCard !== undefined) {
messageCard.hasIpmiError.value = true
}
const result = await this.transition(HostStatus.DOWN)
return result
}
}
default: {
throw new Error(`Unallowed newStatus: ${newStatus}`)
}
}
}
}
class MessageCard {
constructor (host, recurred) {
this.host = host
this.callbackId = uuidv4()
this.messageTs = null
this.recurred = new TimedValue(recurred)
this.status = new TimedValue(HostStatus.DOWN)
this.rebootRequested = new TimedValue(false)
this.rebootRequestedBy = null
this.hasIpmiError = new TimedValue(false)
this.dropToSupervised = false
this.recoverToAutomatic = false
this.consolePreview = null
}
get attachments () {
let text = ''
if (this.recurred.value) {
text += 'This host is still unresponsive.'
} else {
text += `SSH daemon is not responding for last ${pingConfig['loop_period'] * pingConfig['num_trials_before_down']} seconds.`
}
text += ` (${this.recurred.timeFormatting})`
if (!this.host.ipmiHost) {
text += `\n:scream: This host does not support IPMI!`
}
const actions = []
if (this.rebootRequested.value) {
if (this.rebootRequestedBy !== null) {
text += `\n:white_check_mark: <@${this.rebootRequestedBy}> knocks the door!`
} else {
text += `\n:white_check_mark: Rebooting automatically...`
}
text += ` (Sending IPMI reset command)`
text += ` (${this.rebootRequested.timeFormatting})`
} else if (this.status.value === HostStatus.DOWN && this.host.ipmiHost) {
actions.push({
'name': 'reset',
'value': 'reset',
'text': 'Force Reboot',
'type': 'button'
})
}
if (this.hasIpmiError.value) {
text += '\n:x: An error occurred while issuing IPMI command.'
text += ` (${this.hasIpmiError.timeFormatting})`
}
if (this.status.value === HostStatus.WAITING_REBOOT || this.status.value === HostStatus.TESTING_REBOOT) {
text += '\n:arrows_counterclockwise: Checking reachability...'
}
if (this.status.value === HostStatus.NORMAL) {
text += `\n:white_check_mark: She's back!`
text += ` (${this.status.timeFormatting})`
}
if (this.rebootRequested.value && this.status.value === HostStatus.DOWN) {
text += `\n:x: Failed to reboot... sorry about that.`
text += ` (${this.status.timeFormatting})`
}
if (this.dropToSupervised) {
text += `\n:x: Dropping to supervised mode for this host.`
}
if (this.recoverToAutomatic) {
text += `\n:white_check_mark: Recovering to automatic mode for this host.`
}
const attachments = [{
'color': '#2222aa',
'title': `${this.host.hostId} is sleeping`,
'text': text,
'actions': actions,
'callback_id': this.callbackId
}]
if (this.consolePreview !== null) {
attachments.push({
'text': 'Console Preview',
'color': '#555555',
'image_url': `${previewConfig['basepath']}preview/${this.callbackId}/${this.consolePreview.timeStamp.getTime()}/preview.png`,
'ts': Math.floor(this.consolePreview.timeStamp.getTime() / 1000)
})
}
return attachments
}
post () {
if (this.messageTs === null) {
return web.chat.postMessage({ channel: channelId, text: '', attachments: this.attachments })
.then(response => { this.messageTs = response.ts; return response })
} else {
return web.chat.update({ ts: this.messageTs, channel: channelId, text: '', attachments: this.attachments })
}
}
}
class MessageCards {
constructor () {
this.hostIdMap = {}
this.callbackIdMap = {}
}
addByHost (host, recurred) {
const oldCard = this.tryGetByHostId(host.hostId)
if (oldCard !== undefined) {
this.remove(oldCard)
}
const card = new MessageCard(host, recurred)
this.hostIdMap[host.hostId] = card
this.callbackIdMap[card.callbackId] = card
return card
}
remove (card) {
delete this.hostIdMap[card.host.hostId]
delete this.callbackIdMap[card.callbackId]
}
tryGetByCallbackId (callbackId) {
return this.callbackIdMap[callbackId]
}
tryGetByHostId (hostId) {
return this.hostIdMap[hostId]
}
}
async function rebootRequested (callbackId, userId) {
const card = messageCards.tryGetByCallbackId(callbackId)
if (card === undefined) {
await web.chat.postEphemeral({ channel: channelId, text: 'Unknown callbackId\n(It seems that elsabot has suffered a restart. Sorry about that.)', user: userId })
return
}
card.rebootRequested.value = true
card.rebootRequestedBy = userId
await card.host.post(await card.host.transition(HostStatus.WAITING_REBOOT))
}
async function globalHeartbeat () {
while (true) {
const next = Date.now() + pingConfig['loop_period'] * 1000
try {
const promises = []
for (const host of hostList) {
promises.push(host.heartbeat())
}
for (const promise of promises) {
await promise
}
} catch (e) {
console.error(e)
}
const wait = next - Date.now()
if (wait > 0) {
setTimeout(() => globalHeartbeat().catch(reason => console.error(reason)), wait)
break
}
}
}
function system (command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(error)
} else {
resolve(stdout)
}
})
})
}
function sshProbe (host, timeout) {
return new Promise((resolve, reject) => {
const probe = net.createConnection({ port: 22, host: host, timeout: timeout * 1000 }, () => {
probe.end()
resolve()
})
probe.on('error', () => {
probe.destroy()
reject(new Error(`Cannot connect to ssh port (host: ${host})`))
})
probe.on('timeout', () => {
probe.destroy()
reject(new Error(`Timeout occurred while connecting to ssh port (host: ${host})`))
})
})
}
async function consolePreviewUpdate (host) {
const consolePreview = await preview(host.ipmiHost, 'elsabot', host.ipmiPassword)
host.consolePreview = consolePreview
if (host.consolePreviewNeeded) {
consolePreviewUpdate(host).catch(error => console.error(error))
}
}
const config = JSON.parse(readFileSync('config.json', 'utf8'))
const slackConfig = config['slack']
const channelId = slackConfig['channel']
const globalSupervised = config['supervised']
const pingConfig = config['ping']
const previewConfig = config['preview']
const messageCards = new MessageCards()
const hostList = []
const web = new WebClient(slackConfig['token'])
const listener = createMessageAdapter(slackConfig['verification_token'])
for (const hostEntry of config['host']) {
hostList.push(new Host(hostEntry['id'], hostEntry['ping_host'], hostEntry['ipmi_host'], hostEntry['ipmi_password']))
}
listener.action({}, payload => rebootRequested(payload.callback_id, payload.user.id).catch(reason => console.error(reason)))
listener.start(slackConfig['port']).then(() => {
console.log(`Listening on ${slackConfig['port']}`)
})
globalHeartbeat().catch(reason => console.error(reason))
createServer((req, res) => {
web.chat.postMessage({ channel: channelId,
text: 'Hi, there! Elsabot is up and running!',
attachments: [
{
'color': '#2222aa',
'title': 'The following hosts are covered:',
'text': hostList.map(host => `• ${host.hostId} (ping ${host.pingHost}, IPMI ${host.ipmiHost})`).join('\n'),
'mrkdwn': true
},
{
'color': '#2222aa',
'title': `The bot is running in ${globalSupervised ? 'supervised' : 'automatic'} mode`,
'text': globalSupervised ? `I'll ask for confirmation before actually rebooting the host.` : `I'll try automatic reboot whenever I discover an unresponsive host.`
}
]
}).catch(reason => console.error(reason))
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('Sending hello message...\n')
}).listen(config['management_port'], '127.0.0.1', () => console.log(`management port: ${config['management_port']}`))
const previewServer = new Koa()
previewServer.use(koaRoute.get('/preview/:callbackId/:timestamp/preview.png', (ctx, callbackId, timestamp) => {
const card = messageCards.tryGetByCallbackId(callbackId)
if (card === undefined) {
ctx.status = 404
return
}
if (card.consolePreview == null) {
ctx.status = 404
return
}
ctx.type = 'image/png'
ctx.body = card.consolePreview.png
}))
previewServer.listen(previewConfig['port'])
const rtm = new RTMClient(slackConfig['token'])
rtm.start()
exports.system = system