1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | var debug = require('debug')('pm2:paths');
|
8 | var p = require('path');
|
9 | var fs = require('fs')
|
10 |
|
11 | function getDefaultPM2Home() {
|
12 | var PM2_ROOT_PATH;
|
13 |
|
14 | if (process.env.PM2_HOME)
|
15 | PM2_ROOT_PATH = process.env.PM2_HOME;
|
16 | else if (process.env.HOME && !process.env.HOMEPATH)
|
17 | PM2_ROOT_PATH = p.resolve(process.env.HOME, '.pm2');
|
18 | else if (process.env.HOME || process.env.HOMEPATH)
|
19 | PM2_ROOT_PATH = p.resolve(process.env.HOMEDRIVE, process.env.HOME || process.env.HOMEPATH, '.pm2');
|
20 | else {
|
21 | console.error('[PM2][Initialization] Environment variable HOME (Linux) or HOMEPATH (Windows) are not set!');
|
22 | console.error('[PM2][Initialization] Defaulting to /etc/.pm2');
|
23 | PM2_ROOT_PATH = p.resolve('/etc', '.pm2');
|
24 | }
|
25 |
|
26 | debug('pm2 home resolved to %s', PM2_ROOT_PATH, process.env.HOME);
|
27 | return PM2_ROOT_PATH;
|
28 | }
|
29 |
|
30 | module.exports = function(PM2_HOME) {
|
31 | var has_node_embedded = false
|
32 |
|
33 | if (fs.existsSync(p.resolve(__dirname, './node')) === true) {
|
34 | has_node_embedded = true
|
35 | }
|
36 |
|
37 | if (!PM2_HOME) {
|
38 | PM2_HOME = getDefaultPM2Home()
|
39 | }
|
40 |
|
41 | var pm2_file_stucture = {
|
42 | PM2_HOME : PM2_HOME,
|
43 | PM2_ROOT_PATH : PM2_HOME,
|
44 |
|
45 | PM2_CONF_FILE : p.resolve(PM2_HOME, 'conf.js'),
|
46 | PM2_MODULE_CONF_FILE : p.resolve(PM2_HOME, 'module_conf.json'),
|
47 |
|
48 | PM2_LOG_FILE_PATH : p.resolve(PM2_HOME, 'pm2.log'),
|
49 | PM2_PID_FILE_PATH : p.resolve(PM2_HOME, 'pm2.pid'),
|
50 |
|
51 | PM2_RELOAD_LOCKFILE : p.resolve(PM2_HOME, 'reload.lock'),
|
52 |
|
53 | DEFAULT_PID_PATH : p.resolve(PM2_HOME, 'pids'),
|
54 | DEFAULT_LOG_PATH : p.resolve(PM2_HOME, 'logs'),
|
55 | DEFAULT_MODULE_PATH : p.resolve(PM2_HOME, 'modules'),
|
56 | PM2_IO_ACCESS_TOKEN : p.resolve(PM2_HOME, 'pm2-io-token'),
|
57 | DUMP_FILE_PATH : p.resolve(PM2_HOME, 'dump.pm2'),
|
58 | DUMP_BACKUP_FILE_PATH : p.resolve(PM2_HOME, 'dump.pm2.bak'),
|
59 |
|
60 | DAEMON_RPC_PORT : p.resolve(PM2_HOME, 'rpc.sock'),
|
61 | DAEMON_PUB_PORT : p.resolve(PM2_HOME, 'pub.sock'),
|
62 | INTERACTOR_RPC_PORT : p.resolve(PM2_HOME, 'interactor.sock'),
|
63 |
|
64 | INTERACTOR_LOG_FILE_PATH : p.resolve(PM2_HOME, 'agent.log'),
|
65 | INTERACTOR_PID_PATH : p.resolve(PM2_HOME, 'agent.pid'),
|
66 | INTERACTION_CONF : p.resolve(PM2_HOME, 'agent.json5'),
|
67 |
|
68 | HAS_NODE_EMBEDDED : has_node_embedded,
|
69 | BUILTIN_NODE_PATH : has_node_embedded === true ? p.resolve(__dirname, './node/bin/node') : null,
|
70 | BUILTIN_NPM_PATH : has_node_embedded === true ? p.resolve(__dirname, './node/bin/npm') : null,
|
71 | };
|
72 |
|
73 |
|
74 | var paths = Object.keys(pm2_file_stucture);
|
75 | paths.forEach(function (key) {
|
76 | var envKey = key.indexOf('PM2_') > -1 ? key : 'PM2_' + key;
|
77 | if (process.env[envKey] && key !== 'PM2_HOME' && key !== 'PM2_ROOT_PATH') {
|
78 | pm2_file_stucture[key] = process.env[envKey];
|
79 | }
|
80 | });
|
81 |
|
82 | if (process.platform === 'win32' ||
|
83 | process.platform === 'win64') {
|
84 |
|
85 | pm2_file_stucture.DAEMON_RPC_PORT = '\\\\.\\pipe\\rpc.sock';
|
86 | pm2_file_stucture.DAEMON_PUB_PORT = '\\\\.\\pipe\\pub.sock';
|
87 | pm2_file_stucture.INTERACTOR_RPC_PORT = '\\\\.\\pipe\\interactor.sock';
|
88 | }
|
89 |
|
90 | return pm2_file_stucture;
|
91 | };
|