Search in sources :

Example 6 with XOozieClient

use of org.apache.oozie.client.XOozieClient in project oozie by apache.

the class OozieCLI method scriptLanguageCommand.

private void scriptLanguageCommand(CommandLine commandLine, String jobType) throws IOException, OozieCLIException {
    List<String> args = commandLine.getArgList();
    if (args.size() > 0) {
        // checking if args starts with -X (because CLIParser cannot check this)
        if (!args.get(0).equals("-X")) {
            throw new OozieCLIException("Unrecognized option: " + args.get(0) + " Expecting -X");
        }
        args.remove(0);
    }
    if (!commandLine.hasOption(SCRIPTFILE_OPTION)) {
        throw new OozieCLIException("Need to specify -file <scriptfile>");
    }
    if (!commandLine.hasOption(CONFIG_OPTION)) {
        throw new OozieCLIException("Need to specify -config <configfile>");
    }
    try {
        XOozieClient wc = createXOozieClient(commandLine);
        Properties conf = getConfiguration(wc, commandLine);
        String script = commandLine.getOptionValue(SCRIPTFILE_OPTION);
        List<String> paramsList = new ArrayList<String>();
        if (commandLine.hasOption("P")) {
            Properties params = commandLine.getOptionProperties("P");
            for (String key : params.stringPropertyNames()) {
                paramsList.add(key + "=" + params.getProperty(key));
            }
        }
        System.out.println(JOB_ID_PREFIX + wc.submitScriptLanguage(conf, script, args.toArray(new String[args.size()]), paramsList.toArray(new String[paramsList.size()]), jobType));
    } catch (OozieClientException ex) {
        throw new OozieCLIException(ex.toString(), ex);
    }
}
Also used : OozieClientException(org.apache.oozie.client.OozieClientException) XOozieClient(org.apache.oozie.client.XOozieClient) ArrayList(java.util.ArrayList) Properties(java.util.Properties)

Example 7 with XOozieClient

use of org.apache.oozie.client.XOozieClient in project oozie by apache.

the class OozieCLI method jobCommand.

