Example usage for org.apache.commons.cli Option UNLIMITED_VALUES

List of usage examples for org.apache.commons.cli Option UNLIMITED_VALUES

Introduction

In this page you can find the example usage for org.apache.commons.cli Option UNLIMITED_VALUES.

Prototype

int UNLIMITED_VALUES

To view the source code for org.apache.commons.cli Option UNLIMITED_VALUES.

Click Source Link

Document

constant that specifies the number of argument values is infinite

Usage

From source file:org.apache.hadoop.yarn.applications.distributedengine.Client.java

Client(String appMasterMainClass, Configuration conf) {
    this.conf = conf;
    this.appMasterMainClass = appMasterMainClass;
    yarnClient = YarnClient.createYarnClient();
    yarnClient.init(conf);/*  www . jav  a2s.c  om*/
    opts = new Options();
    opts.addOption("appname", true, "Application Name. Default value - DistributedEngine");
    opts.addOption("priority", true, "Application Priority. Default 0");
    opts.addOption("queue", true, "RM Queue in which this application is to be submitted");
    opts.addOption("timeout", true, "Application timeout in milliseconds");
    opts.addOption("master_memory", true,
            "Amount of memory in MB to be requested to run the application master");
    opts.addOption("master_vcores", true,
            "Amount of virtual cores to be requested to run the application master");
    opts.addOption("jar", true, "Jar file containing the application master");
    opts.addOption("shell_command", true, "Shell command to be executed by "
            + "the Application Master. Can only specify either --shell_command " + "or --shell_script");
    opts.addOption("shell_script", true, "Location of the shell script to be "
            + "executed. Can only specify either --shell_command or --shell_script");
    opts.addOption("shell_args", true,
            "Command line args for the shell script." + "Multiple args can be separated by empty space.");
    opts.getOption("shell_args").setArgs(Option.UNLIMITED_VALUES);
    opts.addOption("shell_env", true, "Environment for shell script. Specified as env_key=env_val pairs");
    opts.addOption("shell_cmd_priority", true, "Priority for the shell command containers");
    opts.addOption("container_memory", true, "Amount of memory in MB to be requested to run the shell command");
    opts.addOption("container_vcores", true,
            "Amount of virtual cores to be requested to run the shell command");
    opts.addOption("num_containers", true, "No. of containers on which the shell command needs to be executed");
    opts.addOption("log_properties", true, "log4j.properties file");
    opts.addOption("keep_containers_across_application_attempts", false,
            "Flag to indicate whether to keep containers across application attempts."
                    + " If the flag is true, running containers will not be killed when"
                    + " application attempt fails and these containers will be retrieved by"
                    + " the new application attempt ");
    opts.addOption("debug", false, "Dump out debug information");
    opts.addOption("help", false, "Print usage");

}

From source file:org.apache.hadoop.yarn.applications.distributedshell.Client.java

Client(String appMasterMainClass, Configuration conf) {
    this.conf = conf;
    this.appMasterMainClass = appMasterMainClass;
    yarnClient = YarnClient.createYarnClient();
    yarnClient.init(conf);/*ww w.ja  v a2 s  . c  om*/
    opts = new Options();
    opts.addOption("appname", true, "Application Name. Default value - DistributedShell");
    opts.addOption("priority", true, "Application Priority. Default 0");
    opts.addOption("queue", true, "RM Queue in which this application is to be submitted");
    opts.addOption("timeout", true, "Application timeout in milliseconds");
    opts.addOption("master_memory", true,
            "Amount of memory in MB to be requested to run the application master");
    opts.addOption("master_vcores", true,
            "Amount of virtual cores to be requested to run the application master");
    opts.addOption("jar", true, "Jar file containing the application master");
    opts.addOption("shell_command", true, "Shell command to be executed by "
            + "the Application Master. Can only specify either --shell_command " + "or --shell_script");
    opts.addOption("shell_script", true, "Location of the shell script to be "
            + "executed. Can only specify either --shell_command or --shell_script");
    opts.addOption("shell_args", true,
            "Command line args for the shell script." + "Multiple args can be separated by empty space.");
    opts.getOption("shell_args").setArgs(Option.UNLIMITED_VALUES);
    opts.addOption("shell_env", true, "Environment for shell script. Specified as env_key=env_val pairs");
    opts.addOption("shell_cmd_priority", true, "Priority for the shell command containers");
    opts.addOption("container_memory", true, "Amount of memory in MB to be requested to run the shell command");
    opts.addOption("container_vcores", true,
            "Amount of virtual cores to be requested to run the shell command");
    opts.addOption("container_gpus", true, "Amount of GPUs to be requested to run the shell command");
    opts.addOption("num_containers", true, "No. of containers on which the shell command needs to be executed");
    opts.addOption("log_properties", true, "log4j.properties file");
    opts.addOption("keep_containers_across_application_attempts", false,
            "Flag to indicate whether to keep containers across application attempts."
                    + " If the flag is true, running containers will not be killed when"
                    + " application attempt fails and these containers will be retrieved by"
                    + " the new application attempt ");
    opts.addOption("attempt_failures_validity_interval", true,
            "when attempt_failures_validity_interval in milliseconds is set to > 0,"
                    + "the failure number will not take failures which happen out of "
                    + "the validityInterval into failure count. "
                    + "If failure count reaches to maxAppAttempts, " + "the application will be failed.");
    opts.addOption("debug", false, "Dump out debug information");
    opts.addOption("domain", true, "ID of the timeline domain where the " + "timeline entities will be put");
    opts.addOption("view_acls", true,
            "Users and groups that allowed to " + "view the timeline entities in the given domain");
    opts.addOption("modify_acls", true,
            "Users and groups that allowed to " + "modify the timeline entities in the given domain");
    opts.addOption("create", false,
            "Flag to indicate whether to create the " + "domain specified with -domain.");
    opts.addOption("help", false, "Print usage");
    opts.addOption("node_label_expression", true,
            "Node label expression to determine the nodes" + " where all the containers of this application"
                    + " will be allocated, \"\" means containers"
                    + " can be allocated anywhere, if you don't specify the option,"
                    + " default node_label_expression of queue will be used.");
}

