Skip to content

Commit 00dba2c

Browse files
committed
add get_process_env command
1 parent 0f88865 commit 00dba2c

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.debug.js
2+
*.min.js
3+
node_modules/*

.eslintrc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"rules": {
3+
"indent": [
4+
2,
5+
2
6+
],
7+
"quotes": [
8+
2,
9+
"single"
10+
],
11+
"linebreak-style": [
12+
2,
13+
"unix"
14+
],
15+
"semi": [2, "always"],
16+
"strict": [2, "global"],
17+
"curly": 2,
18+
"eqeqeq": 2,
19+
"no-eval": 2,
20+
"guard-for-in": 2,
21+
"no-caller": 2,
22+
"no-else-return": 2,
23+
"no-eq-null": 2,
24+
"no-extend-native": 2,
25+
"no-extra-bind": 2,
26+
"no-floating-decimal": 2,
27+
"no-implied-eval": 2,
28+
"no-labels": 2,
29+
"no-with": 2,
30+
"no-loop-func": 1,
31+
"no-native-reassign": 2,
32+
"no-redeclare": [2, {"builtinGlobals": true}],
33+
"no-delete-var": 2,
34+
"no-shadow-restricted-names": 2,
35+
"no-undef-init": 2,
36+
"no-use-before-define": 2,
37+
"no-unused-vars": [2, {"args": "none"}],
38+
"no-undef": 2,
39+
"callback-return": [2, ["callback", "cb", "next"]],
40+
"global-require": 0,
41+
"no-console": 0,
42+
"require-yield": 0
43+
},
44+
"env": {
45+
"es6": true,
46+
"node": true,
47+
"browser": true
48+
},
49+
"globals": {
50+
"describe": true,
51+
"it": true,
52+
"before": true,
53+
"after": true
54+
},
55+
"parserOptions": {
56+
"ecmaVersion": 8,
57+
"sourceType": "script",
58+
"ecmaFeatures": {
59+
"jsx": true
60+
}
61+
},
62+
"extends": "eslint:recommended"
63+
}

get_process_env

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
export ENABLE_NODE_LOG=NO
4+
5+
DIR=$(cd "$(dirname "$0")"; pwd)
6+
GET_NODE_EXE=$DIR/get_node_exe
7+
NODE_EXE=`$GET_NODE_EXE`
8+
9+
SCRIPT=$DIR/get_process_env.js
10+
11+
#get_process_env pid
12+
$NODE_EXE "$SCRIPT" $1

get_process_env.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
3+
const { execSync } = require('child_process');
4+
5+
const argv = process.argv.slice(2);
6+
7+
const [pid] = argv;
8+
9+
if (!pid) {
10+
console.log('Please pass <pid>');
11+
console.log('Usage:');
12+
console.log(' get_process_env <pid>');
13+
process.exit(1);
14+
}
15+
16+
function getAlinodeVersion(pid) {
17+
const output = execSync(`ls -l /proc/${pid}/exe`, {
18+
encoding: 'utf8'
19+
});
20+
// get execPath from PID
21+
// $ ls -l /proc/10532/exe
22+
// lrwxrwxrwx 1 puling.tyq users 0 Mar 8 16:54 /proc/10532/exe -> /home/puling.tyq/.tnvm/versions/node/v8.6.0/bin/node
23+
let [, execPath] = output.split('->');
24+
execPath = execPath.trim();
25+
if (!execPath.endsWith('bin/node')) {
26+
return '';
27+
}
28+
29+
const versionOutput = execSync(`${execPath} -p "process.alinode"`, {
30+
encoding: 'utf8'
31+
});
32+
33+
return versionOutput.trim();
34+
}
35+
36+
function getProcessEnv(pid) {
37+
const output = execSync(`cat /proc/${pid}/environ`, {
38+
encoding: 'utf8'
39+
});
40+
const lines = output.split('\u0000');
41+
42+
var env = {ENABLE_NODE_LOG: '', NODE_LOG_DIR: '/tmp'};
43+
44+
for (var i = 0; i < lines.length; i++) {
45+
const line = lines[i];
46+
if (line.startsWith('ENABLE_NODE_LOG')) {
47+
env.ENABLE_NODE_LOG = line.split('=')[1];
48+
}
49+
50+
if (line.startsWith('NODE_LOG_DIR')) {
51+
env.NODE_LOG_DIR = line.split('=')[1];
52+
}
53+
}
54+
55+
if (!env.NODE_LOG_DIR.endsWith('/')) {
56+
env.NODE_LOG_DIR += '/';
57+
}
58+
59+
return env;
60+
}
61+
62+
var env = getProcessEnv(pid);
63+
64+
const logdir = process.env.logdir;
65+
66+
var data = {
67+
alinode_version: getAlinodeVersion(pid),
68+
ENABLE_NODE_LOG: env.ENABLE_NODE_LOG,
69+
NODE_LOG_DIR: env.NODE_LOG_DIR,
70+
logdir: logdir
71+
};
72+
73+
console.log(JSON.stringify(data));

0 commit comments

Comments
 (0)