use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command in project kas-fleetshard by bf2fc6cc711aee1a0c2a.
the class Exec method exec.
/**
* Method executes external command
*
* @param command arguments for command
* @param envVars
* @param timeout timeout for execution
* @param logToOutput log output or not
* @param throwErrors look for errors in output and throws exception if true
* @return execution results
*/
public static ExecResult exec(String input, List<String> command, Set<EnvVar> envVars, int timeout, boolean logToOutput, boolean throwErrors) {
int ret = 1;
ExecResult execResult;
try {
Exec executor = new Exec();
LOGGER.info("Command: {}", String.join(" ", command));
ret = executor.execute(input, command, envVars, timeout);
synchronized (LOCK) {
if (logToOutput) {
LOGGER.info("RETURN code: {}", ret);
if (!executor.out().isEmpty()) {
LOGGER.info("======STDOUT START=======");
LOGGER.info("{}", cutExecutorLog(executor.out()));
LOGGER.info("======STDOUT END======");
}
if (!executor.err().isEmpty()) {
LOGGER.info("======STDERR START=======");
LOGGER.info("{}", cutExecutorLog(executor.err()));
LOGGER.info("======STDERR END======");
}
}
}
execResult = new ExecResult(ret, executor.out(), executor.err());
if (throwErrors && ret != 0) {
String msg = "`" + join(" ", command) + "` got status code " + ret + " and stderr:\n------\n" + executor.stdErr + "\n------\nand stdout:\n------\n" + executor.stdOut + "\n------";
Matcher matcher = ERROR_PATTERN.matcher(executor.err());
KubeClusterException t = null;
if (matcher.find()) {
switch(matcher.group(1)) {
case "NotFound":
t = new KubeClusterException.NotFound(execResult, msg);
break;
case "AlreadyExists":
t = new KubeClusterException.AlreadyExists(execResult, msg);
break;
default:
break;
}
}
matcher = INVALID_PATTERN.matcher(executor.err());
if (matcher.find()) {
t = new KubeClusterException.InvalidResource(execResult, msg);
}
if (t == null) {
t = new KubeClusterException(execResult, msg);
}
throw t;
}
return new ExecResult(ret, executor.out(), executor.err());
} catch (IOException | ExecutionException e) {
throw new KubeClusterException(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new KubeClusterException(e);
}
}
use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class DataCollectingWorker method execute.
@Override
public void execute(Task aTask, WorkerContext ctl) {
boolean finished = false;
try {
TestTask task = (TestTask) aTask;
Command command = task.getCommands().peekFirst();
if (command != null && command.done()) {
task.getCommands().removeFirst();
command = task.getCommands().peekFirst();
}
if (command != null) {
command.execute(ctl, task);
} else {
// Do not record stop caused by end of commands
finished = true;
ctl.stop();
}
data.recordSuccess();
} catch (RetryExecutionControlException ex) {
if (ex.isForce())
data.recordForceRetry();
else
data.recordRetry();
throw ex;
} catch (StopExecutionControlException ex) {
if (!finished)
data.recordStop();
throw ex;
} catch (Exception ex) {
data.recordException();
throw ex;
} finally {
if (!finished)
data.recordExecution();
}
}
use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command in project kas-fleetshard by bf2fc6cc711aee1a0c2a.
the class SuiteUnitTest method testExecutorError.
@SequentialTest
void testExecutorError() {
ExecBuilder command = Exec.builder().withCommand("ppppeeeepppaaa", "jenda").logToOutput(false).throwErrors(true).timeout(60);
assertThrows(KubeClusterException.class, command::exec);
}
use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command in project kas-fleetshard by bf2fc6cc711aee1a0c2a.
the class BaseCmdKubeClient method process.
@Override
@SuppressWarnings("unchecked")
public K process(Map<String, String> parameters, String file, Consumer<String> c) {
List<String> command = command(asList(PROCESS, "-f", file), false);
command.addAll(parameters.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.toList()));
ExecResult exec = Exec.builder().throwErrors(true).withCommand(command).exec();
c.accept(exec.out());
return (K) this;
}
use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class DataCollectingWorker method finallyExecute.
@Override
public void finallyExecute(Task aTask, WorkerContext ctl, Optional<Exception> error) {
TestTask task = (TestTask) aTask;
try {
Command command = task.getFinalCommand();
if (command != null)
command.execute(ctl, task);
data.recordFinallyExecuteSuccess();
} finally {
data.recordCounter(task.getCounter());
data.recordFinallyExecuteAttempt();
data.signalFinished();
}
}
Aggregations