Search in sources :

Example 41 with GnuParser

use of org.apache.commons.cli.GnuParser in project databus by linkedin.

the class BootstrapTableReader method parseArgs.

@SuppressWarnings("static-access")
public static void parseArgs(String[] args) throws IOException {
    CommandLineParser cliParser = new GnuParser();
    Option helpOption = OptionBuilder.withLongOpt(HELP_OPT_LONG_NAME).withDescription("Help screen").create(HELP_OPT_CHAR);
    Option sourcesOption = OptionBuilder.withLongOpt(QUERY_CONFIG_OPT_LONG_NAME).withDescription("Query Config").hasArg().withArgName("property_file").create(QUERY_CONFIG_OPT_CHAR);
    Option dbOption = OptionBuilder.withLongOpt(BOOTSTRAP_DB_PROPS_OPT_LONG_NAME).withDescription("Bootstrap DB properties to use").hasArg().withArgName("property_file").create(BOOTSTRAP_DB_PROP_OPT_CHAR);
    Option log4jPropsOption = OptionBuilder.withLongOpt(LOG4J_PROPS_OPT_LONG_NAME).withDescription("Log4j properties to use").hasArg().withArgName("property_file").create(LOG4J_PROPS_OPT_CHAR);
    Options options = new Options();
    options.addOption(helpOption);
    options.addOption(sourcesOption);
    options.addOption(dbOption);
    options.addOption(log4jPropsOption);
    CommandLine cmd = null;
    try {
        cmd = cliParser.parse(options, args);
    } catch (ParseException pe) {
        LOG.fatal("Bootstrap Physical Config: failed to parse command-line options.", pe);
        throw new RuntimeException("Bootstrap Physical Config: failed to parse command-line options.", pe);
    }
    if (cmd.hasOption(LOG4J_PROPS_OPT_CHAR)) {
        String log4jPropFile = cmd.getOptionValue(LOG4J_PROPS_OPT_CHAR);
        PropertyConfigurator.configure(log4jPropFile);
        LOG.info("Using custom logging settings from file " + log4jPropFile);
    } else {
        PatternLayout defaultLayout = new PatternLayout("%d{ISO8601} +%r [%t] (%p) {%c} %m%n");
        ConsoleAppender defaultAppender = new ConsoleAppender(defaultLayout);
        Logger.getRootLogger().removeAllAppenders();
        Logger.getRootLogger().addAppender(defaultAppender);
        LOG.info("Using default logging settings");
    }
    if (cmd.hasOption(HELP_OPT_CHAR)) {
        printCliHelp(options);
        System.exit(0);
    }
    if (!cmd.hasOption(QUERY_CONFIG_OPT_CHAR)) {
        throw new RuntimeException("Query Config is not provided; use --help for usage");
    }
    if (!cmd.hasOption(BOOTSTRAP_DB_PROP_OPT_CHAR)) {
        throw new RuntimeException("Bootstrap config is not provided; use --help for usage");
    }
    String propFile1 = cmd.getOptionValue(QUERY_CONFIG_OPT_CHAR);
    String propFile2 = cmd.getOptionValue(BOOTSTRAP_DB_PROP_OPT_CHAR);
    LOG.info("Loading bootstrap DB config from properties file " + propFile2);
    _sQueryConfigProps = new Properties();
    FileInputStream f1 = new FileInputStream(propFile1);
    try {
        _sQueryConfigProps.load(f1);
    } finally {
        f1.close();
    }
    _sBootstrapConfigProps = new Properties();
    FileInputStream f2 = new FileInputStream(propFile2);
    try {
        _sBootstrapConfigProps.load(f2);
    } finally {
        f2.close();
    }
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PatternLayout(org.apache.log4j.PatternLayout) GnuParser(org.apache.commons.cli.GnuParser) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Example 42 with GnuParser

use of org.apache.commons.cli.GnuParser in project databus by linkedin.

the class ClusterFileLoggingClient method processLocalArgs.

protected static String[] processLocalArgs(String[] cliArgs) throws IOException, ParseException {
    CommandLineParser cliParser = new GnuParser();
    Options cliOptions = constructCommandLineOptions();
    CommandLine cmd = cliParser.parse(cliOptions, cliArgs, true);
    // Options here has to be up front
    if (cmd.hasOption(CLUSTER_NAME_OPT_NAME)) {
        String cluster = cmd.getOptionValue(CLUSTER_NAME_OPT_NAME);
        String[] clusters = cluster.split(",");
        _clusters = Arrays.asList(clusters);
        LOG.info("Cluster Name = " + _clusters);
    }
    if (cmd.hasOption(RELAY_HOST_OPT_NAME)) {
        _relayHost = cmd.getOptionValue(RELAY_HOST_OPT_NAME);
        LOG.info("Relay Host = " + _relayHost);
    }
    if (cmd.hasOption(RELAY_PORT_OPT_NAME)) {
        _relayPort = cmd.getOptionValue(RELAY_PORT_OPT_NAME);
        LOG.info("Relay Port = " + _relayPort);
    }
    if (cmd.hasOption(EVENT_DUMP_FILE_OPT_NAME)) {
        _eventDumpFile = cmd.getOptionValue(EVENT_DUMP_FILE_OPT_NAME);
        LOG.info("Saving event dump to file: " + _eventDumpFile);
    }
    if (cmd.hasOption(VALUE_DUMP_FILE_OPT_NAME)) {
        _valueDumpFile = cmd.getOptionValue(VALUE_DUMP_FILE_OPT_NAME);
        LOG.info("Saving event value dump to file: " + _valueDumpFile);
    }
    if (cmd.hasOption(HTTP_PORT_OPT_NAME)) {
        _httpPort = cmd.getOptionValue(HTTP_PORT_OPT_NAME);
        LOG.info("Consumer http port =  " + _httpPort);
    }
    if (cmd.hasOption(JMX_SERVICE_PORT_OPT_NAME)) {
        _jmxServicePort = cmd.getOptionValue(JMX_SERVICE_PORT_OPT_NAME);
        LOG.info("Consumer JMX Service port =  " + _jmxServicePort);
    }
    if (cmd.hasOption(BOOTSTRAP_HOST_OPT_NAME)) {
        _bootstrapHost = cmd.getOptionValue(BOOTSTRAP_HOST_OPT_NAME);
        LOG.info("Bootstrap Server = " + _bootstrapHost);
    }
    if (cmd.hasOption(BOOTSTRAP_PORT_OPT_NAME)) {
        _bootstrapPort = cmd.getOptionValue(BOOTSTRAP_PORT_OPT_NAME);
        LOG.info("Bootstrap Server Port = " + _bootstrapPort);
    }
    if (cmd.hasOption(EVENT_PATTERN_OPT_NAME)) {
        _eventPattern = cmd.getOptionValue(EVENT_PATTERN_OPT_NAME);
        LOG.info("Event pattern = " + _eventPattern);
    }
    if (_bootstrapHost != null || _bootstrapPort != null) {
        _enableBootStrap = true;
    }
    // return what left over args
    return cmd.getArgs();
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Example 43 with GnuParser

use of org.apache.commons.cli.GnuParser in project databus by linkedin.

the class CheckpointSerializerMain method parseArgs.

private static void parseArgs(String[] args) throws Exception {
    CommandLineParser cliParser = new GnuParser();
    Options options = createOptions();
    CommandLine cmd = null;
    try {
        cmd = cliParser.parse(options, args);
    } catch (ParseException pe) {
        throw new RuntimeException("failed to parse command-line options.", pe);
    }
    if (cmd.hasOption(HELP_OPT_CHAR) || 0 == cmd.getOptions().length) {
        printCliHelp(options);
        System.exit(0);
    }
    try {
        _action = Action.valueOf(cmd.getOptionValue(ACTION_OPT_CHAR).toUpperCase());
    } catch (Exception e) {
        throw new RuntimeException("invalid action: " + cmd.getOptionValue(ACTION_OPT_CHAR), e);
    }
    if (!cmd.hasOption(SOURCES_OPT_CHAR)) {
        throw new RuntimeException("expected sources list; see --help for more info");
    }
    String sourcesListStr = cmd.getOptionValue(SOURCES_OPT_CHAR);
    _sources = sourcesListStr.split(",");
    if (null == _sources || 0 == _sources.length) {
        throw new RuntimeException("empty sources list");
    }
    for (int i = 0; i < _sources.length; ++i) _sources[i] = _sources[i].trim();
    if (Action.PRINT != _action && !cmd.hasOption(CLIENT_PROPS_FILE_OPT_CHAR) && !cmd.hasOption(CP3_PROPS_FILE_OPT_CHAR)) {
        throw new RuntimeException("expected client or CP3 configuration; see --help for more info");
    }
    String defaultPropPrefix = null;
    if (cmd.hasOption(CLIENT_PROPS_FILE_OPT_CHAR)) {
        try {
            _clientProps = loadProperties(cmd.getOptionValue(CLIENT_PROPS_FILE_OPT_CHAR));
            defaultPropPrefix = "databus2.client";
        } catch (Exception e) {
            throw new RuntimeException("unable to load client properties", e);
        }
    } else if (cmd.hasOption(CP3_PROPS_FILE_OPT_CHAR)) {
        try {
            _cp3Props = loadProperties(cmd.getOptionValue(CP3_PROPS_FILE_OPT_CHAR));
            defaultPropPrefix = "databus2.client.checkpointPersistence";
        } catch (Exception e) {
            throw new RuntimeException("unable to load CP3 properties", e);
        }
    }
    _propPrefix = cmd.hasOption(PROPS_PREFIX_OPT_CHAR) ? cmd.getOptionValue(PROPS_PREFIX_OPT_CHAR) : defaultPropPrefix;
    if (null != _propPrefix && !_propPrefix.endsWith(".")) {
        _propPrefix = _propPrefix + ".";
    }
    if (!cmd.hasOption(ACTION_OPT_CHAR)) {
        throw new RuntimeException("action expected; see --help for more info");
    }
    _scn = parseLongOption(cmd, SCN_OPT_CHAR, "sequence number");
    _sinceScn = parseLongOption(cmd, SINCE_SCN_OPT_CHAR, "last sequence number");
    _startScn = parseLongOption(cmd, START_SCN_OPT_CHAR, "start sequence number");
    _targetScn = parseLongOption(cmd, TARGET_SCN_OPT_CHAR, "target sequence number");
    if (cmd.hasOption(TYPE_OPT_CHAR)) {
        try {
            _cpType = DbusClientMode.valueOf(cmd.getOptionValue(TYPE_OPT_CHAR).toUpperCase());
        } catch (Exception e) {
            throw new RuntimeException("invalid checkpoint type:" + cmd.getOptionValue(TYPE_OPT_CHAR), e);
        }
    }
    if (cmd.hasOption(BOOTSTRAP_SOURCE_OPT_CHAR)) {
        _bootstrapSource = cmd.getOptionValue(BOOTSTRAP_SOURCE_OPT_CHAR);
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) DatabusRuntimeException(com.linkedin.databus.core.DatabusRuntimeException) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) JsonParseException(org.codehaus.jackson.JsonParseException) ParseException(org.apache.commons.cli.ParseException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) DatabusRuntimeException(com.linkedin.databus.core.DatabusRuntimeException) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) ParseException(org.apache.commons.cli.ParseException) Checkpoint(com.linkedin.databus.core.Checkpoint)

Example 44 with GnuParser

use of org.apache.commons.cli.GnuParser in project databus by linkedin.

the class DatabusClusterUtil method main.

/**
     * @param args
     *            DbusClusterUtil -z <zookeper-server> -c <cluster-name> [-p
     *            <partitionNumber] partitions readSCN writeSCN SCN remove
     *            clients
     */
public static void main(String[] args) {
    try {
        GnuParser cmdLineParser = new GnuParser();
        Options options = new Options();
        options.addOption("z", true, "zk-server").addOption("c", true, "cluster-name ").addOption("p", true, "partition").addOption("l", false, "legacy").addOption("h", false, "help");
        CommandLine cmdLineArgs = cmdLineParser.parse(options, args, false);
        if (cmdLineArgs.hasOption('h')) {
            usage();
            System.exit(0);
        }
        if (!cmdLineArgs.hasOption('c')) {
            usage();
            System.exit(1);
        }
        String clusterName = cmdLineArgs.getOptionValue('c');
        String zkServer = cmdLineArgs.getOptionValue('z');
        boolean isLegacyChkptLocation = cmdLineArgs.hasOption('l');
        if (zkServer == null || zkServer.isEmpty()) {
            zkServer = "localhost:2181";
        }
        String partitionStr = cmdLineArgs.getOptionValue('p');
        String partition = partitionStr;
        if ((partition != null) && partition.equals("all")) {
            partition = "";
        }
        String[] fns = cmdLineArgs.getArgs();
        if (fns.length < 1) {
            usage();
            System.exit(1);
        }
        DatabusClusterUtilHelper clusterState = new DatabusClusterUtilHelper(zkServer, clusterName);
        String function = fns[0];
        String arg1 = (fns.length > 1) ? fns[1] : null;
        String arg2 = (fns.length > 2) ? fns[2] : null;
        boolean clusterExists = clusterState.existsCluster();
        if (function.equals("create")) {
            if (!clusterExists) {
                if (arg1 == null) {
                    throw new DatabusClusterUtilException("create: please provide the number of partitions");
                }
                int part = Integer.parseInt(arg1);
                clusterState.createCluster(part);
                return;
            } else {
                throw new DatabusClusterUtilException("Cluster " + clusterName + " already exists");
            }
        }
        if (!clusterExists) {
            throw new DatabusClusterUtilException("Cluster doesn't exist! ");
        }
        if (function.equals("delete")) {
            clusterState.removeCluster();
        } else if (function.equals("partitions")) {
            int numParts = clusterState.getNumPartitions();
            System.out.println(numParts);
        } else {
            // all these functions require the notion of partition;
            Set<Integer> partitions = getPartitions(partition, clusterState.getNumPartitions());
            if (function.equals("sources")) {
                DatabusClusterCkptManager ckptMgr = new DatabusClusterCkptManager(zkServer, clusterName, null, partitions, isLegacyChkptLocation);
                Set<String> sources = ckptMgr.getSourcesFromCheckpoint();
                if (sources != null) {
                    for (String s : sources) {
                        System.out.println(s);
                    }
                } else {
                    throw new DatabusClusterUtilException("sources: Sources not found for cluster " + clusterName);
                }
            } else if (function.equals("clients")) {
                clusterState.getClusterInfo();
                for (Integer p : partitions) {
                    String client = clusterState.getInstanceForPartition(p);
                    System.out.println(p + "\t" + client);
                }
            } else if (function.equals("readSCN")) {
                List<String> sources = getSources(arg1);
                if ((sources != null) && !sources.isEmpty()) {
                    DatabusClusterCkptManager ckptMgr = new DatabusClusterCkptManager(zkServer, clusterName, sources, partitions, isLegacyChkptLocation);
                    Map<Integer, Checkpoint> ckpts = ckptMgr.readCheckpoint();
                    char delim = '\t';
                    for (Map.Entry<Integer, Checkpoint> mkPair : ckpts.entrySet()) {
                        StringBuilder output = new StringBuilder(64);
                        output.append(mkPair.getKey());
                        output.append(delim);
                        Checkpoint cp = mkPair.getValue();
                        if (cp == null) {
                            output.append(-1);
                            output.append(delim);
                            output.append(-1);
                        } else {
                            if (cp.getConsumptionMode() == DbusClientMode.ONLINE_CONSUMPTION) {
                                output.append(cp.getWindowScn());
                                output.append(delim);
                                output.append(cp.getWindowOffset());
                            } else if (cp.getConsumptionMode() == DbusClientMode.BOOTSTRAP_CATCHUP) {
                                output.append(cp.getWindowScn());
                                output.append(delim);
                                output.append(cp.getWindowOffset());
                            } else if (cp.getConsumptionMode() == DbusClientMode.BOOTSTRAP_SNAPSHOT) {
                                output.append(cp.getBootstrapSinceScn());
                                output.append(delim);
                                output.append(-1);
                            }
                        }
                        System.out.println(output.toString());
                    }
                } else {
                    throw new DatabusClusterUtilException("readSCN: please specify non-empty sources");
                }
            } else if (function.equals("checkpoint")) {
                List<String> sources = getSources(arg1);
                if ((sources != null) && !sources.isEmpty()) {
                    DatabusClusterCkptManager ckptMgr = new DatabusClusterCkptManager(zkServer, clusterName, sources, partitions, isLegacyChkptLocation);
                    Map<Integer, Checkpoint> ckpts = ckptMgr.readCheckpoint();
                    char delim = '\t';
                    for (Map.Entry<Integer, Checkpoint> mkPair : ckpts.entrySet()) {
                        StringBuilder output = new StringBuilder(64);
                        output.append(mkPair.getKey());
                        output.append(delim);
                        Checkpoint cp = mkPair.getValue();
                        if (cp == null) {
                            output.append("null");
                        } else {
                            output.append(cp.toString());
                        }
                        System.out.println(output.toString());
                    }
                } else {
                    throw new DatabusClusterUtilException("readSCN: please specify non-empty sources");
                }
            } else if (function.equals("writeSCN")) {
                String scnStr = arg1;
                Long scn = Long.parseLong(scnStr);
                if (partitionStr != null) {
                    List<String> sources = getSources(arg2);
                    if ((sources != null) && !sources.isEmpty()) {
                        DatabusClusterCkptManager ckptMgr = new DatabusClusterCkptManager(zkServer, clusterName, sources, partitions, isLegacyChkptLocation);
                        ckptMgr.writeCheckpoint(scn);
                    } else {
                        throw new DatabusClusterUtilException("writeSCN: please specify non-empty sources");
                    }
                } else {
                    throw new DatabusClusterUtilException("writeSCN: to write the SCN to all partitions please use '-p all'");
                }
            } else if (function.equals("removeSCN")) {
                if (partitionStr != null) {
                    List<String> sources = getSources(arg1);
                    if ((sources != null) && !sources.isEmpty()) {
                        DatabusClusterCkptManager ckptMgr = new DatabusClusterCkptManager(zkServer, clusterName, sources, partitions, isLegacyChkptLocation);
                        ckptMgr.remove();
                    } else {
                        throw new DatabusClusterUtilException("remove: please specify non-empty sources");
                    }
                } else {
                    throw new DatabusClusterUtilException("remove: to remove SCN from all partitions please use '-p all'");
                }
            } else {
                usage();
                System.exit(1);
            }
        }
    } catch (ParseException e) {
        usage();
        System.exit(1);
    } catch (DatabusClusterUtilException e) {
        System.err.println("Error! " + e.toString());
        System.exit(1);
    }
}
Also used : Options(org.apache.commons.cli.Options) Set(java.util.Set) HashSet(java.util.HashSet) GnuParser(org.apache.commons.cli.GnuParser) Checkpoint(com.linkedin.databus.core.Checkpoint) CommandLine(org.apache.commons.cli.CommandLine) Checkpoint(com.linkedin.databus.core.Checkpoint) List(java.util.List) ParseException(org.apache.commons.cli.ParseException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 45 with GnuParser

use of org.apache.commons.cli.GnuParser in project rest.li by linkedin.

the class RestLiSnapshotExporterCmdLineApp method main.

/**
   * @param args restliexporter -sourcepath sourcepath -resourcepackages packagenames [-name api_name] [-outdir outdir]
   */
public static void main(String[] args) {
    CommandLine cl = null;
    try {
        final CommandLineParser parser = new GnuParser();
        cl = parser.parse(OPTIONS, args);
    } catch (ParseException e) {
        System.err.println("Invalid arguments: " + e.getMessage());
        final HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("restliexporter -sourcepath sourcepath [-resourcepackages packagenames] [-resourceclasses classnames]" + "[-name api_name] [-outdir outdir]", OPTIONS);
        System.exit(0);
    }
    final String resolverPath = System.getProperty(AbstractGenerator.GENERATOR_RESOLVER_PATH);
    try {
        final RestLiSnapshotExporter exporter = new RestLiSnapshotExporter();
        exporter.setResolverPath(resolverPath);
        exporter.export(cl.getOptionValue("name"), null, cl.getOptionValues("sourcepath"), cl.getOptionValues("resourcepackages"), cl.getOptionValues("resourceClasses"), cl.getOptionValue("outdir", "."), AdditionalDocProvidersUtil.findDocProviders(log, cl.hasOption("loadAdditionalDocProviders")));
    } catch (Throwable e) {
        log.error("Error writing Snapshot files", e);
        System.out.println("Error writing Snapshot files:\n" + e);
        System.exit(1);
    }
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Aggregations

GnuParser (org.apache.commons.cli.GnuParser)208 CommandLine (org.apache.commons.cli.CommandLine)187 Options (org.apache.commons.cli.Options)165 CommandLineParser (org.apache.commons.cli.CommandLineParser)158 ParseException (org.apache.commons.cli.ParseException)139 HelpFormatter (org.apache.commons.cli.HelpFormatter)92 Path (org.apache.hadoop.fs.Path)40 Option (org.apache.commons.cli.Option)39 IOException (java.io.IOException)32 Job (org.apache.hadoop.mapreduce.Job)27 File (java.io.File)24 Configuration (org.apache.hadoop.conf.Configuration)19 FileInputStream (java.io.FileInputStream)14 ArrayList (java.util.ArrayList)14 Properties (java.util.Properties)13 FileSystem (org.apache.hadoop.fs.FileSystem)11 MissingArgumentException (org.apache.commons.cli.MissingArgumentException)9 FileNotFoundException (java.io.FileNotFoundException)7 URI (java.net.URI)7 URISyntaxException (java.net.URISyntaxException)6