Author: asavu
Date: Mon Nov 14 19:37:45 2011
New Revision: 1201846

URL: http://svn.apache.org/viewvc?rev=1201846&view=rev
Log:
WHIRR-416. Enable lazy image fetching when the image-id is known (asavu)

Modified:
    whirr/trunk/CHANGES.txt
    whirr/trunk/core/src/main/java/org/apache/whirr/ClusterSpec.java
    whirr/trunk/core/src/main/java/org/apache/whirr/service/ComputeCache.java

Modified: whirr/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/whirr/trunk/CHANGES.txt?rev=1201846&r1=1201845&r2=1201846&view=diff
==============================================================================
--- whirr/trunk/CHANGES.txt (original)
+++ whirr/trunk/CHANGES.txt Mon Nov 14 19:37:45 2011
@@ -80,6 +80,8 @@ Trunk (unreleased changes)
     WHIRR-414. Wirr can have a non-zero return code and unterminated 
     (orphaned) host instances (David Alves and asavu)
 
+    WHIRR-416. Enable lazy image fetching when the image-id is known (asavu)
+
   BUG FIXES
 
     WHIRR-377. Fix broken CLI logging config. (asavu via tomwhite)

Modified: whirr/trunk/core/src/main/java/org/apache/whirr/ClusterSpec.java
URL: 
http://svn.apache.org/viewvc/whirr/trunk/core/src/main/java/org/apache/whirr/ClusterSpec.java?rev=1201846&r1=1201845&r2=1201846&view=diff
==============================================================================
--- whirr/trunk/core/src/main/java/org/apache/whirr/ClusterSpec.java (original)
+++ whirr/trunk/core/src/main/java/org/apache/whirr/ClusterSpec.java Mon Nov 14 
19:37:45 2011
@@ -33,6 +33,7 @@ import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import com.google.common.annotations.VisibleForTesting;
 import org.apache.commons.configuration.CompositeConfiguration;
 import org.apache.commons.configuration.Configuration;
 import org.apache.commons.configuration.ConfigurationException;
@@ -43,7 +44,6 @@ import org.jclouds.predicates.validators
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Objects;
 import com.google.common.collect.Iterators;
 import com.google.common.collect.Lists;

Modified: 
whirr/trunk/core/src/main/java/org/apache/whirr/service/ComputeCache.java
URL: 
http://svn.apache.org/viewvc/whirr/trunk/core/src/main/java/org/apache/whirr/service/ComputeCache.java?rev=1201846&r1=1201845&r2=1201846&view=diff
==============================================================================
--- whirr/trunk/core/src/main/java/org/apache/whirr/service/ComputeCache.java 
(original)
+++ whirr/trunk/core/src/main/java/org/apache/whirr/service/ComputeCache.java 
Mon Nov 14 19:37:45 2011
@@ -21,7 +21,6 @@ package org.apache.whirr.service;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Function;
 import com.google.common.base.Objects;
-import com.google.common.base.Throwables;
 import com.google.common.collect.ForwardingObject;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
@@ -33,6 +32,7 @@ import java.util.Properties;
 
 import org.apache.commons.configuration.Configuration;
 import org.apache.commons.configuration.ConfigurationConverter;
+import org.apache.commons.lang.StringUtils;
 import org.apache.whirr.ClusterSpec;
 import org.jclouds.compute.ComputeService;
 import org.jclouds.compute.ComputeServiceContext;
@@ -46,13 +46,17 @@ import org.jclouds.rest.RestContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import static 
org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_AMI_QUERY;
+import static 
org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_CC_AMI_QUERY;
+import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGION;
+
 /**
  * A convenience class for building jclouds {@link ComputeServiceContext} 
objects.
  */
 // singleton enum pattern
 public enum ComputeCache implements Function<ClusterSpec, 
ComputeServiceContext> {
    
-
   INSTANCE;
   
   private static final Logger LOG = 
LoggerFactory.getLogger(ComputeCache.class);
@@ -70,20 +74,10 @@ public enum ComputeCache implements Func
                
         @Override
         public ComputeServiceContext apply(Key arg0) {
-          Properties overrides = new Properties();
-          overrides.putAll(arg0.overrides);
-          if (arg0.provider.equals("stub")) {
-            try {
-              overrides.setProperty("jclouds.modules", 
-                SLF4JLoggingModule.class.getName() + 
",org.apache.whirr.service.DryRunModule");
-            } catch (Exception e) {
-              Throwables.propagate(e);
-            }
-          }
           LOG.debug("creating new ComputeServiceContext {}", arg0);
           ComputeServiceContext context = new 
IgnoreCloseComputeServiceContext(factory.createContext(
             arg0.provider, arg0.identity, arg0.credential,
-            ImmutableSet.<Module>of(), overrides));
+            ImmutableSet.<Module>of(), arg0.overrides));
           LOG.debug("created new ComputeServiceContext {}", context);
           return context;
         }
