Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
WHIRR-361. Emergency upgrade to jclouds 1.1.1 and improved configure …
Browse files Browse the repository at this point in the history
…phase logging (Adrian Cole via asavu)

git-svn-id: https://svn.apache.org/repos/asf/incubator/whirr/trunk@1159016 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Andrei Savu committed Aug 18, 2011
1 parent 6359558 commit a5396d4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Expand Up @@ -81,6 +81,9 @@ Trunk (unreleased changes)

WHIRR-361. Add Guava as an explicit dependency (Adrian Cole via asavu)

WHIRR-361. Emergency upgrade to jclouds 1.1.1 and improved configure phase
logging (Adrian Cole via asavu)

Release 0.5.0 - 2011-05-16

INCOMPATIBLE CHANGES
Expand Down
Expand Up @@ -18,8 +18,12 @@

package org.apache.whirr.actions;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;

import java.io.IOException;
Expand All @@ -40,6 +44,7 @@
import org.jclouds.compute.RunScriptOnNodesException;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.compute.predicates.NodePredicates;
import org.jclouds.domain.Credentials;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.slf4j.Logger;
Expand Down Expand Up @@ -71,27 +76,83 @@ protected void doAction(Map<InstanceTemplate, ClusterActionEvent> eventMap)
for (Entry<InstanceTemplate, ClusterActionEvent> entry : eventMap.entrySet()) {
ClusterSpec clusterSpec = entry.getValue().getClusterSpec();
Cluster cluster = entry.getValue().getCluster();

StatementBuilder statementBuilder = entry.getValue().getStatementBuilder();

ComputeServiceContext computeServiceContext = getCompute().apply(clusterSpec);
ComputeService computeService = computeServiceContext.getComputeService();

Credentials credentials = new Credentials(
clusterSpec.getClusterUser(),
clusterSpec.getPrivateKey());

try {
LOG.info("Running configuration script");
Map<String, ? extends NodeMetadata> nodesInCluster = getNodesForInstanceIdsInCluster(cluster, computeService);

if (LOG.isDebugEnabled()) {
LOG.debug("Nodes in cluster: {}", nodesInCluster.values());
}

Map<String, ? extends NodeMetadata> nodesToApply = Maps.uniqueIndex(
Iterables.filter(nodesInCluster.values(),
toNodeMetadataPredicate(clusterSpec, cluster, entry.getKey().getRoles())),
getNodeId
);

LOG.info("Running configuration script on nodes: {}", nodesToApply.keySet());
if (LOG.isDebugEnabled())
LOG.debug("Running script:\n{}", statementBuilder.render(OsFamily.UNIX));
LOG.debug("script:\n{}", statementBuilder.render(OsFamily.UNIX));

computeService.runScriptOnNodesMatching(
toNodeMetadataPredicate(clusterSpec, cluster, entry.getKey().getRoles()),
withIds(nodesToApply.keySet()),
statementBuilder,
RunScriptOptions.Builder.overrideCredentialsWith(credentials));
RunScriptOptions.Builder.overrideCredentialsWith(credentials)
);

LOG.info("Configuration script run completed");
} catch (RunScriptOnNodesException e) {
// TODO: retry
throw new IOException(e);
}
}
}

private Map<String, ? extends NodeMetadata> getNodesForInstanceIdsInCluster(Cluster cluster,
ComputeService computeService) {
Iterable<String> ids = Iterables.transform(cluster.getInstances(), new Function<Instance, String>() {

@Override
public String apply(Instance arg0) {
return arg0.getId();
}

});

Set<? extends NodeMetadata> nodes = computeService.listNodesDetailsMatching(
NodePredicates.withIds(Iterables.toArray(ids, String.class)));

return Maps.uniqueIndex(nodes, getNodeId);
}

public static Predicate<NodeMetadata> withIds(Iterable<String> ids) {
checkNotNull(ids, "ids must be defined");
final Set<String> search = ImmutableSet.copyOf(ids);
return new Predicate<NodeMetadata>() {
@Override
public boolean apply(NodeMetadata nodeMetadata) {
return search.contains(nodeMetadata.getId());
}
};
}

private static Function<NodeMetadata, String> getNodeId = new Function<NodeMetadata, String>() {

@Override
public String apply(NodeMetadata arg0) {
return arg0.getId();
}

};

private Predicate<NodeMetadata> toNodeMetadataPredicate(final ClusterSpec clusterSpec, final Cluster cluster, final Set<String> roles) {
final Map<String, Instance> nodeIdToInstanceMap = Maps.newHashMap();
Expand All @@ -101,10 +162,6 @@ private Predicate<NodeMetadata> toNodeMetadataPredicate(final ClusterSpec cluste
return new Predicate<NodeMetadata>() {
@Override
public boolean apply(NodeMetadata nodeMetadata) {
// Check it's the correct cluster
if (!clusterSpec.getClusterName().equals(nodeMetadata.getGroup())) {
return false;
}
Instance instance = nodeIdToInstanceMap.get(nodeMetadata.getId());
if (instance == null) {
LOG.debug("No instance for {} found in map", nodeMetadata);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -51,7 +51,7 @@
</modules>

<properties>
<jclouds.version>1.1.0</jclouds.version>
<jclouds.version>1.1.1</jclouds.version>
</properties>

<dependencyManagement>
Expand Down

0 comments on commit a5396d4

Please sign in to comment.