Search in sources :

Example 1 with ExecuteException

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

the class ExecServiceImpl method auxRun.

private ExecBean auxRun(String program, List<String> args, Map<String, String> env) throws NotAuthorizedException, ExecuteException, IOException {
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValues(null);
    // Setup stdout and stderr
    int nbytes = appConf.getInt(AppConfig.EXEC_MAX_BYTES_NAME, -1);
    ByteArrayOutputStream outStream = new MaxByteArrayOutputStream(nbytes);
    ByteArrayOutputStream errStream = new MaxByteArrayOutputStream(nbytes);
    executor.setStreamHandler(new PumpStreamHandler(outStream, errStream));
    // Only run for N milliseconds
    int timeout = appConf.getInt(AppConfig.EXEC_TIMEOUT_NAME, 0);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    executor.setWatchdog(watchdog);
    CommandLine cmd = makeCommandLine(program, args);
    LOG.info("Running: " + cmd);
    ExecBean res = new ExecBean();
    res.exitcode = executor.execute(cmd, execEnv(env));
    String enc = appConf.get(AppConfig.EXEC_ENCODING_NAME);
    res.stdout = outStream.toString(enc);
    res.stderr = errStream.toString(enc);
    try {
        watchdog.checkException();
    } catch (Exception ex) {
        LOG.error("Command: " + cmd + " failed. res=" + res, ex);
    }
    if (watchdog.killedProcess()) {
        String msg = " was terminated due to timeout(" + timeout + "ms).  See " + AppConfig.EXEC_TIMEOUT_NAME + " property";
        LOG.warn("Command: " + cmd + msg + " res=" + res);
        res.stderr += " Command " + msg;
    }
    if (res.exitcode != 0) {
        LOG.info("Command: " + cmd + " failed. res=" + res);
    }
    return res;
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 2 with ExecuteException

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

the class ShellInterpreter method interpret.

@Override
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
    LOGGER.debug("Run shell command '" + cmd + "'");
    OutputStream outStream = new ByteArrayOutputStream();
    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 {
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(new PumpStreamHandler(contextInterpreter.out, contextInterpreter.out));
        executor.setWatchdog(new ExecuteWatchdog(Long.valueOf(getProperty(TIMEOUT_PROPERTY))));
        executors.put(contextInterpreter.getParagraphId(), executor);
        int exitVal = executor.execute(cmdLine);
        LOGGER.info("Paragraph " + contextInterpreter.getParagraphId() + " return with exit value: " + exitVal);
        return new InterpreterResult(Code.SUCCESS, outStream.toString());
    } catch (ExecuteException e) {
        int exitValue = e.getExitValue();
        LOGGER.error("Can not run " + cmd, e);
        Code code = Code.ERROR;
        String message = outStream.toString();
        if (exitValue == 143) {
            code = Code.INCOMPLETE;
            message += "Paragraph received a SIGTERM\n";
            LOGGER.info("The paragraph " + contextInterpreter.getParagraphId() + " stopped executing: " + message);
        }
        message += "ExitValue: " + exitValue;
        return new InterpreterResult(code, message);
    } catch (IOException e) {
        LOGGER.error("Can not run " + cmd, e);
        return new InterpreterResult(Code.ERROR, e.getMessage());
    } finally {
        executors.remove(contextInterpreter.getParagraphId());
    }
}
Also used : DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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)

Example 3 with ExecuteException

use of org.apache.commons.exec.ExecuteException in project robovm by robovm.

the class AppCompiler method main.

