package nagini.client.command;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import nagini.client.NaginiClient;
/**
* Implements all start commands.
*/
public class NaginiCommandStart 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("app")) {
SubCommandStartApp.executeCommand(args);
} else {
printHelp(System.out);
}
}
/**
* Prints command-line help menu.
* */
public static void printHelp(PrintStream stream) {
stream.println();
stream.println("Nagini Start Commands");
stream.println("---------------------");
stream.println("app Start all applications nodes on remote hosts.");
stream.println();
stream.println("To get more information on each command,");
stream.println("please try \'help start <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("app")) {
SubCommandStartApp.printHelp(stream);
} else {
printHelp(stream);
}
}
/**
* start application command
*/
public static class SubCommandStartApp 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);
// optional options
ParserUtils.acceptsAllNodes(parser);
ParserUtils.acceptsNodeMultiple(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(" start app - Start all application nodes on remote hosts");
stream.println();
stream.println("SYNOPSIS");
stream.println(" start app --config <config-path> [-n <node-id-list> | --all-nodes]");
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
*
*/
@SuppressWarnings("unchecked")
public static void executeCommand(String[] args) throws IOException {
OptionParser parser = getParser();
// declare parameters
String configPath = null;
Boolean allNodes = true;
List<Integer> nodeIds = 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);
ParserUtils.checkOptional(options, ParserUtils.OPT_NODE, ParserUtils.OPT_ALL_NODES);
// load parameters
configPath = (String) options.valueOf(ParserUtils.OPT_CONFIG);
if(options.has(ParserUtils.OPT_NODE)) {
nodeIds = (List<Integer>) options.valuesOf(ParserUtils.OPT_NODE);
allNodes = false;
}
// execute command
NaginiClient naginiClient = new NaginiClient(configPath);
if(allNodes) {
naginiClient.serviceOps.startApplicationAllNodes();
} else {
for(Integer nodeId: nodeIds) {
naginiClient.serviceOps.startApplicationOneNode(nodeId);
}
}
}
}
}