Example usage for org.apache.commons.exec CommandLine CommandLine

List of usage examples for org.apache.commons.exec CommandLine CommandLine

Introduction

In this page you can find the example usage for org.apache.commons.exec CommandLine CommandLine.

Prototype

public CommandLine(final CommandLine other) 

Source Link

Document

Copy constructor.

Usage

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
 *//*  w w w.  j  a  v a 2s .  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.tupilabs.pbs.PBS.java

/**
 * PBS qsub command for an Array Job with Specific PBS_ARRAY_IDs to submit, and resource overrides
 * <p>//from www  . ja  v  a 2s.c  o  m
 * Equivalent to qsub -t 1,2,3 -l [resource_name=value,resource_name=value] [param]
 *
 * @param input job input file
 * @param pbsArrayIDs of specified PBS indices
 * @param resourceOverrides list of resource overrides
 * @return job id of array job
 */
public static String qsubArrayJob(String input, List<Integer> pbsArrayIDs, String... resourceOverrides) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    String listArgument = StringUtils.join(pbsArrayIDs, ",");
    cmdLine.addArgument(listArgument);
    cmdLine.addArgument(PARAMETER_RESOURCE_OVERRIDE_STATUS);
    String resourceOverrideArgument = StringUtils.join(resourceOverrides, ",");
    cmdLine.addArgument(resourceOverrideArgument);
    cmdLine.addArgument(input);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0)
        throw new PBSException("Failed to submit job script " + input + ". Error output: " + err.toString());

    String jobId = out.toString();
    return jobId.trim();
}

From source file:com.boundlessgeo.wps.grass.GrassProcesses.java

/**
 * Define a GISBASE/LOCATION_NAME for the provided dem.
 *
 * @param operation Name used for the location on disk
 * @param dem File used to establish CRS and Bounds for the location
 * @return//  ww  w  . jav  a  2s . co  m
 * @throws Exception
 */
static File location(CoordinateReferenceSystem crs) throws Exception {
    String code = CRS.toSRS(crs, true);
    File geodb = new File(System.getProperty("user.home"), "grassdata");
    File location = Files.createTempDirectory(geodb.toPath(), code).toFile();
    KVP kvp = new KVP("geodb", geodb, "location", location);

    // grass70 + ' -c epsg:' + myepsg + ' -e ' + location_path
    CommandLine cmd = new CommandLine(EXEC);
    cmd.addArgument("-c");
    cmd.addArgument("epsg:" + code);
    cmd.addArgument("-e");
    cmd.addArgument("${location}");
    cmd.setSubstitutionMap(kvp);

    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    executor.setWatchdog(new ExecuteWatchdog(60000));
    executor.setStreamHandler(new PumpStreamHandler(System.out));

    int exitValue = executor.execute(cmd);
    return location;
}

From source file:com.tibco.tgdb.test.lib.TGAdmin.java

/**
 * Invoke TG admin synchronously. //from   www  .j  ava2 s .  c o  m
 * Admin operation blocks until it is completed.
 * 
 * @param tgHome TG admin home
 * @param url Url to connect to TG server
 * @param user System User name
 * @param pwd System User password
 * @param logFile TG admin log file location - Generated by admin
 * @param logLevel Specify the log level: info/user1/user2/user3/debug/debugmemory/debugwire
 * @param cmd TG admin command file or command string
 * @param memSize Specify the maximum memory usage (MB). -1 for default (8 MB)
 * @param timeout Number of milliseconds allowed to complete admin operation
 * 
 * @return Output console of admin operation 
 * @throws TGAdminException Admin execution fails or timeout occurs 
 */
