コード例 #1
0
  public ProcessOutput execute(
      JobConsoleLogger console,
      boolean showConsoleOutput,
      String workingDir,
      List<String> command,
      Map<String, String> envMap) {
    ProcessBuilder processBuilder = new ProcessBuilder(command);
    Process process = null;
    ProcessOutput processOutput = null;
    long startTime = System.currentTimeMillis();
    try {
      logger.info(String.format("External process '%s' started", command.get(0)));
      if (workingDir != null) {
        processBuilder.directory(new File(workingDir));
      }
      processBuilder.redirectErrorStream(true).environment().putAll(envMap);
      process = processBuilder.start();

      if (showConsoleOutput) {
        LinesInputStream output = new LinesInputStream(process.getInputStream());
        console.readOutputOf(output);
        int returnCode = process.waitFor();
        waitUntilEmpty(2000, output, null);
        processOutput = new ProcessOutput(returnCode, output.getLines(), new ArrayList<String>());
      } else {
        List output = IOUtils.readLines(process.getInputStream());
        int returnCode = process.waitFor();
        List pendingOutput = IOUtils.readLines(process.getInputStream());
        output.addAll(pendingOutput);
        processOutput = new ProcessOutput(returnCode, output, new ArrayList<String>());
      }
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage());
    } finally {
      if (process != null) {
        closeQuietly(process.getInputStream());
        closeQuietly(process.getErrorStream());
        closeQuietly(process.getOutputStream());
        process.destroy();
        long estimatedTime = System.currentTimeMillis() - startTime;
        logger.info(
            String.format(
                "External process '%s' returned %d after %dms.",
                command.get(0), processOutput.getReturnCode(), estimatedTime));
      }
    }
    return processOutput;
  }
コード例 #2
0
 public void waitUntilEmpty(int timeout, LinesInputStream output, LinesInputStream error)
     throws InterruptedException {
   int numOutLines = output.getLines().size();
   int numErrLines = error != null ? error.getLines().size() : 0;
   while (true) {
     logger.info(String.format("Waiting %dms for stdour/stderr to settle ...", timeout));
     Thread.sleep(timeout, 0);
     int newNumOutLines = output.getLines().size();
     int newNumErrLines = error != null ? error.getLines().size() : 0;
     if (numOutLines == newNumOutLines && newNumErrLines == numErrLines) {
       logger.info("stdour/stderr no longer changing");
       break;
     }
     numOutLines = newNumOutLines;
     numErrLines = newNumErrLines;
   }
 }
コード例 #3
0
 MavenVersion getLatest(List<MavenVersion> allVersions) {
   if (allVersions == null || allVersions.isEmpty()) return null;
   MavenVersion latest = maxSubjectToUpperBound(allVersions);
   if (latest == null) {
     LOGGER.info("maxSubjectToUpperBound is null");
     return null;
   }
   if (lookupParams.isLastVersionKnown()) {
     LOGGER.info("lastKnownVersion is " + lookupParams.getLastKnownVersion());
     MavenVersion lastKnownVersion = new MavenVersion(lookupParams.getLastKnownVersion());
     if (noNewerVersion(latest, lastKnownVersion)) {
       LOGGER.info("no newer version");
       return null;
     }
   }
   if (!lookupParams.lowerBoundGiven() || latest.greaterOrEqual(lookupParams.lowerBound())) {
     return latest;
   } else {
     LOGGER.info("latestSubjectToLowerBound is null");
     return null;
   }
 }
コード例 #4
0
  @Override
  public void run() throws Exception {
    DockerCreateCommand cmd = new DockerCreateCommand(console, configVars);
    cmd.run();
    if (cmd.isSuccessful()) {
      // String id = cmd.getProcessOutput().getStdOut().get(0);
      String id = cmd.getContainerName(); // tmp<.....>
      logger.info(String.format("We think the container's name is %s", id));

      if (!configVars.isEmpty(DockerTask.RUN_PRE_COPY_TO)) {
        String hostDir =
            makeHostDir(getAbsoluteWorkingDir(), configVars.getValue(DockerTask.RUN_PRE_COPY_FROM));
        logger.info(String.format("Copying FROM %s", hostDir));
        new DockerCpCommand(
                console,
                configVars,
                hostDir,
                id + ":" + configVars.getValue(DockerTask.RUN_PRE_COPY_TO))
            .run();
      }

      new DockerStartCommand(console, configVars, id).run();

      if (!configVars.isEmpty(DockerTask.RUN_POST_COPY_FROM)) {
        String hostDir =
            makeHostDir(getAbsoluteWorkingDir(), configVars.getValue(DockerTask.RUN_POST_COPY_TO));
        logger.info(String.format("Copying TO %s", hostDir));
        new DockerCpCommand(
                console,
                configVars,
                id + ":" + configVars.getValue(DockerTask.RUN_POST_COPY_FROM),
                hostDir)
            .run();
      }

      new DockerRmCommand(console, configVars, id).run();
    }
  }
コード例 #5
0
 public MavenVersion getLatest() {
   RepoResponse repoResponse = repositoryConnector.makeAllVersionsRequest(lookupParams);
   LOGGER.debug(repoResponse.responseBody);
   List<MavenVersion> allVersions = getAllVersions(repoResponse);
   MavenVersion latest = getLatest(allVersions);
   if (latest != null) {
     latest.setArtifactId(lookupParams.getArtifactId());
     latest.setGroupId(lookupParams.getGroupId());
     LOGGER.info("Latest is " + latest.getRevisionLabel());
     setLocationAndTrackBack(latest);
   } else {
     LOGGER.warn("getLatest returning null");
   }
   return latest;
 }
コード例 #6
0
 Files getFiles(MavenVersion version) {
   RepoResponse repoResponse =
       repositoryConnector.makeFilesRequest(lookupParams, version.getV_Q());
   LOGGER.debug(repoResponse.responseBody);
   NexusResponseHandler nexusReponseHandler = new NexusResponseHandler(repoResponse);
   List<String> files;
   String pomFile = null;
   if (nexusReponseHandler.canHandle()) {
     files = nexusReponseHandler.getFilesMatching(lookupParams.getArtifactSelectionPattern());
     pomFile = nexusReponseHandler.getPOMurl();
     LOGGER.info("pomFile is " + pomFile);
   } else {
     LOGGER.warn("Returning empty version list - no XML nor HTML Nexus answer found");
     files = Collections.emptyList();
   }
   LOGGER.info("Files: " + files);
   return files.size() > 0
       ? new Files(
           repositoryConnector.getFilesUrlWithBasicAuth(lookupParams, version.getV_Q()),
           files.get(0),
           repositoryConnector.getFilesUrl(lookupParams, version.getV_Q()) + pomFile,
           lookupParams)
       : null;
 }