hola-server 0.8.6 → 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.
Files changed (2) hide show
  1. package/core/bash.js +23 -6
  2. 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:" + stdout, log_extra);
34
+ log_debug(LOG_BASH, "executing on host:" + host.name + ", script:" + script + ",stdout:" + output, log_extra);
23
35
  }
24
- resolve({ stdout: 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:" + stdout, log_extra);
56
+ log_debug(LOG_BASH, "executing on host:" + host.name + ", script_file:" + script_file + ",stdout:" + output, log_extra);
41
57
  }
42
- resolve({ stdout: stdout });
58
+ resolve({ stdout: output });
59
+ fs.unlinkSync(log_file);
43
60
  }
44
61
  });
45
62
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hola-server",
3
- "version": "0.8.6",
3
+ "version": "0.8.7",
4
4
  "description": "a meta programming framework used to build nodejs restful api",
5
5
  "main": "index.js",
6
6
  "scripts": {