Permalink
Browse files

[WHIRR-717] Use of the context name in the dynamic compute cache. Ali…

…gn karaf commands.
  • Loading branch information...
iocanel committed Apr 12, 2013
1 parent b98d885 commit 14c45ff1efacef3a025d1d8bc6a731da9d0db907
@@ -95,7 +95,9 @@
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 @@
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 static ClusterSpec withNoDefaults(Configuration conf)
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 ClusterSpec(Configuration userConfig, boolean loadDefaults, Map<String,No
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 ClusterSpec copy() throws ConfigurationException {
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 int getMaxStartupRetries() {
return maxStartupRetries;
}
+ public String getContextName() {
+ return contextName;
+ }
+
public String getProvider() {
return provider;
}
@@ -584,6 +599,10 @@ public String getClusterName() {
return clusterName;
}
+ public String getBlobStoreContextName() {
+ return blobStoreContextName;
+ }
+
public String getBlobStoreProvider() {
if (blobStoreProvider == null) {
return getDefaultBlobStoreForComputeProvider();
@@ -720,6 +739,10 @@ public void setMaxStartupRetries(int maxStartupRetries) {
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 void setCredential(String credential) {
this.credential = credential;
}
+ public void setBlobStoreContextName(String blobStoreContextName) {
+ this.blobStoreContextName = blobStoreContextName;
+ }
+
public void setBlobStoreProvider(String provider) {
blobStoreProvider = provider;
}
@@ -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 @@
* 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());
}
}
}
@@ -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}"
@@ -28,11 +28,22 @@
@Override
protected Object doExecute() throws Exception {
+ validateInput();
DestroyClusterCommand command = new DestroyClusterCommand(clusterControllerFactory);
ClusterSpec clusterSpec = getClusterSpec();
if (clusterSpec != null) {
command.run(System.in, System.out, System.err, clusterSpec);
}
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.");
+ }
+ }
+ }
}
@@ -28,11 +28,22 @@
@Override
protected Object doExecute() throws Exception {
+ validateInput();
ListClusterCommand command = new ListClusterCommand(clusterControllerFactory);
ClusterSpec clusterSpec = getClusterSpec();
if (clusterSpec != null) {
command.run(System.in, System.out, System.err, clusterSpec);
}
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.");
+ }
+ }
+ }
}
@@ -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 @@
@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,31 +62,54 @@
@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");
}
}
}
- 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 @@ protected ClusterSpec getClusterSpec() throws Exception {
clusterSpec = new ClusterSpec();
}
-
+ if (name != null) {
+ clusterSpec.setContextName(name);
+ }
if (provider != null) {
clusterSpec.setProvider(provider);
Oops, something went wrong.

0 comments on commit 14c45ff

Please sign in to comment.