From a8de10cde0b83743e8e453c8318cd1ab15e7c419 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Wed, 30 Oct 2013 10:02:38 -0400 Subject: DB: fixed how Django settings are loaded --- src/lib/Bcfg2/Server/Admin.py | 24 +++++++++++------------- src/lib/Bcfg2/Server/Core.py | 22 +++++++++++----------- src/lib/Bcfg2/Server/Reports/updatefix.py | 3 +-- src/lib/Bcfg2/Server/models.py | 4 ++-- 4 files changed, 25 insertions(+), 28 deletions(-) (limited to 'src/lib/Bcfg2/Server') diff --git a/src/lib/Bcfg2/Server/Admin.py b/src/lib/Bcfg2/Server/Admin.py index c82a6d7fd..6a56657cf 100644 --- a/src/lib/Bcfg2/Server/Admin.py +++ b/src/lib/Bcfg2/Server/Admin.py @@ -15,6 +15,7 @@ import argparse import lxml.etree import Bcfg2.Logger import Bcfg2.Options +import Bcfg2.DBSettings import Bcfg2.Server.Core import Bcfg2.Client.Proxy from Bcfg2.Server.Plugin import PullSource, Generator, MetadataConsistencyError @@ -22,10 +23,9 @@ from Bcfg2.Utils import hostnames2ranges, Executor, safe_input import Bcfg2.Server.Plugins.Metadata try: - import Bcfg2.settings - os.environ['DJANGO_SETTINGS_MODULE'] = 'Bcfg2.settings' from django.core.exceptions import ImproperlyConfigured from django.core import management + import django.conf import Bcfg2.Server.models HAS_DJANGO = True @@ -833,12 +833,11 @@ class _ReportsCmd(AdminCmd): # pylint: disable=W0223 self.reports_classes = () def setup(self): - # this has to be imported after options are parsed, - # because Django finalizes its settings as soon as it's - # loaded, which means that if we import this before - # Bcfg2.settings has been populated, Django gets a null - # configuration, and subsequent updates to Bcfg2.settings - # won't help. + # this has to be imported after options are parsed, because + # Django finalizes its settings as soon as it's loaded, which + # means that if we import this before Bcfg2.DBSettings has + # been populated, Django gets a null configuration, and + # subsequent updates to Bcfg2.DBSettings won't help. import Bcfg2.Reporting.models # pylint: disable=W0621 self.reports_entries = (Bcfg2.Reporting.models.Group, Bcfg2.Reporting.models.Bundle, @@ -884,7 +883,6 @@ if HAS_DJANGO: """ Sync the Django ORM with the configured database """ def run(self, setup): - management.setup_environ(Bcfg2.settings) Bcfg2.Server.models.load_models() try: management.call_command("syncdb", interactive=False, @@ -911,9 +909,9 @@ if HAS_REPORTS: # this has to be imported after options are parsed, # because Django finalizes its settings as soon as it's # loaded, which means that if we import this before - # Bcfg2.settings has been populated, Django gets a null - # configuration, and subsequent updates to Bcfg2.settings - # won't help. + # Bcfg2.DBSettings has been populated, Django gets a null + # configuration, and subsequent updates to + # Bcfg2.DBSettings won't help. from django.db.transaction import commit_on_success self.run = commit_on_success(self.run) @@ -1011,7 +1009,7 @@ if HAS_REPORTS: self.logger.debug("Filtering by maxdate: %s" % maxdate) ipurge = ipurge.filter(timestamp__lt=maxdate) - if Bcfg2.settings.DATABASES['default']['ENGINE'] == \ + if django.conf.settings.DATABASES['default']['ENGINE'] == \ 'django.db.backends.sqlite3': grp_limit = 100 else: diff --git a/src/lib/Bcfg2/Server/Core.py b/src/lib/Bcfg2/Server/Core.py index e3a9f7fce..9174878b2 100644 --- a/src/lib/Bcfg2/Server/Core.py +++ b/src/lib/Bcfg2/Server/Core.py @@ -14,7 +14,7 @@ import lxml.etree import Bcfg2.Server import Bcfg2.Logger import Bcfg2.Options -import Bcfg2.settings +import Bcfg2.DBSettings import Bcfg2.Server.Statistics import Bcfg2.Server.FileMonitor from itertools import chain @@ -24,14 +24,20 @@ from Bcfg2.Server.Plugin.exceptions import * # pylint: disable=W0401,W0614 from Bcfg2.Server.Plugin.interfaces import * # pylint: disable=W0401,W0614 from Bcfg2.Server.Plugin import track_statistics +try: + from django.core.exceptions import ImproperlyConfigured + from django.core import management + import django.conf + HAS_DJANGO = True +except ImportError: + HAS_DJANGO = False + try: import psyco psyco.full() except ImportError: pass -os.environ['DJANGO_SETTINGS_MODULE'] = 'Bcfg2.settings' - def exposed(func): """ Decorator that sets the ``exposed`` attribute of a function to @@ -198,10 +204,6 @@ class Core(object): #: RLock to be held on writes to the backend db self.db_write_lock = threading.RLock() - # generate Django ORM settings. this must be done _before_ we - # load plugins - Bcfg2.settings.read_config() - # mapping of group name => plugin name to record where groups # that are created by Connector plugins came from self._dynamic_groups = dict() @@ -232,9 +234,7 @@ class Core(object): #: Whether or not it's possible to use the Django database #: backend for plugins that have that capability self._database_available = False - if Bcfg2.settings.HAS_DJANGO: - from django.core.exceptions import ImproperlyConfigured - from django.core import management + if HAS_DJANGO: try: management.call_command("syncdb", interactive=False, verbosity=0) @@ -1343,7 +1343,7 @@ class NetworkCore(Core): self.ca = Bcfg2.Options.setup.ca if self._database_available: - db_settings = Bcfg2.settings.DATABASES['default'] + db_settings = django.conf.settings.DATABASES['default'] if (Bcfg2.Options.setup.daemon and Bcfg2.Options.setup.daemon_uid and db_settings['ENGINE'].endswith(".sqlite3") and diff --git a/src/lib/Bcfg2/Server/Reports/updatefix.py b/src/lib/Bcfg2/Server/Reports/updatefix.py index c3fbcd2e9..91c370994 100644 --- a/src/lib/Bcfg2/Server/Reports/updatefix.py +++ b/src/lib/Bcfg2/Server/Reports/updatefix.py @@ -1,5 +1,4 @@ -import Bcfg2.settings - +import Bcfg2.DBSettings from django.db import connection import django.core.management import sys diff --git a/src/lib/Bcfg2/Server/models.py b/src/lib/Bcfg2/Server/models.py index 51cc835dc..7150c245a 100644 --- a/src/lib/Bcfg2/Server/models.py +++ b/src/lib/Bcfg2/Server/models.py @@ -51,9 +51,9 @@ def load_models(plugins=None): """ load models from plugins specified in the config """ # this has to be imported after options are parsed, because Django # finalizes its settings as soon as it's loaded, which means that - # if we import this before Bcfg2.settings has been populated, + # if we import this before Bcfg2.DBSettings has been populated, # Django gets a null configuration, and subsequent updates to - # Bcfg2.settings won't help. + # Bcfg2.DBSettings won't help. from django.db import models global MODELS -- cgit v1.2.3-1-g7c22