public static void main(String[] args) throws IOException {
    AppCompiler compiler = null;
    Config.Builder builder = null;
    boolean verbose = false;
    boolean run = false;
    boolean archive = false;
    List<Arch> archs = new ArrayList<>();
    String dumpConfigFile = null;
    List<String> runArgs = new ArrayList<String>();
    try {
        builder = new Config.Builder();
        Map<String, PluginArgument> pluginArguments = builder.fetchPluginArguments();
        int i = 0;
        while (i < args.length) {
            if ("-cp".equals(args[i]) || "-classpath".equals(args[i])) {
                for (String p : args[++i].split(File.pathSeparator)) {
                    builder.addClasspathEntry(new File(p));
                }
            } else if ("-bcp".equals(args[i]) || "-bootcp".equals(args[i]) || "-bootclasspath".equals(args[i])) {
                for (String p : args[++i].split(File.pathSeparator)) {
                    builder.addBootClasspathEntry(new File(p));
                }
            } else if ("-jar".equals(args[i])) {
                builder.mainJar(new File(args[++i]));
            } else if ("-o".equals(args[i])) {
                builder.executableName(args[++i]);
            } else if ("-d".equals(args[i])) {
                builder.installDir(new File(args[++i]));
            } else if ("-cache".equals(args[i])) {
                builder.cacheDir(new File(args[++i]));
            } else if ("-home".equals(args[i])) {
                builder.home(new Config.Home(new File(args[++i])));
            } else if ("-tmp".equals(args[i])) {
                builder.tmpDir(new File(args[++i]));
            } else if ("-threads".equals(args[i])) {
                String s = args[++i];
                try {
                    int n = Integer.parseInt(s);
                    // Make sure n > 0 and cap at 128 threads.
                    n = Math.max(n, 1);
                    n = Math.min(n, 128);
                    builder.threads(n);
                } catch (NumberFormatException e) {
                    throw new IllegalArgumentException("Unparsable thread count: " + s);
                }
            } else if ("-run".equals(args[i])) {
                run = true;
            } else if ("-verbose".equals(args[i])) {
                verbose = true;
            } else if ("-config".equals(args[i])) {
                builder.read(new File(args[++i]));
            } else if ("-dumpconfig".equals(args[i])) {
                dumpConfigFile = args[++i];
            } else if ("-properties".equals(args[i])) {
                builder.addProperties(new File(args[++i]));
            } else if (args[i].startsWith("-P")) {
                int index = args[i].indexOf('=');
                if (index <= 0) {
                    throw new IllegalArgumentException("Malformed property: " + args[i]);
                }
                String name = args[i].substring(2, index);
                String value = args[i].substring(index + 1);
                builder.addProperty(name, value);
            } else if ("-debug".equals(args[i])) {
                builder.debug(true);
            } else if ("-use-debug-libs".equals(args[i])) {
                builder.useDebugLibs(true);
            } else if ("-dump-intermediates".equals(args[i])) {
                builder.dumpIntermediates(true);
            } else if ("-dynamic-jni".equals(args[i])) {
            // TODO: Old option not used any longer. We still accept it
            // for now. Delete it in a future release.
            } else if ("-skiprt".equals(args[i])) {
                builder.skipRuntimeLib(true);
            } else if ("-skipsign".equals(args[i])) {
                builder.iosSkipSigning(true);
            } else if ("-clean".equals(args[i])) {
                builder.clean(true);
            } else if ("-help".equals(args[i]) || "-?".equals(args[i])) {
                printUsageAndExit(null, builder.getPlugins());
            } else if ("-version".equals(args[i])) {
                printVersionAndExit();
            } else if ("-cc".equals(args[i])) {
                builder.ccBinPath(new File(args[++i]));
            } else if ("-os".equals(args[i])) {
                String s = args[++i];
                builder.os("auto".equals(s) ? null : OS.valueOf(s));
            } else if ("-arch".equals(args[i])) {
                String s = args[++i];
                if (!"auto".equals(s)) {
                    archs.add(Arch.valueOf(s));
                }
            } else if ("-archs".equals(args[i])) {
                for (String s : args[++i].split(":")) {
                    if (!"auto".equals(s)) {
                        archs.add(Arch.valueOf(s));
                    }
                }
            //                } else if ("-cpu".equals(args[i])) {
            //                    builder.cpu(args[++i]);
            } else if ("-target".equals(args[i])) {
                String s = args[++i];
                builder.targetType("auto".equals(s) ? null : s);
            } else if ("-treeshaker".equals(args[i])) {
                String s = args[++i];
                builder.treeShakerMode(TreeShakerMode.valueOf(s));
            } else if ("-forcelinkclasses".equals(args[i])) {
                for (String p : args[++i].split(":")) {
                    p = p.replace('#', '*');
                    builder.addForceLinkClass(p);
                }
            } else if ("-libs".equals(args[i])) {
                for (String p : args[++i].split(":")) {
                    builder.addLib(new Config.Lib(p, true));
                }
            } else if ("-exportedsymbols".equals(args[i])) {
                for (String p : args[++i].split(":")) {
                    builder.addExportedSymbol(p);
                }
            } else if ("-unhidesymbols".equals(args[i])) {
                for (String p : args[++i].split(":")) {
                    builder.addUnhideSymbol(p);
                }
            } else if ("-frameworks".equals(args[i])) {
                for (String p : args[++i].split(":")) {
                    builder.addFramework(p);
                }
            } else if ("-weakframeworks".equals(args[i])) {
                for (String p : args[++i].split(":")) {
                    builder.addWeakFramework(p);
                }
            } else if ("-resources".equals(args[i])) {
                for (String p : args[++i].split(":")) {
                    if (AntPathMatcher.isPattern(p)) {
                        File dir = new File(AntPathMatcher.rtrimWildcardTokens(p));
                        String pattern = AntPathMatcher.extractPattern(p);
                        builder.addResource(new Resource(dir, null).include(pattern));
                    } else {
                        builder.addResource(new Resource(new File(p)));
                    }
                }
            } else if ("-cacerts".equals(args[i])) {
                String name = args[++i];
                Config.Cacerts cacerts = null;
                if (!"none".equals(name)) {
                    try {
                        cacerts = Config.Cacerts.valueOf(name);
                    } catch (IllegalArgumentException e) {
                        throw new IllegalArgumentException("Illegal -cacerts value: " + name);
                    }
                }
                builder.cacerts(cacerts);
            } else if ("-plist".equals(args[i])) {
                builder.infoPList(new File(args[++i]));
            } else if ("-entitlements".equals(args[i])) {
                builder.iosEntitlementsPList(new File(args[++i]));
            } else if ("-signidentity".equals(args[i])) {
                builder.iosSignIdentity(SigningIdentity.find(SigningIdentity.list(), args[++i]));
            } else if ("-provisioningprofile".equals(args[i])) {
                builder.iosProvisioningProfile(ProvisioningProfile.find(ProvisioningProfile.list(), args[++i]));
            } else if ("-sdk".equals(args[i])) {
                builder.iosSdkVersion(args[++i]);
            } else if ("-printdevicetypes".equals(args[i])) {
                printDeviceTypesAndExit();
            } else if ("-devicetype".equals(args[i])) {
                builder.iosDeviceType(args[++i]);
            } else if ("-archive".equals(args[i])) {
                archive = true;
            } else if ("-createipa".equals(args[i])) {
                archive = true;
            } else if ("-ipaarchs".equals(args[i])) {
                for (String s : args[++i].split(":")) {
                    if (!"auto".equals(s)) {
                        archs.add(Arch.valueOf(s));
                    }
                }
            } else if (args[i].startsWith("-D")) {
            } else if (args[i].startsWith("-X")) {
            } else if (args[i].startsWith("-rvm:")) {
                runArgs.add(args[i]);
            } else if (args[i].startsWith("-")) {
                String argName = args[i].substring(1, args[i].length());
                if (argName.contains("=")) {
                    argName = argName.substring(0, argName.indexOf('='));
                }
                PluginArgument arg = pluginArguments.get(argName);
                if (arg != null) {
                    builder.addPluginArgument(args[i].substring(1));
                } else {
                    throw new IllegalArgumentException("Unrecognized option: " + args[i]);
                }
            } else {
                builder.mainClass(args[i++]);
                break;
            }
            i++;
        }
        builder.archs(archs.toArray(new Arch[archs.size()]));
        while (i < args.length) {
            runArgs.add(args[i++]);
        }
        if (archive && run) {
            throw new IllegalArgumentException("Specify either -run or -createipa/-archive, not both");
        }
        builder.logger(new ConsoleLogger(verbose));
        builder.skipInstall(run);
        if (dumpConfigFile != null) {
            if (dumpConfigFile.equals("-")) {
                builder.write(new OutputStreamWriter(System.out), new File("."));
            } else {
                File file = new File(dumpConfigFile);
                if (file.exists()) {
                    throw new IllegalArgumentException("Cannot dump config to " + file.getAbsolutePath() + ". The file already exists.");
                }
                builder.write(file);
            }
            return;
        }
        compiler = new AppCompiler(builder.build());
    } catch (Throwable t) {
        String message = t.getMessage();
        if (t instanceof ArrayIndexOutOfBoundsException) {
            message = "Missing argument";
        }
        if (t instanceof IndexOutOfBoundsException) {
            message = "Missing argument";
        }
        if (verbose && !(t instanceof StringIndexOutOfBoundsException) && !(t instanceof IllegalArgumentException)) {
            t.printStackTrace();
        }
        printUsageAndExit(message, builder != null ? builder.getPlugins() : null);
    }
    try {
        if (archive) {
            compiler.build();
            compiler.archive();
        } else {
            if (run && !compiler.config.getTarget().canLaunch()) {
                throw new IllegalArgumentException("Cannot launch when building " + compiler.config.getTarget().getType() + " binaries");
            }
            if (run) {
                // Just compile the first slice if multiple archs have been specified
                compiler.compile();
                LaunchParameters launchParameters = compiler.config.getTarget().createLaunchParameters();
                if (launchParameters instanceof IOSSimulatorLaunchParameters) {
                    IOSSimulatorLaunchParameters simParams = (IOSSimulatorLaunchParameters) launchParameters;
                    String deviceName = null;
                    String sdkVersion = null;
                    if (compiler.config.getIosDeviceType() != null) {
                        String[] parts = compiler.config.getIosDeviceType().split("[:;, ]+");
                        deviceName = parts[0].trim();
                        sdkVersion = parts.length > 1 ? parts[1].trim() : null;
                    }
                    DeviceType type = DeviceType.getBestDeviceType(compiler.config.getArch(), null, deviceName, sdkVersion);
                    simParams.setDeviceType(type);
                }
                launchParameters.setArguments(runArgs);
                compiler.launch(launchParameters);
            } else {
                compiler.build();
                compiler.config.getTarget().install();
            }
        }
    } catch (Throwable t) {
        String message = t.getMessage();
        if (verbose && !(t instanceof ExecuteException)) {
            t.printStackTrace();
        }
        printUsageAndExit(message, builder.getPlugins());
    }
}
Also used : Config(org.robovm.compiler.config.Config) IOSSimulatorLaunchParameters(org.robovm.compiler.target.ios.IOSSimulatorLaunchParameters) ArrayList(java.util.ArrayList) ExecuteException(org.apache.commons.exec.ExecuteException) PluginArgument(org.robovm.compiler.plugin.PluginArgument) Resource(org.robovm.compiler.config.Resource) Arch(org.robovm.compiler.config.Arch) DeviceType(org.robovm.compiler.target.ios.DeviceType) ConsoleLogger(org.robovm.compiler.log.ConsoleLogger) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) LaunchParameters(org.robovm.compiler.target.LaunchParameters) IOSSimulatorLaunchParameters(org.robovm.compiler.target.ios.IOSSimulatorLaunchParameters)

