1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var fs = require('fs');
|
12 | var path = require('path');
|
13 | var os = require('os');
|
14 | var util = require('util');
|
15 | var chalk = require('chalk');
|
16 | var fclone = require('fclone');
|
17 | var semver = require('semver');
|
18 | var moment = require('moment');
|
19 | var isBinary = require('./tools/isbinaryfile.js');
|
20 | var cst = require('../constants.js');
|
21 | var extItps = require('./API/interpreter.json');
|
22 | var Config = require('./tools/Config');
|
23 | var pkg = require('../package.json');
|
24 |
|
25 | var Common = module.exports;
|
26 |
|
27 | function homedir() {
|
28 | var env = process.env;
|
29 | var home = env.HOME;
|
30 | var user = env.LOGNAME || env.USER || env.LNAME || env.USERNAME;
|
31 |
|
32 | if (process.platform === 'win32') {
|
33 | return env.USERPROFILE || env.HOMEDRIVE + env.HOMEPATH || home || null;
|
34 | }
|
35 |
|
36 | if (process.platform === 'darwin') {
|
37 | return home || (user ? '/Users/' + user : null);
|
38 | }
|
39 |
|
40 | if (process.platform === 'linux') {
|
41 | return home || (process.getuid() === 0 ? '/root' : (user ? '/home/' + user : null));
|
42 | }
|
43 |
|
44 | return home || null;
|
45 | }
|
46 |
|
47 | function resolveHome(filepath) {
|
48 | if (filepath[0] === '~') {
|
49 | return path.join(homedir(), filepath.slice(1));
|
50 | }
|
51 | return filepath;
|
52 | }
|
53 |
|
54 | Common.determineSilentCLI = function() {
|
55 |
|
56 | var variadicArgsDashesPos = process.argv.indexOf('--');
|
57 | var s1opt = process.argv.indexOf('--silent')
|
58 | var s2opt = process.argv.indexOf('-s')
|
59 |
|
60 | if (process.env.PM2_SILENT || (variadicArgsDashesPos > -1 &&
|
61 | (s1opt != -1 && s1opt < variadicArgsDashesPos) &&
|
62 | (s2opt != -1 != s2opt < variadicArgsDashesPos)) ||
|
63 | (variadicArgsDashesPos == -1 && (s1opt > -1 || s2opt > -1))) {
|
64 | for (var key in console){
|
65 | var code = key.charCodeAt(0);
|
66 | if (code >= 97 && code <= 122){
|
67 | console[key] = function(){};
|
68 | }
|
69 | }
|
70 | process.env.PM2_DISCRETE_MODE = true;
|
71 | }
|
72 | }
|
73 |
|
74 | Common.printVersion = function() {
|
75 | var variadicArgsDashesPos = process.argv.indexOf('--');
|
76 |
|
77 | if (process.argv.indexOf('-v') > -1 && process.argv.indexOf('-v') < variadicArgsDashesPos) {
|
78 | console.log(pkg.version);
|
79 | process.exit(0);
|
80 | }
|
81 | }
|
82 |
|
83 | Common.lockReload = function() {
|
84 | try {
|
85 | var t1 = fs.readFileSync(cst.PM2_RELOAD_LOCKFILE).toString();
|
86 |
|
87 |
|
88 |
|
89 | if (t1 && t1 != '') {
|
90 | var diff = moment().diff(parseInt(t1));
|
91 | if (diff < cst.RELOAD_LOCK_TIMEOUT)
|
92 | return diff;
|
93 | }
|
94 | } catch(e) {}
|
95 |
|
96 | try {
|
97 |
|
98 | fs.writeFileSync(cst.PM2_RELOAD_LOCKFILE, moment.now());
|
99 | return 0;
|
100 | } catch(e) {
|
101 | console.error(e.message || e);
|
102 | }
|
103 | };
|
104 |
|
105 | Common.unlockReload = function() {
|
106 | try {
|
107 | fs.writeFileSync(cst.PM2_RELOAD_LOCKFILE, '');
|
108 | } catch(e) {
|
109 | console.error(e.message || e);
|
110 | }
|
111 | };
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | Common.prepareAppConf = function(opts, app) {
|
122 | |
123 |
|
124 |
|
125 | if (!app.script)
|
126 | return new Error('No script path - aborting');
|
127 |
|
128 | var cwd = null;
|
129 |
|
130 | if (app.cwd) {
|
131 | cwd = path.resolve(app.cwd);
|
132 | process.env.PWD = app.cwd;
|
133 | }
|
134 |
|
135 | if (!app.node_args) {
|
136 | app.node_args = [];
|
137 | }
|
138 |
|
139 | if (app.port && app.env) {
|
140 | app.env.PORT = app.port;
|
141 | }
|
142 |
|
143 |
|
144 | cwd && (cwd[0] != '/') && (cwd = path.resolve(process.cwd(), cwd));
|
145 | cwd = cwd || opts.cwd;
|
146 |
|
147 |
|
148 | app.pm_exec_path = path.resolve(cwd, app.script);
|
149 |
|
150 |
|
151 | if (!fs.existsSync(app.pm_exec_path)) {
|
152 | var ckd;
|
153 |
|
154 | if ((ckd = require('shelljs').which(app.script))) {
|
155 | if (typeof(ckd) !== 'string')
|
156 | ckd = ckd.toString();
|
157 | app.pm_exec_path = ckd;
|
158 | }
|
159 | else
|
160 |
|
161 | return new Error('script not found : ' + app.pm_exec_path);
|
162 | }
|
163 |
|
164 | |
165 |
|
166 |
|
167 | if (app.disable_source_map_support != true) {
|
168 | try {
|
169 | if (fs.accessSync) {
|
170 | fs.accessSync(app.pm_exec_path + '.map', fs.R_OK);
|
171 | app.source_map_support = true;
|
172 | }
|
173 | else {
|
174 |
|
175 | if (fs.existsSync(app.pm_exec_path + '.map')) {
|
176 | app.source_map_support = true;
|
177 | }
|
178 | }
|
179 | } catch(e) {}
|
180 | delete app.disable_source_map_support;
|
181 | }
|
182 |
|
183 | delete app.script;
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | var env = {};
|
189 |
|
190 | |
191 |
|
192 |
|
193 |
|
194 | if (cst.PM2_PROGRAMMATIC)
|
195 | Common.safeExtend(env, process.env);
|
196 | else
|
197 | env = process.env;
|
198 |
|
199 |
|
200 | app.env = [{}, env, app.env || {}].reduce(function(e1, e2){
|
201 | return util._extend(e1, e2);
|
202 | });
|
203 |
|
204 | app.pm_cwd = cwd;
|
205 |
|
206 | Common.sink.resolveInterpreter(app);
|
207 |
|
208 |
|
209 | Common.sink.determineExecMode(app);
|
210 |
|
211 | |
212 |
|
213 |
|
214 | var formated_app_name = app.name.replace(/[^a-zA-Z0-9\\.\\-]/g, '-');
|
215 |
|
216 | ['log', 'out', 'error', 'pid'].forEach(function(f){
|
217 | var af = app[f + '_file'], ps, ext = (f == 'pid' ? 'pid':'log'), isStd = !~['log', 'pid'].indexOf(f);
|
218 | if (af) af = resolveHome(af);
|
219 |
|
220 | if ((f == 'log' && typeof af == 'boolean' && af) || (f != 'log' && !af)) {
|
221 | ps = [cst['DEFAULT_' + ext.toUpperCase() + '_PATH'], formated_app_name + (isStd ? '-' + f : '') + '.' + ext];
|
222 | } else if (f != 'log' || (f == 'log' && af)) {
|
223 | ps = [cwd, af];
|
224 |
|
225 | var dir = path.dirname(path.resolve(cwd, af));
|
226 | if (!fs.existsSync(dir)) {
|
227 | Common.printError(cst.PREFIX_MSG_WARNING + 'Folder does not exist: ' + dir);
|
228 | Common.printOut(cst.PREFIX_MSG + 'Creating folder: ' + dir);
|
229 | require('mkdirp')(dir, function(err) {
|
230 | if (!err) return;
|
231 | Common.printError(cst.PREFIX_MSG_ERR + 'Could not create folder: ' + path.dirname(af));
|
232 | throw new Error('Could not create folder');
|
233 | });
|
234 | }
|
235 |
|
236 | }
|
237 |
|
238 | ps && (app['pm_' + (isStd ? f.substr(0, 3) + '_' : '') + ext + '_path'] = path.resolve.apply(null, ps));
|
239 | delete app[f + '_file'];
|
240 | });
|
241 |
|
242 | return app;
|
243 | };
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | Common.isConfigFile = function (filename) {
|
251 | if (typeof (filename) !== 'string')
|
252 | return null;
|
253 | if (filename.indexOf('.json') !== -1)
|
254 | return 'json';
|
255 | if (filename.indexOf('.yml') > -1 || filename.indexOf('.yaml') > -1)
|
256 | return 'yaml';
|
257 | if (filename.indexOf('.config.js') !== -1)
|
258 | return 'js';
|
259 | if (filename.indexOf('.config.mjs') !== -1)
|
260 | return 'mjs';
|
261 | return null;
|
262 | };
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 | Common.parseConfig = function(confObj, filename) {
|
271 | var yamljs = require('yamljs');
|
272 | var vm = require('vm');
|
273 |
|
274 | if (!filename ||
|
275 | filename == 'pipe' ||
|
276 | filename == 'none' ||
|
277 | filename.indexOf('.json') > -1) {
|
278 | var code = '(' + confObj + ')';
|
279 | var sandbox = {};
|
280 |
|
281 | return vm.runInThisContext(code, sandbox, {
|
282 | filename: path.resolve(filename),
|
283 | displayErrors: false,
|
284 | timeout: 1000
|
285 | });
|
286 | }
|
287 | else if (filename.indexOf('.yml') > -1 ||
|
288 | filename.indexOf('.yaml') > -1) {
|
289 | return yamljs.parse(confObj.toString());
|
290 | }
|
291 | else if (filename.indexOf('.config.js') > -1 || filename.indexOf('.config.mjs') > -1) {
|
292 | var confPath = require.resolve(path.resolve(filename));
|
293 | delete require.cache[confPath];
|
294 | return require(confPath);
|
295 | }
|
296 | };
|
297 |
|
298 | Common.retErr = function(e) {
|
299 | if (!e)
|
300 | return new Error('Unidentified error');
|
301 | if (e instanceof Error)
|
302 | return e;
|
303 | return new Error(e);
|
304 | }
|
305 |
|
306 | Common.sink = {};
|
307 |
|
308 | Common.sink.determineCron = function(app) {
|
309 | var cronJob = require('cron').CronJob;
|
310 |
|
311 | if (app.cron_restart) {
|
312 | try {
|
313 | Common.printOut(cst.PREFIX_MSG + 'cron restart at ' + app.cron_restart);
|
314 | new cronJob(app.cron_restart, function() {
|
315 | Common.printOut(cst.PREFIX_MSG + 'cron pattern for auto restart detected and valid');
|
316 | });
|
317 | } catch(ex) {
|
318 | return new Error('Cron pattern is not valid, trace: ' + ex.stack);
|
319 | }
|
320 | }
|
321 | };
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | Common.sink.determineExecMode = function(app) {
|
327 | if (typeof app.instances == 'undefined')
|
328 | app.instances = 1;
|
329 | if (app.exec_mode)
|
330 | app.exec_mode = app.exec_mode.replace(/^(fork|cluster)$/, '$1_mode');
|
331 |
|
332 | |
333 |
|
334 |
|
335 | if (!app.exec_mode &&
|
336 | (app.instances > 1 || app.instances === 0 || app.instances === -1) &&
|
337 | app.exec_interpreter.indexOf('node') > -1) {
|
338 | app.exec_mode = 'cluster_mode';
|
339 | } else if (!app.exec_mode) {
|
340 | app.exec_mode = 'fork_mode';
|
341 | }
|
342 |
|
343 | };
|
344 |
|
345 | var resolveNodeInterpreter = function(app) {
|
346 | if (app.exec_mode && app.exec_mode.indexOf('cluster') > -1) {
|
347 | Common.printError(cst.PREFIX_MSG_WARNING + chalk.bold.yellow('Choosing the Node.js version in cluster mode is not supported'));
|
348 | return false;
|
349 | }
|
350 |
|
351 | var nvm_path = cst.IS_WINDOWS ? process.env.NVM_HOME : process.env.NVM_DIR;
|
352 | if (!nvm_path) {
|
353 | Common.printError(cst.PREFIX_MSG_ERR + chalk.red('NVM is not available in PATH'));
|
354 | Common.printError(cst.PREFIX_MSG_ERR + chalk.red('Fallback to node in PATH'));
|
355 | var msg = cst.IS_WINDOWS
|
356 | ? 'https://github.com/coreybutler/nvm-windows/releases/'
|
357 | : '$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash';
|
358 | Common.printOut(cst.PREFIX_MSG_ERR + chalk.bold('Install NVM:\n' + msg));
|
359 | }
|
360 | else {
|
361 | var node_version = app.exec_interpreter.split('@')[1];
|
362 | var path_to_node = cst.IS_WINDOWS
|
363 | ? '/v' + node_version + '/node.exe'
|
364 | : semver.satisfies(node_version, '>= 0.12.0')
|
365 | ? '/versions/node/v' + node_version + '/bin/node'
|
366 | : '/v' + node_version + '/bin/node';
|
367 | var nvm_node_path = path.join(nvm_path, path_to_node);
|
368 | try {
|
369 | fs.accessSync(nvm_node_path);
|
370 | } catch(e) {
|
371 | Common.printOut(cst.PREFIX_MSG + 'Installing Node v%s', node_version);
|
372 | var nvm_bin = path.join(nvm_path, 'nvm.' + (cst.IS_WINDOWS ? 'exe' : 'sh'));
|
373 | var nvm_cmd = cst.IS_WINDOWS
|
374 | ? nvm_bin + ' install ' + node_version
|
375 | : '. ' + nvm_bin + ' ; nvm install ' + node_version;
|
376 |
|
377 | Common.printOut(cst.PREFIX_MSG + 'Executing: %s', nvm_cmd);
|
378 | require('shelljs').exec(nvm_cmd);
|
379 |
|
380 |
|
381 |
|
382 |
|
383 | if (cst.IS_WINDOWS)
|
384 | nvm_node_path = nvm_node_path.replace(/node/, 'node' + process.arch.slice(1))
|
385 | }
|
386 |
|
387 | Common.printOut(cst.PREFIX_MSG + chalk.green.bold('Setting Node to v%s (path=%s)'),
|
388 | node_version,
|
389 | nvm_node_path);
|
390 |
|
391 | app.exec_interpreter = nvm_node_path;
|
392 | }
|
393 | };
|
394 |
|
395 |
|
396 |
|
397 |
|
398 | Common.sink.resolveInterpreter = function(app) {
|
399 | var noInterpreter = !app.exec_interpreter;
|
400 | var extName = path.extname(app.pm_exec_path);
|
401 | var betterInterpreter = extItps[extName];
|
402 |
|
403 |
|
404 | if (noInterpreter && betterInterpreter) {
|
405 | app.exec_interpreter = betterInterpreter;
|
406 | }
|
407 |
|
408 | else if (noInterpreter)
|
409 | app.exec_interpreter = isBinary(app.pm_exec_path) ? 'none' : 'node';
|
410 | else if (app.exec_interpreter.indexOf('node@') > -1)
|
411 | resolveNodeInterpreter(app);
|
412 |
|
413 | if (app.exec_interpreter.indexOf('python') > -1)
|
414 | app.env.PYTHONUNBUFFERED = '1'
|
415 |
|
416 | |
417 |
|
418 |
|
419 | if (app.exec_interpreter == 'ts-node') {
|
420 | app.exec_interpreter = path.resolve(__dirname, '../node_modules/.bin/ts-node');
|
421 | }
|
422 |
|
423 | if (app.exec_interpreter == 'lsc') {
|
424 | app.exec_interpreter = path.resolve(__dirname, '../node_modules/.bin/lsc');
|
425 | }
|
426 |
|
427 | if (app.exec_interpreter == 'coffee') {
|
428 | app.exec_interpreter = path.resolve(__dirname, '../node_modules/.bin/coffee');
|
429 | }
|
430 |
|
431 | if (app.exec_interpreter != 'none' && require('shelljs').which(app.exec_interpreter) == null) {
|
432 | Common.printError(cst.PREFIX_MSG_ERR + 'Interpreter ' + app.exec_interpreter + ' does not seem to be available');
|
433 | }
|
434 | return app;
|
435 | };
|
436 |
|
437 | Common.deepCopy = Common.serialize = Common.clone = function(obj) {
|
438 | if (obj === null || obj === undefined) return {};
|
439 | return fclone(obj);
|
440 | };
|
441 |
|
442 | Common.errMod = function(msg) {
|
443 | if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
444 | if (msg instanceof Error)
|
445 | return console.error(msg.message);
|
446 | return console.error(`${cst.PREFIX_MSG_MOD_ERR}${msg}`);
|
447 | }
|
448 |
|
449 | Common.err = function(msg) {
|
450 | if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
451 | if (msg instanceof Error)
|
452 | return console.error(msg.message);
|
453 | return console.error(`${cst.PREFIX_MSG_ERR}${msg}`);
|
454 | }
|
455 |
|
456 | Common.printError = function(msg) {
|
457 | if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
458 | if (msg instanceof Error)
|
459 | return console.error(msg.message);
|
460 | return console.error.apply(console, arguments);
|
461 | };
|
462 |
|
463 | Common.log = function(msg) {
|
464 | if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
465 | return console.log(`${cst.PREFIX_MSG}${msg}`);
|
466 | }
|
467 |
|
468 | Common.warn = function(msg) {
|
469 | if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
470 | return console.log(`${cst.PREFIX_MSG_WARNING}${msg}`);
|
471 | }
|
472 |
|
473 | Common.logMod = function(msg) {
|
474 | if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
475 | return console.log(`${cst.PREFIX_MSG_MOD}${msg}`);
|
476 | }
|
477 |
|
478 | Common.printOut = function() {
|
479 | if (process.env.PM2_SILENT === 'true' || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
480 | return console.log.apply(console, arguments);
|
481 | };
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 | Common.extend = function(destination, source) {
|
488 | if (typeof destination !== 'object') {
|
489 | destination = {};
|
490 | }
|
491 | if (!source || typeof source !== 'object') {
|
492 | return destination;
|
493 | }
|
494 |
|
495 | Object.keys(source).forEach(function(new_key) {
|
496 | if (source[new_key] != '[object Object]')
|
497 | destination[new_key] = source[new_key];
|
498 | });
|
499 |
|
500 | return destination;
|
501 | };
|
502 |
|
503 |
|
504 |
|
505 |
|
506 | Common.safeExtend = function(origin, add){
|
507 | if (!add || typeof add != 'object') return origin;
|
508 |
|
509 |
|
510 | var keysToIgnore = ['name', 'exec_mode', 'env', 'args', 'pm_cwd', 'exec_interpreter', 'pm_exec_path', 'node_args', 'pm_out_log_path', 'pm_err_log_path', 'pm_pid_path', 'pm_id', 'status', 'pm_uptime', 'created_at', 'windowsHide', 'username', 'merge_logs', 'kill_retry_time', 'prev_restart_delay', 'instance_var', 'unstable_restarts', 'restart_time', 'axm_actions', 'pmx_module', 'command', 'watch', 'versioning', 'vizion_runing', 'MODULE_DEBUG', 'pmx', 'axm_options', 'created_at', 'watch', 'vizion', 'axm_dynamic', 'axm_monitor', 'instances', 'automation', 'autorestart', 'unstable_restart', 'treekill', 'exit_code', 'vizion'];
|
511 |
|
512 | var keys = Object.keys(add);
|
513 | var i = keys.length;
|
514 | while (i--) {
|
515 |
|
516 | if(keysToIgnore.indexOf(keys[i]) == -1 && add[keys[i]] != '[object Object]')
|
517 | origin[keys[i]] = add[keys[i]];
|
518 | }
|
519 | return origin;
|
520 | };
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 | Common.mergeEnvironmentVariables = function(app_env, env_name, deploy_conf) {
|
536 | var app = fclone(app_env);
|
537 |
|
538 | var new_conf = {
|
539 | env : {}
|
540 | }
|
541 |
|
542 |
|
543 | for (var key in app.env) {
|
544 | if (typeof app.env[key] == 'object') {
|
545 | app.env[key] = JSON.stringify(app.env[key]);
|
546 | }
|
547 | }
|
548 |
|
549 | |
550 |
|
551 |
|
552 | util._extend(new_conf, app)
|
553 |
|
554 | if (env_name) {
|
555 |
|
556 | if (deploy_conf && deploy_conf[env_name] && deploy_conf[env_name]['env']) {
|
557 | util._extend(new_conf.env, deploy_conf[env_name]['env']);
|
558 | }
|
559 |
|
560 | util._extend(new_conf.env, app.env);
|
561 |
|
562 |
|
563 | if ('env_' + env_name in app) {
|
564 | util._extend(new_conf.env, app['env_' + env_name]);
|
565 | }
|
566 | else {
|
567 | Common.printOut(cst.PREFIX_MSG_WARNING + chalk.bold('Environment [%s] is not defined in process file'), env_name);
|
568 | }
|
569 | }
|
570 |
|
571 | delete new_conf.exec_mode
|
572 |
|
573 | var res = {
|
574 | current_conf: {}
|
575 | }
|
576 |
|
577 | util._extend(res, new_conf.env)
|
578 | util._extend(res.current_conf, new_conf)
|
579 |
|
580 |
|
581 | if (app.exec_interpreter &&
|
582 | app.exec_interpreter.indexOf('@') > -1) {
|
583 | resolveNodeInterpreter(app);
|
584 | res.current_conf.exec_interpreter = app.exec_interpreter
|
585 | }
|
586 |
|
587 | return res
|
588 | }
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 |
|
600 | Common.resolveAppAttributes = function(opts, legacy_app) {
|
601 | var appConf = fclone(legacy_app);
|
602 |
|
603 | var app = Common.prepareAppConf(opts, appConf);
|
604 | if (app instanceof Error) {
|
605 | Common.printError(cst.PREFIX_MSG_ERR + app.message);
|
606 | throw new Error(app.message);
|
607 | }
|
608 | return app;
|
609 | }
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 |
|
616 |
|
617 | Common.verifyConfs = function(appConfs) {
|
618 | if (!appConfs || appConfs.length == 0) {
|
619 | return [];
|
620 | }
|
621 |
|
622 |
|
623 | appConfs = [].concat(appConfs);
|
624 |
|
625 | var verifiedConf = [];
|
626 |
|
627 | for (var i = 0; i < appConfs.length; i++) {
|
628 | var app = appConfs[i];
|
629 |
|
630 | if (app.exec_mode)
|
631 | app.exec_mode = app.exec_mode.replace(/^(fork|cluster)$/, '$1_mode');
|
632 |
|
633 |
|
634 | if (app.cmd && !app.script) {
|
635 | app.script = app.cmd
|
636 | delete app.cmd
|
637 | }
|
638 |
|
639 | if (app.command && !app.script) {
|
640 | app.script = app.command
|
641 | delete app.command
|
642 | }
|
643 |
|
644 | if (!app.env) {
|
645 | app.env = {}
|
646 | }
|
647 |
|
648 |
|
649 | Common.renderApplicationName(app);
|
650 |
|
651 | if (app.execute_command == true) {
|
652 | app.exec_mode = 'fork'
|
653 | delete app.execute_command
|
654 | }
|
655 |
|
656 | app.username = Common.getCurrentUsername();
|
657 |
|
658 | |
659 |
|
660 |
|
661 |
|
662 | if (app.script && app.script.indexOf(' ') > -1 && app.script === path.basename(app.script)) {
|
663 | var _script = app.script;
|
664 | if (require('shelljs').which('bash'))
|
665 | app.script = 'bash';
|
666 | else if (require('shelljs').which('sh'))
|
667 | app.script = 'sh';
|
668 | else
|
669 | throw new Error('bash and sh not available in $PATH')
|
670 |
|
671 | app.args = ['-c', _script];
|
672 | if (!app.name) {
|
673 | app.name = _script
|
674 | }
|
675 | }
|
676 |
|
677 | |
678 |
|
679 |
|
680 | if (app.time) {
|
681 | app.log_date_format = 'YYYY-MM-DDTHH:mm:ss'
|
682 | }
|
683 |
|
684 | |
685 |
|
686 |
|
687 |
|
688 | if ((app.uid || app.gid || app.user) && app.force !== true) {
|
689 |
|
690 | if (cst.IS_WINDOWS === true) {
|
691 | Common.printError(cst.PREFIX_MSG_ERR + '--uid and --git does not works on windows');
|
692 | return new Error('--uid and --git does not works on windows');
|
693 | }
|
694 |
|
695 |
|
696 | if (process.env.NODE_ENV != 'test' && process.getuid && process.getuid() !== 0) {
|
697 | Common.printError(cst.PREFIX_MSG_ERR + 'To use --uid and --gid please run pm2 as root');
|
698 | return new Error('To use UID and GID please run PM2 as root');
|
699 | }
|
700 |
|
701 |
|
702 | var passwd = require('./tools/passwd.js')
|
703 | var users
|
704 | try {
|
705 | users = passwd.getUsers()
|
706 | } catch(e) {
|
707 | Common.printError(e);
|
708 | return new Error(e);
|
709 | }
|
710 |
|
711 | var user_info = users[app.uid || app.user]
|
712 | if (!user_info) {
|
713 | Common.printError(`${cst.PREFIX_MSG_ERR} User ${app.uid || app.user} cannot be found`);
|
714 | return new Error(`${cst.PREFIX_MSG_ERR} User ${app.uid || app.user} cannot be found`);
|
715 | }
|
716 |
|
717 | app.env.HOME = user_info.homedir
|
718 | app.uid = parseInt(user_info.userId)
|
719 | app.gid = parseInt(user_info.groupId)
|
720 | }
|
721 |
|
722 | |
723 |
|
724 |
|
725 | if (process.env.PM2_DEEP_MONITORING) {
|
726 | app.deep_monitoring = true;
|
727 | }
|
728 |
|
729 | if (app.automation == false) {
|
730 | app.pmx = false;
|
731 | }
|
732 |
|
733 | if (app.disable_trace) {
|
734 | app.trace = false
|
735 | delete app.disable_trace;
|
736 | }
|
737 |
|
738 | |
739 |
|
740 |
|
741 | if (app.instances == 'max') {
|
742 | app.instances = 0;
|
743 | }
|
744 |
|
745 | if (typeof(app.instances) === 'string') {
|
746 | app.instances = parseInt(app.instances) || 0;
|
747 | }
|
748 |
|
749 | if (app.exec_mode != 'cluster_mode' &&
|
750 | !app.instances &&
|
751 | typeof(app.merge_logs) == 'undefined') {
|
752 | app.merge_logs = true;
|
753 | }
|
754 |
|
755 | var ret;
|
756 | if ((ret = Common.sink.determineCron(app)) instanceof Error)
|
757 | return ret;
|
758 |
|
759 | |
760 |
|
761 |
|
762 | var ret = Config.validateJSON(app);
|
763 | if (ret.errors && ret.errors.length > 0){
|
764 | ret.errors.forEach(function(err) { warn(err) });
|
765 | return new Error(ret.errors);
|
766 | }
|
767 |
|
768 | verifiedConf.push(ret.config);
|
769 | }
|
770 |
|
771 | return verifiedConf;
|
772 | }
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 |
|
780 | Common.getCurrentUsername = function(){
|
781 | var current_user = '';
|
782 |
|
783 | if (os.userInfo) {
|
784 | try {
|
785 | current_user = os.userInfo().username;
|
786 | } catch (err) {
|
787 |
|
788 |
|
789 | }
|
790 | }
|
791 |
|
792 | if(current_user === '') {
|
793 | current_user = process.env.USER || process.env.LNAME || process.env.USERNAME || process.env.SUDO_USER || process.env.C9_USER || process.env.LOGNAME;
|
794 | }
|
795 |
|
796 | return current_user;
|
797 | }
|
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 | Common.renderApplicationName = function(conf){
|
804 | if (!conf.name && conf.script){
|
805 | conf.name = conf.script !== undefined ? path.basename(conf.script) : 'undefined';
|
806 | var lastDot = conf.name.lastIndexOf('.');
|
807 | if (lastDot > 0){
|
808 | conf.name = conf.name.slice(0, lastDot);
|
809 | }
|
810 | }
|
811 | }
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 | function warn(warning){
|
818 | Common.printOut(cst.PREFIX_MSG_WARNING + warning);
|
819 | }
|