diff --git a/bin/pm2 b/bin/pm2 index 27c133ac..7519235b 100755 --- a/bin/pm2 +++ b/bin/pm2 @@ -967,6 +967,11 @@ commander.command('serve [path] [port]') pm2.serve(path, port || cmd.port, cmd, commander); }); +commander.command('autoinstall') + .action(function() { + pm2.autoinstall() + }) + commander.command('examples') .description('display pm2 usage examples') .action(() => { diff --git a/lib/API/CliUx.js b/lib/API/CliUx.js index 2976db95..727d4e25 100644 --- a/lib/API/CliUx.js +++ b/lib/API/CliUx.js @@ -11,6 +11,7 @@ var Common = require('../Common'); var Spinner = require('./Spinner.js'); var UX = module.exports = {}; var Passwd = require('../tools/passwd.js') +var Configuration = require('../Configuration.js') /** * Description @@ -235,6 +236,8 @@ UX.describeTable = function(proc) { * @return */ UX.dispAsTable = function(list, sys_infos) { + var pm2_conf = Configuration.getSync('pm2') + var condensed_mode = (process.stdout.columns || 300) < 120 var app_head = { id: 4, @@ -469,7 +472,8 @@ UX.dispAsTable = function(list, sys_infos) { proc_id++ // Container display - if (sys_infos && sys_infos.containers && sys_infos.containers.length > 0) { + if (sys_infos && sys_infos.containers && sys_infos.containers.length > 0 && + (pm2_conf && pm2_conf.show_docker == "true")) { var stacked_docker = (process.stdout.columns || 100) < 140 var docker_head = { diff --git a/lib/API/Extra.js b/lib/API/Extra.js index 8a085a5f..f8c07424 100644 --- a/lib/API/Extra.js +++ b/lib/API/Extra.js @@ -428,6 +428,21 @@ module.exports = function(CLI) { }); }; + /** + * API method to launch a process that will serve directory over http + */ + CLI.prototype.autoinstall = function (cb) { + var filepath = path.resolve(path.dirname(module.filename), '../Sysinfo/ServiceDetection/ServiceDetection.js'); + + this.start(filepath, (err, res) => { + if (err) { + Common.printError(cst.PREFIX_MSG_ERR + 'Error while trying to serve : ' + err.message || err); + return cb ? cb(err) : this.speedList(cst.ERROR_EXIT); + } + return cb ? cb(null) : this.speedList(); + }); + } + /** * API method to launch a process that will serve directory over http * diff --git a/lib/API/Modules/NPM.js b/lib/API/Modules/NPM.js index e43685d8..3dd1b5fe 100644 --- a/lib/API/Modules/NPM.js +++ b/lib/API/Modules/NPM.js @@ -253,7 +253,7 @@ function continueInstall(CLI, module_name, opts, cb) { CLI.conf(canonic_module_name, function() { Common.printOut(cst.PREFIX_MSG_MOD + 'Module successfully installed and launched'); - Common.printOut(cst.PREFIX_MSG_MOD + 'Edit configuration via: `pm2 conf`'); + Common.printOut(cst.PREFIX_MSG_MOD + 'Checkout module options: `$ pm2 conf`'); return cb(null, dt); }); }); diff --git a/lib/Sysinfo/ServiceDetection/ServiceDetection.js b/lib/Sysinfo/ServiceDetection/ServiceDetection.js new file mode 100644 index 00000000..3f0b9a1e --- /dev/null +++ b/lib/Sysinfo/ServiceDetection/ServiceDetection.js @@ -0,0 +1,119 @@ + +const PM2 = require('./../../API.js') +const psList = require('../psList.js') +const _ = require('lodash') + +const SERVICES_ASSOCIATION = { + 'mongodb,mongo': { + module: 'pm2-mongodb' + }, + 'redis,redis-server': { + module: 'pm2-redis' + }, + 'elasticsearch': { + module: 'pm2-elasticsearch' + }, + 'docker': { + module: 'pm2-monit-docker' + }, + 'consul': { + module:'pm2-monit-consul' + }, + 'pm2': { + module: 'pm2-probe' + }, + 'fpm': { + module: 'pm2-php-fpm' + } +} + +// 'python,python3': { +// module: 'pm2-python' +// }, +// 'nginx': { +// module: 'pm2-monit-nginx' +// }, +// 'haproxy': { +// module: 'pm2-monit-haproxy' +// }, +// 'traeffik': { +// module: 'pm2-monit-traeffik' +// } + +class ServicesDetection { + constructor() { + this.pm2 = new PM2() + } + + startDetection(cb = () => {}) { + // Check running probes + this.monitoredServices((err, pm2_services) => { + // Check running services + this.discover((err, required_modules) => { + var required_monitoring_probes = Object.keys(required_modules) + // Make the diff between + console.log(`Need to start following modules:`) + console.log(_.difference(required_monitoring_probes, pm2_services)) + this.pm2.install('pm2-server-monit', (err, apps) => { + cb() + }) + }) + }) + } + + monitoredServices(cb) { + var f_proc_list = [] + + this.pm2.list((err, proc_list) => { + f_proc_list = proc_list.map(p => { + return p.name + }) + this.pm2.close() + cb(err, f_proc_list) + }) + } + + discover(cb) { + psList() + .then(processes => { + var supported_systems = Object.keys(SERVICES_ASSOCIATION) + var required_modules = {} + + processes.forEach((proc) => { + supported_systems.forEach(sup_sys => { + var proc_names = sup_sys.split(',') + proc_names.forEach(proc_name => { + if (proc.name.includes(proc_name) === true || + proc.cmd.includes(proc_name) === true) { + var key = SERVICES_ASSOCIATION[sup_sys].module + required_modules[key] = SERVICES_ASSOCIATION[sup_sys] + required_modules[key].monit = proc + } + }) + }) + }) + return cb(null, required_modules) + }) + .catch(e => { + console.error(`Error while listing processes`, e) + }) + } +} + +if (require.main === module) { + var serviceDetection = new ServicesDetection() + + var process = (done) => { + serviceDetection.startDetection((err, procs) => { + done() + }) + } + + var iterate = () => { + process(() => { + setTimeout(iterate, 3000) + }) + } + + iterate() +}