From source file:org.apache.hadoop.yarn.applications.distributedshell.DshellClient.java

DshellClient(String appMasterMainClass, Configuration conf) {
    this.conf = conf;
    this.appMasterMainClass = appMasterMainClass;
    yarnClient = YarnClient.createYarnClient();
    yarnClient.init(conf);/*  w  w w .  ja v  a 2s  . c  o m*/
    opts = new Options();
    opts.addOption("appname", true, "Application Name. Default value - DistributedShell");
    opts.addOption("priority", true, "Application Priority. Default 0");
    opts.addOption("queue", true, "RM Queue in which this application is to be submitted");
    opts.addOption("timeout", true, "Application timeout in milliseconds");
    opts.addOption("master_memory", true,
            "Amount of memory in MB to be requested to run the application master");
    opts.addOption("master_vcores", true,
            "Amount of virtual cores to be requested to run the application master");
    opts.addOption("jar", true, "Jar file containing the application master");
    opts.addOption("shell_command", true, "Shell command to be executed by "
            + "the Application Master. Can only specify either --shell_command " + "or --shell_script");
    opts.addOption("shell_script", true, "Location of the shell script to be "
            + "executed. Can only specify either --shell_command or --shell_script");
    opts.addOption("shell_args", true,
            "Command line args for the shell script." + "Multiple args can be separated by empty space.");
    opts.getOption("shell_args").setArgs(Option.UNLIMITED_VALUES);
    opts.addOption("shell_env", true, "Environment for shell script. Specified as env_key=env_val pairs");
    opts.addOption("shell_cmd_priority", true, "Priority for the shell command containers");
    opts.addOption("container_memory", true, "Amount of memory in MB to be requested to run the shell command");
    opts.addOption("container_vcores", true,
            "Amount of virtual cores to be requested to run the shell command");
    // num_containers??containerscontainers1 
    //opts.addOption("num_containers", true, "No. of containers on which the shell command needs to be executed");
    // 
    // container_files?container_archives?? 
    opts.addOption("container_files", true, "The files that containers will run .  Separated by comma");
    opts.addOption("container_archives", true, "The archives that containers will unzip.  Separated by comma");
    // 
    opts.addOption("log_properties", true, "log4j.properties file");
    opts.addOption("keep_containers_across_application_attempts", false,
            "Flag to indicate whether to keep containers across application attempts."
                    + " If the flag is true, running containers will not be killed when"
                    + " application attempt fails and these containers will be retrieved by"
                    + " the new application attempt ");
    opts.addOption("attempt_failures_validity_interval", true,
            "when attempt_failures_validity_interval in milliseconds is set to > 0,"
                    + "the failure number will not take failures which happen out of "
                    + "the validityInterval into failure count. "
                    + "If failure count reaches to maxAppAttempts, " + "the application will be failed.");
    opts.addOption("debug", false, "Dump out debug information");
    opts.addOption("domain", true, "ID of the timeline domain where the " + "timeline entities will be put");
    opts.addOption("view_acls", true,
            "Users and groups that allowed to " + "view the timeline entities in the given domain");
    opts.addOption("modify_acls", true,
            "Users and groups that allowed to " + "modify the timeline entities in the given domain");
    opts.addOption("create", false,
            "Flag to indicate whether to create the " + "domain specified with -domain.");
    opts.addOption("help", false, "Print usage");
    opts.addOption("node_label_expression", true,
            "Node label expression to determine the nodes" + " where all the containers of this application"
                    + " will be allocated, \"\" means containers"
                    + " can be allocated anywhere, if you don't specify the option,"
                    + " default node_label_expression of queue will be used.");
}

From source file:org.apache.hadoop.yarn.applications.ivic.Client.java

/**
 * appMasterMainClass:org.apache.hadoop.yarn.applications.ivic.ApplicationMaster
 * conf<code>YarnConfiguration<code>yarn??
 *///from   w  w w  .j  ava2s. com
