package nagini.client.command;
import java.io.IOException;
import java.io.PrintStream;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import nagini.client.NaginiClient;
/**
* Implements all control commands.
*/
public class NaginiCommandControl extends AbstractCommand {
/**
* Parses command-line and directs to sub-commands.
*
* @param args Command-line input
* @throws Exception
*/
public static void executeCommand(String[] args) throws Exception {
String subCmd = (args.length > 0) ? args[0] : "";
args = CommandUtils.copyArrayCutFirst(args);
if(subCmd.equals("ping")) {
SubCommandControlPing.executeCommand(args);
} else if(subCmd.equals("stop")) {
SubCommandControlStop.executeCommand(args);
} else if(subCmd.equals("start")) {
SubCommandControlStart.executeCommand(args);
} else {
printHelp(System.out);
}
}
/**
* Prints command-line help menu.
* */
public static void printHelp(PrintStream stream) {
stream.println();
stream.println("Nagini Control Commands");
stream.println("-----------------------");
stream.println("ping Ping all remote servers.");
stream.println("stop Stop all remote servers.");
stream.println("start Start all remote servers.");
stream.println();
stream.println("To get more information on each command,");
stream.println("please try \'help control <command-name>\'.");
stream.println();
}
/**
* Parses command-line input and prints help menu.
*
* @throws Exception
*/
public static void executeHelp(String[] args, PrintStream stream) throws Exception {
String subCmd = (args.length > 0) ? args[0] : "";
if(subCmd.equals("ping")) {
SubCommandControlPing.printHelp(stream);
} else if(subCmd.equals("stop")) {
SubCommandControlStop.printHelp(stream);
} else if(subCmd.equals("start")) {
SubCommandControlStart.printHelp(stream);
} else {
printHelp(stream);
}
}
/**
* control ping command
*/
public static class SubCommandControlPing extends AbstractCommand {
/**
* Initializes parser
*
* @return OptionParser object with all available options
*/
protected static OptionParser getParser() {
OptionParser parser = new OptionParser();
// help options
ParserUtils.acceptsHelp(parser);
// required options
ParserUtils.acceptsConfig(parser);
return parser;
}
/**
* Prints help menu for command.
*
* @param stream PrintStream object for output
* @throws IOException
*/
public static void printHelp(PrintStream stream) throws IOException {
stream.println();
stream.println("NAME");
stream.println(" control ping - Print all remote Nagini server status");
stream.println();
stream.println("SYNOPSIS");
stream.println(" control ping --config <config-path>");
stream.println();
getParser().printHelpOn(stream);
stream.println();
}
/**
* Parses command-line and executes command.
*
* @param args Command-line input
* @param printHelp Tells whether to print help only or execute command
* actually
* @throws IOException
*
*/
public static void executeCommand(String[] args) throws IOException {
OptionParser parser = getParser();
// declare parameters
String configPath = null;
// parse command-line input
OptionSet options = parser.parse(args);
if(options.has(ParserUtils.OPT_HELP)) {
printHelp(System.out);
return;
}
// check required options and/or conflicting options
ParserUtils.checkRequired(options, ParserUtils.OPT_CONFIG);
// load parameters
configPath = (String) options.valueOf(ParserUtils.OPT_CONFIG);
// execute command
new NaginiClient(configPath).controlOps.pingAllHosts();
}
}
/**
* control stop command
*/
public static class SubCommandControlStop extends AbstractCommand {
/**
* Initializes parser
*
* @return OptionParser object with all available options
*/
protected static OptionParser getParser() {
OptionParser parser = new OptionParser();
// help options
ParserUtils.acceptsHelp(parser);
// required options
ParserUtils.acceptsConfig(parser);
return parser;
}
/**
* Prints help menu for command.
*
* @param stream PrintStream object for output
* @throws IOException
*/
public static void printHelp(PrintStream stream) throws IOException {
stream.println();
stream.println("NAME");
stream.println(" control stop - Stop all remote Nagini servers");
stream.println();
stream.println("SYNOPSIS");
stream.println(" control stop --config <config-path>");
stream.println();
getParser().printHelpOn(stream);
stream.println();
}
/**
* Parses command-line and executes command.
*
* @param args Command-line input
* @param printHelp Tells whether to print help only or execute command
* actually
* @throws IOException
*
*/
public static void executeCommand(String[] args) throws IOException {
OptionParser parser = getParser();
// declare parameters
String configPath = null;
// parse command-line input
OptionSet options = parser.parse(args);
if(options.has(ParserUtils.OPT_HELP)) {
printHelp(System.out);
return;
}
// check required options and/or conflicting options
ParserUtils.checkRequired(options, ParserUtils.OPT_CONFIG);
// load parameters
configPath = (String) options.valueOf(ParserUtils.OPT_CONFIG);
// execute command
new NaginiClient(configPath).controlOps.stopAllHosts();
}
}
/**
* control start command
*/
public static class SubCommandControlStart extends AbstractCommand {
/**
* Initializes parser
*
* @return OptionParser object with all available options
*/
protected static OptionParser getParser() {
OptionParser parser = new OptionParser();
// help options
ParserUtils.acceptsHelp(parser);
// required options
ParserUtils.acceptsConfig(parser);
return parser;
}
/**
* Prints help menu for command.
*
* @param stream PrintStream object for output
* @throws IOException
*/
public static void printHelp(PrintStream stream) throws IOException {
stream.println();
stream.println("NAME");
stream.println(" control start - Start all remote Nagini servers");
stream.println();
stream.println("SYNOPSIS");
stream.println(" control start --config <config-path>");
stream.println();
getParser().printHelpOn(stream);
stream.println();
}
/**
* Parses command-line and executes command.
*
* @param args Command-line input
* @param printHelp Tells whether to print help only or execute command
* actually
* @throws Exception
*
*/
public static void executeCommand(String[] args) throws Exception {
OptionParser parser = getParser();
// declare parameters
String configPath = null;
// parse command-line input
OptionSet options = parser.parse(args);
if(options.has(ParserUtils.OPT_HELP)) {
printHelp(System.out);
return;
}
// check required options and/or conflicting options
ParserUtils.checkRequired(options, ParserUtils.OPT_CONFIG);
// load parameters
configPath = (String) options.valueOf(ParserUtils.OPT_CONFIG);
// execute command
new NaginiClient(configPath).controlOps.startAllHosts();
}
}
}