@@ -91,7 +85,8 @@ public enum ComputeCache implements Func
     }
   );
    
-  private static class IgnoreCloseComputeServiceContext extends 
ForwardingObject implements ComputeServiceContext {
+  private static class IgnoreCloseComputeServiceContext
+    extends ForwardingObject implements ComputeServiceContext {
 
     private final ComputeServiceContext context;
 
@@ -152,8 +147,10 @@ public enum ComputeCache implements Func
    * All APIs that are independently configurable.
    * @see <a href="http://code.google.com/p/jclouds/issues/detail?id=657"; />
    */
-  public static final Iterable<String> COMPUTE_APIS = ImmutableSet.of("stub", 
"nova", "vcloud", "elasticstack",
-      "eucalyptus", "deltacloud", "byon");
+  public static final Iterable<String> COMPUTE_APIS = ImmutableSet.of(
+      "stub", "nova", "vcloud", "elasticstack",
+      "eucalyptus", "deltacloud", "byon"
+  );
 
   /**
    *  jclouds providers and apis that can be used in 
ComputeServiceContextFactory
@@ -171,8 +168,8 @@ public enum ComputeCache implements Func
   /**
    * configurable properties, scoped to a provider.
    */
-  public static final Iterable<String> PROVIDER_PROPERTIES = 
ImmutableSet.of("endpoint", "api", "apiversion",
-      "iso3166-codes");
+  public static final Iterable<String> PROVIDER_PROPERTIES = ImmutableSet.of(
+    "endpoint", "api", "apiversion", "iso3166-codes");
 
   /**
    * Key class for the compute context cache
@@ -181,6 +178,7 @@ public enum ComputeCache implements Func
     private String provider;
     private String identity;
     private String credential;
+
     private final String key;
     private final Properties overrides;
 
@@ -188,6 +186,7 @@ public enum ComputeCache implements Func
       provider = spec.getProvider();
       identity = spec.getIdentity();
       credential = spec.getCredential();
+
       key = String.format("%s-%s-%s", provider, identity, credential);
       Configuration jcloudsConfig = 
spec.getConfigurationForKeysWithPrefix("jclouds");
       
@@ -195,12 +194,40 @@ public enum ComputeCache implements Func
       for (String key : COMPUTE_KEYS) {
         for (String property : PROVIDER_PROPERTIES) {
           String prefixedProperty = "jclouds." + key + "." + property;
-          if (jcloudsConfig.containsKey(prefixedProperty))
+
+          if (jcloudsConfig.containsKey(prefixedProperty)) {
             jcloudsConfig.setProperty(key + "." + property, 
                 jcloudsConfig.getProperty(prefixedProperty));
+          }
         }
       }
       overrides = ConfigurationConverter.getProperties(jcloudsConfig);
+
+      if ("aws-ec2".equals(spec.getProvider()) && spec.getImageId() != null) {
+        enableAWSEC2LazyImageFetching(spec);
+      }
+
+      if ("stub".equals(spec.getProvider())) {
+        overrides.setProperty("jclouds.modules",
+          SLF4JLoggingModule.class.getName() + 
",org.apache.whirr.service.DryRunModule"
+        );
+      }
+    }
+
+    /**
+     * AWS EC2 specific optimisation to avoid running queries across
+     * all regiosn when the image-id is know from the property file
+     *
+     * @param spec
+     */
+    private void enableAWSEC2LazyImageFetching(ClusterSpec spec) {
+      overrides.setProperty(PROPERTY_EC2_AMI_QUERY, "");
+      overrides.setProperty(PROPERTY_EC2_CC_AMI_QUERY, "");
+
+      String[] parts = StringUtils.split(spec.getImageId(), '/');
+      checkArgument(parts.length == 2, "Expected to find image-id = 
region/ami-id");
+
+      overrides.setProperty(PROPERTY_REGION, parts[0]);
     }
 
     @Override
@@ -219,8 +246,11 @@ public enum ComputeCache implements Func
     
     @Override
     public String toString() {
-      return Objects.toStringHelper(this).add("provider", 
provider).add("identity", identity)
-          .add("overrides", overrides).toString();
+      return Objects.toStringHelper(this)
+        .add("provider", provider)
+        .add("identity", identity)
+        .add("overrides", overrides)
+        .toString();
     }
   }
 


Reply via email to