summaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2012-01-13 08:09:22 -0500
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2012-01-13 08:09:22 -0500
commit238f6446d09ca8e3b7a082876920922ee1eda4d2 (patch)
tree7a59ba455180c9950631334180af64ba68eeadee /src/lib
parent8c9b825595174455242b3b2f6ca96ddcbdcfff2e (diff)
parentb6654dd316f9cd4f6a6673a89cdd48cc1b38b82a (diff)
downloadbcfg2-238f6446d09ca8e3b7a082876920922ee1eda4d2.tar.gz
bcfg2-238f6446d09ca8e3b7a082876920922ee1eda4d2.tar.bz2
bcfg2-238f6446d09ca8e3b7a082876920922ee1eda4d2.zip
merged branch rules_regex
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/Server/Plugin.py49
-rw-r--r--src/lib/Server/Plugins/Defaults.py4
-rw-r--r--src/lib/Server/Plugins/Packages/PackagesConfig.py31
-rw-r--r--src/lib/Server/Plugins/Packages/Source.py5
-rw-r--r--src/lib/Server/Plugins/Packages/Yum.py14
-rw-r--r--src/lib/Server/Plugins/Packages/__init__.py31
-rw-r--r--src/lib/Server/Plugins/Rules.py16
7 files changed, 90 insertions, 60 deletions
diff --git a/src/lib/Server/Plugin.py b/src/lib/Server/Plugin.py
index d491d1609..4bfa3fdf5 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
@@ -1142,3 +1143,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 9e1ccfa37..b12d633f3 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 32552f25f..c66276179 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,
@@ -36,10 +44,14 @@ class Rules(Bcfg2.Server.Plugin.PrioDir):
# special case for Path tags:
# http://trac.mcs.anl.gov/projects/bcfg2/ticket/967
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)