Skip to content

Commit

Permalink
feat: add service detection system + disable docker container listing…
Browse files Browse the repository at this point in the history
… by default
  • Loading branch information
Unitech committed Jun 17, 2019
1 parent 8f00e37 commit 79a6a2d
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 2 deletions.
5 changes: 5 additions & 0 deletions bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
6 changes: 5 additions & 1 deletion lib/API/CliUx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = {
Expand Down
15 changes: 15 additions & 0 deletions lib/API/Extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion lib/API/Modules/NPM.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down
119 changes: 119 additions & 0 deletions lib/Sysinfo/ServiceDetection/ServiceDetection.js
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 79a6a2d

Please sign in to comment.