diff options
author | Alexander Sulfrian <alexander.sulfrian@fu-berlin.de> | 2015-07-22 18:39:32 +0200 |
---|---|---|
committer | Alexander Sulfrian <alexander.sulfrian@fu-berlin.de> | 2015-08-26 19:12:30 +0200 |
commit | 6851a2931869aa9d9181b7b2d95f048aa5415a23 (patch) | |
tree | 5d56dbf8ecc48ff6a9f1970d91445366e34534f0 /src/lib/Bcfg2/Server/Plugins/Rules.py | |
parent | c7e67299381df961ff8274e9b53827c2bddbb47b (diff) | |
download | bcfg2-6851a2931869aa9d9181b7b2d95f048aa5415a23.tar.gz bcfg2-6851a2931869aa9d9181b7b2d95f048aa5415a23.tar.bz2 bcfg2-6851a2931869aa9d9181b7b2d95f048aa5415a23.zip |
Rules: New options replace_name to replace %{name} in attributes
If you use the regex feature of Rules/Defaults you may need the real name
of the matched entry in an attribute (for example home of POSIXUser).
You can now enable replace_name for rules or defaults and %{name} will be
replaces in the attribues of the Element before adding them to the target
entry.
This allows you to write something like that in Defaults to assing a default
home directory somewhere else to all users with unset home:
<POSIXUser name='.*' home='/somewhere/%{name}'/>
Diffstat (limited to 'src/lib/Bcfg2/Server/Plugins/Rules.py')
-rw-r--r-- | src/lib/Bcfg2/Server/Plugins/Rules.py | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/lib/Bcfg2/Server/Plugins/Rules.py b/src/lib/Bcfg2/Server/Plugins/Rules.py index a3f682ed6..cf659251c 100644 --- a/src/lib/Bcfg2/Server/Plugins/Rules.py +++ b/src/lib/Bcfg2/Server/Plugins/Rules.py @@ -1,10 +1,17 @@ """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' @@ -12,7 +19,10 @@ class Rules(Bcfg2.Server.Plugin.PrioDir): options = Bcfg2.Server.Plugin.PrioDir.options + [ Bcfg2.Options.BooleanOption( cf=("rules", "regex"), dest="rules_regex", - help="Allow regular expressions in Rules")] + 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) @@ -46,7 +56,22 @@ class Rules(Bcfg2.Server.Plugin.PrioDir): 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 |