Juan Hernandez has uploaded a new change for review.

Change subject: packaging: Add support for ${...} in local config
......................................................................

packaging: Add support for ${...} in local config

This patch adds support for references to parameters in the local
configuration file. This is convenient in order to simplify the
packaging of other applications, like reports, as with this support they
don't have to modify the existing configuration files, but add new ones
in the /etc/ovirt-engine/engine.conf.d directory. For example, in order
to add the reports application there is no need to modify the existing
engine.conf file to add the following:

  ENGINE_APPS=engine.ear reports.war

Instead of that a file can be created in engine.conf.d with the
following content:

  ENGINE_APPS=${ENGINE_APPS} reports.war

Change-Id: Ib8ce501241ed1a937db24e1371e2d4227679ae74
Signed-off-by: Juan Hernandez <[email protected]>
---
M 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java
M packaging/fedora/engine-service.py.in
M packaging/fedora/spec/ovirt-engine.spec.in
3 files changed, 143 insertions(+), 27 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/18/12718/1

diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java
index 5881a7d..10c2d3a 100644
--- 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java
@@ -1,24 +1,28 @@
 package org.ovirt.engine.core.utils;
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
 import java.io.FilenameFilter;
 import java.io.IOException;
-import java.io.Reader;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Comparator;
-import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.List;
-import java.util.Properties;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.log4j.Logger;
 
 /**
  * This class stores the local configuration (understanding local as the
- * configuration of the local machine, as oposed to the global configuration
+ * configuration of the local machine, as opposed to the global configuration
  * stored in the database) of the engine loaded from the file specified by the
  * <code>ENGINE_VARS</code> environment variable.
  */
@@ -26,9 +30,15 @@
     // The log:
     private static final Logger log = Logger.getLogger(LocalConfig.class);
 
-    // Default files for defaults and overriden values:
+    // Default files for defaults and overridden values:
     private static final String DEFAULTS_PATH = 
"/usr/share/ovirt-engine/conf/engine.conf.defaults";
     private static final String VARS_PATH = "/etc/ovirt-engine/engine.conf";
+
+    // Compile regular expressions:
+    private static final Pattern COMMENT_EXPR = Pattern.compile("\\s*#.*$");
+    private static final Pattern BLANK_EXPR = Pattern.compile("^\\s*$");
+    private static final Pattern VALUE_EXPR = 
Pattern.compile("^\\s*(\\w+)\\s*=\\s*(.*)$");
+    private static final Pattern REF_EXPR = Pattern.compile("\\$\\{(\\w+)\\}");
 
     // This is a singleton and this is the instance:
     private static final LocalConfig instance = new LocalConfig();
@@ -38,7 +48,7 @@
     }
 
     // The properties object storing the current values of the parameters:
