hola-server 0.8.5 → 0.8.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/bash.js +25 -8
- package/package.json +1 -1
package/core/bash.js
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
1
2
|
const { exec } = require("child_process");
|
|
3
|
+
const { random_code } = require('./random');
|
|
2
4
|
const { is_log_debug, is_log_error, log_debug, log_error } = require('../db/db');
|
|
3
5
|
|
|
4
6
|
const LOG_BASH = "bash";
|
|
5
7
|
|
|
8
|
+
const get_log_file = async () => {
|
|
9
|
+
const home = await run_local_cmd("echo ~");
|
|
10
|
+
const log_dir = `${home}/.hola/ssh`;
|
|
11
|
+
await run_local_cmd(`mkdir -p ${log_dir}`);
|
|
12
|
+
return `${log_dir}/l_${random_code()}.log`;
|
|
13
|
+
}
|
|
14
|
+
|
|
6
15
|
/**
|
|
7
16
|
* Run script and get stdout
|
|
8
17
|
* @param {host info,contains user,ip and password} host
|
|
@@ -10,36 +19,44 @@ const LOG_BASH = "bash";
|
|
|
10
19
|
* @returns
|
|
11
20
|
*/
|
|
12
21
|
const run_script = async (host, script, log_extra) => {
|
|
22
|
+
const log_file = await get_log_file();
|
|
23
|
+
|
|
13
24
|
return new Promise((resolve) => {
|
|
14
|
-
exec(`ssh ${host.auth} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p ${host.port} ${host.user}@${host.ip} /bin/bash <<'EOT' \n ${script} \nEOT\n`, { maxBuffer: 1024 * 150000 }, (error, stdout) => {
|
|
25
|
+
exec(`ssh ${host.auth} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p ${host.port} ${host.user}@${host.ip} /bin/bash <<'EOT' > ${log_file} \n ${script} \nEOT\n`, { maxBuffer: 1024 * 150000 }, (error, stdout) => {
|
|
15
26
|
if (error) {
|
|
16
27
|
if (is_log_error()) {
|
|
17
28
|
log_error(LOG_BASH, "error running on host:" + host.name + " the script:" + script + ",error:" + error, log_extra);
|
|
18
29
|
}
|
|
19
30
|
resolve({ stdout: stdout, err: "error running the script:" + script + ",error:" + error });
|
|
20
31
|
} else {
|
|
32
|
+
const output = fs.readFileSync(log_file, { encoding: 'utf8', flag: 'r' });
|
|
21
33
|
if (is_log_debug()) {
|
|
22
|
-
log_debug(LOG_BASH, "executing on host:" + host.name + ", script:" + script + ",stdout:" +
|
|
34
|
+
log_debug(LOG_BASH, "executing on host:" + host.name + ", script:" + script + ",stdout:" + output, log_extra);
|
|
23
35
|
}
|
|
24
|
-
resolve({ stdout:
|
|
36
|
+
resolve({ stdout: output });
|
|
37
|
+
fs.unlinkSync(log_file);
|
|
25
38
|
}
|
|
26
39
|
});
|
|
27
40
|
});
|
|
28
41
|
};
|
|
29
42
|
|
|
30
43
|
const run_script_file = async (host, script_file, log_extra) => {
|
|
44
|
+
const log_file = await get_log_file();
|
|
45
|
+
|
|
31
46
|
return new Promise((resolve) => {
|
|
32
|
-
exec(`ssh ${host.auth} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p ${host.port} ${host.user}@${host.ip} /bin/bash < ${script_file}`, (error, stdout) => {
|
|
47
|
+
exec(`ssh ${host.auth} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p ${host.port} ${host.user}@${host.ip} /bin/bash < ${script_file} > ${log_file}`, (error, stdout) => {
|
|
33
48
|
if (error) {
|
|
34
49
|
if (is_log_error()) {
|
|
35
50
|
log_error(LOG_BASH, "error running on host:" + host.name + " the script_file:" + script_file + ",error:" + error, log_extra);
|
|
36
51
|
}
|
|
37
52
|
resolve({ stdout: stdout, err: "error running the script:" + script_file + ",error:" + error });
|
|
38
53
|
} else {
|
|
54
|
+
const output = fs.readFileSync(log_file, { encoding: 'utf8', flag: 'r' });
|
|
39
55
|
if (is_log_debug()) {
|
|
40
|
-
log_debug(LOG_BASH, "executing on host:" + host.name + ", script_file:" + script_file + ",stdout:" +
|
|
56
|
+
log_debug(LOG_BASH, "executing on host:" + host.name + ", script_file:" + script_file + ",stdout:" + output, log_extra);
|
|
41
57
|
}
|
|
42
|
-
resolve({ stdout:
|
|
58
|
+
resolve({ stdout: output });
|
|
59
|
+
fs.unlinkSync(log_file);
|
|
43
60
|
}
|
|
44
61
|
});
|
|
45
62
|
});
|
|
@@ -77,7 +94,7 @@ const run_local_cmd = async (cmd, log_extra) => {
|
|
|
77
94
|
*/
|
|
78
95
|
const scp = async (host, remote_file, local_file, log_extra) => {
|
|
79
96
|
return new Promise((resolve) => {
|
|
80
|
-
exec(`
|
|
97
|
+
exec(`scp ${host.auth} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -P ${host.port} -q ${host.user}@${host.ip}:${remote_file} ${local_file}`, (error, stdout) => {
|
|
81
98
|
if (error) {
|
|
82
99
|
if (is_log_error()) {
|
|
83
100
|
log_error(LOG_BASH, "error scp on host:" + host.name + " remote:" + remote_file + ",local:" + local_file + ",error:" + error, log_extra);
|
|
@@ -102,7 +119,7 @@ const scp = async (host, remote_file, local_file, log_extra) => {
|
|
|
102
119
|
*/
|
|
103
120
|
const scpr = async (host, local_file, remote_file, log_extra) => {
|
|
104
121
|
return new Promise((resolve) => {
|
|
105
|
-
exec(`
|
|
122
|
+
exec(`scp ${host.auth} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -P ${host.port} -q ${local_file} ${host.user}@${host.ip}:${remote_file}`, (error, stdout) => {
|
|
106
123
|
if (error) {
|
|
107
124
|
if (is_log_error()) {
|
|
108
125
|
log_error(LOG_BASH, "error scpr on host:" + host.name + " remote:" + remote_file + ",local:" + local_file + ",error:" + error, log_extra);
|