Issue 363: set prty so that sudo works when requiretty exists

This commit is contained in:
Adrian Cole 2010-09-19 22:58:56 -07:00
parent 839fedb0aa
commit 8d728ecd4d
4 changed files with 35 additions and 28 deletions

View File

@ -22,12 +22,16 @@ package org.jclouds.aws.ec2.compute;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.get; 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 java.util.Properties;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.BlobStoreContextFactory; import org.jclouds.blobstore.BlobStoreContextFactory;
import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.Blob;
import org.jclouds.compute.BaseComputeServiceLiveTest;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory; import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
@ -54,6 +58,7 @@ public class BlobStoreAndComputeServiceLiveTest {
protected String blobStoreProvider; protected String blobStoreProvider;
protected String computeServiceProvider; protected String computeServiceProvider;
protected Map<String, String> keyPair;
protected Properties setupCredentials(String provider) { protected Properties setupCredentials(String provider) {
String identity = checkNotNull(System.getProperty("test." + provider + ".identity"), "test." + provider String identity = checkNotNull(System.getProperty("test." + provider + ".identity"), "test." + provider
@ -74,8 +79,13 @@ public class BlobStoreAndComputeServiceLiveTest {
return overrides; return overrides;
} }
protected void setupKeyPairForTest() throws FileNotFoundException, IOException {
keyPair = BaseComputeServiceLiveTest.setupKeyPair();
}
@BeforeGroups(groups = { "live" }) @BeforeGroups(groups = { "live" })
public void setupClient() { public void setupClient() throws FileNotFoundException, IOException {
setupKeyPairForTest();
computeContext = new ComputeServiceContextFactory().createContext(computeServiceProvider, ImmutableSet.of( computeContext = new ComputeServiceContextFactory().createContext(computeServiceProvider, ImmutableSet.of(
new Log4JLoggingModule(), new JschSshClientModule()), setupCredentials(computeServiceProvider)); new Log4JLoggingModule(), new JschSshClientModule()), setupCredentials(computeServiceProvider));
blobContext = new BlobStoreContextFactory().createContext(blobStoreProvider, ImmutableSet blobContext = new BlobStoreContextFactory().createContext(blobStoreProvider, ImmutableSet

View File

@ -303,10 +303,7 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest {
try { try {
for (SecurityGroup group : securityGroupClient.describeSecurityGroupsInRegion(null)) for (SecurityGroup group : securityGroupClient.describeSecurityGroupsInRegion(null))
if (group.getName().startsWith("jclouds#" + tag) || group.getName().equals(tag)) { if (group.getName().startsWith("jclouds#" + tag) || group.getName().equals(tag)) {
System.err.printf("deleting group %s%n", group.getName());
securityGroupClient.deleteSecurityGroupInRegion(null, group.getName()); securityGroupClient.deleteSecurityGroupInRegion(null, group.getName());
} else {
System.err.printf("group %s didn't match %s%n", group.getName(), tag);
} }
} catch (Exception e) { } catch (Exception e) {
@ -314,10 +311,7 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest {
try { try {
for (KeyPair pair : keyPairClient.describeKeyPairsInRegion(null)) for (KeyPair pair : keyPairClient.describeKeyPairsInRegion(null))
if (pair.getKeyName().startsWith("jclouds#" + tag) || pair.getKeyName().equals(tag)) { if (pair.getKeyName().startsWith("jclouds#" + tag) || pair.getKeyName().equals(tag)) {
System.err.printf("deleting key %s%n", pair.getKeyName());
keyPairClient.deleteKeyPairInRegion(null, pair.getKeyName()); keyPairClient.deleteKeyPairInRegion(null, pair.getKeyName());
} else {
System.err.printf("key %s didn't match %s%n", pair.getKeyName(), tag);
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -94,13 +94,14 @@ public class RunScriptOnNode implements SshCallable<ExecResponse> {
ssh.put(scriptName, script); ssh.put(scriptName, script);
ExecResponse returnVal = ssh.exec("chmod 755 " + scriptName); ExecResponse returnVal = ssh.exec("chmod 755 " + scriptName);
returnVal = ssh.exec("./" + scriptName + " init"); returnVal = ssh.exec("./" + scriptName + " init");
logger.debug("<< initialized(%d)", returnVal.getExitCode());
if (runAsRoot) String command = (runAsRoot) ? runScriptAsRoot() : runScriptAsDefaultUser();
returnVal = runScriptAsRoot(); returnVal = runCommand(command);
else logger.debug("<< start(%d)", returnVal.getExitCode());
returnVal = runScriptAsDefaultUser();
runScriptNotRunning.apply(new CommandUsingClient("./" + scriptName + " status", ssh)); boolean complete = runScriptNotRunning.apply(new CommandUsingClient("./" + scriptName + " status", ssh));
logger.debug("<< complete(%d)", returnVal.getExitCode()); logger.debug("<< complete(%s)", complete);
if (logger.isDebugEnabled() || returnVal.getExitCode() != 0) { if (logger.isDebugEnabled() || returnVal.getExitCode() != 0) {
logger.debug("<< stdout from %s as %s@%s\n%s", scriptName, node.getCredentials().identity, Iterables.get(node logger.debug("<< stdout from %s as %s@%s\n%s", scriptName, node.getCredentials().identity, Iterables.get(node
.getPublicAddresses(), 0), ssh.exec("./" + scriptName + " tail").getOutput()); .getPublicAddresses(), 0), ssh.exec("./" + scriptName + " tail").getOutput());
@ -110,33 +111,34 @@ public class RunScriptOnNode implements SshCallable<ExecResponse> {
return returnVal; 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 @Override
public void setConnection(SshClient ssh, Logger logger) { public void setConnection(SshClient ssh, Logger logger) {
this.logger = checkNotNull(logger, "logger"); this.logger = checkNotNull(logger, "logger");
this.ssh = checkNotNull(ssh, "ssh"); this.ssh = checkNotNull(ssh, "ssh");
} }
private ExecResponse runScriptAsRoot() { private String runScriptAsRoot() {
String command;
if (node.getCredentials().identity.equals("root")) { if (node.getCredentials().identity.equals("root")) {
logger.debug(">> running %s as %s@%s", scriptName, node.getCredentials().identity, Iterables.get(node command = "./" + scriptName + " start";
.getPublicAddresses(), 0));
return ssh.exec("./" + scriptName + " start");
} else if (ComputeServiceUtils.isKeyAuth(node)) { } else if (ComputeServiceUtils.isKeyAuth(node)) {
logger.debug(">> running sudo %s as %s@%s", scriptName, node.getCredentials().identity, Iterables.get(node command = "sudo ./" + scriptName + " start";
.getPublicAddresses(), 0));
return ssh.exec("sudo ./" + scriptName + " start");
} else { } else {
logger.debug(">> running sudo -S %s as %s@%s", scriptName, node.getCredentials().identity, Iterables.get(node command = String.format("echo '%s'|sudo -S ./%s", node.getCredentials().credential, scriptName + " start");
.getPublicAddresses(), 0));
return ssh.exec(String.format("echo '%s'|sudo -S ./%s", node.getCredentials().credential, scriptName
+ " start"));
} }
return command;
} }
private ExecResponse runScriptAsDefaultUser() { private String runScriptAsDefaultUser() {
logger.debug(">> running script %s as %s@%s", scriptName, node.getCredentials().identity, Iterables.get(node return "./" + scriptName + " start";
.getPublicAddresses(), 0));
return ssh.exec(String.format("./%s", scriptName + " start"));
} }
@Override @Override

View File

@ -264,6 +264,7 @@ public class JschSshClient implements SshClient {
try { try {
try { try {
executor = (ChannelExec) session.openChannel("exec"); executor = (ChannelExec) session.openChannel("exec");
executor.setPty(true);
} catch (JSchException e) { } catch (JSchException e) {
throw new SshException(String.format("%s@%s:%d: Error connecting to exec.", username, host, port), e); throw new SshException(String.format("%s@%s:%d: Error connecting to exec.", username, host, port), e);
} }