From f5d3b73b36712944558f5529785b29621cb5cc11 Mon Sep 17 00:00:00 2001 From: Tim Laszlo Date: Wed, 15 Aug 2012 12:35:26 -0500 Subject: Core: add database_available flag to allow plugins to see availability --- src/lib/Bcfg2/Server/Core.py | 9 ++++++++- src/lib/Bcfg2/Server/Plugin.py | 2 +- src/lib/Bcfg2/Server/Plugins/DBStats.py | 4 ++-- src/lib/Bcfg2/Server/SchemaUpdater/__init__.py | 4 ++++ 4 files changed, 15 insertions(+), 4 deletions(-) (limited to 'src/lib/Bcfg2/Server') diff --git a/src/lib/Bcfg2/Server/Core.py b/src/lib/Bcfg2/Server/Core.py index f25542eae..4465abb9a 100644 --- a/src/lib/Bcfg2/Server/Core.py +++ b/src/lib/Bcfg2/Server/Core.py @@ -102,14 +102,15 @@ class BaseCore(object): # load plugins Bcfg2.settings.read_config(cfile=self.cfile, repo=self.datastore) + self._database_available = False # verify our database schema try: from Bcfg2.Server.SchemaUpdater import update_database, UpdaterError try: update_database() + self._database_available = True except UpdaterError: self.logger.error("Failed to update database schema") - raise CoreInitError except ImportError: # assume django is not installed pass @@ -623,3 +624,9 @@ class BaseCore(object): """Get the data of the decision list.""" client, metadata = self.resolve_client(address) return self.GetDecisions(metadata, mode) + + @property + def database_available(self): + """Is the database configured and available""" + return self._database_available + diff --git a/src/lib/Bcfg2/Server/Plugin.py b/src/lib/Bcfg2/Server/Plugin.py index 58e5dafeb..a9d8a2996 100644 --- a/src/lib/Bcfg2/Server/Plugin.py +++ b/src/lib/Bcfg2/Server/Plugin.py @@ -156,7 +156,7 @@ class DatabaseBacked(Plugin): use_db = self.core.setup.cfp.getboolean(self.name.lower(), "use_database", default=False) - if use_db and has_django: + if use_db and has_django and self.core.database_available: return True elif not use_db: return False diff --git a/src/lib/Bcfg2/Server/Plugins/DBStats.py b/src/lib/Bcfg2/Server/Plugins/DBStats.py index fa1f45a39..b8750f850 100644 --- a/src/lib/Bcfg2/Server/Plugins/DBStats.py +++ b/src/lib/Bcfg2/Server/Plugins/DBStats.py @@ -27,8 +27,8 @@ class DBStats(Bcfg2.Server.Plugin.ThreadedStatistics, Bcfg2.Server.Plugin.PullSource.__init__(self) self.cpath = "%s/Metadata/clients.xml" % datastore self.core = core - logger.debug("Searching for new models to " - "add to the statistics database") + if not self.core.database_available: + raise Bcfg2.Server.Plugin.PluginInitError def handle_statistic(self, metadata, data): newstats = data.find("Statistics") diff --git a/src/lib/Bcfg2/Server/SchemaUpdater/__init__.py b/src/lib/Bcfg2/Server/SchemaUpdater/__init__.py index ac33724a0..d57175cd7 100644 --- a/src/lib/Bcfg2/Server/SchemaUpdater/__init__.py +++ b/src/lib/Bcfg2/Server/SchemaUpdater/__init__.py @@ -1,4 +1,5 @@ from django.db import connection, DatabaseError +from django.core.exceptions import ImproperlyConfigured import django.core.management import logging import pkgutil @@ -232,6 +233,9 @@ def update_database(): except UpdaterError: raise + except ImproperlyConfigured: + logger.error("Django is not properly configured: %s" % traceback.format_exc().splitlines()[-1]) + raise UpdaterError except: logger.error("Error while updating the database") for x in traceback.format_exc().splitlines(): -- cgit v1.2.3-1-g7c22