Client(String appMasterMainClass, Configuration conf) {
    LOG.info("**********??");
    this.conf = conf;
    this.appMasterMainClass = appMasterMainClass;
    yarnClient = YarnClient.createYarnClient();
    yarnClient.init(conf);
    opts = new Options();
    LOG.info("*******************");
    //add portal user's id
    opts.addOption("user_id", true, "the current user's id.");
    opts.addOption("appname", true, "Application Name. Default value - ivic");
    opts.addOption("priority", true, "Application Priority. Default 0");
    opts.addOption("queue", true, "RM Queue in which this application is to be submitted");
    opts.addOption("timeout", true, "Application timeout in milliseconds");
    opts.addOption("master_memory", true,
            "Amount of memory in MB to be requested to run the application master");
    opts.addOption("master_vcores", true,
            "Amount of virtual cores to be requested to run the application master");
    opts.addOption("jar", true, "Jar file containing the application master");
    opts.addOption("shell_command", true, "Shell command to be executed by "
            + "the Application Master. Can only specify either --shell_command " + "or --shell_script");
    opts.addOption("shell_script", true, "Location of the shell script to be "
            + "executed. Can only specify either --shell_command or --shell_script");
    opts.addOption("shell_args", true,
            "Command line args for the shell script." + "Multiple args can be separated by empty space.");
    opts.getOption("shell_args").setArgs(Option.UNLIMITED_VALUES);
    opts.addOption("shell_env", true, "Environment for shell script. Specified as env_key=env_val pairs");
    opts.addOption("shell_cmd_priority", true, "Priority for the shell command containers");
    opts.addOption("container_memory", true, "Amount of memory in MB to be requested to run the shell command");
    opts.addOption("container_vcores", true,
            "Amount of virtual cores to be requested to run the shell command");
    opts.addOption("num_containers", true, "No. of containers on which the shell command needs to be executed");
    opts.addOption("log_properties", true, "log4j.properties file");
    opts.addOption("keep_containers_across_application_attempts", false,
            "Flag to indicate whether to keep containers across application attempts."
                    + " If the flag is true, running containers will not be killed when"
                    + " application attempt fails and these containers will be retrieved by"
                    + " the new application attempt ");
    opts.addOption("attempt_failures_validity_interval", true,
            "when attempt_failures_validity_interval in milliseconds is set to > 0,"
                    + "the failure number will not take failures which happen out of "
                    + "the validityInterval into failure count. "
                    + "If failure count reaches to maxAppAttempts, " + "the application will be failed.");
    opts.addOption("debug", false, "Dump out debug information");
    opts.addOption("domain", true, "ID of the timeline domain where the " + "timeline entities will be put");
    opts.addOption("view_acls", true,
            "Users and groups that allowed to " + "view the timeline entities in the given domain");
    opts.addOption("modify_acls", true,
            "Users and groups that allowed to " + "modify the timeline entities in the given domain");
    opts.addOption("create", false,
            "Flag to indicate whether to create the " + "domain specified with -domain.");
    opts.addOption("help", false, "Print usage");
    opts.addOption("node_label_expression", true,
            "Node label expression to determine the nodes" + " where all the containers of this application"
                    + " will be allocated, \"\" means containers"
                    + " can be allocated anywhere, if you don't specify the option,"
                    + " default node_label_expression of queue will be used.");
    LOG.info("**********YarnClient??????*********");
}

From source file:org.apache.hadoop.yarn.applications.mapred.Client.java

Client(String appMasterMainClass, Configuration conf) {
    this.conf = conf;
    this.appMasterMainClass = appMasterMainClass;
    yarnClient = YarnClient.createYarnClient();
    yarnClient.init(conf);// www.  j  a v a 2s  . com
    opts = new Options();
    opts.addOption("appname", true, "Application Name. Default value - DistributedShell");
    opts.addOption("priority", true, "Application Priority. Default 0");
    opts.addOption("queue", true, "RM Queue in which this application is to be submitted");
    opts.addOption("timeout", true, "Application timeout in milliseconds");
    opts.addOption("master_memory", true,
            "Amount of memory in MB to be requested to run the application master");
    opts.addOption("master_vcores", true,
            "Amount of virtual cores to be requested to run the application master");
    opts.addOption("jar", true, "Jar file containing the application master");
    opts.addOption("shell_command", true, "Shell command to be executed by "
            + "the Application Master. Can only specify either --shell_command " + "or --shell_script");
    opts.addOption("shell_script", true, "Location of the shell script to be "
            + "executed. Can only specify either --shell_command or --shell_script");
    opts.addOption("shell_args", true,
            "Command line args for the shell script." + "Multiple args can be separated by empty space.");
    opts.getOption("shell_args").setArgs(Option.UNLIMITED_VALUES);
    opts.addOption("shell_env", true, "Environment for shell script. Specified as env_key=env_val pairs");
    opts.addOption("shell_cmd_priority", true, "Priority for the shell command containers");
    opts.addOption("container_memory", true, "Amount of memory in MB to be requested to run the shell command");
    opts.addOption("container_vcores", true,
            "Amount of virtual cores to be requested to run the shell command");
    opts.addOption("num_mappers", true, "No. of containers on which the shell command needs to be executed");
    opts.addOption("num_reducers", true, "No. of containers on which the shell command needs to be executed");
    opts.addOption("log_properties", true, "log4j.properties file");
    opts.addOption("keep_containers_across_application_attempts", false,
            "Flag to indicate whether to keep containers across application attempts."
                    + " If the flag is true, running containers will not be killed when"
                    + " application attempt fails and these containers will be retrieved by"
                    + " the new application attempt ");
    opts.addOption("debug", false, "Dump out debug information");
    opts.addOption("help", false, "Print usage");

}

From source file:org.apache.hadoop.yarn.client.cli.ApplicationCLI.java

