private ProcessCompletionResult runTask(String host, String command) {
int exitCode = 0;
ProcessCompletionResult result = null;
// all errors will be captured in the error stream
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
ChannelExec channel = null;
try {
Session session = createSSHSession(host);
channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(errorStream);
InputStream in=channel.getInputStream();
// connect actually execs the process on a remote node
channel.connect();
// synchronously consume the stdout. If no stdout is available
// this method exits.
consumeProcessStream(host, in, outputStream, channel);
channel.disconnect();
if ( channel.getExitStatus() != 0 || errorStream.size() > 0 ) {
result = new ProcessCompletionResult(channel.getExitStatus(), host, command, new String(errorStream.toByteArray()));
} else {
result = new ProcessCompletionResult(channel.getExitStatus(), host, command);
}
} catch (Throwable e) {
if ( channel == null ) {
exitCode = -1;
} else {
exitCode = channel.getExitStatus();
}
try {
outputStream.write(errorStream.toByteArray());
} catch( Exception ex) {
ex.printStackTrace();