public static String invoke(String tgHome, String url, String user, String pwd, String logFile, String logLevel,
        String cmd, int memSize, long timeout) throws TGAdminException {
    if (tgHome == null)
        throw new TGAdminException("TGAdmin - TGDB home is not defined");
    File ftgHome = new File(tgHome);
    if (!ftgHome.exists())
        throw new TGAdminException("TGAdmin - TGDB home '" + tgHome + "' does not exist");

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(output);
    Executor tgExec = new DefaultExecutor();
    tgExec.setStreamHandler(psh);
    tgExec.setWorkingDirectory(new File(tgHome + "/bin"));

    CommandLine tgCL = new CommandLine((new File(tgHome + "/bin/" + process)).getAbsolutePath());

    // Define arguments
    List<String> args = new ArrayList<String>();
    if (url != null) {
        args.add("--url");
        args.add(url);
    }
    if (user != null) {
        args.add("--uid");
        args.add(user);
    }
    if (pwd != null) {
        args.add("--pwd");
        args.add(pwd);
    }
    if (logFile != null) {
        args.add("--log");
        args.add(logFile);
    }
    if (logLevel != null) {
        args.add("--log-level");
        args.add(logLevel);
    }
    if (memSize >= 0) {
        args.add("--max-memory");
        args.add(Integer.toString(memSize));
    }

    File cmdFile = null;
    if (cmd == null)
        throw new TGAdminException("TGAdmin - Command is required.");
    if (cmd.matches(
            "(?s)^(kill|show|describe|create|stop|info|checkpoint|dump|set|connect|disconnect|export|import|exit).*$")) {
        try {
            cmdFile = new File(tgHome + "/invokeAdminScript.txt");
            Files.write(Paths.get(cmdFile.toURI()), cmd.getBytes(StandardCharsets.UTF_8));
        } catch (IOException ioe) {
            throw new TGAdminException("TGAdmin - " + ioe.getMessage());
        }
    } else {
        cmdFile = new File(cmd);
    }

    if (!cmdFile.exists()) {
        throw new TGAdminException("TGAdmin - Command file '" + cmdFile + "' does not exist");
    }
    args.add("--file");
    args.add(cmdFile.getAbsolutePath());

    tgCL.addArguments((String[]) args.toArray(new String[args.size()]));

    ExecuteWatchdog tgWatch = new ExecuteWatchdog(timeout);
    tgExec.setWatchdog(tgWatch);
    System.out.println("TGAdmin - Invoking " + StringUtils.toString(tgCL.toStrings(), " "));

    long endProcTime = 0;
    long totalProcTime = 0;
    try {
        TGAdmin.startProcTime = System.currentTimeMillis();
        tgExec.execute(tgCL);
        endProcTime = System.currentTimeMillis();
    } catch (IOException ee) {
        if (tgWatch.killedProcess())
            throw new TGAdminException("TGAdmin - Operation did not complete within " + timeout + " ms",
                    output.toString());
        else {
            try {
                Thread.sleep(1000); // make sure output has time to fill up
            } catch (InterruptedException ie) {
                ;
            }
            throw new TGAdminException("TGAdmin - Execution failed: " + ee.getMessage(), output.toString());
        }
    }
    if (url != null && !output.toString().contains("Successfully connected to server"))
        throw new TGAdminException("TGAdmin - Admin could not connect to server " + url + " with user " + user,
                output.toString());
    if (endProcTime != 0)
        totalProcTime = endProcTime - TGAdmin.startProcTime;
    if (TGAdmin.showOperationBanner)
        System.out.println(
                "TGAdmin - Operation completed" + (totalProcTime > 0 ? " in " + totalProcTime + " msec" : ""));
    return output.toString();
}

From source file:com.googlecode.mycontainer.maven.plugin.ExecMojo.java

