server: validate that settings.yaml exists before bootstrapping

Otherwise, if settings.yaml could not be read, the server would silently
fall back to the default settings.
Now the server will refuse to start with a descriptive error message if
the settings.yaml file doesn't exist or can't be accessed.

Fixes: https://github.com/ansible-community/ara/issues/117
Change-Id: I8ac3001b7b19ead619ee911e19cb210a347f1c0a
This commit is contained in:
David Moreau Simard 2020-04-14 16:04:47 -04:00
parent 821b0fe880
commit c86758350a
No known key found for this signature in database
GPG Key ID: 938880DAFC753E80
2 changed files with 18 additions and 2 deletions

View File

@ -21,7 +21,12 @@ import sys
from django.conf import settings
from ara.setup.exceptions import MissingDjangoException, MissingMysqlclientException, MissingPsycopgException
from ara.setup.exceptions import (
MissingDjangoException,
MissingMysqlclientException,
MissingPsycopgException,
MissingSettingsException,
)
def main():
@ -32,6 +37,12 @@ def main():
except ImportError as e:
raise MissingDjangoException from e
# Validate that the settings file exists and is readable before bootstrapping
if not os.path.exists(settings.ARA_SETTINGS):
print("[ara] Unable to access or read settings file: %s" % settings.ARA_SETTINGS)
raise MissingSettingsException
print("[ara] Using settings file: %s" % settings.ARA_SETTINGS)
if settings.DATABASE_ENGINE == "django.db.backends.postgresql":
try:
import psycopg2 # noqa
@ -45,7 +56,6 @@ def main():
raise MissingMysqlclientException from e
execute_from_command_line(sys.argv)
print("[ara] Using settings file: %s" % settings.ARA_SETTINGS)
if __name__ == "__main__":

View File

@ -32,3 +32,9 @@ 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)
class MissingSettingsException(Exception):
def __init__(self):
exc = "The specified settings file does not exist or permissions are insufficient to read it."
super().__init__(exc)