hola-server 0.8.3 → 0.8.5

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 +52 -27
  2. package/package.json +2 -2
package/core/bash.js CHANGED
@@ -9,17 +9,17 @@ const LOG_BASH = "bash";
9
9
  * @param {commands to run} script
10
10
  * @returns
11
11
  */
12
- const run_script = async (host, script) => {
12
+ const run_script = async (host, script, log_extra) => {
13
13
  return new Promise((resolve) => {
14
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) => {
15
15
  if (error) {
16
16
  if (is_log_error()) {
17
- log_error(LOG_BASH, "error running on host:" + host.name + " the script:" + script + ",error:" + error);
17
+ log_error(LOG_BASH, "error running on host:" + host.name + " the script:" + script + ",error:" + error, log_extra);
18
18
  }
19
- resolve({ err: "error running the script:" + script + ",error:" + error });
19
+ resolve({ stdout: stdout, err: "error running the script:" + script + ",error:" + error });
20
20
  } else {
21
21
  if (is_log_debug()) {
22
- log_debug(LOG_BASH, "executing on host:" + host.name + ", script:" + script + ",stdout:" + stdout);
22
+ log_debug(LOG_BASH, "executing on host:" + host.name + ", script:" + script + ",stdout:" + stdout, log_extra);
23
23
  }
24
24
  resolve({ stdout: stdout });
25
25
  }
@@ -27,17 +27,17 @@ const run_script = async (host, script) => {
27
27
  });
28
28
  };
29
29
 
30
- const run_script_file = async (host, script_file) => {
30
+ const run_script_file = async (host, script_file, log_extra) => {
31
31
  return new Promise((resolve) => {
32
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) => {
33
33
  if (error) {
34
34
  if (is_log_error()) {
35
- log_error(LOG_BASH, "error running on host:" + host.name + " the script_file:" + script_file + ",error:" + error);
35
+ log_error(LOG_BASH, "error running on host:" + host.name + " the script_file:" + script_file + ",error:" + error, log_extra);
36
36
  }
37
- resolve({ err: "error running the script:" + script_file + ",error:" + error });
37
+ resolve({ stdout: stdout, err: "error running the script:" + script_file + ",error:" + error });
38
38
  } else {
39
39
  if (is_log_debug()) {
40
- log_debug(LOG_BASH, "executing on host:" + host.name + ", script_file:" + script_file + ",stdout:" + stdout);
40
+ log_debug(LOG_BASH, "executing on host:" + host.name + ", script_file:" + script_file + ",stdout:" + stdout, log_extra);
41
41
  }
42
42
  resolve({ stdout: stdout });
43
43
  }
@@ -57,7 +57,7 @@ const run_local_cmd = async (cmd, log_extra) => {
57
57
  if (is_log_error()) {
58
58
  log_error(LOG_BASH, "error running on local host with cmd:" + cmd + ",error:" + error, log_extra);
59
59
  }
60
- resolve({ err: "error running the cmd:" + cmd + ",error:" + error }, log_extra);
60
+ resolve({ stdout: stdout, err: "error running the cmd:" + cmd + ",error:" + error }, log_extra);
61
61
  } else {
62
62
  if (is_log_debug()) {
63
63
  log_debug(LOG_BASH, "executing on local host with cmd:" + cmd + ",stdout:" + stdout, log_extra);
@@ -75,17 +75,42 @@ const run_local_cmd = async (cmd, log_extra) => {
75
75
  * @param {local file path} locale_file
76
76
  * @returns
77
77
  */
78
- const scp = async (host, remote_file, local_file) => {
78
+ const scp = async (host, remote_file, local_file, log_extra) => {
79
79
  return new Promise((resolve) => {
80
- 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) => {
80
+ exec(`sshpass -p '${host.pwd}' scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -P ${host.port} -q ${host.user}@${host.ip}:${remote_file} ${local_file}`, (error, stdout) => {
81
81
  if (error) {
82
82
  if (is_log_error()) {
83
- log_error(LOG_BASH, "error scp on host:" + host.name + " remote:" + remote_file + ",local:" + local_file + ",error:" + error);
83
+ log_error(LOG_BASH, "error scp on host:" + host.name + " remote:" + remote_file + ",local:" + local_file + ",error:" + error, log_extra);
84
84
  }
85
- resolve({ err: "error scp:" + remote_file + " to locale:" + local_file + ",err:" + error });
85
+ resolve({ stdout: stdout, err: "error scp:" + remote_file + " to locale:" + local_file + ",err:" + error });
86
86
  } else {
87
87
  if (is_log_debug()) {
88
- log_debug(LOG_BASH, "executing scp on host:" + host.name + ", remote:" + remote_file + ",local:" + local_file + ",stdout:" + stdout);
88
+ log_debug(LOG_BASH, "executing scp on host:" + host.name + ", remote:" + remote_file + ",local:" + local_file + ",stdout:" + stdout, log_extra);
89
+ }
90
+ resolve({ stdout: stdout });
91
+ }
92
+ });
93
+ });
94
+ };
95
+
96
+ /**
97
+ * Scp local file to remote file
98
+ * @param {remote host} host
99
+ * @param {local file path} locale_file
100
+ * @param {remote file path} remote_file
101
+ * @returns
102
+ */
103
+ const scpr = async (host, local_file, remote_file, log_extra) => {
104
+ return new Promise((resolve) => {
105
+ exec(`sshpass -p '${host.pwd}' scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -P ${host.port} -q ${local_file} ${host.user}@${host.ip}:${remote_file}`, (error, stdout) => {
106
+ if (error) {
107
+ if (is_log_error()) {
108
+ log_error(LOG_BASH, "error scpr on host:" + host.name + " remote:" + remote_file + ",local:" + local_file + ",error:" + error, log_extra);
109
+ }
110
+ resolve({ stdout: stdout, err: "error scp:" + remote_file + " to locale:" + local_file + ",err:" + error });
111
+ } else {
112
+ if (is_log_debug()) {
113
+ log_debug(LOG_BASH, "executing scpr on host:" + host.name + ", remote:" + remote_file + ",local:" + local_file + ",stdout:" + stdout, log_extra);
89
114
  }
90
115
  resolve({ stdout: stdout });
91
116
  }
@@ -99,8 +124,8 @@ const scp = async (host, remote_file, local_file) => {
99
124
  * @param {command to run} cmd
100
125
  * @returns
101
126
  */
102
- const run_simple_cmd = async (host, cmd) => {
103
- const { err, stdout } = await run_script(host, cmd);
127
+ const run_simple_cmd = async (host, cmd, log_extra) => {
128
+ const { err, stdout } = await run_script(host, cmd, log_extra);
104
129
  if (err) {
105
130
  return null;
106
131
  } else {
@@ -113,8 +138,8 @@ const run_simple_cmd = async (host, cmd) => {
113
138
  * @param {command to run} cmd
114
139
  * @returns
115
140
  */
116
- const run_simple_local_cmd = async (cmd) => {
117
- const { err, stdout } = await run_local_cmd(cmd);
141
+ const run_simple_local_cmd = async (cmd, log_extra) => {
142
+ const { err, stdout } = await run_local_cmd(cmd, log_extra);
118
143
  if (err) {
119
144
  return null;
120
145
  } else {
@@ -139,11 +164,11 @@ const run_simple_local_cmd = async (cmd) => {
139
164
  * @param {key to retrieve info} key
140
165
  * @returns
141
166
  */
142
- const get_info = (stdout, key) => {
167
+ const get_info = (stdout, key, log_extra) => {
143
168
  const word_key = key.split(" ").join("\\s+");
144
169
  const regex = new RegExp(`\n\\s?${word_key}\\s?:(.*)\\s`, 'g');
145
170
  if (is_log_debug()) {
146
- log_debug(LOG_BASH, "get_info and regex:" + JSON.stringify(regex));
171
+ log_debug(LOG_BASH, "get_info and regex:" + JSON.stringify(regex), log_extra);
147
172
  }
148
173
 
149
174
  const results = stdout.matchAll(regex);
@@ -154,7 +179,7 @@ const get_info = (stdout, key) => {
154
179
  }
155
180
 
156
181
  if (is_log_debug()) {
157
- log_debug(LOG_BASH, "get_info and matched:" + JSON.stringify(matched));
182
+ log_debug(LOG_BASH, "get_info and matched:" + JSON.stringify(matched), log_extra);
158
183
  }
159
184
  return matched;
160
185
  }
@@ -260,24 +285,24 @@ const read_obj_line = (stdout, keys, ignore = 1, delimiter = " ") => {
260
285
  * @param {host info,contains user,ip and password} host
261
286
  * @param {*} attrs {name:"attr name",cmd:"command to get the attr"}
262
287
  */
263
- const get_system_attributes = async (host, attrs) => {
288
+ const get_system_attributes = async (host, attrs, log_extra) => {
264
289
  const obj = {};
265
290
  for (let i = 0; i < attrs.length; i++) {
266
291
  const attr = attrs[i];
267
- const value = await run_simple_cmd(host, attr.cmd);
292
+ const value = await run_simple_cmd(host, attr.cmd, log_extra);
268
293
  value && (obj[attr.name] = value);
269
294
  }
270
295
  return obj;
271
296
  }
272
297
 
273
- const stop_process = async (host, process_name, stop_cmd, using_full) => {
298
+ const stop_process = async (host, process_name, stop_cmd, using_full, log_extra) => {
274
299
  const grep_cmd = using_full == true ? `pgrep -f "${process_name}" | wc -l` : `pgrep ${process_name} | wc -l`;
275
- const { stdout } = await run_script(host, grep_cmd);
300
+ const { stdout } = await run_script(host, grep_cmd, log_extra);
276
301
  const has_process = stdout && parseInt(stdout) > 0;
277
302
  if (has_process) {
278
- await run_script(host, stop_cmd);
303
+ await run_script(host, stop_cmd, log_extra);
279
304
  }
280
305
  return has_process;
281
306
  }
282
307
 
283
- module.exports = { stop_process, scp, run_script, run_script_file, run_simple_cmd, run_local_cmd, run_simple_local_cmd, get_info, get_system_attributes, read_key_value_line, read_obj_line };
308
+ module.exports = { stop_process, scp, scpr, run_script, run_script_file, run_simple_cmd, run_local_cmd, run_simple_local_cmd, get_info, get_system_attributes, read_key_value_line, read_obj_line };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hola-server",
3
- "version": "0.8.3",
3
+ "version": "0.8.5",
4
4
  "description": "a meta programming framework used to build nodejs restful api",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -43,4 +43,4 @@
43
43
  "url": "https://github.com/hery-node/hola-server/issues"
44
44
  },
45
45
  "homepage": "https://github.com/hery-node/hola-server#readme"
46
- }
46
+ }