UNPKG

23.9 kBJavaScriptView Raw
1/**
2 * Copyright 2013 the PM2 project authors. All rights reserved.
3 * Use of this source code is governed by a license that
4 * can be found in the LICENSE file.
5 */
6
7/**
8 * Common Utilities ONLY USED IN ->CLI<-
9 */
10
11var fs = require('fs');
12var path = require('path');
13var os = require('os');
14var util = require('util');
15var chalk = require('chalk');
16var fclone = require('fclone');
17var semver = require('semver');
18var moment = require('moment');
19var isBinary = require('./tools/isbinaryfile.js');
20var cst = require('../constants.js');
21var extItps = require('./API/interpreter.json');
22var Config = require('./tools/Config');
23var pkg = require('../package.json');
24
25var Common = module.exports;
26
27function 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
47function resolveHome(filepath) {
48 if (filepath[0] === '~') {
49 return path.join(homedir(), filepath.slice(1));
50 }
51 return filepath;
52}
53
54Common.determineSilentCLI = function() {
55 // pm2 should ignore -s --silent -v if they are after '--'
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
74Common.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
83Common.lockReload = function() {
84 try {
85 var t1 = fs.readFileSync(cst.PM2_RELOAD_LOCKFILE).toString();
86
87 // Check if content and if time < 30 return locked
88 // Else if content detected (lock file staled), allow and rewritte
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 // Write latest timestamp
98 fs.writeFileSync(cst.PM2_RELOAD_LOCKFILE, moment.now());
99 return 0;
100 } catch(e) {
101 console.error(e.message || e);
102 }
103};
104
105Common.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 * Resolve app paths and replace missing values with defaults.
115 * @method prepareAppConf
116 * @param app {Object}
117 * @param {} cwd
118 * @param {} outputter
119 * @return app
120 */
121Common.prepareAppConf = function(opts, app) {
122 /**
123 * Minimum validation
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 // CWD option resolving
144 cwd && (cwd[0] != '/') && (cwd = path.resolve(process.cwd(), cwd));
145 cwd = cwd || opts.cwd;
146
147 // Full path script resolution
148 app.pm_exec_path = path.resolve(cwd, app.script);
149
150 // If script does not exist after resolution
151 if (!fs.existsSync(app.pm_exec_path)) {
152 var ckd;
153 // Try resolve command available in $PATH
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 // Throw critical error
161 return new Error('script not found : ' + app.pm_exec_path);
162 }
163
164 /**
165 * Auto detect .map file and enable source map support automatically
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 // Support for Node 0.10.x
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 // Set current env by first adding the process environment and then extending/replacing it
186 // with env specified on command-line or JSON file.
187
188 var env = {};
189
190 /**
191 * Do not copy internal pm2 environment variables if acting on process
192 * is made from a programmatic script started by PM2
193 */
194 if (cst.PM2_PROGRAMMATIC)
195 Common.safeExtend(env, process.env);
196 else
197 env = process.env;
198
199 // Change to double check (dropped , {pm_cwd: cwd})
200 app.env = [{}, env, app.env || {}].reduce(function(e1, e2){
201 return util._extend(e1, e2);
202 });
203
204 app.pm_cwd = cwd;
205 // Interpreter
206 Common.sink.resolveInterpreter(app);
207
208 // Exec mode and cluster stuff
209 Common.sink.determineExecMode(app);
210
211 /**
212 * Scary
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 // PM2 paths
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 * Check if filename is a configuration file
247 * @param {string} filename
248 * @return {mixed} null if not conf file, json or yaml if conf
249 */
250Common.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 * Parses a config file like ecosystem.config.js. Supported formats: JS, JSON, JSON5, YAML.
266 * @param {string} confString contents of the config file
267 * @param {string} filename path to the config file
268 * @return {Object} config object
269 */
270Common.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
298Common.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
306Common.sink = {};
307
308Common.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 * Handle alias (fork <=> fork_mode, cluster <=> cluster_mode)
325 */
326Common.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 * Here we put the default exec mode
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
345var 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 // in order to support both arch, nvm for Windows renames 'node.exe' to:
381 // 'node32.exe' for x32 arch
382 // 'node64.exe' for x64 arch
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 * Resolve interpreter
397 */
398Common.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 // No interpreter defined and correspondance in schema hashmap
404 if (noInterpreter && betterInterpreter) {
405 app.exec_interpreter = betterInterpreter;
406 }
407 // Else if no Interpreter detect if process is binary
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 * Specific installed JS transpilers
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
437Common.deepCopy = Common.serialize = Common.clone = function(obj) {
438 if (obj === null || obj === undefined) return {};
439 return fclone(obj);
440};
441
442Common.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
449Common.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
456Common.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
463Common.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
468Common.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
473Common.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
478Common.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 * Raw extend
486 */
487Common.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 * This is useful when starting script programmatically
505 */
506Common.safeExtend = function(origin, add){
507 if (!add || typeof add != 'object') return origin;
508
509 //Ignore PM2's set environment variables from the nested env
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 //Only copy stuff into the env that we don't have already.
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 * Extend the app.env object of with the properties taken from the
525 * app.env_[envName] and deploy configuration.
526 * Also update current json attributes
527 *
528 * Used only for Configuration file processing
529 *
530 * @param {Object} app The app object.
531 * @param {string} envName The given environment name.
532 * @param {Object} deployConf Deployment configuration object (from JSON file or whatever).
533 * @returns {Object} The app.env variables object.
534 */
535Common.mergeEnvironmentVariables = function(app_env, env_name, deploy_conf) {
536 var app = fclone(app_env);
537
538 var new_conf = {
539 env : {}
540 }
541
542 // Stringify possible object
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 * Extra configuration update
551 */
552 util._extend(new_conf, app)
553
554 if (env_name) {
555 // First merge variables from deploy.production.env object as least priority.
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 // Then, last and highest priority, merge the app.env_production object.
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 // #2541 force resolution of node interpreter
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 * This function will resolve paths, option and environment
592 * CALLED before 'prepare' God call (=> PROCESS INITIALIZATION)
593 * @method resolveAppAttributes
594 * @param {Object} opts
595 * @param {Object} opts.cwd
596 * @param {Object} opts.pm2_home
597 * @param {Object} appConf application configuration
598 * @return app
599 */
600Common.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 * Verify configurations
613 * Called on EVERY Operation (start/restart/reload/stop...)
614 * @param {Array} appConfs
615 * @returns {Array}
616 */
617Common.verifyConfs = function(appConfs) {
618 if (!appConfs || appConfs.length == 0) {
619 return [];
620 }
621
622 // Make sure it is an Array.
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 // JSON conf: alias cmd to script
634 if (app.cmd && !app.script) {
635 app.script = app.cmd
636 delete app.cmd
637 }
638 // JSON conf: alias command to script
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 // Render an app name if not existing.
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 * If command is like pm2 start "python xx.py --ok"
660 * Then automatically start the script with bash -c and set a name eq to command
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 * Add log_date_format by default
679 */
680 if (app.time) {
681 app.log_date_format = 'YYYY-MM-DDTHH:mm:ss'
682 }
683
684 /**
685 * Checks + Resolve UID/GID
686 * comes from pm2 --uid <> --gid <> or --user
687 */
688 if ((app.uid || app.gid || app.user) && app.force !== true) {
689 // 1/ Check if windows
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 // 2/ Verify that user is root (todo: verify if other has right)
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 // 3/ Resolve user info via /etc/password
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 * Specific options of PM2.io
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 * Instances params
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 * Now validation configuration
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 * Get current username
776 * Called on EVERY starting app
777 *
778 * @returns {String}
779 */
780Common.getCurrentUsername = function(){
781 var current_user = '';
782
783 if (os.userInfo) {
784 try {
785 current_user = os.userInfo().username;
786 } catch (err) {
787 // For the case of unhandled error for uv_os_get_passwd
788 // https://github.com/Unitech/pm2/issues/3184
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 * Render an app name if not existing.
801 * @param {Object} conf
802 */
803Common.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 * Show warnings
815 * @param {String} warning
816 */
817function warn(warning){
818 Common.printOut(cst.PREFIX_MSG_WARNING + warning);
819}