@Override
public int run(String[] args) throws Exception {
    Options opts = new Options();
    String title = null;/*w  w w .  j a  v a 2 s.c  o m*/
    if (args.length > 0 && args[0].equalsIgnoreCase(APPLICATION)) {
        title = APPLICATION;
        opts.addOption(STATUS_CMD, true, "Prints the status of the application.");
        opts.addOption(LIST_CMD, false,
                "List applications. " + "Supports optional use of -appTypes to filter applications "
                        + "based on application type, "
                        + "and -appStates to filter applications based on application state.");
        opts.addOption(MOVE_TO_QUEUE_CMD, true, "Moves the application to a " + "different queue.");
        opts.addOption(QUEUE_CMD, true,
                "Works with the movetoqueue command to" + " specify which queue to move an application to.");
        opts.addOption(HELP_CMD, false, "Displays help for all commands.");
        Option appTypeOpt = new Option(APP_TYPE_CMD, true, "Works with -list to "
                + "filter applications based on " + "input comma-separated list of application types.");
        appTypeOpt.setValueSeparator(',');
        appTypeOpt.setArgs(Option.UNLIMITED_VALUES);
        appTypeOpt.setArgName("Types");
        opts.addOption(appTypeOpt);
        Option appStateOpt = new Option(APP_STATE_CMD, true,
                "Works with -list " + "to filter applications based on input comma-separated list of "
                        + "application states. " + getAllValidApplicationStates());
        appStateOpt.setValueSeparator(',');
        appStateOpt.setArgs(Option.UNLIMITED_VALUES);
        appStateOpt.setArgName("States");
        opts.addOption(appStateOpt);
        opts.addOption(APP_ID, true, "Specify Application Id to be operated");
        opts.addOption(UPDATE_PRIORITY, true,
                "update priority of an application. ApplicationId can be" + " passed using 'appId' option.");
        Option killOpt = new Option(KILL_CMD, true,
                "Kills the application. " + "Set of applications can be provided separated with space");
        killOpt.setValueSeparator(' ');
        killOpt.setArgs(Option.UNLIMITED_VALUES);
        killOpt.setArgName("Application ID");
        opts.addOption(killOpt);
        opts.getOption(MOVE_TO_QUEUE_CMD).setArgName("Application ID");
        opts.getOption(QUEUE_CMD).setArgName("Queue Name");
        opts.getOption(STATUS_CMD).setArgName("Application ID");
        opts.getOption(APP_ID).setArgName("Application ID");
        opts.getOption(UPDATE_PRIORITY).setArgName("Priority");
    } else if (args.length > 0 && args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) {
        title = APPLICATION_ATTEMPT;
        opts.addOption(STATUS_CMD, true, "Prints the status of the application attempt.");
        opts.addOption(LIST_CMD, true, "List application attempts for application.");
        opts.addOption(FAIL_CMD, true, "Fails application attempt.");
        opts.addOption(HELP_CMD, false, "Displays help for all commands.");
        opts.getOption(STATUS_CMD).setArgName("Application Attempt ID");
        opts.getOption(LIST_CMD).setArgName("Application ID");
        opts.getOption(FAIL_CMD).setArgName("Application Attempt ID");
    } else if (args.length > 0 && args[0].equalsIgnoreCase(CONTAINER)) {
        title = CONTAINER;
        opts.addOption(STATUS_CMD, true, "Prints the status of the container.");
        opts.addOption(LIST_CMD, true, "List containers for application attempt.");
        opts.addOption(HELP_CMD, false, "Displays help for all commands.");
        opts.getOption(STATUS_CMD).setArgName("Container ID");
        opts.getOption(LIST_CMD).setArgName("Application Attempt ID");
        opts.addOption(SIGNAL_CMD, true,
                "Signal the container. The available signal commands are "
                        + java.util.Arrays.asList(SignalContainerCommand.values())
                        + " Default command is OUTPUT_THREAD_DUMP.");
        opts.getOption(SIGNAL_CMD).setArgName("container ID [signal command]");
        opts.getOption(SIGNAL_CMD).setArgs(3);
    }

    int exitCode = -1;
    CommandLine cliParser = null;
    try {
        cliParser = new GnuParser().parse(opts, args);
    } catch (MissingArgumentException ex) {
        sysout.println("Missing argument for options");
        printUsage(title, opts);
        return exitCode;
    }

    if (cliParser.hasOption(STATUS_CMD)) {
        if (args.length != 3) {
            printUsage(title, opts);
            return exitCode;
        }
        if (args[0].equalsIgnoreCase(APPLICATION)) {
            exitCode = printApplicationReport(cliParser.getOptionValue(STATUS_CMD));
        } else if (args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) {
            exitCode = printApplicationAttemptReport(cliParser.getOptionValue(STATUS_CMD));
        } else if (args[0].equalsIgnoreCase(CONTAINER)) {
            exitCode = printContainerReport(cliParser.getOptionValue(STATUS_CMD));
        }
        return exitCode;
    } else if (cliParser.hasOption(LIST_CMD)) {
        if (args[0].equalsIgnoreCase(APPLICATION)) {
            allAppStates = false;
            Set<String> appTypes = new HashSet<String>();
            if (cliParser.hasOption(APP_TYPE_CMD)) {
                String[] types = cliParser.getOptionValues(APP_TYPE_CMD);
                if (types != null) {
                    for (String type : types) {
                        if (!type.trim().isEmpty()) {
                            appTypes.add(StringUtils.toUpperCase(type).trim());
                        }
                    }
                }
            }

            EnumSet<YarnApplicationState> appStates = EnumSet.noneOf(YarnApplicationState.class);
            if (cliParser.hasOption(APP_STATE_CMD)) {
                String[] states = cliParser.getOptionValues(APP_STATE_CMD);
                if (states != null) {
                    for (String state : states) {
                        if (!state.trim().isEmpty()) {
                            if (state.trim().equalsIgnoreCase(ALLSTATES_OPTION)) {
                                allAppStates = true;
                                break;
                            }
                            try {
                                appStates.add(
                                        YarnApplicationState.valueOf(StringUtils.toUpperCase(state).trim()));
                            } catch (IllegalArgumentException ex) {
                                sysout.println("The application state " + state + " is invalid.");
                                sysout.println(getAllValidApplicationStates());
                                return exitCode;
                            }
                        }
                    }
                }
            }
            listApplications(appTypes, appStates);
        } else if (args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) {
            if (args.length != 3) {
                printUsage(title, opts);
                return exitCode;
            }
            listApplicationAttempts(cliParser.getOptionValue(LIST_CMD));
        } else if (args[0].equalsIgnoreCase(CONTAINER)) {
            if (args.length != 3) {
                printUsage(title, opts);
                return exitCode;
            }
            listContainers(cliParser.getOptionValue(LIST_CMD));
        }
    } else if (cliParser.hasOption(KILL_CMD)) {
        if (args.length < 3 || hasAnyOtherCLIOptions(cliParser, opts, KILL_CMD)) {
            printUsage(title, opts);
            return exitCode;
        }
        return killApplication(cliParser.getOptionValues(KILL_CMD));
    } else if (cliParser.hasOption(MOVE_TO_QUEUE_CMD)) {
        if (!cliParser.hasOption(QUEUE_CMD)) {
            printUsage(title, opts);
            return exitCode;
        }
        moveApplicationAcrossQueues(cliParser.getOptionValue(MOVE_TO_QUEUE_CMD),
                cliParser.getOptionValue(QUEUE_CMD));
    } else if (cliParser.hasOption(FAIL_CMD)) {
        if (!args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) {
            printUsage(title, opts);
            return exitCode;
        }
        failApplicationAttempt(cliParser.getOptionValue(FAIL_CMD));
    } else if (cliParser.hasOption(HELP_CMD)) {
        printUsage(title, opts);
        return 0;
    } else if (cliParser.hasOption(UPDATE_PRIORITY)) {
        if (!cliParser.hasOption(APP_ID)) {
            printUsage(title, opts);
            return exitCode;
        }
        updateApplicationPriority(cliParser.getOptionValue(APP_ID), cliParser.getOptionValue(UPDATE_PRIORITY));
    } else if (cliParser.hasOption(SIGNAL_CMD)) {
        if (args.length < 3 || args.length > 4) {
            printUsage(title, opts);
            return exitCode;
        }
        final String[] signalArgs = cliParser.getOptionValues(SIGNAL_CMD);
        final String containerId = signalArgs[0];
        SignalContainerCommand command = SignalContainerCommand.OUTPUT_THREAD_DUMP;
        if (signalArgs.length == 2) {
            command = SignalContainerCommand.valueOf(signalArgs[1]);
        }
        signalToContainer(containerId, command);
    } else {
        syserr.println("Invalid Command Usage : ");
        printUsage(title, opts);
    }
    return 0;
}

