summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Plugins/Properties.py
diff options
context:
space:
mode:
authorSol Jerome <sol.jerome@gmail.com>2012-03-24 11:20:07 -0500
committerSol Jerome <sol.jerome@gmail.com>2012-03-24 11:20:07 -0500
commitdab1d03d81c538966d03fb9318a4588a9e803b44 (patch)
treef51e27fa55887e9fb961766805fe43f0da56c5b9 /src/lib/Bcfg2/Server/Plugins/Properties.py
parent5cd6238df496a3cea178e4596ecd87967cce1ce6 (diff)
downloadbcfg2-dab1d03d81c538966d03fb9318a4588a9e803b44.tar.gz
bcfg2-dab1d03d81c538966d03fb9318a4588a9e803b44.tar.bz2
bcfg2-dab1d03d81c538966d03fb9318a4588a9e803b44.zip
Allow to run directly from a git checkout (#1037)
Signed-off-by: Sol Jerome <sol.jerome@gmail.com>
Diffstat (limited to 'src/lib/Bcfg2/Server/Plugins/Properties.py')
-rw-r--r--src/lib/Bcfg2/Server/Plugins/Properties.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/lib/Bcfg2/Server/Plugins/Properties.py b/src/lib/Bcfg2/Server/Plugins/Properties.py
new file mode 100644
index 000000000..680881858
--- /dev/null
+++ b/src/lib/Bcfg2/Server/Plugins/Properties.py
@@ -0,0 +1,76 @@
+import os
+import re
+import sys
+import copy
+import logging
+import lxml.etree
+import Bcfg2.Server.Plugin
+
+logger = logging.getLogger('Bcfg2.Plugins.Properties')
+
+class PropertyFile(Bcfg2.Server.Plugin.StructFile):
+ """Class for properties files."""
+ def write(self):
+ """ Write the data in this data structure back to the property
+ file """
+ if self.validate_data():
+ try:
+ open(self.name,
+ "wb").write(lxml.etree.tostring(self.xdata,
+ pretty_print=True))
+ return True
+ except IOError:
+ err = sys.exc_info()[1]
+ logger.error("Failed to write %s: %s" % (self.name, err))
+ return False
+ else:
+ return False
+
+ def validate_data(self):
+ """ ensure that the data in this object validates against the
+ XML schema for this property file (if a schema exists) """
+ schemafile = self.name.replace(".xml", ".xsd")
+ if os.path.exists(schemafile):
+ try:
+ schema = lxml.etree.XMLSchema(file=schemafile)
+ except:
+ logger.error("Failed to process schema for %s" % self.name)
+ return False
+ else:
+ # no schema exists
+ return True
+
+ if not schema.validate(self.xdata):
+ logger.error("Data for %s fails to validate; run bcfg2-lint for "
+ "more details" % self.name)
+ return False
+ else:
+ return True
+
+
+class PropDirectoryBacked(Bcfg2.Server.Plugin.DirectoryBacked):
+ __child__ = PropertyFile
+ patterns = re.compile(r'.*\.xml$')
+
+
+class Properties(Bcfg2.Server.Plugin.Plugin,
+ Bcfg2.Server.Plugin.Connector):
+ """
+ The properties plugin maps property
+ files into client metadata instances.
+ """
+ name = 'Properties'
+
+ def __init__(self, core, datastore):
+ Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore)
+ Bcfg2.Server.Plugin.Connector.__init__(self)
+ try:
+ self.store = PropDirectoryBacked(self.data, core.fam)
+ except OSError:
+ e = sys.exc_info()[1]
+ self.logger.error("Error while creating Properties store: %s %s" %
+ (e.strerror, e.filename))
+ raise Bcfg2.Server.Plugin.PluginInitError
+
+ def get_additional_data(self, _):
+ return copy.copy(self.store.entries)