From 94e7c81fe571dd6b977e1da1c43ca54323fe4de1 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 30 Sep 2008 23:27:30 +0200 Subject: Man kann jetzt mehrere Hosts mit einem Befehl herunterfahren oder neustarten --- src/Client/CommandParser.cpp | 79 ++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 22 deletions(-) (limited to 'src/Client/CommandParser.cpp') diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 1ba986f..135f771 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -39,13 +39,14 @@ namespace Client { const CommandParser::Command CommandParser::commands[] = { {{"help", "?", 0}, "help [command]", "Displays usage information about commands", "Displays usage information about a command. If no command is given, a list of all available commands is displayed.", &CommandParser::helpCommand}, {{"list_hosts", "hosts", 0}, "list_hosts [-a]", "Lists the currently active hosts", "Lists the currently active hosts.\n\n -a\tAlso list inactive hosts", &CommandParser::listHostsCommand}, - {{"reboot", 0}, "reboot host", "Reboots a host", "Reboots a host.", &CommandParser::rebootCommand}, - {{"shutdown", "halt", 0}, "shutdown host", "Shuts a host down", "Shuts a host down.", &CommandParser::shutdownCommand}, + {{"reboot", 0}, "reboot *|host...", "Reboots host", "Reboots hosts. * will reboot all hosts.", &CommandParser::rebootCommand}, + {{"shutdown", "halt", 0}, "shutdown *|host...", "Shuts hosts down", "Shuts hosts down. * will shut down all hosts.", &CommandParser::shutdownCommand}, {{"status", "st", 0}, "status [host]", "Displays status information", "Displays host status information. If no host is given, server status information is displayed.", &CommandParser::statusCommand}, {{"exit", "quit", 0}, "exit", "Closes the connection and quits the client", "Closes the connection and quits the client.", &CommandParser::exitCommand}, {{0}, 0, 0, 0, 0} }; + const CommandParser::Command* CommandParser::findCommand(const std::string& command) { for(int i = 0; commands[i].commands[0] != 0; ++i) { for(int j = 0; commands[i].commands[j] != 0; ++j) { @@ -65,6 +66,42 @@ void CommandParser::printUsage(const std::string& command) { Common::Logger::logf("Usage: %s\n", cmd->cmdline); } + +std::map CommandParser::parseHostList(const std::vector &args, bool mustBeActive) { + const std::map& hosts = InformationManager::getInformationManager()->getDaemons(); + std::map ret; + + for(std::vector::const_iterator arg = args.begin(); arg != args.end(); ++arg) { + if(*arg == "*") { + if(mustBeActive) { + for(std::map::const_iterator host = hosts.begin(); host != hosts.end(); ++host) { + if(host->second.getState() != Common::HostInfo::INACTIVE) + ret.insert(*host); + } + + if(ret.empty()) + Common::Logger::log("No hosts active."); + } + else { + ret = hosts; + } + } + else { + std::map::const_iterator host = hosts.find(*arg); + + if(host == hosts.end()) + Common::Logger::logf(Common::Logger::ERROR, "Host '%s' doesn't exist.", arg->c_str()); + else if(mustBeActive && host->second.getState() == Common::HostInfo::INACTIVE) + Common::Logger::logf(Common::Logger::WARNING, "Host '%s' is inactive.", arg->c_str()); + else + ret.insert(*host); + } + } + + return ret; +} + + void CommandParser::helpCommand(const std::vector &args) { if(args.size() == 1) { std::cout << "Available commands:" << std::endl << std::endl; @@ -86,7 +123,7 @@ void CommandParser::helpCommand(const std::vector &args) { std::cout << command->longdesc << std::endl << std::endl; } else - Common::Logger::logf(Common::Logger::WARNING,"%s: Command '%s' doesn't exist.", args[0].c_str(), args[1].c_str()); + Common::Logger::logf(Common::Logger::WARNING, "%s: Command '%s' doesn't exist.", args[0].c_str(), args[1].c_str()); } else { Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); @@ -152,36 +189,34 @@ void CommandParser::rebootCommand(const std::vector &args) { printUsage("reboot"); return; } - else if(args.size() > 2) { - Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); - printUsage("reboot"); - return; - } - Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr( - new Requests::DaemonCommandRequest(args[1], true, sigc::mem_fun(commandManager, &CommandManager::daemonCommandRequestFinished)) - )); + std::map hosts = parseHostList(std::vector(args.begin()+1, args.end()), true); - commandManager.activeRequests++; + for(std::map::iterator host = hosts.begin(); host != hosts.end(); ++host) { + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr( + new Requests::DaemonCommandRequest(host->first, true, sigc::mem_fun(commandManager, &CommandManager::daemonCommandRequestFinished)) + )); + + commandManager.activeRequests++; + } } void CommandParser::shutdownCommand(const std::vector &args) { if(args.size() < 2) { Common::Logger::logf(Common::Logger::ERROR, "%s: No host given.", args[0].c_str()); - printUsage("reboot"); - return; - } - else if(args.size() > 2) { - Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); - printUsage("reboot"); + printUsage("shutdown"); return; } - Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr( - new Requests::DaemonCommandRequest(args[1], false, sigc::mem_fun(commandManager, &CommandManager::daemonCommandRequestFinished)) - )); + std::map hosts = parseHostList(std::vector(args.begin()+1, args.end()), true); - commandManager.activeRequests++; + for(std::map::iterator host = hosts.begin(); host != hosts.end(); ++host) { + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr( + new Requests::DaemonCommandRequest(host->first, false, sigc::mem_fun(commandManager, &CommandManager::daemonCommandRequestFinished)) + )); + + commandManager.activeRequests++; + } } void CommandParser::statusCommand(const std::vector &args) { -- cgit v1.2.3