diff --git a/lib/CLI.js b/lib/CLI.js index 1466d482..56f83666 100644 --- a/lib/CLI.js +++ b/lib/CLI.js @@ -6,9 +6,7 @@ var fs = require('fs'); var path = require('path'); var async = require('async'); var debug = require('debug')('pm2:monit'); -var semver = require('semver'); var util = require('util'); -var vm = require('vm'); var chalk = require('chalk'); var exec = require('child_process').exec; var p = path; @@ -29,14 +27,7 @@ var Utility = require('./Utility.js'); var Modularizer = require('./Modularizer.js'); var Configuration = require('../lib/Configuration.js'); -var CliHelper = require('./CLI/helper.js'); - - -var Deploy = require('pm2-deploy'); - -var exitCli = Common.exitCli; -var printError = Common.printError; -var printOut = Common.printOut; +var CLIDeploy = require('./CLI/Deploy.js'); CLI.pm2Init = function() { if (!fs.existsSync(cst.PM2_ROOT_PATH)) { @@ -149,7 +140,7 @@ CLI._startScript = function(script, opts, cb) { conf.script = script; if ((appConf = verifyConfs(conf)) === null) - return exitCli(cst.ERROR_EXIT); + return Common.exitCli(cst.ERROR_EXIT); conf = appConf[0]; @@ -158,7 +149,7 @@ CLI._startScript = function(script, opts, cb) { */ if (appConf.write) { var dst_path = path.join(process.env.PWD, conf.name + '-pm2.json'); - printOut(cst.PREFIX_MSG + 'Writing configuration to', chalk.blue(dst_path)); + Common.printOut(cst.PREFIX_MSG + 'Writing configuration to', chalk.blue(dst_path)); // pretty JSON try { fs.writeFileSync(dst_path, JSON.stringify(conf, null, 2)); @@ -182,7 +173,7 @@ CLI._startScript = function(script, opts, cb) { if (ids.length > 0) { CLI._restart(script, function(err, list) { if (err) return cb(err); - printOut(cst.PREFIX_MSG + 'Process successfully started'); + Common.printOut(cst.PREFIX_MSG + 'Process successfully started'); return cb(true, list); }); } @@ -192,7 +183,7 @@ CLI._startScript = function(script, opts, cb) { else { CLI._restart('all', function(err, list) { if (err) return cb(err); - printOut(cst.PREFIX_MSG + 'Process successfully started'); + Common.printOut(cst.PREFIX_MSG + 'Process successfully started'); return cb(true, list); }); } @@ -203,14 +194,14 @@ CLI._startScript = function(script, opts, cb) { CLI._restart(script, function(err, list) { if (err) return cb(err); - printOut(cst.PREFIX_MSG + 'Process successfully started'); + Common.printOut(cst.PREFIX_MSG + 'Process successfully started'); return cb(true, list); }); } function restartExistingProcessPath(cb) { Satan.executeRemote('findByFullPath', path.resolve(process.cwd(), script), function(err, exec) { - if (err) return cb ? cb(new Error(err)) : exitCli(cst.ERROR_EXIT); + if (err) return cb ? cb(new Error(err)) : Common.exitCli(cst.ERROR_EXIT); if (exec && (exec[0].pm2_env.status == cst.STOPPED_STATUS || exec[0].pm2_env.status == cst.STOPPING_STATUS || @@ -218,15 +209,15 @@ CLI._startScript = function(script, opts, cb) { var app_name = exec[0].pm2_env.name; CLI._restart(app_name, function(err, list) { - if (err) return cb ? cb(new Error(err)) : exitCli(cst.ERROR_EXIT); + if (err) return cb ? cb(new Error(err)) : Common.exitCli(cst.ERROR_EXIT); - printOut(cst.PREFIX_MSG + 'Process successfully started'); + Common.printOut(cst.PREFIX_MSG + 'Process successfully started'); return cb(true, list); }); return false; } else if (exec && !opts.force) { - printError(cst.PREFIX_MSG_ERR + 'Script already launched, add -f option to force re-execution'); + Common.printError(cst.PREFIX_MSG_ERR + 'Script already launched, add -f option to force re-execution'); return cb(new Error('Script already launched')); } @@ -235,11 +226,11 @@ CLI._startScript = function(script, opts, cb) { try { resolved_paths = resolvePaths(conf); } catch(e) { - printError(e); + Common.printError(e); return cb(e); } - printOut(cst.PREFIX_MSG + 'Starting %s in %s (%d instance' + (resolved_paths.instances > 1 ? 's' : '') + ')', + Common.printOut(cst.PREFIX_MSG + 'Starting %s in %s (%d instance' + (resolved_paths.instances > 1 ? 's' : '') + ')', script, resolved_paths.exec_mode, resolved_paths.instances); if (!resolved_paths.env) resolved_paths.env = {}; @@ -248,11 +239,11 @@ CLI._startScript = function(script, opts, cb) { Satan.executeRemote('prepare', resolved_paths, function(err, data) { if (err) { - printError(cst.PREFIX_MSG_ERR + 'Error while launching application', err.stack || err); + Common.printError(cst.PREFIX_MSG_ERR + 'Error while launching application', err.stack || err); return cb({msg : err}); } - printOut(cst.PREFIX_MSG + 'Done.'); + Common.printOut(cst.PREFIX_MSG + 'Done.'); return cb(true, data); }); return false; @@ -266,7 +257,7 @@ CLI._startScript = function(script, opts, cb) { ], function(err, data) { if (err instanceof Error) { - return cb ? cb(err) : exitCli(cst.ERROR_EXIT); + return cb ? cb(err) : Common.exitCli(cst.ERROR_EXIT); } var ret = {}; @@ -305,10 +296,10 @@ CLI._startJson = function(cmd, opts, jsonVia, cb) { try { data = fs.readFileSync(cmd); } catch(e) { - printError(cst.PREFIX_MSG_ERR + 'JSON ' + cmd +' not found'); - return cb ? cb(e) : exitCli(cst.ERROR_EXIT); + Common.printError(cst.PREFIX_MSG_ERR + 'JSON ' + cmd +' not found'); + return cb ? cb(e) : Common.exitCli(cst.ERROR_EXIT); } - appConf = parseConfig(data, cmd); + appConf = Utility.parseConfig(data, cmd); } // v2 JSON declaration @@ -318,7 +309,7 @@ CLI._startJson = function(cmd, opts, jsonVia, cb) { if (!Array.isArray(appConf)) appConf = [appConf]; //convert to array if ((appConf = verifyConfs(appConf)) === null) - return cb ? cb({success:false}) : exitCli(cst.ERROR_EXIT); + return cb ? cb({success:false}) : Common.exitCli(cst.ERROR_EXIT); // Get App list var apps_name = []; @@ -350,8 +341,8 @@ CLI._startJson = function(cmd, opts, jsonVia, cb) { try { resolved_paths = resolvePaths(app); } catch (e) { - printError(e); - return cb ? cb({msg : e.message || e}) : exitCli(cst.ERROR_EXIT); + Common.printError(e); + return cb ? cb({msg : e.message || e}) : Common.exitCli(cst.ERROR_EXIT); } // Set watch to true for app if argument passed to CLI @@ -366,11 +357,11 @@ CLI._startJson = function(cmd, opts, jsonVia, cb) { Satan.executeRemote('prepare', resolved_paths, function(err, data) { if (err) { - printError(cst.PREFIX_MSG + 'Process failed to launch', err); + Common.printError(cst.PREFIX_MSG + 'Process failed to launch', err); return next(); } - printOut(cst.PREFIX_MSG + 'Process launched'); + Common.printOut(cst.PREFIX_MSG + 'Process launched'); apps_info = apps_info.concat(data); next(); }); @@ -383,8 +374,8 @@ CLI._startJson = function(cmd, opts, jsonVia, cb) { Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } async.eachLimit(list, cst.CONCURRENT_ACTIONS, function(proc, next) { @@ -394,7 +385,7 @@ CLI._startJson = function(cmd, opts, jsonVia, cb) { if (action == 'reload') { CLI._reloadProcessName(proc.pm2_env.name, 'reloadProcessId', function(err, ret) { - if (err) printError(err); + if (err) Common.printError(err); Satan.notifyGod('reload', proc.pm2_env.pm_id); @@ -404,7 +395,7 @@ CLI._startJson = function(cmd, opts, jsonVia, cb) { }); } else if (action == 'gracefulReload') { CLI._reloadProcessName(proc.pm2_env.name, 'softReloadProcessId', function(err, ret) { - if (err) printError(err); + if (err) Common.printError(err); // And Remove from array to spy Satan.notifyGod('graceful reload', proc.pm2_env.pm_id); @@ -430,7 +421,7 @@ CLI._startJson = function(cmd, opts, jsonVia, cb) { // Pass `env` option CLI._restart(proc.pm2_env.name, env, function(err, ret) { - if (err) printError(err); + if (err) Common.printError(err); Satan.notifyGod('restart', proc.pm2_env.pm_id); // And Remove from array to spy @@ -440,7 +431,7 @@ CLI._startJson = function(cmd, opts, jsonVia, cb) { }); } }, function(err) { - if (err) return cb ? cb(new Error(err)) : exitCli(cst.ERROR_EXIT); + if (err) return cb ? cb(new Error(err)) : Common.exitCli(cst.ERROR_EXIT); // Start missing apps return startApps(apps_name, function(err, apps) { return cb ? cb(err, apps) : speedList(err ? 1 : 0); @@ -450,65 +441,7 @@ CLI._startJson = function(cmd, opts, jsonVia, cb) { }); } -/** - * Deploy command - */ -CLI.deploy = function(file, commands, cb) { - if (file == 'help') { - CliHelper.deployHelp(); - return cb ? cb() : exitCli(cst.SUCCESS_EXIT); - } - - var args = commands.rawArgs; - var env; - - args.splice(0, args.indexOf('deploy') + 1); - - // Find ecosystem file by default - if (file.indexOf('.json') == -1) { - env = args[0]; - file = Utility.whichFileExists(['ecosystem.js', 'ecosystem.json', 'ecosystem.json5', 'package.json']); - - if (!file) { - printError('Not any default deployment file exists'); - return cb ? cb('Not any default ecosystem file present') : exitCli(cst.ERROR_EXIT); - } - } - else - env = args[1]; - - var json_conf = null; - - try { - json_conf = parseConfig(fs.readFileSync(file), file); - } catch (e) { - printError(e); - return cb ? cb(e) : exitCli(cst.ERROR_EXIT); - } - - if (!env) { - CliHelper.deployHelp(); - return cb ? cb() : exitCli(cst.SUCCESS_EXIT); - } - - if (!json_conf.deploy || !json_conf.deploy[env]) { - printError('%s environment is not defined in %s file', env, file); - return cb ? cb('%s environment is not defined in %s file') : exitCli(cst.ERROR_EXIT); - } - - if (!json_conf.deploy[env]['post-deploy']) { - json_conf.deploy[env]['post-deploy'] = 'pm2 startOrRestart ' + file + ' --env ' + env; - } - - Deploy.deployForEnv(json_conf.deploy, env, args, function(err, data) { - if (err) { - printError('Deploy failed'); - return cb ? cb(err) : exitCli(cst.ERROR_EXIT); - } - printOut('--> Success'); - return cb ? cb(null, data) : exitCli(cst.SUCCESS_EXIT); - }); -} +CLI.deploy = CLIDeploy.deploy; /** * Get version of the daemonized PM2 @@ -517,7 +450,7 @@ CLI.deploy = function(file, commands, cb) { */ CLI.getVersion = function(cb) { Satan.executeRemote('getVersion', {}, function(err, version) { - return cb ? cb.apply(null, arguments) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb.apply(null, arguments) : Common.exitCli(cst.SUCCESS_EXIT); }); } @@ -540,20 +473,20 @@ CLI.actionFromJson = function(action, file, opts, jsonVia, cb) { } else if (jsonVia == 'file') { var data = fs.readFileSync(file); - appConf = parseConfig(data, file); + appConf = Utility.parseConfig(data, file); // v2 JSON declaration if (appConf.apps) appConf = appConf.apps; } else if (jsonVia == 'pipe') { appConf = json5.parse(file); } else { - printError('Bad call to actionFromJson, jsonVia should be one of file, pipe'); - return exitCli(cst.ERROR_EXIT); + Common.printError('Bad call to actionFromJson, jsonVia should be one of file, pipe'); + return Common.exitCli(cst.ERROR_EXIT); } if (!Array.isArray(appConf)) appConf = [appConf]; //convert to array if ((appConf = verifyConfs(appConf)) === null) - return exitCli(cst.ERROR_EXIT); + return Common.exitCli(cst.ERROR_EXIT); var ret_processes = []; @@ -573,7 +506,7 @@ CLI.actionFromJson = function(action, file, opts, jsonVia, cb) { Common.getProcessIdByName(name, function(err, ids) { if (err) { - printError(err); + Common.printError(err); return next1(); } if (!ids) return next1(); @@ -595,7 +528,7 @@ CLI.actionFromJson = function(action, file, opts, jsonVia, cb) { Satan.executeRemote(action, opts, function(err, res) { ret_processes.push(res); if (err) { - printError(err); + Common.printError(err); return next2(); } @@ -607,7 +540,7 @@ CLI.actionFromJson = function(action, file, opts, jsonVia, cb) { Satan.notifyGod('stop', id); } - printOut(cst.PREFIX_MSG + 'Process ' + id + ' restarted'); + Common.printOut(cst.PREFIX_MSG + 'Process ' + id + ' restarted'); return next2(); }); }, function(err) { @@ -630,12 +563,12 @@ CLI.startup = function(platform, opts, cb) { if (opts.user) { console.log(cst.PREFIX_MSG + 'You have to run this command as root. Execute the following command:'); console.log(chalk.grey(' sudo su -c "env PATH=$PATH:' + p.dirname(process.execPath) + ' pm2 startup ' + platform + ' -u ' + opts.user + ' --hp ' + process.env.HOME + '"')); - return cb ? cb({msg: 'You have to run this with elevated rights'}) : exitCli(cst.ERROR_EXIT); + return cb ? cb({msg: 'You have to run this with elevated rights'}) : Common.exitCli(cst.ERROR_EXIT); } return exec('whoami', function(err, stdout, stderr) { console.log(cst.PREFIX_MSG + 'You have to run this command as root. Execute the following command:'); console.log(chalk.grey(' sudo su -c "env PATH=$PATH:' + p.dirname(process.execPath) + ' pm2 startup ' + platform + ' -u ' + stdout.trim() + ' --hp ' + process.env.HOME + '"')); - cb ? cb({msg: 'You have to run this with elevated rights'}) : exitCli(cst.ERROR_EXIT); + cb ? cb({msg: 'You have to run this with elevated rights'}) : Common.exitCli(cst.ERROR_EXIT); }); } @@ -671,7 +604,7 @@ CLI.startup = function(platform, opts, cb) { else script = script.replace(/%HOME_PATH%/g, cst.PM2_ROOT_PATH); - printOut(cst.PREFIX_MSG + 'Generating system init script in ' + scriptFile); + Common.printOut(cst.PREFIX_MSG + 'Generating system init script in ' + scriptFile); try { fs.writeFileSync(scriptFile, script); @@ -680,15 +613,15 @@ CLI.startup = function(platform, opts, cb) { } if (!fs.existsSync(scriptFile)) { - printOut(script); - printOut(cst.PREFIX_MSG_ERR + ' There is a problem when trying to write file : ' + scriptFile); - return cb ? cb({msg:'Problem with ' + scriptFile}) : exitCli(cst.ERROR_EXIT); + Common.printOut(script); + Common.printOut(cst.PREFIX_MSG_ERR + ' There is a problem when trying to write file : ' + scriptFile); + return cb ? cb({msg:'Problem with ' + scriptFile}) : Common.exitCli(cst.ERROR_EXIT); } var cmd; var cmdAsUser; - printOut(cst.PREFIX_MSG + 'Making script booting at startup...'); + Common.printOut(cst.PREFIX_MSG + 'Making script booting at startup...'); switch (platform) { case 'systemd': @@ -706,7 +639,7 @@ CLI.startup = function(platform, opts, cb) { case 'amazon': cmd = 'chmod +x ' + scriptFile + '; chkconfig --add ' + p.basename(scriptFile); fs.closeSync(fs.openSync('/var/lock/subsys/pm2-init.sh', 'w')); - printOut(cst.PREFIX_MSG + '/var/lock/subsys/pm2-init.sh lockfile has been added'); + Common.printOut(cst.PREFIX_MSG + '/var/lock/subsys/pm2-init.sh lockfile has been added'); break; case 'gentoo': cmd = 'chmod +x ' + scriptFile + '; rc-update add ' + p.basename(scriptFile) + ' default'; @@ -729,32 +662,32 @@ CLI.startup = function(platform, opts, cb) { cmd = 'pm2 dump'; } - printOut(cst.PREFIX_MSG + '-' + platform + '- Using the command:\n %s', chalk.grey(cmd)); + Common.printOut(cst.PREFIX_MSG + '-' + platform + '- Using the command:\n %s', chalk.grey(cmd)); exec(cmd, function(err, stdo, stde) { if (err) { - printError(err); - printError('----- Are you sure you use the right platform command line option ? centos / redhat, amazon, ubuntu, gentoo, systemd or darwin?'); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + Common.printError('----- Are you sure you use the right platform command line option ? centos / redhat, amazon, ubuntu, gentoo, systemd or darwin?'); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } - printOut(stdo.toString().replace(/[\r\n]$/, '')); - printOut(cst.PREFIX_MSG + 'Done.'); - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); + Common.printOut(stdo.toString().replace(/[\r\n]$/, '')); + Common.printOut(cst.PREFIX_MSG + 'Done.'); + return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); } CLI.logrotate = function(opts, cb) { if (process.getuid() != 0) { return exec('whoami', function(err, stdout, stderr) { - printError(cst.PREFIX_MSG + 'You have to run this command as root. Execute the following command:\n' + + Common.printError(cst.PREFIX_MSG + 'You have to run this command as root. Execute the following command:\n' + chalk.grey(' sudo env PATH=$PATH:' + p.dirname(process.execPath) + ' pm2 logrotate -u ' + stdout.trim())); - cb ? cb({msg: 'You have to run this with elevated rights'}) : exitCli(cst.ERROR_EXIT); + cb ? cb({msg: 'You have to run this with elevated rights'}) : Common.exitCli(cst.ERROR_EXIT); }); } if(!fs.existsSync('/etc/logrotate.d')) { - printError(cst.PREFIX_MSG + '/etc/logrotate.d does not exist we can not copy the default configuration.'); - return cb ? cb({msg: '/etc/logrotate.d does not exist'}) : exitCli(cst.ERROR_EXIT); + Common.printError(cst.PREFIX_MSG + '/etc/logrotate.d does not exist we can not copy the default configuration.'); + return cb ? cb({msg: '/etc/logrotate.d does not exist'}) : Common.exitCli(cst.ERROR_EXIT); } var script = fs.readFileSync(path.join(__dirname, cst.LOGROTATE_SCRIPT), {encoding: 'utf8'}); @@ -770,8 +703,8 @@ CLI.logrotate = function(opts, cb) { console.error(e.stack || e); } - printOut(cst.PREFIX_MSG + 'Logrotate configuration added to /etc/logrotate.d/pm2'); - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); + Common.printOut(cst.PREFIX_MSG + 'Logrotate configuration added to /etc/logrotate.d/pm2'); + return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); } /** @@ -781,11 +714,11 @@ CLI.logrotate = function(opts, cb) { CLI.ping = function(cb) { Satan.executeRemote('ping', {}, function(err, res) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } - printOut(res); - return cb ? cb(null, res) : exitCli(cst.SUCCESS_EXIT); + Common.printOut(res); + return cb ? cb(null, res) : Common.exitCli(cst.SUCCESS_EXIT); }); } @@ -798,7 +731,7 @@ CLI.reset = function(process_name, cb) { async.eachLimit(ids, cst.CONCURRENT_ACTIONS, function(id, next) { Satan.executeRemote('resetMetaProcessId', id, function(err, res) { if (err) console.error(err); - printOut(cst.PREFIX_MSG + 'Resetting meta for process id %d', id); + Common.printOut(cst.PREFIX_MSG + 'Resetting meta for process id %d', id); return next(); }); }, function(err) { @@ -810,8 +743,8 @@ CLI.reset = function(process_name, cb) { if (process_name == 'all') { Common.getAllProcessId(function(err, ids) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } return processIds(ids, cb); }); @@ -819,12 +752,12 @@ CLI.reset = function(process_name, cb) { else if (isNaN(process_name)) { Common.getProcessIdByName(process_name, function(err, ids) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } if (ids.length === 0) { - printError('Unknown process name'); - return cb ? cb({msg:'Unknown process name'}) : exitCli(cst.ERROR_EXIT); + Common.printError('Unknown process name'); + return cb ? cb({msg:'Unknown process name'}) : Common.exitCli(cst.ERROR_EXIT); } return processIds(ids, cb); }); @@ -845,16 +778,16 @@ CLI.resurrect = function(cb) { } catch(e) { console.error(cst.PREFIX_MSG + 'No processes saved; DUMP file doesn\'t exist'); if (cb) return cb(e); - else return exitCli(cst.ERROR_EXIT); + else return Common.exitCli(cst.ERROR_EXIT); } (function ex(apps) { if (!apps[0]) return cb ? cb(null, apps) : speedList(); Satan.executeRemote('prepare', apps[0], function(err, dt) { if (err) - printError('Process %s not launched - (script missing)', apps[0].pm_exec_path); + Common.printError('Process %s not launched - (script missing)', apps[0].pm_exec_path); else - printOut('Process %s launched', apps[0].pm_exec_path); + Common.printOut('Process %s launched', apps[0].pm_exec_path); Satan.notifyGod('resurrect', dt[0].pm2_env.pm_id); @@ -873,7 +806,7 @@ CLI.resurrect = function(cb) { */ CLI.updatePM2 = CLI.update = function(cb) { - printOut('Be sure to have the latest version by doing `npm install pm2@latest -g` before doing this procedure.'); + Common.printOut('Be sure to have the latest version by doing `npm install pm2@latest -g` before doing this procedure.'); // Dump PM2 processes Satan.executeRemote('notifyKillPM2', {}, function() {}); @@ -884,7 +817,7 @@ CLI.updatePM2 = CLI.update = function(cb) { Satan.launchDaemon(function(err, child) { Satan.launchRPC(function() { CLI.resurrect(function() { - printOut(chalk.blue.bold('>>>>>>>>>> PM2 updated')); + Common.printOut(chalk.blue.bold('>>>>>>>>>> PM2 updated')); require('./Modularizer.js').launchAll(function() { return cb ? cb(null, {success:true}) : speedList(); }); @@ -907,8 +840,8 @@ CLI.dump = function(cb) { var env_arr = []; Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { - printError('Error retrieving process list: ' + err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError('Error retrieving process list: ' + err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } /** @@ -924,7 +857,7 @@ CLI.dump = function(cb) { console.error(e.stack || e); } if (cb) return cb(null, {success:true}); - else return exitCli(cst.SUCCESS_EXIT); + else return Common.exitCli(cst.SUCCESS_EXIT); } (function ex(apps) { @@ -952,10 +885,10 @@ CLI.web = function(cb) { execMode : 'fork_mode' }, function(err, proc) { if (err) { - printError(cst.PREFIX_MSG_ERR + 'Error while launching application', err.stack || err); + Common.printError(cst.PREFIX_MSG_ERR + 'Error while launching application', err.stack || err); return cb ? cb({msg:err}) : speedList(); } - printOut(cst.PREFIX_MSG + 'Process launched'); + Common.printOut(cst.PREFIX_MSG + 'Process launched'); return cb ? cb(null, proc) : speedList(); }); } @@ -987,8 +920,8 @@ CLI.reload = function(process_name, cb) { CLI._reloadAll = function (reload_method, cb) { Common.getAllProcess(function(err, procs) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } async.eachLimit(procs, cst.CONCURRENT_ACTIONS, function(proc, next) { @@ -1004,7 +937,7 @@ CLI._reloadAll = function (reload_method, cb) { } Satan.executeRemote(reload_method, proc.pm2_env.pm_id, function(err, list) { - printOut(cst.PREFIX_MSG + 'Process %s successfully reloaded', proc.pm2_env.name); + Common.printOut(cst.PREFIX_MSG + 'Process %s successfully reloaded', proc.pm2_env.name); Satan.notifyGod('reload', proc.pm2_env.pm_id); return next(); }); @@ -1024,17 +957,17 @@ CLI._reloadAll = function (reload_method, cb) { * @return */ CLI._reloadProcessName = function(process_name, reload_method, cb) { - printOut(cst.PREFIX_MSG + 'Reloading process by name %s', process_name); + Common.printOut(cst.PREFIX_MSG + 'Reloading process by name %s', process_name); Common.getProcessByName(process_name, function(err, processes) { if (err) { - return cb ? cb({msg : err}) : exitCli(cst.ERROR_EXIT); + return cb ? cb({msg : err}) : Common.exitCli(cst.ERROR_EXIT); } if (processes.length === 0) { - printError('No processes with this name: %s', process_name); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError('No processes with this name: %s', process_name); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } async.eachLimit(processes, cst.CONCURRENT_ACTIONS, function(proc, next) { @@ -1058,15 +991,15 @@ CLI._reloadProcessName = function(process_name, reload_method, cb) { Satan.notifyGod('reload', proc.pm2_env.pm_id); - printOut(cst.PREFIX_MSG + 'Process %s successfully reloaded', proc.pm2_env.name); + Common.printOut(cst.PREFIX_MSG + 'Process %s successfully reloaded', proc.pm2_env.name); return next(); }); }, function(err) { if (err) { - printError(err.stack); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err.stack); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } - printOut(cst.PREFIX_MSG + 'All processes reloaded'); + Common.printOut(cst.PREFIX_MSG + 'All processes reloaded'); return cb ? cb(null, processes) : setTimeout(speedList, 300); }); return false; @@ -1110,17 +1043,17 @@ CLI._jsonStartOrAction = function(action, json_conf, opts, cb) { try { var data = fs.readFileSync(json_conf); } catch(e) { - printError('Configuration file %s is missing. Action canceled.', json_conf); - return cb ? cb(e) : exitCli(cst.ERROR_EXIT); + Common.printError('Configuration file %s is missing. Action canceled.', json_conf); + return cb ? cb(e) : Common.exitCli(cst.ERROR_EXIT); } - var appConf = parseConfig(data, json_conf), deployConf = null; + var appConf = Utility.parseConfig(data, json_conf), deployConf = null; // v2 JSON declaration if (appConf.deploy) deployConf = appConf.deploy; if (appConf.apps) appConf = appConf.apps; if ((appConf = verifyConfs(appConf)) === null) - return exitCli(cst.ERROR_EXIT); + return Common.exitCli(cst.ERROR_EXIT); var apps_name = []; @@ -1143,8 +1076,8 @@ CLI._jsonStartOrAction = function(action, json_conf, opts, cb) { try { resolved_paths = resolvePaths(app); } catch (e) { - printError(e); - return cb ? cb({msg : e.message || e}) : exitCli(cst.ERROR_EXIT); + Common.printError(e); + return cb ? cb({msg : e.message || e}) : Common.exitCli(cst.ERROR_EXIT); } Satan.executeRemote('prepare', resolved_paths, function(err, data) { @@ -1159,8 +1092,8 @@ CLI._jsonStartOrAction = function(action, json_conf, opts, cb) { Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } async.eachLimit(list, cst.CONCURRENT_ACTIONS, function(proc, next) { @@ -1169,7 +1102,7 @@ CLI._jsonStartOrAction = function(action, json_conf, opts, cb) { if (action == 'reload') { CLI._reloadProcessName(proc.pm2_env.name, 'reloadProcessId', function(err, ret) { - if (err) printError(err); + if (err) Common.printError(err); Satan.notifyGod('reload', proc.pm2_env.pm_id); @@ -1179,7 +1112,7 @@ CLI._jsonStartOrAction = function(action, json_conf, opts, cb) { }); } else if (action == 'gracefulReload') { CLI._reloadProcessName(proc.pm2_env.name, 'softReloadProcessId', function(err, ret) { - if (err) printError(err); + if (err) Common.printError(err); // And Remove from array to spy Satan.notifyGod('graceful reload', proc.pm2_env.pm_id); @@ -1203,7 +1136,7 @@ CLI._jsonStartOrAction = function(action, json_conf, opts, cb) { // Pass `env` option CLI._restart(proc.pm2_env.name, env, function(err, ret) { - if (err) printError(err); + if (err) Common.printError(err); Satan.notifyGod('restart', proc.pm2_env.pm_id); // And Remove from array to spy @@ -1214,7 +1147,7 @@ CLI._jsonStartOrAction = function(action, json_conf, opts, cb) { } return false; }, function(err) { - if (err) return cb ? cb(new Error(err)) : exitCli(cst.ERROR_EXIT); + if (err) return cb ? cb(new Error(err)) : Common.exitCli(cst.ERROR_EXIT); // Start missing apps return startApps(apps_name, function() { return cb ? cb(null, {success:true}) : speedList(); @@ -1257,7 +1190,7 @@ CLI._operate = function(action_name, process_name, envs, cb) { Satan.executeRemote(action_name, opts, function(err, res) { if (err) { - printError(cst.PREFIX_MSG_ERR + 'Process %s not found', id); + Common.printError(cst.PREFIX_MSG_ERR + 'Process %s not found', id); return next('Process not found'); } @@ -1269,11 +1202,11 @@ CLI._operate = function(action_name, process_name, envs, cb) { Satan.notifyGod('stop', id); } - printOut(cst.PREFIX_MSG + action_name + ' process id %d', id); + Common.printOut(cst.PREFIX_MSG + action_name + ' process id %d', id); return next(); }); }, function(err) { - if (err) return cb ? cb(new Error(err)) : exitCli(cst.ERROR_EXIT); + if (err) return cb ? cb(new Error(err)) : Common.exitCli(cst.ERROR_EXIT); return cb ? cb(null, {success:true}) : speedList(); }); } @@ -1281,12 +1214,12 @@ CLI._operate = function(action_name, process_name, envs, cb) { if (process_name == 'all') { Common.getAllProcessId(function(err, ids) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } if (!ids || ids.length === 0) { - printError(cst.PREFIX_MSG_WARNING + 'No process found'); - return cb ? cb({ success : false, msg : 'process name not found'}) : exitCli(cst.ERROR_EXIT); + Common.printError(cst.PREFIX_MSG_WARNING + 'No process found'); + return cb ? cb({ success : false, msg : 'process name not found'}) : Common.exitCli(cst.ERROR_EXIT); } return processIds(ids, cb); @@ -1302,12 +1235,12 @@ CLI._operate = function(action_name, process_name, envs, cb) { Common.getProcessIdByName(process_name, allow_module_restart, function(err, ids, full_detail) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } if (!ids || ids.length === 0) { - printError(cst.PREFIX_MSG_ERR + 'Process %s not found', process_name); - return cb ? cb({ success : false, msg : 'process name not found'}) : exitCli(cst.ERROR_EXIT); + Common.printError(cst.PREFIX_MSG_ERR + 'Process %s not found', process_name); + return cb ? cb({ success : false, msg : 'process name not found'}) : Common.exitCli(cst.ERROR_EXIT); } /** @@ -1367,7 +1300,7 @@ CLI.delete = function(process_name, jsonVia, cb) { process_name = process_name.toString(); } - printOut(cst.PREFIX_MSG + 'Deleting %s process', process_name); + Common.printOut(cst.PREFIX_MSG + 'Deleting %s process', process_name); if (jsonVia == 'pipe') return CLI.actionFromJson('deleteProcessId', process_name, commander, 'pipe', cb); @@ -1386,7 +1319,7 @@ CLI.stop = function(process_name, cb) { if (typeof(process_name) === 'number') process_name = process_name.toString(); - printOut(cst.PREFIX_MSG + 'Stopping ' + process_name); + Common.printOut(cst.PREFIX_MSG + 'Stopping ' + process_name); if (process_name == "-") { process.stdin.resume(); @@ -1423,8 +1356,8 @@ CLI.generateSample = function() { } catch (e) { console.error(e.stack || e); } - printOut('File %s generated', path.join(process.env.PWD, f_name)); - exitCli(cst.SUCCESS_EXIT); + Common.printOut('File %s generated', path.join(process.env.PWD, f_name)); + Common.exitCli(cst.SUCCESS_EXIT); } /** @@ -1435,8 +1368,8 @@ CLI.generateSample = function() { CLI.list = function(cb) { Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } return cb ? cb(null, list) : speedList(); }); @@ -1451,18 +1384,18 @@ CLI.list = function(cb) { CLI.jlist = function(debug) { Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { - printError(err); - exitCli(cst.ERROR_EXIT); + Common.printError(err); + Common.exitCli(cst.ERROR_EXIT); } if (debug) { - printOut(util.inspect(list, false, null, false)); + Common.printOut(util.inspect(list, false, null, false)); } else { - printOut(JSON.stringify(list)); + Common.printOut(JSON.stringify(list)); } - exitCli(cst.SUCCESS_EXIT); + Common.exitCli(cst.SUCCESS_EXIT); }); } @@ -1476,7 +1409,7 @@ CLI.scale = function(app_name, number, cb) { function addProcs(proc, value, cb) { (function ex(proc, number) { if (number-- === 0) return cb(); - printOut(cst.PREFIX_MSG + 'Scaling up application'); + Common.printOut(cst.PREFIX_MSG + 'Scaling up application'); Satan.executeRemote('duplicateProcessId', proc.pm2_env.pm_id, ex.bind(this, proc, number)); })(proc, number); } @@ -1496,18 +1429,18 @@ CLI.scale = function(app_name, number, cb) { Common.getProcessByName(app_name, function(err, procs) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } if (!procs || procs.length === 0) { - printError(cst.PREFIX_MSG_ERR + 'Application %s not found', app_name); - return cb ? cb({msg: 'App not found'}) : exitCli(cst.ERROR_EXIT); + Common.printError(cst.PREFIX_MSG_ERR + 'Application %s not found', app_name); + return cb ? cb({msg: 'App not found'}) : Common.exitCli(cst.ERROR_EXIT); } if (procs[0].pm2_env.exec_mode !== 'cluster_mode') { - printError(cst.PREFIX_MSG_ERR + 'Application %s is not in cluster mode', app_name); - return cb ? cb({msg: 'App not in cluster mode'}) : exitCli(cst.ERROR_EXIT); + Common.printError(cst.PREFIX_MSG_ERR + 'Application %s is not in cluster mode', app_name); + return cb ? cb({msg: 'App not in cluster mode'}) : Common.exitCli(cst.ERROR_EXIT); } var proc_number = procs.length; @@ -1529,8 +1462,8 @@ CLI.scale = function(app_name, number, cb) { else if (number > 0) return addProcs(procs[0], number, end); else { - printError(cst.PREFIX_MSG_ERR + 'Nothing to do'); - return cb ? cb({msg: 'Same process number'}) : exitCli(cst.ERROR_EXIT); + Common.printError(cst.PREFIX_MSG_ERR + 'Nothing to do'); + return cb ? cb({msg: 'Same process number'}) : Common.exitCli(cst.ERROR_EXIT); } } }); @@ -1542,29 +1475,29 @@ CLI.scale = function(app_name, number, cb) { * @return */ CLI.flush = function(cb) { - printOut(cst.PREFIX_MSG + 'Flushing ' + cst.PM2_LOG_FILE_PATH); + Common.printOut(cst.PREFIX_MSG + 'Flushing ' + cst.PM2_LOG_FILE_PATH); fs.closeSync(fs.openSync(cst.PM2_LOG_FILE_PATH, 'w')); Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } list.forEach(function(l) { - printOut(cst.PREFIX_MSG + 'Flushing'); - printOut(cst.PREFIX_MSG + l.pm2_env.pm_out_log_path); - printOut(cst.PREFIX_MSG + l.pm2_env.pm_err_log_path); + Common.printOut(cst.PREFIX_MSG + 'Flushing'); + Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_out_log_path); + Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_err_log_path); if (l.pm2_env.pm_log_path) { - printOut(cst.PREFIX_MSG + l.pm2_env.pm_log_path); + Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_log_path); fs.closeSync(fs.openSync(l.pm2_env.pm_log_path, 'w')); } fs.closeSync(fs.openSync(l.pm2_env.pm_out_log_path, 'w')); fs.closeSync(fs.openSync(l.pm2_env.pm_err_log_path, 'w')); }); - printOut(cst.PREFIX_MSG + 'Logs flushed'); - return cb ? cb(null, list) : exitCli(cst.SUCCESS_EXIT); + Common.printOut(cst.PREFIX_MSG + 'Logs flushed'); + return cb ? cb(null, list) : Common.exitCli(cst.SUCCESS_EXIT); }); } @@ -1579,8 +1512,8 @@ CLI.describe = function(pm2_id, cb) { Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { - printError('Error retrieving process list: ' + err); - exitCli(cst.ERROR_EXIT); + Common.printError('Error retrieving process list: ' + err); + Common.exitCli(cst.ERROR_EXIT); } list.forEach(function(proc) { @@ -1591,8 +1524,8 @@ CLI.describe = function(pm2_id, cb) { }); if (found_proc.length === 0) { - printError(cst.PREFIX_MSG_WARNING + '%s doesn\'t exist', pm2_id); - return cb ? cb(null, []) : exitCli(cst.ERROR_EXIT); + Common.printError(cst.PREFIX_MSG_WARNING + '%s doesn\'t exist', pm2_id); + return cb ? cb(null, []) : Common.exitCli(cst.ERROR_EXIT); } if (!cb) { @@ -1601,7 +1534,7 @@ CLI.describe = function(pm2_id, cb) { }); } - return cb ? cb(null, found_proc) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb(null, found_proc) : Common.exitCli(cst.SUCCESS_EXIT); }); } @@ -1611,14 +1544,14 @@ CLI.describe = function(pm2_id, cb) { * @return */ CLI.reloadLogs = function(cb) { - printOut('Reloading all logs...'); + Common.printOut('Reloading all logs...'); Satan.executeRemote('reloadLogs', {}, function(err, logs) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } - printOut('All logs reloaded'); - return cb ? cb(null, logs) : exitCli(cst.SUCCESS_EXIT); + Common.printOut('All logs reloaded'); + return cb ? cb(null, logs) : Common.exitCli(cst.SUCCESS_EXIT); }); } @@ -1634,10 +1567,10 @@ CLI.sendDataToProcessId = function(proc_id, packet, cb) { Satan.executeRemote('sendDataToProcessId', packet, function(err, res) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } - printOut('successfully sent data to process'); + Common.printOut('successfully sent data to process'); return cb ? cb(null, res) : speedList(); }); } @@ -1655,10 +1588,10 @@ CLI.sendSignalToProcessName = function(signal, process_name, cb) { process_name : process_name }, function(err, list) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } - printOut('successfully sent signal %s to process name %s', signal, process_name); + Common.printOut('successfully sent signal %s to process name %s', signal, process_name); return cb ? cb(null, list) : speedList(); }); } @@ -1677,10 +1610,10 @@ CLI.sendSignalToProcessId = function(signal, process_id, cb) { process_id : process_id }, function(err, list) { if (err) { - printError(err); - return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT); + Common.printError(err); + return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); } - printOut('successfully sent signal %s to process id %s', signal, process_id); + Common.printOut('successfully sent signal %s to process id %s', signal, process_id); return cb ? cb(null, list) : speedList(); }); } @@ -1701,7 +1634,7 @@ CLI.monit = function(cb) { if (err) { console.error('Error retrieving process list: ' + err); - exitCli(cst.ERROR_EXIT); + Common.exitCli(cst.ERROR_EXIT); } Monit.refresh(list); @@ -1755,14 +1688,14 @@ CLI.streamLogs = function(id, lines, raw, timestamp, exclusive) { // Get the list of all running apps Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { - printError(err); - exitCli(cst.ERROR_EXIT); + Common.printError(err); + Common.exitCli(cst.ERROR_EXIT); } if (lines === 0) return Log.stream(id, raw, timestamp, exclusive); if (!raw) - printOut(chalk['inverse'](util.format.call(this, '[PM2] Tailing last %d lines for [%s] process%s', lines, id, id === 'all' ? 'es' : '', '\n'))); + Common.printOut(chalk['inverse'](util.format.call(this, '[PM2] Tailing last %d lines for [%s] process%s', lines, id, id === 'all' ? 'es' : '', '\n'))); // Populate the array `files_list` with the paths of all files we need to tail list.forEach(function(proc) { @@ -1810,18 +1743,18 @@ CLI.streamLogs = function(id, lines, raw, timestamp, exclusive) { * @return */ CLI.killDaemon = CLI.kill = function(cb) { - printOut(cst.PREFIX_MSG + 'Stopping PM2...'); + Common.printOut(cst.PREFIX_MSG + 'Stopping PM2...'); Satan.executeRemote('notifyKillPM2', {}, function() {}); CLI.killAllModules(function() { CLI._operate('deleteProcessId', 'all', function(err, list) { - printOut(cst.PREFIX_MSG + 'All processes have been stopped and deleted'); + Common.printOut(cst.PREFIX_MSG + 'All processes have been stopped and deleted'); InteractorDaemonizer.killDaemon(function(err, data) { Satan.killDaemon(function(err, res) { - if (err) printError(err); - printOut(cst.PREFIX_MSG + 'PM2 stopped'); - return cb ? cb(err, res) : exitCli(cst.SUCCESS_EXIT); + if (err) Common.printError(err); + Common.printOut(cst.PREFIX_MSG + 'PM2 stopped'); + return cb ? cb(err, res) : Common.exitCli(cst.SUCCESS_EXIT); }); }); }); @@ -1873,8 +1806,8 @@ CLI.publish = function(module_name, cb) { CLI.generateModuleSample = function(app_name, cb) { Modularizer.generateSample(app_name, function(err, data) { if (err) - return cb ? cb(err) : exitCli(cst.ERROR_EXIT); - return cb ? cb(null, data) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb(err) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(null, data) : Common.exitCli(cst.SUCCESS_EXIT); }); } @@ -1893,7 +1826,7 @@ CLI.deleteModule = function(module_name, cb) { Common.getAllProcess(function(err, procs) { if (err) { - Common.printError('Error retrieving process list: ' + err); + Common.Common.printError('Error retrieving process list: ' + err); return cb(err); } @@ -1929,20 +1862,7 @@ function displayConf(target_app, cb) { }); } -CLI.get = function(key, cb) { - if (!key || key == 'all') { - displayConf(function(err, data) { - if (err) - return cb ? cb({success:false, err:err}) : exitCli(cst.ERROR_EXIT); - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); - }); - return false; - } - Configuration.get(key, function(err, data) { - Common.printOut(data); - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); - }); -} +CLI var Password = require('./Interactor/Password.js'); @@ -1957,18 +1877,18 @@ CLI.set = function(key, value, cb) { value = Password.generate(value); Configuration.set(key, value, function(err) { if (err) - return cb ? cb({success:false, err:err }) : exitCli(cst.ERROR_EXIT); + return cb ? cb({success:false, err:err }) : Common.exitCli(cst.ERROR_EXIT); InteractorDaemonizer.launchRPC(function(err) { if (err) { displayConf('pm2', function() { - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); return false; } InteractorDaemonizer.rpc.passwordSet(function() { InteractorDaemonizer.disconnectRPC(function() { displayConf('pm2', function() { - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); }); }); @@ -1983,7 +1903,7 @@ CLI.set = function(key, value, cb) { */ Configuration.set(key, value, function(err) { if (err) - return cb ? cb({success:false, err:err }) : exitCli(cst.ERROR_EXIT); + return cb ? cb({success:false, err:err }) : Common.exitCli(cst.ERROR_EXIT); var values = []; @@ -2003,13 +1923,13 @@ CLI.set = function(key, value, cb) { if (!err) Common.printOut(cst.PREFIX_MSG + 'Module %s restarted', app_name); displayConf(app_name, function() { - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); }); return false; } displayConf(app_name, function() { - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); }); } @@ -2017,7 +1937,7 @@ CLI.set = function(key, value, cb) { CLI.multiset = function(serial, cb) { Configuration.multiset(serial, function(err, data) { if (err) - return cb ? cb({success:false, err:err}) : exitCli(cst.ERROR_EXIT); + return cb ? cb({success:false, err:err}) : Common.exitCli(cst.ERROR_EXIT); var values = []; var key = serial.match(/(?:[^ "]+|"[^"]*")+/g)[0]; @@ -2038,13 +1958,13 @@ CLI.multiset = function(serial, cb) { if (!err) Common.printOut(cst.PREFIX_MSG + 'Module %s restarted', app_name); displayConf(app_name, function() { - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); }); return false; } displayConf(app_name, function() { - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); }); @@ -2053,11 +1973,11 @@ CLI.multiset = function(serial, cb) { CLI.unset = function(key, cb) { Configuration.unset(key, function(err) { if (err) { - return cb ? cb({success:false, err:err }) : exitCli(cst.ERROR_EXIT); + return cb ? cb({success:false, err:err }) : Common.exitCli(cst.ERROR_EXIT); } displayConf(function() { - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); }); } @@ -2072,23 +1992,23 @@ CLI.conf = function(key, value, cb) { if (key && value) { CLI.set(key, value, function(err) { if (err) - return cb ? cb({success:false, err:err}) : exitCli(cst.ERROR_EXIT); - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb({success:false, err:err}) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); } // If only key = get else if (key) { CLI.get(key, function(err, data) { if (err) - return cb ? cb({success:false, err:err}) : exitCli(cst.ERROR_EXIT); - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb({success:false, err:err}) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); } else { displayConf(function(err, data) { if (err) - return cb ? cb({success:false, err:err}) : exitCli(cst.ERROR_EXIT); - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb({success:false, err:err}) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); } } @@ -2116,8 +2036,8 @@ CLI.interact = function(secret_key, public_key, machine_name, recycle, cb) { recycle : recycle || null }, function(err, dt) { if (err) - return cb ? cb(err) : exitCli(cst.ERROR_EXIT); - return cb ? cb(null, dt) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb(err) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(null, dt) : Common.exitCli(cst.SUCCESS_EXIT); }); } @@ -2127,7 +2047,7 @@ CLI.interact = function(secret_key, public_key, machine_name, recycle, cb) { */ CLI.killInteract = function(cb) { InteractorDaemonizer.killDaemon(function(err) { - return cb ? cb({msg:'Interactor not launched'}) : exitCli(cst.SUCCESS_EXIT); + return cb ? cb({msg:'Interactor not launched'}) : Common.exitCli(cst.SUCCESS_EXIT); }); } @@ -2138,11 +2058,11 @@ CLI.killInteract = function(cb) { CLI.infoInteract = function(cb) { getInteractInfo(function(err, data) { if (err) { - printError('Interactor not launched'); - return cb ? cb({msg:'Interactor not launched'}) : exitCli(cst.ERROR_EXIT); + Common.printError('Interactor not launched'); + return cb ? cb({msg:'Interactor not launched'}) : Common.exitCli(cst.ERROR_EXIT); } - printOut(data); - return cb ? cb(null, data) : exitCli(cst.SUCCESS_EXIT); + Common.printOut(data); + return cb ? cb(null, data) : Common.exitCli(cst.SUCCESS_EXIT); }); } @@ -2213,11 +2133,11 @@ CLI.forward = Version.forward; CLI.forceGc = CLI.gc = function(cb) { Satan.executeRemote('forceGc', {}, function(err, data) { if (data && data.success === false) { - printError(cst.PREFIX_MSG_ERR + 'Garbage collection failed'); - return cb ? cb({success:false}) : exitCli(cst.ERROR_EXIT); + Common.printError(cst.PREFIX_MSG_ERR + 'Garbage collection failed'); + return cb ? cb({success:false}) : Common.exitCli(cst.ERROR_EXIT); } else { - printOut(cst.PREFIX_MSG + 'Garbage collection manually triggered'); - return cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); + Common.printOut(cst.PREFIX_MSG + 'Garbage collection manually triggered'); + return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); } }); } @@ -2228,14 +2148,14 @@ CLI.forceGc = CLI.gc = function(cb) { * @method deepUpdate */ CLI.deepUpdate = function(cb) { - printOut(cst.PREFIX_MSG + 'Updating PM2...'); + Common.printOut(cst.PREFIX_MSG + 'Updating PM2...'); var exec = require('shelljs').exec; var child = exec("npm i -g pm2@latest; pm2 update", {async : true}); child.stdout.on('end', function() { - printOut(cst.PREFIX_MSG + 'PM2 successfully updated'); - cb ? cb(null, {success:true}) : exitCli(cst.SUCCESS_EXIT); + Common.printOut(cst.PREFIX_MSG + 'PM2 successfully updated'); + cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); } @@ -2302,25 +2222,25 @@ var speedList = CLI.speedList = function(code) { return setTimeout(speedList, 1400); } console.error('Error retrieving process list: %s.\nA process seems to be on infinite loop, retry in 5 seconds',err); - return exitCli(cst.ERROR_EXIT); + return Common.exitCli(cst.ERROR_EXIT); } if (commander.miniList && !commander.silent) UX.miniDisplay(list); else if (!commander.silent) { if (gl_interact_infos) { - printOut(chalk.green.bold('●') + ' Agent online - public key: %s - machine name: %s - Web access: https://app.keymetrics.io/', gl_interact_infos.public_key, gl_interact_infos.machine_name); + Common.printOut(chalk.green.bold('●') + ' Agent online - public key: %s - machine name: %s - Web access: https://app.keymetrics.io/', gl_interact_infos.public_key, gl_interact_infos.machine_name); } UX.dispAsTable(list, gl_interact_infos); - printOut(chalk.white.italic(' Use `pm2 show ` to get more details about an app')); + Common.printOut(chalk.white.italic(' Use `pm2 show ` to get more details about an app')); } if (Satan._noDaemonMode) { - printOut('--no-daemon option enabled = do not exit pm2 CLI'); - printOut('PM2 daemon PID = %s', fs.readFileSync(cst.PM2_PID_FILE_PATH)); + Common.printOut('--no-daemon option enabled = do not exit pm2 CLI'); + Common.printOut('PM2 daemon PID = %s', fs.readFileSync(cst.PM2_PID_FILE_PATH)); return CLI.streamLogs('all', 0, false, 'HH:mm:ss', false); } else { - return exitCli(code ? code : cst.SUCCESS_EXIT); + return Common.exitCli(code ? code : cst.SUCCESS_EXIT); } }); } @@ -2378,7 +2298,7 @@ function mergeEnvironmentVariables(app, envName, deployConf) { function resolvePaths(appConf) { var app = Common.prepareAppConf(appConf); if (app instanceof Error) { - printError(cst.PREFIX_MSG_ERR + app.message); + Common.printError(cst.PREFIX_MSG_ERR + app.message); throw new Error(app.message); } return app; @@ -2478,12 +2398,6 @@ function checkDeprecates(conf){ // Sanity check, default to number of cores if value can't be parsed if (typeof(conf.instances) === 'string') conf.instances = parseInt(conf.instances) || 0; - - // Ensure instance param is not a negative value - // if (conf.instances < 0) { - // warn('You passed a negative value to indicate the number of instances... Setting this to maximum instances.'); - // conf.instances = 0; - // } } /** @@ -2500,31 +2414,10 @@ function prepareAppName(conf){ } } -/** - * Parses a config file like ecosystem.json. Supported formats: JS, JSON, JSON5. - * @param {string} confString contents of the config file - * @param {string} filename path to the config file - * @return {Object} config object - */ -function parseConfig(confString, filename) { - var code = '(' + confString + ')'; - var sandbox = {}; - if (semver.satisfies(process.version, '>= 0.12.0')) { - return vm.runInThisContext(code, sandbox, { - filename: path.resolve(filename), - displayErrors: false, - timeout: 1000, - }); - } else { - // Use the Node 0.10 API - return vm.runInNewContext(code, sandbox, filename); - } -} - /** * Show warnings * @param {String} warning */ function warn(warning){ - printOut(cst.PREFIX_MSG_WARNING + warning); + Common.printOut(cst.PREFIX_MSG_WARNING + warning); } diff --git a/lib/Utility.js b/lib/Utility.js index 55e27713..9091270e 100644 --- a/lib/Utility.js +++ b/lib/Utility.js @@ -6,6 +6,9 @@ var cst = require('../constants.js'); var async = require('async'); var util = require('util'); +var vm = require('vm'); +var semver = require('semver'); + var Utility = module.exports = { getDate : function() { return Date.now(); @@ -128,5 +131,25 @@ var Utility = module.exports = { })(types.splice(0, 1)); async.waterfall(flows, callback); + }, + /** + * Parses a config file like ecosystem.json. Supported formats: JS, JSON, JSON5. + * @param {string} confString contents of the config file + * @param {string} filename path to the config file + * @return {Object} config object + */ + parseConfig : function(confString, filename) { + var code = '(' + confString + ')'; + var sandbox = {}; + if (semver.satisfies(process.version, '>= 0.12.0')) { + return vm.runInThisContext(code, sandbox, { + filename: path.resolve(filename), + displayErrors: false, + timeout: 1000 + }); + } else { + // Use the Node 0.10 API + return vm.runInNewContext(code, sandbox, filename); + } } };