Issue 130: added retry logic for ssh runscript

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2724 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2010-01-22 21:35:45 +00:00
parent b7f9426a02
commit 4b00bd9649
2 changed files with 39 additions and 20 deletions

View File

@ -21,6 +21,7 @@ package org.jclouds.compute.util;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.net.ConnectException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -33,6 +34,7 @@ import org.jclouds.logging.Logger;
import org.jclouds.ssh.SshClient; import org.jclouds.ssh.SshClient;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.inject.Inject; import com.google.inject.Inject;
@ -71,26 +73,43 @@ public class ComputeUtils {
SshClient ssh = isKeyAuth(node) ? sshFactory.create(socket, node.getCredentials().account, SshClient ssh = isKeyAuth(node) ? sshFactory.create(socket, node.getCredentials().account,
node.getCredentials().key.getBytes()) : sshFactory.create(socket, node node.getCredentials().key.getBytes()) : sshFactory.create(socket, node
.getCredentials().account, node.getCredentials().key); .getCredentials().account, node.getCredentials().key);
try { for (int i = 0; i < 3; i++) {
ssh.connect(); try {
String scriptName = node.getId() + ".sh"; ssh.connect();
ssh.put(scriptName, new ByteArrayInputStream(script)); runScriptOnNodeWithClient(ssh, node, script);
ssh.exec("chmod 755 " + scriptName); } catch (RuntimeException from) {
if (node.getCredentials().account.equals("root")) { if (Iterables.size(Iterables.filter(Throwables.getCausalChain(from),
logger.debug(">> running %s as %s", scriptName, node.getCredentials().account); ConnectException.class)) >= 1) {
logger.debug("<< complete(%d)", ssh.exec("./" + scriptName).getExitCode()); try {
} else if (isKeyAuth(node)) { Thread.sleep(100);
logger.debug(">> running sudo %s as %s", scriptName, node.getCredentials().account); } catch (InterruptedException e) {
logger.debug("<< complete(%d)", ssh.exec("sudo ./" + scriptName).getExitCode()); Throwables.propagate(e);
} else { }
logger.debug(">> running sudo -S %s as %s", scriptName, node.getCredentials().account); continue;
logger.debug("<< complete(%d)", ssh.exec( }
String.format("echo %s|sudo -S ./%s", node.getCredentials().key, scriptName)) Throwables.propagate(from);
.getExitCode()); } finally {
if (ssh != null)
ssh.disconnect();
} }
} finally { }
if (ssh != null) }
ssh.disconnect();
private void runScriptOnNodeWithClient(SshClient ssh, CreateNodeResponse node, byte[] script) {
String scriptName = node.getId() + ".sh";
ssh.put(scriptName, new ByteArrayInputStream(script));
ssh.exec("chmod 755 " + scriptName);
if (node.getCredentials().account.equals("root")) {
logger.debug(">> running %s as %s", scriptName, node.getCredentials().account);
logger.debug("<< complete(%d)", ssh.exec("./" + scriptName).getExitCode());
} else if (isKeyAuth(node)) {
logger.debug(">> running sudo %s as %s", scriptName, node.getCredentials().account);
logger.debug("<< complete(%d)", ssh.exec("sudo ./" + scriptName).getExitCode());
} else {
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))
.getExitCode());
} }
} }

View File

@ -67,7 +67,7 @@
<compute action="destroy" provider="${url}"> <compute action="destroy" provider="${url}">
<node name="${nodename}" /> <node name="${nodename}" />
</compute> </compute>
<sleep seconds="2" /> <sleep seconds="5" />
</target> </target>
<target name="create" description="create the node ${nodename}" depends="destroy" > <target name="create" description="create the node ${nodename}" depends="destroy" >