-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlablib.js
94 lines (81 loc) · 2.52 KB
/
lablib.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
#!/bin/node
/*srvctl */
/*
Usage:
Import the function definitions
const lablib = '../../lablib.js';
const msg = require(lablib).msg;
const ntc = require(lablib).ntc;
const err = require(lablib).err;
const get = require(lablib).get;
const run = require(lablib).run;
const rok = require(lablib).rok;
...
Use them
msg("hello world");
*/
const os = require('os');
const HOSTNAME = os.hostname();
const execSync = require('child_process').execSync;
// javascript lablib
const $RED='\x1b[31m';
const $GREEN='\x1b[32m';
const $YELLOW='\x1b[33m';
const $BLUE='\x1b[34m';
const $GRAY='\x1b[37m';
const $CLEAR='\x1b[0m';
const $TAG = $BLUE + '[ ' + HOSTNAME.split('.')[0] + ' ]';
const SC_INSTALL_DIR = process.env.SC_INSTALL_DIR;
exports.msg = function msg() {
console.log($TAG + $GREEN, ...arguments, $CLEAR);
};
exports.ntc = function ntc() {
console.log($YELLOW, ...arguments, $CLEAR);
};
exports.err = function err() {
console.log($RED +'JS-ERROR',...arguments, $CLEAR);
};
exports.get = function get(cmd) {
try {
var result = execSync(cmd,{shell: "/bin/bash"}).toString();
if (result.length > 0) return result;
} catch (e) {
console.log($RED +'JS-ERROR in get: ', e.stderr.toString(), $CLEAR);
}
};
exports.run = function run(cmd) {
try {
console.log($BLUE + '[' + process.env.USER + '@' + HOSTNAME + ' ' + process.env.PWD.split('/')[process.env.PWD.split('/').length -1] +']#' + $YELLOW , cmd, $CLEAR);
var result = execSync(cmd,{shell: "/bin/bash"}).toString();
if (result.length > 0) console.log(result);
} catch (e) {
var stderr = '';
if (e.stderr !== undefined ) stderr = e.stderr.toString();
console.log($RED +'JS-ERROR in run: ' + cmd, stderr, $CLEAR);
}
};
exports.rok = function check(cmd) {
try {
var result = execSync(cmd + ' 2> /dev/null',{shell: "/bin/bash"});
return true;
} catch (e) {
/// exit code
//e.status;
/// stdout
//e.message;
//e.stderr;
return false;
}
};
exports.exec_function = function run(cmd) {
try {
console.log($GREEN , cmd, $CLEAR);
var prefix = SC_INSTALL_DIR + "/srvctl.sh exec-function ";
var result = execSync(prefix + cmd,{shell: "/bin/bash"}).toString();
if (result.length > 0) console.log(result);
} catch (e) {
var stderr = '';
if (e.stderr !== undefined ) stderr = e.stderr.toString();
console.log($RED +'ERROR lablib.js exec_function ' + cmd, e, $CLEAR);
}
};