Issue 129: tidied up ssh output for ant/compute commands

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2714 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2010-01-21 08:50:10 +00:00
parent bc1c1fd793
commit 56116204df
5 changed files with 26 additions and 11 deletions

View File

@ -25,10 +25,12 @@ public class ExecResponse {
private final String error; private final String error;
private final String output; private final String output;
private final int exitCode;
public ExecResponse(String output, String error) { public ExecResponse(String output, String error, int exitCode) {
this.output = output; this.output = output;
this.error = error; this.error = error;
this.exitCode = exitCode;
} }
public String getError() { public String getError() {
@ -41,7 +43,7 @@ public class ExecResponse {
@Override @Override
public String toString() { public String toString() {
return "ExecResponse [error=" + error + ", output=" + output + "]"; return "ExecResponse [output=" + output + ", error=" + error + ", exitCode=" + exitCode + "]";
} }
@Override @Override
@ -49,6 +51,7 @@ public class ExecResponse {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + ((error == null) ? 0 : error.hashCode()); result = prime * result + ((error == null) ? 0 : error.hashCode());
result = prime * result + exitCode;
result = prime * result + ((output == null) ? 0 : output.hashCode()); result = prime * result + ((output == null) ? 0 : output.hashCode());
return result; return result;
} }
@ -67,6 +70,8 @@ public class ExecResponse {
return false; return false;
} else if (!error.equals(other.error)) } else if (!error.equals(other.error))
return false; return false;
if (exitCode != other.exitCode)
return false;
if (output == null) { if (output == null) {
if (other.output != null) if (other.output != null)
return false; return false;
@ -75,4 +80,8 @@ public class ExecResponse {
return true; return true;
} }
public int getExitCode() {
return exitCode;
}
} }

View File

@ -204,7 +204,7 @@ public class JschSshClient implements SshClient {
try { try {
executor.connect(); executor.connect();
return new ExecResponse(Utils.toStringAndClose(executor.getInputStream()), error return new ExecResponse(Utils.toStringAndClose(executor.getInputStream()), error
.toString()); .toString(), executor.getExitStatus());
} catch (Exception e) { } catch (Exception e) {
throw new SshException(String.format("%s@%s:%d: Error executing command: ", username, throw new SshException(String.format("%s@%s:%d: Error executing command: ", username,
host.getHostAddress(), port, command), e); host.getHostAddress(), port, command), e);

View File

@ -82,7 +82,7 @@ public class JschSshClientLiveTest {
public ExecResponse exec(String command) { public ExecResponse exec(String command) {
if (command.equals("hostname")) { if (command.equals("hostname")) {
try { try {
return new ExecResponse(InetAddress.getLocalHost().getHostName(), ""); return new ExecResponse(InetAddress.getLocalHost().getHostName(), "", 0);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -39,6 +39,7 @@ import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.domain.Template; import org.jclouds.compute.domain.Template;
import org.jclouds.compute.options.RunNodeOptions; import org.jclouds.compute.options.RunNodeOptions;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.ssh.jsch.config.JschSshClientModule;
import org.jclouds.tools.ant.logging.config.AntLoggingModule; import org.jclouds.tools.ant.logging.config.AntLoggingModule;
import com.google.common.base.Function; import com.google.common.base.Function;
@ -74,9 +75,11 @@ public class ComputeTaskUtils {
try { try {
Properties props = new Properties(); Properties props = new Properties();
props.putAll(projectProvider.get().getProperties()); props.putAll(projectProvider.get().getProperties());
return new ComputeServiceContextFactory().createContext(from, ImmutableSet return new ComputeServiceContextFactory().createContext(from, ImmutableSet.of(
.of((Module) new AntLoggingModule(projectProvider.get(), (Module) new AntLoggingModule(projectProvider.get(),
ComputeServiceConstants.COMPUTE_LOGGER)), props); ComputeServiceConstants.COMPUTE_LOGGER),
new JschSshClientModule()),
props);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -136,13 +136,16 @@ public class VCloudComputeService implements ComputeService, VCloudComputeClient
ssh.put(scriptName, new ByteArrayInputStream(script)); ssh.put(scriptName, new ByteArrayInputStream(script));
ssh.exec("chmod 755 " + scriptName); ssh.exec("chmod 755 " + scriptName);
if (node.getCredentials().account.equals("root")) { if (node.getCredentials().account.equals("root")) {
logger.debug(ssh.exec("./" + scriptName).toString()); logger.debug(">> running %s as %s", scriptName, node.getCredentials().account);
logger.debug("<< complete(%d)", ssh.exec("./" + scriptName).getExitCode());
} else if (isKeyBasedAuth(node)) { } else if (isKeyBasedAuth(node)) {
logger.debug(ssh.exec("sudo ./" + scriptName).toString()); logger.debug(">> running sudo %s as %s", scriptName, node.getCredentials().account);
logger.debug("<< complete(%d)", ssh.exec("sudo ./" + scriptName).getExitCode());
} else { } else {
logger.debug(ssh.exec( logger.debug(">> running sudo -S %s as %s", scriptName, node.getCredentials().account);
logger.debug("<< complete(%d)", ssh.exec(
String.format("echo %s|sudo -S ./%s", node.getCredentials().key, scriptName)) String.format("echo %s|sudo -S ./%s", node.getCredentials().key, scriptName))
.toString()); .getExitCode());
} }
} finally { } finally {
if (ssh != null) if (ssh != null)