Improve database engine error handling when starting the server

This will check if the database driver library is installed before
attempting to run the server.

Fixes: https://github.com/ansible-community/ara/issues/63
Change-Id: I42a7eca4909a560aba95d725021f4db1f9682428
This commit is contained in:
David Moreau Simard 2019-11-19 10:49:51 -05:00
parent d82857d4cb
commit a00c2f42e6
No known key found for this signature in database
GPG Key ID: 938880DAFC753E80
2 changed files with 26 additions and 2 deletions

View File

@ -21,7 +21,7 @@ import sys
from django.conf import settings
from ara.setup.exceptions import MissingDjangoException
from ara.setup.exceptions import MissingDjangoException, MissingMysqlclientException, MissingPsycopgException
def main():
@ -32,6 +32,18 @@ def main():
except ImportError as e:
raise MissingDjangoException from e
if settings.DATABASE_ENGINE == "django.db.backends.postgresql":
try:
import psycopg2 # noqa
except ImportError as e:
raise MissingPsycopgException from e
if settings.DATABASE_ENGINE == "django.db.backends.mysql":
try:
import MySQLdb # noqa
except ImportError as e:
raise MissingMysqlclientException from e
execute_from_command_line(sys.argv)
print("[ara] Using settings file: %s" % settings.ARA_SETTINGS)

View File

@ -18,5 +18,17 @@
class MissingDjangoException(Exception):
def __init__(self):
exc = "Unable to import Django: the server dependencies can be installed with 'pip install ara[server]'"
exc = "The server dependencies must be installed to record data offline or run the API server."
super().__init__(exc)
class MissingPsycopgException(Exception):
def __init__(self):
exc = "The psycopg2 python library must be installed in order to use the PostgreSQL database engine."
super().__init__(exc)
class MissingMysqlclientException(Exception):
def __init__(self):
exc = "The mysqlclient python library must be installed in order to use the MySQL database engine."
super().__init__(exc)