diff options
author | Narayan Desai <desai@mcs.anl.gov> | 2009-07-15 12:04:19 +0000 |
---|---|---|
committer | Narayan Desai <desai@mcs.anl.gov> | 2009-07-15 12:04:19 +0000 |
commit | f7c16272eed2b130e05ee23509cbee35575bc411 (patch) | |
tree | f625a701282b0bfb2d88760b78732299f8958b9d /src | |
parent | 9e6d589d2d1b3ed536354bf7ec61601d06cad75b (diff) | |
download | bcfg2-f7c16272eed2b130e05ee23509cbee35575bc411.tar.gz bcfg2-f7c16272eed2b130e05ee23509cbee35575bc411.tar.bz2 bcfg2-f7c16272eed2b130e05ee23509cbee35575bc411.zip |
GroupPatterns plugin: initial implementation
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@5327 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/Server/Plugins/GroupPatterns.py | 68 | ||||
-rw-r--r-- | src/lib/Server/Plugins/__init__.py | 3 | ||||
-rwxr-xr-x | src/sbin/bcfg2-repo-validate | 4 |
3 files changed, 73 insertions, 2 deletions
diff --git a/src/lib/Server/Plugins/GroupPatterns.py b/src/lib/Server/Plugins/GroupPatterns.py new file mode 100644 index 000000000..1a5d590c1 --- /dev/null +++ b/src/lib/Server/Plugins/GroupPatterns.py @@ -0,0 +1,68 @@ + +import re, lxml.etree +import Bcfg2.Server.Plugin + +class PatternMap(object): + def __init__(self, pattern, groupname): + self.pattern = pattern + self.re = re.compile(pattern) + self.groupname = groupname + + def process(self, name): + match = self.re.match(name) + if not match: + return None + ret = self.groupname + sub = match.groups() + for idx in range(len(sub)): + ret = ret.replace('$%s' % (idx+1), sub[idx]) + return ret + +class PatternFile(Bcfg2.Server.Plugin.SingleXMLFileBacked): + def __init__(self, filename, fam): + Bcfg2.Server.Plugin.SingleXMLFileBacked.__init__(self, filename, fam) + self.patterns = [] + + def Index(self): + self.patterns = [] + try: + parsed = lxml.etree.XML(self.data) + except: + Bcfg2.Server.Plugin.logger.error("Failed to read file %s" % self.name) + return + for entry in parsed.findall('GroupPattern'): + try: + pat = entry.find('NamePattern').text + grp = entry.find('Group').text + self.patterns.append(PatternMap(pat, grp)) + except: + Bcfg2.Server.Plugin.logger.error(\ + "GroupPatterns: Failed to initialize pattern %s" % \ + (entry.get('pattern'))) + + def process_patterns(self, hostname): + ret = [] + for pattern in self.patterns: + try: + gn = pattern.process(hostname) + if gn: + ret.append(gn) + except: + Bcfg2.Server.Plugin.logger.error(\ + "GroupPatterns: Failed to process pattern %s for %s" % \ + (pattern.pattern, hostname), exc_info=1) + return ret + +class GroupPatterns(Bcfg2.Server.Plugin.Plugin, + Bcfg2.Server.Plugin.Connector): + name = "GroupPatterns" + experimental = True + + def __init__(self, core, datastore): + Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore) + Bcfg2.Server.Plugin.Connector.__init__(self) + self.config = PatternFile(self.data + '/config.xml', + core.fam) + + def get_additional_groups(self, metadata): + return self.config.process_patterns(metadata.hostname) diff --git a/src/lib/Server/Plugins/__init__.py b/src/lib/Server/Plugins/__init__.py index 48b6caffb..a8e7c3f63 100644 --- a/src/lib/Server/Plugins/__init__.py +++ b/src/lib/Server/Plugins/__init__.py @@ -4,4 +4,5 @@ __revision__ = '$Revision$' __all__ = ['Account', 'Base', 'Bundler', 'Cfg', 'Decisions', 'GBundler', 'Git', 'Hostbase', 'Metadata', 'NagiosGen', 'Packages', 'Properties', 'Probes', 'Pkgmgr', 'Rules', 'SSHbase', 'Snapshots' 'Statistics', - 'Svcmgr', 'Svn', 'TCheetah', 'Trigger', 'SGenshi', 'TGenshi', 'Vhost'] + 'Svcmgr', 'Svn', 'TCheetah', 'Trigger', 'SGenshi', 'TGenshi', + 'GroupPatterns'] diff --git a/src/sbin/bcfg2-repo-validate b/src/sbin/bcfg2-repo-validate index 62df87447..cb9caca6e 100755 --- a/src/sbin/bcfg2-repo-validate +++ b/src/sbin/bcfg2-repo-validate @@ -71,6 +71,7 @@ if __name__ == '__main__': deps_list = glob.glob("%s/Deps/*.xml" % repo) dec_list = glob.glob("%s/Decisions/*" % repo) pkgcfg_list = glob.glob("%s/Packages/config.xml" % repo) + gp_list = glob.glob('%s/GroupPatterns/config.xml' % repo) # warn on duplicate Pkgmgr entries with the same priority pset = set() @@ -104,7 +105,8 @@ if __name__ == '__main__': 'services':(services_list, "%s/services.xsd"), 'deps':(deps_list, "%s/deps.xsd"), 'decisions': (dec_list, "%s/decisions.xsd"), - 'packages': (pkgcfg_list, "%s/packages.xsd"), + 'packages': (pkgcfg_list, "%s/packages.xsd"), + 'grouppatterns': (gp_list, "%s/grouppatterns.xsd"), } failures = 0 |