Fixed some bugs in config parsing + added vhosts.vhost variable
This commit is contained in:
parent
fe654d1ee6
commit
02daddc0ce
4 changed files with 94 additions and 27 deletions
|
@ -60,9 +60,9 @@ class Config {
|
|||
/// the parser using opts parameter.
|
||||
/// \param configfile path to config file
|
||||
/// \param opts extra options which will be recognized by a parser
|
||||
bool load(const std::string &configfile, boost::program_options::options_description &opts);
|
||||
bool load(const std::string &configfile, boost::program_options::options_description &opts, const std::string &jid = "");
|
||||
|
||||
bool load(std::istream &ifs, boost::program_options::options_description &opts);
|
||||
bool load(std::istream &ifs, boost::program_options::options_description &opts, const std::string &jid = "");
|
||||
|
||||
bool load(std::istream &ifs);
|
||||
|
||||
|
@ -71,7 +71,7 @@ class Config {
|
|||
/// This function loads only config variables needed by libtransport.
|
||||
/// \see load(const std::string &, boost::program_options::options_description &)
|
||||
/// \param configfile path to config file
|
||||
bool load(const std::string &configfile);
|
||||
bool load(const std::string &configfile, const std::string &jid = "");
|
||||
|
||||
bool reload();
|
||||
|
||||
|
|
|
@ -122,6 +122,7 @@ int main(int argc, char **argv)
|
|||
boost::program_options::variables_map vm;
|
||||
bool no_daemon = false;
|
||||
std::string config_file;
|
||||
std::string jid;
|
||||
|
||||
|
||||
#ifndef WIN32
|
||||
|
@ -139,6 +140,7 @@ int main(int argc, char **argv)
|
|||
desc.add_options()
|
||||
("help,h", "help")
|
||||
("no-daemonize,n", "Do not run spectrum as daemon")
|
||||
("jid,j", boost::program_options::value<std::string>(&jid)->default_value(""), "Specify JID of transport manually")
|
||||
("config", boost::program_options::value<std::string>(&config_file)->default_value(""), "Config file")
|
||||
;
|
||||
try
|
||||
|
@ -177,7 +179,7 @@ int main(int argc, char **argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
if (!config.load(vm["config"].as<std::string>())) {
|
||||
if (!config.load(vm["config"].as<std::string>(), jid)) {
|
||||
std::cerr << "Can't load configuration file.\n";
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -82,12 +82,17 @@ static std::string searchForBinary(const std::string &binary) {
|
|||
}
|
||||
|
||||
// Executes new backend
|
||||
static unsigned long exec_(std::string path, std::string config) {
|
||||
static unsigned long exec_(std::string path, std::string config, std::string jid = "") {
|
||||
// fork and exec
|
||||
pid_t pid = fork();
|
||||
if ( pid == 0 ) {
|
||||
// child process
|
||||
exit(execl(path.c_str(), path.c_str(), config.c_str(), NULL));
|
||||
if (jid.empty()) {
|
||||
exit(execl(path.c_str(), path.c_str(), config.c_str(), NULL));
|
||||
}
|
||||
else {
|
||||
exit(execl(path.c_str(), path.c_str(), "-j", jid.c_str(), config.c_str(), NULL));
|
||||
}
|
||||
} else if ( pid < 0 ) {
|
||||
// fork failed
|
||||
}
|
||||
|
@ -143,15 +148,27 @@ static void start_all_instances(ManagerConfig *config) {
|
|||
Config cfg;
|
||||
if (cfg.load(itr->path().string()) == false) {
|
||||
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
int pid = isRunning(CONFIG_STRING(&cfg, "service.pidfile"));
|
||||
if (pid == 0) {
|
||||
std::cout << "Starting " << itr->path() << ": OK\n";
|
||||
exec_(spectrum2_binary, itr->path().string());
|
||||
}
|
||||
else {
|
||||
std::cout << "Starting " << itr->path() << ": Already started (PID=" << pid << ")\n";
|
||||
std::vector<std::string> vhosts = CONFIG_VECTOR(&cfg, "vhosts.vhost");
|
||||
vhosts.push_back(CONFIG_STRING(&cfg, "service.jid"));
|
||||
|
||||
BOOST_FOREACH(std::string &vhost, vhosts) {
|
||||
Config vhostCfg;
|
||||
if (vhostCfg.load(itr->path().string(), vhost) == false) {
|
||||
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile"));
|
||||
if (pid == 0) {
|
||||
std::cout << "Starting " << itr->path() << ": OK\n";
|
||||
exec_(spectrum2_binary, itr->path().string(), vhost);
|
||||
}
|
||||
else {
|
||||
std::cout << "Starting " << itr->path() << ": Already started (PID=" << pid << ")\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -184,13 +201,24 @@ static void stop_all_instances(ManagerConfig *config) {
|
|||
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
|
||||
}
|
||||
|
||||
int pid = isRunning(CONFIG_STRING(&cfg, "service.pidfile"));
|
||||
if (pid) {
|
||||
std::cout << "Stopping " << itr->path() << ": OK\n";
|
||||
kill(pid, SIGTERM);
|
||||
}
|
||||
else {
|
||||
std::cout << "Stopping " << itr->path() << ": Not running\n";
|
||||
std::vector<std::string> vhosts = CONFIG_VECTOR(&cfg, "vhosts.vhost");
|
||||
vhosts.push_back(CONFIG_STRING(&cfg, "service.jid"));
|
||||
|
||||
BOOST_FOREACH(std::string &vhost, vhosts) {
|
||||
Config vhostCfg;
|
||||
if (vhostCfg.load(itr->path().string(), vhost) == false) {
|
||||
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile"));
|
||||
if (pid) {
|
||||
std::cout << "Stopping " << itr->path() << ": OK\n";
|
||||
kill(pid, SIGTERM);
|
||||
}
|
||||
else {
|
||||
std::cout << "Stopping " << itr->path() << ": Not running\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,13 +31,13 @@ using namespace boost::program_options;
|
|||
|
||||
namespace Transport {
|
||||
|
||||
bool Config::load(const std::string &configfile, boost::program_options::options_description &opts) {
|
||||
bool Config::load(const std::string &configfile, boost::program_options::options_description &opts, const std::string &jid) {
|
||||
std::ifstream ifs(configfile.c_str());
|
||||
if (!ifs.is_open())
|
||||
return false;
|
||||
|
||||
m_file = configfile;
|
||||
bool ret = load(ifs, opts);
|
||||
bool ret = load(ifs, opts, jid);
|
||||
ifs.close();
|
||||
|
||||
char path[PATH_MAX] = "";
|
||||
|
@ -49,7 +49,7 @@ bool Config::load(const std::string &configfile, boost::program_options::options
|
|||
return ret;
|
||||
}
|
||||
|
||||
bool Config::load(std::istream &ifs, boost::program_options::options_description &opts) {
|
||||
bool Config::load(std::istream &ifs, boost::program_options::options_description &opts, const std::string &_jid) {
|
||||
m_unregistered.clear();
|
||||
opts.add_options()
|
||||
("service.jid", value<std::string>()->default_value(""), "Transport Jabber ID")
|
||||
|
@ -76,6 +76,7 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description
|
|||
("service.memory_collector_time", value<int>()->default_value(0), "Time in seconds after which backend with most memory is set to die.")
|
||||
("service.more_resources", value<bool>()->default_value(false), "Allow more resources to be connected in server mode at the same time.")
|
||||
("service.enable_privacy_lists", value<bool>()->default_value(true), "")
|
||||
("vhosts.vhost", value<std::vector<std::string> >()->multitoken(), "")
|
||||
("identity.name", value<std::string>()->default_value("Spectrum 2 Transport"), "Name showed in service discovery.")
|
||||
("identity.category", value<std::string>()->default_value("gateway"), "Disco#info identity category. 'gateway' by default.")
|
||||
("identity.type", value<std::string>()->default_value(""), "Type of transport ('icq','msn','gg','irc', ...)")
|
||||
|
@ -102,11 +103,47 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description
|
|||
|
||||
parsed_options parsed = parse_config_file(ifs, opts, true);
|
||||
|
||||
bool found_working = false;
|
||||
bool found_pidfile = false;
|
||||
std::string jid = "";
|
||||
BOOST_FOREACH(option opt, parsed.options) {
|
||||
BOOST_FOREACH(option &opt, parsed.options) {
|
||||
if (opt.string_key == "service.jid") {
|
||||
jid = opt.value[0];
|
||||
if (_jid.empty()) {
|
||||
jid = opt.value[0];
|
||||
}
|
||||
else {
|
||||
opt.value[0] = _jid;
|
||||
jid = _jid;
|
||||
}
|
||||
}
|
||||
else if (opt.string_key == "service.backend_port") {
|
||||
if (opt.value[0] == "0") {
|
||||
unsigned long r = 0;
|
||||
BOOST_FOREACH(char c, _jid) {
|
||||
r += (int) c;
|
||||
}
|
||||
srand(time(NULL) + r);
|
||||
int randomPort = 30000 + rand() % 10000;
|
||||
opt.value[0] = boost::lexical_cast<std::string>(randomPort);
|
||||
}
|
||||
}
|
||||
else if (opt.string_key == "service.working_dir") {
|
||||
found_working = true;
|
||||
}
|
||||
else if (opt.string_key == "service.pidfile") {
|
||||
found_pidfile = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found_working) {
|
||||
std::vector<std::string> value;
|
||||
value.push_back("/var/lib/spectrum2/$jid");
|
||||
parsed.options.push_back(boost::program_options::basic_option<char>("service.working_dir", value));
|
||||
}
|
||||
if (!found_pidfile) {
|
||||
std::vector<std::string> value;
|
||||
value.push_back("/var/run/spectrum2/$jid.pid");
|
||||
parsed.options.push_back(boost::program_options::basic_option<char>("service.pidfile", value));
|
||||
}
|
||||
|
||||
BOOST_FOREACH(option &opt, parsed.options) {
|
||||
|
@ -131,9 +168,9 @@ bool Config::load(std::istream &ifs) {
|
|||
return load(ifs, opts);
|
||||
}
|
||||
|
||||
bool Config::load(const std::string &configfile) {
|
||||
bool Config::load(const std::string &configfile, const std::string &jid) {
|
||||
options_description opts("Transport options");
|
||||
return load(configfile, opts);
|
||||
return load(configfile, opts, jid);
|
||||
}
|
||||
|
||||
bool Config::reload() {
|
||||
|
|
Loading…
Add table
Reference in a new issue