summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Plugins/Rules.py
blob: cf659251c55d55f43cb4e44a029a779b7f9f60a4 (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
69
70
71
72
73
74
75
76
77
"""This generator provides rule-based entry mappings."""

import copy
import re
import string
import Bcfg2.Options
import Bcfg2.Server.Plugin


class NameTemplate(string.Template):
    """Simple subclass of string.Template with a custom delimiter."""
    delimiter = '%'


class Rules(Bcfg2.Server.Plugin.PrioDir):
    """This is a generator that handles service assignments."""
    __author__ = 'bcfg-dev@mcs.anl.gov'

    options = Bcfg2.Server.Plugin.PrioDir.options + [
        Bcfg2.Options.BooleanOption(
            cf=("rules", "regex"), dest="rules_regex",
            help="Allow regular expressions in Rules"),
        Bcfg2.Options.BooleanOption(
            cf=("rules", "replace_name"), dest="rules_replace_name",
            help="Replace %{name} in attributes with name of target entry")]

    def __init__(self, core):
        Bcfg2.Server.Plugin.PrioDir.__init__(self, core)
        self._regex_cache = dict()

    def HandlesEntry(self, entry, metadata):
        for src in self.entries.values():
            for candidate in src.XMLMatch(metadata).xpath("//%s" % entry.tag):
                if self._matches(entry, metadata, candidate):
                    return True
        return False

    HandleEntry = Bcfg2.Server.Plugin.PrioDir.BindEntry

    def _matches(self, entry, metadata, candidate):
        if Bcfg2.Server.Plugin.PrioDir._matches(self, entry, metadata,
                                                candidate):
            return True
        elif (entry.tag == "Path" and
              entry.get('name').rstrip("/") ==
              candidate.get("name").rstrip("/")):
            # special case for Path tags:
            # http://trac.mcs.anl.gov/projects/bcfg2/ticket/967
            return True
        elif self._regex_enabled:
            # attempt regular expression matching
            rule = candidate.get("name")
            if rule not in self._regex_cache:
                self._regex_cache[rule] = re.compile("%s$" % rule)
            if self._regex_cache[rule].match(entry.get('name')):
                return True
        return False

    def _apply(self, entry, data):
        if self._replace_name_enabled:
            data = copy.deepcopy(data)
            for key, val in list(data.attrib.items()):
                data.attrib[key] = NameTemplate(val).safe_substitute(
                    name=entry.get('name'))

        Bcfg2.Server.Plugin.PrioDir._apply(self, entry, data)

    @property
    def _regex_enabled(self):
        """ Return True if rules regexes are enabled, False otherwise """
        return Bcfg2.Options.setup.rules_regex

    @property
    def _replace_name_enabled(self):
        """ Return True if the replace_name feature is enabled,
        False otherwise """
        return Bcfg2.Options.setup.rules_replace_name