public QueuedItemResult queueDispatcherJob(final IDispatchedJob dispatchedJob)
      throws CentralDispatcherException {
    final HashMap<String, String> params = new HashMap<String, String>();
    if (null == dispatchedJob.getJobRef()) {
      throw new IllegalArgumentException("JobRef was null");
    }
    if (null != dispatchedJob.getJobRef().getJobId()) {
      params.put("id", dispatchedJob.getJobRef().getJobId());
    } else {
      if (null != dispatchedJob.getJobRef().getName()) {
        params.put("jobName", dispatchedJob.getJobRef().getName());
      }
      if (null != dispatchedJob.getJobRef().getGroup()) {
        params.put("groupPath", dispatchedJob.getJobRef().getGroup());
      }
    }

    addNodeSetParams(params, dispatchedJob.getNodeSet(), dispatchedJob.isKeepgoing(), "extra.");
    addLoglevelParams(params, dispatchedJob.getLoglevel(), "extra.");
    if (null != dispatchedJob.getArgs() && dispatchedJob.getArgs().length > 0) {
      params.put("extra.argString", CLIUtils.generateArgline(null, dispatchedJob.getArgs()));
    }
    /*
     * submit request to the URL path, expecting standard "queued item list" result
     */
    return submitQueueRequest(null, params, RUNDECK_JOBS_RUN);
  }
Exemple #2
0
  private static ArrayList<String> buildCommandForNode(
      ExecArgList command, Map<String, Map<String, String>> dataContext, String osFamily) {
    final Converter<String, String> quote = CLIUtils.argumentQuoteForOperatingSystem(osFamily);
    final Converter<String, String> expand =
        DataContextUtils.replaceDataReferencesConverter(
            dataContext, DataContextUtils.replaceMissingOptionsWithBlank, false);

    final ArrayList<String> commandList = new ArrayList<String>();
    CommandVisitor visiter = new CommandVisitor(commandList, quote, expand);
    command.visitWith(visiter);
    return commandList;
  }
  public QueuedItemResult queueDispatcherJob(final IDispatchedJob dispatchedJob)
      throws CentralDispatcherException {
    final HashMap<String, String> params = new HashMap<String, String>();
    if (null == dispatchedJob.getJobRef()) {
      throw new IllegalArgumentException("JobRef was null");
    }
    final String jobid;
    if (null != dispatchedJob.getJobRef().getJobId()) {
      jobid = dispatchedJob.getJobRef().getJobId();
    } else {
      if (null == dispatchedJob.getJobRef().getName()
          || "".equals(dispatchedJob.getJobRef().getName())) {
        throw new CentralDispatcherException("job name input is required");
      }
      final String project = dispatchedJob.getJobRef().getProject();
      final String name = dispatchedJob.getJobRef().getName();
      final String group;
      if (null != dispatchedJob.getJobRef().getGroup()
          && !"".equals(dispatchedJob.getJobRef().getGroup())) {
        group = dispatchedJob.getJobRef().getGroup();
      } else {
        // indicates a top level job
        group = "-";
      }

      // Query to find matching job
      final Collection<IStoredJob> iStoredJobs =
          reallistStoredJobs(new IStoredJobsQueryImpl(name, group, null, project));
      if (null == iStoredJobs) {
        throw new CentralDispatcherException("Unable to query jobs");
      }
      if (iStoredJobs.size() < 1) {
        throw new CentralDispatcherException(
            "Job not found matching query: " + (null != group ? group : "") + "/" + name);
      }
      if (iStoredJobs.size() > 1) {
        ArrayList<String> ids = new ArrayList<String>();
        for (final IStoredJob iStoredJob : iStoredJobs) {
          ids.add(iStoredJob.getJobId());
        }
        throw new CentralDispatcherException(
            "Job was not unique: "
                + (null != group ? group : "")
                + "/"
                + name
                + ": "
                + iStoredJobs.size()
                + " jobs found: "
                + ids);
      }
      // use found id
      final IStoredJob next = iStoredJobs.iterator().next();
      jobid = next.getJobId();
    }

    addAPINodeSetParams(params, dispatchedJob.getNodeSet(), dispatchedJob.isKeepgoing());
    addLoglevelParams(params, dispatchedJob.getLoglevel());
    if (null != dispatchedJob.getArgs() && dispatchedJob.getArgs().length > 0) {
      params.put("argString", CLIUtils.generateArgline(null, dispatchedJob.getArgs()));
    }

    final String apipath = substitutePathVariable(RUNDECK_API_JOBS_RUN, "id", jobid);
    return submitExecutionRequest(null, params, apipath);
  }
  public QueuedItemResult queueDispatcherScript(final IDispatchedScript iDispatchedScript)
      throws CentralDispatcherException {
    final String argString;
    final String scriptString;
    final boolean isExec;

    try {

      // write script to file
      final InputStream stream = iDispatchedScript.getScriptAsStream();
      if (null != iDispatchedScript.getScript() || null != stream) {
        // full script
        if (null != iDispatchedScript.getScript()) {
          scriptString = iDispatchedScript.getScript();
        } else {
          // read stream to string
          final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
          Streams.copyStream(stream, byteArrayOutputStream);
          scriptString = new String(byteArrayOutputStream.toByteArray());
        }
        if (null != iDispatchedScript.getArgs() && iDispatchedScript.getArgs().length > 0) {
          argString = CLIUtils.generateArgline(null, iDispatchedScript.getArgs());
        } else {
          argString = null;
        }

        isExec = false;
      } else if (null != iDispatchedScript.getServerScriptFilePath()) {
        // server-local script filepath

        // read stream to string
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Streams.copyStream(
            new FileInputStream(new File(iDispatchedScript.getServerScriptFilePath())),
            byteArrayOutputStream);
        scriptString = new String(byteArrayOutputStream.toByteArray());

        if (null != iDispatchedScript.getArgs() && iDispatchedScript.getArgs().length > 0) {
          argString = CLIUtils.generateArgline(null, iDispatchedScript.getArgs());
        } else {
          argString = null;
        }
        isExec = false;
      } else if (null != iDispatchedScript.getArgs() && iDispatchedScript.getArgs().length > 0) {
        // shell command
        scriptString = null;
        argString = CLIUtils.generateArgline(null, iDispatchedScript.getArgs());
        isExec = true;
      } else {
        throw new IllegalArgumentException(
            "Dispatched script did not specify a command, script or filepath");
      }

    } catch (IOException e) {
      throw new CentralDispatcherServerRequestException(
          "Unable to queue command: " + e.getMessage(), e);
    }

    // request parameters
    final HashMap<String, String> params = new HashMap<String, String>();

    params.put("project", iDispatchedScript.getFrameworkProject());
    if (isExec) {
      params.put("exec", argString);
    } else {
      params.put("scriptFile", scriptString);
    }
    addLoglevelParams(params, iDispatchedScript.getLoglevel());
    addAPINodeSetParams(
        params, iDispatchedScript.getNodeSet(), iDispatchedScript.getNodeSet().isKeepgoing());

    return submitRunRequest(
        null, params, isExec ? RUNDECK_API_RUN_COMMAND : RUNDECK_API_RUN_SCRIPT);
  }