Files
@ bfb1488c950d
Branch filter:
Location: libtransport.git/spectrum_manager/src/main.cpp - annotation
bfb1488c950d
9.4 KiB
text/x-c++hdr
merge latest changes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d 7f01140b4fb0 7f01140b4fb0 cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d 7f01140b4fb0 d3ccb9f195c8 7f01140b4fb0 3932a5c77e9a 3932a5c77e9a 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 3932a5c77e9a d3ccb9f195c8 3932a5c77e9a d3ccb9f195c8 d3ccb9f195c8 d3ccb9f195c8 d3ccb9f195c8 d3ccb9f195c8 d3ccb9f195c8 3932a5c77e9a 9d23a3ba8ba4 3932a5c77e9a 3932a5c77e9a 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d 47d27ef2e1be cd2501c4360d cd2501c4360d 47d27ef2e1be cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d 47d27ef2e1be cd2501c4360d cd2501c4360d 47d27ef2e1be cd2501c4360d 47d27ef2e1be cd2501c4360d cd2501c4360d cd2501c4360d 47d27ef2e1be cd2501c4360d 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be cd2501c4360d 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be cd2501c4360d 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be cd2501c4360d 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be da535d6bf47b da535d6bf47b da535d6bf47b 47d27ef2e1be cd2501c4360d da535d6bf47b da535d6bf47b da535d6bf47b cd2501c4360d cd2501c4360d 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be da535d6bf47b 47d27ef2e1be 47d27ef2e1be da535d6bf47b da535d6bf47b da535d6bf47b 47d27ef2e1be cd2501c4360d 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be cd2501c4360d cd2501c4360d 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 cd2501c4360d cd2501c4360d cd2501c4360d 7f01140b4fb0 cd2501c4360d 7f01140b4fb0 cd2501c4360d 140278d70d9d cd2501c4360d 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d 7f01140b4fb0 cd2501c4360d 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 cd2501c4360d 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 7f01140b4fb0 cd2501c4360d 7f01140b4fb0 7f01140b4fb0 cd2501c4360d 7f01140b4fb0 cd2501c4360d 7f01140b4fb0 7f01140b4fb0 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a cd2501c4360d cd2501c4360d 7f01140b4fb0 47d27ef2e1be 47d27ef2e1be 47d27ef2e1be cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d 3932a5c77e9a cd2501c4360d 7f01140b4fb0 3932a5c77e9a 3932a5c77e9a cd2501c4360d cd2501c4360d cd2501c4360d 3932a5c77e9a cd2501c4360d 3932a5c77e9a 3932a5c77e9a 3932a5c77e9a cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d cd2501c4360d 7f01140b4fb0 | #include "managerconfig.h"
#include "transport/transport.h"
#include "transport/usermanager.h"
#include "transport/logger.h"
#include "transport/sqlite3backend.h"
#include "transport/userregistration.h"
#include "transport/networkpluginserver.h"
#include "Swiften/EventLoop/SimpleEventLoop.h"
#include <boost/foreach.hpp>
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <boost/filesystem.hpp>
#include "signal.h"
#include "sys/wait.h"
using namespace Transport;
using namespace boost::filesystem;
using namespace boost;
static int finished;
static std::string *m;
static void handleDisconnected(Swift::Client *client, const boost::optional<Swift::ClientError> &, const std::string &server) {
std::cout << "[ DISCONNECTED ] " << server << "\n";
if (--finished == 0) {
exit(0);
}
}
static void handleConnected(Swift::Client *client, const std::string &server) {
boost::shared_ptr<Swift::Message> message(new Swift::Message());
message->setTo(server);
message->setFrom(client->getJID());
message->setBody(*m);
client->sendMessage(message);
}
static void handleMessageReceived(Swift::Client *client, Swift::Message::ref message, const std::string &server) {
std::string body = message->getBody();
boost::replace_all(body, "\n", "\n[ OK ] " + server + ": ");
std::cout << "[ OK ] " << server << ": " << body << "\n";
if (--finished == 0) {
exit(0);
}
}
static std::string searchForBinary(const std::string &binary) {
std::vector<std::string> path_list;
char * env_path = getenv("PATH");
if (env_path != NULL) {
std::string buffer = "";
for (int s = 0; s < strlen(env_path); s++) {
if (env_path[s] == ':') {
path_list.insert(path_list.end(), std::string(buffer));
buffer = "";
}
else {
buffer += env_path[s];
}
}
if (buffer != "") {
path_list.insert(path_list.end(), std::string(buffer));
buffer = "";
}
for (std::vector<std::string>::iterator dit = path_list.begin(); dit < path_list.end(); dit++) {
std::string bpath = *dit;
bpath += "/";
bpath += binary;
path p(bpath);
if (exists(p) && !is_directory(p)) {
return bpath;
}
}
}
return "";
}
// Executes new backend
static unsigned long exec_(std::string path, std::string config) {
// fork and exec
pid_t pid = fork();
if ( pid == 0 ) {
// child process
exit(execl(path.c_str(), path.c_str(), config.c_str(), NULL));
} else if ( pid < 0 ) {
// fork failed
}
else {
waitpid(pid, 0, 0);
}
return (unsigned long) pid;
}
static int isRunning(const std::string &pidfile) {
path p(pidfile);
if (!exists(p) || is_directory(p)) {
return 0;
}
std::ifstream f(p.string().c_str(), std::ios_base::in);
std::string pid;
f >> pid;
if (pid.empty())
return 0;
if (kill(boost::lexical_cast<int>(pid), 0) != 0)
return 0;
return boost::lexical_cast<int>(pid);
}
static void start_all_instances(ManagerConfig *config) {
path p(CONFIG_STRING(config, "service.config_directory"));
try {
if (!exists(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(6);
}
if (!is_directory(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(7);
}
std::string spectrum2_binary = searchForBinary("spectrum2");
if (spectrum2_binary.empty()) {
std::cerr << "spectrum2 binary not found in PATH\n";
exit(8);
}
directory_iterator end_itr;
for (directory_iterator itr(p); itr != end_itr; ++itr) {
if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
Config cfg;
if (cfg.load(itr->path().string()) == false) {
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
}
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";
}
}
}
}
catch (const filesystem_error& ex) {
std::cerr << "boost filesystem error\n";
exit(5);
}
}
static void stop_all_instances(ManagerConfig *config) {
path p(CONFIG_STRING(config, "service.config_directory"));
try {
if (!exists(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(6);
}
if (!is_directory(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(7);
}
directory_iterator end_itr;
for (directory_iterator itr(p); itr != end_itr; ++itr) {
if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
Config cfg;
if (cfg.load(itr->path().string()) == false) {
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";
}
}
}
}
catch (const filesystem_error& ex) {
std::cerr << "boost filesystem error\n";
exit(5);
}
}
void ask_local_servers(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &message) {
path p(CONFIG_STRING(config, "service.config_directory"));
try {
if (!exists(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(6);
}
if (!is_directory(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(7);
}
directory_iterator end_itr;
for (directory_iterator itr(p); itr != end_itr; ++itr) {
if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
Config cfg;
if (cfg.load(itr->path().string()) == false) {
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
}
finished++;
Swift::Client *client = new Swift::Client(CONFIG_STRING(&cfg, "service.admin_username"), CONFIG_STRING(&cfg, "service.admin_password"), &networkFactories);
client->setAlwaysTrustCertificates();
client->onConnected.connect(boost::bind(&handleConnected, client, CONFIG_STRING(&cfg, "service.jid")));
client->onDisconnected.connect(bind(&handleDisconnected, client, _1, CONFIG_STRING(&cfg, "service.jid")));
client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, CONFIG_STRING(&cfg, "service.jid")));
Swift::ClientOptions opt;
opt.allowPLAINWithoutTLS = true;
client->connect(opt);
}
}
}
catch (const filesystem_error& ex) {
std::cerr << "boost filesystem error\n";
exit(5);
}
}
int main(int argc, char **argv)
{
ManagerConfig config;
std::string config_file;
std::string command;
boost::program_options::variables_map vm;
boost::program_options::options_description desc("Usage: spectrum [OPTIONS] <COMMAND>\nAllowed options");
desc.add_options()
("help,h", "Show help output")
("config,c", boost::program_options::value<std::string>(&config_file)->default_value("/etc/spectrum2/spectrum_manager.cfg"), "Spectrum manager config file")
("command", boost::program_options::value<std::string>(&command)->default_value(""), "Command")
;
try
{
boost::program_options::positional_options_description p;
p.add("command", -1);
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
options(desc).positional(p).run(), vm);
boost::program_options::notify(vm);
if(vm.count("help"))
{
std::cout << desc << "\n";
return 1;
}
}
catch (std::runtime_error& e)
{
std::cout << desc << "\n";
return 2;
}
catch (...)
{
std::cout << desc << "\n";
return 3;
}
if (!config.load(config_file)) {
std::cerr << "Can't load configuration file.\n";
return 4;
}
if (command.empty()) {
std::cout << desc << "\n";
return 1;
}
if (command == "start") {
start_all_instances(&config);
}
else if (command == "stop") {
stop_all_instances(&config);
}
else {
Swift::SimpleEventLoop eventLoop;
Swift::BoostNetworkFactories networkFactories(&eventLoop);
std::string message = command;
m = &message;
ask_local_servers(&config, networkFactories, message);
std::vector<std::string> servers = CONFIG_VECTOR(&config, "servers.server");
for (std::vector<std::string>::const_iterator it = servers.begin(); it != servers.end(); it++) {
finished++;
Swift::Client *client = new Swift::Client(CONFIG_STRING(&config, "service.admin_username") + "@" + *it, CONFIG_STRING(&config, "service.admin_password"), &networkFactories);
client->setAlwaysTrustCertificates();
client->onConnected.connect(boost::bind(&handleConnected, client, *it));
client->onDisconnected.connect(bind(&handleDisconnected, client, _1, *it));
client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, *it));
Swift::ClientOptions opt;
opt.allowPLAINWithoutTLS = true;
client->connect(opt);
// std::cout << *it << "\n";
}
eventLoop.run();
}
}
|