-
Notifications
You must be signed in to change notification settings - Fork 6
/
sshServer.js
59 lines (54 loc) · 1.92 KB
/
sshServer.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
const execFn = require('./execFn.js')
const tinyAsyncPool = require('tiny-async-pool')
const commandNum = 4
async function asyncPoolAll(...args) {
const results = [];
for await (const result of tinyAsyncPool(...args)) {
results.push(result);
}
return results;
}
const normal = '正常'
module.exports = (config, conn) => {
return new Promise((resolve, reject) => {
const exec = execFn(conn)
conn.on('ready', async (err) => {
if (err) reject(err)
console.log('连接成功');
const before = `echo "${config.password}" | sudo -S `
const rol = new Array(commandNum).fill(normal)
rol[0] = config.host
exec(before + 'systemctl status docker').then((content) => {
const isRun = String(content).includes('active (running)')
if (!isRun) {
rol[1] = '异常'
}
})
// FIXME:该命令结果有问题,结果均返回空
exec(before + 'docker info |grep -A 5 "WARNING"').then((content) => {
if (content) {
rol[2] = '异常'
}
})
const result = await exec(before + 'docker ps -a -q')
const dockerIds = result.split('\n').filter(r => r)
// 控制进程数为9,超出进程数报错
await asyncPoolAll(3, dockerIds, async (id) => {
const data = await exec(before + `docker logs --tail 200 ${id}`)
if (data.includes('error')) {
if (rol[3] === normal) {
rol[3] = ''
}
rol[3] += `${id}异常;`
return true
}
return false
});
conn.end()
resolve(rol)
}).connect({
...config,
readyTimeout: 5000
});
})
}