Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.commons.lang3.StringUtils;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please refer com.cloud.utils.StringUtils, and add a proxy method there if you need one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DaanHoogland as discussed in the review #4702 (review), it will not be done.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thois is bad


public final class ProcessRunner {
public static final Logger LOG = Logger.getLogger(ProcessRunner.class);
Expand Down Expand Up @@ -73,43 +74,44 @@ public ProcessResult executeCommands(final List<String> commands, final Duration
String stdOutput = null;
String stdError = null;

String oneLineCommand = StringUtils.join(commands, " ");

try {
LOG.debug(String.format("Preparing command [%s] to execute.", oneLineCommand));
final Process process = new ProcessBuilder().command(commands).start();

LOG.debug(String.format("Submitting command [%s].", oneLineCommand));
final Future<Integer> processFuture = executor.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return process.waitFor();
}
});
try {
LOG.debug(String.format("Waiting for a response from command [%s]. Defined timeout: [%s].", oneLineCommand, timeOut.getStandardSeconds()));
retVal = processFuture.get(timeOut.getStandardSeconds(), TimeUnit.SECONDS);
} catch (ExecutionException e) {
LOG.warn(String.format("Failed to complete the requested command [%s] due to execution error.", oneLineCommand), e);
retVal = -2;
stdError = e.getMessage();
if (LOG.isTraceEnabled()) {
LOG.trace("Failed to complete the requested command due to execution error: " + e.getMessage());
}
} catch (TimeoutException e) {
LOG.warn(String.format("Failed to complete the requested command [%s] within timeout. Defined timeout: [%s].", oneLineCommand, timeOut.getStandardSeconds()), e);
retVal = -1;
stdError = "Operation timed out, aborted";
if (LOG.isTraceEnabled()) {
LOG.trace("Failed to complete the requested command within timeout: " + e.getMessage());
}
stdError = "Operation timed out, aborted.";
} finally {
if (Strings.isNullOrEmpty(stdError)) {
stdOutput = CharStreams.toString(new InputStreamReader(process.getInputStream()));
stdError = CharStreams.toString(new InputStreamReader(process.getErrorStream()));
}
process.destroy();
}
if (LOG.isTraceEnabled()) {
LOG.trace("Process standard output: " + stdOutput);
LOG.trace("Process standard error output: " + stdError);
}

LOG.debug(String.format("Process standard output for command [%s]: [%s].", oneLineCommand, stdOutput));
LOG.debug(String.format("Process standard error output command [%s]: [%s].", oneLineCommand, stdError));
} catch (IOException | InterruptedException e) {
LOG.error(String.format("Exception caught error running command [%s].", oneLineCommand), e);
stdError = e.getMessage();
LOG.error("Exception caught error running commands: " + e.getMessage());
}
return new ProcessResult(stdOutput, stdError, retVal);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public void testProcessRunnerWithTimeout() {
ProcessResult result = RUNNER.executeCommands(Arrays.asList("sleep", "5"), Duration.standardSeconds(1));
Assert.assertNotEquals(result.getReturnCode(), 0);
Assert.assertTrue(result.getStdError().length() > 0);
Assert.assertEquals(result.getStdError(), "Operation timed out, aborted");
Assert.assertEquals(result.getStdError(), "Operation timed out, aborted.");
}

@Test
public void testProcessRunnerWithTimeoutAndException() {
ProcessResult result = RUNNER.executeCommands(Arrays.asList("ls", "/some/dir/that/should/not/exist"), Duration.standardSeconds(2));
Assert.assertNotEquals(result.getReturnCode(), 0);
Assert.assertTrue(result.getStdError().length() > 0);
Assert.assertNotEquals(result.getStdError(), "Operation timed out, aborted");
Assert.assertNotEquals(result.getStdError(), "Operation timed out, aborted.");
}

@Test(expected = IllegalArgumentException.class)
Expand Down