Configuration.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * Copyright 2013-2022 the PM2 project authors. All rights reserved.
  3. * Use of this source code is governed by a license that
  4. * can be found in the LICENSE file.
  5. */
  6. var Configuration = module.exports = {};
  7. var fs = require('fs');
  8. var Common = require('./Common');
  9. var eachSeries = require('async/eachSeries');
  10. var cst = require('../constants.js');
  11. function splitKey(key) {
  12. var values = [key];
  13. if (key.indexOf('.') > -1)
  14. values = key.match(/(?:[^."]+|"[^"]*")+/g).map(function(dt) { return dt.replace(/"/g, '') });
  15. else if (key.indexOf(':') > -1)
  16. values = key.match(/(?:[^:"]+|"[^"]*")+/g).map(function(dt) { return dt.replace(/"/g, '') });
  17. return values;
  18. }
  19. function serializeConfiguration(json_conf) {
  20. return JSON.stringify(json_conf, null, 4)
  21. }
  22. Configuration.set = function(key, value, cb) {
  23. fs.readFile(cst.PM2_MODULE_CONF_FILE, function(err, data) {
  24. if (err) return cb(err);
  25. var json_conf = JSON.parse(data);
  26. var values = splitKey(key);
  27. if (values.length > 0) {
  28. var levels = values;
  29. var tmp = json_conf;
  30. levels.forEach(function(key, index) {
  31. if (index == levels.length -1)
  32. tmp[key] = value;
  33. else if (!tmp[key]) {
  34. tmp[key] = {};
  35. tmp = tmp[key];
  36. }
  37. else {
  38. if (typeof(tmp[key]) != 'object')
  39. tmp[key] = {};
  40. tmp = tmp[key];
  41. }
  42. });
  43. }
  44. else {
  45. if (json_conf[key] && typeof(json_conf[key]) === 'string')
  46. Common.printOut(cst.PREFIX_MSG + 'Replacing current value key %s by %s', key, value);
  47. json_conf[key] = value;
  48. }
  49. fs.writeFile(cst.PM2_MODULE_CONF_FILE, serializeConfiguration(json_conf), function(err, data) {
  50. if (err) return cb(err);
  51. return cb(null, json_conf);
  52. });
  53. return false;
  54. });
  55. };
  56. Configuration.unset = function(key, cb) {
  57. fs.readFile(cst.PM2_MODULE_CONF_FILE, function(err, data) {
  58. if (err) return cb(err);
  59. var json_conf = JSON.parse(data);
  60. var values = splitKey(key);
  61. if (values.length > 0) {
  62. var levels = values;
  63. var tmp = json_conf;
  64. levels.forEach(function(key, index) {
  65. if (index == levels.length -1)
  66. delete tmp[key];
  67. else if (!tmp[key]) {
  68. tmp[key] = {};
  69. tmp = tmp[key];
  70. }
  71. else {
  72. if (typeof(tmp[key]) != 'object')
  73. tmp[key] = {};
  74. tmp = tmp[key];
  75. }
  76. });
  77. }
  78. else
  79. delete json_conf[key];
  80. if (err) return cb(err);
  81. if (key === 'all')
  82. json_conf = {};
  83. fs.writeFile(cst.PM2_MODULE_CONF_FILE, serializeConfiguration(json_conf), function(err, data) {
  84. if (err) return cb(err);
  85. return cb(null, json_conf);
  86. });
  87. return false;
  88. });
  89. }
  90. Configuration.setSyncIfNotExist = function(key, value) {
  91. try {
  92. var conf = JSON.parse(fs.readFileSync(cst.PM2_MODULE_CONF_FILE));
  93. } catch(e) {
  94. return null;
  95. }
  96. var values = splitKey(key);
  97. var exists = false;
  98. if (values.length > 1 && conf && conf[values[0]]) {
  99. exists = Object.keys(conf[values[0]]).some(function(key) {
  100. if (key == values[1])
  101. return true;
  102. return false;
  103. });
  104. }
  105. if (exists === false)
  106. return Configuration.setSync(key, value);
  107. return null;
  108. };
  109. Configuration.setSync = function(key, value) {
  110. try {
  111. var data = fs.readFileSync(cst.PM2_MODULE_CONF_FILE);
  112. } catch(e) {
  113. return null;
  114. }
  115. var json_conf = JSON.parse(data);
  116. var values = splitKey(key);
  117. if (values.length > 0) {
  118. var levels = values;
  119. var tmp = json_conf;
  120. levels.forEach(function(key, index) {
  121. if (index == levels.length -1)
  122. tmp[key] = value;
  123. else if (!tmp[key]) {
  124. tmp[key] = {};
  125. tmp = tmp[key];
  126. }
  127. else {
  128. if (typeof(tmp[key]) != 'object')
  129. tmp[key] = {};
  130. tmp = tmp[key];
  131. }
  132. });
  133. }
  134. else {
  135. if (json_conf[key] && typeof(json_conf[key]) === 'string')
  136. Common.printOut(cst.PREFIX_MSG + 'Replacing current value key %s by %s', key, value);
  137. json_conf[key] = value;
  138. }
  139. if (key === 'all')
  140. json_conf = {};
  141. try {
  142. fs.writeFileSync(cst.PM2_MODULE_CONF_FILE, serializeConfiguration(json_conf));
  143. return json_conf;
  144. } catch(e) {
  145. console.error(e.message);
  146. return null;
  147. }
  148. };
  149. Configuration.unsetSync = function(key) {
  150. try {
  151. var data = fs.readFileSync(cst.PM2_MODULE_CONF_FILE);
  152. } catch(e) {
  153. return null;
  154. }
  155. var json_conf = JSON.parse(data);
  156. var values = splitKey(key);
  157. if (values.length > 0) {
  158. var levels = values;
  159. var tmp = json_conf;
  160. levels.forEach(function(key, index) {
  161. if (index == levels.length -1)
  162. delete tmp[key];
  163. else if (!tmp[key]) {
  164. tmp[key] = {};
  165. tmp = tmp[key];
  166. }
  167. else {
  168. if (typeof(tmp[key]) != 'object')
  169. tmp[key] = {};
  170. tmp = tmp[key];
  171. }
  172. });
  173. }
  174. else
  175. delete json_conf[key];
  176. if (key === 'all')
  177. json_conf = {};
  178. try {
  179. fs.writeFileSync(cst.PM2_MODULE_CONF_FILE, serializeConfiguration(json_conf));
  180. } catch(e) {
  181. console.error(e.message);
  182. return null;
  183. }
  184. };
  185. Configuration.multiset = function(serial, cb) {
  186. var arrays = [];
  187. serial = serial.match(/(?:[^ "]+|"[^"]*")+/g);
  188. while (serial.length > 0)
  189. arrays.push(serial.splice(0, 2));
  190. eachSeries(arrays, function(el, next) {
  191. Configuration.set(el[0], el[1], next);
  192. }, cb);
  193. };
  194. Configuration.get = function(key, cb) {
  195. Configuration.getAll(function(err, data) {
  196. var climb = splitKey(key);
  197. climb.some(function(val) {
  198. if (!data[val]) {
  199. data = null;
  200. return true;
  201. }
  202. data = data[val];
  203. return false;
  204. });
  205. if (!data) return cb({err : 'Unknown key'}, null);
  206. return cb(null, data);
  207. });
  208. };
  209. Configuration.getSync = function(key) {
  210. try {
  211. var data = Configuration.getAllSync();
  212. } catch(e) {
  213. return null;
  214. }
  215. var climb = splitKey(key);
  216. climb.some(function(val) {
  217. if (!data[val]) {
  218. data = null;
  219. return true;
  220. }
  221. data = data[val];
  222. return false;
  223. });
  224. if (!data) return null;
  225. return data;
  226. };
  227. Configuration.getAll = function(cb) {
  228. fs.readFile(cst.PM2_MODULE_CONF_FILE, function(err, data) {
  229. if (err) return cb(err);
  230. return cb(null, JSON.parse(data));
  231. });
  232. };
  233. Configuration.getAllSync = function() {
  234. try {
  235. return JSON.parse(fs.readFileSync(cst.PM2_MODULE_CONF_FILE));
  236. } catch(e) {
  237. console.error(e.stack || e);
  238. return {};
  239. }
  240. };