summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Client/CommandParser.cpp79
-rw-r--r--src/Client/CommandParser.h4
2 files changed, 61 insertions, 22 deletions
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<std::string, Common::HostInfo> CommandParser::parseHostList(const std::vector<std::string> &args, bool mustBeActive) {
+ const std::map<std::string, Common::HostInfo>& hosts = InformationManager::getInformationManager()->getDaemons();
+ std::map<std::string, Common::HostInfo> ret;
+
+ for(std::vector<std::string>::const_iterator arg = args.begin(); arg != args.end(); ++arg) {
+ if(*arg == "*") {
+ if(mustBeActive) {
+ for(std::map<std::string, Common::HostInfo>::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<std::string, Common::HostInfo>::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<std::string> &args) {
if(args.size() == 1) {
std::cout << "Available commands:" << std::endl << std::endl;
@@ -86,7 +123,7 @@ void CommandParser::helpCommand(const std::vector<std::string> &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<std::string> &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<Common::RequestBase>(
- new Requests::DaemonCommandRequest(args[1], true, sigc::mem_fun(commandManager, &CommandManager::daemonCommandRequestFinished))
- ));
+ std::map<std::string, Common::HostInfo> hosts = parseHostList(std::vector<std::string>(args.begin()+1, args.end()), true);
- commandManager.activeRequests++;
+ for(std::map<std::string, Common::HostInfo>::iterator host = hosts.begin(); host != hosts.end(); ++host) {
+ Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(
+ new Requests::DaemonCommandRequest(host->first, true, sigc::mem_fun(commandManager, &CommandManager::daemonCommandRequestFinished))
+ ));
+
+ commandManager.activeRequests++;
+ }
}
void CommandParser::shutdownCommand(const std::vector<std::string> &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<Common::RequestBase>(
- new Requests::DaemonCommandRequest(args[1], false, sigc::mem_fun(commandManager, &CommandManager::daemonCommandRequestFinished))
- ));
+ std::map<std::string, Common::HostInfo> hosts = parseHostList(std::vector<std::string>(args.begin()+1, args.end()), true);
- commandManager.activeRequests++;
+ for(std::map<std::string, Common::HostInfo>::iterator host = hosts.begin(); host != hosts.end(); ++host) {
+ Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(
+ new Requests::DaemonCommandRequest(host->first, false, sigc::mem_fun(commandManager, &CommandManager::daemonCommandRequestFinished))
+ ));
+
+ commandManager.activeRequests++;
+ }
}
void CommandParser::statusCommand(const std::vector<std::string> &args) {
diff --git a/src/Client/CommandParser.h b/src/Client/CommandParser.h
index 86be22f..daca0b9 100644
--- a/src/Client/CommandParser.h
+++ b/src/Client/CommandParser.h
@@ -21,8 +21,10 @@
#define MAD_CLIENT_COMMANDPARSER_H_
#include "CommandManager.h"
+#include <Common/HostInfo.h>
#include <Common/Request.h>
+#include <map>
#include <string>
#include <vector>
@@ -56,6 +58,8 @@ class CommandParser {
const Command* findCommand(const std::string& command);
void printUsage(const std::string& command);
+ std::map<std::string, Common::HostInfo> parseHostList(const std::vector<std::string> &args, bool mustBeActive = false);
+
void helpCommand(const std::vector<std::string> &args);
void listHostsCommand(const std::vector<std::string> &args);
void rebootCommand(const std::vector<std::string> &args);