Changeset - e921269e4e03
[Not reviewed]
0 2 0
Jan Kaluza - 9 years ago 2016-02-03 06:27:42
jkaluza@redhat.com
Spectrum manager: Replace exit(...) with return; and handle errors in the main()
2 files changed with 61 insertions and 48 deletions:
0 comments (0 inline, 0 general)
spectrum_manager/src/main.cpp
Show inline comments
 
@@ -26,12 +26,13 @@ using namespace boost;
 
int main(int argc, char **argv)
 
{
 
	ManagerConfig config;
 
	std::string config_file;
 
	std::vector<std::string> command;
 
	boost::program_options::variables_map vm;
 
	int ret = 0;
 

	
 
	boost::program_options::options_description desc("Usage: spectrum [OPTIONS] <COMMAND>\n"
 
													 "       spectrum [OPTIONS] <instance_JID> <other>\nCommands:\n"
 
													 " start - start all local Spectrum2 instances\n"
 
													 " stop  - stop all local Spectrum2 instances\n"
 
													 " restart  - restart all local Spectrum2 instances\n"
 
@@ -76,25 +77,41 @@ int main(int argc, char **argv)
 
	if (command.empty()) {
 
		std::cout << desc << "\n";
 
		return 1;
 
	}
 

	
 
	if (command[0] == "start") {
 
		return start_instances(&config);
 
		ret = start_instances(&config);
 
		if (get_response().find("Error") == 0) {
 
			std::cerr << get_response();
 
		}
 
		return ret;
 
	}
 
	else if (command[0] == "stop") {
 
		stop_instances(&config);
 
	}
 
	else if (command[0] == "status") {
 
		return show_status(&config);
 
		ret = show_status(&config);
 
		if (get_response().find("Error") == 0) {
 
			std::cerr << get_response();
 
		}
 
		return ret;
 
	}
 
	else if (command[0] == "list") {
 
		std::vector<std::string> list = show_list(&config);
 
		if (get_response().find("Error") == 0) {
 
			std::cerr << get_response();
 
		}
 
		return ret;
 
	}
 
	else if (command[0] == "restart") {
 
		return restart_instances(&config);
 
		ret = restart_instances(&config);
 
		if (get_response().find("Error") == 0) {
 
			std::cerr << get_response();
 
		}
 
		return ret;
 
	}
 
	else if (command[0] == "server") {
 
		Server server(&config, config_file);
 
		if (server.start() == false) {
 
			std::cerr << "Can't set up server handler.\n";
 
			return 1;
 
@@ -110,20 +127,31 @@ int main(int argc, char **argv)
 

	
 
		std::string jid = command[0];
 
		command.erase(command.begin());
 
		std::string cmd = boost::algorithm::join(command, " ");
 

	
 
		if (cmd == "start") {
 
			return start_instances(&config, jid);
 
			ret = start_instances(&config, jid);
 
			if (get_response().find("Error") == 0) {
 
				std::cerr << get_response();
 
			}
 
			return ret;
 
		}
 
		else if (cmd == "stop") {
 
			stop_instances(&config, jid);
 
			return 0;
 
			if (get_response().find("Error") == 0) {
 
				std::cerr << get_response();
 
			}
 
			return ret;
 
		}
 
		else if (cmd == "restart") {
 
			return restart_instances(&config, jid);
 
			ret = restart_instances(&config, jid);
 
			if (get_response().find("Error") == 0) {
 
				std::cerr << get_response();
 
			}
 
			return ret;
 
		}
 

	
 
		ask_local_server(&config, networkFactories, jid, cmd);
 
		eventLoop.runUntilEvents();
 

	
 
		struct timeval td_start,td_end;
spectrum_manager/src/methods.cpp
Show inline comments
 
@@ -150,24 +150,24 @@ int start_instances(ManagerConfig *config, const std::string &_jid) {
 
	int rv = 0;
 
	response = "";
 
	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";
 
			response = "Error: Config directory " + CONFIG_STRING(config, "service.config_directory") + " does not exist\n";
 
			return 6;
 
		}
 

	
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			response = "Error: Config directory " + CONFIG_STRING(config, "service.config_directory") + " does not exist\n";
 
			return 7;
 
		}
 

	
 
		std::string spectrum2_binary = searchForBinary("spectrum2");
 
		if (spectrum2_binary.empty()) {
 
			std::cerr << "spectrum2 binary not found in PATH\n";
 
			response = "Error: spectrum2 binary not found in PATH\n";
 
			return 8;
 
		}
 

	
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
@@ -195,46 +195,44 @@ int start_instances(ManagerConfig *config, const std::string &_jid) {
 
					}
 

	
 
					int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile"));
 
					if (pid == 0) {
 
						int rc;
 
						response = "Starting " + itr->path().string() + ": OK\n";
 
						std::cout << "Starting " << itr->path() << ": OK\n";
 
						exec_(spectrum2_binary, itr->path().string(), vhost, rc);
 
						if (rv == 0) {
 
							rv = rc;
 
						}
 
					}
 
					else {
 
						response = "Starting " + itr->path().string() + ": Already started (PID=" + boost::lexical_cast<std::string>(pid) + ")\n";
 
						std::cout << "Starting " << itr->path() << ": Already started (PID=" << pid << ")\n";
 
					}
 
				}
 
			}
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "Filesystem error: " << ex.what() << "\n";
 
		response = "Error: Filesystem error: " + std::string(ex.what()) + "\n";
 
		return 6;
 
	}
 
	return rv;
 
}
 

	
 
