Search in sources :

Example 76 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project zeppelin by apache.

the class ShellInterpreter method createSecureConfiguration.

public void createSecureConfiguration() throws InterpreterException {
    Properties properties = getProperties();
    CommandLine cmdLine = CommandLine.parse(shell);
    cmdLine.addArgument("-c", false);
    String kinitCommand = String.format("kinit -k -t %s %s", properties.getProperty("zeppelin.shell.keytab.location"), properties.getProperty("zeppelin.shell.principal"));
    cmdLine.addArgument(kinitCommand, false);
    DefaultExecutor executor = new DefaultExecutor();
    try {
        executor.execute(cmdLine);
    } catch (Exception e) {
        LOGGER.error("Unable to run kinit for zeppelin user " + kinitCommand, e);
        throw new InterpreterException(e);
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) Properties(java.util.Properties) ExecuteException(org.apache.commons.exec.ExecuteException) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) IOException(java.io.IOException)

Example 77 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project zeppelin by apache.

the class ShellInterpreter method internalInterpret.

@Override
public InterpreterResult internalInterpret(String cmd, InterpreterContext context) {
    LOGGER.debug("Run shell command '{}'", cmd);
    CommandLine cmdLine = CommandLine.parse(shell);
    // they need to be delimited by '&&' instead
    if (isWindows) {
        String[] lines = StringUtils.split(cmd, "\n");
        cmd = StringUtils.join(lines, " && ");
    }
    cmdLine.addArgument(cmd, false);
    try {
        contextMap.put(context.getParagraphId(), context);
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(new PumpStreamHandler(context.out, context.out));
        executor.setWatchdog(new ExecuteWatchdog(Long.MAX_VALUE));
        executorMap.put(context.getParagraphId(), executor);
        if (Boolean.valueOf(getProperty(DIRECTORY_USER_HOME))) {
            executor.setWorkingDirectory(new File(System.getProperty("user.home")));
        }
        int exitVal = executor.execute(cmdLine);
        LOGGER.info("Paragraph {} return with exit value: {}", context.getParagraphId(), exitVal);
        if (exitVal == 0) {
            return new InterpreterResult(Code.SUCCESS);
        } else {
            return new InterpreterResult(Code.ERROR);
        }
    } catch (ExecuteException e) {
        int exitValue = e.getExitValue();
        LOGGER.error("Can not run command: " + cmd, e);
        Code code = Code.ERROR;
        StringBuilder messageBuilder = new StringBuilder();
        if (exitValue == 143) {
            code = Code.INCOMPLETE;
            messageBuilder.append("Paragraph received a SIGTERM\n");
            LOGGER.info("The paragraph {} stopped executing: {}", context.getParagraphId(), messageBuilder.toString());
        }
        messageBuilder.append("ExitValue: " + exitValue);
        return new InterpreterResult(code, messageBuilder.toString());
    } catch (IOException e) {
        LOGGER.error("Can not run command: " + cmd, e);
        return new InterpreterResult(Code.ERROR, e.getMessage());
    } finally {
        executorMap.remove(context.getParagraphId());
        contextMap.remove(context.getParagraphId());
    }
}
Also used : DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) IOException(java.io.IOException) Code(org.apache.zeppelin.interpreter.InterpreterResult.Code) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException) File(java.io.File)

Example 78 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project tycho by eclipse.

the class JdkLibraryInfoProvider method generateLibraryInfo.

private LibraryInfo generateLibraryInfo(String javaHome) {
    String executable = javaHome + File.separator + "bin" + File.separator + "java";
    if (File.separatorChar == '\\') {
        executable = executable + ".exe";
    }
    if (!new File(executable).isFile()) {
        getLog().warn(executable + " not found. Fallback to scan " + javaHome + "/lib/*.jar and " + javaHome + "/lib/ext/*.jar for bootclasspath");
        return new LibraryInfo("unknown", scanLibFolders(javaHome), new String[0], new String[0]);
    }
    CommandLine cli = new CommandLine(executable);
    cli.addArguments(new String[] { "-classpath", getLibDetectorJar().getAbsolutePath(), "org.eclipse.tycho.libdetector.LibraryDetector" }, false);
    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(30 * 1000L);
    executor.setWatchdog(watchdog);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler handler = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(handler);
    int exitValue = -1;
    try {
        exitValue = executor.execute(cli);
    } catch (ExecuteException e) {
        if (watchdog.killedProcess()) {
            throw new RuntimeException("Timeout 30 s exceeded. Commandline " + cli.toString() + " was killed. Output: " + outputStream.toString());
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    if (exitValue == 0) {
        return parseLibraryInfo(outputStream.toString(), javaHome);
    } else {
        throw new RuntimeException(cli.toString() + " process exit code was " + exitValue + ". Output: " + outputStream.toString());
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) LibraryInfo(org.eclipse.tycho.compiler.jdt.copied.LibraryInfo) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ExecuteException(org.apache.commons.exec.ExecuteException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 79 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project frontend-maven-plugin by eirslett.

the class ProcessExecutor method createExecutor.

private Executor createExecutor(File workingDirectory, long timeoutInSeconds) {
    DefaultExecutor executor = new DefaultExecutor();
    executor.setWorkingDirectory(workingDirectory);
    // Fixes #41
    executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    if (timeoutInSeconds > 0) {
        executor.setWatchdog(new ExecuteWatchdog(timeoutInSeconds * 1000));
    }
    return executor;
}
Also used : DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Example 80 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project vespa by vespa-engine.

the class CommandExecutor method writeToOutputStream.

private void writeToOutputStream(ByteArrayOutputStream outputStream, String command) throws IOException {
    CommandLine cmdLine = new CommandLine("/bin/bash");
    cmdLine.addArgument("-c", false);
    cmdLine.addArgument(command, false);
    DefaultExecutor executor = new DefaultExecutor();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(streamHandler);
    executor.execute(cmdLine);
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor)

Aggregations

DefaultExecutor (org.apache.commons.exec.DefaultExecutor)81 CommandLine (org.apache.commons.exec.CommandLine)62 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)47 IOException (java.io.IOException)36 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)33 ExecuteException (org.apache.commons.exec.ExecuteException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)26 File (java.io.File)24 Executor (org.apache.commons.exec.Executor)11 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)9 ShutdownHookProcessDestroyer (org.apache.commons.exec.ShutdownHookProcessDestroyer)9 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 URL (java.net.URL)4 Properties (java.util.Properties)4 LogOutputStream (org.apache.commons.exec.LogOutputStream)4 InputStream (java.io.InputStream)3 PipedInputStream (java.io.PipedInputStream)3 PipedOutputStream (java.io.PipedOutputStream)3 HashSet (java.util.HashSet)3