private void jobCommand(CommandLine commandLine) throws IOException, OozieCLIException {
    XOozieClient wc = createXOozieClient(commandLine);
    List<String> options = new ArrayList<String>();
    for (Option option : commandLine.getOptions()) {
        options.add(option.getOpt());
    }
    try {
        if (options.contains(SUBMIT_OPTION)) {
            System.out.println(JOB_ID_PREFIX + wc.submit(getConfiguration(wc, commandLine)));
        } else if (options.contains(START_OPTION)) {
            wc.start(commandLine.getOptionValue(START_OPTION));
        } else if (options.contains(DRYRUN_OPTION) && !options.contains(UPDATE_OPTION)) {
            String dryrunStr = wc.dryrun(getConfiguration(wc, commandLine));
            if (dryrunStr.equals("OK")) {
                // workflow
                System.out.println("OK");
            } else {
                // coordinator
                String[] dryrunStrs = dryrunStr.split("action for new instance");
                int arraysize = dryrunStrs.length;
                System.out.println("***coordJob after parsing: ***");
                System.out.println(dryrunStrs[0]);
                int aLen = dryrunStrs.length - 1;
                if (aLen < 0) {
                    aLen = 0;
                }
                System.out.println("***total coord actions is " + aLen + " ***");
                for (int i = 1; i <= arraysize - 1; i++) {
                    System.out.println(RULER);
                    System.out.println("coordAction instance: " + i + ":");
                    System.out.println(dryrunStrs[i]);
                }
            }
        } else if (options.contains(SUSPEND_OPTION)) {
            wc.suspend(commandLine.getOptionValue(SUSPEND_OPTION));
        } else if (options.contains(RESUME_OPTION)) {
            wc.resume(commandLine.getOptionValue(RESUME_OPTION));
        } else if (options.contains(IGNORE_OPTION)) {
            String ignoreScope = null;
            if (options.contains(ACTION_OPTION)) {
                ignoreScope = commandLine.getOptionValue(ACTION_OPTION);
                if (ignoreScope == null || ignoreScope.isEmpty()) {
                    throw new OozieCLIException("-" + ACTION_OPTION + " is empty");
                }
            }
            printCoordActionsStatus(wc.ignore(commandLine.getOptionValue(IGNORE_OPTION), ignoreScope));
        } else if (options.contains(KILL_OPTION)) {
            if (commandLine.getOptionValue(KILL_OPTION).contains("-C") && (options.contains(DATE_OPTION) || options.contains(ACTION_OPTION))) {
                String coordJobId = commandLine.getOptionValue(KILL_OPTION);
                String scope = null;
                String rangeType = null;
                if (options.contains(DATE_OPTION) && options.contains(ACTION_OPTION)) {
                    throw new OozieCLIException("Invalid options provided for rerun: either" + DATE_OPTION + " or " + ACTION_OPTION + " expected. Don't use both at the same time.");
                }
                if (options.contains(DATE_OPTION)) {
                    rangeType = RestConstants.JOB_COORD_SCOPE_DATE;
                    scope = commandLine.getOptionValue(DATE_OPTION);
                } else if (options.contains(ACTION_OPTION)) {
                    rangeType = RestConstants.JOB_COORD_SCOPE_ACTION;
                    scope = commandLine.getOptionValue(ACTION_OPTION);
                } else {
                    throw new OozieCLIException("Invalid options provided for rerun: " + DATE_OPTION + " or " + ACTION_OPTION + " expected.");
                }
                printCoordActions(wc.kill(coordJobId, rangeType, scope));
            } else {
                wc.kill(commandLine.getOptionValue(KILL_OPTION));
            }
        } else if (options.contains(CHANGE_OPTION)) {
            wc.change(commandLine.getOptionValue(CHANGE_OPTION), getChangeValue(commandLine));
        } else if (options.contains(RUN_OPTION)) {
            System.out.println(JOB_ID_PREFIX + wc.run(getConfiguration(wc, commandLine)));
        } else if (options.contains(RERUN_OPTION)) {
            if (commandLine.getOptionValue(RERUN_OPTION).contains("-W")) {
                if (isConfigurationSpecified(wc, commandLine)) {
                    wc.reRun(commandLine.getOptionValue(RERUN_OPTION), getConfiguration(wc, commandLine));
                } else {
                    wc.reRun(commandLine.getOptionValue(RERUN_OPTION), new Properties());
                }
            } else if (commandLine.getOptionValue(RERUN_OPTION).contains("-B")) {
                String bundleJobId = commandLine.getOptionValue(RERUN_OPTION);
                String coordScope = null;
                String dateScope = null;
                boolean refresh = false;
                boolean noCleanup = false;
                if (options.contains(ACTION_OPTION)) {
                    throw new OozieCLIException("Invalid options provided for bundle rerun. " + ACTION_OPTION + " is not valid for bundle rerun");
                }
                if (options.contains(DATE_OPTION)) {
                    dateScope = commandLine.getOptionValue(DATE_OPTION);
                }
                if (options.contains(COORD_OPTION)) {
                    coordScope = commandLine.getOptionValue(COORD_OPTION);
                }
                if (options.contains(RERUN_REFRESH_OPTION)) {
                    refresh = true;
                }
                if (options.contains(RERUN_NOCLEANUP_OPTION)) {
                    noCleanup = true;
                }
                wc.reRunBundle(bundleJobId, coordScope, dateScope, refresh, noCleanup);
                if (coordScope != null && !coordScope.isEmpty()) {
                    System.out.println("Coordinators [" + coordScope + "] of bundle " + bundleJobId + " are scheduled to rerun on date ranges [" + dateScope + "].");
                } else {
                    System.out.println("All coordinators of bundle " + bundleJobId + " are scheduled to rerun on the date ranges [" + dateScope + "].");
                }
            } else {
                String coordJobId = commandLine.getOptionValue(RERUN_OPTION);
                String scope = null;
                String rerunType = null;
                boolean refresh = false;
                boolean noCleanup = false;
                boolean failed = false;
                if (options.contains(DATE_OPTION) && options.contains(ACTION_OPTION)) {
                    throw new OozieCLIException("Invalid options provided for rerun: either" + DATE_OPTION + " or " + ACTION_OPTION + " expected. Don't use both at the same time.");
                }
                if (options.contains(DATE_OPTION)) {
                    rerunType = RestConstants.JOB_COORD_SCOPE_DATE;
                    scope = commandLine.getOptionValue(DATE_OPTION);
                } else if (options.contains(ACTION_OPTION)) {
                    rerunType = RestConstants.JOB_COORD_SCOPE_ACTION;
                    scope = commandLine.getOptionValue(ACTION_OPTION);
                } else {
                    throw new OozieCLIException("Invalid options provided for rerun: " + DATE_OPTION + " or " + ACTION_OPTION + " expected.");
                }
                if (options.contains(RERUN_REFRESH_OPTION)) {
                    refresh = true;
                }
                if (options.contains(RERUN_NOCLEANUP_OPTION)) {
                    noCleanup = true;
                }
                Properties props = null;
                if (isConfigurationSpecified(wc, commandLine)) {
                    props = getConfiguration(wc, commandLine);
                }
                if (options.contains(RERUN_FAILED_OPTION)) {
                    failed = true;
                }
                printCoordActions(wc.reRunCoord(coordJobId, rerunType, scope, refresh, noCleanup, failed, props));
            }
        } else if (options.contains(INFO_OPTION)) {
            String timeZoneId = getTimeZoneId(commandLine);
            final String optionValue = commandLine.getOptionValue(INFO_OPTION);
            if (optionValue.endsWith("-B")) {
                String filter = commandLine.getOptionValue(FILTER_OPTION);
                if (filter != null) {
                    throw new OozieCLIException("Filter option is currently not supported for a Bundle job");
                }
                printBundleJob(wc.getBundleJobInfo(optionValue), timeZoneId, options.contains(VERBOSE_OPTION));
            } else if (optionValue.endsWith("-C")) {
                String s = commandLine.getOptionValue(OFFSET_OPTION);
                int start = Integer.parseInt((s != null) ? s : "-1");
                s = commandLine.getOptionValue(LEN_OPTION);
                int len = Integer.parseInt((s != null) ? s : "-1");
                String filter = commandLine.getOptionValue(FILTER_OPTION);
                String order = commandLine.getOptionValue(ORDER_OPTION);
                printCoordJob(wc.getCoordJobInfo(optionValue, filter, start, len, order), timeZoneId, options.contains(VERBOSE_OPTION));
            } else if (optionValue.contains("-C@")) {
                if (options.contains(ALL_WORKFLOWS_FOR_COORD_ACTION)) {
                    printWfsForCoordAction(wc.getWfsForCoordAction(optionValue), timeZoneId);
                } else {
                    String filter = commandLine.getOptionValue(FILTER_OPTION);
                    if (filter != null) {
                        throw new OozieCLIException("Filter option is not supported for a Coordinator action");
                    }
                    printCoordAction(wc.getCoordActionInfo(optionValue), timeZoneId);
                }
            } else if (optionValue.contains("-W@")) {
                String filter = commandLine.getOptionValue(FILTER_OPTION);
                if (filter != null) {
                    throw new OozieCLIException("Filter option is not supported for a Workflow action");
                }
                printWorkflowAction(wc.getWorkflowActionInfo(optionValue), timeZoneId, options.contains(VERBOSE_OPTION));
            } else {
                String filter = commandLine.getOptionValue(FILTER_OPTION);
                if (filter != null) {
                    throw new OozieCLIException("Filter option is currently not supported for a Workflow job");
                }
                String s = commandLine.getOptionValue(OFFSET_OPTION);
                int start = Integer.parseInt((s != null) ? s : "0");
                s = commandLine.getOptionValue(LEN_OPTION);
                String jobtype = commandLine.getOptionValue(JOBTYPE_OPTION);
                jobtype = (jobtype != null) ? jobtype : "wf";
                int len = Integer.parseInt((s != null) ? s : "0");
                printJob(wc.getJobInfo(optionValue, start, len), timeZoneId, options.contains(VERBOSE_OPTION));
            }
        } else if (options.contains(LOG_OPTION)) {
            PrintStream ps = System.out;
            String logFilter = null;
            if (options.contains(RestConstants.LOG_FILTER_OPTION)) {
                logFilter = commandLine.getOptionValue(RestConstants.LOG_FILTER_OPTION);
            }
            if (commandLine.getOptionValue(LOG_OPTION).contains("-C")) {
                String logRetrievalScope = null;
                String logRetrievalType = null;
                if (options.contains(ACTION_OPTION)) {
                    logRetrievalType = RestConstants.JOB_LOG_ACTION;
                    logRetrievalScope = commandLine.getOptionValue(ACTION_OPTION);
                }
                if (options.contains(DATE_OPTION)) {
                    logRetrievalType = RestConstants.JOB_LOG_DATE;
                    logRetrievalScope = commandLine.getOptionValue(DATE_OPTION);
                }
                try {
                    wc.getJobLog(commandLine.getOptionValue(LOG_OPTION), logRetrievalType, logRetrievalScope, logFilter, ps);
                } finally {
                    ps.close();
                }
            } else {
                if (!options.contains(ACTION_OPTION) && !options.contains(DATE_OPTION)) {
                    wc.getJobLog(commandLine.getOptionValue(LOG_OPTION), null, null, logFilter, ps);
                } else {
                    throw new OozieCLIException("Invalid options provided for log retrieval. " + ACTION_OPTION + " and " + DATE_OPTION + " are valid only for coordinator job log retrieval");
                }
            }
        } else if (options.contains(ERROR_LOG_OPTION)) {
            PrintStream ps = System.out;
            try {
                wc.getJobErrorLog(commandLine.getOptionValue(ERROR_LOG_OPTION), ps);
            } finally {
                ps.close();
            }
        } else if (options.contains(AUDIT_LOG_OPTION)) {
            PrintStream ps = System.out;
            try {
                wc.getJobAuditLog(commandLine.getOptionValue(AUDIT_LOG_OPTION), ps);
            } finally {
                ps.close();
            }
        } else if (options.contains(DEFINITION_OPTION)) {
            System.out.println(wc.getJobDefinition(commandLine.getOptionValue(DEFINITION_OPTION)));
        } else if (options.contains(CONFIG_CONTENT_OPTION)) {
            if (commandLine.getOptionValue(CONFIG_CONTENT_OPTION).endsWith("-C")) {
                System.out.println(wc.getCoordJobInfo(commandLine.getOptionValue(CONFIG_CONTENT_OPTION)).getConf());
            } else if (commandLine.getOptionValue(CONFIG_CONTENT_OPTION).endsWith("-W")) {
                System.out.println(wc.getJobInfo(commandLine.getOptionValue(CONFIG_CONTENT_OPTION)).getConf());
            } else if (commandLine.getOptionValue(CONFIG_CONTENT_OPTION).endsWith("-B")) {
                System.out.println(wc.getBundleJobInfo(commandLine.getOptionValue(CONFIG_CONTENT_OPTION)).getConf());
            } else {
                System.out.println("ERROR:  job id [" + commandLine.getOptionValue(CONFIG_CONTENT_OPTION) + "] doesn't end with either C or W or B");
            }
        } else if (options.contains(UPDATE_OPTION)) {
            String coordJobId = commandLine.getOptionValue(UPDATE_OPTION);
            Properties conf = null;
            String dryrun = "";
            String showdiff = "";
            if (commandLine.getOptionValue(CONFIG_OPTION) != null) {
                conf = getConfiguration(wc, commandLine);
            }
            if (options.contains(DRYRUN_OPTION)) {
                dryrun = "true";
            }
            if (commandLine.getOptionValue(SHOWDIFF_OPTION) != null) {
                showdiff = commandLine.getOptionValue(SHOWDIFF_OPTION);
            }
            if (conf == null) {
                System.out.println(wc.updateCoord(coordJobId, dryrun, showdiff));
            } else {
                System.out.println(wc.updateCoord(coordJobId, conf, dryrun, showdiff));
            }
        } else if (options.contains(POLL_OPTION)) {
            String jobId = commandLine.getOptionValue(POLL_OPTION);
            int timeout = 30;
            int interval = 5;
            String timeoutS = commandLine.getOptionValue(TIMEOUT_OPTION);
            if (timeoutS != null) {
                timeout = Integer.parseInt(timeoutS);
            }
            String intervalS = commandLine.getOptionValue(INTERVAL_OPTION);
            if (intervalS != null) {
                interval = Integer.parseInt(intervalS);
            }
            boolean verbose = commandLine.hasOption(VERBOSE_OPTION);
            wc.pollJob(jobId, timeout, interval, verbose);
        } else if (options.contains(SLA_ENABLE_ALERT)) {
            slaAlertCommand(commandLine.getOptionValue(SLA_ENABLE_ALERT), wc, commandLine, options);
        } else if (options.contains(SLA_DISABLE_ALERT)) {
            slaAlertCommand(commandLine.getOptionValue(SLA_DISABLE_ALERT), wc, commandLine, options);
        } else if (options.contains(SLA_CHANGE)) {
            slaAlertCommand(commandLine.getOptionValue(SLA_CHANGE), wc, commandLine, options);
        } else if (options.contains(WORKFLOW_ACTIONS_RETRIES)) {
            printWorkflowActionRetries(wc.getWorkflowActionRetriesInfo(commandLine.getOptionValue(WORKFLOW_ACTIONS_RETRIES)), commandLine.getOptionValue(WORKFLOW_ACTIONS_RETRIES));
        } else if (options.contains(COORD_ACTION_MISSING_DEPENDENCIES)) {
            String actions = null, dates = null;
            if (options.contains(ACTION_OPTION)) {
                actions = commandLine.getOptionValue(ACTION_OPTION);
            }
            if (options.contains(DATE_OPTION)) {
                dates = commandLine.getOptionValue(DATE_OPTION);
            }
            wc.getCoordActionMissingDependencies(commandLine.getOptionValue(COORD_ACTION_MISSING_DEPENDENCIES), actions, dates, System.out);
        }
    } catch (OozieClientException ex) {
        throw new OozieCLIException(ex.toString(), ex);
    }
}
Also used : PrintStream(java.io.PrintStream) OozieClientException(org.apache.oozie.client.OozieClientException) XOozieClient(org.apache.oozie.client.XOozieClient) ArrayList(java.util.ArrayList) Option(org.apache.commons.cli.Option) Properties(java.util.Properties)

