Search in sources :

Example 26 with CommandLine

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

the class PythonInterpreter method createGatewayServerAndStartScript.

private void createGatewayServerAndStartScript() throws UnknownHostException {
    createPythonScript();
    if (System.getenv("ZEPPELIN_HOME") != null) {
        py4jLibPath = System.getenv("ZEPPELIN_HOME") + File.separator + ZEPPELIN_PY4JPATH;
        pythonLibPath = System.getenv("ZEPPELIN_HOME") + File.separator + ZEPPELIN_PYTHON_LIBS;
    } else {
        Path workingPath = Paths.get("..").toAbsolutePath();
        py4jLibPath = workingPath + File.separator + ZEPPELIN_PY4JPATH;
        pythonLibPath = workingPath + File.separator + ZEPPELIN_PYTHON_LIBS;
    }
    port = findRandomOpenPortOnAllLocalInterfaces();
    gatewayServer = new GatewayServer(this, port, GatewayServer.DEFAULT_PYTHON_PORT, InetAddress.getByName("0.0.0.0"), InetAddress.getByName("0.0.0.0"), GatewayServer.DEFAULT_CONNECT_TIMEOUT, GatewayServer.DEFAULT_READ_TIMEOUT, (List) null);
    gatewayServer.start();
    // Run python shell
    String pythonCmd = getPythonCommand();
    CommandLine cmd = CommandLine.parse(pythonCmd);
    if (!pythonCmd.endsWith(".py")) {
        // PythonDockerInterpreter set pythoncmd with script
        cmd.addArgument(getScriptPath(), false);
    }
    cmd.addArgument(Integer.toString(port), false);
    cmd.addArgument(getLocalIp(), false);
    executor = new DefaultExecutor();
    outputStream = new InterpreterOutputStream(logger);
    PipedOutputStream ps = new PipedOutputStream();
    in = null;
    try {
        in = new PipedInputStream(ps);
    } catch (IOException e1) {
        throw new InterpreterException(e1);
    }
    ins = new BufferedWriter(new OutputStreamWriter(ps));
    input = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, outputStream, in);
    executor.setStreamHandler(streamHandler);
    executor.setWatchdog(new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT));
    try {
        Map env = EnvironmentUtils.getProcEnvironment();
        if (!env.containsKey("PYTHONPATH")) {
            env.put("PYTHONPATH", py4jLibPath + File.pathSeparator + pythonLibPath);
        } else {
            env.put("PYTHONPATH", env.get("PYTHONPATH") + File.pathSeparator + py4jLibPath + File.pathSeparator + pythonLibPath);
        }
        logger.info("cmd = {}", cmd.toString());
        executor.execute(cmd, env, this);
        pythonscriptRunning = true;
    } catch (IOException e) {
        throw new InterpreterException(e);
    }
    try {
        input.write("import sys, getopt\n".getBytes());
        ins.flush();
    } catch (IOException e) {
        throw new InterpreterException(e);
    }
}
Also used : Path(java.nio.file.Path) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) InterpreterOutputStream(org.apache.zeppelin.interpreter.util.InterpreterOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedWriter(java.io.BufferedWriter) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) List(java.util.List) OutputStreamWriter(java.io.OutputStreamWriter) GatewayServer(py4j.GatewayServer) Map(java.util.Map)

Example 27 with CommandLine

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

the class ShellSecurityImpl method createSecureConfiguration.

public static void createSecureConfiguration(Properties properties, String shell) {
    String authType = properties.getProperty("zeppelin.shell.auth.type").trim().toUpperCase();
    switch(authType) {
        case "KERBEROS":
            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 {
                int exitVal = 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) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException)

Example 28 with CommandLine

use of org.apache.commons.exec.CommandLine in project hive by apache.

the class ExecServiceImpl method makeCommandLine.

private CommandLine makeCommandLine(String program, List<String> args) throws NotAuthorizedException, IOException {
    String path = validateProgram(program);
    CommandLine cmd = new CommandLine(path);
    if (args != null)
        for (String arg : args) cmd.addArgument(arg, false);
    return cmd;
}
Also used : CommandLine(org.apache.commons.exec.CommandLine)

Example 29 with CommandLine

use of org.apache.commons.exec.CommandLine in project elastic-job by dangdangdotcom.

the class ScriptJobExecutor method executeScript.

private void executeScript(final ShardingContext shardingContext, final String scriptCommandLine) {
    CommandLine commandLine = CommandLine.parse(scriptCommandLine);
    commandLine.addArgument(GsonFactory.getGson().toJson(shardingContext), false);
    try {
        new DefaultExecutor().execute(commandLine);
    } catch (final IOException ex) {
        throw new JobConfigurationException("Execute script failure.", ex);
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) JobConfigurationException(com.dangdang.ddframe.job.exception.JobConfigurationException) IOException(java.io.IOException)

Example 30 with CommandLine

use of org.apache.commons.exec.CommandLine in project storm by apache.

the class Utils method execCommand.

public static int execCommand(String... command) throws ExecuteException, IOException {
    CommandLine cmd = new CommandLine(command[0]);
    for (int i = 1; i < command.length; i++) {
        cmd.addArgument(command[i]);
    }
    DefaultExecutor exec = new DefaultExecutor();
    return exec.execute(cmd);
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) DefaultExecutor(org.apache.commons.exec.DefaultExecutor)

Aggregations

CommandLine (org.apache.commons.exec.CommandLine)44 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)26 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)20 IOException (java.io.IOException)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 ExecuteException (org.apache.commons.exec.ExecuteException)12 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)11 File (java.io.File)8 PipedInputStream (java.io.PipedInputStream)4 PipedOutputStream (java.io.PipedOutputStream)4 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)4 ExecuteResultHandler (org.apache.commons.exec.ExecuteResultHandler)4 InputStream (java.io.InputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 Map (java.util.Map)3 Executor (org.apache.commons.exec.Executor)3 BufferedWriter (java.io.BufferedWriter)2 DataInputStream (java.io.DataInputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2