From source file:org.apache.hadoop.yarn.client.cli.LogsCLI.java

@Override
public int run(String[] args) throws Exception {

    Options opts = new Options();
    opts.addOption(HELP_CMD, false, "Displays help for all commands.");
    Option appIdOpt = new Option(APPLICATION_ID_OPTION, true, "ApplicationId (required)");
    appIdOpt.setRequired(true);/*from   w ww .  j  av a  2s  . c o  m*/
    opts.addOption(appIdOpt);
    opts.addOption(CONTAINER_ID_OPTION, true,
            "ContainerId. " + "By default, it will only print syslog if the application is runing."
                    + " Work with -logFiles to get other logs.");
    opts.addOption(NODE_ADDRESS_OPTION, true, "NodeAddress in the format " + "nodename:port");
    opts.addOption(APP_OWNER_OPTION, true, "AppOwner (assumed to be current user if not specified)");
    Option amOption = new Option(AM_CONTAINER_OPTION, true,
            "Prints the AM Container logs for this application. "
                    + "Specify comma-separated value to get logs for related AM Container. "
                    + "For example, If we specify -am 1,2, we will get the logs for "
                    + "the first AM Container as well as the second AM Container. "
                    + "To get logs for all AM Containers, use -am ALL. "
                    + "To get logs for the latest AM Container, use -am -1. "
                    + "By default, it will only print out syslog. Work with -logFiles " + "to get other logs");
    amOption.setValueSeparator(',');
    amOption.setArgs(Option.UNLIMITED_VALUES);
    amOption.setArgName("AM Containers");
    opts.addOption(amOption);
    Option logFileOpt = new Option(CONTAINER_LOG_FILES, true,
            "Work with -am/-containerId and specify comma-separated value "
                    + "to get specified container log files. Use \"ALL\" to fetch all the "
                    + "log files for the container.");
    logFileOpt.setValueSeparator(',');
    logFileOpt.setArgs(Option.UNLIMITED_VALUES);
    logFileOpt.setArgName("Log File Name");
    opts.addOption(logFileOpt);

    opts.getOption(APPLICATION_ID_OPTION).setArgName("Application ID");
    opts.getOption(CONTAINER_ID_OPTION).setArgName("Container ID");
    opts.getOption(NODE_ADDRESS_OPTION).setArgName("Node Address");
    opts.getOption(APP_OWNER_OPTION).setArgName("Application Owner");
    opts.getOption(AM_CONTAINER_OPTION).setArgName("AM Containers");

    Options printOpts = new Options();
    printOpts.addOption(opts.getOption(HELP_CMD));
    printOpts.addOption(opts.getOption(CONTAINER_ID_OPTION));
    printOpts.addOption(opts.getOption(NODE_ADDRESS_OPTION));
    printOpts.addOption(opts.getOption(APP_OWNER_OPTION));
    printOpts.addOption(opts.getOption(AM_CONTAINER_OPTION));
    printOpts.addOption(opts.getOption(CONTAINER_LOG_FILES));

    if (args.length < 1) {
        printHelpMessage(printOpts);
        return -1;
    }
    if (args[0].equals("-help")) {
        printHelpMessage(printOpts);
        return 0;
    }
    CommandLineParser parser = new GnuParser();
    String appIdStr = null;
    String containerIdStr = null;
    String nodeAddress = null;
    String appOwner = null;
    boolean getAMContainerLogs = false;
    String[] logFiles = null;
    List<String> amContainersList = new ArrayList<String>();
    try {
        CommandLine commandLine = parser.parse(opts, args, true);
        appIdStr = commandLine.getOptionValue(APPLICATION_ID_OPTION);
        containerIdStr = commandLine.getOptionValue(CONTAINER_ID_OPTION);
        nodeAddress = commandLine.getOptionValue(NODE_ADDRESS_OPTION);
        appOwner = commandLine.getOptionValue(APP_OWNER_OPTION);
        getAMContainerLogs = commandLine.hasOption(AM_CONTAINER_OPTION);
        if (getAMContainerLogs) {
            String[] amContainers = commandLine.getOptionValues(AM_CONTAINER_OPTION);
            for (String am : amContainers) {
                boolean errorInput = false;
                if (!am.trim().equalsIgnoreCase("ALL")) {
                    try {
                        int id = Integer.parseInt(am.trim());
                        if (id != -1 && id <= 0) {
                            errorInput = true;
                        }
                    } catch (NumberFormatException ex) {
                        errorInput = true;
                    }
                    if (errorInput) {
                        System.err.println("Invalid input for option -am. Valid inputs are 'ALL', -1 "
                                + "and any other integer which is larger than 0.");
                        printHelpMessage(printOpts);
                        return -1;
                    }
                    amContainersList.add(am.trim());
                } else {
                    amContainersList.add("ALL");
                    break;
                }
            }
        }
        if (commandLine.hasOption(CONTAINER_LOG_FILES)) {
            logFiles = commandLine.getOptionValues(CONTAINER_LOG_FILES);
        }
    } catch (ParseException e) {
        System.err.println("options parsing failed: " + e.getMessage());
        printHelpMessage(printOpts);
        return -1;
    }

    if (appIdStr == null) {
        System.err.println("ApplicationId cannot be null!");
        printHelpMessage(printOpts);
        return -1;
    }

    ApplicationId appId = null;
    try {
        appId = ApplicationId.fromString(appIdStr);
    } catch (Exception e) {
        System.err.println("Invalid ApplicationId specified");
        return -1;
    }

    LogCLIHelpers logCliHelper = new LogCLIHelpers();
    logCliHelper.setConf(getConf());

    if (appOwner == null || appOwner.isEmpty()) {
        appOwner = UserGroupInformation.getCurrentUser().getShortUserName();
    }

    YarnApplicationState appState = YarnApplicationState.NEW;
    try {
        appState = getApplicationState(appId);
        if (appState == YarnApplicationState.NEW || appState == YarnApplicationState.NEW_SAVING
                || appState == YarnApplicationState.SUBMITTED) {
            System.out.println("Logs are not avaiable right now.");
            return -1;
        }
    } catch (IOException | YarnException e) {
        System.err.println(
                "Unable to get ApplicationState." + " Attempting to fetch logs directly from the filesystem.");
    }

    // To get am logs
    if (getAMContainerLogs) {
        // if we do not specify the value for CONTAINER_LOG_FILES option,
        // we will only output syslog
        if (logFiles == null || logFiles.length == 0) {
            logFiles = new String[] { "syslog" };
        }
        // If the application is running, we will call the RM WebService
        // to get the AppAttempts which includes the nodeHttpAddress
        // and containerId for all the AM Containers.
        // After that, we will call NodeManager webService to get the
        // related logs
        if (appState == YarnApplicationState.ACCEPTED || appState == YarnApplicationState.RUNNING) {
            return printAMContainerLogs(getConf(), appIdStr, amContainersList, logFiles, logCliHelper, appOwner,
                    false);
        } else {
            // If the application is in the final state, we will call RM webservice
            // to get all AppAttempts information first. If we get nothing,
            // we will try to call AHS webservice to get related AppAttempts
            // which includes nodeAddress for the AM Containers.
            // After that, we will use nodeAddress and containerId
            // to get logs from HDFS directly.
            if (getConf().getBoolean(YarnConfiguration.APPLICATION_HISTORY_ENABLED,
                    YarnConfiguration.DEFAULT_APPLICATION_HISTORY_ENABLED)) {
                return printAMContainerLogs(getConf(), appIdStr, amContainersList, logFiles, logCliHelper,
                        appOwner, true);
            } else {
                System.out.println("Can not get AMContainers logs for the application:" + appId);
                System.out.println("This application:" + appId + " is finished."
                        + " Please enable the application history service. Or Using "
                        + "yarn logs -applicationId <appId> -containerId <containerId> "
                        + "--nodeAddress <nodeHttpAddress> to get the container logs");
                return -1;
            }
        }
    }

    int resultCode = 0;
    if (containerIdStr != null) {
        // if we provide the node address and the application is in the final
        // state, we could directly get logs from HDFS.
        if (nodeAddress != null && isApplicationFinished(appState)) {
            // if user specified "ALL" as the logFiles param, pass null
            // to logCliHelper so that it fetches all the logs
            List<String> logs;
            if (logFiles == null) {
                logs = null;
            } else if (fetchAllLogFiles(logFiles)) {
                logs = null;
            } else {
                logs = Arrays.asList(logFiles);
            }
            return logCliHelper.dumpAContainersLogsForALogType(appIdStr, containerIdStr, nodeAddress, appOwner,
                    logs);
        }
        try {
            // If the nodeAddress is not provided, we will try to get
            // the ContainerReport. In the containerReport, we could get
            // nodeAddress and nodeHttpAddress
            ContainerReport report = getContainerReport(containerIdStr);
            String nodeHttpAddress = report.getNodeHttpAddress()
                    .replaceFirst(WebAppUtils.getHttpSchemePrefix(getConf()), "");
            String nodeId = report.getAssignedNode().toString();
            // If the application is not in the final state,
            // we will provide the NodeHttpAddress and get the container logs
            // by calling NodeManager webservice.
            if (!isApplicationFinished(appState)) {
                if (logFiles == null || logFiles.length == 0) {
                    logFiles = new String[] { "syslog" };
                }
                printContainerLogsFromRunningApplication(getConf(), appIdStr, containerIdStr, nodeHttpAddress,
                        nodeId, logFiles, logCliHelper, appOwner);
            } else {
                String[] requestedLogFiles = logFiles;
                if (fetchAllLogFiles(logFiles)) {
                    requestedLogFiles = null;
                }
                // If the application is in the final state, we will directly
                // get the container logs from HDFS.
                printContainerLogsForFinishedApplication(appIdStr, containerIdStr, nodeId, requestedLogFiles,
                        logCliHelper, appOwner);
            }
            return resultCode;
        } catch (IOException | YarnException ex) {
            System.err.println(
                    "Unable to get logs for this container:" + containerIdStr + "for the application:" + appId);
            if (!getConf().getBoolean(YarnConfiguration.APPLICATION_HISTORY_ENABLED,
                    YarnConfiguration.DEFAULT_APPLICATION_HISTORY_ENABLED)) {
                System.out.println("Please enable the application history service. Or ");
            }
            System.out.println("Using " + "yarn logs -applicationId <appId> -containerId <containerId> "
                    + "--nodeAddress <nodeHttpAddress> to get the container logs");
            return -1;
        }
    } else {
        if (nodeAddress == null) {
            resultCode = logCliHelper.dumpAllContainersLogs(appId, appOwner, System.out);
        } else {
            System.out.println("Should at least provide ContainerId!");
            printHelpMessage(printOpts);
            resultCode = -1;
        }
    }
    return resultCode;
}

