summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Plugins/Decisions.py
blob: 5521ea04542c1743e2271c786a90537435f6011b (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
""" The Decisions plugin provides a flexible method to whitelist or
blacklist certain entries. """

import os
import sys
import lxml.etree
import Bcfg2.Server.Plugin


class DecisionFile(Bcfg2.Server.Plugin.SpecificData):
    """ Representation of a Decisions XML file """

    def __init__(self, name, specific, encoding):
        Bcfg2.Server.Plugin.SpecificData.__init__(self, name, specific,
                                                  encoding)
        self.contents = None

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

    def get_decisions(self):
        """ Get a list of whitelist or blacklist tuples """
        return [(x.get('type'), x.get('name'))
                for x in self.contents.xpath('.//Decision')]


class Decisions(Bcfg2.Server.Plugin.EntrySet,
                Bcfg2.Server.Plugin.Plugin,
                Bcfg2.Server.Plugin.Decision):
    """ Decisions plugin

    Arguments:
    - `core`: Bcfg2.Core instance
    - `datastore`: File repository location
    """
    basename_is_regex = True
    __author__ = 'bcfg-dev@mcs.anl.gov'

    def __init__(self, core, datastore):
        Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore)
        Bcfg2.Server.Plugin.Decision.__init__(self)

        Bcfg2.Server.Plugin.EntrySet.__init__(self, '(white|black)list',
                                              self.data,
                                              DecisionFile,
                                              core.setup['encoding'])
        try:
            core.fam.AddMonitor(self.data, self)
        except OSError:
            err = sys.exc_info()[1]
            msg = 'Adding filemonitor for %s failed: %s' % (self.data, err)
            self.logger.error(msg)
            raise Bcfg2.Server.Plugin.PluginInitError(msg)

    def HandleEvent(self, event):
        """ Handle events on Decision files by passing them off to
        EntrySet.handle_event """
        if event.filename != self.path:
            return self.handle_event(event)

    def GetDecisions(self, metadata, mode):
        ret = []
        for cdt in self.get_matching(metadata):
            if os.path.basename(cdt).startswith(mode):
                ret.extend(cdt.get_decisions())
        return ret