diff --git a/spectrum_manager/src/main.cpp b/spectrum_manager/src/main.cpp index 94d29345..9f09d2af 100644 --- a/spectrum_manager/src/main.cpp +++ b/spectrum_manager/src/main.cpp @@ -82,6 +82,7 @@ int main(int argc, char **argv) " spectrum [OPTIONS] \nCommands:\n" " start - start all local Spectrum2 instances\n" " stop - stop all local Spectrum2 instances\n" + " restart - restart all local Spectrum2 instances\n" " status - status of local Spectrum2 instances\n" " - send command to local Spectrum2 instance and print output\n" "Allowed options"); @@ -164,6 +165,9 @@ int main(int argc, char **argv) stop_instances(&config, jid); return 0; } + else if (cmd == "restart") { + return restart_instances(&config, jid); + } ask_local_server(&config, networkFactories, jid, cmd); // std::string message = command; diff --git a/spectrum_manager/src/methods.cpp b/spectrum_manager/src/methods.cpp index a9b56cf4..aabbfee8 100644 --- a/spectrum_manager/src/methods.cpp +++ b/spectrum_manager/src/methods.cpp @@ -276,6 +276,100 @@ void stop_instances(ManagerConfig *config, const std::string &_jid) { } } +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); + } + + 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"; + return 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"; + } + + std::vector vhosts; + if (CONFIG_HAS_KEY(&cfg, "vhosts.vhost")) + 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; + } + + if (!_jid.empty() && _jid != vhost) { + continue; + } + + int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile")); + if (pid) { + response ="Stopping " + itr->path().string() + ": "; + std::cout << "Stopping " << itr->path() << ": "; + kill(pid, SIGTERM); + + sleep(1); + int count = 20; + while (kill(pid, 0) == 0 && count != 0) { + std::cout << "."; + sleep(1); + count--; + } + if (count == 0) { + response += "ERROR (timeout)\n"; + std::cout << " ERROR (timeout)\n"; + } + else { + response += "OK\n"; + std::cout << " OK\n"; + + 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 = "Stopping " + itr->path().string() + ": Not running\n"; + std::cout << "Stopping " << itr->path() << ": Not running\n"; + } + } + } + } + } + catch (const filesystem_error& ex) { + std::cerr << "Filesystem error: " << ex.what() << "\n"; + exit(5); + } + + return rv; +} + int show_status(ManagerConfig *config) { int ret = 0; path p(CONFIG_STRING(config, "service.config_directory")); diff --git a/spectrum_manager/src/methods.h b/spectrum_manager/src/methods.h index 4ee5209f..df21ab47 100644 --- a/spectrum_manager/src/methods.h +++ b/spectrum_manager/src/methods.h @@ -45,6 +45,7 @@ int getPort(const std::string &portfile); int isRunning(const std::string &pidfile); int start_instances(ManagerConfig *config, const std::string &_jid = ""); +int restart_instances(ManagerConfig *config, const std::string &_jid = ""); void stop_instances(ManagerConfig *config, const std::string &_jid = ""); int show_status(ManagerConfig *config);