summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Plugins/GroupPatterns.py
blob: 1a5d590c1d5b4196c5d02c9f17e7e6c233c61af7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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)