You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2015/01/11 15:50:19 UTC

karaf-cellar git commit: [KARAF-1157] Improve the cluster:sync command

Repository: karaf-cellar
Updated Branches:
  refs/heads/master cc915f521 -> 60a24ca29


[KARAF-1157] Improve the cluster:sync command


Project: http://git-wip-us.apache.org/repos/asf/karaf-cellar/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf-cellar/commit/60a24ca2
Tree: http://git-wip-us.apache.org/repos/asf/karaf-cellar/tree/60a24ca2
Diff: http://git-wip-us.apache.org/repos/asf/karaf-cellar/diff/60a24ca2

Branch: refs/heads/master
Commit: 60a24ca29b1b897fc37e1e23a0e4d0e1d0f87316
Parents: cc915f5
Author: Jean-Baptiste Onofré <jb...@apache.org>
Authored: Sun Jan 11 15:49:51 2015 +0100
Committer: Jean-Baptiste Onofré <jb...@apache.org>
Committed: Sun Jan 11 15:49:51 2015 +0100

----------------------------------------------------------------------
 assembly/src/main/resources/groups.cfg          |   2 -
 .../resources/OSGI-INF/blueprint/blueprint.xml  |   6 +-
 .../resources/OSGI-INF/blueprint/blueprint.xml  |   6 +-
 .../resources/OSGI-INF/blueprint/blueprint.xml  |   6 +-
 .../resources/OSGI-INF/blueprint/blueprint.xml  |   6 +-
 shell/pom.xml                                   |   5 +
 .../apache/karaf/cellar/shell/SyncCommand.java  | 131 ++++++++++++++++---
 .../OSGI-INF/blueprint/shell-cluster.xml        |   2 +
 8 files changed, 142 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/60a24ca2/assembly/src/main/resources/groups.cfg
----------------------------------------------------------------------
diff --git a/assembly/src/main/resources/groups.cfg b/assembly/src/main/resources/groups.cfg
index 475cf27..b538ad1 100644
--- a/assembly/src/main/resources/groups.cfg
+++ b/assembly/src/main/resources/groups.cfg
@@ -49,6 +49,4 @@ default.feature.blacklist.outbound = config,management,hazelcast,cellar*
 default.bundle.sync = cluster
 default.config.sync = cluster
 default.feature.sync = cluster
-default.feature.repositories.sync = cluster
 default.obr.urls.sync = cluster
-default.obr.bundles.sync = cluster

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/60a24ca2/bundle/src/main/resources/OSGI-INF/blueprint/blueprint.xml
----------------------------------------------------------------------
diff --git a/bundle/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/bundle/src/main/resources/OSGI-INF/blueprint/blueprint.xml
index 20ab5a5..0742ab5 100644
--- a/bundle/src/main/resources/OSGI-INF/blueprint/blueprint.xml
+++ b/bundle/src/main/resources/OSGI-INF/blueprint/blueprint.xml
@@ -37,7 +37,11 @@
         <property name="bundleContext" ref="blueprintBundleContext"/>
         <property name="eventProducer" ref="eventProducer"/>
     </bean>
-    <service ref="synchronizer" interface="org.apache.karaf.cellar.core.Synchronizer"/>
+    <service ref="synchronizer" interface="org.apache.karaf.cellar.core.Synchronizer">
+        <service-properties>
+            <entry key="resource" value="bundle"/>
+        </service-properties>
+    </service>
 
     <!-- Cluster Bundle Event Handler -->
     <bean id="eventHandler" class="org.apache.karaf.cellar.bundle.BundleEventHandler"

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/60a24ca2/config/src/main/resources/OSGI-INF/blueprint/blueprint.xml
----------------------------------------------------------------------
diff --git a/config/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/config/src/main/resources/OSGI-INF/blueprint/blueprint.xml
index f2c0b95..de653da 100644
--- a/config/src/main/resources/OSGI-INF/blueprint/blueprint.xml
+++ b/config/src/main/resources/OSGI-INF/blueprint/blueprint.xml
@@ -46,7 +46,11 @@
         <property name="eventProducer" ref="eventProducer"/>
         <property name="storage" value="${storage}"/>
     </bean>
-    <service ref="synchronizer" interface="org.apache.karaf.cellar.core.Synchronizer"/>
+    <service ref="synchronizer" interface="org.apache.karaf.cellar.core.Synchronizer">
+        <service-properties>
+            <entry key="resource" value="config"/>
+        </service-properties>
+    </service>
 
     <!-- Cluster Event Handler -->
     <bean id="eventHandler" class="org.apache.karaf.cellar.config.ConfigurationEventHandler"

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/60a24ca2/features/src/main/resources/OSGI-INF/blueprint/blueprint.xml
----------------------------------------------------------------------
diff --git a/features/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/features/src/main/resources/OSGI-INF/blueprint/blueprint.xml
index 3556bcd..4f30536 100644
--- a/features/src/main/resources/OSGI-INF/blueprint/blueprint.xml
+++ b/features/src/main/resources/OSGI-INF/blueprint/blueprint.xml
@@ -36,7 +36,11 @@
         <property name="configurationAdmin" ref="configurationAdmin"/>
         <property name="featuresService" ref="featuresService"/>
     </bean>