From source file:org.apache.hadoop.yarn.client.cli.NodeCLI.java

@Override
public int run(String[] args) throws Exception {

    Options opts = new Options();
    opts.addOption(HELP_CMD, false, "Displays help for all commands.");
    opts.addOption(STATUS_CMD, true, "Prints the status report of the node.");
    opts.addOption(LIST_CMD, false,/*ww w  . j av  a 2s  .c  o  m*/
            "List all running nodes. " + "Supports optional use of -states to filter nodes "
                    + "based on node state, all -all to list all nodes, "
                    + "-showDetails to display more details about each node.");
    Option nodeStateOpt = new Option(NODE_STATE_CMD, true,
            "Works with -list to filter nodes based on input comma-separated " + "list of node states. "
                    + getAllValidNodeStates());
    nodeStateOpt.setValueSeparator(',');
    nodeStateOpt.setArgs(Option.UNLIMITED_VALUES);
    nodeStateOpt.setArgName("States");
    opts.addOption(nodeStateOpt);
    Option allOpt = new Option(NODE_ALL, false, "Works with -list to list all nodes.");
    opts.addOption(allOpt);
    Option showDetailsOpt = new Option(NODE_SHOW_DETAILS, false,
            "Works with -list to show more details about each node.");
    opts.addOption(showDetailsOpt);
    opts.getOption(STATUS_CMD).setArgName("NodeId");

    if (args != null && args.length > 0) {
        for (int i = args.length - 1; i >= 0; i--) {
            if (args[i].equalsIgnoreCase("-" + NODE_ALL)) {
                args[i] = "-" + NODE_ALL;
            }
        }
    }

    int exitCode = -1;
    CommandLine cliParser = null;
    try {
        cliParser = new GnuParser().parse(opts, args);
    } catch (MissingArgumentException ex) {
        sysout.println("Missing argument for options");
        printUsage(opts);
        return exitCode;
    }

    if (cliParser.hasOption("status")) {
        if (args.length != 2) {
            printUsage(opts);
            return exitCode;
        }
        printNodeStatus(cliParser.getOptionValue("status"));
    } else if (cliParser.hasOption("list")) {
        Set<NodeState> nodeStates = new HashSet<NodeState>();
        if (cliParser.hasOption(NODE_ALL)) {
            for (NodeState state : NodeState.values()) {
                nodeStates.add(state);
            }
        } else if (cliParser.hasOption(NODE_STATE_CMD)) {
            String[] types = cliParser.getOptionValues(NODE_STATE_CMD);
            if (types != null) {
                for (String type : types) {
                    if (!type.trim().isEmpty()) {
                        try {
                            nodeStates.add(NodeState
                                    .valueOf(org.apache.hadoop.util.StringUtils.toUpperCase(type.trim())));
                        } catch (IllegalArgumentException ex) {
                            sysout.println("The node state " + type + " is invalid.");
                            sysout.println(getAllValidNodeStates());
                            return exitCode;
                        }
                    }
                }
            }
        } else {
            nodeStates.add(NodeState.RUNNING);
        }

        // List all node details with more information.
        if (cliParser.hasOption(NODE_SHOW_DETAILS)) {
            listDetailedClusterNodes(nodeStates);
        } else {
            listClusterNodes(nodeStates);
        }
    } else if (cliParser.hasOption(HELP_CMD)) {
        printUsage(opts);
        return 0;
    } else {
        syserr.println("Invalid Command Usage : ");
        printUsage(opts);
    }
    return 0;
}