Example 4 with ExecuteException

use of org.apache.commons.exec.ExecuteException in project robovm by robovm.

the class AsyncExecutor method executeInternal.

/**
     * Execute an internal process.
     *
     * @param command the command to execute
     * @param environment the execution enviroment
     * @param dir the working directory
     * @param streams process the streams (in, out, err) of the process
     * @return the launched {@link Process}
     * @throws IOException executing the process failed
     */
private Process executeInternal(final CommandLine command, final Map<String, String> environment, final File dir, final ExecuteStreamHandler streams, final ExecuteResultHandler handler) throws IOException {
    final Process process = this.launch(command, environment, dir);
    try {
        streams.setProcessInputStream(process.getOutputStream());
        streams.setProcessOutputStream(process.getInputStream());
        streams.setProcessErrorStream(process.getErrorStream());
    } catch (IOException e) {
        process.destroy();
        throw e;
    }
    streams.start();
    // add the process to the list of those to destroy if the VM exits
    if (this.getProcessDestroyer() != null) {
        this.getProcessDestroyer().add(process);
    }
    if (watchdog != null) {
        watchdog.start(process);
    }
    Thread waitForThread = new Thread(AsyncExecutor.class.getSimpleName() + ".waitFor-" + threadCounter.incrementAndGet()) {

        public void run() {
            int exitValue = Executor.INVALID_EXITVALUE;
            try {
                try {
                    exitValue = process.waitFor();
                } catch (InterruptedException e) {
                    process.destroy();
                }
                if (watchdog != null) {
                    watchdog.stop();
                }
                streams.stop();
                closeStreams(process);
                if (watchdog != null) {
                    try {
                        watchdog.checkException();
                    } catch (Exception e) {
                        throw new IOException(e.getMessage());
                    }
                }
                if (handler != null) {
                    handler.onProcessComplete(exitValue);
                }
            } catch (ExecuteException e) {
                if (handler != null) {
                    handler.onProcessFailed(e);
                }
            } catch (Exception e) {
                if (handler != null) {
                    handler.onProcessFailed(new ExecuteException("Execution failed", exitValue, e));
                }
            } finally {
                // remove the process to the list of those to destroy if the VM exits
                if (AsyncExecutor.this.getProcessDestroyer() != null) {
                    AsyncExecutor.this.getProcessDestroyer().remove(process);
                }
            }
        }
    };
    waitForThread.setDaemon(true);
    waitForThread.start();
    return process;
}
Also used : ExecuteException(org.apache.commons.exec.ExecuteException) IOException(java.io.IOException) IOException(java.io.IOException) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 5 with ExecuteException

