-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathsystem.js
87 lines (79 loc) · 2.75 KB
/
system.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
const osName = require('os-name');
const utils = require('../utils');
const os = require('os');
module.exports = {
getContainerInfo: () => {
utils.log('trace', 'getContainerInfo');
if (utils.isLinux)
return Promise.all([utils.fileExists('/.dockerenv'), utils.readFile('/proc/self/cgroup')])
.then(results => {
utils.log('trace', 'getContainerInfoThen', results);
return Promise.resolve(['Container', results[0] || results[1] ? 'Yes' : 'N/A']);
})
.catch(err => utils.log('trace', 'getContainerInfoCatch', err));
return Promise.resolve(['Container', 'N/A']);
},
getCPUInfo: () => {
utils.log('trace', 'getCPUInfo');
let info;
try {
const cpus = os.cpus();
info = '(' + cpus.length + ') ' + os.arch() + ' ' + cpus[0].model;
} catch (err) {
info = 'Unknown';
}
return Promise.all(['CPU', info]);
},
getMemoryInfo: () => {
utils.log('trace', 'getMemoryInfo');
return Promise.all([
'Memory',
`${utils.toReadableBytes(os.freemem())} / ${utils.toReadableBytes(os.totalmem())}`,
]);
},
getOSInfo: () => {
utils.log('trace', 'getOSInfo');
let version;
if (utils.isMacOS) {
version = utils.run('sw_vers -productVersion ');
} else if (utils.isLinux) {
version = utils.run('cat /etc/os-release').then(v => {
const distro = (v || '').match(/NAME="(.+)"/) || '';
const versionInfo = (v || '').match(/VERSION="(.+)"/) || ['', ''];
const versionStr = versionInfo !== null ? versionInfo[1] : '';
return `${distro[1]} ${versionStr}`.trim() || '';
});
} else if (utils.isWindows) {
version = Promise.resolve(os.release());
} else {
version = Promise.resolve();
}
return version.then(v => {
let info = osName(os.platform(), os.release());
if (v) info += ` ${v}`;
return ['OS', info];
});
},
getShellInfo: () => {
utils.log('trace', 'getShellInfo', process.env);
if (utils.isMacOS || utils.isLinux) {
const shell =
process.env.SHELL || utils.runSync('getent passwd $LOGNAME | cut -d: -f7 | head -1');
let command = `${shell} --version 2>&1`;
if (shell.match('/bin/ash')) command = `${shell} --help 2>&1`;
return Promise.all([utils.run(command).then(utils.findVersion), utils.which(shell)]).then(v =>
utils.determineFound('Shell', v[0] || 'Unknown', v[1])
);
}
return Promise.resolve(['Shell', 'N/A']);
},
getGLibcInfo: () => {
utils.log('trace', 'getGLibc');
if (utils.isLinux) {
return Promise.all([utils.run(`ldd --version`).then(utils.findVersion)]).then(v =>
utils.determineFound('GLibc', v[0] || 'Unknown')
);
}
return Promise.resolve(['GLibc', 'N/A']);
},
};