-    <service ref="synchronizer" interface="org.apache.karaf.cellar.core.Synchronizer"/>
+    <service ref="synchronizer" interface="org.apache.karaf.cellar.core.Synchronizer">
+        <service-properties>
+            <entry key="resource" value="feature"/>
+        </service-properties>
+    </service>
 
     <!-- Cluster Features Event Handler -->
     <bean id="featuresEventHandler" class="org.apache.karaf.cellar.features.FeaturesEventHandler"

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/60a24ca2/obr/src/main/resources/OSGI-INF/blueprint/blueprint.xml
----------------------------------------------------------------------
diff --git a/obr/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/obr/src/main/resources/OSGI-INF/blueprint/blueprint.xml
index 061ce99..e3a5881 100644
--- a/obr/src/main/resources/OSGI-INF/blueprint/blueprint.xml
+++ b/obr/src/main/resources/OSGI-INF/blueprint/blueprint.xml
@@ -36,7 +36,11 @@
         <property name="groupManager" ref="groupManager"/>
         <property name="configurationAdmin" ref="configurationAdmin"/>
     </bean>
-    <service ref="obrUrlSynchronizer" interface="org.apache.karaf.cellar.core.Synchronizer"/>
+    <service ref="obrUrlSynchronizer" interface="org.apache.karaf.cellar.core.Synchronizer">
+        <service-properties>
+            <entry key="resource" value="obr.urls"/>
+        </service-properties>
+    </service>
 
     <!-- Cluster OBR URL Event Handler -->
     <bean id="obrUrlEventHandler" class="org.apache.karaf.cellar.obr.ObrUrlEventHandler"

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/60a24ca2/shell/pom.xml
----------------------------------------------------------------------
diff --git a/shell/pom.xml b/shell/pom.xml
index bdfc24c..9ee4116 100644
--- a/shell/pom.xml
+++ b/shell/pom.xml
@@ -41,6 +41,11 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>org.apache.karaf.cellar</groupId>
             <artifactId>org.apache.karaf.cellar.core</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/60a24ca2/shell/src/main/java/org/apache/karaf/cellar/shell/SyncCommand.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/karaf/cellar/shell/SyncCommand.java b/shell/src/main/java/org/apache/karaf/cellar/shell/SyncCommand.java
index 04a2f89..d6cd94c 100644
--- a/shell/src/main/java/org/apache/karaf/cellar/shell/SyncCommand.java
+++ b/shell/src/main/java/org/apache/karaf/cellar/shell/SyncCommand.java
@@ -13,36 +13,135 @@
  */
 package org.apache.karaf.cellar.shell;
 
+import org.apache.karaf.cellar.core.Configurations;
 import org.apache.karaf.cellar.core.Group;
 import org.apache.karaf.cellar.core.Synchronizer;
+import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
-import org.osgi.framework.InvalidSyntaxException;
+import org.apache.karaf.shell.commands.Option;
 import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
 
+import java.util.Dictionary;
+import java.util.HashSet;
+import java.util.Properties;
 import java.util.Set;
 
