Configuration.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. var Common = require('../Common.js');
  2. var cst = require('../../constants.js');
  3. var UX = require('./UX');
  4. var chalk = require('chalk');
  5. var Configuration = require('../Configuration.js');
  6. module.exports = function(CLI) {
  7. CLI.prototype.get = function(key, cb) {
  8. var that = this;
  9. if (!key || key == 'all') {
  10. displayConf(function(err, data) {
  11. if (err)
  12. return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
  13. return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
  14. });
  15. return false;
  16. }
  17. Configuration.get(key, function(err, data) {
  18. if (err) {
  19. return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
  20. }
  21. // pm2 conf module-name
  22. if (key.indexOf(':') === -1 && key.indexOf('.') === -1) {
  23. displayConf(key, function() {
  24. console.log('Modules configuration. Copy/Paste line to edit values.')
  25. return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT)
  26. });
  27. return false;
  28. }
  29. // pm2 conf module-name:key
  30. var module_name, key_name;
  31. if (key.indexOf(':') > -1) {
  32. module_name = key.split(':')[0];
  33. key_name = key.split(':')[1];
  34. } else if (key.indexOf('.') > -1) {
  35. module_name = key.split('.')[0];
  36. key_name = key.split('.')[1];
  37. }
  38. Common.printOut('Value for module ' + chalk.blue(module_name), 'key ' + chalk.blue(key_name) + ': ' + chalk.bold.green(data));
  39. return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
  40. });
  41. };
  42. CLI.prototype.set = function(key, value, cb) {
  43. var that = this;
  44. if (!key) {
  45. interactiveConfigEdit(function(err) {
  46. if (err)
  47. return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
  48. return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
  49. });
  50. return false;
  51. }
  52. /**
  53. * Set value
  54. */
  55. Configuration.set(key, value, function(err) {
  56. if (err)
  57. return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
  58. var values = [];
  59. if (key.indexOf('.') > -1)
  60. values = key.split('.');
  61. if (key.indexOf(':') > -1)
  62. values = key.split(':');
  63. if (values && values.length > 1) {
  64. // The first element is the app name (module_conf.json)
  65. var app_name = values[0];
  66. process.env.PM2_PROGRAMMATIC = 'true';
  67. that.restart(app_name, {
  68. updateEnv : true
  69. }, function(err, data) {
  70. process.env.PM2_PROGRAMMATIC = 'false';
  71. if (!err)
  72. Common.printOut(cst.PREFIX_MSG + 'Module %s restarted', app_name);
  73. Common.log('Setting changed')
  74. displayConf(app_name, function() {
  75. return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
  76. });
  77. });
  78. return false;
  79. }
  80. displayConf(null, function() {
  81. return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
  82. });
  83. });
  84. };
  85. CLI.prototype.multiset = function(serial, cb) {
  86. var that = this;
  87. Configuration.multiset(serial, function(err, data) {
  88. if (err)
  89. return cb ? cb({success:false, err:err}) : that.exitCli(cst.ERROR_EXIT);
  90. var values = [];
  91. var key = serial.match(/(?:[^ "]+|"[^"]*")+/g)[0];
  92. if (key.indexOf('.') > -1)
  93. values = key.split('.');
  94. if (key.indexOf(':') > -1)
  95. values = key.split(':');
  96. if (values && values.length > 1) {
  97. // The first element is the app name (module_conf.json)
  98. var app_name = values[0];
  99. process.env.PM2_PROGRAMMATIC = 'true';
  100. that.restart(app_name, {
  101. updateEnv : true
  102. }, function(err, data) {
  103. process.env.PM2_PROGRAMMATIC = 'false';
  104. if (!err)
  105. Common.printOut(cst.PREFIX_MSG + 'Module %s restarted', app_name);
  106. displayConf(app_name, function() {
  107. return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT)
  108. });
  109. });
  110. return false;
  111. }
  112. displayConf(app_name, function() {
  113. return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT)
  114. });
  115. });
  116. };
  117. CLI.prototype.unset = function(key, cb) {
  118. var that = this;
  119. Configuration.unset(key, function(err) {
  120. if (err) {
  121. return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
  122. }
  123. displayConf(function() { cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT) });
  124. });
  125. };
  126. CLI.prototype.conf = function(key, value, cb) {
  127. var that = this;
  128. if (typeof(value) === 'function') {
  129. cb = value;
  130. value = null;
  131. }
  132. // If key + value = set
  133. if (key && value) {
  134. that.set(key, value, function(err) {
  135. if (err)
  136. return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
  137. return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
  138. });
  139. }
  140. // If only key = get
  141. else if (key) {
  142. that.get(key, function(err, data) {
  143. if (err)
  144. return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
  145. return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
  146. });
  147. }
  148. else {
  149. interactiveConfigEdit(function(err) {
  150. if (err)
  151. return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
  152. return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
  153. });
  154. }
  155. };
  156. };
  157. function interactiveConfigEdit(cb) {
  158. UX.helpers.openEditor(cst.PM2_MODULE_CONF_FILE, function(err, data) {
  159. Common.printOut(chalk.bold('Module configuration (%s) edited.'), cst.PM2_MODULE_CONF_FILE);
  160. Common.printOut(chalk.bold('To take changes into account, please restart module related.'), cst.PM2_MODULE_CONF_FILE);
  161. if (err)
  162. return cb(Common.retErr(err));
  163. return cb(null, {success:true});
  164. });
  165. }
  166. /**
  167. * Configuration
  168. */
  169. function displayConf(target_app, cb) {
  170. if (typeof(target_app) == 'function') {
  171. cb = target_app;
  172. target_app = null;
  173. }
  174. Configuration.getAll(function(err, data) {
  175. UX.helpers.dispKeys(data, target_app);
  176. return cb();
  177. });
  178. }