From source file:org.apache.isis.core.runtime.runner.opts.OptionHandlerAdditionalProperty.java

@Override
@SuppressWarnings("static-access")
public void addOption(final Options options) {
    final Option option = OptionBuilder.withArgName("property=value").hasArg().withValueSeparator()
            .withDescription("use value for given property").create(ADDITIONAL_PROPERTY);
    option.setArgs(Option.UNLIMITED_VALUES);
    options.addOption(option);/*from  ww  w  .j ava  2 s  .co  m*/
}

From source file:org.apache.marmotta.loader.core.MarmottaLoader.java

/**
 * Build command line options. Base options are:
 * <ul>// w w w  . j  a  va2  s  . com
 *     <li>-h | --help: show help</li>
 *     <li>-B | --backend: backend to use (kiwi, hbase, berkeleydb)</li>
 *     <li>-z | --gzip: input is GZIP encoded</li>
 *     <li>-j | --bzip2: input is BZIP2 encoded</li>
 *     <li>-c | --context: URI of a context to add the statement to</li>
 *     <li>-b | --base: URI to use as base for resolving relative URIs</li>
 *     <li>-f | --file: input file to use for loading</li>
 *     <li>-d | --dir:  input directory containing files to use for loading</li>
 *     <li>-t | --type: input format to use for parsing (MIME type)</li>
 *     <li>-s | --statistics: collect statistics and write a graph into the file given</li>
 * </ul>
 *
 * In addition, loader backends can provide their own additional command line options.
 *
 * @return
 */