void stop_instances(ManagerConfig *config, const std::string &_jid) {
 
	response = "";
 
	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);
 
			response = "Error: Config directory " + CONFIG_STRING(config, "service.config_directory") + " does not exist\n";
 
			return;
 
		}
 

	
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
			response = "Error: Config directory " + CONFIG_STRING(config, "service.config_directory") + " does not exist\n";
 
			return;
 
		}
 

	
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
				Config cfg;
 
@@ -286,37 +284,37 @@ void stop_instances(ManagerConfig *config, const std::string &_jid) {
 
					}
 
				}
 
			}
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "Filesystem error: " << ex.what() << "\n";
 
		exit(5);
 
		response = "Error: Filesystem error: " + std::string(ex.what()) + "\n";
 
		return;
 
	}
 
}
 

	
 
int restart_instances(ManagerConfig *config, const std::string &_jid) {
 
	response = "";
 
	path p(CONFIG_STRING(config, "service.config_directory"));
 
	int rv = 0;
 
	int rc;
 

	
 
	try {
 
		if (!exists(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(6);
 
			response = "Error: Config directory " + CONFIG_STRING(config, "service.config_directory") + " does not exist\n";
 
			return 1;
 
		}
 

	
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
			response = "Error: Config directory " + CONFIG_STRING(config, "service.config_directory") + " does not exist\n";
 
			return 2;
 
		}
 

	
 
		std::string spectrum2_binary = searchForBinary("spectrum2");
 
		if (spectrum2_binary.empty()) {
 
			std::cerr << "spectrum2 binary not found in PATH\n";
 
			response = "Error: spectrum2 binary not found in PATH\n";
 
			return 8;
 
		}
 

	
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
@@ -378,14 +376,14 @@ int restart_instances(ManagerConfig *config, const std::string &_jid) {
 
					}
 
				}
 
			}
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "Filesystem error: " << ex.what() << "\n";
 
		exit(5);
 
		response = "Error: Filesystem error: " + std::string(ex.what()) + "\n";
 
		return 1;
 
	}
 

	
 
	return rv;
 
}
 

	
 
int show_status(ManagerConfig *config) {
 
@@ -522,18 +520,18 @@ static void handleConnected(boost::shared_ptr<Swift::Connection> m_conn, const s
 
std::string get_config(ManagerConfig *config, const std::string &jid, const std::string &key) {
 
	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);
 
			return "";
 
		}
 

	
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
			return "";
 
		}
 

	
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
				Config cfg;
 
@@ -561,19 +559,19 @@ std::string get_config(ManagerConfig *config, const std::string &jid, const std:
 
void ask_local_server(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &jid, const std::string &message) {
 
	response = "";
 
	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);
 
			response = "Error: Config directory " + CONFIG_STRING(config, "service.config_directory") + " does not exist\n";
 
			return;
 
		}
 

	
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
			response = "Error: Config directory " + CONFIG_STRING(config, "service.config_directory") + " does not exist\n";
 
			return;
 
		}
 

	
 
		bool found = false;
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
@@ -591,50 +589,37 @@ void ask_local_server(ManagerConfig *config, Swift::BoostNetworkFactories &netwo
 

	
 
				boost::shared_ptr<Swift::Connection> m_conn;
 
				m_conn = networkFactories.getConnectionFactory()->createConnection();
 
				m_conn->onDataRead.connect(boost::bind(&handleDataRead, m_conn, _1));
 
				m_conn->onConnectFinished.connect(boost::bind(&handleConnected, m_conn, message, _1));
 
				m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(CONFIG_STRING(&cfg, "service.backend_host")), getPort(CONFIG_STRING(&cfg, "service.portfile"))));
 

	
 
// 				finished++;
 
// 				Swift::Client *client = new Swift::Client(CONFIG_VECTOR(&cfg, "service.admin_jid")[0], 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);
 
			}
 
		}
 

	
 
		if (!found) {
 
			response = "Config file for Spectrum instance with this JID was not found\n";
 
			std::cerr << "Config file for Spectrum instance with this JID was not found\n";
 
// 			exit(20);
 
			response = "Error: Config file for Spectrum instance with this JID was not found\n";
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "Filesystem error: " << ex.what() << "\n";
 
		exit(5);
 
		response = "Error: Filesystem error: " + std::string(ex.what()) + "\n";
 
	}
 
}
 

	
 
std::vector<std::string> show_list(ManagerConfig *config, bool show) {
 
	path p(CONFIG_STRING(config, "service.config_directory"));
 
	std::vector<std::string> list;
 

	
 
	try {
 
		if (!exists(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(6);
 
			return list;
 
		}
 

	
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
			return list;
 
		}
 

	
 
		bool found = false;
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
0 comments (0 inline, 0 general)