From cb06ff05e830eb344e2d9a3288d813d38fe00ea0 Mon Sep 17 00:00:00 2001 From: deploy-soon <44163510+deploy-soon@users.noreply.github.com> Date: Sun, 24 Nov 2019 15:45:55 +0900 Subject: [PATCH] [Setting] Database supports MySQL (#57) * separate settings files into debug and deploy * django settings sonar exclustions * mysql setting --- backend/.gitignore | 1 + backend/triplannet/.config/settings_base.json | 5 ++++ .../triplannet/.config/settings_debug.json | 8 ++++++ .../triplannet/.config/settings_deploy.json | 16 ++++++++++++ backend/triplannet/manage.py | 2 +- backend/triplannet/sonar-project.properties | 2 +- .../triplannet/settings/__init__.py | 0 .../{settings.py => settings/base.py} | 19 ++++++++++---- .../triplannet/triplannet/settings/debug.py | 17 ++++++++++++ .../triplannet/triplannet/settings/deploy.py | 26 +++++++++++++++++++ .../triplannet/triplannet/wsgi/__init__.py | 0 .../triplannet/{wsgi.py => wsgi/debug.py} | 2 +- backend/triplannet/triplannet/wsgi/deploy.py | 16 ++++++++++++ 13 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 backend/triplannet/.config/settings_base.json create mode 100644 backend/triplannet/.config/settings_debug.json create mode 100644 backend/triplannet/.config/settings_deploy.json create mode 100644 backend/triplannet/triplannet/settings/__init__.py rename backend/triplannet/triplannet/{settings.py => settings/base.py} (88%) create mode 100644 backend/triplannet/triplannet/settings/debug.py create mode 100644 backend/triplannet/triplannet/settings/deploy.py create mode 100644 backend/triplannet/triplannet/wsgi/__init__.py rename backend/triplannet/triplannet/{wsgi.py => wsgi/debug.py} (96%) create mode 100644 backend/triplannet/triplannet/wsgi/deploy.py diff --git a/backend/.gitignore b/backend/.gitignore index 853f25a..2e188b9 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -86,4 +86,5 @@ __pycache__/ local_settings.py .env +.config/ db.sqlite3 diff --git a/backend/triplannet/.config/settings_base.json b/backend/triplannet/.config/settings_base.json new file mode 100644 index 0000000..14ebc7e --- /dev/null +++ b/backend/triplannet/.config/settings_base.json @@ -0,0 +1,5 @@ +{ + "django": { + "secret_key": "swpp2019-team6" + } +} diff --git a/backend/triplannet/.config/settings_debug.json b/backend/triplannet/.config/settings_debug.json new file mode 100644 index 0000000..71db950 --- /dev/null +++ b/backend/triplannet/.config/settings_debug.json @@ -0,0 +1,8 @@ +{ + "django": { + "allowed_hosts": [ + "localhost", + "127.0.0.1" + ] + } +} diff --git a/backend/triplannet/.config/settings_deploy.json b/backend/triplannet/.config/settings_deploy.json new file mode 100644 index 0000000..350ce7d --- /dev/null +++ b/backend/triplannet/.config/settings_deploy.json @@ -0,0 +1,16 @@ +{ + "django": { + "allowed_hosts": [ + "localhost", + "127.0.0.1" + ], + "secret_key": "SECRET_KEY", + "database": { + "NAME": "SCHEMA", + "USER": "USER", + "PASSWORD": "PASSWORD", + "HOST": "HOST", + "PORT": 3306 + } + } +} diff --git a/backend/triplannet/manage.py b/backend/triplannet/manage.py index 80299c5..17d8806 100755 --- a/backend/triplannet/manage.py +++ b/backend/triplannet/manage.py @@ -5,7 +5,7 @@ def main(): - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'triplannet.settings') + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'triplannet.settings.debug') try: from django.core.management import execute_from_command_line except ImportError as exc: diff --git a/backend/triplannet/sonar-project.properties b/backend/triplannet/sonar-project.properties index 619e648..8f96c43 100644 --- a/backend/triplannet/sonar-project.properties +++ b/backend/triplannet/sonar-project.properties @@ -9,6 +9,6 @@ sonar.sources=. #sonar.python.pylint.reportPath=pylint-report.txt sonar.test=./triplannet -sonar.exclusions=**/migrations/* +sonar.exclusions=**/migrations/*,./.config/*,./triplannet/settings/*,./triplannet/wsgi/* sonar.test.inclusions=**/tests.py sonar.python.coverage.reportPath=./coverage.xml diff --git a/backend/triplannet/triplannet/settings/__init__.py b/backend/triplannet/triplannet/settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/triplannet/triplannet/settings.py b/backend/triplannet/triplannet/settings/base.py similarity index 88% rename from backend/triplannet/triplannet/settings.py rename to backend/triplannet/triplannet/settings/base.py index f21635b..c73f5dc 100644 --- a/backend/triplannet/triplannet/settings.py +++ b/backend/triplannet/triplannet/settings/base.py @@ -9,18 +9,28 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.2/ref/settings/ """ - import os +import json +from os.path import join as pjoin +from os.path import dirname as pdir from datetime import timedelta # Build paths inside the project like this: os.path.join(BASE_DIR, ...) -BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +BASE_DIR = pdir(pdir(pdir(os.path.abspath(__file__)))) + +# load config files +CONFIG_DIR = pjoin(BASE_DIR, '.config') +CONFIG_BASE_FILE = pjoin(CONFIG_DIR, 'settings_base.json') +CONFIG_DEBUG_FILE = pjoin(CONFIG_DIR, 'settings_debug.json') +CONFIG_DEPLOY_FILE = pjoin(CONFIG_DIR, 'settings_deploy.json') + +config_base = json.loads(open(CONFIG_BASE_FILE).read()) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'q^mnq)5qnon98ghvps#dbuj!-bvgip(7@9y=%d#-p%^3!z4rbs' +SECRET_KEY = config_base['django']['secret_key'] # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -118,7 +128,6 @@ }, ] -WSGI_APPLICATION = 'triplannet.wsgi.application' # Database @@ -127,7 +136,7 @@ DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + 'NAME': pjoin(BASE_DIR, 'db.sqlite3'), } } diff --git a/backend/triplannet/triplannet/settings/debug.py b/backend/triplannet/triplannet/settings/debug.py new file mode 100644 index 0000000..071b73e --- /dev/null +++ b/backend/triplannet/triplannet/settings/debug.py @@ -0,0 +1,17 @@ +from .base import * + +config_debug = json.loads(open(CONFIG_DEBUG_FILE).read()) + +DEBUG = True + +ALLOWED_HOSTS = config_debug['django']['allowed_hosts'] + +WSGI_APPLICATION = 'triplannet.wsgi.debug.application' + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': pjoin(BASE_DIR, 'db.sqlite3'), + } +} + diff --git a/backend/triplannet/triplannet/settings/deploy.py b/backend/triplannet/triplannet/settings/deploy.py new file mode 100644 index 0000000..ff231aa --- /dev/null +++ b/backend/triplannet/triplannet/settings/deploy.py @@ -0,0 +1,26 @@ +from .base import * + +config_deploy = json.loads(open(CONFIG_DEPLOY_FILE).read()) + +DEBUG = False + +ALLOWED_HOSTS = config_deploy['django']['allowed_hosts'] + +WSGI_APPLICATION = 'triplannet.wsgi.deploy.application' + +SECRET_KEY = config_deploy['django']['secret_key'] + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': config_deploy['django']['database']['NAME'], + 'USER': config_deploy['django']['database']['USER'], + 'PASSWORD': config_deploy['django']['database']['PASSWORD'], + 'HOST': config_deploy['django']['database']['HOST'], + 'PORT': config_deploy['django']['database']['PORT'], + 'OPTIONS': { + 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'" + } + } +} + diff --git a/backend/triplannet/triplannet/wsgi/__init__.py b/backend/triplannet/triplannet/wsgi/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/triplannet/triplannet/wsgi.py b/backend/triplannet/triplannet/wsgi/debug.py similarity index 96% rename from backend/triplannet/triplannet/wsgi.py rename to backend/triplannet/triplannet/wsgi/debug.py index 77214c4..51f23e0 100644 --- a/backend/triplannet/triplannet/wsgi.py +++ b/backend/triplannet/triplannet/wsgi/debug.py @@ -11,6 +11,6 @@ from django.core.wsgi import get_wsgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'triplannet.settings') +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'triplannet.settings.debug') application = get_wsgi_application() diff --git a/backend/triplannet/triplannet/wsgi/deploy.py b/backend/triplannet/triplannet/wsgi/deploy.py new file mode 100644 index 0000000..89b9f39 --- /dev/null +++ b/backend/triplannet/triplannet/wsgi/deploy.py @@ -0,0 +1,16 @@ +""" +WSGI config for triplannet project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'triplannet.settings.deploy') + +application = get_wsgi_application()