Search in sources :

Example 11 with Executor

use of org.apache.commons.exec.Executor in project project-build-plugin by axonivy.

the class TestStartEngine method canStartEngine.

@Test
public void canStartEngine() throws Exception {
    StartTestEngineMojo mojo = rule.getMojo();
    assertThat(getProperty(EngineControl.Property.TEST_ENGINE_URL)).isNull();
    assertThat(getProperty(EngineControl.Property.TEST_ENGINE_LOG)).isNull();
    Executor startedProcess = null;
    try {
        startedProcess = mojo.startEngine();
        assertThat(getProperty(EngineControl.Property.TEST_ENGINE_URL)).startsWith("http://").endsWith("/");
        assertThat(new File(getProperty(EngineControl.Property.TEST_ENGINE_LOG))).exists();
    } finally {
        kill(startedProcess);
    }
}
Also used : Executor(org.apache.commons.exec.Executor) File(java.io.File) BaseEngineProjectMojoTest(ch.ivyteam.ivy.maven.BaseEngineProjectMojoTest) Test(org.junit.Test)

Example 12 with Executor

use of org.apache.commons.exec.Executor in project kylo by Teradata.

the class MultiUserProcessManager method refreshKerberosTicket.

/**
 * Calls kinit to request a new Kerberos ticket if the previous one is about to expire.
 */
