root: rewrite bootstrap in python, remove management command

This commit is contained in:
Jens Langhammer 2020-05-16 22:14:26 +02:00
parent a6301055f0
commit 10d3f9ac2f
8 changed files with 46 additions and 53 deletions

View File

@ -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/

View File

@ -1,3 +1,3 @@
#!/bin/bash -ex
/app/manage.py bootstrap
$@
/app/wait_for_db.py
"$@"

39
docker/wait_for_db.py Executable file
View File

@ -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...")

View File

@ -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

View File

@ -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)

View File

@ -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