-    private Properties values = new Properties();
+    private Map<String, String> values = new HashMap<String, String>();
 
     private LocalConfig() {
         // This is the list of configuration files that will be loaded and
@@ -54,7 +64,7 @@
         File defaultsFile = new File(defaultsPath);
         configFiles.add(defaultsFile);
 
-        // Locate the overriden values file and add it to the list:
+        // Locate the overridden values file and add it to the list:
         String varsPath = System.getenv("ENGINE_VARS");
         if (varsPath == null) {
             varsPath = VARS_PATH;
@@ -105,12 +115,15 @@
         // Dump the properties to the log (this should probably be DEBUG, but 
as
         // it will usually happen only once, during the startup, is not that a
         // problem to use INFO):
-        @SuppressWarnings("unchecked")
-        Enumeration<String> keys = (Enumeration<String>) 
values.propertyNames();
-        while (keys.hasMoreElements()) {
-            String key = keys.nextElement();
-            String value = values.getProperty(key);
-            log.info("Value of property \"" + key + "\" is \"" + value + 
"\".");
+        if (log.isInfoEnabled()) {
+            Set<String> keys = values.keySet();
+            List<String> list = new ArrayList<String>(keys.size());
+            list.addAll(keys);
+            Collections.sort(list);
+            for (String key : list) {
+                String value = values.get(key);
+                log.info("Value of property \"" + key + "\" is \"" + value + 
"\".");
+            }
         }
     }
 
@@ -129,10 +142,13 @@
         }
 
         // Load the file:
-        Reader reader = null;
+        BufferedReader reader = null;
         try {
-            reader = new FileReader(file);
-            values.load(reader);
+            reader = new BufferedReader(new FileReader(file));
+            String line = null;
+            while ((line = reader.readLine()) != null) {
+                loadLine(line);
+            }
             log.info("Loaded file \"" + file.getAbsolutePath() + "\".");
         }
         catch (IOException exception) {
@@ -152,6 +168,59 @@
     }
 
     /**
+     * Load the contents of a line from a properties file, expanding
+     * references to variables.
+     *
+     * @param line the line from the properties file
+     */
+    private void loadLine(String line) throws IOException {
+        // Remove comments:
+        Matcher commentMatch = COMMENT_EXPR.matcher(line);
+        if (commentMatch.find()) {
+            line = line.substring(0, commentMatch.start()) + 
line.substring(commentMatch.end());
+        }
+
+        // Skip empty lines:
+        Matcher blankMatch = BLANK_EXPR.matcher(line);
+        if (blankMatch.find()) {
+            return;
+        }
+
+        // Separate name from value:
+        Matcher keyValueMatch = VALUE_EXPR.matcher(line);
+        System.out.println("line: -" + line + "-");
+        if (!keyValueMatch.find()) {
+            return;
+        }
+        String key = keyValueMatch.group(1);
+        System.out.println("key: -" + key + "-");
+        String value = keyValueMatch.group(2);
+        System.out.println("value: -" + value + "-");
+
+        // Strip quotes from value:
+        if (value.length() >= 2 && value.startsWith("\"") && 
value.endsWith("\"")) {
+            value = value.substring(1, value.length() - 1);
+        }
+
+        // Expand references to other parameters:
+        for (;;) {
+            Matcher refMatch = REF_EXPR.matcher(value);
+            if (!refMatch.find()) {
+                break;
+            }
+            String refKey = refMatch.group(1);
+            String refValue = values.get(refKey);
+            if (refValue == null) {
+                break;
+            }
+            value = value.substring(0, refMatch.start()) + refValue + 
value.substring(refMatch.end());
+        }
+
+        // Update the values:
+        values.put(key, value);
+    }
+
+    /**
      * Get the value of a property given its name.
      *
      * @param key the name of the property
@@ -160,7 +229,7 @@
      *     value
      */
     public String getProperty(String key) {
-        String value = values.getProperty(key);
+        String value = values.get(key);
         if (value == null) {
             // Loudly alert in the log and throw an exception:
             String message = "The property \"" + key + "\" doesn't have a 
value.";
@@ -171,7 +240,7 @@
             // a serious error:
             // System.exit(1)
         }
-        return values.getProperty(key);
+        return values.get(key);
     }
 
     // Accepted values for boolean properties (please keep them sorted as we 
use
diff --git a/packaging/fedora/engine-service.py.in 
b/packaging/fedora/engine-service.py.in
index 67ac127..2a1dcd2 100644
--- a/packaging/fedora/engine-service.py.in
+++ b/packaging/fedora/engine-service.py.in
@@ -20,7 +20,6 @@
 # description: oVirt Engine
 # pidfile: /var/run/ovirt-engine.pid
 
-import configobj
 import errno
 import glob
 import grp
@@ -86,20 +85,69 @@
 # Helper class to simplify getting values from the configuration, specially
 # from the template used to generate the application server configuration
 # file:
-class Config(configobj.ConfigObj):
+class Config():
+    # Compile regular expressions:
+    COMMENT_EXPR = re.compile(r"\s*#.*$")
+    BLANK_EXPR = re.compile(r"^\s.*$")
+    VALUE_EXPR = re.compile("^\s*(\w+)\s*=\s*(.*)$")
+    REF_EXPR = re.compile(r"\$\{(\w+)\}")
+
     def __init__(self, files):
-        # Initialize ourselves as an empty configuration object:
-        configobj.ConfigObj.__init__(self)
+        # Save the list of files:
+        self.files = files
+
+        # Start with an empty set of values:
+        self.values = {}
 
         # Merge all the given configuration files, in the same order
         # given, so that the values in one file are overriden by values
         # in files appearing later in the list:
-        for file in files:
-            config = configobj.ConfigObj(file)
-            self.merge(config)
+        for file in self.files:
+            self.loadFile(file)
+
+    def loadFile(self, file):
+        with open(file, "r") as fd:
+            for line in fd:
+                self.loadLine(line)
+
+    def loadLine(self, line):
+        # Remove comments:
+        commentMatch = Config.COMMENT_EXPR.search(line)
+        if commentMatch is not None:
+            line = line[:commentMatch.start()] + line[commentMatch.end():]
+
+        # Skip empty lines:
+        emptyMatch = Config.BLANK_EXPR.search(line)
+        if emptyMatch is not None:
+            return
+
+        # Separate name from value:
+        keyValueMatch = Config.VALUE_EXPR.search(line)
+        if keyValueMatch is None:
+            return
+        key = keyValueMatch.group(1)
+        value = keyValueMatch.group(2)
+
+        # Strip quotes from value:
+        if len(value) >= 2 and value[0] == '"' and value[-1] == '"':
+            value = value[1:-1]
+
+        # Expand references to other parameters:
+        while True:
+            refMatch = Config.REF_EXPR.search(value)
+            if refMatch is None:
+                break
+            refKey = refMatch.group(1)
+            refValue = self.values.get(refKey)
+            if refValue is None:
+                break
+            value = value[:refMatch.start()] + refValue + 
value[refMatch.end():]
+
+        # Update the values:
+        self.values[key] = value
 
     def getString(self, name):
-        text = self.get(name)
+        text = self.values.get(name)
         if text is None:
             raise Exception("The parameter \"%s\" doesn't have a value." % 
name)
         return text
diff --git a/packaging/fedora/spec/ovirt-engine.spec.in 
b/packaging/fedora/spec/ovirt-engine.spec.in
index 40d6db2..0be49f0 100644
--- a/packaging/fedora/spec/ovirt-engine.spec.in
+++ b/packaging/fedora/spec/ovirt-engine.spec.in
@@ -118,7 +118,6 @@
 Requires: ntp
 Requires: openssl
 Requires: policycoreutils-python
-Requires: python-configobj
 Requires: python-cheetah
 Requires: ovirt-host-deploy-java
 Requires: xz


--
To view, visit http://gerrit.ovirt.org/12718
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib8ce501241ed1a937db24e1371e2d4227679ae74
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Juan Hernandez <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to