Fixed default configuration loading and do not write default config if a config is specified.

Change-Id: I671f59256bbc5a5d5c68e3bfbabfd26375fbb406
This commit is contained in:
Florian Apolloner 2019-01-15 18:27:31 +01:00
parent e47f5afb2e
commit 5bae31785f
1 changed files with 23 additions and 16 deletions

View File

@ -7,11 +7,20 @@ import yaml
from django.utils.crypto import get_random_string
from dynaconf import LazySettings
settings = LazySettings(GLOBAL_ENV_FOR_DYNACONF="ARA", ENVVAR_FOR_DYNACONF="ARA_SETTINGS")
BASE_DIR = os.environ.get("ARA_BASE_DIR", os.path.expanduser("~/.ara/server"))
DEFAULT_CONFIG = os.path.join(BASE_DIR, "default_config.yaml")
settings = LazySettings(
GLOBAL_ENV_FOR_DYNACONF="ARA", ENVVAR_FOR_DYNACONF="ARA_SETTINGS", SETTINGS_MODULE_FOR_DYNACONF=DEFAULT_CONFIG
)
# reread BASE_DIR since it might have gotten changed in the config file.
BASE_DIR = settings.get("BASE_DIR", BASE_DIR)
# Django doesn't set up logging until it's too late to use it in settings.py.
# Set it up from the configuration so we can use it.
DEBUG = settings.get("DEBUG", False, "@bool")
LOG_LEVEL = settings.get("LOG_LEVEL", "INFO")
# fmt: off
LOGGING = {
@ -43,13 +52,6 @@ logging.config.dictConfig(LOGGING)
logger = logging.getLogger(__name__)
logger.debug("Loaded logging configuration")
# Ensure default base configuration/data directory exists
BASE_DIR = settings.get("BASE_DIR", os.path.expanduser("~/.ara"))
SERVER_DIR = settings.get("SERVER_DIR", os.path.join(BASE_DIR, "server"))
if not os.path.isdir(SERVER_DIR):
logger.info(f"Creating SERVER_DIR data directory: {SERVER_DIR}")
os.makedirs(SERVER_DIR, mode=0o700)
# Django built-in server and npm development server
ALLOWED_HOSTS = settings.get("ALLOWED_HOSTS", ["::1", "127.0.0.1", "localhost"])
CORS_ORIGIN_WHITELIST = settings.get("CORS_ORIGIN_WHITELIST", ["127.0.0.1:8000", "localhost:3000"])
@ -70,7 +72,7 @@ SECRET_KEY = get_secret_key()
# We're not expecting ARA to use multiple concurrent databases.
# Make it easier for users to specify the configuration for a single database.
DATABASE_ENGINE = settings.get("DATABASE_ENGINE", "django.db.backends.sqlite3")
DATABASE_NAME = settings.get("DATABASE_NAME", os.path.join(SERVER_DIR, "ansible.sqlite"))
DATABASE_NAME = settings.get("DATABASE_NAME", os.path.join(BASE_DIR, "ansible.sqlite"))
DATABASE_USER = settings.get("DATABASE_USER", None)
DATABASE_PASSWORD = settings.get("DATABASE_PASSWORD", None)
DATABASE_HOST = settings.get("DATABASE_HOST", None)
@ -142,10 +144,10 @@ USE_L10N = True
LANGUAGE_CODE = "en-us"
STATIC_URL = settings.get("STATIC_URL", "/static/")
STATIC_ROOT = settings.get("STATIC_ROOT", os.path.join(SERVER_DIR, "www", "static"))
STATIC_ROOT = settings.get("STATIC_ROOT", os.path.join(BASE_DIR, "www", "static"))
MEDIA_URL = settings.get("MEDIA_URL", "/media/")
MEDIA_ROOT = settings.get("MEDIA_ROOT", os.path.join(SERVER_DIR, "www", "media"))
MEDIA_ROOT = settings.get("MEDIA_ROOT", os.path.join(BASE_DIR, "www", "media"))
WSGI_APPLICATION = "ara.server.wsgi.application"
ROOT_URLCONF = "ara.server.urls"
@ -166,9 +168,16 @@ REST_FRAMEWORK = {
"TEST_REQUEST_DEFAULT_FORMAT": "json",
}
ARA_SETTINGS = os.getenv("ARA_SETTINGS", DEFAULT_CONFIG)
logger.info(f"Using configuration file: {ARA_SETTINGS}")
# Ensure default base configuration/data directory exists
if not os.path.isdir(BASE_DIR):
logger.info(f"Creating data & configuration directory: {BASE_DIR}")
os.makedirs(BASE_DIR, mode=0o700)
# TODO: Split this out to a CLI command (django-admin command ?)
DEFAULT_CONFIG = os.path.join(SERVER_DIR, "default_config.yaml")
if not os.path.exists(DEFAULT_CONFIG):
if not os.path.exists(DEFAULT_CONFIG) and "ARA_SETTINGS" not in os.environ:
CONFIG = dict(
BASE_DIR=BASE_DIR,
ALLOWED_HOSTS=ALLOWED_HOSTS,
@ -193,8 +202,6 @@ if not os.path.exists(DEFAULT_CONFIG):
# $ export ARA_SETTINGS={DEFAULT_CONFIG}
"""
logger.info(f"Writing default config to {DEFAULT_CONFIG}")
config_file.write(textwrap.dedent(comment))
yaml.dump({"default": CONFIG}, config_file, default_flow_style=False)
ARA_SETTINGS = os.getenv("ARA_SETTINGS", DEFAULT_CONFIG)
logger.info(f"Using configuration file: {ARA_SETTINGS}")