Skip to content

Commit

Permalink
Merge pull request #3799 from Unitech/refactor-agent
Browse files Browse the repository at this point in the history
refactor: split / remove unused code / DRY / pm2 enterprise|plus - br…
  • Loading branch information
Unitech committed Jul 16, 2018
2 parents 88551b8 + 89e2a12 commit bcc4fea
Show file tree
Hide file tree
Showing 15 changed files with 601 additions and 605 deletions.
102 changes: 59 additions & 43 deletions bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ commander.command('link [secret] [public] [name]')
.option('--info-node [url]', 'set url info node')
.option('--ws', 'websocket mode')
.description('link with the pm2 monitoring dashboard')
.action(pm2._pre_interact.bind(pm2));
.action(pm2.linkManagement.bind(pm2));

commander.command('unlink')
.description('unlink with the pm2 monitoring dashboard')
Expand All @@ -594,59 +594,75 @@ commander.command('open')
pm2.openDashboard();
});

function connect(type, sec, pub, name, _opts) {
var opts = {
infoNode: _opts.infoNode,
discrete: _opts.discrete,
installAll: _opts.installAll,
public: pub,
secret: sec,
name: name,
type: type,
ws: true
}

if (sec == 'link' || sec == 'relink' || (sec == null && pub == null && pm2.gl_is_km_linked)) {
return pm2.linkManagement(null, null, null, opts, function(err, dt) {
pm2.speedList()
})
}

if (sec == 'delete') {
return pm2.linkManagement('delete', null, null, opts, function(err, dt) {
pm2.clearSetup(opts, function() {
console.log(cst.PM2_IO_MSG + 'Agent disabled, modules removed, monitoring disabled');
setTimeout(function() {
pm2.speedList()
}, 300)
})
})
}

if (opts.public && opts.secret)
return pm2.linkManagement(opts.secret, opts.public, opts.name, opts, function(err, dt) {
pm2.minimumSetup(opts, function() {
console.log(cst.PM2_IO_MSG + 'Remote dashboard: https://app.pm2.io/#/r/' + dt.public_key);
pm2.speedList()
})
})

pm2.register(opts);
}

commander.command('enterprise [secret] [public] [name]')
.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 enterprise')
.action(function(sec, pub, name, _opts) {
connect('enterprise', sec, pub, name, _opts)
})

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,
gen_uid: _opts.gen,
public: pub,
secret: sec,
name: name,
ws: true
}

if (!name)
opts.name = require('os').hostname() + '-' + require('crypto').randomBytes(2).toString('hex')

if (sec == 'link' || sec == 'relink') {
return pm2._pre_interact(null, null, null, opts, function(err, dt) {
pm2.speedList()
})
}

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()
}, 300)
})
})
}

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);
connect('plus', sec, pub, name, _opts)
});

commander.command('login')
.description('[DEPRECATED] use plus command instead')
.description('Login to pm2 plus')
.action(function(name) {
connect('plus', null, null, null, {})
});

commander.command('logout')
.description('Logout from pm2 plus')
.action(function(name) {
pm2.loginToKM();
connect('plus', 'delete', null, null, {})
});

//
Expand Down
7 changes: 5 additions & 2 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ var csts = {
PREFIX_MSG_WARNING : chalk.yellow('[PM2][WARN] '),
PREFIX_MSG_SUCCESS : chalk.cyan('[PM2] '),

PM2_IO_MSG : chalk.cyan('[PM2 I/O] '),
PM2_IO_MSG_ERR : chalk.red('[PM2 I/O] '),

TEMPLATE_FOLDER : p.join(__dirname, 'lib/templates'),

APP_CONF_DEFAULT_FILE : 'ecosystem.config.js',
Expand Down Expand Up @@ -60,8 +63,8 @@ var csts = {
KEYMETRICS_ROOT_URL : process.env.KEYMETRICS_NODE || process.env.ROOT_URL || process.env.INFO_NODE || 'root.keymetrics.io',


KEYMETRICS_BANNER : '../lib/motd',
KEYMETRICS_UPDATE : '../lib/motd.update',
PM2_BANNER : '../lib/motd',
PM2_UPDATE : '../lib/API/pm2-plus/pres/motd.update',
DEFAULT_MODULE_JSON : 'package.json',

REMOTE_PORT_TCP : isNaN(parseInt(process.env.KEYMETRICS_PUSH_PORT)) ? 80 : parseInt(process.env.KEYMETRICS_PUSH_PORT),
Expand Down
12 changes: 8 additions & 4 deletions lib/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ class API {
that.getVersion(function(err, new_version) {
// If not linked to PM2 plus, and update PM2 to latest, display motd.update
if (!that.gl_is_km_linked && !err && (pkg.version != new_version)) {
var dt = fs.readFileSync(path.join(__dirname, that._conf.KEYMETRICS_UPDATE));
var dt = fs.readFileSync(path.join(__dirname, that._conf.PM2_UPDATE));
console.log(dt.toString());
}

Expand Down Expand Up @@ -604,7 +604,7 @@ class API {
Common.printOut(conf.PREFIX_MSG + 'All processes have been stopped and deleted');
process.env.PM2_SILENT = 'false';

that.killInteract(function(err, data) {
that.killAgent(function(err, data) {
that.Client.killDaemon(function(err, res) {
if (err) Common.printError(err);
Common.printOut(conf.PREFIX_MSG + 'PM2 stopped');
Expand Down Expand Up @@ -1671,10 +1671,14 @@ class API {
//////////////////////////

require('./API/Extra.js')(API);
require('./API/Interaction.js')(API);
require('./API/Deploy.js')(API);
require('./API/Modules/Modules.js')(API);
require('./API/PM2/PM2IO.js')(API);

require('./API/pm2-plus/link.js')(API);
require('./API/pm2-plus/process-selector.js')(API);
require('./API/pm2-plus/helpers.js')(API);
require('./API/pm2-plus/PM2IO.js')(API);

require('./API/Configuration.js')(API);
require('./API/Version.js')(API);
require('./API/Startup.js')(API);
Expand Down
183 changes: 0 additions & 183 deletions lib/API/Interaction.js

This file was deleted.

Loading

0 comments on commit bcc4fea

Please sign in to comment.