use of org.apache.commons.exec.ExecuteException in project robovm by robovm.

the class Executor method execCapture.

public String execCapture() throws IOException {
    ExecuteStreamHandler oldStreamHandler = streamHandler;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CommandLine commandLine = generateCommandLine();
    try {
        streamHandler(new PumpStreamHandler(baos));
        logCommandLine(commandLine);
        DefaultExecutor executor = initExecutor(new DefaultExecutor());
        executor.execute(commandLine, generateEnv());
        return new String(baos.toByteArray()).trim();
    } catch (ExecuteException e) {
        String output = new String(baos.toByteArray()).trim();
        if (output.length() > 0 && e.getMessage().startsWith("Process exited with an error")) {
            StackTraceElement[] origStackTrace = e.getStackTrace();
            e = new ExecuteException("Command '" + commandLine + "' failed with output: " + output + " ", e.getExitValue());
            e.setStackTrace(origStackTrace);
        }
        throw e;
    } finally {
        streamHandler = oldStreamHandler;
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteStreamHandler(org.apache.commons.exec.ExecuteStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ExecuteException (org.apache.commons.exec.ExecuteException)16 CommandLine (org.apache.commons.exec.CommandLine)11 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)11 IOException (java.io.IOException)10 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 File (java.io.File)5 ExecuteResultHandler (org.apache.commons.exec.ExecuteResultHandler)3 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)3 ExecuteStreamHandler (org.apache.commons.exec.ExecuteStreamHandler)2 Executor (org.apache.commons.exec.Executor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ZipFile (java.util.zip.ZipFile)1