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,8 +73,29 @@ 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);
for (int i = 0; i < 3; i++) {
try { try {
ssh.connect(); ssh.connect();
runScriptOnNodeWithClient(ssh, node, script);
} catch (RuntimeException from) {
if (Iterables.size(Iterables.filter(Throwables.getCausalChain(from),
ConnectException.class)) >= 1) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Throwables.propagate(e);
}
continue;
}
Throwables.propagate(from);
} finally {
if (ssh != null)
ssh.disconnect();
}
}
}
private void runScriptOnNodeWithClient(SshClient ssh, CreateNodeResponse node, byte[] script) {
String scriptName = node.getId() + ".sh"; String scriptName = node.getId() + ".sh";
ssh.put(scriptName, new ByteArrayInputStream(script)); ssh.put(scriptName, new ByteArrayInputStream(script));
ssh.exec("chmod 755 " + scriptName); ssh.exec("chmod 755 " + scriptName);
@ -88,10 +111,6 @@ public class ComputeUtils {
String.format("echo %s|sudo -S ./%s", node.getCredentials().key, scriptName)) String.format("echo %s|sudo -S ./%s", node.getCredentials().key, scriptName))
.getExitCode()); .getExitCode());
} }
} finally {
if (ssh != null)
ssh.disconnect();
}
} }
public static boolean isKeyAuth(CreateNodeResponse createdNode) { public static boolean isKeyAuth(CreateNodeResponse createdNode) {

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" >