CommandLine getExecutablePath(Map enviro, File dir) {
    File execFile = new File(executable);
    String exec = null;/*from  ww  w  . j a  v a  2 s .  c  om*/
    if (execFile.exists()) {
        getLog().debug("Toolchains are ignored, 'executable' parameter is set to " + executable);
        exec = execFile.getAbsolutePath();
    } else {
        Toolchain tc = getToolchain();

        // if the file doesn't exist & toolchain is null, the exec is
        // probably in the PATH...
        // we should probably also test for isFile and canExecute, but the
        // second one is only
        // available in SDK 6.
        if (tc != null) {
            getLog().info("Toolchain in exec-maven-plugin: " + tc);
            exec = tc.findTool(executable);
        } else {
            if (OS.isFamilyWindows()) {
                String ex = executable.indexOf(".") < 0 ? executable + ".bat" : executable;
                File f = new File(dir, ex);
                if (f.exists()) {
                    exec = ex;
                } else {
                    // now try to figure the path from PATH, PATHEXT env
                    // vars
                    // if bat file, wrap in cmd /c
                    String path = (String) enviro.get("PATH");
                    if (path != null) {
                        String[] elems = StringUtils.split(path, File.pathSeparator);
                        for (int i = 0; i < elems.length; i++) {
                            f = new File(new File(elems[i]), ex);
                            if (f.exists()) {
                                exec = ex;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    if (exec == null) {
        exec = executable;
    }

    CommandLine toRet;
    if (OS.isFamilyWindows() && exec.toLowerCase(Locale.getDefault()).endsWith(".bat")) {
        toRet = new CommandLine("cmd");
        toRet.addArgument("/c");
        toRet.addArgument(exec);
    } else {
        toRet = new CommandLine(exec);
    }

    return toRet;
}

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);//ww  w  .  j  a  v a  2  s. c  o  m

    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 {
        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:com.netflix.genie.web.tasks.job.JobCompletionService.java

/**
 * Uploads the job directory to the archive location.
 *
 * @param job The job./*ww  w .  j  a v  a  2 s.  c  o m*/
 * @throws GenieException if there is any problem
 */
private boolean processJobDir(final Job job) throws GenieException, IOException {
    log.debug("Got a job finished event. Will process job directory.");
    boolean result = false;
    final Optional<String> oJobId = job.getId();

    // The deletion of dependencies and archiving only happens for job requests which are not Invalid.
    if (oJobId.isPresent()
            && !(this.jobSearchService.getJobStatus(job.getId().get()).equals(JobStatus.INVALID))) {
        final String jobId = oJobId.get();
        final File jobDir = new File(this.baseWorkingDir, jobId);

        if (jobDir.exists()) {
            if (this.deleteDependencies) {
                this.deleteApplicationDependencies(jobId, jobDir);
            }

            final Optional<String> archiveLocation = job.getArchiveLocation();
            if (archiveLocation.isPresent() && !Strings.isNullOrEmpty(archiveLocation.get())) {
                log.debug("Archiving job directory");
                // Create the tar file
                final File localArchiveFile = new File(jobDir, "genie/logs/" + jobId + ".tar.gz");

                final CommandLine commandLine;
                if (this.runAsUserEnabled) {
                    commandLine = new CommandLine("sudo");
                    commandLine.addArgument("tar");
                } else {
                    commandLine = new CommandLine("tar");
                }
                commandLine.addArgument("-c");
                commandLine.addArgument("-z");
                commandLine.addArgument("-f");
                commandLine.addArgument(localArchiveFile.getCanonicalPath());
                commandLine.addArgument("./");

                this.executor.setWorkingDirectory(jobDir);

                log.debug("Archive command : {}", commandLine.toString());
                this.executor.execute(commandLine);

                // Upload the tar file to remote location
                this.genieFileTransferService.putFile(localArchiveFile.getCanonicalPath(),
                        archiveLocation.get());

                // At this point the archive file is successfully uploaded to archive location specified in the job.
                // Now we can delete it from local disk to save space if enabled.
                if (this.deleteArchiveFile) {
                    log.debug("Deleting archive file");
                    try {
                        if (this.runAsUserEnabled) {
                            final CommandLine deleteCommand = new CommandLine("sudo");
                            deleteCommand.addArgument("rm");
                            deleteCommand.addArgument("-f");
                            deleteCommand.addArgument(localArchiveFile.getCanonicalPath());

                            this.executor.setWorkingDirectory(jobDir);
                            log.debug("Delete command: {}", deleteCommand.toString());
                            this.executor.execute(deleteCommand);
                        } else if (!localArchiveFile.delete()) {
                            log.error("Failed to delete archive file for job: {}", jobId);
                            this.archiveFileDeletionFailure.increment();
                        }
                    } catch (final Exception e) {
                        log.error("Failed to delete archive file for job: {}", jobId, e);
                        this.archiveFileDeletionFailure.increment();
                    }
                }
                result = true;
            }
        }
    }
    return result;
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * PBS qsub command for an Array Job with Specific PBS_ARRAY_IDs to submit
 * <p>//w w  w.  ja  v a 2s .c om
 * Equivalent to qsub -t 5-20 [param]
 *
 * @param input job input file
 * @param beginIndex beginning of index range
 * @param endIndex end of index range
 * @return job id of array job
 */
public static String qsubArrayJob(String input, int beginIndex, int endIndex) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    String rangeArgument = beginIndex + "-" + endIndex;
    cmdLine.addArgument(rangeArgument);
    cmdLine.addArgument(input);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0)
        throw new PBSException("Failed to submit job script " + input + ". Error output: " + err.toString());

    String jobId = out.toString();
    return jobId.trim();
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * PBS qsub command for an Array Job with Specific PBS_ARRAY_IDs to submit AND a range to submit
 * <p>//from w  w w .ja v  a  2  s  .  c o  m
 * Equivalent to qsub -t 1,2,3,5-20 [param]
 *
 * @param input job input file
 * @param pbsArrayIDs list of specified indices
 * @param beginIndex beginning of index range
 * @param endIndex end of index range
 * @return job id of array job
 */
public static String qsubArrayJob(String input, List<Integer> pbsArrayIDs, int beginIndex, int endIndex) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    String rangeArgument = beginIndex + "-" + endIndex;
    String listArgument = StringUtils.join(pbsArrayIDs, ",");
    String combinedArgument = listArgument + "," + rangeArgument;
    cmdLine.addArgument(combinedArgument);
    cmdLine.addArgument(input);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0)
        throw new PBSException("Failed to submit job script " + input + ". Error output: " + err.toString());

    String jobId = out.toString();
    return jobId.trim();
}

From source file:it.drwolf.ridire.index.cwb.CWBPatternSearcher.java

private void executeCQPQuery(File queryFile) throws ExecuteException, IOException {
    Executor executor = new DefaultExecutor();
    File tempSh = File.createTempFile("ridireSH", ".sh");
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append("export LC_ALL=C\n");
    stringBuffer.append(this.cqpExecutable + " -f " + queryFile.getAbsolutePath() + " -D " + this.cqpCorpusName
            + " -r " + this.cqpRegistry + "\n");
    FileUtils.writeStringToFile(tempSh, stringBuffer.toString());
    tempSh.setExecutable(true);/*w ww  .  j av  a 2s  . c o m*/
    CommandLine commandLine = new CommandLine(tempSh.getAbsolutePath());
    executor.execute(commandLine);
    FileUtils.deleteQuietly(tempSh);
}