From 5fc3effb174ff6e9fbfd05346134ac8861477884 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Fri, 30 Dec 2011 09:50:05 -0500 Subject: added SimpleConfig plugin for easy config files; made Packages and Rules use SimpleConfig; made regex in rules off by default, but configurable in rules.conf --- src/lib/Server/Plugin.py | 49 +++++++++++++++++++++++ src/lib/Server/Plugins/Defaults.py | 4 ++ src/lib/Server/Plugins/Packages/PackagesConfig.py | 31 ++++---------- src/lib/Server/Plugins/Packages/Source.py | 5 +-- src/lib/Server/Plugins/Packages/Yum.py | 14 ++----- src/lib/Server/Plugins/Packages/__init__.py | 31 +++++--------- src/lib/Server/Plugins/Rules.py | 16 +++++++- 7 files changed, 90 insertions(+), 60 deletions(-) (limited to 'src/lib') diff --git a/src/lib/Server/Plugin.py b/src/lib/Server/Plugin.py index 3dc1cab38..41ac9dc20 100644 --- a/src/lib/Server/Plugin.py +++ b/src/lib/Server/Plugin.py @@ -11,6 +11,7 @@ import posixpath import re import sys import threading +from Bcfg2.Bcfg2Py3k import ConfigParser from lxml.etree import XML, XMLSyntaxError @@ -1133,3 +1134,51 @@ class GroupSpool(Plugin, Generator): return reqid = self.core.fam.AddMonitor(name, self) self.handles[reqid] = relative + +class SimpleConfig(FileBacked, + ConfigParser.SafeConfigParser): + ''' a simple plugin config using ConfigParser ''' + _required = True + + def __init__(self, plugin): + filename = os.path.join(plugin.data, plugin.name.lower() + ".conf") + self.plugin = plugin + self.fam = self.plugin.core.fam + Bcfg2.Server.Plugin.FileBacked.__init__(self, filename) + ConfigParser.SafeConfigParser.__init__(self) + + if (self._required or + (not self._required and os.path.exists(self.name))): + self.fam.AddMonitor(self.name, self) + + def Index(self): + """ Build local data structures """ + for section in self.sections(): + self.remove_section(section) + self.read(self.name) + + def get(self, section, option, default=None): + """ convenience method for getting config items """ + try: + return ConfigParser.SafeConfigParser.get(self, section, option) + except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): + if default is not None: + return default + else: + raise + + def getboolean(self, section, option, default=None): + """ convenience method for getting boolean config items """ + try: + return ConfigParser.SafeConfigParser.getboolean(self, + section, option) + except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): + if default is not None: + return default + else: + raise + except ValueError: + if default is not None: + return default + else: + raise diff --git a/src/lib/Server/Plugins/Defaults.py b/src/lib/Server/Plugins/Defaults.py index 23104946e..b208c1126 100644 --- a/src/lib/Server/Plugins/Defaults.py +++ b/src/lib/Server/Plugins/Defaults.py @@ -49,3 +49,7 @@ class Defaults(Bcfg2.Server.Plugins.Rules.Rules, finally: if is_bound: entry.tag = "Bound" + entry.tag + + def _regex_enabled(self): + """ Defaults depends on regex matching, so force it enabled """ + return True diff --git a/src/lib/Server/Plugins/Packages/PackagesConfig.py b/src/lib/Server/Plugins/Packages/PackagesConfig.py index d3732bf96..dd39bb495 100644 --- a/src/lib/Server/Plugins/Packages/PackagesConfig.py +++ b/src/lib/Server/Plugins/Packages/PackagesConfig.py @@ -1,33 +1,18 @@ -import os import logging -from Bcfg2.Bcfg2Py3k import ConfigParser -from Bcfg2.Server.Plugins.Packages import * +import Bcfg2.Server.Plugin logger = logging.getLogger('Packages') -class PackagesConfig(Bcfg2.Server.Plugin.FileBacked, - ConfigParser.SafeConfigParser): - def __init__(self, filename, fam, packages): - Bcfg2.Server.Plugin.FileBacked.__init__(self, filename) - ConfigParser.SafeConfigParser.__init__(self) - - self.fam = fam - # packages.conf isn't strictly necessary, so only set a - # monitor if it exists. if it gets added, that will require a - # server restart - if os.path.exists(self.name): - self.fam.AddMonitor(self.name, self) - - self.pkg_obj = packages - +class PackagesConfig(Bcfg2.Server.Plugin.SimpleConfig): + _required = False + def Index(self): """ Build local data structures """ - for section in self.sections(): - self.remove_section(section) - self.read(self.name) - if self.pkg_obj.sources.loaded: + Bcfg2.Server.Plugin.SimpleConfig.Index(self) + + if self.plugin.sources.loaded: # only reload Packages plugin if sources have been loaded. # otherwise, this is getting called on server startup, and # we have to wait until all sources have been indexed # before we can call Packages.Reload() - self.pkg_obj.Reload() + self.plugin.Reload() diff --git a/src/lib/Server/Plugins/Packages/Source.py b/src/lib/Server/Plugins/Packages/Source.py index 72c7a4bfd..dbb510053 100644 --- a/src/lib/Server/Plugins/Packages/Source.py +++ b/src/lib/Server/Plugins/Packages/Source.py @@ -257,9 +257,8 @@ class Source(object): if not found_arch: return False - if (self.config.has_section("global") and - self.config.has_option("global", "magic_groups") and - self.config.getboolean("global", "magic_groups") == False): + if self.config.getboolean("global", "magic_groups", + default=True) == False: return True else: for group in self.basegroups: diff --git a/src/lib/Server/Plugins/Packages/Yum.py b/src/lib/Server/Plugins/Packages/Yum.py index 369b7a7d2..f3cb5a532 100644 --- a/src/lib/Server/Plugins/Packages/Yum.py +++ b/src/lib/Server/Plugins/Packages/Yum.py @@ -113,11 +113,8 @@ class YumCollection(Collection): "%s-yum.conf" % self.cachekey) self.write_config() - try: - self.helper = self.config.get("yum", "helper") - except ConfigParser.NoOptionError: - self.helper = "/usr/sbin/bcfg2-yum-helper" - + self.helper = self.config.get("yum", "helper", + default="/usr/sbin/bcfg2-yum-helper") if has_pulp: _setup_pulp(self.config) @@ -192,11 +189,8 @@ class YumCollection(Collection): for key in needkeys: # figure out the path of the key on the client - try: - keydir = self.config.get("global", "gpg_keypath") - except (ConfigParser.NoOptionError, - ConfigParser.NoSectionError): - keydir = "/etc/pki/rpm-gpg" + keydir = self.config.get("global", "gpg_keypath", + default="/etc/pki/rpm-gpg") remotekey = os.path.join(keydir, os.path.basename(key)) localkey = os.path.join(self.keypath, os.path.basename(key)) kdata = open(localkey).read() diff --git a/src/lib/Server/Plugins/Packages/__init__.py b/src/lib/Server/Plugins/Packages/__init__.py index 5b1515920..b8babfac2 100644 --- a/src/lib/Server/Plugins/Packages/__init__.py +++ b/src/lib/Server/Plugins/Packages/__init__.py @@ -39,25 +39,20 @@ class Packages(Bcfg2.Server.Plugin.Plugin, os.makedirs(self.keypath) # set up config files - self.config = PackagesConfig(os.path.join(self.data, "packages.conf"), - core.fam, self) + self.config = PackagesConfig(self) self.sources = PackagesSources(os.path.join(self.data, "sources.xml"), self.cachepath, core.fam, self, self.config) @property def disableResolver(self): - try: - return self.config.get("global", "resolver").lower() == "disabled" - except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): - return False + return self.config.get("global", "resolver", + default="enabled").lower() == "disabled" @property def disableMetaData(self): - try: - return self.config.get("global", "metadata").lower() == "disabled" - except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): - return False + return self.config.get("global", "metadata", + default="enabled").lower() == "disabled" def create_config(self, entry, metadata): """ create yum/apt config for the specified host """ @@ -78,13 +73,8 @@ class Packages(Bcfg2.Server.Plugin.Plugin, entry.set('version', 'auto') entry.set('type', collection.ptype) elif entry.tag == 'Path': - if (self.config.has_section("global") and - ((self.config.has_option("global", "yum_config") and - entry.get("name") == self.config.get("global", - "yum_config")) or - (self.config.has_option("global", "apt_config") and - entry.get("name") == self.config.get("global", - "apt_config")))): + if (entry.get("name") == self.config.get("global", "yum_config") or + entry.get("name") == self.config.get("global", "apt_config")): self.create_config(entry, metadata) def HandlesEntry(self, entry, metadata): @@ -94,11 +84,8 @@ class Packages(Bcfg2.Server.Plugin.Plugin, return True elif entry.tag == 'Path': # managed entries for yum/apt configs - if ((self.config.has_option("global", "yum_config") and - entry.get("name") == self.config.get("global", - "yum_config")) or - (self.config.has_option("global", "apt_config") and - entry.get("name") == self.config.get("global", "apt_config"))): + if (entry.get("name") == self.config.get("global", "yum_config") or + entry.get("name") == self.config.get("global", "apt_config")): return True return False diff --git a/src/lib/Server/Plugins/Rules.py b/src/lib/Server/Plugins/Rules.py index a146dca6a..fde0f3d59 100644 --- a/src/lib/Server/Plugins/Rules.py +++ b/src/lib/Server/Plugins/Rules.py @@ -4,12 +4,20 @@ __revision__ = '$Revision$' import re import Bcfg2.Server.Plugin +class RulesConfig(Bcfg2.Server.Plugin.SimpleConfig): + _required = False + 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 __init__(self, core, datastore): + Bcfg2.Server.Plugin.PrioDir.__init__(self, core, datastore) + self.config = RulesConfig(self) + self._regex_cache = dict() + def HandlesEntry(self, entry, metadata): if entry.tag in self.Entries: return self._matches(entry, metadata, @@ -28,10 +36,14 @@ class Rules(Bcfg2.Server.Plugin.PrioDir): def _matches(self, entry, metadata, rules): if Bcfg2.Server.Plugin.PrioDir._matches(self, entry, metadata, rules): return True - else: + elif self._regex_enabled: # attempt regular expression matching for rule in rules: - if re.match("%s$" % rule, entry.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 _regex_enabled(self): + return self.config.getboolean("rules", "regex", default=False) -- cgit v1.2.3-1-g7c22