diff options
author | Chris St. Pierre <chris.a.st.pierre@gmail.com> | 2013-11-25 11:24:35 -0500 |
---|---|---|
committer | Chris St. Pierre <chris.a.st.pierre@gmail.com> | 2013-11-25 11:24:35 -0500 |
commit | 16b4744544ab140c4ab9bc733a7dfa76cf4e578c (patch) | |
tree | a36ab3e0765707318866b5d3b69fe06a0bec96a9 /src | |
parent | bcea1949fa2a84e87c51d128a17a25a70d50ca13 (diff) | |
download | bcfg2-16b4744544ab140c4ab9bc733a7dfa76cf4e578c.tar.gz bcfg2-16b4744544ab140c4ab9bc733a7dfa76cf4e578c.tar.bz2 bcfg2-16b4744544ab140c4ab9bc733a7dfa76cf4e578c.zip |
Core: Avoid starting server if database is enabled but cannot be used
Plugins that use the database often act quite differently depending on
whether or not the database is enabled. If we start the server
without the database (e.g., the connection failed), then Very Strange
Things can happen.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/Bcfg2/Server/Core.py | 8 | ||||
-rw-r--r-- | src/lib/Bcfg2/Server/Plugin/helpers.py | 17 |
2 files changed, 17 insertions, 8 deletions
diff --git a/src/lib/Bcfg2/Server/Core.py b/src/lib/Bcfg2/Server/Core.py index 5ec1b5bce..c2cf6b7a4 100644 --- a/src/lib/Bcfg2/Server/Core.py +++ b/src/lib/Bcfg2/Server/Core.py @@ -228,11 +228,11 @@ class BaseCore(object): verbosity=0) self._database_available = True except ImproperlyConfigured: - err = sys.exc_info()[1] - self.logger.error("Django configuration problem: %s" % err) + self.logger.error("Django configuration problem: %s" % + sys.exc_info()[1]) except: - err = sys.exc_info()[1] - self.logger.error("Database update failed: %s" % err) + self.logger.error("Database update failed: %s" % + sys.exc_info()[1]) if do_chown and self._database_available: try: diff --git a/src/lib/Bcfg2/Server/Plugin/helpers.py b/src/lib/Bcfg2/Server/Plugin/helpers.py index d9e208746..f39609a5b 100644 --- a/src/lib/Bcfg2/Server/Plugin/helpers.py +++ b/src/lib/Bcfg2/Server/Plugin/helpers.py @@ -16,7 +16,7 @@ from Bcfg2.Compat import CmpMixin, wraps from Bcfg2.Server.Plugin.base import Debuggable, Plugin from Bcfg2.Server.Plugin.interfaces import Generator from Bcfg2.Server.Plugin.exceptions import SpecificityError, \ - PluginExecutionError + PluginExecutionError, PluginInitError try: import django # pylint: disable=W0611 @@ -131,6 +131,18 @@ class DatabaseBacked(Plugin): #: conform to the possible values that function can handle. option = "use_database" + def __init__(self, core, datastore): + Plugin.__init__(self, core, datastore) + use_db = self.core.setup.cfp.getboolean(self.section, + self.option, + default=False) + if use_db and not HAS_DJANGO: + raise PluginInitError("%s is True but Django not found" % + self.option) + elif use_db and not self.core.database_available: + raise PluginInitError("%s is True but the database is unavailable " + "due to prior errors" % self.option) + def _section(self): """ The section to look in for :attr:`DatabaseBacked.option` """ @@ -146,10 +158,7 @@ class DatabaseBacked(Plugin): default=False) if use_db and HAS_DJANGO and self.core.database_available: return True - elif not use_db: - return False else: - self.logger.error("%s is true but django not found" % self.option) return False @property |