mirror of
https://github.com/apache/jclouds.git
synced 2025-02-16 15:08:28 +00:00
added runScriptOnNodesMatching; removed runScriptOnNodesWithTag as it proved unflexible and the new method is a broader case; use runScriptOnNodesMatching instead
This commit is contained in:
parent
8b50a401cb
commit
dd4087982b
@ -21,6 +21,7 @@ package org.jclouds.compute;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import org.jclouds.compute.domain.ComputeMetadata;
|
||||
import org.jclouds.compute.domain.Image;
|
||||
import org.jclouds.compute.domain.NodeMetadata;
|
||||
@ -170,27 +171,26 @@ public interface ComputeService {
|
||||
/**
|
||||
* Runs the script without any additional options
|
||||
*
|
||||
* @see #runScriptOnNodesWithTag(String, byte[], org.jclouds.compute.options.RunScriptOptions)
|
||||
* @see #runScriptOnNodesMatching(Predicate, byte[], org.jclouds.compute.options.RunScriptOptions)
|
||||
*/
|
||||
Map<NodeMetadata, ExecResponse> runScriptOnNodesWithTag(String tag, byte[] runScript)
|
||||
Map<NodeMetadata, ExecResponse> runScriptOnNodesMatching(Predicate<NodeMetadata> filter, byte[] runScript)
|
||||
throws RunScriptOnNodesException;
|
||||
|
||||
/**
|
||||
* Run the script on all nodes with the specific tag.
|
||||
*
|
||||
* @param tag
|
||||
* tag to look up the nodes
|
||||
* @param filter
|
||||
* Predicate-based filter to define on which nodes the script is to be
|
||||
* executed
|
||||
* @param runScript
|
||||
* script to run in byte format. If the script is a string, use
|
||||
* {@link String#getBytes()} to retrieve the bytes
|
||||
* @param options
|
||||
* nullable options to how to run the script
|
||||
* nullable options to how to run the script, whether to override credentials
|
||||
* @return map with node identifiers and corresponding responses
|
||||
* @throws RunScriptOnNodesException
|
||||
* when there's a problem running the script on the nodes. Note that successful and
|
||||
* failed nodes are a part of this exception, so be sure to inspect this carefully.
|
||||
* @throws RunScriptOnNodesException if anything goes wrong during script execution
|
||||
*/
|
||||
Map<NodeMetadata, ExecResponse> runScriptOnNodesWithTag(String tag, byte[] runScript,
|
||||
Map<NodeMetadata, ExecResponse> runScriptOnNodesMatching(Predicate<NodeMetadata> filter, byte[] runScript,
|
||||
RunScriptOptions options) throws RunScriptOnNodesException;
|
||||
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import org.jclouds.compute.domain.NodeMetadata;
|
||||
import org.jclouds.compute.options.RunScriptOptions;
|
||||
import org.jclouds.compute.util.ComputeUtils;
|
||||
@ -35,21 +36,19 @@ public class RunScriptOnNodesException extends Exception {
|
||||
|
||||
/** The serialVersionUID */
|
||||
private static final long serialVersionUID = -2272965726680821281L;
|
||||
private final String tag;
|
||||
private final byte[] runScript;
|
||||
private final RunScriptOptions options;
|
||||
private final Map<NodeMetadata, ExecResponse> successfulNodes;
|
||||
private final Map<? extends NodeMetadata, ? extends Throwable> failedNodes;
|
||||
private final Map<?, Exception> executionExceptions;
|
||||
|
||||
public RunScriptOnNodesException(String tag, final byte[] runScript,
|
||||
public RunScriptOnNodesException(final byte[] runScript,
|
||||
@Nullable final RunScriptOptions options,
|
||||
Map<NodeMetadata, ExecResponse> successfulNodes, Map<?, Exception> executionExceptions,
|
||||
Map<? extends NodeMetadata, ? extends Throwable> failedNodes) {
|
||||
super(String.format("error runScript on node tag(%s) options(%s)%n%s%n%s", tag,
|
||||
super(String.format("error runScript on filtered nodes options(%s)%n%s%n%s",
|
||||
options, ComputeUtils.createExecutionErrorMessage(executionExceptions), ComputeUtils
|
||||
.createNodeErrorMessage(failedNodes)));
|
||||
this.tag = tag;
|
||||
this.runScript = runScript;
|
||||
this.options = options;
|
||||
this.successfulNodes = successfulNodes;
|
||||
@ -58,7 +57,6 @@ public class RunScriptOnNodesException extends Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Nodes that performed ssh without error
|
||||
*/
|
||||
public Map<NodeMetadata, ExecResponse> getSuccessfulNodes() {
|
||||
@ -81,10 +79,6 @@ public class RunScriptOnNodesException extends Exception {
|
||||
return failedNodes;
|
||||
}
|
||||
|
||||
public String getTag() {
|
||||
return tag;
|
||||
}
|
||||
|
||||
public byte[] getRunScript() {
|
||||
return runScript;
|
||||
}
|
||||
|
@ -27,7 +27,6 @@ import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static org.jclouds.concurrent.ConcurrentUtils.awaitCompletion;
|
||||
import static org.jclouds.concurrent.ConcurrentUtils.makeListenable;
|
||||
import static org.jclouds.util.Utils.checkNotEmpty;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@ -99,6 +98,7 @@ public class BaseComputeService implements ComputeService {
|
||||
protected final Provider<TemplateBuilder> templateBuilderProvider;
|
||||
protected final ComputeUtils utils;
|
||||
protected final ExecutorService executor;
|
||||
protected final ComputeMetadataToNodeMetadata computeMetadataToNodeMetadata;
|
||||
|
||||
private static class NodeMatchesTag implements Predicate<NodeMetadata> {
|
||||
private final String tag;
|
||||
@ -138,6 +138,7 @@ public class BaseComputeService implements ComputeService {
|
||||
"templateBuilderProvider");
|
||||
this.utils = checkNotNull(utils, "utils");
|
||||
this.executor = checkNotNull(executor, "executor");
|
||||
this.computeMetadataToNodeMetadata = new ComputeMetadataToNodeMetadata();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -223,17 +224,20 @@ public class BaseComputeService implements ComputeService {
|
||||
*/
|
||||
protected Set<? extends NodeMetadata> doListNodesWithTag(final String tag) {
|
||||
return Sets.newHashSet(Iterables.filter(Iterables.transform(listNodesStrategy
|
||||
.execute(GetNodesOptions.NONE), new Function<ComputeMetadata, NodeMetadata>() {
|
||||
.execute(GetNodesOptions.NONE), computeMetadataToNodeMetadata), new NodeMatchesTag(tag)));
|
||||
}
|
||||
|
||||
class ComputeMetadataToNodeMetadata
|
||||
implements Function<ComputeMetadata, NodeMetadata> {
|
||||
|
||||
@Override
|
||||
public NodeMetadata apply(ComputeMetadata from) {
|
||||
return from instanceof NodeMetadata ? NodeMetadata.class.cast(from)
|
||||
: getNodeMetadata(from);
|
||||
}
|
||||
|
||||
}), new NodeMatchesTag(tag)));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Set<? extends NodeMetadata> listNodesWithTag(String tag) {
|
||||
logger.debug(">> listing nodes by tag(%s)", tag);
|
||||
@ -306,31 +310,33 @@ public class BaseComputeService implements ComputeService {
|
||||
|
||||
/**
|
||||
* @throws RunScriptOnNodesException
|
||||
* @see #runScriptOnNodesWithTag(String, byte[], org.jclouds.compute.options.RunScriptOptions)
|
||||
* @see #runScriptOnNodesMatching(Predicate, byte[], org.jclouds.compute.options.RunScriptOptions)
|
||||
*/
|
||||
public Map<NodeMetadata, ExecResponse> runScriptOnNodesWithTag(String tag, byte[] runScript)
|
||||
public Map<NodeMetadata, ExecResponse> runScriptOnNodesMatching(Predicate<NodeMetadata> filter, byte[] runScript)
|
||||
throws RunScriptOnNodesException {
|
||||
return runScriptOnNodesWithTag(tag, runScript, RunScriptOptions.NONE);
|
||||
return runScriptOnNodesMatching(filter, runScript, RunScriptOptions.NONE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the script on all nodes with the specific tag.
|
||||
*
|
||||
* @param tag
|
||||
* tag to look up the nodes
|
||||
* @param filter
|
||||
* Predicate-based filter to define on which nodes the script is to be
|
||||
* executed
|
||||
* @param runScript
|
||||
* script to run in byte format. If the script is a string, use
|
||||
* {@link String#getBytes()} to retrieve the bytes
|
||||
* @param options
|
||||
* nullable options to how to run the script, whether to override credentials
|
||||
* @return map with node identifiers and corresponding responses
|
||||
* @throws RunScriptOnNodesException
|
||||
* @throws RunScriptOnNodesException if anything goes wrong during script execution
|
||||
*/
|
||||
public Map<NodeMetadata, ExecResponse> runScriptOnNodesWithTag(String tag,
|
||||
public Map<NodeMetadata, ExecResponse> runScriptOnNodesMatching(Predicate<NodeMetadata> filter,
|
||||
final byte[] runScript, @Nullable final RunScriptOptions options)
|
||||
throws RunScriptOnNodesException {
|
||||
Iterable<? extends NodeMetadata> nodes = verifyParametersAndGetNodes(tag, runScript,
|
||||
Iterable<? extends NodeMetadata> nodes = verifyParametersAndGetNodes(filter, runScript,
|
||||
(options != null) ? options : RunScriptOptions.NONE);
|
||||
|
||||
final Map<NodeMetadata, ExecResponse> execs = Maps.newHashMap();
|
||||
|
||||
final Map<NodeMetadata, Exception> badNodes = Maps.newLinkedHashMap();
|
||||
@ -369,26 +375,22 @@ public class BaseComputeService implements ComputeService {
|
||||
Map<?, Exception> exceptions = awaitCompletion(responses, executor, null, logger,
|
||||
"starting nodes");
|
||||
if (exceptions.size() > 0 || badNodes.size() > 0) {
|
||||
throw new RunScriptOnNodesException(tag, runScript, options, execs, exceptions, badNodes);
|
||||
throw new RunScriptOnNodesException(runScript, options, execs, exceptions, badNodes);
|
||||
}
|
||||
return execs;
|
||||
|
||||
}
|
||||
|
||||
private Iterable<? extends NodeMetadata> verifyParametersAndGetNodes(String tag,
|
||||
private Iterable<? extends NodeMetadata> verifyParametersAndGetNodes(Predicate<NodeMetadata> filter,
|
||||
byte[] runScript, final RunScriptOptions options) {
|
||||
checkNotEmpty(tag, "Tag must be provided");
|
||||
checkNotNull(filter, "Filter must be provided");
|
||||
checkNotNull(runScript,
|
||||
"The script (represented by bytes array - use \"script\".getBytes() must be provided");
|
||||
checkNotNull(options, "options");
|
||||
Iterable<? extends NodeMetadata> nodes = Iterables.filter(listNodesWithTag(tag),
|
||||
new Predicate<NodeMetadata>() {
|
||||
Iterable<? extends NodeMetadata> nodes = Iterables.filter(
|
||||
Iterables.transform(listNodes(), computeMetadataToNodeMetadata),
|
||||
filter);
|
||||
|
||||
@Override
|
||||
public boolean apply(NodeMetadata input) {
|
||||
return input.getState() == NodeState.RUNNING;
|
||||
}
|
||||
|
||||
});
|
||||
return Iterables.transform(nodes, new Function<NodeMetadata, NodeMetadata>() {
|
||||
|
||||
@Override
|
||||
|
@ -43,6 +43,7 @@ import javax.annotation.Nullable;
|
||||
import javax.annotation.Resource;
|
||||
import javax.inject.Named;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import org.jclouds.Constants;
|
||||
import org.jclouds.compute.domain.ComputeMetadata;
|
||||
import org.jclouds.compute.domain.NodeMetadata;
|
||||
@ -464,5 +465,4 @@ public class ComputeUtils {
|
||||
}
|
||||
return providers;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -77,6 +77,8 @@ import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Module;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
@ -247,8 +249,15 @@ public abstract class BaseComputeServiceLiveTest {
|
||||
|
||||
private Map<NodeMetadata, ExecResponse> runScriptWithCreds(String tag, OsFamily osFamily,
|
||||
Credentials creds) throws RunScriptOnNodesException {
|
||||
Predicate<NodeMetadata> filter = new Predicate<NodeMetadata>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable NodeMetadata nodeMetadata) {
|
||||
return true; /*accept all*/
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
return client.runScriptOnNodesWithTag(tag, buildScript(osFamily).getBytes(),
|
||||
return client.runScriptOnNodesMatching(filter, buildScript(osFamily).getBytes(),
|
||||
RunScriptOptions.Builder.overrideCredentialsWith(creds));
|
||||
} catch (SshException e) {
|
||||
if (Throwables.getRootCause(e).getMessage().contains("Auth fail")) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user