Renamed most of config to settings and dropped the default_ prefix.

Dropping the default prefix from the default settings file allows a user
to run an ARA production config by just changing that file. This seems
more in line with default configuration search paths of most other
programs out there.

Since the generated SECRET_KEY should be safe, there should be no
problem in reusing the generated file.

Change-Id: I1cfbc04cba378d7bda9386b344efae85b3490296
This commit is contained in:
Florian Apolloner 2019-01-15 18:37:33 +01:00
parent 2c8eb916b2
commit 600a189b21
1 changed files with 17 additions and 16 deletions

View File

@ -8,10 +8,10 @@ from django.utils.crypto import get_random_string
from dynaconf import LazySettings
BASE_DIR = os.environ.get("ARA_BASE_DIR", os.path.expanduser("~/.ara/server"))
DEFAULT_CONFIG = os.path.join(BASE_DIR, "default_config.yaml")
DEFAULT_SETTINGS = os.path.join(BASE_DIR, "settings.yaml")
settings = LazySettings(
GLOBAL_ENV_FOR_DYNACONF="ARA", ENVVAR_FOR_DYNACONF="ARA_SETTINGS", SETTINGS_MODULE_FOR_DYNACONF=DEFAULT_CONFIG
GLOBAL_ENV_FOR_DYNACONF="ARA", ENVVAR_FOR_DYNACONF="ARA_SETTINGS", SETTINGS_MODULE_FOR_DYNACONF=DEFAULT_SETTINGS
)
# reread BASE_DIR since it might have gotten changed in the config file.
@ -62,7 +62,7 @@ ADMINS = settings.get("ADMINS", ())
def get_secret_key():
if not settings.get("SECRET_KEY"):
logger.warn(f"No configuration found for SECRET_KEY. Generating a random key...")
logger.warn(f"No setting found for SECRET_KEY. Generating a random key...")
return get_random_string(length=50)
return settings.get("SECRET_KEY")
@ -168,17 +168,18 @@ REST_FRAMEWORK = {
"TEST_REQUEST_DEFAULT_FORMAT": "json",
}
ARA_SETTINGS = os.getenv("ARA_SETTINGS", DEFAULT_CONFIG)
logger.info(f"Using configuration file: {ARA_SETTINGS}")
ARA_SETTINGS = os.getenv("ARA_SETTINGS", DEFAULT_SETTINGS)
logger.info(f"Using settings file: {ARA_SETTINGS}")
# TODO: Split this out to a CLI command (django-admin command ?)
# 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 ?)
if not os.path.exists(DEFAULT_CONFIG) and "ARA_SETTINGS" not in os.environ:
CONFIG = dict(
if not os.path.exists(DEFAULT_SETTINGS) and "ARA_SETTINGS" not in os.environ:
SETTINGS = dict(
BASE_DIR=BASE_DIR,
ALLOWED_HOSTS=ALLOWED_HOSTS,
CORS_ORIGIN_WHITELIST=CORS_ORIGIN_WHITELIST,
@ -193,15 +194,15 @@ if not os.path.exists(DEFAULT_CONFIG) and "ARA_SETTINGS" not in os.environ:
LOG_LEVEL=LOG_LEVEL,
LOGGING=LOGGING,
)
with open(DEFAULT_CONFIG, "w+") as config_file:
with open(DEFAULT_SETTINGS, "w+") as settings_file:
comment = f"""
---
# This is a default configuration template generated by ARA.
# To use a configuration file such as this one, you need to export the
# ARA_SETTINGS configuration variable like so:
# $ export ARA_SETTINGS="{DEFAULT_CONFIG}"
# This is a default settings template generated by ARA.
# To use a settings file such as this one, you need to export the
# ARA_SETTINGS environment variable like so:
# $ export ARA_SETTINGS="{DEFAULT_SETTINGS}"
"""
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)
logger.info(f"Writing default settings to {DEFAULT_SETTINGS}")
settings_file.write(textwrap.dedent(comment))
yaml.dump({"default": SETTINGS}, settings_file, default_flow_style=False)