diff --git a/Dockerfile b/Dockerfile index 6f533468b..e39439f9a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,6 +26,7 @@ COPY ./passbook/ /app/passbook COPY ./manage.py /app/ COPY ./docker/uwsgi.ini /app/ COPY ./docker/bootstrap.sh /bootstrap.sh +COPY ./docker/wait_for_db.py /app/wait_for_db.py WORKDIR /app/ diff --git a/docker/bootstrap.sh b/docker/bootstrap.sh index 67cd1a5a8..d99696622 100755 --- a/docker/bootstrap.sh +++ b/docker/bootstrap.sh @@ -1,3 +1,3 @@ #!/bin/bash -ex -/app/manage.py bootstrap -$@ +/app/wait_for_db.py +"$@" diff --git a/docker/wait_for_db.py b/docker/wait_for_db.py new file mode 100755 index 000000000..6f7e99489 --- /dev/null +++ b/docker/wait_for_db.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +"""This file needs to be run from the root of the project to correctly +import passbook. This is done by the dockerfile.""" +from time import sleep +from psycopg2 import connect, OperationalError +from passbook.lib.config import CONFIG +from structlog import get_logger +from redis import Redis +from redis.exceptions import RedisError + +LOGGER = get_logger() + +while True: + try: + conn = connect( + dbname=CONFIG.y("postgresql.name"), + user=CONFIG.y("postgresql.user"), + password=CONFIG.y("postgresql.password"), + host=CONFIG.y("postgresql.host"), + ) + conn.cursor() + break + except OperationalError: + sleep(1) + LOGGER.warning("PostgreSQL Connection failed, retrying...") + +while True: + try: + redis = Redis( + host=CONFIG.y("redis.host"), + port=6379, + db=CONFIG.y("redis.message_queue_db"), + password=CONFIG.y("redis.password"), + ) + redis.ping() + break + except RedisError: + sleep(1) + LOGGER.warning("Redis Connection failed, retrying...") diff --git a/passbook/lib/default.yml b/passbook/lib/default.yml index df570ea9e..3d0138659 100644 --- a/passbook/lib/default.yml +++ b/passbook/lib/default.yml @@ -12,6 +12,7 @@ redis: message_queue_db: 1 debug: false +log_level: warning # Error reporting, sends stacktrace to sentry.beryju.org error_reporting: false diff --git a/passbook/lib/management/__init__.py b/passbook/lib/management/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/passbook/lib/management/commands/__init__.py b/passbook/lib/management/commands/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/passbook/lib/management/commands/bootstrap.py b/passbook/lib/management/commands/bootstrap.py deleted file mode 100644 index 2d7acbbdc..000000000 --- a/passbook/lib/management/commands/bootstrap.py +++ /dev/null @@ -1,50 +0,0 @@ -"""passbook management command to bootstrap""" -from sys import exit as _exit -from time import sleep - -from django.core.management.base import BaseCommand -from django.db import connection -from django.db.utils import OperationalError -from django_redis import get_redis_connection -from redis.exceptions import ConnectionError as RedisConnectionError -from structlog import get_logger - -LOGGER = get_logger() - - -class Command(BaseCommand): - """Bootstrap passbook, ensure Database and Cache are - reachable, and directories are writeable""" - - help = """Bootstrap passbook, ensure Database and Cache are - reachable, and directories are writeable""" - - def check_database(self) -> bool: - """Return true if database is reachable, false otherwise""" - try: - connection.cursor() - LOGGER.info("Database reachable") - return True - except OperationalError: - LOGGER.info("Database unreachable") - return False - - def check_cache(self) -> bool: - """Return true if cache is reachable, false otherwise""" - try: - con = get_redis_connection("default") - con.ping() - LOGGER.info("Cache reachable") - return True - except RedisConnectionError: - LOGGER.info("Cache unreachable") - return False - - def handle(self, *args, **options): - LOGGER.info("passbook bootstrapping...") - should_check = True - while should_check: - should_check = not (self.check_database() and self.check_cache()) - sleep(1) - LOGGER.info("Dependencies are up, exiting...") - _exit(0) diff --git a/passbook/root/settings.py b/passbook/root/settings.py index b889c9366..d1b218d1f 100644 --- a/passbook/root/settings.py +++ b/passbook/root/settings.py @@ -329,7 +329,8 @@ LOGGING = { }, "loggers": {}, } -LOG_LEVEL = "DEBUG" if DEBUG else "WARNING" +LOG_LEVEL = CONFIG.y("log_level").upper() + _LOGGING_HANDLER_MAP = { "": LOG_LEVEL, "passbook": LOG_LEVEL, @@ -355,6 +356,7 @@ TEST_OUTPUT_VERBOSE = 2 TEST_OUTPUT_FILE_NAME = "unittest.xml" if any("test" in arg for arg in sys.argv): + LOGGER.warning("Testing mode enabled, no logging from now on...") LOGGING = None TEST = True CELERY_TASK_ALWAYS_EAGER = True