use of org.apache.commons.cli.DefaultParser in project alluxio by Alluxio.
the class AlluxioFuse method parseOptions.
/**
* Parses CLI options.
*
* @param args CLI args
* @return Alluxio-FUSE configuration options
*/
private static AlluxioFuseOptions parseOptions(String[] args) {
final Options opts = new Options();
final Option mntPoint = Option.builder("m").hasArg().required(false).longOpt("mount-point").desc("Desired local mount point for alluxio-fuse.").build();
final Option alluxioRoot = Option.builder("r").hasArg().required(false).longOpt("alluxio-root").desc("Path within alluxio that will be used as the root of the FUSE mount " + "(e.g., /users/foo; defaults to /)").build();
final Option help = Option.builder("h").required(false).desc("Print this help").build();
final Option fuseOption = Option.builder("o").valueSeparator(',').required(false).hasArgs().desc("FUSE mount options").build();
opts.addOption(mntPoint);
opts.addOption(alluxioRoot);
opts.addOption(help);
opts.addOption(fuseOption);
final CommandLineParser parser = new DefaultParser();
try {
CommandLine cli = parser.parse(opts, args);
if (cli.hasOption("h")) {
final HelpFormatter fmt = new HelpFormatter();
fmt.printHelp(AlluxioFuse.class.getName(), opts);
return null;
}
String mntPointValue = cli.getOptionValue("m");
String alluxioRootValue = cli.getOptionValue("r");
List<String> fuseOpts = new ArrayList<>();
boolean noUserMaxWrite = true;
if (cli.hasOption("o")) {
String[] fopts = cli.getOptionValues("o");
// keep the -o
for (final String fopt : fopts) {
fuseOpts.add("-o" + fopt);
if (noUserMaxWrite && fopt.startsWith("max_write")) {
noUserMaxWrite = false;
}
}
}
// from conf
if (noUserMaxWrite) {
final long maxWrite = Configuration.getLong(PropertyKey.FUSE_MAXWRITE_BYTES);
fuseOpts.add(String.format("-omax_write=%d", maxWrite));
}
if (mntPointValue == null) {
mntPointValue = Configuration.get(PropertyKey.FUSE_MOUNT_DEFAULT);
LOG.info("Mounting on default {}", mntPointValue);
}
if (alluxioRootValue == null) {
alluxioRootValue = Configuration.get(PropertyKey.FUSE_FS_ROOT);
LOG.info("Using default alluxio root {}", alluxioRootValue);
}
final boolean fuseDebug = Configuration.getBoolean(PropertyKey.FUSE_DEBUG_ENABLED);
return new AlluxioFuseOptions(mntPointValue, alluxioRootValue, fuseDebug, fuseOpts);
} catch (ParseException e) {
System.err.println("Error while parsing CLI: " + e.getMessage());
final HelpFormatter fmt = new HelpFormatter();
fmt.printHelp(AlluxioFuse.class.getName(), opts);
return null;
}
}
use of org.apache.commons.cli.DefaultParser in project alluxio by Alluxio.
the class GetConf method getConf.
/**
* Implements get configuration.
*
* @param args list of arguments
* @return 0 on success, 1 on failures
*/
public static int getConf(String... args) {
CommandLineParser parser = new DefaultParser();
CommandLine cmd;
try {
cmd = parser.parse(OPTIONS, args, true);
} catch (ParseException e) {
printHelp("Unable to parse input args: " + e.getMessage());
return 1;
}
Preconditions.checkNotNull(cmd, "Unable to parse input args");
args = cmd.getArgs();
switch(args.length) {
case 0:
for (Entry<String, String> entry : Configuration.toMap().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(String.format("%s=%s", key, value));
}
break;
case 1:
if (!PropertyKey.isValid(args[0])) {
printHelp(String.format("%s is not a valid configuration key", args[0]));
return 1;
}
PropertyKey key = PropertyKey.fromString(args[0]);
if (!Configuration.containsKey(key)) {
System.out.println("");
} else {
if (cmd.hasOption(UNIT_OPTION_NAME)) {
String arg = cmd.getOptionValue(UNIT_OPTION_NAME).toUpperCase();
Unit unit;
try {
unit = Unit.valueOf(arg);
System.out.println(Configuration.getBytes(key) / unit.getValue());
} catch (IllegalArgumentException e) {
printHelp(String.format("%s is not a valid unit", arg));
return 1;
}
} else {
System.out.println(Configuration.get(key));
}
}
break;
default:
printHelp("More arguments than expected");
return 1;
}
return 0;
}
use of org.apache.commons.cli.DefaultParser in project heron by twitter.
the class SubmitterMain method main.
public static void main(String[] args) throws Exception {
Options options = constructOptions();
Options helpOptions = constructHelpOptions();
CommandLineParser parser = new DefaultParser();
// parse the help options first.
CommandLine cmd = parser.parse(helpOptions, args, true);
if (cmd.hasOption("h")) {
usage(options);
return;
}
try {
// Now parse the required options
cmd = parser.parse(options, args);
} catch (ParseException e) {
usage(options);
throw new RuntimeException("Error parsing command line options: ", e);
}
Level logLevel = Level.INFO;
if (isVerbose(cmd)) {
logLevel = Level.ALL;
}
// init log
LoggingHelper.loggerInit(logLevel, false);
// load the topology definition into topology proto
TopologyAPI.Topology topology = TopologyUtils.getTopology(cmd.getOptionValue("topology_defn"));
Config config = loadConfig(cmd, topology);
LOG.fine("Static config loaded successfully");
LOG.fine(config.toString());
SubmitterMain submitterMain = new SubmitterMain(config, topology);
/* Meaning of exit status code:
- status code = 0:
program exits without error
- 0 < status code < 100:
program fails to execute before program execution. For example,
JVM cannot find or load main class
- 100 <= status code < 200:
program fails to launch after program execution. For example,
topology definition file fails to be loaded
- status code >= 200
program sends out dry-run response */
try {
submitterMain.submitTopology();
} catch (SubmitDryRunResponse response) {
LOG.log(Level.FINE, "Sending out dry-run response");
// Output may contain UTF-8 characters, so we should print using UTF-8 encoding
PrintStream out = new PrintStream(System.out, true, StandardCharsets.UTF_8.name());
out.print(submitterMain.renderDryRunResponse(response));
// Exit with status code 200 to indicate dry-run response is sent out
// SUPPRESS CHECKSTYLE RegexpSinglelineJava
System.exit(200);
// SUPPRESS CHECKSTYLE IllegalCatch
} catch (Exception e) {
/* Since only stderr is used (by logging), we use stdout here to
propagate error message back to Python's executor.py (invoke site). */
LOG.log(Level.FINE, "Exception when submitting topology", e);
System.out.println(e.getMessage());
// Exit with status code 100 to indicate that error has happened on user-land
// SUPPRESS CHECKSTYLE RegexpSinglelineJava
System.exit(100);
}
LOG.log(Level.FINE, "Topology {0} submitted successfully", topology.getName());
}
use of org.apache.commons.cli.DefaultParser in project heron by twitter.
the class RuntimeManagerMain method main.
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException, ParseException {
Options options = constructOptions();
Options helpOptions = constructHelpOptions();
CommandLineParser parser = new DefaultParser();
// parse the help options first.
CommandLine cmd = parser.parse(helpOptions, args, true);
if (cmd.hasOption("h")) {
usage(options);
return;
}
try {
// Now parse the required options
cmd = parser.parse(options, args);
} catch (ParseException e) {
usage(options);
throw new RuntimeException("Error parsing command line options: ", e);
}
Boolean verbose = false;
Level logLevel = Level.INFO;
if (cmd.hasOption("v")) {
logLevel = Level.ALL;
verbose = true;
}
// init log
LoggingHelper.loggerInit(logLevel, false);
String cluster = cmd.getOptionValue("cluster");
String role = cmd.getOptionValue("role");
String environ = cmd.getOptionValue("environment");
String heronHome = cmd.getOptionValue("heron_home");
String configPath = cmd.getOptionValue("config_path");
String overrideConfigFile = cmd.getOptionValue("override_config_file");
String releaseFile = cmd.getOptionValue("release_file");
String topologyName = cmd.getOptionValue("topology_name");
String commandOption = cmd.getOptionValue("command");
String componentParallelism = cmd.getOptionValue("component_parallelism");
// Optional argument in the case of restart
// TODO(karthik): convert into CLI
String containerId = Integer.toString(-1);
if (cmd.hasOption("container_id")) {
containerId = cmd.getOptionValue("container_id");
}
Boolean dryRun = false;
if (cmd.hasOption("u")) {
dryRun = true;
}
// Default dry-run output format type
DryRunFormatType dryRunFormat = DryRunFormatType.TABLE;
if (dryRun && cmd.hasOption("t")) {
String format = cmd.getOptionValue("dry_run_format");
dryRunFormat = DryRunFormatType.getDryRunFormatType(format);
LOG.fine(String.format("Running dry-run mode using format %s", format));
}
Command command = Command.makeCommand(commandOption);
// add config parameters from the command line
Config.Builder commandLineConfig = Config.newBuilder().put(Key.CLUSTER, cluster).put(Key.ROLE, role).put(Key.ENVIRON, environ).put(Key.DRY_RUN, dryRun).put(Key.DRY_RUN_FORMAT_TYPE, dryRunFormat).put(Key.VERBOSE, verbose).put(Key.TOPOLOGY_CONTAINER_ID, containerId);
// This is a command line option, but not a valid config key. Hence we don't use Keys
if (componentParallelism != null) {
commandLineConfig.put(RuntimeManagerRunner.NEW_COMPONENT_PARALLELISM_KEY, componentParallelism);
}
Config.Builder topologyConfig = Config.newBuilder().put(Key.TOPOLOGY_NAME, topologyName);
// build the final config by expanding all the variables
Config config = Config.toLocalMode(Config.newBuilder().putAll(ConfigLoader.loadConfig(heronHome, configPath, releaseFile, overrideConfigFile)).putAll(commandLineConfig.build()).putAll(topologyConfig.build()).build());
LOG.fine("Static config loaded successfully ");
LOG.fine(config.toString());
/* Meaning of exit status code:
- status code = 0:
program exits without error
- 0 < status code < 100:
program fails to execute before program execution. For example,
JVM cannot find or load main class
- 100 <= status code < 200:
program fails to launch after program execution. For example,
topology definition file fails to be loaded
- status code == 200
program sends out dry-run response */
/* Since only stderr is used (by logging), we use stdout here to
propagate any message back to Python's executor.py (invoke site). */
// Create a new instance of RuntimeManagerMain
RuntimeManagerMain runtimeManagerMain = new RuntimeManagerMain(config, command);
try {
runtimeManagerMain.manageTopology();
// SUPPRESS CHECKSTYLE IllegalCatch
} catch (UpdateDryRunResponse response) {
LOG.log(Level.FINE, "Sending out dry-run response");
// Output may contain UTF-8 characters, so we should print using UTF-8 encoding
PrintStream out = new PrintStream(System.out, true, StandardCharsets.UTF_8.name());
out.print(runtimeManagerMain.renderDryRunResponse(response));
// SUPPRESS CHECKSTYLE RegexpSinglelineJava
// Exit with status code 200 to indicate dry-run response is sent out
System.exit(200);
// SUPPRESS CHECKSTYLE IllegalCatch
} catch (Exception e) {
LOG.log(Level.FINE, "Exception when submitting topology", e);
System.out.println(e.getMessage());
// Exit with status code 100 to indicate that error has happened on user-land
// SUPPRESS CHECKSTYLE RegexpSinglelineJava
System.exit(100);
}
}
use of org.apache.commons.cli.DefaultParser in project heron by twitter.
the class SchedulerMain method main.
public static void main(String[] args) throws Exception {
// construct the options and help options first.
Options options = constructOptions();
Options helpOptions = constructHelpOptions();
// parse the options
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(helpOptions, args, true);
// print help, if we receive wrong set of arguments
if (cmd.hasOption("h")) {
usage(options);
return;
}
// Now parse the required options
try {
cmd = parser.parse(options, args);
} catch (ParseException e) {
usage(options);
throw new RuntimeException("Error parsing command line options: ", e);
}
// It returns a new empty Properties instead of null,
// if no properties passed from command line. So no need for null check.
Properties schedulerProperties = cmd.getOptionProperties(SchedulerUtils.SCHEDULER_COMMAND_LINE_PROPERTIES_OVERRIDE_OPTION);
// initialize the scheduler with the options
String topologyName = cmd.getOptionValue("topology_name");
SchedulerMain schedulerMain = createInstance(cmd.getOptionValue("cluster"), cmd.getOptionValue("role"), cmd.getOptionValue("environment"), cmd.getOptionValue("topology_bin"), topologyName, Integer.parseInt(cmd.getOptionValue("http_port")), cmd.hasOption("verbose"), schedulerProperties);
LOG.info("Scheduler command line properties override: " + schedulerProperties.toString());
// run the scheduler
boolean ret = schedulerMain.runScheduler();
// Log the result and exit
if (!ret) {
throw new RuntimeException("Failed to schedule topology: " + topologyName);
} else {
// stop the server and close the state manager
LOG.log(Level.INFO, "Shutting down topology: {0}", topologyName);
}
}
Aggregations