/** * Runs docker command asynchronously. * * @param arguments command line arguments * @param out stdout will be written in to this file * * @return a handle used to stop the command process * * @throws IOException when command has error */ public ExecuteWatchdog dockerAsync(final List<Object> arguments, OutputStream out) throws IOException { CommandLine cmdLine = new CommandLine(DOCKER); arguments.forEach((arg) -> { cmdLine.addArgument(arg + ""); }); LOG.debug("[{} {}]", cmdLine.getExecutable(), StringUtils.join(cmdLine.getArguments(), " ")); List<String> output = new ArrayList<>(); ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); Executor executor = new DefaultExecutor(); executor.setWatchdog(watchdog); PrintWriter writer = new PrintWriter(out); ESH esh = new ESH(writer); executor.setStreamHandler(esh); executor.execute(cmdLine, new DefaultExecuteResultHandler()); return watchdog; }
@BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("Java Temp Dir: " +System.getProperty("java.io.tmpdir")); executor = new DefaultExecutor(); resultHandler = new DefaultExecuteResultHandler(); String javaHome = System.getProperty("java.home"); String userDir = System.getProperty("user.dir"); executor.setStreamHandler(new PumpStreamHandler(System.out)); watchdog = new ExecuteWatchdog(10000); executor.setWatchdog(watchdog); executor.execute(new CommandLine(javaHome + SystemUtils.FILE_SEPARATOR + "bin"+ SystemUtils.FILE_SEPARATOR+"java.exe").addArgument("-version")); executor.execute(new CommandLine(javaHome + SystemUtils.FILE_SEPARATOR + "bin"+ SystemUtils.FILE_SEPARATOR+"java.exe") .addArgument("-jar") .addArgument(userDir + "/../moneta-dropwizard/target/moneta-dropwizard-" + ContractTestSuite.getProjectVersion() + ".jar") .addArgument("server") .addArgument("src/main/resources/dropwizard/moneta-dropwizard.yaml"), resultHandler); Thread.sleep(3000); System.out.println("Test sequence starting...."); }
public void run() { try { executor = new DaemonExecutor(); resultHandler = new DefaultExecuteResultHandler(); String javaHome = System.getProperty("java.home"); String userDir = System.getProperty("user.dir"); executor.setStreamHandler(new PumpStreamHandler(System.out)); watchdog = new ExecuteWatchdog(15000); executor.setWatchdog(watchdog); executor.execute(new CommandLine(javaHome + SystemUtils.FILE_SEPARATOR + "bin"+ SystemUtils.FILE_SEPARATOR+"java.exe").addArgument("-version")); executor.execute(new CommandLine(javaHome + SystemUtils.FILE_SEPARATOR + "bin"+ SystemUtils.FILE_SEPARATOR+"java.exe") .addArgument("-jar") .addArgument(userDir + "/../moneta-springboot/target/moneta-springboot-" + ContractTestSuite.getProjectVersion() + ".jar")); } catch (Exception e) { e.printStackTrace(); } }
@SuppressWarnings("rawtypes") public static void execAsync(String display, CommandLine commandline) throws ShellCommandException { log.debug("executing async command: " + commandline); DefaultExecutor exec = new DefaultExecutor(); ExecuteResultHandler handler = new DefaultExecuteResultHandler(); PumpStreamHandler streamHandler = new PumpStreamHandler( new PritingLogOutputStream()); exec.setStreamHandler(streamHandler); try { if (display == null || display.isEmpty()) { exec.execute(commandline, handler); } else { Map env = EnvironmentUtils.getProcEnvironment(); EnvironmentUtils.addVariableToEnvironment(env, "DISPLAY=:" + display); exec.execute(commandline, env, handler); } } catch (Exception e) { throw new ShellCommandException( "An error occured while executing shell command: " + commandline, e); } }
public static boolean testExecutable() { CommandLine commandLine = CommandLine.parse(PythonCLIProcessor.pythonExecutable + " --version"); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); Executor executor = new DefaultExecutor(); // put a watchdog with a timeout ExecuteWatchdog watchdog = new ExecuteWatchdog(new Long(timeout_seconds) * 1000); executor.setWatchdog(watchdog); try { executor.execute(commandLine, resultHandler); resultHandler.waitFor(); int exitVal = resultHandler.getExitValue(); if (exitVal != 0) { return false; } return true; } catch (Exception e) { return false; } }
private static boolean testExecutable() { CommandLine commandLine = CommandLine.parse(RCLIProcessor.rExecutable + " " + VERSION_CALL); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); Executor executor = new DefaultExecutor(); // put a watchdog with a timeout ExecuteWatchdog watchdog = new ExecuteWatchdog(new Long(TIMEOUT_SECONDS) * 1000); executor.setWatchdog(watchdog); try { executor.execute(commandLine, resultHandler); resultHandler.waitFor(); int exitVal = resultHandler.getExitValue(); if (exitVal != 0) { return false; } return true; } catch (Exception e) { return false; } }
@Override protected final void executeGoal() throws MojoExecutionException { init(); LOG.info("command={}", command); LOG.info("arguments={}", Arrays.toString(arguments)); final CommandLine cmdLine = createCommandLine(); final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); final DaemonExecutor executor = new DaemonExecutor(); try { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final PumpStreamHandler psh = new PumpStreamHandler(bos); executor.setStreamHandler(psh); executor.setWorkingDirectory(getEventStoreDir()); executor.execute(cmdLine, resultHandler); final List<String> messages = waitForHttpServer(resultHandler, bos); logDebug(messages); final String pid = extractPid(messages); LOG.info("Event store process ID: {}", pid); writePid(pid); } catch (final IOException ex) { throw new MojoExecutionException( "Error executing the command line: " + cmdLine, ex); } }
public static void learn(String path, String factsForLearningFile, String rulesDefinitionFile, String learnedWeightsFile) throws Exception { CommandLine cmdLine = new CommandLine(path + "/learnwts"); cmdLine.addArgument("-i"); cmdLine.addArgument(factsForLearningFile); cmdLine.addArgument("-o"); cmdLine.addArgument(rulesDefinitionFile); cmdLine.addArgument("-t"); cmdLine.addArgument(learnedWeightsFile); cmdLine.addArgument("-g -multipleDatabases"); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 1000); Executor executor = new DefaultExecutor(); executor.setExitValue(1); executor.setWatchdog(watchdog); executor.execute(cmdLine, resultHandler); resultHandler.waitFor(); }
private void startAppiumServer() { try { // Kill any node servers that may be running stopAppiumServer(); log.info("START APPIUM"); CommandLine command = new CommandLine(ConfigurationManager.getInstance().getMobileAppiumNodeServerCommandLine()); for (String argument : ConfigurationManager.getInstance().getMobileAppiumNodeServerArguments()) { command.addArgument(argument); } DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(1); executor.execute(command, resultHandler); Thread.sleep(10000); log.info("APPIUM server started successfully."); } catch (IOException e) { log.error("Appium Server Failed to start.", e); } catch (InterruptedException ie) { log.error("Appium Server wait failed.", ie); } }
private Future<CommandResult> executeCommand(String command, File executionDirectory, boolean silent) throws IOException { StringWriter writer = new StringWriter(); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); try (WriterOutputStream outputStream = new WriterOutputStream(writer)) { String outerCommand = "/bin/bash -lc"; CommandLine outer = CommandLine.parse(outerCommand); outer.addArgument(command, false); DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(executionDirectory); executor.setStreamHandler(new PumpStreamHandler(silent ? outputStream : System.out, null)); executor.execute(outer, ENVIRONMENT, resultHandler); resultHandler.waitFor(); } catch (InterruptedException e) { throw new IllegalStateException(e); } return new AsyncResult<CommandResult>( new CommandResult(resultHandler.getExitValue(), writer.toString(), resultHandler.getException())); }
/** * Execute a command on the operating system using Apache Commons Exec. This * function runs asynchronously and dumps both stderr and stdout streams to * a temp file. * * @param commandLine The command to be executed. * @param outputStreamHandler An output stream to dump the process stderr * and stdout to it. */ public static void executeCommandUsingApacheExec(CommandLine commandLine, OutputStream outputStreamHandler) { try { DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStreamHandler); Executor process = new DefaultExecutor(); process.setExitValue(0); process.setStreamHandler(streamHandler); process.execute(commandLine, resultHandler); } catch (Exception ex) { LOGGER.log(Level.SEVERE, "An exception was thrown.", ex); } }
public void start() throws Exception { CommandLine cmdLine = new CommandLine(p4d); // cmdLine.addArgument("-vserver=5"); cmdLine.addArgument("-C1"); cmdLine.addArgument("-r"); cmdLine.addArgument(formatPath(p4root.getAbsolutePath())); cmdLine.addArgument("-p"); cmdLine.addArgument(p4port); cmdLine.addArgument("-Llog"); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); DefaultExecutor executor = new DefaultExecutor(); executor.execute(cmdLine, resultHandler); logger.info("start signal sent..."); P4PasswordImpl auth = new P4PasswordImpl(CredentialsScope.SYSTEM, "id", "desc", p4port, null, "admin", "0", "Password"); int retry = 0; while (retry < 20) { try { p4 = new ConnectionHelper(auth); break; } catch (Exception e) { Thread.sleep(100); } } }
public static String getVersion() { try { URL scriptURL = PythonCLIProbe.class.getResource(versionScriptFile); File sf = new File(scriptURL.toURI()); String scriptPath = sf.getAbsolutePath(); CommandLine commandLine = CommandLine.parse(PythonCLIProcessor.pythonExecutable + " " + scriptPath); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); Executor executor = new DefaultExecutor(); // put a watchdog with a timeout ExecuteWatchdog watchdog = new ExecuteWatchdog(new Long(timeout_seconds) * 1000); executor.setWatchdog(watchdog); ByteArrayOutputStream os = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(os); executor.setStreamHandler(psh); executor.execute(commandLine, resultHandler); resultHandler.waitFor(); int exitVal = resultHandler.getExitValue(); if (exitVal != 0) { return null; } return (os.toString()); } catch (Exception e) { return null; } }
private static String getVersion() { try { CommandLine commandLine = CommandLine.parse(RCLIProcessor.rExecutable + " " + VERSION_CALL); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); Executor executor = new DefaultExecutor(); // put a watchdog with a timeout ExecuteWatchdog watchdog = new ExecuteWatchdog(new Long(TIMEOUT_SECONDS) * 1000); executor.setWatchdog(watchdog); ByteArrayOutputStream os = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(os); executor.setStreamHandler(psh); executor.execute(commandLine, resultHandler); resultHandler.waitFor(); int exitVal = resultHandler.getExitValue(); if (exitVal != 0) { return null; } String osString = os.toString(); String versionString = osString.substring(osString.lastIndexOf(": ") + 2); versionString = versionString.substring(0, versionString.indexOf('\n')); return (versionString); } catch (Exception e) { LOGGER.error("Could not get version string.", e); return null; } }
private List<String> waitForHttpServer( final DefaultExecuteResultHandler resultHandler, final ByteArrayOutputStream bos) throws MojoExecutionException { // Wait for result int wait = 0; while ((wait++ < maxWaitCycles) && !resultHandler.hasResult() && !bos.toString().contains(upMessage)) { sleep(sleepMs); } if (bos.toString().contains(upMessage)) { // Success return asList(bos.toString()); } // Failure final List<String> messages = asList(bos.toString()); logError(messages); // Exception if (resultHandler.hasResult()) { throw new MojoExecutionException( "Error starting the server. Exit code=" + resultHandler.getExitValue(), resultHandler.getException()); } // Timeout throw new MojoExecutionException( "Waited too long for the server to start!"); }
public static void startNewInstance () { try { CommandLine cmdLine = CommandLine.parse(getRestartCommand()); DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(null, null, null)); executor.execute(cmdLine, new DefaultExecuteResultHandler()); } catch (Exception e) { Log.exception(e); } }
/** * Using apache commons exec for cmd line execution * * @param command * @return exitCode * @throws ExecuteException * @throws IOException * @throws InterruptedException */ private void startEmulator() throws ExecuteException, IOException, InterruptedException { CommandLine cmdLine = new CommandLine( sdkDir + "/" + GCLOUD_EXE); cmdLine.addArgument("beta"); cmdLine.addArgument("emulators"); cmdLine.addArgument("bigtable"); cmdLine.addArgument("start"); cmdLine.addArgument("--quiet"); cmdLine.addArgument("--host-port"); cmdLine.addArgument(HOST_PORT); // Using a result handler makes the emulator run async DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); // watchdog shuts down the emulator, later watchdog = new ExecuteWatchdog( ExecuteWatchdog.INFINITE_TIMEOUT); Executor executor = new DefaultExecutor(); executor.setWatchdog(watchdog); executor.execute( cmdLine, resultHandler); // we need to wait here for a bit, in case the emulator needs to update // itself Thread.sleep(EMULATOR_SPINUP_DELAY_MS); }
public static DefaultExecuteResultHandler executeBash(String command) throws IOException { CommandLine cmdLine = CommandLine.parse("bash"); cmdLine.addArgument("-c", false); cmdLine.addArgument(command, false); DefaultExecutor executor = new DefaultExecutor(); ByteArrayOutputStream outputStreamAgentStart = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(outputStreamAgentStart)); DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler(); executor.execute(cmdLine, handler); return handler; }