From 28c61716ee5e8f2402205e4b06ed7ee0a942a3cc Mon Sep 17 00:00:00 2001 From: vince Date: Tue, 10 Jul 2018 14:40:19 +0200 Subject: [PATCH 1/3] feature: with pm2 plus command ask to install modules --- lib/API/PM2/PM2IO.js | 49 +++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/lib/API/PM2/PM2IO.js b/lib/API/PM2/PM2IO.js index 9a6633e10..5fa3b6319 100644 --- a/lib/API/PM2/PM2IO.js +++ b/lib/API/PM2/PM2IO.js @@ -12,6 +12,8 @@ const Table = require('cli-table-redemption'); const open = require('../../tools/open.js'); const pkg = require('../../../package.json') const IOAPI = require('@pm2/js-api') +const semver = require('semver'); +const Modularizer = require('../Modules/Modularizer.js'); // const CustomStrategy = require('./custom_auth') @@ -103,20 +105,22 @@ module.exports = function(CLI) { printMotd(); promptly.confirm(chalk.bold('Do you have a pm2.io account? (y/n)'), (err, answer) => { - if (answer == true) { - return this.loginToKM(); - } - CLIAuthStrategy.registerViaCLI((err, data) => { - console.log('[-] Creating Bucket...') - - io.bucket.create({ - name: 'Node.JS Monitoring' - }).then(res => { - const bucket = res.data.bucket - console.log(chalk.bold.green('[+] Bucket created!')) - linkOpenExit(bucket) - }) - }) + + promptly.confirm(chalk.bold('Do you want to install monitoring modules (pm2-logrotate, pm2-server-monit, ...) ? (y/n)'), (errModule, answerModule) => { + if(!errModule && answerModule === true) { + const modules = ['pm2-logrotate', 'pm2-server-monit', 'event-loop-inspector'] + + if (semver.satisfies(process.version, '< 8.0.0')) { + modules.push('v8-profiler-node8') + } + + Modularizer.installMultipleModules(modules, () => { + return KMConnection(answer) + }); + } else { + return KMConnection(answer) + } + }); }); } @@ -242,4 +246,21 @@ module.exports = function(CLI) { var dt = fs.readFileSync(path.join(__dirname, 'motd')); console.log(dt.toString()); } + + function KMConnection(answer) { + if (answer == true) { + return CLI.prototype.loginToKM(); + } + CLIAuthStrategy.registerViaCLI((err, data) => { + console.log('[-] Creating Bucket...') + + io.bucket.create({ + name: 'Node.JS Monitoring' + }).then(res => { + const bucket = res.data.bucket + console.log(chalk.bold.green('[+] Bucket created!')) + linkOpenExit(bucket) + }) + }) + } }; From 68e87b39ae2b57e9fbb0b0abde68112c839f05ee Mon Sep 17 00:00:00 2001 From: vince Date: Tue, 10 Jul 2018 16:38:35 +0200 Subject: [PATCH 2/3] feature: ask to install module after connection with KM --- lib/API/Modules/Modularizer.js | 2 +- lib/API/PM2/PM2IO.js | 81 +++++++++++++++++++--------------- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/lib/API/Modules/Modularizer.js b/lib/API/Modules/Modularizer.js index b8b88e1e9..f54631d4b 100644 --- a/lib/API/Modules/Modularizer.js +++ b/lib/API/Modules/Modularizer.js @@ -137,7 +137,7 @@ Modularizer.installMultipleModules = function (modules, cb) { } } - cb(err); + if(cb) cb(err); }); }; diff --git a/lib/API/PM2/PM2IO.js b/lib/API/PM2/PM2IO.js index 5fa3b6319..c4c424dee 100644 --- a/lib/API/PM2/PM2IO.js +++ b/lib/API/PM2/PM2IO.js @@ -45,7 +45,7 @@ module.exports = function(CLI) { }); }; - CLI.prototype.loginToKM = function() { + CLI.prototype.loginToKM = function(cb) { var promptly = require('promptly') return CLIAuthStrategy._retrieveTokens((err, tokens) => { @@ -83,15 +83,16 @@ module.exports = function(CLI) { } }); - if (target_bucket == null) + if (target_bucket == null) { return retryInsertion(); - linkOpenExit(target_bucket); + } + return linkOpenExit(target_bucket, cb); }); })(); } else { var target_bucket = buckets[0]; - linkOpenExit(target_bucket) + linkOpenExit(target_bucket, cb) } }).catch(err => { console.error(chalk.bold.red(`Oups, a error happened : ${err}`)) @@ -106,21 +107,42 @@ module.exports = function(CLI) { promptly.confirm(chalk.bold('Do you have a pm2.io account? (y/n)'), (err, answer) => { - promptly.confirm(chalk.bold('Do you want to install monitoring modules (pm2-logrotate, pm2-server-monit, ...) ? (y/n)'), (errModule, answerModule) => { - if(!errModule && answerModule === true) { - const modules = ['pm2-logrotate', 'pm2-server-monit', 'event-loop-inspector'] - - if (semver.satisfies(process.version, '< 8.0.0')) { - modules.push('v8-profiler-node8') - } - - Modularizer.installMultipleModules(modules, () => { - return KMConnection(answer) + if (answer == true) { + return CLI.prototype.loginToKM(() => { + promptly.confirm(chalk.bold('Do you want to install monitoring modules (pm2-logrotate, pm2-server-monit, ...) ? (y/n)'), (errModule, answerModule) => { + if(!errModule && answerModule === true) { + const modules = ['pm2-logrotate', 'pm2-server-monit', 'event-loop-inspector'] + + if (semver.satisfies(process.version, '< 8.0.0')) { + modules.push('v8-profiler-node8') + } + + Modularizer.installMultipleModules(modules, () => { + console.log(chalk.bold.green('[+] Exiting now.')) + setTimeout(function() { + process.exit(cst.SUCCESS_EXIT); + }, 100); + }); + } else { + console.log(chalk.bold.green('[+] Exiting now.')) + setTimeout(function() { + process.exit(cst.SUCCESS_EXIT); + }, 100); + } }); - } else { - return KMConnection(answer) - } - }); + }); + } + CLIAuthStrategy.registerViaCLI((err, data) => { + console.log('[-] Creating Bucket...') + + io.bucket.create({ + name: 'Node.JS Monitoring' + }).then(res => { + const bucket = res.data.bucket + console.log(chalk.bold.green('[+] Bucket created!')) + linkOpenExit(bucket) + }) + }) }); } @@ -218,7 +240,7 @@ module.exports = function(CLI) { }; - function linkOpenExit(target_bucket) { + function linkOpenExit(target_bucket, cb) { console.log('[-] Linking local PM2 to newly created bucket...') KMDaemon.launchAndInteract(cst, { public_key : target_bucket.public_id, @@ -231,6 +253,10 @@ module.exports = function(CLI) { setTimeout(function() { open('https://app.pm2.io/#/r/' + target_bucket.public_id); + + if (cb) { + return cb() + } console.log(chalk.bold.green('[+] Opened! Exiting now.')) setTimeout(function() { process.exit(cst.SUCCESS_EXIT); @@ -246,21 +272,4 @@ module.exports = function(CLI) { var dt = fs.readFileSync(path.join(__dirname, 'motd')); console.log(dt.toString()); } - - function KMConnection(answer) { - if (answer == true) { - return CLI.prototype.loginToKM(); - } - CLIAuthStrategy.registerViaCLI((err, data) => { - console.log('[-] Creating Bucket...') - - io.bucket.create({ - name: 'Node.JS Monitoring' - }).then(res => { - const bucket = res.data.bucket - console.log(chalk.bold.green('[+] Bucket created!')) - linkOpenExit(bucket) - }) - }) - } }; From 35a1ed2a1328a859a7797ec8e22024d171599d86 Mon Sep 17 00:00:00 2001 From: vince Date: Tue, 10 Jul 2018 16:52:58 +0200 Subject: [PATCH 3/3] feature: reload all apps after connection to pm2 plus --- lib/API/PM2/PM2IO.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/API/PM2/PM2IO.js b/lib/API/PM2/PM2IO.js index c4c424dee..698e9c067 100644 --- a/lib/API/PM2/PM2IO.js +++ b/lib/API/PM2/PM2IO.js @@ -105,6 +105,8 @@ module.exports = function(CLI) { var promptly = require('promptly'); printMotd(); + const self = this + promptly.confirm(chalk.bold('Do you have a pm2.io account? (y/n)'), (err, answer) => { if (answer == true) { @@ -118,14 +120,16 @@ module.exports = function(CLI) { } Modularizer.installMultipleModules(modules, () => { - console.log(chalk.bold.green('[+] Exiting now.')) + self.reload('all') setTimeout(function() { + console.log(chalk.bold.green('[+] Exiting now.')) process.exit(cst.SUCCESS_EXIT); }, 100); }); } else { - console.log(chalk.bold.green('[+] Exiting now.')) + self.reload('all') setTimeout(function() { + console.log(chalk.bold.green('[+] Exiting now.')) process.exit(cst.SUCCESS_EXIT); }, 100); }