From 565fc77e6ec2f8d5cc119b2eaa944da8082078cc Mon Sep 17 00:00:00 2001 From: Unitech Date: Fri, 28 Sep 2018 21:17:15 +0200 Subject: [PATCH] add command to display application environment --- bin/pm2 | 6 ++++++ lib/API/CliUx.js | 1 + lib/API/Extra.js | 27 +++++++++++++++++++++++++++ lib/API/Modules/Modularizer.js | 10 +++++----- lib/API/Modules/NPM.js | 2 +- lib/API/Modules/TAR.js | 34 +++++++++++++++++----------------- lib/API/Modules/index.js | 4 ++-- lib/Common.js | 14 +++++++++++++- 8 files changed, 72 insertions(+), 26 deletions(-) diff --git a/bin/pm2 b/bin/pm2 index bf925688..945f1087 100755 --- a/bin/pm2 +++ b/bin/pm2 @@ -796,6 +796,12 @@ commander.command('info ') pm2.describe(proc_id); }); +commander.command('env ') + .description('(alias) describe all parameters of a process id') + .action(function(proc_id) { + pm2.env(proc_id); + }); + commander.command('show ') .description('(alias) describe all parameters of a process id') .action(function(proc_id) { diff --git a/lib/API/CliUx.js b/lib/API/CliUx.js index 92a20ffe..b54445c3 100644 --- a/lib/API/CliUx.js +++ b/lib/API/CliUx.js @@ -191,6 +191,7 @@ UX.describeTable = function(process) { Common.printOut(chalk.white.italic(' Add your own code metrics: http://bit.ly/code-metrics')); Common.printOut(chalk.white.italic(' Use `pm2 logs %s [--lines 1000]` to display logs'), pm2_env.name); + Common.printOut(chalk.white.italic(' Use `pm2 env %s` to display environement variables'), pm2_env.pm_id); Common.printOut(chalk.white.italic(' Use `pm2 monit` to monitor CPU and Memory usage'), pm2_env.name); }; diff --git a/lib/API/Extra.js b/lib/API/Extra.js index c3075349..57651015 100644 --- a/lib/API/Extra.js +++ b/lib/API/Extra.js @@ -30,6 +30,33 @@ module.exports = function(CLI) { }); }; + /** + * Show application environment + * @method env + * @callback cb + */ + CLI.prototype.env = function(app_id, cb) { + var procs = [] + var printed = 0 + + this.Client.executeRemote('getMonitorData', {}, (err, list) => { + list.forEach(l => { + if (app_id == l.pm_id) { + printed++ + Object.keys(l.pm2_env.env).forEach(key => { + console.log(`${key}: ${chalk.green(l.pm2_env.env[key])}`) + }) + } + }) + + if (printed == 0) { + Common.err(`Modules with id ${app_id} not found`) + return cb ? cb.apply(null, arguments) : this.exitCli(cst.ERROR_EXIT); + } + return cb ? cb.apply(null, arguments) : this.exitCli(cst.SUCCESS_EXIT); + }) + }; + /** * Get version of the daemonized PM2 * @method getVersion diff --git a/lib/API/Modules/Modularizer.js b/lib/API/Modules/Modularizer.js index c857201b..c9df6fbe 100644 --- a/lib/API/Modules/Modularizer.js +++ b/lib/API/Modules/Modularizer.js @@ -26,7 +26,7 @@ Modularizer.install = function (CLI, module_name, opts, cb) { } if (LOCAL.INTERNAL_MODULES.hasOwnProperty(module_name)) { - Common.log(`Adding dependency ${module_name} to PM2 Runtime`); + Common.logMod(`Adding dependency ${module_name} to PM2 Runtime`); var currentModule = LOCAL.INTERNAL_MODULES[module_name]; if (currentModule && currentModule.hasOwnProperty('dependencies')) { LOCAL.installMultipleModules(currentModule.dependencies, cb); @@ -35,15 +35,15 @@ Modularizer.install = function (CLI, module_name, opts, cb) { } } else if (module_name == '.') { - Common.log(`Installing local NPM module`); + Common.logMod(`Installing local NPM module`); return NPM.localStart(CLI, opts, cb) } else if (opts.tarball || module_name.indexOf('.tar.gz') > -1) { - Common.log(`Installing TAR module`); + Common.logMod(`Installing TAR module`); TAR.install(CLI, module_name, opts, cb) } else { - Common.log(`Installing NPM ${module_name} module`); + Common.logMod(`Installing NPM ${module_name} module`); NPM.install(CLI, module_name, opts, cb) } }; @@ -114,7 +114,7 @@ Modularizer.uninstall = function(CLI, module_name, cb) { TAR.uninstall(CLI, module_name, cb) } else { - Common.err('Unknown module') + Common.errMod('Unknown module') CLI.exitCli(1) } }; diff --git a/lib/API/Modules/NPM.js b/lib/API/Modules/NPM.js index c391ee61..c703e892 100644 --- a/lib/API/Modules/NPM.js +++ b/lib/API/Modules/NPM.js @@ -159,7 +159,7 @@ function moduleExistInLocalDB(CLI, module_name, cb) { function install(CLI, module_name, opts, cb) { moduleExistInLocalDB(CLI, module_name, function (exists) { if (exists) { - Common.log('Module already installed. Updating.'); + Common.logMod('Module already installed. Updating.'); Rollback.backup(module_name); diff --git a/lib/API/Modules/TAR.js b/lib/API/Modules/TAR.js index 953043a5..001a0396 100644 --- a/lib/API/Modules/TAR.js +++ b/lib/API/Modules/TAR.js @@ -29,7 +29,7 @@ module.exports = { */ function install(PM2, module_filepath, opts, cb) { - Common.log(`Unpacking local tarball ${module_filepath}`) + Common.logMod(`Unpacking local tarball ${module_filepath}`) // Remote file retrieval if (module_filepath.includes('http') === true) { @@ -38,7 +38,7 @@ function install(PM2, module_filepath, opts, cb) { return retrieveRemote(module_filepath, target_filepath, (err) => { if (err) { - Common.err(err) + Common.errMod(err) process.exit(1) } installLocal(PM2, target_filepath, opts, cb) @@ -69,7 +69,7 @@ function installLocal(PM2, module_filepath, opts, cb) { getModuleName(module_filepath, function(err, module_name) { if (err) return cb(err) - Common.log(`Module name ${module_name} being installed`) + Common.logMod(`Module name ${module_name} being installed`) var install_path = path.join(cst.DEFAULT_MODULE_PATH, module_name); @@ -94,7 +94,7 @@ function installLocal(PM2, module_filepath, opts, cb) { } function runInstall(PM2, target_path, module_name, code, cb) { - Common.log(`Module unpacked in ${target_path}`) + Common.logMod(`Module unpacked in ${target_path}`) var config_file = path.join(target_path, 'package.json') var conf @@ -103,7 +103,7 @@ function runInstall(PM2, target_path, module_name, code, cb) { conf = require(config_file) module_name = conf.name } catch(e) { - Common.err(new Error('Cannot find package.json file with name attribute at least')); + Common.errMod(new Error('Cannot find package.json file with name attribute at least')); } var opts = {} @@ -123,7 +123,7 @@ function runInstall(PM2, target_path, module_name, code, cb) { installed_at: Date.now() }) - Common.log(`Module INSTALLED and STARTED`) + Common.logMod(`Module INSTALLED and STARTED`) return cb(null, 'Module installed & Starter') }) } @@ -166,12 +166,12 @@ function start(PM2, module_name, cb) { function uninstall(PM2, module_name, cb) { var module_path = path.join(cst.DEFAULT_MODULE_PATH, module_name); - Common.log(`Removing ${module_name} from auto startup`) + Common.logMod(`Removing ${module_name} from auto startup`) try { var pkg = require(path.join(module_path, 'package.json')) } catch(e) { - Common.err('Could not retrieve module package.json'); + Common.errMod('Could not retrieve module package.json'); return cb(e) } @@ -232,7 +232,7 @@ function package(module_path, target_path, cb) { var cmd = `tar zcf ${target_fullpath} -C ${base_folder} --transform 's,${module_folder_name},module,' ${module_folder_name}` - Common.log(`Gziping ${module_path} to ${target_fullpath}`) + Common.logMod(`Gziping ${module_path} to ${target_fullpath}`) var tar = exec(cmd, (err, sto, ste) => { if (err) { @@ -255,7 +255,7 @@ function publish(PM2, folder, cb) { try { var pkg = JSON.parse(fs.readFileSync(path.join(target_folder, 'package.json')).toString()) } catch(e) { - Common.err(`${process.cwd()} module does not contain any package.json`) + Common.errMod(`${process.cwd()} module does not contain any package.json`) process.exit(1) } @@ -267,15 +267,15 @@ function publish(PM2, folder, cb) { var module_name = path.basename(current_path) var target_path = os.tmpdir() - Common.log(`Starting publishing procedure for ${module_name}@${pkg.version}`) + Common.logMod(`Starting publishing procedure for ${module_name}@${pkg.version}`) package(current_path, target_path, (err, res) => { if (err) { - Common.err('Can\'t package, exiting') + Common.errMod('Can\'t package, exiting') process.exit(1) } - Common.log(`Package [${pkg.name}] created in path ${res.path}`) + Common.logMod(`Package [${pkg.name}] created in path ${res.path}`) var data = { module_data: { @@ -288,19 +288,19 @@ function publish(PM2, folder, cb) { }; var uri = `${PM2.user_conf.registry}/api/v1/modules` - Common.log(`Sending Package to remote ${pkg.name} ${uri}`) + Common.logMod(`Sending Package to remote ${pkg.name} ${uri}`) require('needle') .post(uri, data, { multipart: true }, function(err, res, body) { if (err) { - Common.err(err) + Common.errMod(err) process.exit(1) } if (res.statusCode !== 200) { - Common.err(`${pkg.name}-${pkg.version}: ${res.body.msg}`) + Common.errMod(`${pkg.name}-${pkg.version}: ${res.body.msg}`) process.exit(1) } - Common.log(`Module ${module_name} published under version ${pkg.version}`) + Common.logMod(`Module ${module_name} published under version ${pkg.version}`) process.exit(0) }) }) diff --git a/lib/API/Modules/index.js b/lib/API/Modules/index.js index 9cf5e67b..14e7db40 100644 --- a/lib/API/Modules/index.js +++ b/lib/API/Modules/index.js @@ -54,10 +54,10 @@ module.exports = function(CLI) { CLI.prototype.package = function(module_path, cb) { Modularizer.package(this, module_path, (err, res) => { if (err) { - Common.err(err) + Common.errMod(err) return cb ? cb(err) : this.exitCli(1) } - Common.log(`Module packaged in ${res.path}`) + Common.logMod(`Module packaged in ${res.path}`) return cb ? cb(err) : this.exitCli(0) }) }; diff --git a/lib/Common.js b/lib/Common.js index 76f8fbd1..cfb4122d 100644 --- a/lib/Common.js +++ b/lib/Common.js @@ -400,13 +400,20 @@ Common.deepCopy = Common.serialize = Common.clone = function(obj) { return fclone(obj); }; -Common.err = function(msg) { +Common.errMod = function(msg) { if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false; if (msg instanceof Error) return console.error(msg.message); return console.error(`${cst.PREFIX_MSG_MOD_ERR}${msg}`); } +Common.err = function(msg) { + if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false; + if (msg instanceof Error) + return console.error(msg.message); + return console.error(`${cst.PREFIX_MSG_ERR}${msg}`); +} + Common.printError = function(msg) { if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false; if (msg instanceof Error) @@ -415,6 +422,11 @@ Common.printError = function(msg) { }; Common.log = function(msg) { + if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false; + return console.log(`${cst.PREFIX_MSG}${msg}`); +} + +Common.logMod = function(msg) { if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false; return console.log(`${cst.PREFIX_MSG_MOD}${msg}`); }