Updated Branches: refs/heads/trunk b98d88565 -> 14c45ff1e
[WHIRR-717] Use of the context name in the dynamic compute cache. Align karaf commands. Project: http://git-wip-us.apache.org/repos/asf/whirr/repo Commit: http://git-wip-us.apache.org/repos/asf/whirr/commit/14c45ff1 Tree: http://git-wip-us.apache.org/repos/asf/whirr/tree/14c45ff1 Diff: http://git-wip-us.apache.org/repos/asf/whirr/diff/14c45ff1 Branch: refs/heads/trunk Commit: 14c45ff1efacef3a025d1d8bc6a731da9d0db907 Parents: b98d885 Author: Ioannis Canellos <ioca...@apache.org> Authored: Fri Apr 12 23:27:56 2013 +0300 Committer: Ioannis Canellos <ioca...@apache.org> Committed: Fri Apr 12 23:27:56 2013 +0300 ---------------------------------------------------------------------- .../main/java/org/apache/whirr/ClusterSpec.java | 31 +++++++- .../apache/whirr/service/DynamicComputeCache.java | 65 +++------------ platforms/karaf/commands/pom.xml | 5 + .../apache/whirr/karaf/command/DestroyCluster.java | 11 +++ .../apache/whirr/karaf/command/ListCluster.java | 11 +++ .../karaf/command/support/WhirrCommandSupport.java | 43 ++++++++-- .../resources/OSGI-INF/blueprint/blueprint.xml | 23 +++++- platforms/karaf/feature/pom.xml | 1 + 8 files changed, 126 insertions(+), 64 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/whirr/blob/14c45ff1/core/src/main/java/org/apache/whirr/ClusterSpec.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/whirr/ClusterSpec.java b/core/src/main/java/org/apache/whirr/ClusterSpec.java index 4374f47..8ca973a 100644 --- a/core/src/main/java/org/apache/whirr/ClusterSpec.java +++ b/core/src/main/java/org/apache/whirr/ClusterSpec.java @@ -95,7 +95,9 @@ public class ClusterSpec { MAX_STARTUP_RETRIES(Integer.class, false, "The number of retries in case of insufficient " + "successfully started instances. Default value is 1."), - + + CONTEXT_NAME(String.class, false, "The name of an existing compute service context. "), + PROVIDER(String.class, false, "The name of the cloud provider. " + "E.g. aws-ec2, cloudservers-uk"), @@ -113,6 +115,9 @@ public class ClusterSpec { PRIVATE_KEY_FILE(String.class, false, "The filename of the " + "private RSA key used to connect to instances."), + + BLOBSTORE_CONTEXT_NAME(String.class, false, "The name of an existing blob store context. "), + BLOBSTORE_PROVIDER(String.class, false, "The blob store provider. " + "E.g. aws-s3, cloudfiles-us, cloudfiles-uk"), @@ -274,11 +279,13 @@ public class ClusterSpec { private List<InstanceTemplate> instanceTemplates; private int maxStartupRetries; + private String contextName; private String provider; private String endpoint; private String identity; private String credential; + private String blobStoreContextName; private String blobStoreProvider; private String blobStoreIdentity; private String blobStoreEndpoint; @@ -360,12 +367,14 @@ public class ClusterSpec { setJdkInstallUrl(getString(Property.JDK_INSTALL_URL)); setKerberosRealm(getString(Property.KERBEROS_REALM)); - + + setContextName(getString(Property.CONTEXT_NAME)); setProvider(getString(Property.PROVIDER)); setEndpoint(getString(Property.ENDPOINT)); setIdentity(getString(Property.IDENTITY)); setCredential(getString(Property.CREDENTIAL)); + setBlobStoreContextName(getString(Property.BLOBSTORE_CONTEXT_NAME)); setBlobStoreProvider(getString(Property.BLOBSTORE_PROVIDER)); setBlobStoreEndpoint(getString(Property.BLOBSTORE_ENDPOINT)); setBlobStoreIdentity(getString(Property.BLOBSTORE_IDENTITY)); @@ -427,10 +436,12 @@ public class ClusterSpec { r.setInstanceTemplates(Lists.newLinkedList(getInstanceTemplates())); r.setMaxStartupRetries(getMaxStartupRetries()); + r.setContextName(getContextName()); r.setProvider(getProvider()); r.setIdentity(getIdentity()); r.setCredential(getCredential()); + r.setBlobStoreContextName(getBlobStoreContextName()); r.setBlobStoreProvider(getBlobStoreProvider()); r.setBlobStoreIdentity(getBlobStoreIdentity()); r.setBlobStoreCredential(getBlobStoreCredential()); @@ -555,6 +566,10 @@ public class ClusterSpec { return maxStartupRetries; } + public String getContextName() { + return contextName; + } + public String getProvider() { return provider; } @@ -584,6 +599,10 @@ public class ClusterSpec { return clusterName; } + public String getBlobStoreContextName() { + return blobStoreContextName; + } + public String getBlobStoreProvider() { if (blobStoreProvider == null) { return getDefaultBlobStoreForComputeProvider(); @@ -720,6 +739,10 @@ public class ClusterSpec { this.maxStartupRetries = maxStartupRetries; } + public void setContextName(String contextName) { + this.contextName = contextName; + } + public void setProvider(String provider) { if ("ec2".equals(provider)) { LOG.warn("Please use provider \"aws-ec2\" instead of \"ec2\""); @@ -757,6 +780,10 @@ public class ClusterSpec { this.credential = credential; } + public void setBlobStoreContextName(String blobStoreContextName) { + this.blobStoreContextName = blobStoreContextName; + } + public void setBlobStoreProvider(String provider) { blobStoreProvider = provider; } http://git-wip-us.apache.org/repos/asf/whirr/blob/14c45ff1/core/src/main/java/org/apache/whirr/service/DynamicComputeCache.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/whirr/service/DynamicComputeCache.java b/core/src/main/java/org/apache/whirr/service/DynamicComputeCache.java index ec9381c..027160f 100644 --- a/core/src/main/java/org/apache/whirr/service/DynamicComputeCache.java +++ b/core/src/main/java/org/apache/whirr/service/DynamicComputeCache.java @@ -19,12 +19,10 @@ package org.apache.whirr.service; import com.google.common.base.Function; -import com.google.common.base.Objects; +import com.google.common.base.Strings; import org.apache.whirr.ClusterSpec; import org.jclouds.compute.ComputeService; import org.jclouds.compute.ComputeServiceContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; @@ -33,67 +31,28 @@ import java.util.Map; * A class for adding {@link ComputeServiceContext} on runtime. */ public class DynamicComputeCache implements Function<ClusterSpec, ComputeServiceContext> { - - private static final Logger LOG = LoggerFactory.getLogger(DynamicComputeCache.class); - private final Map<Key, ComputeServiceContext> serviceContextMap = new HashMap<Key, ComputeServiceContext>(); + private final Map<String, ComputeServiceContext> serviceContextMap = new HashMap<String, ComputeServiceContext>(); @Override public ComputeServiceContext apply(ClusterSpec arg0) { - return serviceContextMap.get(new Key(arg0)); - } - - public void bind(ComputeService computeService) { - if (computeService != null) { - serviceContextMap.put(new Key(computeService), computeService.getContext()); + String name = arg0.getContextName(); + if (!Strings.isNullOrEmpty(name)) { + return serviceContextMap.get(name); + } else { + return ComputeCache.INSTANCE.apply(arg0); } } - public void unbind(ComputeService computeService) { + public synchronized void bind(ComputeService computeService) { if (computeService != null) { - serviceContextMap.remove(new Key(computeService)); + serviceContextMap.put(computeService.getContext().unwrap().getName(), computeService.getContext()); } } - /** - * Key class for the compute context cache - */ - private static class Key { - private String provider; - private final String key; - - public Key(ClusterSpec spec) { - provider = spec.getProvider(); - - key = Objects.toStringHelper("").omitNullValues() - .add("provider", provider).toString(); - } - - public Key(ComputeService computeService) { - provider = computeService.getContext().unwrap().getId(); - - key = Objects.toStringHelper("").omitNullValues() - .add("provider", provider).toString(); - } - - @Override - public boolean equals(Object that) { - if (that instanceof Key) { - return Objects.equal(this.key, ((Key)that).key); - } - return false; - } - - @Override - public int hashCode() { - return Objects.hashCode(key); - } - - @Override - public String toString() { - return Objects.toStringHelper(this) - .add("provider", provider) - .toString(); + public synchronized void unbind(ComputeService computeService) { + if (computeService != null) { + serviceContextMap.remove(computeService.getContext().unwrap().getName()); } } } http://git-wip-us.apache.org/repos/asf/whirr/blob/14c45ff1/platforms/karaf/commands/pom.xml ---------------------------------------------------------------------- diff --git a/platforms/karaf/commands/pom.xml b/platforms/karaf/commands/pom.xml index 2b6e8b0..62ee86e 100644 --- a/platforms/karaf/commands/pom.xml +++ b/platforms/karaf/commands/pom.xml @@ -36,6 +36,11 @@ <osgi.import> !org.apache.whirr.karaf.command*, org.apache.commons.configuration*;version="[1.6,2)", + org.apache.felix.service.command, + org.apache.felix.gogo.commands, + org.apache.karaf.shell.console;version="[2.2,4)", + org.apache.karaf.shell.console.commands;version="[2.2,4)", + org.apache.karaf.shell.console.completer;version="[2.2,4)", * </osgi.import> <osgi.export>org.apache.whirr.karaf.command*;version="${project.version}" http://git-wip-us.apache.org/repos/asf/whirr/blob/14c45ff1/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/DestroyCluster.java ---------------------------------------------------------------------- diff --git a/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/DestroyCluster.java b/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/DestroyCluster.java index a6a96d8..87a6c2a 100644 --- a/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/DestroyCluster.java +++ b/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/DestroyCluster.java @@ -28,6 +28,7 @@ public class DestroyCluster extends WhirrCommandSupport { @Override protected Object doExecute() throws Exception { + validateInput(); DestroyClusterCommand command = new DestroyClusterCommand(clusterControllerFactory); ClusterSpec clusterSpec = getClusterSpec(); if (clusterSpec != null) { @@ -35,4 +36,14 @@ public class DestroyCluster extends WhirrCommandSupport { } return null; } + + public void validateInput() throws Exception { + if (pid != null || fileName != null) { + return; + } else { + if ((name == null || getComputeServiceByName(name) == null) && (provider == null || getComputeServiceByProvider(provider) == null)) { + throw new Exception("A proper configuration or a valid compute name / provider should be provided."); + } + } + } } http://git-wip-us.apache.org/repos/asf/whirr/blob/14c45ff1/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/ListCluster.java ---------------------------------------------------------------------- diff --git a/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/ListCluster.java b/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/ListCluster.java index 11531a2..2933dbf 100644 --- a/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/ListCluster.java +++ b/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/ListCluster.java @@ -28,6 +28,7 @@ public class ListCluster extends WhirrCommandSupport { @Override protected Object doExecute() throws Exception { + validateInput(); ListClusterCommand command = new ListClusterCommand(clusterControllerFactory); ClusterSpec clusterSpec = getClusterSpec(); if (clusterSpec != null) { @@ -35,4 +36,14 @@ public class ListCluster extends WhirrCommandSupport { } return null; } + + public void validateInput() throws Exception { + if (pid != null || fileName != null) { + return; + } else { + if ((name == null || getComputeServiceByName(name) == null) && (provider == null || getComputeServiceByProvider(provider) == null)) { + throw new Exception("A proper configuration or a valid compute name / provider should be provided."); + } + } + } } http://git-wip-us.apache.org/repos/asf/whirr/blob/14c45ff1/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/support/WhirrCommandSupport.java ---------------------------------------------------------------------- diff --git a/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/support/WhirrCommandSupport.java b/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/support/WhirrCommandSupport.java index 697e7f9..8e0ee85 100644 --- a/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/support/WhirrCommandSupport.java +++ b/platforms/karaf/commands/src/main/java/org/apache/whirr/karaf/command/support/WhirrCommandSupport.java @@ -19,6 +19,7 @@ package org.apache.whirr.karaf.command.support; import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.felix.gogo.commands.Argument; import org.apache.felix.gogo.commands.Option; import org.apache.karaf.shell.console.OsgiCommandSupport; import org.apache.whirr.ClusterControllerFactory; @@ -40,6 +41,9 @@ public abstract class WhirrCommandSupport extends OsgiCommandSupport { @Option(required = false, name = "--pid", description = "The PID of the configuration") protected String pid; + @Option(required = false, name = "--name", description = "The compute context name") + protected String name; + @Option(required = false, name = "--provider", description = "The compute provider") protected String provider; @@ -58,23 +62,25 @@ public abstract class WhirrCommandSupport extends OsgiCommandSupport { @Option(required = false, name = "--hardwareId", description = "The hardware") protected String hardwareId; - @Option(required = false, name = "--cluster-name", description = "The name of the cluster") - protected String clusterName; - @Option(required = false, name = "--private-key-file", description = "The private ssh key") protected String privateKey; + @Argument(name = "clusterName", index = 0 ,required = true, multiValued = false, description = "The name of the cluster") + protected String clusterName; + + protected ClusterControllerFactory clusterControllerFactory; protected ConfigurationAdmin configurationAdmin; protected List<ComputeService> computeServices; public void validateInput() throws Exception { + if (pid != null || fileName != null) { return; } else { - if (provider == null || getComputeService(provider) == null) { - throw new Exception("A proper configuration or a valid provider should be provided."); + if ((name == null || getComputeServiceByName(name) == null) && (provider == null || getComputeServiceByProvider(provider) == null)) { + throw new Exception("A proper configuration or a valid compute name / provider should be provided."); } if (templates == null) { throw new Exception("A proper configuration or a valid template should be specified"); @@ -82,7 +88,28 @@ public abstract class WhirrCommandSupport extends OsgiCommandSupport { } } - public ComputeService getComputeService(String provider) { + /** + * Returns the first {@link ComputeService} that matches the name. + * @param name + * @return + */ + public ComputeService getComputeServiceByName(String name) { + if (computeServices != null && !computeServices.isEmpty()) { + for (ComputeService computeService : computeServices) { + if (computeService.getContext().unwrap().getName().equals(name)) { + return computeService; + } + } + } + return null; + } + + /** + * Returns the first {@link ComputeService} that matches the provider. + * @param provider + * @return + */ + public ComputeService getComputeServiceByProvider(String provider) { if (computeServices != null && !computeServices.isEmpty()) { for (ComputeService computeService : computeServices) { if (computeService.getContext().unwrap().getId().equals(provider)) { @@ -108,7 +135,9 @@ public abstract class WhirrCommandSupport extends OsgiCommandSupport { clusterSpec = new ClusterSpec(); } - + if (name != null) { + clusterSpec.setContextName(name); + } if (provider != null) { clusterSpec.setProvider(provider); http://git-wip-us.apache.org/repos/asf/whirr/blob/14c45ff1/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml ---------------------------------------------------------------------- diff --git a/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml index 4c1f0d0..255605e 100644 --- a/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -17,7 +17,8 @@ See the License for the specific language governing permissions and limitations under the License. --> -<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> +<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" + xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"> <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0"> <command name="whirr/launch-cluster"> @@ -27,6 +28,7 @@ <property name="computeServices" ref="computeServices"/> </action> <optional-completers> + <entry key="--name" value-ref="nameCompleter"/> <entry key="--provider" value-ref="computeProviderCompleter"/> <entry key="--imageId" value-ref="imageCompleter"/> <entry key="--locationId" value-ref="locationCompleter"/> @@ -40,6 +42,10 @@ <property name="configurationAdmin" ref="configurationAdmin"/> <property name="computeServices" ref="computeServices"/> </action> + <optional-completers> + <entry key="--name" value-ref="nameCompleter"/> + <entry key="--provider" value-ref="computeProviderCompleter"/> + </optional-completers> </command> <command name="whirr/destroy-cluster"> <action class="org.apache.whirr.karaf.command.DestroyCluster"> @@ -47,6 +53,10 @@ <property name="configurationAdmin" ref="configurationAdmin"/> <property name="computeServices" ref="computeServices"/> </action> + <optional-completers> + <entry key="--name" value-ref="nameCompleter"/> + <entry key="--provider" value-ref="computeProviderCompleter"/> + </optional-completers> </command> <command name="whirr/destroy-instance"> <action class="org.apache.whirr.karaf.command.DestroyInstance"> @@ -54,6 +64,10 @@ <property name="configurationAdmin" ref="configurationAdmin"/> <property name="computeServices" ref="computeServices"/> </action> + <optional-completers> + <entry key="--name" value-ref="nameCompleter"/> + <entry key="--provider" value-ref="computeProviderCompleter"/> + </optional-completers> </command> <command name="whirr/run-script"> <action class="org.apache.whirr.karaf.command.RunScript"> @@ -61,6 +75,10 @@ <property name="configurationAdmin" ref="configurationAdmin"/> <property name="computeServices" ref="computeServices"/> </action> + <optional-completers> + <entry key="--name" value-ref="nameCompleter"/> + <entry key="--provider" value-ref="computeProviderCompleter"/> + </optional-completers> </command> </command-bundle> @@ -72,12 +90,13 @@ <property name="roleCompleter" ref="roleCompleter"/> </bean> + <reference id="nameCompleter" interface="org.apache.karaf.shell.console.Completer" filter="(completer-type=compute-service-id)"/> <reference id="imageCompleter" interface="org.apache.karaf.shell.console.Completer" filter="(completer-type=image)"/> <reference id="locationCompleter" interface="org.apache.karaf.shell.console.Completer" filter="(completer-type=location)"/> <reference id="hardwareCompleter" interface="org.apache.karaf.shell.console.Completer" filter="(completer-type=hardware)"/> <reference id="computeProviderCompleter" interface="org.apache.karaf.shell.console.Completer" filter="(completer-type=compute-provider)"/> - <reference id="clusterControllerFactory" interface="org.apache.whirr.ClusterControllerFactory"/> + <reference id="clusterControllerFactory" ext:proxy-method="classes" interface="org.apache.whirr.ClusterControllerFactory"/> <reference id="configurationAdmin" interface="org.osgi.service.cm.ConfigurationAdmin"/> <reference-list id="computeServices" interface="org.jclouds.compute.ComputeService" availability="optional"/> http://git-wip-us.apache.org/repos/asf/whirr/blob/14c45ff1/platforms/karaf/feature/pom.xml ---------------------------------------------------------------------- diff --git a/platforms/karaf/feature/pom.xml b/platforms/karaf/feature/pom.xml index 77e204e..2f5015d 100644 --- a/platforms/karaf/feature/pom.xml +++ b/platforms/karaf/feature/pom.xml @@ -31,6 +31,7 @@ <artifactId>apache-whirr</artifactId> <packaging>pom</packaging> <name>Apache Whirr Karaf Feature</name> + <properties> <activation.spec.version>1.1</activation.spec.version> <ant.bundle.version>1.7.0_5</ant.bundle.version>