helpers.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. var cst = require('../../../constants.js');
  2. var Common = require('../../Common.js');
  3. const chalk = require('chalk');
  4. const forEach = require('async/forEach');
  5. const open = require('../../tools/open.js');
  6. const Modules = require('../Modules');
  7. function processesAreAlreadyMonitored(CLI, cb) {
  8. CLI.Client.executeRemote('getMonitorData', {}, function(err, list) {
  9. if (err) return cb(false);
  10. var l = list.filter(l => l.pm2_env.km_link == true)
  11. var l2 = list.filter(l => l.name == 'pm2-server-monit')
  12. return cb(l.length > 0 && l2.length > 0 ? true : false)
  13. })
  14. }
  15. module.exports = function(CLI) {
  16. CLI.prototype.openDashboard = function() {
  17. if (!this.gl_interact_infos) {
  18. Common.printError(chalk.bold.white('Agent if offline, type `$ pm2 plus` to log in'));
  19. return this.exitCli(cst.ERROR_EXIT);
  20. }
  21. var uri = `https://app.pm2.io/#/r/${this.gl_interact_infos.public_key}`
  22. console.log(cst.PM2_IO_MSG + ` Opening ${uri}`)
  23. open(uri);
  24. setTimeout(_ => {
  25. this.exitCli();
  26. }, 200);
  27. };
  28. CLI.prototype.clearSetup = function (opts, cb) {
  29. const modules = ['event-loop-inspector']
  30. this.gl_is_km_linked = false
  31. forEach(modules, (_module, next) => {
  32. Modules.uninstall(this, _module, () => {
  33. next()
  34. });
  35. }, (err) => {
  36. this.reload('all', () => {
  37. return cb()
  38. })
  39. })
  40. }
  41. /**
  42. * Install required package and enable flags for current running processes
  43. */
  44. CLI.prototype.minimumSetup = function (opts, cb) {
  45. var self = this;
  46. this.gl_is_km_linked = true
  47. function install(cb) {
  48. var modules = []
  49. if (opts.type === 'enterprise' || opts.type === 'plus') {
  50. modules = ['pm2-logrotate', 'pm2-server-monit']
  51. if (opts.type === 'enterprise') {
  52. modules.push('deep-metrics')
  53. }
  54. }
  55. forEach(modules, (_module, next) => {
  56. Modules.install(self, _module, {}, () => {
  57. next()
  58. });
  59. }, (err) => {
  60. self.reload('all', () => {
  61. return cb()
  62. })
  63. })
  64. }
  65. processesAreAlreadyMonitored(self, (already_monitored) => {
  66. if (already_monitored) {
  67. console.log(cst.PM2_IO_MSG + ` PM2 ${opts.type || ''} bundle already installed`);
  68. return cb()
  69. }
  70. if (opts.installAll)
  71. return install(cb)
  72. // promptly.confirm(chalk.bold('Install all pm2 plus dependencies ? (y/n)'), (err, answer) => {
  73. // if (!err && answer === true)
  74. return install(cb)
  75. // self.reload('all', () => {
  76. // return cb()
  77. // })
  78. // });
  79. })
  80. }
  81. }