List of usage examples for org.apache.commons.exec CommandLine addArgument
public CommandLine addArgument(final String argument)
From source file:de.esukom.decoit.ifmapclient.iptables.RulesExecutor.java
public boolean executePredefinedRule(byte ruleType, String[] arg) { IfMapClient.LOGGER.info("[IPTABLES] -> executing predefined command..."); // use apache's commons-exec for executing command! CommandLine cmdLine = new CommandLine(command); // get the predefined rule-parameters String[] ruleParams = Rules.getPredefindedRuleParameters(ruleType, arg); if (ruleParams != null) { // add rule-parameters to CommanLine-Object for (int i = 0; i < ruleParams.length; i++) { cmdLine.addArgument(ruleParams[i]); }//from w w w. j a v a 2 s .co m // execute command DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); ExecuteWatchdog watchdog = new ExecuteWatchdog(mTimeout); Executor executor = new DefaultExecutor(); executor.setExitValue(1); executor.setWatchdog(watchdog); try { executor.execute(cmdLine, resultHandler); } catch (ExecuteException e) { IfMapClient.LOGGER.warning( "[IPTABLES] -> error while executing predefined command: execute-exception occured!"); return false; } catch (IOException e) { IfMapClient.LOGGER .warning("[IPTABLES] -> error while executing predefined command: io-exception occured!"); return false; } try { // some time later the result handler callback was invoked so we // can safely request the exit value resultHandler.waitFor(); int exitCode = resultHandler.getExitValue(); IfMapClient.LOGGER.warning("[IPTABLES] -> command executed, exit-code is: " + exitCode); switch (exitCode) { case EXIT_CODE_SUCCESS: return true; case EXIT_CODE_ERROR: return false; default: return false; } } catch (InterruptedException e) { IfMapClient.LOGGER.warning( "[IPTABLES] -> error while executing predefined command: interrupted-exception occured!"); return false; } } else { IfMapClient.LOGGER.warning( "[IPTABLES] -> error while excuting predefined command: rule-parameters-list is null!"); return false; } }
From source file:com.blackducksoftware.tools.scmconnector.integrations.mercurial.MercurialConnector.java
@Override public int sync() { int exitStatus = -1; CommandLine command = CommandLine.parse(EXECUTABLE); String sourceDirIncludingModule = addPathComponentToPath(getFinalSourceDirectory(), getModuleNameFromURL(repositoryURL)); File dir = new File(sourceDirIncludingModule); if (localWorkingCopyExists(dir, ".hg", getModuleNameFromURL(repositoryURL))) { command.addArgument("pull"); command.addArgument("-u"); } else {// w w w . ja v a 2 s . c om // Need to adjust the working dir for Mercury checkout: must not // include the module dir part sourceDirIncludingModule = getFinalSourceDirectory(); dir = new File(sourceDirIncludingModule); command.addArgument("clone"); command.addArgument(repositoryURL); } try { exitStatus = commandLineExecutor.executeCommand(log, command, dir); } catch (Exception e) { log.error("Problem performing command", e); } return exitStatus; }
From source file:com.blackducksoftware.tools.scmconnector.integrations.git.GitConnector.java
private void gitCheckoutBranchShaOrPrefixedTag(String branchShaOrPrefixedTag) throws Exception { CommandLine command = CommandLine.parse(EXECUTABLE); String sourceDirIncludingModule = addPathComponentToPath(getFinalSourceDirectory(), getModuleNameFromURLGit(repositoryURL)); File targetDir = new File(sourceDirIncludingModule); command.addArgument("checkout"); command.addArgument(branchShaOrPrefixedTag); int exitStatus = commandLineExecutor.executeCommand(log, command, targetDir); if (exitStatus != 0) { throw new Exception("Git checkout " + branchShaOrPrefixedTag + " failed: " + exitStatus); }//from www.j a va 2 s . co m }
From source file:com.alibaba.jstorm.utils.JStormUtils.java
/** * If it is backend, please set resultHandler, such as DefaultExecuteResultHandler If it is frontend, ByteArrayOutputStream.toString get the result * <p/>/*from www.ja v a 2 s . c o m*/ * This function don't care whether the command is successfully or not * * @param command * @param environment * @param workDir * @param resultHandler * @return * @throws IOException */ public static ByteArrayOutputStream launchProcess(String command, final Map environment, final String workDir, ExecuteResultHandler resultHandler) throws IOException { String[] cmdlist = command.split(" "); CommandLine cmd = new CommandLine(cmdlist[0]); for (String cmdItem : cmdlist) { if (StringUtils.isBlank(cmdItem) == false) { cmd.addArgument(cmdItem); } } DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); if (StringUtils.isBlank(workDir) == false) { executor.setWorkingDirectory(new File(workDir)); } ByteArrayOutputStream out = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(out, out); if (streamHandler != null) { executor.setStreamHandler(streamHandler); } try { if (resultHandler == null) { executor.execute(cmd, environment); } else { executor.execute(cmd, environment, resultHandler); } } catch (ExecuteException e) { // @@@@ // failed to run command } return out; }
From source file:edu.isi.misd.scanner.network.registry.data.service.RegistryServiceImpl.java
@Override public String convertDataProcessingSpecification(String workingDir, String execName, String args, String input, Integer dataSetId) throws Exception { Executor exec = new DefaultExecutor(); exec.setWorkingDirectory(new File(workingDir)); CommandLine cl = new CommandLine(execName); cl.addArgument(args); ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes("UTF-8")); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(out, err, in); exec.setStreamHandler(streamHandler); try {/* www . ja va 2 s. c o m*/ exec.execute(cl); } catch (Exception e) { throw new Exception(e.toString() + "\n\n" + err.toString("UTF-8")); } String output = out.toString("UTF-8"); if (dataSetId != null) { DataSetDefinition dataSet = dataSetDefinitionRepository.findOne(dataSetId); if (dataSet == null) { throw new ResourceNotFoundException(dataSetId); } dataSet.setDataProcessingSpecification(input); dataSet.setDataProcessingProgram(output); dataSetDefinitionRepository.save(dataSet); } return output; }
From source file:info.pancancer.arch3.worker.WorkerRunnable.java
/** * This function will execute a workflow, based on the content of the Job object that is passed in. * * @param message/*from w w w .j a va 2 s. c o m*/ * - The message that will be published on the queue when the worker starts running the job. * @param job * - The job contains information about what workflow to execute, and how. * @return The complete stdout and stderr from the workflow execution will be returned. */ private WorkflowResult launchJob(String message, Job job, String seqwareEngine, String seqwareSettingsFile, String dockerImage) { WorkflowResult workflowResult = null; ExecutorService exService = Executors.newFixedThreadPool(2); WorkflowRunner workflowRunner = new WorkflowRunner(); try { Path pathToINI = writeINIFile(job); resultsChannel.basicPublish(this.resultsQueueName, this.resultsQueueName, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes(StandardCharsets.UTF_8)); resultsChannel.waitForConfirms(); //TODO: Parameterize dockerImage if (dockerImage == null || dockerImage.trim() == null) { dockerImage = "pancancer/seqware_whitestar_pancancer:latest"; } CommandLine cli = new CommandLine("docker"); cli.addArgument("run"); List<String> args = new ArrayList<>( Arrays.asList("--rm", "-h", "master", "-t", "-v", "/var/run/docker.sock:/var/run/docker.sock", "-v", job.getWorkflowPath() + ":/workflow", "-v", pathToINI + ":/ini", "-v", "/datastore:/datastore", "-v", "/home/" + this.userName + "/.gnos:/home/ubuntu/.gnos")); if (seqwareSettingsFile != null) { args.addAll(Arrays.asList("-v", seqwareSettingsFile + ":/home/seqware/.seqware/settings")); } args.addAll(Arrays.asList(dockerImage, "seqware", "bundle", "launch", "--dir", "/workflow", "--ini", "/ini", "--no-metadata", "--engine", seqwareEngine)); String[] argsArray = new String[args.size()]; cli.addArguments(args.toArray(argsArray)); WorkerHeartbeat heartbeat = new WorkerHeartbeat(); heartbeat.setQueueName(this.resultsQueueName); // channels should not be shared between threads https://www.rabbitmq.com/api-guide.html#channel-threads // heartbeat.setReportingChannel(resultsChannel); heartbeat.setSettings(settings); heartbeat.setSecondsDelay( settings.getDouble(Constants.WORKER_HEARTBEAT_RATE, WorkerHeartbeat.DEFAULT_DELAY)); heartbeat.setJobUuid(job.getUuid()); heartbeat.setVmUuid(this.vmUuid); heartbeat.setNetworkID(this.networkAddress); heartbeat.setStatusSource(workflowRunner); long presleep = settings.getLong(Constants.WORKER_PREWORKER_SLEEP, WorkerRunnable.DEFAULT_PRESLEEP); long postsleep = settings.getLong(Constants.WORKER_POSTWORKER_SLEEP, WorkerRunnable.DEFAULT_POSTSLEEP); long presleepMillis = Base.ONE_SECOND_IN_MILLISECONDS * presleep; long postsleepMillis = Base.ONE_SECOND_IN_MILLISECONDS * postsleep; workflowRunner.setCli(cli); workflowRunner.setPreworkDelay(presleepMillis); workflowRunner.setPostworkDelay(postsleepMillis); // Submit both @SuppressWarnings("unused") // We will never actually do submit.get(), because the heartbeat should keep running until it is terminated by // exService.shutdownNow(). Future<?> submit = exService.submit(heartbeat); Future<WorkflowResult> workflowResultFuture = exService.submit(workflowRunner); // make sure both are complete workflowResult = workflowResultFuture.get(); // don't get the heartbeat if the workflow is complete already log.info("Docker execution result: " + workflowResult.getWorkflowStdout()); } catch (SocketException e) { // This comes from trying to get the IP address. log.error(e.getMessage(), e); } catch (IOException e) { // This could be caused by a problem writing the file, or publishing a message to the queue. log.error(e.getMessage(), e); } catch (ExecutionException e) { log.error("Error executing workflow: " + e.getMessage(), e); } catch (InterruptedException e) { log.error("Workflow may have been interrupted: " + e.getMessage(), e); } finally { exService.shutdownNow(); } return workflowResult; }
From source file:com.netflix.genie.web.tasks.job.JobCompletionService.java
/** * Delete the application dependencies off disk to save space. * * @param jobId The ID of the job to delete dependencies for * @param jobDir The job working directory *//*from w ww . j a va2s .co m*/ private void deleteApplicationDependencies(final String jobId, final File jobDir) { log.debug("Deleting dependencies as its enabled."); if (jobDir.exists()) { try { final List<String> appIds = this.jobSearchService.getJobApplications(jobId).stream() .map(Application::getId).filter(Optional::isPresent).map(Optional::get) .collect(Collectors.toList()); for (final String appId : appIds) { final File appDependencyDir = new File(jobDir, JobConstants.GENIE_PATH_VAR + JobConstants.FILE_PATH_DELIMITER + JobConstants.APPLICATION_PATH_VAR + JobConstants.FILE_PATH_DELIMITER + appId + JobConstants.FILE_PATH_DELIMITER + JobConstants.DEPENDENCY_FILE_PATH_PREFIX); if (appDependencyDir.exists()) { if (this.runAsUserEnabled) { final CommandLine deleteCommand = new CommandLine("sudo"); deleteCommand.addArgument("rm"); deleteCommand.addArgument("-rf"); deleteCommand.addArgument(appDependencyDir.getCanonicalPath()); log.debug("Delete command is {}", deleteCommand.toString()); this.executor.execute(deleteCommand); } else { FileUtils.deleteDirectory(appDependencyDir); } } } } catch (Exception e) { log.error("Could not delete job dependencies after completion for job: {} due to error {}", jobId, e); this.deleteDependenciesFailure.increment(); } } }
From source file:com.netflix.genie.web.tasks.job.JobCompletionService.java
/** * An external fail-safe mechanism to clean up processes left behind by the run.sh after the * job is killed or failed. This method is a no-op for jobs whose status is INVALID. * * @param jobId The id of the job to cleanup processes for. *//*www . j a v a2s . c o m*/ private void cleanupProcesses(final String jobId) { try { if (!this.jobSearchService.getJobStatus(jobId).equals(JobStatus.INVALID)) { this.jobSearchService.getJobExecution(jobId).getProcessId().ifPresent(pid -> { try { final CommandLine commandLine = new CommandLine(JobConstants.UNIX_PKILL_COMMAND); commandLine.addArgument(JobConstants.getKillFlag()); commandLine.addArgument(Integer.toString(pid)); this.executor.execute(commandLine); // The process group should not exist and the above code should always throw and exception. // If it does not then the bash script is not cleaning up stuff well during kills // or the script is done but child processes are still remaining. This metric tracks all that. this.processGroupCleanupFailureRate.increment(); } catch (final Exception e) { log.debug("Received expected exception. Ignoring."); } }); } } catch (final GenieException ge) { log.error("Unable to cleanup process for job due to exception. " + jobId, ge); this.processGroupCleanupFailureRate.increment(); } }
From source file:it.drwolf.ridire.index.cwb.CWBFrequencyList.java
private String getFrequencyList(boolean deleteFLFile, List<String> semDescription, List<String> funDescription, int quantityP, String type, Integer threshold, boolean sorted) { CommandLine commandLine = CommandLine.parse(this.cwbscanExecutable); commandLine.addArgument("-q"); if (threshold != null && threshold > 0) { commandLine.addArgument("-f"); commandLine.addArgument(threshold + ""); }//from w ww. j a va 2s . c o m commandLine.addArgument("-r").addArgument(this.cqpRegistry); commandLine.addArgument("-C"); commandLine.addArgument(this.cqpCorpusName); if (type.equals("forma")) { commandLine.addArgument("word+0"); } else if (type.equals("PoS")) { commandLine.addArgument("pos+0"); } else if (type.equals("easypos")) { commandLine.addArgument("easypos+0"); } else if (type.equals("lemma")) { commandLine.addArgument("lemma+0"); } else if (type.equals("PoS-forma")) { commandLine.addArgument("pos+0"); commandLine.addArgument("word+0"); } else if (type.equals("PoS-lemma")) { commandLine.addArgument("pos+0"); commandLine.addArgument("lemma+0"); } String semFuncParam = ""; if (funDescription != null && funDescription.size() > 0 && funDescription.get(0) != null && funDescription.get(0).trim().length() > 0 || semDescription != null && semDescription.size() > 0 && semDescription.get(0) != null && semDescription.get(0).trim().length() > 0) { semFuncParam = "?"; if (funDescription != null && funDescription.size() > 0 && funDescription.get(0) != null && funDescription.get(0).trim().length() > 0) { String fd = StringUtils.join(funDescription, "\\|"); semFuncParam += "text_functional=/\\(" + fd + "\\)/ "; } if (semDescription != null && semDescription.size() > 0 && semDescription.get(0) != null && semDescription.get(0).trim().length() > 0) { String sd = StringUtils.join(semDescription, "\\|"); semFuncParam += "text_semantic=/\\(" + sd + "\\)/ "; } commandLine.addArgument(semFuncParam); } if (sorted) { commandLine.addArgument("|"); commandLine.addArgument("sort"); commandLine.addArgument("-nr"); commandLine.addArgument("-k"); commandLine.addArgument("1"); } if (quantityP > 0) { commandLine.addArgument("|"); commandLine.addArgument("head"); commandLine.addArgument("-" + quantityP); } File flTempFile = null; try { flTempFile = File.createTempFile("ridireFL", null); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } commandLine.addArgument(" > "); commandLine.addArgument(flTempFile.getAbsolutePath()); String c = commandLine.toString(); try { File tempSh = File.createTempFile("ridireSH", ".sh"); FileUtils.writeStringToFile(tempSh, c); tempSh.setExecutable(true); commandLine = CommandLine.parse(tempSh.getAbsolutePath()); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(CWBFrequencyList.TIMEOUT); executor.setWatchdog(watchdog); ByteArrayOutputStream baosStdOut = new ByteArrayOutputStream(1024); ByteArrayOutputStream baosStdErr = new ByteArrayOutputStream(1024); ExecuteStreamHandler executeStreamHandler = new PumpStreamHandler(baosStdOut, baosStdErr, null); executor.setStreamHandler(executeStreamHandler); int exitValue = 0; exitValue = executor.execute(commandLine); FileUtils.deleteQuietly(tempSh); if (exitValue == 0) { StrTokenizer strTokenizer = new StrTokenizer(); this.frequencyList = new ArrayList<FrequencyItem>(); List<String> lines = FileUtils.readLines(flTempFile); for (String line : lines) { strTokenizer.reset(line); String[] tokens = strTokenizer.getTokenArray(); if (tokens.length == 2) { FrequencyItem frequencyItem = new FrequencyItem(tokens[1], Integer.parseInt(tokens[0].trim())); this.frequencyList.add(frequencyItem); } else if (tokens.length == 3) { FrequencyItem frequencyItem = new FrequencyItem(tokens[2], tokens[1], Integer.parseInt(tokens[0].trim())); this.frequencyList.add(frequencyItem); } } if (deleteFLFile) { FileUtils.deleteQuietly(flTempFile); } } } catch (ExecuteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return flTempFile.getAbsolutePath(); }
From source file:ddf.content.plugin.video.VideoThumbnailPlugin.java
private CommandLine getFFmpegCreateThumbnailCommand(final String videoFilePath, final String thumbnailFilePath, final String seek, final int numFrames) { final String filterChainFlag = "-vf"; final String filterChain = "thumbnail,scale=200:-1"; final String videoFramesToOutputFlag = "-frames:v"; final String videoFramesToOutput = String.valueOf(numFrames); final String videoSyncFlag = "-vsync"; final String videoSyncVariableFrameRate = "vfr"; final CommandLine command = new CommandLine(ffmpegPath).addArgument(SUPPRESS_PRINTING_BANNER_FLAG); if (seek != null) { final String seekFlag = "-ss"; command.addArgument(seekFlag).addArgument(seek); }//from w w w . ja v a 2s.c o m command.addArgument(INPUT_FILE_FLAG).addArgument(videoFilePath, DONT_HANDLE_QUOTING) .addArgument(filterChainFlag).addArgument(filterChain).addArgument(videoFramesToOutputFlag) .addArgument(videoFramesToOutput) // The "-vsync vfr" argument prevents frames from being duplicated, which allows us // to get a different thumbnail for each of the output images. .addArgument(videoSyncFlag).addArgument(videoSyncVariableFrameRate); command.addArgument(thumbnailFilePath, DONT_HANDLE_QUOTING).addArgument(OVERWRITE_EXISTING_FILE_FLAG); return command; }