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/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 +++++++---------------- 4 files changed, 23 insertions(+), 58 deletions(-) (limited to 'src/lib/Server/Plugins/Packages') 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 -- cgit v1.2.3-1-g7c22