From 8d728ecd4df38ce9876d187b852bec142369af84 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sun, 19 Sep 2010 22:58:56 -0700 Subject: [PATCH] Issue 363: set prty so that sudo works when requiretty exists --- .../BlobStoreAndComputeServiceLiveTest.java | 12 ++++- .../compute/EC2ComputeServiceLiveTest.java | 6 --- .../compute/callables/RunScriptOnNode.java | 44 ++++++++++--------- .../org/jclouds/ssh/jsch/JschSshClient.java | 1 + 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/aws/core/src/test/java/org/jclouds/aws/ec2/compute/BlobStoreAndComputeServiceLiveTest.java b/aws/core/src/test/java/org/jclouds/aws/ec2/compute/BlobStoreAndComputeServiceLiveTest.java index cb59bb6b0e..ed9d68fb4b 100644 --- a/aws/core/src/test/java/org/jclouds/aws/ec2/compute/BlobStoreAndComputeServiceLiveTest.java +++ b/aws/core/src/test/java/org/jclouds/aws/ec2/compute/BlobStoreAndComputeServiceLiveTest.java @@ -22,12 +22,16 @@ package org.jclouds.aws.ec2.compute; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.collect.Iterables.get; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Map; import java.util.Properties; import org.jclouds.Constants; import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.BlobStoreContextFactory; import org.jclouds.blobstore.domain.Blob; +import org.jclouds.compute.BaseComputeServiceLiveTest; import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContextFactory; import org.jclouds.compute.domain.NodeMetadata; @@ -54,6 +58,7 @@ public class BlobStoreAndComputeServiceLiveTest { protected String blobStoreProvider; protected String computeServiceProvider; + protected Map keyPair; protected Properties setupCredentials(String provider) { String identity = checkNotNull(System.getProperty("test." + provider + ".identity"), "test." + provider @@ -74,8 +79,13 @@ public class BlobStoreAndComputeServiceLiveTest { return overrides; } + protected void setupKeyPairForTest() throws FileNotFoundException, IOException { + keyPair = BaseComputeServiceLiveTest.setupKeyPair(); + } + @BeforeGroups(groups = { "live" }) - public void setupClient() { + public void setupClient() throws FileNotFoundException, IOException { + setupKeyPairForTest(); computeContext = new ComputeServiceContextFactory().createContext(computeServiceProvider, ImmutableSet.of( new Log4JLoggingModule(), new JschSshClientModule()), setupCredentials(computeServiceProvider)); blobContext = new BlobStoreContextFactory().createContext(blobStoreProvider, ImmutableSet diff --git a/aws/core/src/test/java/org/jclouds/aws/ec2/compute/EC2ComputeServiceLiveTest.java b/aws/core/src/test/java/org/jclouds/aws/ec2/compute/EC2ComputeServiceLiveTest.java index e7a33b4dee..45bdbf6843 100644 --- a/aws/core/src/test/java/org/jclouds/aws/ec2/compute/EC2ComputeServiceLiveTest.java +++ b/aws/core/src/test/java/org/jclouds/aws/ec2/compute/EC2ComputeServiceLiveTest.java @@ -303,10 +303,7 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest { try { for (SecurityGroup group : securityGroupClient.describeSecurityGroupsInRegion(null)) if (group.getName().startsWith("jclouds#" + tag) || group.getName().equals(tag)) { - System.err.printf("deleting group %s%n", group.getName()); securityGroupClient.deleteSecurityGroupInRegion(null, group.getName()); - } else { - System.err.printf("group %s didn't match %s%n", group.getName(), tag); } } catch (Exception e) { @@ -314,10 +311,7 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest { try { for (KeyPair pair : keyPairClient.describeKeyPairsInRegion(null)) if (pair.getKeyName().startsWith("jclouds#" + tag) || pair.getKeyName().equals(tag)) { - System.err.printf("deleting key %s%n", pair.getKeyName()); keyPairClient.deleteKeyPairInRegion(null, pair.getKeyName()); - } else { - System.err.printf("key %s didn't match %s%n", pair.getKeyName(), tag); } } catch (Exception e) { diff --git a/compute/src/main/java/org/jclouds/compute/callables/RunScriptOnNode.java b/compute/src/main/java/org/jclouds/compute/callables/RunScriptOnNode.java index 8358eb9108..db0d446e36 100644 --- a/compute/src/main/java/org/jclouds/compute/callables/RunScriptOnNode.java +++ b/compute/src/main/java/org/jclouds/compute/callables/RunScriptOnNode.java @@ -94,13 +94,14 @@ public class RunScriptOnNode implements SshCallable { ssh.put(scriptName, script); ExecResponse returnVal = ssh.exec("chmod 755 " + scriptName); returnVal = ssh.exec("./" + scriptName + " init"); + logger.debug("<< initialized(%d)", returnVal.getExitCode()); - if (runAsRoot) - returnVal = runScriptAsRoot(); - else - returnVal = runScriptAsDefaultUser(); - runScriptNotRunning.apply(new CommandUsingClient("./" + scriptName + " status", ssh)); - logger.debug("<< complete(%d)", returnVal.getExitCode()); + String command = (runAsRoot) ? runScriptAsRoot() : runScriptAsDefaultUser(); + returnVal = runCommand(command); + logger.debug("<< start(%d)", returnVal.getExitCode()); + + boolean complete = runScriptNotRunning.apply(new CommandUsingClient("./" + scriptName + " status", ssh)); + logger.debug("<< complete(%s)", complete); if (logger.isDebugEnabled() || returnVal.getExitCode() != 0) { logger.debug("<< stdout from %s as %s@%s\n%s", scriptName, node.getCredentials().identity, Iterables.get(node .getPublicAddresses(), 0), ssh.exec("./" + scriptName + " tail").getOutput()); @@ -110,33 +111,34 @@ public class RunScriptOnNode implements SshCallable { return returnVal; } + private ExecResponse runCommand(String command) { + ExecResponse returnVal; + logger.debug(">> running [%s] as %s@%s", command.replace(node.getCredentials().credential, "XXXXX"), node + .getCredentials().identity, Iterables.get(node.getPublicAddresses(), 0)); + returnVal = ssh.exec(command); + return returnVal; + } + @Override public void setConnection(SshClient ssh, Logger logger) { this.logger = checkNotNull(logger, "logger"); this.ssh = checkNotNull(ssh, "ssh"); } - private ExecResponse runScriptAsRoot() { + private String runScriptAsRoot() { + String command; if (node.getCredentials().identity.equals("root")) { - logger.debug(">> running %s as %s@%s", scriptName, node.getCredentials().identity, Iterables.get(node - .getPublicAddresses(), 0)); - return ssh.exec("./" + scriptName + " start"); + command = "./" + scriptName + " start"; } else if (ComputeServiceUtils.isKeyAuth(node)) { - logger.debug(">> running sudo %s as %s@%s", scriptName, node.getCredentials().identity, Iterables.get(node - .getPublicAddresses(), 0)); - return ssh.exec("sudo ./" + scriptName + " start"); + command = "sudo ./" + scriptName + " start"; } else { - logger.debug(">> running sudo -S %s as %s@%s", scriptName, node.getCredentials().identity, Iterables.get(node - .getPublicAddresses(), 0)); - return ssh.exec(String.format("echo '%s'|sudo -S ./%s", node.getCredentials().credential, scriptName - + " start")); + command = String.format("echo '%s'|sudo -S ./%s", node.getCredentials().credential, scriptName + " start"); } + return command; } - private ExecResponse runScriptAsDefaultUser() { - logger.debug(">> running script %s as %s@%s", scriptName, node.getCredentials().identity, Iterables.get(node - .getPublicAddresses(), 0)); - return ssh.exec(String.format("./%s", scriptName + " start")); + private String runScriptAsDefaultUser() { + return "./" + scriptName + " start"; } @Override diff --git a/extensions/ssh/jsch/src/main/java/org/jclouds/ssh/jsch/JschSshClient.java b/extensions/ssh/jsch/src/main/java/org/jclouds/ssh/jsch/JschSshClient.java index 5c208f99ae..343cd09550 100644 --- a/extensions/ssh/jsch/src/main/java/org/jclouds/ssh/jsch/JschSshClient.java +++ b/extensions/ssh/jsch/src/main/java/org/jclouds/ssh/jsch/JschSshClient.java @@ -264,6 +264,7 @@ public class JschSshClient implements SshClient { try { try { executor = (ChannelExec) session.openChannel("exec"); + executor.setPty(true); } catch (JSchException e) { throw new SshException(String.format("%s@%s:%d: Error connecting to exec.", username, host, port), e); }