private ProcessExecutionResults startNode(File basedir) {
    if (log.isDebugEnabled()) {
      log.debug("Starting node at " + basedir);
    }
    File binDir = new File(basedir, "bin");
    File startScript;
    SystemInfo systemInfo = SystemInfoFactory.createSystemInfo();
    ProcessExecution startScriptExe;

    if (systemInfo.getOperatingSystemType() == OperatingSystemType.WINDOWS) {
      startScript = new File(binDir, "cassandra.bat");
      startScriptExe = createProcessExecution(null, startScript);
    } else {
      startScript = new File(binDir, "cassandra");
      startScriptExe = createProcessExecution(null, startScript);
      startScriptExe.addArguments(Arrays.asList("-p", "cassandra.pid"));
    }

    startScriptExe.setWaitForCompletion(0);

    ProcessExecutionResults results = systemInfo.executeProcess(startScriptExe);
    if (log.isDebugEnabled()) {
      log.debug(startScript + " returned with exit code [" + results.getExitCode() + "]");
    }

    return results;
  }
  @Override
  public void addDeployFile(String filename, String directory) {
    super.addDeployFile(filename, directory);

    String msg;
    File existingFile = new File(this.baseWorkingDirectory, filename);
    ProcessExecution pe = getUnzipExecution(existingFile, directory);

    if (pe != null) {
      ProcessExecutionResults results = this.systemInfo.executeProcess(pe);
      if (results.getError() != null) {
        msg = "Could not unbundle file [" + pe + "]: " + results;
        audit("deploy", BundleResourceDeploymentHistory.Status.FAILURE, msg);
        throw new RuntimeException(msg, results.getError());
      } else if (results.getExitCode() == null || results.getExitCode().intValue() > 0) {
        msg = "Failed to unbundle file [" + pe + "]: " + results;
        audit("deploy", BundleResourceDeploymentHistory.Status.FAILURE, msg);
        throw new RuntimeException(msg);
      } else {
        msg = "extracted files from [" + existingFile + "] to [" + directory + "]";
      }
      // existingFile.delete(); WOULD WE WANT TO REMOVE THE COMPRESSED FILE?
      audit("deploy", BundleResourceDeploymentHistory.Status.SUCCESS, msg);
    } else {
      // not a zipped format - just move the file to the directory as-is
      File newFile = new File(directory, filename);
      if (!existingFile.renameTo(newFile)) {
        msg = "Failed to move [" + existingFile + "] to [" + newFile + "]";
        audit("deploy", BundleResourceDeploymentHistory.Status.FAILURE, msg);
        throw new RuntimeException(msg);
      }
      audit(
          "deploy",
          BundleResourceDeploymentHistory.Status.SUCCESS,
          "renamed [" + existingFile + "] to [" + newFile + "]");
    }
  }
Example #3
0
  public OperationResult invokeOperation(String name, Configuration params) throws Exception {
    if (name.equals(EXECUTE_OPERATION)) {
      OperationResult operationResult = new OperationResult();

      File scriptFile = getScriptFile();
      SystemInfo systemInfo = this.resourceContext.getSystemInformation();
      ProcessExecution processExecution =
          ProcessExecutionUtility.createProcessExecution(scriptFile);

      processExecution.setWaitForCompletion(1000L * 60 * 60); // 1 hour
      processExecution.setCaptureOutput(true);

      // TODO: Make the script's cwd configurable, but default it to the
      // directory containing the script.
      processExecution.setWorkingDirectory(scriptFile.getParent());

      setEnvironmentVariables(processExecution);
      setCommandLineArguments(params, processExecution);

      if (log.isDebugEnabled()) {
        log.debug(processExecution);
      }

      ProcessExecutionResults processExecutionResults = systemInfo.executeProcess(processExecution);
      if (processExecutionResults.getError() != null) {
        throw new Exception(processExecutionResults.getError());
      }

      Integer exitCode = processExecutionResults.getExitCode();
      String output = processExecutionResults.getCapturedOutput(); // NOTE:
      // this
      // is
      // stdout
      // +
      // stderr

      Configuration complexResults = operationResult.getComplexResults();
      complexResults.put(new PropertySimple(EXIT_CODE_RESULT_PROP, exitCode));
      complexResults.put(new PropertySimple(OUTPUT_RESULT_PROP, output));
      if (exitCode != null && exitCode != 0) {
        operationResult.setErrorMessage(
            "Exit code was '" + exitCode + "', see operation results for details");
      }

      return operationResult;
    } else {
      throw new IllegalArgumentException("Unsupported operation: " + name);
    }
  }