Example 8 with XOozieClient

use of org.apache.oozie.client.XOozieClient in project oozie by apache.

the class OozieCLI method jobsCommand.

private void jobsCommand(CommandLine commandLine) throws IOException, OozieCLIException {
    XOozieClient wc = createXOozieClient(commandLine);
    List<String> options = new ArrayList<String>();
    for (Option option : commandLine.getOptions()) {
        options.add(option.getOpt());
    }
    String filter = commandLine.getOptionValue(FILTER_OPTION);
    String s = commandLine.getOptionValue(OFFSET_OPTION);
    int start = Integer.parseInt((s != null) ? s : "0");
    s = commandLine.getOptionValue(LEN_OPTION);
    String jobtype = commandLine.getOptionValue(JOBTYPE_OPTION);
    String timeZoneId = getTimeZoneId(commandLine);
    jobtype = (jobtype != null) ? jobtype : "wf";
    int len = Integer.parseInt((s != null) ? s : "0");
    String bulkFilterString = commandLine.getOptionValue(BULK_OPTION);
    try {
        if (options.contains(KILL_OPTION)) {
            printBulkModifiedJobs(wc.killJobs(filter, jobtype, start, len), timeZoneId, "killed");
        } else if (options.contains(SUSPEND_OPTION)) {
            printBulkModifiedJobs(wc.suspendJobs(filter, jobtype, start, len), timeZoneId, "suspended");
        } else if (options.contains(RESUME_OPTION)) {
            printBulkModifiedJobs(wc.resumeJobs(filter, jobtype, start, len), timeZoneId, "resumed");
        } else if (bulkFilterString != null) {
            printBulkJobs(wc.getBulkInfo(bulkFilterString, start, len), timeZoneId, commandLine.hasOption(VERBOSE_OPTION));
        } else if (jobtype.toLowerCase().contains("wf")) {
            printJobs(wc.getJobsInfo(filter, start, len), timeZoneId, commandLine.hasOption(VERBOSE_OPTION));
        } else if (jobtype.toLowerCase().startsWith("coord")) {
            printCoordJobs(wc.getCoordJobsInfo(filter, start, len), timeZoneId, commandLine.hasOption(VERBOSE_OPTION));
        } else if (jobtype.toLowerCase().startsWith("bundle")) {
            printBundleJobs(wc.getBundleJobsInfo(filter, start, len), timeZoneId, commandLine.hasOption(VERBOSE_OPTION));
        }
    } catch (OozieClientException ex) {
        throw new OozieCLIException(ex.toString(), ex);
    }
}
Also used : OozieClientException(org.apache.oozie.client.OozieClientException) XOozieClient(org.apache.oozie.client.XOozieClient) ArrayList(java.util.ArrayList) Option(org.apache.commons.cli.Option)

