2016-03-22 23:33:43 +01:00
|
|
|
/**
|
2026-02-08 15:04:58 +01:00
|
|
|
* Copyright 2013-present the PM2 project authors. All rights reserved.
|
2016-03-22 23:33:43 +01:00
|
|
|
* Use of this source code is governed by a license that
|
|
|
|
|
* can be found in the LICENSE file.
|
|
|
|
|
*/
|
2015-02-09 16:01:23 -05:00
|
|
|
|
|
|
|
|
var Configuration = module.exports = {};
|
|
|
|
|
|
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
|
|
var Common = require('./Common');
|
2018-07-19 09:44:06 -04:00
|
|
|
var eachSeries = require('async/eachSeries');
|
2015-02-09 16:01:23 -05:00
|
|
|
var cst = require('../constants.js');
|
|
|
|
|
|
2015-09-18 17:31:50 +02:00
|
|
|
function splitKey(key) {
|
2015-09-26 14:08:44 +02:00
|
|
|
var values = [key];
|
2015-09-18 17:31:50 +02:00
|
|
|
|
|
|
|
|
if (key.indexOf('.') > -1)
|
|
|
|
|
values = key.match(/(?:[^."]+|"[^"]*")+/g).map(function(dt) { return dt.replace(/"/g, '') });
|
|
|
|
|
else if (key.indexOf(':') > -1)
|
|
|
|
|
values = key.match(/(?:[^:"]+|"[^"]*")+/g).map(function(dt) { return dt.replace(/"/g, '') });
|
|
|
|
|
|
|
|
|
|
return values;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 23:36:09 +02:00
|
|
|
function serializeConfiguration(json_conf) {
|
|
|
|
|
return JSON.stringify(json_conf, null, 4)
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 02:13:41 +02:00
|
|
|
Configuration.set = function(key, value, cb) {
|
|
|
|
|
fs.readFile(cst.PM2_MODULE_CONF_FILE, function(err, data) {
|
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
|
|
var json_conf = JSON.parse(data);
|
|
|
|
|
|
|
|
|
|
var values = splitKey(key);
|
|
|
|
|
|
|
|
|
|
if (values.length > 0) {
|
|
|
|
|
var levels = values;
|
|
|
|
|
|
|
|
|
|
var tmp = json_conf;
|
|
|
|
|
|
|
|
|
|
levels.forEach(function(key, index) {
|
|
|
|
|
if (index == levels.length -1)
|
|
|
|
|
tmp[key] = value;
|
|
|
|
|
else if (!tmp[key]) {
|
|
|
|
|
tmp[key] = {};
|
|
|
|
|
tmp = tmp[key];
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (typeof(tmp[key]) != 'object')
|
|
|
|
|
tmp[key] = {};
|
|
|
|
|
tmp = tmp[key];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (json_conf[key] && typeof(json_conf[key]) === 'string')
|
|
|
|
|
Common.printOut(cst.PREFIX_MSG + 'Replacing current value key %s by %s', key, value);
|
|
|
|
|
|
|
|
|
|
json_conf[key] = value;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 23:36:09 +02:00
|
|
|
fs.writeFile(cst.PM2_MODULE_CONF_FILE, serializeConfiguration(json_conf), function(err, data) {
|
2015-10-15 02:13:41 +02:00
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
|
|
return cb(null, json_conf);
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-02-09 16:01:23 -05:00
|
|
|
Configuration.unset = function(key, cb) {
|
|
|
|
|
fs.readFile(cst.PM2_MODULE_CONF_FILE, function(err, data) {
|
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
2015-03-01 18:56:53 -05:00
|
|
|
var json_conf = JSON.parse(data);
|
2015-02-09 16:01:23 -05:00
|
|
|
|
2015-09-18 17:31:50 +02:00
|
|
|
var values = splitKey(key);
|
2015-07-12 21:07:08 +02:00
|
|
|
|
|
|
|
|
if (values.length > 0) {
|
|
|
|
|
var levels = values;
|
|
|
|
|
|
|
|
|
|
var tmp = json_conf;
|
|
|
|
|
|
|
|
|
|
levels.forEach(function(key, index) {
|
|
|
|
|
if (index == levels.length -1)
|
|
|
|
|
delete tmp[key];
|
|
|
|
|
else if (!tmp[key]) {
|
|
|
|
|
tmp[key] = {};
|
|
|
|
|
tmp = tmp[key];
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (typeof(tmp[key]) != 'object')
|
|
|
|
|
tmp[key] = {};
|
|
|
|
|
tmp = tmp[key];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
delete json_conf[key];
|
|
|
|
|
|
|
|
|
|
if (err) return cb(err);
|
2015-02-09 16:01:23 -05:00
|
|
|
|
|
|
|
|
if (key === 'all')
|
|
|
|
|
json_conf = {};
|
|
|
|
|
|
2019-06-10 23:36:09 +02:00
|
|
|
fs.writeFile(cst.PM2_MODULE_CONF_FILE, serializeConfiguration(json_conf), function(err, data) {
|
2015-02-09 16:01:23 -05:00
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
|
|
return cb(null, json_conf);
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
});
|
2015-10-15 02:13:41 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-21 18:29:23 +02:00
|
|
|
Configuration.setSyncIfNotExist = function(key, value) {
|
|
|
|
|
try {
|
|
|
|
|
var conf = JSON.parse(fs.readFileSync(cst.PM2_MODULE_CONF_FILE));
|
|
|
|
|
} catch(e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var values = splitKey(key);
|
|
|
|
|
var exists = false;
|
|
|
|
|
|
|
|
|
|
if (values.length > 1 && conf && conf[values[0]]) {
|
|
|
|
|
exists = Object.keys(conf[values[0]]).some(function(key) {
|
|
|
|
|
if (key == values[1])
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (exists === false)
|
|
|
|
|
return Configuration.setSync(key, value);
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-15 02:13:41 +02:00
|
|
|
Configuration.setSync = function(key, value) {
|
|
|
|
|
try {
|
|
|
|
|
var data = fs.readFileSync(cst.PM2_MODULE_CONF_FILE);
|
|
|
|
|
} catch(e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var json_conf = JSON.parse(data);
|
|
|
|
|
|
|
|
|
|
var values = splitKey(key);
|
|
|
|
|
|
|
|
|
|
if (values.length > 0) {
|
|
|
|
|
var levels = values;
|
|
|
|
|
|
|
|
|
|
var tmp = json_conf;
|
|
|
|
|
|
|
|
|
|
levels.forEach(function(key, index) {
|
|
|
|
|
if (index == levels.length -1)
|
|
|
|
|
tmp[key] = value;
|
|
|
|
|
else if (!tmp[key]) {
|
|
|
|
|
tmp[key] = {};
|
|
|
|
|
tmp = tmp[key];
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (typeof(tmp[key]) != 'object')
|
|
|
|
|
tmp[key] = {};
|
|
|
|
|
tmp = tmp[key];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (json_conf[key] && typeof(json_conf[key]) === 'string')
|
|
|
|
|
Common.printOut(cst.PREFIX_MSG + 'Replacing current value key %s by %s', key, value);
|
|
|
|
|
|
|
|
|
|
json_conf[key] = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (key === 'all')
|
|
|
|
|
json_conf = {};
|
|
|
|
|
|
|
|
|
|
try {
|
2019-06-10 23:36:09 +02:00
|
|
|
fs.writeFileSync(cst.PM2_MODULE_CONF_FILE, serializeConfiguration(json_conf));
|
2015-10-15 02:13:41 +02:00
|
|
|
return json_conf;
|
|
|
|
|
} catch(e) {
|
|
|
|
|
console.error(e.message);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Configuration.unsetSync = function(key) {
|
|
|
|
|
try {
|
|
|
|
|
var data = fs.readFileSync(cst.PM2_MODULE_CONF_FILE);
|
|
|
|
|
} catch(e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var json_conf = JSON.parse(data);
|
|
|
|
|
|
|
|
|
|
var values = splitKey(key);
|
|
|
|
|
|
|
|
|
|
if (values.length > 0) {
|
|
|
|
|
var levels = values;
|
|
|
|
|
|
|
|
|
|
var tmp = json_conf;
|
|
|
|
|
|
|
|
|
|
levels.forEach(function(key, index) {
|
|
|
|
|
if (index == levels.length -1)
|
|
|
|
|
delete tmp[key];
|
|
|
|
|
else if (!tmp[key]) {
|
|
|
|
|
tmp[key] = {};
|
|
|
|
|
tmp = tmp[key];
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (typeof(tmp[key]) != 'object')
|
|
|
|
|
tmp[key] = {};
|
|
|
|
|
tmp = tmp[key];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
delete json_conf[key];
|
|
|
|
|
|
|
|
|
|
if (key === 'all')
|
|
|
|
|
json_conf = {};
|
|
|
|
|
|
|
|
|
|
try {
|
2019-06-10 23:36:09 +02:00
|
|
|
fs.writeFileSync(cst.PM2_MODULE_CONF_FILE, serializeConfiguration(json_conf));
|
2015-10-15 02:13:41 +02:00
|
|
|
} catch(e) {
|
|
|
|
|
console.error(e.message);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2015-02-09 16:01:23 -05:00
|
|
|
};
|
|
|
|
|
|
2015-09-26 14:08:44 +02:00
|
|
|
Configuration.multiset = function(serial, cb) {
|
|
|
|
|
var arrays = [];
|
2015-09-26 20:02:00 +02:00
|
|
|
serial = serial.match(/(?:[^ "]+|"[^"]*")+/g);
|
2015-09-26 14:08:44 +02:00
|
|
|
|
|
|
|
|
while (serial.length > 0)
|
|
|
|
|
arrays.push(serial.splice(0, 2));
|
|
|
|
|
|
2018-07-19 09:44:06 -04:00
|
|
|
eachSeries(arrays, function(el, next) {
|
2015-09-26 14:08:44 +02:00
|
|
|
Configuration.set(el[0], el[1], next);
|
|
|
|
|
}, cb);
|
|
|
|
|
};
|
|
|
|
|
|
2015-04-19 17:21:26 -04:00
|
|
|
Configuration.get = function(key, cb) {
|
|
|
|
|
Configuration.getAll(function(err, data) {
|
2015-09-18 17:31:50 +02:00
|
|
|
var climb = splitKey(key);
|
2015-04-19 17:21:26 -04:00
|
|
|
|
|
|
|
|
climb.some(function(val) {
|
|
|
|
|
if (!data[val]) {
|
|
|
|
|
data = null;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
data = data[val];
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
2015-11-17 13:11:53 +13:00
|
|
|
if (!data) return cb({err : 'Unknown key'}, null);
|
2015-04-19 17:21:26 -04:00
|
|
|
return cb(null, data);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-07-12 21:31:18 +02:00
|
|
|
Configuration.getSync = function(key) {
|
|
|
|
|
try {
|
|
|
|
|
var data = Configuration.getAllSync();
|
|
|
|
|
} catch(e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-18 17:31:50 +02:00
|
|
|
var climb = splitKey(key);
|
2015-07-12 21:31:18 +02:00
|
|
|
|
|
|
|
|
climb.some(function(val) {
|
|
|
|
|
if (!data[val]) {
|
|
|
|
|
data = null;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
data = data[val];
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!data) return null;
|
|
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
2015-02-09 16:01:23 -05:00
|
|
|
Configuration.getAll = function(cb) {
|
|
|
|
|
fs.readFile(cst.PM2_MODULE_CONF_FILE, function(err, data) {
|
|
|
|
|
if (err) return cb(err);
|
2015-03-01 18:56:53 -05:00
|
|
|
return cb(null, JSON.parse(data));
|
2015-02-09 16:01:23 -05:00
|
|
|
});
|
|
|
|
|
};
|
2015-02-09 17:02:06 -05:00
|
|
|
|
|
|
|
|
Configuration.getAllSync = function() {
|
|
|
|
|
try {
|
2015-03-01 18:56:53 -05:00
|
|
|
return JSON.parse(fs.readFileSync(cst.PM2_MODULE_CONF_FILE));
|
2015-02-09 17:02:06 -05:00
|
|
|
} catch(e) {
|
|
|
|
|
console.error(e.stack || e);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
};
|