summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/Bcfg2/Server')
-rw-r--r--src/lib/Bcfg2/Server/Admin.py17
-rw-r--r--src/lib/Bcfg2/Server/Core.py14
-rw-r--r--src/lib/Bcfg2/Server/Plugins/Reporting.py15
-rw-r--r--src/lib/Bcfg2/Server/Reports/reports/models.py2
-rw-r--r--src/lib/Bcfg2/Server/Reports/updatefix.py8
-rw-r--r--src/lib/Bcfg2/Server/models.py29
6 files changed, 53 insertions, 32 deletions
diff --git a/src/lib/Bcfg2/Server/Admin.py b/src/lib/Bcfg2/Server/Admin.py
index a17aeca62..b038980a1 100644
--- a/src/lib/Bcfg2/Server/Admin.py
+++ b/src/lib/Bcfg2/Server/Admin.py
@@ -25,15 +25,19 @@ import Bcfg2.Server.Plugins.Metadata
try:
from django.core.exceptions import ImproperlyConfigured
from django.core import management
+ import django
import django.conf
import Bcfg2.Server.models
HAS_DJANGO = True
- try:
- import south # pylint: disable=W0611
+ if django.VERSION[0] == 1 and django.VERSION[1] >= 7:
HAS_REPORTS = True
- except ImportError:
- HAS_REPORTS = False
+ else:
+ try:
+ import south # pylint: disable=W0611
+ HAS_REPORTS = True
+ except ImportError:
+ HAS_REPORTS = False
except ImportError:
HAS_DJANGO = False
HAS_REPORTS = False
@@ -902,7 +906,6 @@ if HAS_DJANGO:
""" Sync the Django ORM with the configured database """
def run(self, setup):
- Bcfg2.Server.models.load_models()
try:
Bcfg2.DBSettings.sync_databases(
interactive=False,
@@ -1206,6 +1209,10 @@ class CLI(Bcfg2.Options.CommandRegistry):
components=[self])
parser.add_options(self.subcommand_options)
parser.parse()
+ if django.VERSION[0] == 1 and django.VERSION[1] >= 7:
+ # this has been introduced in django 1.7, so pylint fails with
+ # older django releases
+ django.setup() # pylint: disable=E1101
def run(self):
""" Run bcfg2-admin """
diff --git a/src/lib/Bcfg2/Server/Core.py b/src/lib/Bcfg2/Server/Core.py
index 25acc8ac0..a9628c024 100644
--- a/src/lib/Bcfg2/Server/Core.py
+++ b/src/lib/Bcfg2/Server/Core.py
@@ -27,6 +27,7 @@ from Bcfg2.Server.Statistics import track_statistics
try:
from django.core.exceptions import ImproperlyConfigured
+ import django
import django.conf
HAS_DJANGO = True
except ImportError:
@@ -83,10 +84,14 @@ def close_db_connection(func):
""" The decorated function """
rv = func(self, *args, **kwargs)
if self._database_available: # pylint: disable=W0212
- from django import db
self.logger.debug("%s: Closing database connection" %
threading.current_thread().getName())
- db.close_connection()
+
+ if django.VERSION[0] == 1 and django.VERSION[1] >= 7:
+ for connection in django.db.connections.all():
+ connection.close()
+ else:
+ django.db.close_connection()
return rv
return inner
@@ -430,6 +435,7 @@ class Core(object):
self.logger.error("Unexpected instantiation failure for plugin %s"
% plugin, exc_info=1)
+ @close_db_connection
def shutdown(self):
""" Perform plugin and FAM shutdown tasks. """
if not self._running:
@@ -444,10 +450,6 @@ class Core(object):
for plugin in list(self.plugins.values()):
plugin.shutdown()
self.logger.info("%s: All plugins shut down" % self.name)
- if self._database_available:
- from django import db
- self.logger.info("%s: Closing database connection" % self.name)
- db.close_connection()
@property
def metadata_cache_mode(self):
diff --git a/src/lib/Bcfg2/Server/Plugins/Reporting.py b/src/lib/Bcfg2/Server/Plugins/Reporting.py
index 5c73546b4..e372006c7 100644
--- a/src/lib/Bcfg2/Server/Plugins/Reporting.py
+++ b/src/lib/Bcfg2/Server/Plugins/Reporting.py
@@ -9,12 +9,15 @@ from Bcfg2.Reporting.Transport.base import TransportError
from Bcfg2.Server.Plugin import Statistics, PullSource, Threaded, \
PluginInitError, PluginExecutionError
-# required for reporting
try:
- import south # pylint: disable=W0611
- HAS_SOUTH = True
+ import django
+ if django.VERSION[0] == 1 and django.VERSION[1] >= 7:
+ HAS_REPORTING = True
+ else:
+ import south # pylint: disable=W0611
+ HAS_REPORTING = True
except ImportError:
- HAS_SOUTH = False
+ HAS_REPORTING = False
def _rpc_call(method):
@@ -48,8 +51,8 @@ class Reporting(Statistics, Threaded, PullSource):
self.whoami = platform.node()
self.transport = None
- if not HAS_SOUTH:
- msg = "Django south is required for Reporting"
+ if not HAS_REPORTING:
+ msg = "Django 1.7+ or Django south is required for Reporting"
self.logger.error(msg)
raise PluginInitError(msg)
diff --git a/src/lib/Bcfg2/Server/Reports/reports/models.py b/src/lib/Bcfg2/Server/Reports/reports/models.py
index ac4c8eac4..67aa425d9 100644
--- a/src/lib/Bcfg2/Server/Reports/reports/models.py
+++ b/src/lib/Bcfg2/Server/Reports/reports/models.py
@@ -266,7 +266,7 @@ class Reason(models.Model):
current_to = models.CharField(max_length=1024, blank=True)
version = models.CharField(max_length=1024, blank=True)
current_version = models.CharField(max_length=1024, blank=True)
- current_exists = models.BooleanField() # False means its missing. Default True
+ current_exists = models.BooleanField(default=True) # False means its missing.
current_diff = models.TextField(max_length=1024*1024, blank=True)
is_binary = models.BooleanField(default=False)
is_sensitive = models.BooleanField(default=False)
diff --git a/src/lib/Bcfg2/Server/Reports/updatefix.py b/src/lib/Bcfg2/Server/Reports/updatefix.py
index 91c370994..09b218464 100644
--- a/src/lib/Bcfg2/Server/Reports/updatefix.py
+++ b/src/lib/Bcfg2/Server/Reports/updatefix.py
@@ -4,7 +4,7 @@ import django.core.management
import sys
import logging
import traceback
-from Bcfg2.Server.models import InternalDatabaseVersion
+from Bcfg2.Server.models import internal_database_version
logger = logging.getLogger('Bcfg2.Server.Reports.UpdateFix')
@@ -138,7 +138,7 @@ def rollupdate(current_version):
exc_info=1)
# since array start at 0 but version start at 1
# we add 1 to the normal count
- ret = InternalDatabaseVersion.objects.create(version=i + 1)
+ ret = internal_database_version().create(version=i + 1)
return ret
else:
return None
@@ -149,10 +149,10 @@ def update_database():
try:
logger.debug("Running upgrade of models to the new one")
django.core.management.call_command("syncdb", interactive=False, verbosity=0)
- know_version = InternalDatabaseVersion.objects.order_by('-version')
+ know_version = internal_database_version().order_by('-version')
if not know_version:
logger.debug("No version, creating initial version")
- know_version = InternalDatabaseVersion.objects.create(version=lastversion)
+ know_version = internal_database_version().create(version=lastversion)
else:
know_version = know_version[0]
logger.debug("Presently at %s" % know_version)
diff --git a/src/lib/Bcfg2/Server/models.py b/src/lib/Bcfg2/Server/models.py
index 8d6642a25..9c0153c74 100644
--- a/src/lib/Bcfg2/Server/models.py
+++ b/src/lib/Bcfg2/Server/models.py
@@ -8,6 +8,7 @@ import Bcfg2.Server.Plugins
LOGGER = logging.getLogger(__name__)
MODELS = []
+INTERNAL_DATABASE_VERSION = None
class _OptionContainer(object):
@@ -56,15 +57,23 @@ def load_models(plugins=None):
setattr(sys.modules[__name__], sym, obj)
MODELS.append(sym)
- class InternalDatabaseVersion(models.Model):
- """ Object that tell us to which version the database is """
- version = models.IntegerField()
- updated = models.DateTimeField(auto_now_add=True)
+def internal_database_version():
+ global INTERNAL_DATABASE_VERSION
- def __str__(self):
- return "version %d updated %s" % (self.version,
- self.updated.isoformat())
+ if INTERNAL_DATABASE_VERSION is None:
+ from django.db import models
+ class InternalDatabaseVersion(models.Model):
+ """ Object that tell us to which version the database is """
+ version = models.IntegerField()
+ updated = models.DateTimeField(auto_now_add=True)
- class Meta: # pylint: disable=C0111,W0232
- app_label = "reports"
- get_latest_by = "version"
+ def __str__(self):
+ return "version %d updated %s" % (self.version,
+ self.updated.isoformat())
+
+ class Meta: # pylint: disable=C0111,W0232
+ app_label = "reports"
+ get_latest_by = "version"
+ INTERNAL_DATABASE_VERSION = InternalDatabaseVersion
+
+ return INTERNAL_DATABASE_VERSION.objects