summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Plugins/Decisions.py
blob: 556f75502b0d785398f21f47e79007e004dea3fb (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
import logging
import lxml.etree
import sys

import Bcfg2.Server.Plugin
logger = logging.getLogger('Bcfg2.Plugins.Decisions')

class DecisionFile(Bcfg2.Server.Plugin.SpecificData):
    def handle_event(self, event):
        Bcfg2.Server.Plugin.SpecificData.handle_event(self, event)
        self.contents = lxml.etree.XML(self.data)

    def get_decisions(self):
        return [(x.get('type'), x.get('name')) for x in self.contents.xpath('.//Decision')]

class DecisionSet(Bcfg2.Server.Plugin.EntrySet):
    def __init__(self, path, fam, encoding):
        """Container for decision specification files.

        Arguments:
        - `path`: repository path
        - `fam`: reference to the file monitor
        - `encoding`: XML character encoding

        """
        pattern = '(white|black)list'
        Bcfg2.Server.Plugin.EntrySet.__init__(self, pattern, path, \
                                              DecisionFile, encoding)
        try:
            fam.AddMonitor(path, self)
        except OSError:
            e = sys.exc_info()[1]
            logger.error('Adding filemonitor for %s failed. '
                         'Make sure directory exists' % path)
            raise Bcfg2.Server.Plugin.PluginInitError(e)

    def HandleEvent(self, event):
        if event.filename != self.path:
            return self.handle_event(event)

    def GetDecisions(self, metadata, mode):
        ret = []
        candidates = [c for c in self.get_matching(metadata)
                      if c.name.split('/')[-1].startswith(mode)]
        for c in candidates:
            ret += c.get_decisions()
        return ret

class Decisions(DecisionSet,
                Bcfg2.Server.Plugin.Plugin,
                Bcfg2.Server.Plugin.Decision):
    name = 'Decisions'
    __version__ = '$Id$'
    __author__ = 'bcfg-dev@mcs.anl.gov'

    def __init__(self, core, datastore):
        """Decisions plugins

        Arguments:
        - `core`: Bcfg2.Core instance
        - `datastore`: File repository location

        """
        Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore)
        Bcfg2.Server.Plugin.Decision.__init__(self)
        DecisionSet.__init__(self, self.data, core.fam, core.encoding)