From 0789ef9206d33a334d24f5407ef862e3c3b1a376 Mon Sep 17 00:00:00 2001 From: Samuel Paccoud - FUN MOOC Date: Thu, 13 Sep 2018 16:17:44 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7(databases)=20improve=20flexibility?= =?UTF-8?q?=20of=20mysql=20and=20mogodb=20settings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We want to be able to configure separately each part of the database settings (e.g. db name versus password) so that they can come from different sources and be injected differently. For example the password comes from a vault and is injected via environment variables versus the database name comes from a yaml file and is injected by mounting the file in the container. --- config/cms/docker_run_production.py | 40 ++++++++++++++++++++------ config/lms/docker_run_production.py | 44 +++++++++++++++++++++++------ env.d/development | 2 +- 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/config/cms/docker_run_production.py b/config/cms/docker_run_production.py index e50655c8..28887807 100644 --- a/config/cms/docker_run_production.py +++ b/config/cms/docker_run_production.py @@ -397,16 +397,25 @@ USER_TASKS_ARTIFACT_STORAGE = COURSE_IMPORT_EXPORT_STORAGE +# Databases + +DATABASE_ENGINE = config("DATABASE_ENGINE", default="django.db.backends.mysql") +DATABASE_HOST = config("DATABASE_HOST", default="mysql") +DATABASE_PORT = config("DATABASE_PORT", default=3306, formatter=int) +DATABASE_NAME = config("DATABASE_NAME", default="edxapp") +DATABASE_USER = config("DATABASE_USER", default="edxapp_user") +DATABASE_PASSWORD = config("DATABASE_PASSWORD", default="password") + DATABASES = config( "DATABASES", default={ "default": { - "ENGINE": "django.db.backends.mysql", - "HOST": "mysql", - "PORT": "3306", - "NAME": "edxapp", - "USER": "fun", - "PASSWORD": "password", + "ENGINE": DATABASE_ENGINE, + "HOST": DATABASE_HOST, + "PORT": DATABASE_PORT, + "NAME": DATABASE_NAME, + "USER": DATABASE_USER, + "PASSWORD": DATABASE_PASSWORD, } }, formatter=json.loads, @@ -417,9 +426,24 @@ config("MODULESTORE", default=MODULESTORE, formatter=json.loads) ) +MONGODB_PASSWORD = config("MONGODB_PASSWORD", default="") +MONGODB_HOST = config("MONGODB_HOST", default="mongodb") +MONGODB_PORT = config("MONGODB_PORT", default=27017, formatter=int) +MONGODB_NAME = config("MONGODB_NAME", default="edxapp") +MONGODB_USER = config("MONGODB_USER", default=None) +MONGODB_SSL = config("MONGODB_SSL", default=False, formatter=bool) + DOC_STORE_CONFIG = config( "DOC_STORE_CONFIG", - default={"db": "edxapp", "host": "mongodb"}, + default={ + "collection": "modulestore", + "host": MONGODB_HOST, + "port": MONGODB_PORT, + "db": MONGODB_NAME, + "user": MONGODB_USER, + "password": MONGODB_PASSWORD, + "ssl": MONGODB_SSL, + }, formatter=json.loads, ) @@ -438,7 +462,7 @@ CONTENTSTORE = config( "CONTENTSTORE", default={ - "DOC_STORE_CONFIG": {"host": ["mongodb"], "db": "edxapp", "port": 27017}, + "DOC_STORE_CONFIG": DOC_STORE_CONFIG, "ENGINE": "xmodule.contentstore.mongo.MongoContentStore", }, formatter=json.loads, diff --git a/config/lms/docker_run_production.py b/config/lms/docker_run_production.py index 364b61eb..ebdbca86 100644 --- a/config/lms/docker_run_production.py +++ b/config/lms/docker_run_production.py @@ -631,18 +631,28 @@ "FILE_UPLOAD_STORAGE_PREFIX", default=FILE_UPLOAD_STORAGE_PREFIX ) +# Databases + # If there is a database called 'read_replica', you can use the use_read_replica_if_available # function in util/query.py, which is useful for very large database reads + +DATABASE_ENGINE = config("DATABASE_ENGINE", default="django.db.backends.mysql") +DATABASE_HOST = config("DATABASE_HOST", default="mysql") +DATABASE_PORT = config("DATABASE_PORT", default=3306, formatter=int) +DATABASE_NAME = config("DATABASE_NAME", default="edxapp") +DATABASE_USER = config("DATABASE_USER", default="edxapp_user") +DATABASE_PASSWORD = config("DATABASE_PASSWORD", default="password") + DATABASES = config( "DATABASES", default={ "default": { - "ENGINE": "django.db.backends.mysql", - "HOST": "mysql", - "PORT": "3306", - "NAME": "edxapp", - "USER": "fun", - "PASSWORD": "password", + "ENGINE": DATABASE_ENGINE, + "HOST": DATABASE_HOST, + "PORT": DATABASE_PORT, + "NAME": DATABASE_NAME, + "USER": DATABASE_USER, + "PASSWORD": DATABASE_PASSWORD, } }, formatter=json.loads, @@ -658,16 +668,34 @@ MODULESTORE = convert_module_store_setting_if_needed( config("MODULESTORE", default=MODULESTORE, formatter=json.loads) ) + +MONGODB_PASSWORD = config("MONGODB_PASSWORD", default="") +MONGODB_HOST = config("MONGODB_HOST", default="mongodb") +MONGODB_PORT = config("MONGODB_PORT", default=27017, formatter=int) +MONGODB_NAME = config("MONGODB_NAME", default="edxapp") +MONGODB_USER = config("MONGODB_USER", default=None) +MONGODB_SSL = config("MONGODB_SSL", default=False, formatter=bool) + DOC_STORE_CONFIG = config( "DOC_STORE_CONFIG", - default={"host": "mongodb", "db": "edxapp"}, + default={ + "collection": "modulestore", + "host": MONGODB_HOST, + "port": MONGODB_PORT, + "db": MONGODB_NAME, + "user": MONGODB_USER, + "password": MONGODB_PASSWORD, + "ssl": MONGODB_SSL, + }, formatter=json.loads, ) + MONGODB_LOG = config("MONGODB_LOG", default={}, formatter=json.loads) + CONTENTSTORE = config( "CONTENTSTORE", default={ - "DOC_STORE_CONFIG": {"host": ["mongodb"], "db": "edxapp", "port": 27017}, + "DOC_STORE_CONFIG": DOC_STORE_CONFIG, "ENGINE": "xmodule.contentstore.mongo.MongoContentStore", }, formatter=json.loads, diff --git a/env.d/development b/env.d/development index a67e4856..d91203f9 100644 --- a/env.d/development +++ b/env.d/development @@ -6,7 +6,7 @@ DJANGO_SETTINGS_MODULE: lms.envs.fun.docker_run MYSQL_ROOT_PASSWORD= MYSQL_ALLOW_EMPTY_PASSWORD=yes MYSQL_DATABASE=edxapp -MYSQL_USER=fun +MYSQL_USER=edxapp_user MYSQL_PASSWORD=password # Email