You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2015/05/19 17:30:56 UTC

svn commit: r1680316 - in /sling/trunk/contrib/crankstart: core/ core/src/main/java/org/apache/sling/crankstart/core/ core/src/main/java/org/apache/sling/crankstart/core/commands/ launcher/src/test/resources/ launcher/src/test/resources/provisioning-mo...

Author: bdelacretaz
Date: Tue May 19 15:30:56 2015
New Revision: 1680316

URL: http://svn.apache.org/r1680316
Log:
SLING-4728 - work in progress, provisioning model support in Crankstart

Added:
    sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/ProvisioningModel.java
    sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/
    sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/base.txt
    sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/sling-extensions.txt
    sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/start-level-99.txt
Modified:
    sling/trunk/contrib/crankstart/core/pom.xml
    sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/CrankstartFileProcessor.java
    sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/InstallBundle.java
    sling/trunk/contrib/crankstart/launcher/src/test/resources/launcher-test.crank.txt

Modified: sling/trunk/contrib/crankstart/core/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/core/pom.xml?rev=1680316&r1=1680315&r2=1680316&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/core/pom.xml (original)
+++ sling/trunk/contrib/crankstart/core/pom.xml Tue May 19 15:30:56 2015
@@ -112,6 +112,12 @@
             <version>1.0.1-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.provisioning.model</artifactId>
+            <version>1.1.0</version>
+            <scope>provided</scope>
+        </dependency>
         <!-- 
             We use a class from the config admin implementation to read config files,
             and we process it using maven-shade-plugin to avoid conflicts 

Modified: sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/CrankstartFileProcessor.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/CrankstartFileProcessor.java?rev=1680316&r1=1680315&r2=1680316&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/CrankstartFileProcessor.java (original)
+++ sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/CrankstartFileProcessor.java Tue May 19 15:30:56 2015
@@ -36,6 +36,7 @@ import org.apache.sling.crankstart.core.
 import org.apache.sling.crankstart.core.commands.InstallBundle;
 import org.apache.sling.crankstart.core.commands.Log;
 import org.apache.sling.crankstart.core.commands.NullCommand;
+import org.apache.sling.crankstart.core.commands.ProvisioningModel;
 import org.apache.sling.crankstart.core.commands.SetOsgiFrameworkProperty;
 import org.apache.sling.crankstart.core.commands.StartBundles;
 import org.apache.sling.crankstart.core.commands.StartFramework;
@@ -66,6 +67,7 @@ public class CrankstartFileProcessor imp
         builtinCommands.add(new StartFramework());
         builtinCommands.add(new Configure());
         builtinCommands.add(new Defaults());
+        builtinCommands.add(new ProvisioningModel());
         builtinCommands.add(new Exit());
         
         // Need a null "classpath" command as our launcher uses it

Modified: sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/InstallBundle.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/InstallBundle.java?rev=1680316&r1=1680315&r2=1680316&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/InstallBundle.java (original)
+++ sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/InstallBundle.java Tue May 19 15:30:56 2015
@@ -16,6 +16,7 @@
  */
 package org.apache.sling.crankstart.core.commands;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 
