mirror of https://github.com/apache/jclouds.git
Issue 363: set prty so that sudo works when requiretty exists
This commit is contained in:
parent
839fedb0aa
commit
8d728ecd4d
|
@ -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<String, String> 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
|
||||
|
|
|
@ -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) {
|
||||
|
||||
|
|
|
@ -94,13 +94,14 @@ public class RunScriptOnNode implements SshCallable<ExecResponse> {
|
|||
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<ExecResponse> {
|
|||
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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue