use of org.apache.commons.cli.CommandLineParser in project groovy by apache.
the class Groovyc method runCompiler.
private void runCompiler(String[] commandLine) {
// hand crank it so we can add our own compiler configuration
try {
Options options = FileSystemCompiler.createCompilationOptions();
CommandLineParser cliParser = new DefaultParser();
CommandLine cli;
cli = cliParser.parse(options, commandLine);
configuration = FileSystemCompiler.generateCompilerConfigurationFromOptions(cli);
configuration.setScriptExtensions(getScriptExtensions());
String tmpExtension = getScriptExtension();
if (tmpExtension.startsWith("*."))
tmpExtension = tmpExtension.substring(1);
configuration.setDefaultScriptExtension(tmpExtension);
// Load the file name list
String[] filenames = FileSystemCompiler.generateFileNamesFromOptions(cli);
boolean fileNameErrors = filenames == null;
fileNameErrors = fileNameErrors || !FileSystemCompiler.validateFiles(filenames);
if (targetBytecode != null) {
configuration.setTargetBytecode(targetBytecode);
}
if (!fileNameErrors) {
FileSystemCompiler.doCompilation(configuration, makeCompileUnit(), filenames, forceLookupUnnamedFiles);
}
} catch (Exception re) {
Throwable t = re;
if ((re.getClass() == RuntimeException.class) && (re.getCause() != null)) {
// unwrap to the real exception
t = re.getCause();
}
StringWriter writer = new StringWriter();
new ErrorReporter(t, false).write(new PrintWriter(writer));
String message = writer.toString();
taskSuccess = false;
if (errorProperty != null) {
getProject().setNewProperty(errorProperty, "true");
}
if (failOnError) {
log.error(message);
throw new BuildException("Compilation Failed", t, getLocation());
} else {
log.error(message);
}
}
}
use of org.apache.commons.cli.CommandLineParser in project hadoop by apache.
the class NodeInfo method main.
@SuppressWarnings("static-access")
public static void main(String[] args) throws IOException {
GenericOptionsParser genericParser = new GenericOptionsParser(args);
String[] remainingArgs = genericParser.getRemainingArgs();
Option conf = OptionBuilder.hasArg().create("conffile");
Option help = OptionBuilder.withLongOpt("help").create('h');
Options opts = new Options().addOption(conf).addOption(help);
CommandLineParser specificParser = new GnuParser();
CommandLine cmd = null;
try {
cmd = specificParser.parse(opts, remainingArgs);
} catch (MissingArgumentException e) {
terminate(1, "No argument specified for -conffile option");
} catch (ParseException e) {
terminate(1, USAGE);
}
if (cmd == null) {
terminate(1, "Failed to parse options");
}
if (cmd.hasOption('h')) {
terminate(0, USAGE);
}
List<File> files = new ArrayList<File>();
if (cmd.hasOption("conffile")) {
String[] values = cmd.getOptionValues("conffile");
for (String value : values) {
File confFile = new File(value);
if (confFile.isFile()) {
files.add(confFile);
} else if (confFile.isDirectory()) {
for (File file : listFiles(confFile)) {
files.add(file);
}
} else {
terminate(1, confFile.getAbsolutePath() + " is neither a file nor directory");
}
}
} else {
String confDirName = System.getenv(HADOOP_CONF_DIR);
if (confDirName == null) {
terminate(1, HADOOP_CONF_DIR + " is not defined");
}
File confDir = new File(confDirName);
if (!confDir.isDirectory()) {
terminate(1, HADOOP_CONF_DIR + " is not a directory");
}
files = Arrays.asList(listFiles(confDir));
}
if (files.isEmpty()) {
terminate(1, "No input file to validate");
}
boolean ok = true;
for (File file : files) {
String path = file.getAbsolutePath();
List<String> errors = checkConf(new FileInputStream(file));
if (errors.isEmpty()) {
System.out.println(path + ": valid");
} else {
ok = false;
System.err.println(path + ":");
for (String error : errors) {
System.err.println("\t" + error);
}
}
}
if (ok) {
System.out.println("OK");
} else {
terminate(1, "Invalid file exists");
}
}
use of org.apache.commons.cli.CommandLineParser in project flink by apache.
the class MesosApplicationMasterRunner method run.
/**
* The instance entry point for the Mesos AppMaster. Obtains user group
* information and calls the main work method {@link #runPrivileged(Configuration,Configuration)} as a
* privileged action.
*
* @param args The command line arguments.
* @return The process exit code.
*/
protected int run(final String[] args) {
try {
LOG.debug("All environment variables: {}", ENV);
// loading all config values here has the advantage that the program fails fast, if any
// configuration problem occurs
CommandLineParser parser = new PosixParser();
CommandLine cmd = parser.parse(ALL_OPTIONS, args);
final Configuration dynamicProperties = BootstrapTools.parseDynamicProperties(cmd);
GlobalConfiguration.setDynamicProperties(dynamicProperties);
final Configuration config = GlobalConfiguration.loadConfiguration();
// configure the default filesystem
try {
FileSystem.setDefaultScheme(config);
} catch (IOException e) {
throw new IOException("Error while setting the default " + "filesystem scheme from configuration.", e);
}
// configure security
SecurityUtils.SecurityConfiguration sc = new SecurityUtils.SecurityConfiguration(config);
SecurityUtils.install(sc);
// run the actual work in the installed security context
return SecurityUtils.getInstalledContext().runSecured(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return runPrivileged(config, dynamicProperties);
}
});
} catch (Throwable t) {
// make sure that everything whatever ends up in the log
LOG.error("Mesos AppMaster initialization failed", t);
return INIT_ERROR_EXIT_CODE;
}
}
use of org.apache.commons.cli.CommandLineParser in project flink by apache.
the class MesosTaskManagerRunner method runTaskManager.
public static void runTaskManager(String[] args, final Class<? extends TaskManager> taskManager) throws Exception {
EnvironmentInformation.logEnvironmentInfo(LOG, taskManager.getSimpleName(), args);
SignalHandler.register(LOG);
JvmShutdownSafeguard.installAsShutdownHook(LOG);
// try to parse the command line arguments
CommandLineParser parser = new PosixParser();
CommandLine cmd = parser.parse(ALL_OPTIONS, args);
final Configuration configuration;
try {
final Configuration dynamicProperties = BootstrapTools.parseDynamicProperties(cmd);
GlobalConfiguration.setDynamicProperties(dynamicProperties);
LOG.debug("Mesos dynamic properties: {}", dynamicProperties);
configuration = GlobalConfiguration.loadConfiguration();
} catch (Throwable t) {
LOG.error("Failed to load the TaskManager configuration and dynamic properties.", t);
System.exit(TaskManager.STARTUP_FAILURE_RETURN_CODE());
return;
}
// read the environment variables
final Map<String, String> envs = System.getenv();
final String tmpDirs = envs.get(MesosConfigKeys.ENV_FLINK_TMP_DIR);
// configure local directory
String flinkTempDirs = configuration.getString(ConfigConstants.TASK_MANAGER_TMP_DIR_KEY, null);
if (flinkTempDirs != null) {
LOG.info("Overriding Mesos temporary file directories with those " + "specified in the Flink config: {}", flinkTempDirs);
} else if (tmpDirs != null) {
LOG.info("Setting directories for temporary files to: {}", tmpDirs);
configuration.setString(ConfigConstants.TASK_MANAGER_TMP_DIR_KEY, tmpDirs);
}
// configure the default filesystem
try {
FileSystem.setDefaultScheme(configuration);
} catch (IOException e) {
throw new IOException("Error while setting the default " + "filesystem scheme from configuration.", e);
}
// tell akka to die in case of an error
configuration.setBoolean(ConfigConstants.AKKA_JVM_EXIT_ON_FATAL_ERROR, true);
// Infer the resource identifier from the environment variable
String containerID = Preconditions.checkNotNull(envs.get(MesosConfigKeys.ENV_FLINK_CONTAINER_ID));
final ResourceID resourceId = new ResourceID(containerID);
LOG.info("ResourceID assigned for this container: {}", resourceId);
// Run the TM in the security context
SecurityUtils.SecurityConfiguration sc = new SecurityUtils.SecurityConfiguration(configuration);
SecurityUtils.install(sc);
try {
SecurityUtils.getInstalledContext().runSecured(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
TaskManager.selectNetworkInterfaceAndRunTaskManager(configuration, resourceId, taskManager);
return 0;
}
});
} catch (Throwable t) {
LOG.error("Error while starting the TaskManager", t);
System.exit(TaskManager.STARTUP_FAILURE_RETURN_CODE());
}
}
use of org.apache.commons.cli.CommandLineParser in project flink by apache.
the class FlinkYarnSessionCli method run.
public int run(String[] args) {
//
// Command Line Options
//
Options options = new Options();
addGeneralOptions(options);
addRunOptions(options);
CommandLineParser parser = new PosixParser();
CommandLine cmd;
try {
cmd = parser.parse(options, args);
} catch (Exception e) {
System.out.println(e.getMessage());
printUsage();
return 1;
}
// Query cluster for metrics
if (cmd.hasOption(QUERY.getOpt())) {
AbstractYarnClusterDescriptor yarnDescriptor = getClusterDescriptor();
String description;
try {
description = yarnDescriptor.getClusterDescription();
} catch (Exception e) {
System.err.println("Error while querying the YARN cluster for available resources: " + e.getMessage());
e.printStackTrace(System.err);
return 1;
}
System.out.println(description);
return 0;
} else if (cmd.hasOption(APPLICATION_ID.getOpt())) {
AbstractYarnClusterDescriptor yarnDescriptor = getClusterDescriptor();
//configure ZK namespace depending on the value passed
String zkNamespace = cmd.hasOption(ZOOKEEPER_NAMESPACE.getOpt()) ? cmd.getOptionValue(ZOOKEEPER_NAMESPACE.getOpt()) : yarnDescriptor.getFlinkConfiguration().getString(HA_ZOOKEEPER_NAMESPACE_KEY, cmd.getOptionValue(APPLICATION_ID.getOpt()));
LOG.info("Going to use the ZK namespace: {}", zkNamespace);
yarnDescriptor.getFlinkConfiguration().setString(HA_ZOOKEEPER_NAMESPACE_KEY, zkNamespace);
try {
yarnCluster = yarnDescriptor.retrieve(cmd.getOptionValue(APPLICATION_ID.getOpt()));
} catch (Exception e) {
throw new RuntimeException("Could not retrieve existing Yarn application", e);
}
if (detachedMode) {
LOG.info("The Flink YARN client has been started in detached mode. In order to stop " + "Flink on YARN, use the following command or a YARN web interface to stop it:\n" + "yarn application -kill " + APPLICATION_ID.getOpt());
yarnCluster.disconnect();
} else {
runInteractiveCli(yarnCluster, true);
}
} else {
AbstractYarnClusterDescriptor yarnDescriptor;
try {
yarnDescriptor = createDescriptor(null, cmd);
} catch (Exception e) {
System.err.println("Error while starting the YARN Client: " + e.getMessage());
e.printStackTrace(System.err);
return 1;
}
try {
yarnCluster = yarnDescriptor.deploy();
} catch (Exception e) {
System.err.println("Error while deploying YARN cluster: " + e.getMessage());
e.printStackTrace(System.err);
return 1;
}
//------------------ ClusterClient deployed, handle connection details
String jobManagerAddress = yarnCluster.getJobManagerAddress().getAddress().getHostName() + ":" + yarnCluster.getJobManagerAddress().getPort();
System.out.println("Flink JobManager is now running on " + jobManagerAddress);
System.out.println("JobManager Web Interface: " + yarnCluster.getWebInterfaceURL());
// file that we write into the conf/ dir containing the jobManager address and the dop.
File yarnPropertiesFile = getYarnPropertiesLocation(yarnCluster.getFlinkConfiguration());
Properties yarnProps = new Properties();
yarnProps.setProperty(YARN_APPLICATION_ID_KEY, yarnCluster.getApplicationId().toString());
if (yarnDescriptor.getTaskManagerSlots() != -1) {
String parallelism = Integer.toString(yarnDescriptor.getTaskManagerSlots() * yarnDescriptor.getTaskManagerCount());
yarnProps.setProperty(YARN_PROPERTIES_PARALLELISM, parallelism);
}
// add dynamic properties
if (yarnDescriptor.getDynamicPropertiesEncoded() != null) {
yarnProps.setProperty(YARN_PROPERTIES_DYNAMIC_PROPERTIES_STRING, yarnDescriptor.getDynamicPropertiesEncoded());
}
writeYarnProperties(yarnProps, yarnPropertiesFile);
if (detachedMode) {
// print info and quit:
LOG.info("The Flink YARN client has been started in detached mode. In order to stop " + "Flink on YARN, use the following command or a YARN web interface to stop it:\n" + "yarn application -kill " + yarnCluster.getApplicationId() + System.lineSeparator() + "Please also note that the temporary files of the YARN session in {} will not be removed.", yarnDescriptor.getSessionFilesDir());
yarnCluster.waitForClusterToBeReady();
yarnCluster.disconnect();
} else {
runInteractiveCli(yarnCluster, acceptInteractiveInput);
}
}
return 0;
}
Aggregations