diff --git a/utils/pom.xml b/utils/pom.xml index 1ec0dbec30c..8f42567762d 100755 --- a/utils/pom.xml +++ b/utils/pom.xml @@ -184,6 +184,10 @@ commons-compress ${cs.commons-compress.version} + + org.apache.commons + commons-lang3 + diff --git a/utils/src/main/java/org/apache/cloudstack/utils/process/ProcessRunner.java b/utils/src/main/java/org/apache/cloudstack/utils/process/ProcessRunner.java index c8ea6bf59a4..7bb2164210d 100644 --- a/utils/src/main/java/org/apache/cloudstack/utils/process/ProcessRunner.java +++ b/utils/src/main/java/org/apache/cloudstack/utils/process/ProcessRunner.java @@ -34,6 +34,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import org.apache.commons.lang3.StringUtils; public final class ProcessRunner { public static final Logger LOG = Logger.getLogger(ProcessRunner.class); @@ -73,8 +74,13 @@ public final class ProcessRunner { 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 processFuture = executor.submit(new Callable() { @Override public Integer call() throws Exception { @@ -82,19 +88,16 @@ public final class ProcessRunner { } }); 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())); @@ -102,14 +105,13 @@ public final class ProcessRunner { } 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); } -} \ No newline at end of file +} diff --git a/utils/src/test/java/org/apache/cloudstack/utils/process/ProcessTest.java b/utils/src/test/java/org/apache/cloudstack/utils/process/ProcessTest.java index 2c158c800a4..a175e6010cd 100644 --- a/utils/src/test/java/org/apache/cloudstack/utils/process/ProcessTest.java +++ b/utils/src/test/java/org/apache/cloudstack/utils/process/ProcessTest.java @@ -49,7 +49,7 @@ public class ProcessTest { 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 @@ -57,7 +57,7 @@ public class ProcessTest { 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)