@@ -24,6 +25,7 @@ import org.apache.sling.crankstart.api.C
 import org.apache.sling.crankstart.api.CrankstartContext;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
 import org.osgi.framework.startlevel.BundleStartLevel;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -45,22 +47,25 @@ public class InstallBundle implements Cr
     @Override
     public void execute(CrankstartContext crankstartContext, CrankstartCommandLine commandLine) throws Exception {
         final String bundleRef = commandLine.getQualifier();
-        final URL url = new URL(bundleRef);
         final BundleContext ctx = crankstartContext.getOsgiFramework().getBundleContext();
+        final int level = getStartLevel(crankstartContext);
+        installBundle(ctx, log, bundleRef, level);
+    }
+    
+    static void installBundle(BundleContext ctx, Logger log, String urlString, int startLevel) throws IOException, BundleException {
+        final URL url = new URL(urlString);
         final InputStream bundleStream = url.openStream();
         try {
-            final Bundle b = ctx.installBundle(bundleRef, url.openStream());
-            
-            final int level = getStartLevel(crankstartContext);
-            if(level > 0) {
+            final Bundle b = ctx.installBundle(urlString, url.openStream());
+            if(startLevel > 0) {
                 final BundleStartLevel bsl = (BundleStartLevel)b.adapt(BundleStartLevel.class);
                 if(bsl == null) {
-                    log.warn("Bundle does not adapt to BundleStartLevel, cannot set start level", bundleRef);
+                    log.warn("Bundle does not adapt to BundleStartLevel, cannot set start level", urlString);
                 }
-                bsl.setStartLevel(level);
+                bsl.setStartLevel(startLevel);
             }
             
-            log.info("bundle installed at start level {}: {}", level, bundleRef);
+            log.info("bundle installed at start level {}: {}", startLevel, urlString);
         } finally {
             bundleStream.close();
         }

Added: sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/ProvisioningModel.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/ProvisioningModel.java?rev=1680316&view=auto
==============================================================================
--- sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/ProvisioningModel.java (added)
+++ sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/ProvisioningModel.java Tue May 19 15:30:56 2015
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.crankstart.core.commands;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.Reader;
+
+import org.apache.sling.crankstart.api.CrankstartCommand;
+import org.apache.sling.crankstart.api.CrankstartCommandLine;
+import org.apache.sling.crankstart.api.CrankstartContext;
+import org.apache.sling.provisioning.model.Artifact;
+import org.apache.sling.provisioning.model.ArtifactGroup;
+import org.apache.sling.provisioning.model.Feature;
+import org.apache.sling.provisioning.model.Model;
+import org.apache.sling.provisioning.model.ModelUtility;
+import org.apache.sling.provisioning.model.RunMode;
+import org.apache.sling.provisioning.model.io.ModelReader;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** CrankstartCommand that reads a Sling Provisioning Model
+ *  and installs all its artifacts */
+public class ProvisioningModel implements CrankstartCommand {
+    public static final String I_CMD = "provisioning.model";
+    public static final String MODEL_FILE_EXT = ".txt";
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    
+    
+    @Override
+    public boolean appliesTo(CrankstartCommandLine commandLine) {
+        return I_CMD.equals(commandLine.getVerb());
+    }
+
+    public String getDescription() {
+        return I_CMD + ": read a provisioning model and install its artifacts";
+    }
+    
+    @Override
+    public void execute(CrankstartContext crankstartContext, CrankstartCommandLine commandLine) throws Exception {
+        final File modelFolder = new File(commandLine.getQualifier());
+        if(!modelFolder.isDirectory() || !modelFolder.canRead()) {
+            throw new IOException("Cannot read specified provisioning model folder " + modelFolder.getAbsolutePath());
+        }
+        Model m = null;
+        for(String filename : modelFolder.list()) {
+            final File modelFile = new File(modelFolder, filename);
+            if(!modelFile.getName().endsWith(MODEL_FILE_EXT)) {
+                log.warn("Model file name does not end with {}, ignored: {}", MODEL_FILE_EXT, modelFile.getAbsolutePath());
+                continue;
+            }
+            final Reader r = new FileReader(modelFile);
+            try {
+                final Model current = ModelReader.read(r, modelFile.getAbsolutePath());
+                if(m == null) {
+                    log.info("Initial model: {}", modelFile.getName());
+                    m = current;
+                } else {
+                    log.info("Merging additional model: {}", modelFile.getName());
+                    ModelUtility.merge(m, current);
+                }
+            } finally {
+                r.close();
+            }
+            
+        }
+        
+        log.info("Processing the merged provisioning model from {}", modelFolder);
+        processModel(crankstartContext, m);
+        log.info("Done processing the merged provisioning model");
+    }
+    
+    private void processModel(CrankstartContext crankstartContext, Model m) throws IOException, BundleException {
+        final BundleContext ctx = crankstartContext.getOsgiFramework().getBundleContext();
+        m = ModelUtility.getEffectiveModel(m, null);
+        for(Feature f : m.getFeatures()) {
+            log.info("Processing provisioning model feature: {}", f.getName());
+            for(RunMode rm : f.getRunModes()) {
+                for(ArtifactGroup g : rm.getArtifactGroups()) {
+                    final int startLevel = g.getStartLevel();
+                    for(Artifact a : g) {
+                        // TODO for now, naively assume a is a bundle, and mvn: protocol
+                        final String url = "mvn:" + a.getGroupId() + "/" + a.getArtifactId() + "/" + a.getVersion();
+                        InstallBundle.installBundle(ctx, log, url, startLevel);
+                    }
+                }
+            }
+        }
+    }
+}

Modified: sling/trunk/contrib/crankstart/launcher/src/test/resources/launcher-test.crank.txt
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/test/resources/launcher-test.crank.txt?rev=1680316&r1=1680315&r2=1680316&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/launcher/src/test/resources/launcher-test.crank.txt (original)
+++ sling/trunk/contrib/crankstart/launcher/src/test/resources/launcher-test.crank.txt Tue May 19 15:30:56 2015
@@ -3,7 +3,6 @@
 
 # Default values for our variables
 defaults single.path /single
-defaults felix.http.jetty.version 2.2.0
 
 # Bootstrap classpath (variables are not supported here)
 classpath mvn:org.apache.felix/org.apache.felix.framework/4.4.0
@@ -11,6 +10,7 @@ classpath mvn:org.slf4j/slf4j-api/1.6.2
 classpath mvn:org.slf4j/slf4j-simple/1.6.2
 classpath mvn:org.apache.sling/org.apache.sling.crankstart.core/1.0.1-SNAPSHOT
 classpath mvn:org.apache.sling/org.apache.sling.crankstart.api/1.0.1-SNAPSHOT
+classpath mvn:org.apache.sling/org.apache.sling.provisioning.model/1.1.0
 
 # OSGi properties
 osgi.property org.osgi.service.http.port ${http.port}
@@ -20,35 +20,16 @@ osgi.property org.osgi.framework.storage
 # if this is not the first startup.
 start.framework
 
-# Start ConfigAdmin, HTTP service and SCR
-bundle mvn:org.apache.felix/org.apache.felix.http.jetty/${felix.http.jetty.version}
-bundle mvn:org.apache.felix/org.apache.felix.eventadmin/1.3.2
-bundle mvn:org.apache.felix/org.apache.felix.scr/1.8.2
-bundle mvn:org.apache.felix/org.apache.felix.metatype/1.0.10
-bundle mvn:org.apache.sling/org.apache.sling.commons.osgi/2.2.0
-bundle mvn:org.apache.sling/org.apache.sling.commons.log/2.1.2
-bundle mvn:org.apache.felix/org.apache.felix.configadmin/1.6.0
-bundle mvn:org.apache.felix/org.apache.felix.webconsole/3.1.6
-
 # The crankstart.api.fragment bundle makes the crankstart.api package available
 # to bundles, required for bundles to provide crankstart extension commands like
 # the test.system.property command below
 bundle mvn:org.apache.sling/org.apache.sling.crankstart.api.fragment/1.0.3-SNAPSHOT
 bundle mvn:org.apache.sling/org.apache.sling.crankstart.test.services/1.0.1-SNAPSHOT
 
-# Test our Sling extension commands, that add a bundle via the Sling installer
-# (which requires commons.json and jcr-wrapper)
-bundle mvn:org.apache.sling/org.apache.sling.installer.core/3.5.0
-bundle mvn:org.apache.sling/org.apache.sling.commons.json/2.0.6
-bundle mvn:org.apache.sling/org.apache.sling.jcr.jcr-wrapper/2.0.0
-bundle mvn:org.apache.sling/org.apache.sling.crankstart.sling.extensions/1.0.1-SNAPSHOT
-bundle mvn:commons-io/commons-io/2.4
-
-# Install a bundle at a start level higher than the current one
-# to be able to check that it's not active
-defaults crankstart.bundle.start.level 99
-bundle mvn:commons-collections/commons-collections/3.2.1
-
+# Add provisioning model artifacts
+# TODO move all bundles there
+provisioning.model src/test/resources/provisioning-model
+ 
 # Now start our bundles
 start.all.bundles
 
@@ -84,6 +65,6 @@ sling.installer.resource mvn:org.apache.
 
 # And register the installer resources
 sling.installer.register crankstart
-  
+
 # Informative log  
 log felix http service should come up at http://localhost:${http.port}

Added: sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/base.txt
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/base.txt?rev=1680316&view=auto
==============================================================================
--- sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/base.txt (added)
+++ sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/base.txt Tue May 19 15:30:56 2015
@@ -0,0 +1,39 @@
+#
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+#
+# This is a feature description
+#
+# A feature consists of variables and run mode dependent artifacts.
+#
+
+# Test our Sling extension commands, that add a bundle via the Sling installer
+# (which requires commons.json and jcr-wrapper)
+[feature name=crankstart.test.base]
+
+[variables]
+  felix.http.jetty.version=2.2.0
+
+[artifacts]
+  org.apache.felix/org.apache.felix.http.jetty/${felix.http.jetty.version}
+  org.apache.felix/org.apache.felix.eventadmin/1.3.2
+  org.apache.felix/org.apache.felix.scr/1.8.2
+  org.apache.felix/org.apache.felix.metatype/1.0.10
+  org.apache.sling/org.apache.sling.commons.osgi/2.2.0
+  org.apache.sling/org.apache.sling.commons.log/2.1.2
+  org.apache.felix/org.apache.felix.configadmin/1.6.0
+  org.apache.felix/org.apache.felix.webconsole/3.1.6
\ No newline at end of file

Added: sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/sling-extensions.txt
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/sling-extensions.txt?rev=1680316&view=auto
==============================================================================
--- sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/sling-extensions.txt (added)
+++ sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/sling-extensions.txt Tue May 19 15:30:56 2015
@@ -0,0 +1,33 @@
+#
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+#
+# This is a feature description
+#
+# A feature consists of variables and run mode dependent artifacts.
+#
+
+# Test our Sling extension commands, that add a bundle via the Sling installer
+# (which requires commons.json and jcr-wrapper)
+[feature name=sling.extensions]
+
+[artifacts]
+  org.apache.sling/org.apache.sling.installer.core/3.5.0
+  org.apache.sling/org.apache.sling.commons.json/2.0.6
+  org.apache.sling/org.apache.sling.jcr.jcr-wrapper/2.0.0
+  org.apache.sling/org.apache.sling.crankstart.sling.extensions/1.0.1-SNAPSHOT
+  commons-io/commons-io/2.4

Added: sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/start-level-99.txt
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/start-level-99.txt?rev=1680316&view=auto
==============================================================================
--- sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/start-level-99.txt (added)
+++ sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/start-level-99.txt Tue May 19 15:30:56 2015
@@ -0,0 +1,24 @@
+#
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+#
+# Add a bundle at a start level higher that our framework's to verify
+# that it is installed but not active
+[feature name=startlevel99]
+
+[artifacts startLevel=99]
+  commons-collections/commons-collections/3.2.1