Example 9 with XOozieClient

use of org.apache.oozie.client.XOozieClient in project oozie by apache.

the class OozieCLI method createXOozieClient.

/**
 * Create a XOozieClient.
 * <p>
 * It injects any '-Dheader:' as header to the the {@link org.apache.oozie.client.OozieClient}.
 *
 * @param commandLine the parsed command line options.
 * @return a pre configured eXtended workflow client.
 * @throws OozieCLIException thrown if the XOozieClient could not be configured.
 */
protected XOozieClient createXOozieClient(CommandLine commandLine) throws OozieCLIException {
    XOozieClient wc = new AuthOozieClient(getOozieUrl(commandLine), getAuthOption(commandLine));
    addHeader(wc);
    setDebugMode(wc, commandLine.hasOption(DEBUG_OPTION));
    setRetryCount(wc);
    return wc;
}
Also used : AuthOozieClient(org.apache.oozie.client.AuthOozieClient) XOozieClient(org.apache.oozie.client.XOozieClient)

Aggregations

XOozieClient (org.apache.oozie.client.XOozieClient)9 OozieClientException (org.apache.oozie.client.OozieClientException)8 ArrayList (java.util.ArrayList)4 Properties (java.util.Properties)4 Option (org.apache.commons.cli.Option)3 AuthOozieClient (org.apache.oozie.client.AuthOozieClient)2 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 ParseException (org.apache.commons.cli.ParseException)1 OozieClient (org.apache.oozie.client.OozieClient)1 SYSTEM_MODE (org.apache.oozie.client.OozieClient.SYSTEM_MODE)1 DOMException (org.w3c.dom.DOMException)1 NodeList (org.w3c.dom.NodeList)1 SAXException (org.xml.sax.SAXException)1