private static Options buildOptions() {
    final Options options = new Options();

    options.addOption("h", "help", false, "print this help");

    OptionGroup compression = new OptionGroup();
    compression.addOption(new Option("z", "gzip", false, "input is gzip compressed"));
    compression.addOption(new Option("j", "bzip2", false, "input is bzip2 compressed"));
    options.addOptionGroup(compression);

    final Option backend = OptionBuilder.withArgName("backend").hasArgs(1)
            .withDescription("backend to use ("
                    + StringUtils.join(
                            Iterators.transform(backends.iterator(), new BackendIdentifierFunction()), ", ")
                    + ")")
            .withLongOpt("backend").create('B');
    options.addOption(backend);

    final Option base = OptionBuilder.withArgName("base").hasArgs(1)
            .withDescription("base URI to use for resolving relative URIs").withLongOpt("base").create('b');
    options.addOption(base);

    final Option context = OptionBuilder.withArgName("context").hasArgs(1)
            .withDescription("URI of a context to add the statement to").withLongOpt("context").create('c');
    options.addOption(context);

    final Option format = OptionBuilder.withArgName("type").hasArgs(1)
            .withDescription("input format to use for parsing (MIME type) in case auto-guessing does not work")
            .withLongOpt("type").create('t');
    options.addOption(format);

    // input options
    OptionGroup input = new OptionGroup();
    input.setRequired(true);
    final Option file = OptionBuilder.withArgName("file").hasArgs(Option.UNLIMITED_VALUES)
            .withDescription("input file(s) to load").withLongOpt("file").create('f');
    input.addOption(file);

    final Option directories = OptionBuilder.withArgName("dir").hasArgs(Option.UNLIMITED_VALUES)
            .withDescription("input directories(s) to load").withLongOpt("dir").create('d');
    input.addOption(directories);

    final Option archives = OptionBuilder.withArgName("archive").hasArgs(Option.UNLIMITED_VALUES)
            .withDescription("input archives(s) to load (zip, tar.gz)").withLongOpt("archive").create('a');
    input.addOption(archives);
    options.addOptionGroup(input);

    final Option statistics = OptionBuilder.withArgName("statistics")
            .withDescription("collect statistics and write a graph into the file given")
            .withLongOpt("statistics").hasOptionalArg().create('s');
    options.addOption(statistics);

    final Option property = OptionBuilder.withArgName("property=value").hasArgs(2).withValueSeparator()
            .withDescription("set configuration property to value").create("D");
    options.addOption(property);

    for (LoaderBackend b : backends) {
        for (Option o : b.getOptions()) {
            options.addOption(o);
        }
    }

    return options;
}