Skip to content

Commit

Permalink
Merge pull request #3784 from Unitech/pm2-plus-cli
Browse files Browse the repository at this point in the history
feature: pm2 plus cli
  • Loading branch information
Unitech committed Jul 11, 2018
2 parents 466d270 + 1da6edd commit e8c13c3
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 93 deletions.
45 changes: 38 additions & 7 deletions bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,6 @@ commander.command('report')
// PM2 I/O
//
commander.command('link [secret] [public] [name]')
.alias('interact')
.option('--info-node [url]', 'set url info node')
.option('--ws', 'websocket mode')
.description('link with the pm2 monitoring dashboard')
Expand Down Expand Up @@ -595,15 +594,47 @@ commander.command('open')
pm2.openDashboard();
});

commander.command('register')
.alias('plus')
.description('register on pm2 monitoring')
.action(function(name) {
pm2.registerToKM();
commander.command('plus [secret] [public] [name]')
.alias('register')
.option('--info-node [url]', 'set url info node for on-premise pm2 plus')
.option('-d --discrete', 'silent mode')
.option('-a --install-all', 'install all modules (force yes)')
.description('enable pm2 plus')
.action(function(sec, pub, name, _opts) {
var opts = {
infoNode: _opts.infoNode,
discrete: _opts.discrete,
installAll: _opts.installAll,
public: pub,
secret: sec,
name: name,
ws: true
}

if (sec == 'delete') {
return pm2._pre_interact('delete', null, null, opts, function(err, dt) {
pm2.clearSetup(opts, function() {
console.log(chalk.green('[PM2.IO]') + ' Agent disabled, modules removed, monitoring disabled');
setTimeout(function() {
pm2.speedList()
}, 1000)
})
})
}

if (opts.public && opts.secret)
pm2._pre_interact(opts.secret, opts.public, opts.name, opts, function(err, dt) {
pm2.minimumSetup(opts, function() {
console.log(chalk.green('[PM2.IO]') + ' Remote dashboard: https://app.pm2.io/#/r/' + dt.public_key);
pm2.speedList()
})
})
else
pm2.registerToKM(opts);
});

commander.command('login')
.description('use login to link with the pm2 monitoring dashboard')
.description('[DEPRECATED] use plus command instead')
.action(function(name) {
pm2.loginToKM();
});
Expand Down
2 changes: 1 addition & 1 deletion lib/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,7 @@ class API {
UX.miniDisplay(list);
else if (!commander.silent) {
if (that.gl_interact_infos) {
Common.printOut('%s Agent Online | Access: %s | Server: %s | Transport %s',
Common.printOut('%s PM2+ activated | Web: %s | Server: %s | Transport %s',
chalk.green.bold('⇆'),
chalk.bold('https://app.pm2.io/#/r/' + that.gl_interact_infos.public_key),
chalk.bold(that.gl_interact_infos.machine_name),
Expand Down
6 changes: 1 addition & 5 deletions lib/API/CliUx.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,10 @@ UX.dispAsTable = function(list, commander) {
if (l.pm2_env.axm_options) {
var deep_monitored = l.pm2_env.axm_options.tracing_enabled || false;
if (deep_monitored == true) {
key = key + ' ' + chalk.green('⏱');
key = chalk.green('✚') + ' ' + key;
}
}

if (typeof l.pm2_env._km_monitored === 'boolean' && l.pm2_env._km_monitored === false) {
key = chalk.bold.red('◉') + ' ' + key;
}

if (l.pm2_env.pmx_module == true) {
// pm2 ls for Modules
obj[key] = [];
Expand Down
48 changes: 24 additions & 24 deletions lib/API/Interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,21 @@ module.exports = function(CLI) {
//
// Interact
//
CLI.prototype._pre_interact = function(cmd, public_key, machine, opts) {
CLI.prototype._pre_interact = function(cmd, public_key, machine, opts, cb) {
var that = this;

if (cmd == 'stop' || cmd == 'kill') {
that.gl_is_km_linked = false
console.log(chalk.cyan('[PM2 agent]') + ' Stopping agent...');
that.killInteract(function() {
that.killInteract(function(err) {
if (err) {
Common.printError(err);
return process.exit(cst.ERROR_EXIT);
}
console.log(chalk.cyan('[PM2 agent]') + ' Stopped');
return process.exit(cst.SUCCESS_EXIT);
that.reload('all', () => {
return process.exit(cst.SUCCESS_EXIT);
})
});
return false;
}
Expand All @@ -102,35 +109,23 @@ module.exports = function(CLI) {
}

if (cmd == 'delete') {
that.killInteract(function() {
that.gl_is_km_linked = false
console.log(chalk.cyan('[PM2 agent]') + ' Permanently disable agent...');
that.killInteract(function(err) {
try {
fs.unlinkSync(cst.INTERACTION_CONF);
} catch(e) {
console.log(chalk.cyan('[PM2 agent]') + ' No interaction config file found');
return process.exit(cst.SUCCESS_EXIT);
}
console.log(chalk.cyan('[PM2 agent]') + ' Agent interaction ended');
return process.exit(cst.SUCCESS_EXIT);
if (!cb)
return process.exit(cst.SUCCESS_EXIT);
return cb()
});
return false;
}

if (cmd == 'start' || cmd == 'restart') {
KMDaemon.launchAndInteract(that._conf, {
public_key : null,
secret_key : null,
machine_name : null,
info_node : null,
pm2_version: pkg.version
}, function(err, dt) {
if (err) {
Common.printError(err);
return that.exitCli(cst.ERROR_EXIT);
}
return that.exitCli(cst.SUCCESS_EXIT);
});
}

if (cmd && !public_key) {
console.error(chalk.cyan('[PM2 agent]') + ' Command [%s] unknown or missing public key', cmd);
return process.exit(cst.ERROR_EXIT);
Expand Down Expand Up @@ -162,11 +157,16 @@ module.exports = function(CLI) {
process.env.AGENT_TRANSPORT_AXON = true
process.env.AGENT_TRANSPORT_WEBSOCKET = false
}

KMDaemon.launchAndInteract(that._conf, infos, function(err, dt) {
if (err)
if (err) {
Common.printError(chalk.red('[PM2 agent] ') + err)
Common.printError(chalk.cyan('[PM2 agent] ') + 'Run `$ pm2 plus` to connect')
return that.exitCli(cst.ERROR_EXIT);
return that.exitCli(cst.SUCCESS_EXIT);
}
if (!cb) {
return that.exitCli(cst.SUCCESS_EXIT);
}
return cb(null, dt)
});
};

Expand Down
12 changes: 6 additions & 6 deletions lib/API/Modules/Modularizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Modularizer = module.exports = {};

var MODULE_CONF_PREFIX = 'module-db-v2';

var KNOWN_MODULES = {
var INTERNAL_MODULES = {
'deep-monitoring': {
dependencies: [{name: 'v8-profiler-node8'}, {name: 'gc-stats'}, {name: 'event-loop-inspector'}]
},
Expand Down Expand Up @@ -68,19 +68,19 @@ Modularizer.install = function (CLI, moduleName, opts, cb) {
return cb(Common.retErr(e));
}

Modularizer.installMultipleModules(config.dependencies, cb);
Modularizer.installMultipleInternalModules(config.dependencies, cb);
return;
}

Common.printOut(cst.PREFIX_MSG_MOD + 'Installing module ' + moduleName);

var canonicModuleName = Utility.getCanonicModuleName(moduleName);

if (KNOWN_MODULES.hasOwnProperty(moduleName)) {
var currentModule = KNOWN_MODULES[moduleName];
if (INTERNAL_MODULES.hasOwnProperty(moduleName)) {
var currentModule = INTERNAL_MODULES[moduleName];

if (currentModule && currentModule.hasOwnProperty('dependencies')) {
Modularizer.installMultipleModules(currentModule.dependencies, cb);
Modularizer.installMultipleInternalModules(currentModule.dependencies, cb);
} else {
installModuleByName(currentModule, cb);
}
Expand Down Expand Up @@ -109,7 +109,7 @@ Modularizer.install = function (CLI, moduleName, opts, cb) {
});
};

Modularizer.installMultipleModules = function (modules, cb) {
Modularizer.installMultipleInternalModules = function (modules, cb) {
var functionList = [];
for (var i = 0; i < modules.length; i++) {
functionList.push((function (index) {
Expand Down
Loading

0 comments on commit e8c13c3

Please sign in to comment.