-@Command(scope = "cluster", name = "sync", description = "Force the call of all cluster synchronizers available")
+@Command(scope = "cluster", name = "sync", description = "Manipulate the synchronizers")
 public class SyncCommand extends ClusterCommandSupport {
 
+    @Option(name = "-g", aliases = { "--group" }, description = "The cluster group name", required = false, multiValued = false)
+    private String groupName;
+
+    @Option(name = "-b", aliases = { "--bundle" }, description = "Cluster bundle support", required = false, multiValued = false)
+    private boolean bundleSync = false;
+
+    @Option(name = "-c", aliases = { "--config" }, description = "Cluster config support", required = false, multiValued = false)
+    private boolean configSync = false;
+
+    @Option(name = "-f", aliases = { "--feature" }, description = "Cluster feature support", required = false, multiValued = false)
+    private boolean featuresSync = false;
+
+    @Option(name = "-o", aliases = { "--obr" }, description = "Cluster OBR support", required = false, multiValued = false)
+    private boolean obrSync = false;
+
+    @Argument(name = "policy", description = "The definition of the sync policy for the given cluster resource", required = false, multiValued = false)
+    private String policy;
+
+    private ConfigurationAdmin configurationAdmin;
+
     @Override
     protected Object doExecute() throws Exception {
-        Set<Group> localGroups = groupManager.listLocalGroups();
-        for (Group group : localGroups) {
-            System.out.println("Synchronizing cluster group " + group.getName());
-            try {
-                ServiceReference[] serviceReferences = bundleContext.getAllServiceReferences("org.apache.karaf.cellar.core.Synchronizer", null);
-                if (serviceReferences != null && serviceReferences.length > 0) {
-                    for (ServiceReference ref : serviceReferences) {
-                        Synchronizer synchronizer = (Synchronizer) bundleContext.getService(ref);
-                        synchronizer.sync(group);
-                        bundleContext.ungetService(ref);
-                    }
-                }
-            } catch (InvalidSyntaxException e) {
-                // ignore
+        boolean allResources = false;
+        // if the user didn't provide any resource, we consider all
+        if (!bundleSync && !configSync && !featuresSync && !obrSync) {
+            allResources = true;
+        }
+        Set<Group> groups;
+        if (groupName == null || groupName.isEmpty()) {
+            groups = groupManager.listLocalGroups();
+        } else {
+            groups = new HashSet<Group>();
+            if (groupManager.findGroupByName(groupName) == null) {
+                System.err.println("Cluster group " + groupName + " doesn't exist");
+                return null;
+            }
+            groups.add(groupManager.findGroupByName(groupName));
+        }
+        if (policy == null || policy.isEmpty()) {
+            // we are in sync mode
+            // constructing the target cluster groups
+            for (Group group : groups) {
+                System.out.println("Synchronizing cluster group " + group.getName());
+                if (bundleSync || allResources) {
+                    doSync("bundle", group);
+                }
+                if (configSync || allResources) {
+                    doSync("config", group);
+                }
+                if (featuresSync || allResources) {
+                    doSync("feature", group);
+                }
+                if (obrSync || allResources) {
+                    doSync("obr.urls", group);
+                }
+            }
+        } else {
+            // we are in set mode
+            if (!policy.equalsIgnoreCase("cluster") && !policy.equalsIgnoreCase("node") && !policy.equalsIgnoreCase("disabled")) {
+                System.err.println("The sync policy " + policy + " is not valid. Valid sync policies are: cluster, node, disabled");
+                return null;
+            }
+            for (Group group : groups) {
+                System.out.println("Updating sync policy for cluster group " + group.getName());
+                if (bundleSync || allResources) {
+                    updateSync("bundle", group, policy);
+                }
+                if (configSync || allResources) {
+                    updateSync("config", group, policy);
+                }
+                if (featuresSync || allResources) {
+                    updateSync("feature", group, policy);
+                }
+                if (obrSync || allResources) {
+                    updateSync("obr.urls", group, policy);
+                }
             }
         }
         return null;
     }
 
+    private void doSync(String resource, Group group) throws Exception {
+        // looking for the resource synchronizer
+        System.out.print("\t" + resource + ": ");
+        ServiceReference[] references = bundleContext.getAllServiceReferences(Synchronizer.class.getName(), "(resource=" + resource + ")");
+        if (references != null && references.length > 0) {
+            for (ServiceReference reference : references) {
+                Synchronizer synchronizer = (Synchronizer) bundleContext.getService(reference);
+                synchronizer.sync(group);
+                bundleContext.ungetService(reference);
+            }
+            System.out.println("done");
+        } else {
+            System.out.println("No synchronizer found for " + resource);
+        }
+    }
+
+    private void updateSync(String resource, Group group, String policy) throws Exception {
+        System.out.print("\t" + resource + ": ");
+        Configuration configuration = configurationAdmin.getConfiguration(Configurations.GROUP, null);
+        if (configuration != null) {
+            Dictionary properties = configuration.getProperties();
+            if (properties == null)
+                properties = new Properties();
+            properties.put(group.getName() + "." + resource + ".sync", policy);
+            configuration.update(properties);
+            System.out.println("done");
+        }
+    }
+
+    public void setConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
+        this.configurationAdmin = configurationAdmin;
+    }
 }

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/60a24ca2/shell/src/main/resources/OSGI-INF/blueprint/shell-cluster.xml
----------------------------------------------------------------------
diff --git a/shell/src/main/resources/OSGI-INF/blueprint/shell-cluster.xml b/shell/src/main/resources/OSGI-INF/blueprint/shell-cluster.xml
index fcf38be..b5b2991 100644
--- a/shell/src/main/resources/OSGI-INF/blueprint/shell-cluster.xml
+++ b/shell/src/main/resources/OSGI-INF/blueprint/shell-cluster.xml
@@ -34,6 +34,7 @@
             <action class="org.apache.karaf.cellar.shell.SyncCommand">
                 <property name="clusterManager" ref="clusterManager"/>
                 <property name="groupManager" ref="groupManager"/>
+                <property name="configurationAdmin" ref="configurationAdmin"/>
             </action>
         </command>
         <command>
@@ -189,6 +190,7 @@
     <reference id="clusterManager" interface="org.apache.karaf.cellar.core.ClusterManager" availability="optional"/>
     <reference id="groupManager" interface="org.apache.karaf.cellar.core.GroupManager" availability="optional"/>
     <reference id="executionContext" interface="org.apache.karaf.cellar.core.command.ExecutionContext" availability="optional"/>
+    <reference id="configurationAdmin" interface="org.osgi.service.cm.ConfigurationAdmin"/>
 
     <bean id="allNodesCompleter" class="org.apache.karaf.cellar.core.shell.completer.AllNodeCompleter">
         <property name="clusterManager" ref="clusterManager"/>