diff options
author | Chris St. Pierre <chris.a.st.pierre@gmail.com> | 2011-08-31 14:28:12 -0400 |
---|---|---|
committer | Chris St. Pierre <chris.a.st.pierre@gmail.com> | 2011-08-31 14:28:12 -0400 |
commit | acdbf137595b45de2dda5b9ba3b33a1bb836e075 (patch) | |
tree | 650d64241c1e57941ba54d673a76686dc43ba488 | |
parent | 5b542f5d57ed6c7fdb7f9dc41427491eab5e7a45 (diff) | |
download | bcfg2-acdbf137595b45de2dda5b9ba3b33a1bb836e075.tar.gz bcfg2-acdbf137595b45de2dda5b9ba3b33a1bb836e075.tar.bz2 bcfg2-acdbf137595b45de2dda5b9ba3b33a1bb836e075.zip |
added regex support to Rules
-rw-r--r-- | doc/server/plugins/generators/rules.txt | 108 | ||||
-rw-r--r-- | src/lib/Server/Plugin.py | 35 | ||||
-rw-r--r-- | src/lib/Server/Plugins/Rules.py | 32 |
3 files changed, 122 insertions, 53 deletions
diff --git a/doc/server/plugins/generators/rules.txt b/doc/server/plugins/generators/rules.txt index 369fb8ae6..70d894958 100644 --- a/doc/server/plugins/generators/rules.txt +++ b/doc/server/plugins/generators/rules.txt @@ -77,34 +77,35 @@ Package Tag The Package Tag may have the following attributes: -+------------+----------------------------------------------+--------+ -| Name | Description | Values | -+============+==============================================+========+ -| name | Package Name | String | -+------------+----------------------------------------------+--------+ -| version | Package Version or version='noverify' to | String | -| | not do version checking in the Yum driver | | -| | only (temporary work a round). | | -+------------+----------------------------------------------+--------+ -| file | Package file name. Several other attributes | String | -| | (name, version) can be automatically defined | | -| | based on regular expressions defined in the | | -| | Pkgmgr plugin. | | -+------------+----------------------------------------------+--------+ -| simplefile | Package file name. No name parsing is | String | -| | performed, so no extra fields get set | | -+------------+----------------------------------------------+--------+ -| verify | verify='false' - do not do package | String | -| | verification | | -+------------+----------------------------------------------+--------+ -| multiarch | Comma separated list of the architectures of | String | -| | this package that should be installed. | | -+------------+----------------------------------------------+--------+ -| srcs | Filename creation rules for multiarch | String | -| | packages. | | -+------------+----------------------------------------------+--------+ -| type | Package type. (rpm, yum, apt,sysv,blast) | String | -+------------+----------------------------------------------+--------+ ++------------+----------------------------------------------+----------+ +| Name | Description | Values | ++============+==============================================+==========+ +| name | Package name or regular expression | String | +| | | or regex | ++------------+----------------------------------------------+----------+ +| version | Package Version or version='noverify' to | String | +| | not do version checking in the Yum driver | | +| | only (temporary work a round). | | ++------------+----------------------------------------------+----------+ +| file | Package file name. Several other attributes | String | +| | (name, version) can be automatically defined | | +| | based on regular expressions defined in the | | +| | Pkgmgr plugin. | | ++------------+----------------------------------------------+----------+ +| simplefile | Package file name. No name parsing is | String | +| | performed, so no extra fields get set | | ++------------+----------------------------------------------+----------+ +| verify | verify='false' - do not do package | String | +| | verification | | ++------------+----------------------------------------------+----------+ +| multiarch | Comma separated list of the architectures of | String | +| | this package that should be installed. | | ++------------+----------------------------------------------+----------+ +| srcs | Filename creation rules for multiarch | String | +| | packages. | | ++------------+----------------------------------------------+----------+ +| type | Package type. (rpm, yum, apt,sysv,blast) | String | ++------------+----------------------------------------------+----------+ Action Tag ---------- @@ -119,7 +120,8 @@ Service Tag +============+===============================+=====================================================+ | mode | Per Service Mode (New in 1.0) | (manual | default | supervised | interactive_only ) | +------------+-------------------------------+-----------------------------------------------------+ -| name | Service Name | String | +| name | Service name or regular | String or regex | +| | expression | | +------------+-------------------------------+-----------------------------------------------------+ | status | Should the service be on or | (on | off | ignore) | | | off (default: off). | | @@ -204,14 +206,21 @@ The Path tag has different values depending on the *type* attribute of the path specified in your configuration. Below is a set of tables which describe the attributes available for various Path types. +Attributes common to all Path tags: + ++----------+---------------------------------------------------+-----------------+ +| Name | Description | Values | ++==========+===================================================+=================+ +| name | Full path or regular expression matching the path | String or regex | ++----------+---------------------------------------------------+-----------------+ + + device ^^^^^^ +----------+---------------------+-------------------+ | Name | Description | Values | +==========+=====================+===================+ -| name | Name of the device | String | -+----------+---------------------+-------------------+ | dev_type | Type of device | (block|char|fifo) | +----------+---------------------+-------------------+ | owner | Device owner | String | @@ -231,8 +240,6 @@ directory +-------+------------------------------+------------+ | Name | Description | Values | +=======+==============================+============+ -| name | Directory Name | String | -+-------+------------------------------+------------+ | perms | Permissions of the directory | String | +-------+------------------------------+------------+ | owner | Owner of the directory | String | @@ -249,8 +256,6 @@ hardlink +------+----------------------+--------+ | Name | Description | Values | +======+======================+========+ -| name | Name of the hardlink | String | -+------+----------------------+--------+ | to | File to link to | String | +------+----------------------+--------+ @@ -260,9 +265,6 @@ nonexistent +-----------+--------------------+-------------+ | Name | Description | Values | +===========+====================+=============+ -| name | Name of the | String | -| | (nonexistent) file | | -+-----------+--------------------+-------------+ | type | Type of file | nonexistent | +-----------+--------------------+-------------+ | recursive | Recursively remove | true | @@ -275,8 +277,6 @@ permissions +-------+--------------------------+--------+ | Name | Description | Values | +=======+==========================+========+ -| name | Name of the file. | String | -+-------+--------------------------+--------+ | perms | Permissions of the file. | String | +-------+--------------------------+--------+ | owner | Owner of the file. | String | @@ -290,8 +290,6 @@ symlink +------+----------------------+--------+ | Name | Description | Values | +======+======================+========+ -| name | Name of the symlink. | String | -+------+----------------------+--------+ | to | File to link to | String | +------+----------------------+--------+ @@ -352,3 +350,29 @@ how to assign Rules to a host's literal configuration. <Service name='syslog' status='on' type='chkconfig'/> <Service name='vmware-tools' status='on' type='chkconfig'/> </Rules> + +Using Regular Expressions in Rules +================================== + +The ``name`` attribute in Rules supports the use of regular +expressions to match multiple abstract configuration entries. For +instance, to make all Service entries use the ``systemd`` +tool on Fedora 15 and the ``chkconfig`` tool on Fedora 14, you could +do:: + + <Rules priority="0"> + <Group name="fedora-15"> + <Service name=".*" type="systemd"/> + </Group> + <Group name="fedora-14"> + <Service name=".*" type="chkconfig"/> + </Group> + </Rules> + +Note that only one Rule can apply to any abstract entry, so you cannot +specify multiple regexs to match the same rule. In the use case +above, you would have to specify the services fully (except for type) +in the bundles. + +Attributes specified in a bundle have precedence over attributes +specified in Rules, so you can use Rules to set defaults. diff --git a/src/lib/Server/Plugin.py b/src/lib/Server/Plugin.py index 11d026c11..1e34b9ca4 100644 --- a/src/lib/Server/Plugin.py +++ b/src/lib/Server/Plugin.py @@ -728,7 +728,8 @@ class XMLSrc(XMLFileBacked): self.priority = int(xdata.get('priority')) except (ValueError, TypeError): if not self.noprio: - logger.error("Got bogus priority %s for file %s" % (xdata.get('priority'), self.name)) + logger.error("Got bogus priority %s for file %s" % + (xdata.get('priority'), self.name)) del xdata, data def Cache(self, metadata): @@ -778,16 +779,25 @@ class PrioDir(Plugin, Generator, XMLDirectoryBacked): except KeyError: self.Entries[itype] = {child: self.BindEntry} + def _matches(self, entry, metadata, rules): + return entry.get('name') in rules + def BindEntry(self, entry, metadata): - """Check package lists of package entries.""" - name = entry.get('name') - if False in [src.Cache(metadata) for src in - list(self.entries.values())]: + attrs = self.get_attrs(entry, metadata) + for key, val in list(attrs.items()): + entry.attrib[key] = val + + def get_attrs(self, entry, metadata): + """ get a list of attributes to add to the entry during the bind """ + if False in [src.Cache(metadata) + for src in list(self.entries.values())]: self.logger.error("Called before data loaded") raise PluginExecutionError matching = [src for src in list(self.entries.values()) - if src.cache and entry.tag in src.cache[1] - and name in src.cache[1][entry.tag]] + if (src.cache and + entry.tag in src.cache[1] and + self._matches(entry, metadata, + src.cache[1][entry.tag]))] if len(matching) == 0: raise PluginExecutionError elif len(matching) == 1: @@ -804,13 +814,18 @@ class PrioDir(Plugin, Generator, XMLDirectoryBacked): raise PluginExecutionError index = prio.index(max(prio)) - data = matching[index].cache[1][entry.tag][name] + for rname in matching[index].cache[1][entry.tag]: + if self._matches(entry, metadata, rname): + data = matching[index].cache[1][entry.tag][rname] + break if '__text__' in data: entry.text = data['__text__'] if '__children__' in data: [entry.append(copy.deepcopy(item)) for item in data['__children__']] - [entry.attrib.__setitem__(key, data[key]) for key in list(data.keys()) \ - if not key.startswith('__')] + + return dict([(key, data[key]) + for key in list(data.keys()) + if not key.startswith('__')]) # new unified EntrySet backend diff --git a/src/lib/Server/Plugins/Rules.py b/src/lib/Server/Plugins/Rules.py index eb0547cdb..e8412c6f1 100644 --- a/src/lib/Server/Plugins/Rules.py +++ b/src/lib/Server/Plugins/Rules.py @@ -1,11 +1,41 @@ """This generator provides rule-based entry mappings.""" __revision__ = '$Revision$' +import re import Bcfg2.Server.Plugin - class Rules(Bcfg2.Server.Plugin.PrioDir): """This is a generator that handles service assignments.""" name = 'Rules' __version__ = '$Id$' __author__ = 'bcfg-dev@mcs.anl.gov' + + def HandlesEntry(self, entry, metadata): + if entry.tag in self.Entries: + if entry.get("name") in self.Entries[entry.tag]: + return True + else: + for rule in self.Entries[entry.tag].keys(): + if re.search(rule, entry.get('name')): + return True + return False + + def HandleEntry(self, entry, metadata): + return self.BindEntry(entry, metadata) + + def BindEntry(self, entry, metadata): + attrs = self.get_attrs(entry, metadata) + for key, val in list(attrs.items()): + if key not in entry.attrib: + entry.attrib[key] = val + + def _matches(self, entry, metadata, rules): + if Bcfg2.Server.Plugin.PrioDir._matches(self, entry, metadata, rules): + return True + else: + # attempt regular expression matching + for rule in rules: + if re.search(rule, entry.get('name')): + return True + return False + |