link.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var cst = require('../../../constants.js');
  2. var Common = require('../../Common.js');
  3. var chalk = require('chalk');
  4. var fs = require('fs');
  5. var KMDaemon = require('@pm2/agent/src/InteractorClient');
  6. var pkg = require('../../../package.json')
  7. module.exports = function(CLI) {
  8. CLI.prototype.linkManagement = function(cmd, public_key, machine, opts, cb) {
  9. var that = this;
  10. // pm2 link stop || kill
  11. if (cmd == 'stop' || cmd == 'kill') {
  12. that.gl_is_km_linked = false
  13. console.log(cst.PM2_IO_MSG + ' Stopping agent...');
  14. return that.killAgent(function(err) {
  15. if (err) {
  16. Common.printError(err);
  17. return process.exit(cst.ERROR_EXIT);
  18. }
  19. console.log(cst.PM2_IO_MSG + ' Stopped');
  20. that.reload('all', () => {
  21. return process.exit(cst.SUCCESS_EXIT);
  22. })
  23. });
  24. }
  25. // pm2 link info
  26. if (cmd == 'info') {
  27. console.log(cst.PM2_IO_MSG + ' Getting agent information...');
  28. that.agentInfos(function(err, infos) {
  29. if (err) {
  30. console.error(cst.PM2_IO_MSG_ERR + ' ' + err.message);
  31. return that.exitCli(cst.ERROR_EXIT);
  32. }
  33. console.log(infos);
  34. return that.exitCli(cst.SUCCESS_EXIT);
  35. });
  36. return false;
  37. }
  38. // pm2 link delete
  39. if (cmd == 'delete') {
  40. that.gl_is_km_linked = false
  41. console.log(cst.PM2_IO_MSG + ' Permanently disable agent...');
  42. that.killAgent(function(err) {
  43. try {
  44. fs.unlinkSync(cst.INTERACTION_CONF);
  45. } catch(e) {
  46. console.log(cst.PM2_IO_MSG + ' No interaction config file found');
  47. return process.exit(cst.SUCCESS_EXIT);
  48. }
  49. console.log(cst.PM2_IO_MSG + ' Agent interaction ended');
  50. if (!cb)
  51. return process.exit(cst.SUCCESS_EXIT);
  52. return cb()
  53. });
  54. return false;
  55. }
  56. if (cmd && !public_key) {
  57. console.error(cst.PM2_IO_MSG + ' Command [%s] unknown or missing public key', cmd);
  58. return process.exit(cst.ERROR_EXIT);
  59. }
  60. // pm2 link xxx yyy
  61. var infos;
  62. if (!cmd) {
  63. infos = null;
  64. }
  65. else
  66. infos = {
  67. public_key : public_key,
  68. secret_key : cmd,
  69. machine_name : machine,
  70. info_node : opts.infoNode || null,
  71. pm2_version: pkg.version
  72. }
  73. that.link(infos, cb)
  74. };
  75. CLI.prototype.link = function(infos, cb) {
  76. var that = this;
  77. process.env.WS_JSON_PATCH = true
  78. KMDaemon.launchAndInteract(cst, infos, function(err, dt) {
  79. if (err) {
  80. Common.printError(cst.PM2_IO_MSG + ' Run `$ pm2 plus` to connect')
  81. return that.exitCli(cst.ERROR_EXIT);
  82. }
  83. console.log(chalk.bold.green('[+] PM2+ activated!'))
  84. if (!cb) {
  85. return that.exitCli(cst.SUCCESS_EXIT);
  86. }
  87. return cb(null, dt)
  88. });
  89. };
  90. CLI.prototype.agentInfos = function(cb) {
  91. KMDaemon.getInteractInfo(this._conf, function(err, data) {
  92. if (err)
  93. return cb(Common.retErr(err));
  94. return cb(null, data);
  95. });
  96. };
  97. CLI.prototype.killAgent = function(cb) {
  98. var that = this;
  99. KMDaemon.killInteractorDaemon(that._conf, function(err) {
  100. if (err)
  101. return cb ? cb(Common.retErr(err)) : that.exitCli(cst.SUCCESS_EXIT);
  102. return cb ? cb(null) : that.exitCli(cst.SUCCESS_EXIT);
  103. });
  104. };
  105. CLI.prototype.unlink = function(cb) {
  106. this.linkManagement('delete', cb);
  107. };
  108. };