From 9baf08d984cbf0f6e875173f095337df8d7c54a9 Mon Sep 17 00:00:00 2001 From: Douglas Paz Date: Tue, 10 Jul 2018 10:45:56 -0300 Subject: [PATCH 1/6] Add docker-compose default env var --- docker/docker-compose.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index f6232b613..e1537c35a 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -9,13 +9,13 @@ services: ports: - 80:80 environment: - - SECRET_KEY + - SECRET_KEY=SK - DEBUG - ALLOWED_HOSTS - DEFAULT_DATABASE - LANGUAGE_CODE - TIME_ZONE - - EMAIL_HOST + - EMAIL_HOST= - EMAIL_PORT - DEFAULT_FROM_EMAIL - SERVER_EMAIL @@ -26,8 +26,8 @@ services: - ADMINS - CSRF_COOKIE_DOMAIN - CSRF_COOKIE_SECURE - - BOTHUB_WEBAPP_BASE_URL - - BOTHUB_NLP_BASE_URL + - BOTHUB_WEBAPP_BASE_URL=http://localhost/ + - BOTHUB_NLP_BASE_URL=http://localhost:2657/ - WEBAPP_REPO=https://github.com/Ilhasoft/bothub-webapp - WEBAPP_BRANCH=master - - API_BASE_URL + - API_BASE_URL=/api From 27988a1eb3f81ac03b310cbac42308f5ff514e98 Mon Sep 17 00:00:00 2001 From: Douglas Paz Date: Tue, 10 Jul 2018 10:47:16 -0300 Subject: [PATCH 2/6] Add log in docker entrypoint --- docker/entrypoint.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 1e6ad430b..ac00886f6 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -6,7 +6,8 @@ cd $WORKDIR python manage.py migrate python manage.py collectstatic --noinput -gunicorn -e DJANGO_SETTINGS_MODULE=bothub.settings -b unix:/tmp/bothub.sock -D bothub.wsgi +gunicorn -e DJANGO_SETTINGS_MODULE=bothub.settings -b unix:/tmp/bothub.sock -D bothub.wsgi --log-file /var/log/gunicorn.log --log-level debug --capture-output mkdir -p /run/nginx/ -nginx -g "daemon off;" +nginx +tail -f /var/log/nginx/* /var/log/gunicorn.log From a95ac5a66eaaf3c56d98b2b3e5489cb072fe8cd5 Mon Sep 17 00:00:00 2001 From: Douglas Paz Date: Tue, 10 Jul 2018 11:02:08 -0300 Subject: [PATCH 3/6] Add iPython in dev dependencies --- Pipfile | 1 + Pipfile.lock | 129 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 123 insertions(+), 7 deletions(-) diff --git a/Pipfile b/Pipfile index 6198c19a8..3db4c7e09 100644 --- a/Pipfile +++ b/Pipfile @@ -17,6 +17,7 @@ whitenoise = "*" [dev-packages] "flake8" = "*" coverage = "*" +ipython = "*" [requires] python_version = "3.6" diff --git a/Pipfile.lock b/Pipfile.lock index e1b997c3e..c0aa1b212 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "bd16c4f09fafe3908320fc1b3b5e2c91a695bc63563a759b98c24c49045a0904" + "sha256": "fec7ef2907473f50ce1c0949af954a10731e17ca699cb3c9da3c2977f1a1149c" }, "pipfile-spec": 6, "requires": { @@ -63,11 +63,11 @@ }, "django-cors-headers": { "hashes": [ - "sha256:0e9532628b3aa8806442d4d0b15e56112e6cfbef3735e13401935c98b842a2b4", - "sha256:c7ec4816ec49416517b84f317499d1519db62125471922ab78d670474ed9b987" + "sha256:1460b67c51706307e3f66515e86b10181badd66448a2fc44278f47bf402c6fd3", + "sha256:3c618a964e3034ae4dad8bbb902e625daa6c894444ddeaf7b24125299add54f0" ], "index": "pypi", - "version": "==2.2.0" + "version": "==2.3.0" }, "django-filter": { "hashes": [ @@ -120,10 +120,10 @@ }, "pytz": { "hashes": [ - "sha256:65ae0c8101309c45772196b21b74c46b2e5d11b6275c45d251b150d5da334555", - "sha256:c06425302f2cf668f1bba7a0a03f3c1d34d4ebeef2c72003da308b3947c7f749" + "sha256:a061aa0a9e06881eb8b3b2b43f05b9439d6583c206d0a6c340ff72a7b6669053", + "sha256:ffb9ef1de172603304d9d2819af6f5ece76f2e85ec10692a524dd876e72bf277" ], - "version": "==2018.4" + "version": "==2018.5" }, "requests": { "hashes": [ @@ -158,6 +158,21 @@ } }, "develop": { + "appnope": { + "hashes": [ + "sha256:5b26757dc6f79a3b7dc9fab95359328d5747fcb2409d331ea66d0272b90ab2a0", + "sha256:8b995ffe925347a2138d7ac0fe77155e4311a0ea6d6da4f5128fe4b3cbe5ed71" + ], + "markers": "sys_platform == 'darwin'", + "version": "==0.1.0" + }, + "backcall": { + "hashes": [ + "sha256:38ecd85be2c1e78f77fd91700c76e14667dc21e2713b63876c0eb901196e01e4", + "sha256:bbbf4b1e5cd2bdb08f915895b51081c041bac22394fdfcfdfbe9f14b77c08bf2" + ], + "version": "==0.1.0" + }, "coverage": { "hashes": [ "sha256:03481e81d558d30d230bc12999e3edffe392d244349a90f4ef9b88425fac74ba", @@ -200,6 +215,13 @@ "index": "pypi", "version": "==4.5.1" }, + "decorator": { + "hashes": [ + "sha256:2c51dff8ef3c447388fe5e4453d24a2bf128d3a4c32af3fabef1f01c6851ab82", + "sha256:c39efa13fbdeb4506c476c9b3babf6a718da943dab7811c206005a4a956c080c" + ], + "version": "==4.3.0" + }, "flake8": { "hashes": [ "sha256:7253265f7abd8b313e3892944044a365e3f4ac3fcdcfb4298f55ee9ddf188ba0", @@ -208,6 +230,28 @@ "index": "pypi", "version": "==3.5.0" }, + "ipython": { + "hashes": [ + "sha256:a0c96853549b246991046f32d19db7140f5b1a644cc31f0dc1edc86713b7676f", + "sha256:eca537aa61592aca2fef4adea12af8e42f5c335004dfa80c78caf80e8b525e5c" + ], + "index": "pypi", + "version": "==6.4.0" + }, + "ipython-genutils": { + "hashes": [ + "sha256:72dd37233799e619666c9f639a9da83c34013a73e8bbc79a7a6348d93c61fab8", + "sha256:eb2e116e75ecef9d4d228fdc66af54269afa26ab4463042e33785b887c628ba8" + ], + "version": "==0.2.0" + }, + "jedi": { + "hashes": [ + "sha256:b409ed0f6913a701ed474a614a3bb46e6953639033e31f769ca7581da5bd1ec1", + "sha256:c254b135fb39ad76e78d4d8f92765ebc9bf92cbc76f49e97ade1d5f5121e1f6f" + ], + "version": "==0.12.1" + }, "mccabe": { "hashes": [ "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42", @@ -215,6 +259,43 @@ ], "version": "==0.6.1" }, + "parso": { + "hashes": [ + "sha256:35704a43a3c113cce4de228ddb39aab374b8004f4f2407d070b6a2ca784ce8a2", + "sha256:895c63e93b94ac1e1690f5fdd40b65f07c8171e3e53cbd7793b5b96c0e0a7f24" + ], + "version": "==0.3.1" + }, + "pexpect": { + "hashes": [ + "sha256:2a8e88259839571d1251d278476f3eec5db26deb73a70be5ed5dc5435e418aba", + "sha256:3fbd41d4caf27fa4a377bfd16fef87271099463e6fa73e92a52f92dfee5d425b" + ], + "markers": "sys_platform != 'win32'", + "version": "==4.6.0" + }, + "pickleshare": { + "hashes": [ + "sha256:84a9257227dfdd6fe1b4be1319096c20eb85ff1e82c7932f36efccfe1b09737b", + "sha256:c9a2541f25aeabc070f12f452e1f2a8eae2abd51e1cd19e8430402bdf4c1d8b5" + ], + "version": "==0.7.4" + }, + "prompt-toolkit": { + "hashes": [ + "sha256:1df952620eccb399c53ebb359cc7d9a8d3a9538cb34c5a1344bdbeb29fbcc381", + "sha256:3f473ae040ddaa52b52f97f6b4a493cfa9f5920c255a12dc56a7d34397a398a4", + "sha256:858588f1983ca497f1cf4ffde01d978a3ea02b01c8a26a8bbc5cd2e66d816917" + ], + "version": "==1.0.15" + }, + "ptyprocess": { + "hashes": [ + "sha256:923f299cc5ad920c68f2bc0bc98b75b9f838b93b599941a6b63ddbc2476394c0", + "sha256:d7cc528d76e76342423ca640335bd3633420dc1366f258cb31d05e865ef5ca1f" + ], + "version": "==0.6.0" + }, "pycodestyle": { "hashes": [ "sha256:682256a5b318149ca0d2a9185d365d8864a768a28db66a84a2ea946bcc426766", @@ -228,6 +309,40 @@ "sha256:8d616a382f243dbf19b54743f280b80198be0bca3a5396f1d2e1fca6223e8805" ], "version": "==1.6.0" + }, + "pygments": { + "hashes": [ + "sha256:78f3f434bcc5d6ee09020f92ba487f95ba50f1e3ef83ae96b9d5ffa1bab25c5d", + "sha256:dbae1046def0efb574852fab9e90209b23f556367b5a320c0bcb871c77c3e8cc" + ], + "version": "==2.2.0" + }, + "simplegeneric": { + "hashes": [ + "sha256:dc972e06094b9af5b855b3df4a646395e43d1c9d0d39ed345b7393560d0b9173" + ], + "version": "==0.8.1" + }, + "six": { + "hashes": [ + "sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9", + "sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb" + ], + "version": "==1.11.0" + }, + "traitlets": { + "hashes": [ + "sha256:9c4bd2d267b7153df9152698efb1050a5d84982d3384a37b2c1f7723ba3e7835", + "sha256:c6cb5e6f57c5a9bdaa40fa71ce7b4af30298fbab9ece9815b5d995ab6217c7d9" + ], + "version": "==4.3.2" + }, + "wcwidth": { + "hashes": [ + "sha256:3df37372226d6e63e1b1e1eda15c594bca98a22d33a23832a90998faa96bc65e", + "sha256:f4ebe71925af7b40a864553f761ed559b43544f8f71746c2d756c7fe788ade7c" + ], + "version": "==0.1.7" } } } From 64efcf4f926a2de77fe15986e1a72385fc5b6df4 Mon Sep 17 00:00:00 2001 From: Douglas Paz Date: Tue, 10 Jul 2018 11:55:39 -0300 Subject: [PATCH 4/6] Add bothub.health in LOGGING setting --- bothub/settings.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/bothub/settings.py b/bothub/settings.py index e7c9472de..ce2dc108a 100644 --- a/bothub/settings.py +++ b/bothub/settings.py @@ -2,6 +2,7 @@ import dj_database_url from decouple import config +from django.utils.log import DEFAULT_LOGGING # Build paths inside the project like this: os.path.join(BASE_DIR, ...) @@ -204,3 +205,21 @@ 'CSRF_COOKIE_SECURE', default=False, cast=bool) + + +# Logging + +LOGGING = DEFAULT_LOGGING +LOGGING['formatters']['bothub.health'] = { + 'format': '[bothub.health] {message}', + 'style': '{', +} +LOGGING['handlers']['bothub.health'] = { + 'level': 'DEBUG', + 'class': 'logging.StreamHandler', + 'formatter': 'bothub.health', +} +LOGGING['loggers']['bothub.health.checks'] = { + 'handlers': ['bothub.health'], + 'level': 'DEBUG', +} From eb555e37f0327c938bc517598e30b073332f40b2 Mon Sep 17 00:00:00 2001 From: Douglas Paz Date: Tue, 10 Jul 2018 11:55:58 -0300 Subject: [PATCH 5/6] Fix #165 --- bothub/health/checks.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/bothub/health/checks.py b/bothub/health/checks.py index e3ba2c595..5e2a47460 100644 --- a/bothub/health/checks.py +++ b/bothub/health/checks.py @@ -1,3 +1,12 @@ +import logging + +from rest_framework import status + + +logger = logging.getLogger('bothub.health.checks') + +CHECK_ACCESSIBLE_API_URL = '/api/repositories/' + def check_database_connection(**kwargs): from django.db import connections from django.db.utils import OperationalError @@ -12,12 +21,13 @@ def check_database_connection(**kwargs): def check_accessible_api(request, **kwargs): - import requests - HTTP_HOST = request.META.get('HTTP_HOST') - repositories_url = 'http://{}/api/repositories/'.format(HTTP_HOST) - request = requests.get(repositories_url) - try: - request.raise_for_status() + from django.test import Client + logger.info('making request to {}'.format(CHECK_ACCESSIBLE_API_URL)) + client = Client() + response = client.get(CHECK_ACCESSIBLE_API_URL) + logger.info('{} status code: {}'.format( + CHECK_ACCESSIBLE_API_URL, + response.status_code)) + if response.status_code is status.HTTP_200_OK: return True - except requests.HTTPError as e: - return False + return False From 423f4faa719c1c0f4d89135552169aac586e8fd3 Mon Sep 17 00:00:00 2001 From: Douglas Paz Date: Tue, 10 Jul 2018 12:11:21 -0300 Subject: [PATCH 6/6] Closes #161 --- bothub/health/checks.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/bothub/health/checks.py b/bothub/health/checks.py index 5e2a47460..c50725741 100644 --- a/bothub/health/checks.py +++ b/bothub/health/checks.py @@ -7,17 +7,22 @@ CHECK_ACCESSIBLE_API_URL = '/api/repositories/' + def check_database_connection(**kwargs): from django.db import connections from django.db.utils import OperationalError - db_conn = connections['default'] - if not db_conn: - return False - try: - db_conn.cursor() - return True - except OperationalError as e: + if len(connections.all()) is 0: return False + logger.info('found {} database connection'.format(len(connections.all()))) + for i, conn in enumerate(connections.all(), 1): + db_conn = connections['default'] + try: + db_conn.cursor() + logger.info('#{} db connection OKAY'.format(i)) + except OperationalError as e: + logger.warning('#{} db connection ERROR'.format(i)) + return False + return True def check_accessible_api(request, **kwargs):