private void refreshKerberosTicket() {
    // Determine if a new ticket is needed
    if (kerberos == null || kerberos.getInitInterval() <= 0 || kerberosNextInit > DateTimeUtils.currentTimeMillis()) {
        return;
    }
    // Build executor
    final Executor executor = new DefaultExecutor();
    final ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer();
    executor.setProcessDestroyer(processDestroyer);
    final Logger outputLogger = LoggerFactory.getLogger(getClass().getName() + ".kinit");
    final LoggerOutputStream outputStream = new LoggerOutputStream(outputLogger);
    final PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(streamHandler);
    final ExecuteWatchdog watchdog = new ExecuteWatchdog(TimeUnit.SECONDS.toMillis(kerberos.getInitTimeout()));
    executor.setWatchdog(watchdog);
    // Run kinit to acquire a new ticket
    final CommandLine command = new CommandLine("kinit").addArgument("-kt").addArgument(kerberos.getKeytabLocation()).addArgument(kerberos.getKerberosPrincipal());
    log.debug("Acquiring a new Kerberos ticket with command: {}", command);
    int exitCode;
    try {
        exitCode = executor.execute(command);
    } catch (final IOException e) {
        log.error("Failed to execute kinit", e);
        exitCode = -1;
    }
    // Record next time to acquire ticket
    if (!executor.isFailure(exitCode)) {
        kerberosNextInit = DateTimeUtils.currentTimeMillis() + TimeUnit.SECONDS.toMillis(kerberos.getInitInterval());
    } else {
        if (watchdog.killedProcess()) {
            log.error("Failed to acquire a Kerberos ticket within the allotted time: {}", kerberos.getInitTimeout());
        } else {
            log.error("Kinit exited with non-zero status: {}", exitCode);
        }
        kerberosNextInit = DateTimeUtils.currentTimeMillis() + TimeUnit.SECONDS.toMillis(kerberos.getRetryInterval());
        throw new IllegalStateException("Failed to acquire a Kerberos ticket");
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) IOException(java.io.IOException) Logger(org.slf4j.Logger) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Example 13 with Executor

use of org.apache.commons.exec.Executor in project openhab1-addons by openhab.

the class ExecBinding method executeCommandAndWaitResponse.

/**
     * <p>
     * Executes <code>commandLine</code>. Sometimes (especially observed on
     * MacOS) the commandLine isn't executed properly. In that cases another
     * exec-method is to be used. To accomplish this please use the special
     * delimiter '<code>@@</code>'. If <code>commandLine</code> contains this
     * delimiter it is split into a String[] array and the special exec-method
     * is used.
     * </p>
     * <p>
     * A possible {@link IOException} gets logged but no further processing is
     * done.
     * </p>
     *
     * @param commandLine the command line to execute
     * @return response data from executed command line
     */
private String executeCommandAndWaitResponse(String commandLine) {
    String retval = null;
    CommandLine cmdLine = null;
    if (commandLine.contains(CMD_LINE_DELIMITER)) {
        String[] cmdArray = commandLine.split(CMD_LINE_DELIMITER);
        cmdLine = new CommandLine(cmdArray[0]);
        for (int i = 1; i < cmdArray.length; i++) {
            cmdLine.addArgument(cmdArray[i], false);
        }
    } else {
        cmdLine = CommandLine.parse(commandLine);
    }
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    Executor executor = new DefaultExecutor();
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(stdout);
    executor.setExitValue(1);
    executor.setStreamHandler(streamHandler);
    executor.setWatchdog(watchdog);
    try {
        executor.execute(cmdLine, resultHandler);
        logger.debug("executed commandLine '{}'", commandLine);
    } catch (ExecuteException e) {
        logger.error("couldn't execute commandLine '" + commandLine + "'", e);
    } catch (IOException e) {
        logger.error("couldn't execute commandLine '" + commandLine + "'", e);
    }
    // can safely request the exit code
    try {
        resultHandler.waitFor();
        int exitCode = resultHandler.getExitValue();
        retval = StringUtils.chomp(stdout.toString());
        logger.debug("exit code '{}', result '{}'", exitCode, retval);
    } catch (InterruptedException e) {
        logger.error("Timeout occured when executing commandLine '" + commandLine + "'", e);
    }
    return retval;
}
Also used : DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CommandLine(org.apache.commons.exec.CommandLine) Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 14 with Executor

use of org.apache.commons.exec.Executor in project project-build-plugin by axonivy.

the class TestDeployToRunningEngine method canDeployIar.

@Test
public void canDeployIar() throws Exception {
    StartTestEngineMojo mojo = rule.getMojo();
    DeployToEngineMojo deployMojo = deployRule.getMojo();
    deployMojo.deployTimeoutInSeconds = 120;
    deployMojo.deployEngineDirectory = mojo.engineDirectory.getAbsoluteFile();
    deployMojo.deployFile = new File("src/test/resources/deploy-single-7.1.0-SNAPSHOT.iar");
    deployMojo.deployTestUsers = true;
    deployMojo.deployToEngineApplication = "Portal";
    File deployedIar = getTarget(deployMojo.deployFile, deployMojo);
    File deployedIarFlagFile = new File(deployedIar.getParent(), deployedIar.getName() + ".deployed");
    File deployedIarLogFile = new File(deployedIar.getParent(), deployedIar.getName() + ".deploymentLog");
    Executor startedProcess = null;
    try {
        startedProcess = mojo.startEngine();
        deployMojo.execute();
        assertThat(deployedIar).doesNotExist();
        assertThat(deployedIarFlagFile).exists();
        assertThat(deployedIarLogFile).exists();
        assertThat(linesOf(deployedIarLogFile)).haveAtLeast(1, new Condition<>(s -> s.contains("Deploying users ..."), ""));
    } finally {
        kill(startedProcess);
    }
}
Also used : Executor(org.apache.commons.exec.Executor) Rule(org.junit.Rule) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Condition(org.assertj.core.api.Condition) Test(org.junit.Test) Assertions.linesOf(org.assertj.core.api.Assertions.linesOf) File(java.io.File) Executor(org.apache.commons.exec.Executor) File(java.io.File) Test(org.junit.Test)

Example 15 with Executor

use of org.apache.commons.exec.Executor in project project-build-plugin by axonivy.

the class TestStartEngine method engineStartCanFailFast.

@Test
@Ignore
public void engineStartCanFailFast() throws Exception {
    StartTestEngineMojo mojo = rule.getMojo();
    File engineDir = installUpToDateEngineRule.getMojo().getRawEngineDirectory();
    File configDir = new File(engineDir, "configuration");
    File tmpConfigDir = new File(engineDir, "config.bkp");
    configDir.renameTo(tmpConfigDir);
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    Executor startedProcess = null;
    try {
        startedProcess = mojo.startEngine();
        fail("Engine start should fail as no configuration directory exists.");
    } catch (RuntimeException ex) {
        stopWatch.stop();
        long seconds = TimeUnit.SECONDS.convert(stopWatch.getTime(), TimeUnit.MILLISECONDS);
        assertThat(seconds).describedAs("engine start should fail early if engine config is incomplete").isLessThanOrEqualTo(20);
    } finally {
        kill(startedProcess);
        FileUtils.deleteDirectory(configDir);
        tmpConfigDir.renameTo(configDir);
    }
}
Also used : Executor(org.apache.commons.exec.Executor) File(java.io.File) StopWatch(org.apache.commons.lang3.time.StopWatch) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Executor (org.apache.commons.exec.Executor)27 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)16 File (java.io.File)12 CommandLine (org.apache.commons.exec.CommandLine)10 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)10 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)10 Test (org.junit.Test)10 IOException (java.io.IOException)8 BaseEngineProjectMojoTest (ch.ivyteam.ivy.maven.BaseEngineProjectMojoTest)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)6 ExecuteException (org.apache.commons.exec.ExecuteException)6 ShutdownHookProcessDestroyer (org.apache.commons.exec.ShutdownHookProcessDestroyer)6 StartTestEngineMojo (ch.ivyteam.ivy.maven.test.StartTestEngineMojo)2 OutputStream (java.io.OutputStream)2 PrintStream (java.io.PrintStream)2 ExecuteResultHandler (org.apache.commons.exec.ExecuteResultHandler)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.linesOf (org.assertj.core.api.Assertions.linesOf)2