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);
}
}
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");
}
}
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;
}
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);
}
}
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);
}
}
Aggregations