summaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorNarayan Desai <desai@mcs.anl.gov>2005-09-07 17:35:15 +0000
committerNarayan Desai <desai@mcs.anl.gov>2005-09-07 17:35:15 +0000
commit2f96daf78c757ae08b9207c9fe808dd080ca4a75 (patch)
treecc49c138dfd42b42af9ff96e3ee93e602b649cea /src/lib
parent47d0ca27ba448001dc0c686341795e91785b4881 (diff)
downloadbcfg2-2f96daf78c757ae08b9207c9fe808dd080ca4a75.tar.gz
bcfg2-2f96daf78c757ae08b9207c9fe808dd080ca4a75.tar.bz2
bcfg2-2f96daf78c757ae08b9207c9fe808dd080ca4a75.zip
update to new plugin API
2005/09/06 22:31:57-05:00 anl.gov!desai Rename: src/lib/Server/Generators/TCheetah.py -> src/lib/Server/Plugins/TCheetah.py (Logical change 1.300) git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@1224 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/Server/Plugins/TCheetah.py110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/lib/Server/Plugins/TCheetah.py b/src/lib/Server/Plugins/TCheetah.py
index e69de29bb..528af2019 100644
--- a/src/lib/Server/Plugins/TCheetah.py
+++ b/src/lib/Server/Plugins/TCheetah.py
@@ -0,0 +1,110 @@
+'''This module implements a templating generator based on Cheetah'''
+__revision__ = '$Revision$'
+
+from posixpath import isdir
+from syslog import syslog, LOG_ERR
+from Bcfg2.Server.Plugin import Plugin, PluginExecutionError, FileBacked, SingleXMLFileBacked
+from elementtree.ElementTree import XML
+from xml.parsers.expat import ExpatError
+from Cheetah.Template import Template
+
+class TemplateFile(FileBacked):
+ '''Template file creates Cheetah template structures for the loaded file'''
+ def __init__(self, name, properties):
+ FileBacked.__init__(self, name)
+ self.properties = properties
+
+ def Index(self):
+ '''Create the template data structures'''
+ self.template = Template(self.data, searchList=[self.properties])
+ self.template.properties = self.properties.properties
+ # put in owner permission detection
+
+ def BuildFile(self, entry, metadata):
+ '''Build literal file information'''
+ self.metadata = metadata
+ try:
+ entry.text = str(self.template)
+ except:
+ syslog(LOG_ERR, "TCheetah: Failed to template %s" % entry.get('name'))
+ raise PluginExecutionError
+ entry.attrib.update({'owner':'root', 'group':'root', 'perms':'0644'})
+
+
+class CheetahProperties(SingleXMLFileBacked):
+ '''Class for Cheetah properties'''
+ def Index(self):
+ '''Build data into an elementtree object for templating usage'''
+ try:
+ self.properties = XML(self.data)
+ del self.data
+ except ExpatError:
+ syslog(LOG_ERR, "TCheetah: Failed to parse properties")
+
+class TCheetah(Plugin):
+ '''The TCheetah generator implements a templating mechanism for configuration files'''
+ __name__ = 'TCheetah'
+ __version__ = '$Id$'
+ __author__ = 'bcfg-dev@mcs.anl.gov'
+
+ def __init__(self, core, datastore):
+ Plugin.__init__(self, core, datastore)
+ if self.data[-1] == '/':
+ self.data = self.data[:-1]
+ self.Entries['ConfigFile'] = {}
+ self.entries = {}
+ self.handles = {}
+ self.AddDirectoryMonitor('')
+ self.properties = CheetahProperties('%s/../etc/properties.xml' % self.data, self.core.fam)
+
+ def BuildEntry(self, entry, metadata):
+ '''Dispatch fetch calls to the correct object'''
+ self.entries[entry.get('name')].BuildFile(entry, metadata)
+
+ def MapName(self, name):
+ '''MapName finds the object corresponding to a particular file
+ the DirShadow MapName method maps filenames literally'''
+ return name
+
+ def HandleEvent(self, event):
+ '''Unified FAM event handler for DirShadow'''
+ #print "got event %s %s %s" % ( event.code2str(), event.filename, event.requestID)
+ action = event.code2str()
+ if event.filename[0] == '/':
+ return
+ epath = "".join([self.data, self.handles[event.requestID], event.filename])
+ identifier = self.MapName(epath[len(self.data):])
+ if action in ['exists', 'created']:
+ if isdir(epath):
+ self.AddDirectoryMonitor(epath[len(self.data):])
+ else:
+ if self.entries.has_key(identifier):
+ pass
+ else:
+ #try:
+ self.entries[identifier] = TemplateFile(epath, self.properties)
+ self.entries[identifier].HandleEvent(event)
+ self.Entries['ConfigFile'][identifier] = self.BuildEntry
+ #except:
+ # syslog(LOG_ERR, "TCheetah: bad format for file %s" % identifier)
+ elif action == 'changed':
+ if self.entries.has_key(identifier):
+ self.entries[identifier].HandleEvent(event)
+ elif action == 'deleted':
+ if self.entries.has_key[identifier]:
+ del self.entries[identifier]
+ del self.Entries['ConfigFile'][identifier]
+
+ def AddDirectoryMonitor(self, relative):
+ '''Add new directory to FAM structures'''
+ if not relative:
+ relative = '/'
+ if relative[-1] != '/':
+ relative += '/'
+ name = self.data + relative
+ if relative not in self.handles.values():
+ if not isdir(name):
+ print "Cheetah: Failed to open directory %s" % (name)
+ return
+ reqid = self.core.fam.AddMonitor(name, self)
+ self.handles[reqid] = relative