normalized tag parsing code

This commit is contained in:
Adrian Cole 2010-08-16 12:25:22 -07:00
parent 5b215533f1
commit 6e09985659
8 changed files with 109 additions and 131 deletions

View File

@ -24,6 +24,8 @@ import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jclouds.compute.ComputeServiceContextBuilder;
import org.jclouds.compute.domain.Architecture;
@ -45,22 +47,24 @@ import com.google.common.collect.Iterables;
* @author Adrian Cole
*/
public class ComputeServiceUtils {
public static final Pattern DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX = Pattern.compile("([^-]+)-[0-9a-f]+");
public static String parseTagFromName(String from) {
Matcher matcher = DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX.matcher(from);
return matcher.find() ? matcher.group(1) : "NOTAG-" + from;
}
public static final Map<org.jclouds.compute.domain.OsFamily, Map<String, String>> NAME_VERSION_MAP = ImmutableMap
.<org.jclouds.compute.domain.OsFamily, Map<String, String>> of(
org.jclouds.compute.domain.OsFamily.CENTOS, ImmutableMap
.<String, String> builder().put("5.3", "5.3").put("5.4", "5.4").put(
"5.5", "5.5").build(),
org.jclouds.compute.domain.OsFamily.RHEL, ImmutableMap
.<String, String> builder().put("5.3", "5.3").put("5.4", "5.4").put(
"5.5", "5.5").build(),
org.jclouds.compute.domain.OsFamily.UBUNTU, ImmutableMap
.<String, String> builder().put("hardy", "8.04").put("intrepid",
"8.10").put("jaunty", "9.04").put("karmic", "9.10").put(
"lucid", "10.04").put("maverick", "10.10").build());
.<org.jclouds.compute.domain.OsFamily, Map<String, String>> of(org.jclouds.compute.domain.OsFamily.CENTOS,
ImmutableMap.<String, String> builder().put("5.3", "5.3").put("5.4", "5.4").put("5.5", "5.5")
.build(), org.jclouds.compute.domain.OsFamily.RHEL,
ImmutableMap.<String, String> builder().put("5.3", "5.3").put("5.4", "5.4").put("5.5", "5.5")
.build(), org.jclouds.compute.domain.OsFamily.UBUNTU, ImmutableMap
.<String, String> builder().put("hardy", "8.04").put("intrepid", "8.10").put("jaunty",
"9.04").put("karmic", "9.10").put("lucid", "10.04").put("maverick", "10.10")
.build());
public static String parseVersionOrReturnEmptyString(org.jclouds.compute.domain.OsFamily family,
final String in) {
public static String parseVersionOrReturnEmptyString(org.jclouds.compute.domain.OsFamily family, final String in) {
if (NAME_VERSION_MAP.containsKey(family)) {
CONTAINS_SUBSTRING contains = new CONTAINS_SUBSTRING(in.replace('-', '.'));
try {
@ -94,27 +98,24 @@ public class ComputeServiceUtils {
Formatter fmt = new Formatter().format("Execution failures:%n%n");
int index = 1;
for (Entry<?, Exception> errorMessage : executionExceptions.entrySet()) {
fmt.format("%s) %s on %s:%n%s%n%n", index++, errorMessage.getValue().getClass()
.getSimpleName(), errorMessage.getKey(), Throwables
.getStackTraceAsString(errorMessage.getValue()));
fmt.format("%s) %s on %s:%n%s%n%n", index++, errorMessage.getValue().getClass().getSimpleName(), errorMessage
.getKey(), Throwables.getStackTraceAsString(errorMessage.getValue()));
}
return fmt.format("%s error[s]", executionExceptions.size()).toString();
}
public static String createNodeErrorMessage(
Map<? extends NodeMetadata, ? extends Throwable> failedNodes) {
public static String createNodeErrorMessage(Map<? extends NodeMetadata, ? extends Throwable> failedNodes) {
Formatter fmt = new Formatter().format("Node failures:%n%n");
int index = 1;
for (Entry<? extends NodeMetadata, ? extends Throwable> errorMessage : failedNodes.entrySet()) {
fmt.format("%s) %s on node %s:%n%s%n%n", index++, errorMessage.getValue().getClass()
.getSimpleName(), errorMessage.getKey().getId(), Throwables
.getStackTraceAsString(errorMessage.getValue()));
fmt.format("%s) %s on node %s:%n%s%n%n", index++, errorMessage.getValue().getClass().getSimpleName(),
errorMessage.getKey().getId(), Throwables.getStackTraceAsString(errorMessage.getValue()));
}
return fmt.format("%s error[s]", failedNodes.size()).toString();
}
public static Iterable<? extends ComputeMetadata> filterByName(
Iterable<? extends ComputeMetadata> nodes, final String name) {
public static Iterable<? extends ComputeMetadata> filterByName(Iterable<? extends ComputeMetadata> nodes,
final String name) {
return Iterables.filter(nodes, new Predicate<ComputeMetadata>() {
@Override
public boolean apply(ComputeMetadata input) {
@ -144,8 +145,7 @@ public class ComputeServiceUtils {
public static boolean isKeyAuth(NodeMetadata createdNode) {
return createdNode.getCredentials().credential != null
&& createdNode.getCredentials().credential
.startsWith("-----BEGIN RSA PRIVATE KEY-----");
&& createdNode.getCredentials().credential.startsWith("-----BEGIN RSA PRIVATE KEY-----");
}
/**
@ -153,10 +153,9 @@ public class ComputeServiceUtils {
* returns a new instance of {@link NodeMetadata} that has new credentials
*/
public static NodeMetadata installNewCredentials(NodeMetadata node, Credentials newCredentials) {
return new NodeMetadataImpl(node.getProviderId(), node.getName(), node.getId(), node
.getLocation(), node.getUri(), node.getUserMetadata(), node.getTag(), node
.getImage(), node.getState(), node.getPublicAddresses(), node.getPrivateAddresses(),
node.getExtra(), newCredentials);
return new NodeMetadataImpl(node.getProviderId(), node.getName(), node.getId(), node.getLocation(),
node.getUri(), node.getUserMetadata(), node.getTag(), node.getImage(), node.getState(), node
.getPublicAddresses(), node.getPrivateAddresses(), node.getExtra(), newCredentials);
}
public static Iterable<String> getSupportedProviders() {

View File

@ -195,8 +195,8 @@ public class ComputeUtils {
@Nullable SshCallable<?> last) {
checkState(this.sshFactory != null, "runScript requested, but no SshModule configured");
checkNodeHasPublicIps(node);
checkNotNull(node.getCredentials(), "credentials for node " + node.getId());
checkNotNull(node.getCredentials().credential, "credentials.credential for node " + node.getId());
checkNotNull(node.getCredentials(), "credentials for node " + node.getName());
checkNotNull(node.getCredentials().credential, "credentials.credential for node " + node.getName());
SshClient ssh = createSshClientOncePortIsListeningOnNode(node);
try {
ssh.connect();

View File

@ -20,12 +20,11 @@
package org.jclouds.gogrid.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.compute.util.ComputeServiceUtils.parseTagFromName;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import javax.inject.Inject;
@ -54,7 +53,6 @@ import com.google.common.collect.Iterables;
*/
@Singleton
public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
public static final Pattern ALL_BEFORE_HYPHEN_HEX = Pattern.compile("([^-]+)-[0-9a-f]+");
@Resource
protected Logger logger = Logger.NULL;
@ -90,8 +88,7 @@ public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
@Override
public NodeMetadata apply(Server from) {
Matcher matcher = ALL_BEFORE_HYPHEN_HEX.matcher(from.getName());
final String tag = matcher.find() ? matcher.group(1) : null;
String tag = parseTagFromName(from.getName());
Set<String> ipSet = ImmutableSet.of(from.getIp().getIp());
NodeState state = serverStateToNodeState.get(from.getState());
Credentials creds = client.getServerServices().getServerCredentialsList().get(from.getName());

View File

@ -20,11 +20,10 @@
package org.jclouds.ibmdev.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.compute.util.ComputeServiceUtils.parseTagFromName;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import javax.inject.Inject;
@ -50,7 +49,6 @@ import com.google.common.collect.ImmutableSet;
*/
@Singleton
public class InstanceToNodeMetadata implements Function<Instance, NodeMetadata> {
public static final Pattern ALL_BEFORE_HYPHEN_HEX = Pattern.compile("([^-]+)-[0-9a-f]+");
@Resource
protected Logger logger = Logger.NULL;
@ -61,11 +59,9 @@ public class InstanceToNodeMetadata implements Function<Instance, NodeMetadata>
@Inject
InstanceToNodeMetadata(Map<Instance.Status, NodeState> instanceStateToNodeState,
Map<String, ? extends Image> images,
@Named("CREDENTIALS") Map<String, String> credentialsMap,
Map<String, ? extends Image> images, @Named("CREDENTIALS") Map<String, String> credentialsMap,
Map<String, ? extends Location> locations) {
this.instanceStateToNodeState = checkNotNull(instanceStateToNodeState,
"instanceStateToNodeState");
this.instanceStateToNodeState = checkNotNull(instanceStateToNodeState, "instanceStateToNodeState");
this.images = checkNotNull(images, "images");
this.credentialsMap = checkNotNull(credentialsMap, "credentialsMap");
this.locations = checkNotNull(locations, "locations");
@ -73,16 +69,14 @@ public class InstanceToNodeMetadata implements Function<Instance, NodeMetadata>
@Override
public NodeMetadata apply(Instance from) {
Matcher matcher = ALL_BEFORE_HYPHEN_HEX.matcher(from.getName());
final String tag = matcher.find() ? matcher.group(1) : null;
Set<String> ipSet = from.getIp() != null ? ImmutableSet.of(from.getIp()) : ImmutableSet
.<String> of();
String tag = parseTagFromName(from.getName());
Set<String> ipSet = from.getIp() != null ? ImmutableSet.of(from.getIp()) : ImmutableSet.<String> of();
NodeState state = instanceStateToNodeState.get(from.getStatus());
Image image = images.get(from.getImageId());
String key = tag != null ? credentialsMap.get(tag) : null;
return new NodeMetadataImpl(from.getId() + "", from.getName(), from.getId() + "", locations
.get(image.getLocation()), null, ImmutableMap.<String, String> of(), tag, image,
state, ipSet, ImmutableList.<String> of(), ImmutableMap.<String, String> of(),
new Credentials(image.getDefaultCredentials().identity, key));
return new NodeMetadataImpl(from.getId() + "", from.getName(), from.getId() + "", locations.get(image
.getLocation()), null, ImmutableMap.<String, String> of(), tag, image, state, ipSet, ImmutableList
.<String> of(), ImmutableMap.<String, String> of(), new Credentials(
image.getDefaultCredentials().identity, key));
}
}

View File

@ -20,12 +20,11 @@
package org.jclouds.rackspace.cloudservers.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.compute.util.ComputeServiceUtils.parseTagFromName;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import javax.inject.Inject;
@ -50,7 +49,6 @@ import com.google.common.collect.Iterables;
* @author Adrian Cole
*/
public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
public static final Pattern DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX = Pattern.compile("([^-]+)-[0-9a-f]+");
private final Location location;
private final Map<ServerStatus, NodeState> serverToNodeState;
private final Set<? extends Image> images;
@ -84,8 +82,7 @@ public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
@Override
public NodeMetadata apply(Server from) {
Matcher matcher = DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX.matcher(from.getName());
final String tag = matcher.find() ? matcher.group(1) : null;
String tag = parseTagFromName(from.getName());
Location host = new LocationImpl(LocationScope.HOST, from.getHostId(), from.getHostId(), location);
Image image = null;
try {
@ -97,4 +94,5 @@ public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
tag, image, serverToNodeState.get(from.getStatus()), from.getAddresses().getPublicAddresses(), from
.getAddresses().getPrivateAddresses(), ImmutableMap.<String, String> of(), null);
}
}

View File

@ -21,6 +21,7 @@ package org.jclouds.rimuhosting.miro.compute.config;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.compute.domain.OsFamily.UBUNTU;
import static org.jclouds.compute.util.ComputeServiceUtils.parseTagFromName;
import static org.jclouds.rimuhosting.miro.reference.RimuHostingConstants.PROPERTY_RIMUHOSTING_DEFAULT_DC;
import java.util.Map;
@ -183,7 +184,8 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule {
@Inject
protected RimuHostingAddNodeWithTagStrategy(RimuHostingClient client,
Function<Server, Iterable<String>> getPublicAddresses, Map<RunningState, NodeState> runningStateToNodeState) {
Function<Server, Iterable<String>> getPublicAddresses,
Map<RunningState, NodeState> runningStateToNodeState) {
this.client = client;
this.getPublicAddresses = getPublicAddresses;
this.runningStateToNodeState = runningStateToNodeState;
@ -307,7 +309,7 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule {
Location location = new LocationImpl(LocationScope.ZONE, from.getLocation().getId(), from.getLocation()
.getName(), null);
String tag = from.getName().replaceAll("-[0-9]+", "");
String tag = parseTagFromName(from.getName());
Credentials creds = null;
Image image = null;
@ -318,8 +320,8 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule {
}
NodeState state = runningStateToNodeState.get(from.getState());
return new NodeMetadataImpl(from.getId() + "", from.getName(), from.getId() + "", location, null, ImmutableMap
.<String, String> of(), tag, image, state, getPublicAddresses.apply(from), ImmutableList.<String> of(),
ImmutableMap.<String, String> of(), creds);
.<String, String> of(), tag, image, state, getPublicAddresses.apply(from), ImmutableList
.<String> of(), ImmutableMap.<String, String> of(), creds);
}
}
@ -381,8 +383,9 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule {
@Singleton
protected Set<? extends Size> provideSizes(RimuHostingClient sync, Set<? extends Image> images,
Set<? extends Location> locations, LogHolder holder,
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userExecutor, Function<ComputeMetadata, String> indexer)
throws InterruptedException, TimeoutException, ExecutionException {
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userExecutor,
Function<ComputeMetadata, String> indexer) throws InterruptedException, TimeoutException,
ExecutionException {
final Set<Size> sizes = Sets.newHashSet();
holder.logger.debug(">> providing sizes");
for (final PricingPlan from : sync.getPricingPlanList()) {
@ -417,7 +420,8 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule {
@Provides
@Singleton
protected Set<? extends Image> provideImages(final RimuHostingClient sync, LogHolder holder,
Function<ComputeMetadata, String> indexer) throws InterruptedException, ExecutionException, TimeoutException {
Function<ComputeMetadata, String> indexer) throws InterruptedException, ExecutionException,
TimeoutException {
final Set<Image> images = Sets.newHashSet();
holder.logger.debug(">> providing images");
for (final org.jclouds.rimuhosting.miro.domain.Image from : sync.getImageList()) {
@ -438,8 +442,8 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule {
}
images.add(new ImageImpl(from.getId(), from.getDescription(), from.getId(), null, null, ImmutableMap
.<String, String> of(), from.getDescription(), version, os, osDescription, arch, new Credentials("root",
null)));
.<String, String> of(), from.getDescription(), version, os, osDescription, arch, new Credentials(
"root", null)));
}
holder.logger.debug("<< images(%d)", images.size());
return images;

View File

@ -20,12 +20,11 @@
package org.jclouds.slicehost.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.compute.util.ComputeServiceUtils.parseTagFromName;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import javax.inject.Inject;
@ -47,7 +46,6 @@ import com.google.common.collect.Iterables;
* @author Adrian Cole
*/
public class SliceToNodeMetadata implements Function<Slice, NodeMetadata> {
public static final Pattern DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX = Pattern.compile("([^-]+)-[0-9a-f]+");
private final Location location;
private final Map<Slice.Status, NodeState> sliceToNodeState;
private final Set<? extends Image> images;
@ -78,8 +76,7 @@ public class SliceToNodeMetadata implements Function<Slice, NodeMetadata> {
@Override
public NodeMetadata apply(Slice from) {
Matcher matcher = DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX.matcher(from.getName());
final String tag = matcher.find() ? matcher.group(1) : null;
String tag = parseTagFromName(from.getName());
Image image = null;
try {
image = Iterables.find(images, new FindImageForSlice(from));

View File

@ -20,12 +20,11 @@
package org.jclouds.vcloud.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.compute.util.ComputeServiceUtils.parseTagFromName;
import java.net.URI;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import javax.inject.Inject;
@ -49,8 +48,8 @@ import org.jclouds.vcloud.domain.VAppStatus;
import com.google.common.collect.ImmutableMap;
/**
* Configures the {@link VCloudComputeServiceContext}; requires
* {@link BaseVCloudComputeClient} bound.
* Configures the {@link VCloudComputeServiceContext}; requires {@link BaseVCloudComputeClient}
* bound.
*
* @author Adrian Cole
*/
@ -67,8 +66,6 @@ public class VCloudGetNodeMetadata {
protected final GetExtra getExtra;
protected final Map<VAppStatus, NodeState> vAppStatusToNodeState;
public static final Pattern TAG_PATTERN_WITHOUT_TEMPLATE = Pattern.compile("([^-]+)-[0-9a-f]+");
@Inject
VCloudGetNodeMetadata(VCloudClient client, VCloudComputeClient computeClient,
Map<VAppStatus, NodeState> vAppStatusToNodeState, GetExtra getExtra,
@ -83,22 +80,14 @@ public class VCloudGetNodeMetadata {
public NodeMetadata execute(String in) {
URI id = URI.create(in);
VApp vApp = client.getVApp(id);
if (vApp == null)
VApp from = client.getVApp(id);
if (from == null)
return null;
String tag = parseTagFromName(from.getName());
Location location = findLocationForResourceInVDC.apply(from.getVDC());
return new NodeMetadataImpl(in, from.getName(), in, location, from.getId(), ImmutableMap.<String, String> of(),
tag, null, vAppStatusToNodeState.get(from.getStatus()), computeClient.getPublicAddresses(id),
computeClient.getPrivateAddresses(id), getExtra.apply(from), null);
}
String tag = null;
Image image = null;
Matcher matcher = TAG_PATTERN_WITHOUT_TEMPLATE.matcher(vApp.getName());
if (matcher.find()) {
tag = matcher.group(1);
} else {
tag = "NOTAG-" + vApp.getName();
}
Location location = findLocationForResourceInVDC.apply(vApp.getVDC());
return new NodeMetadataImpl(vApp.getId().toASCIIString(), vApp.getName(), vApp.getId()
.toASCIIString(), location, vApp.getId(), ImmutableMap.<String, String> of(), tag, image,
vAppStatusToNodeState.get(vApp.getStatus()), computeClient.getPublicAddresses(id), computeClient
.getPrivateAddresses(id), getExtra.apply(vApp), null);
}
}