From b78589d20de4abf97067c3b1c1cbfe72b498f616 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Sun, 25 Mar 2018 04:25:33 -0400 Subject: [PATCH] Update docker files (#598) * Add redis to docker-compose, bump worker count to 4 * Add workers & missing SECRET_KEY error * Remove uwsgi specific files * Parse database host in `docker-entrypoint.sh`. Closes #587 --- ctfd.ini | 46 -------------------------------------------- docker-compose.yml | 12 +++++++++++- docker-entrypoint.sh | 22 ++++++++++++++++----- wsgi.py | 3 --- 4 files changed, 28 insertions(+), 55 deletions(-) delete mode 100644 ctfd.ini delete mode 100644 wsgi.py diff --git a/ctfd.ini b/ctfd.ini deleted file mode 100644 index 951cae8..0000000 --- a/ctfd.ini +++ /dev/null @@ -1,46 +0,0 @@ -# UWSGI Configuration File -# Install uwsgi (sudo apt-get install uwsgi), copy this file to -# /etc/uwsgi/apps-available and then link it in /etc/uwsgi/apps-enabled -# Only two lines below (commented) need to be changed for your config. -# Then, you can use something like the following in your nginx config: -# -# # SERVER_ROOT is not / (e.g. /ctf) -# location = /ctf { rewrite ^ /ctf/; } -# location /ctf { -# include uwsgi_params; -# uwsgi_pass unix:/run/uwsgi/app/ctfd/socket; -# } -# -# # SERVER_ROOT is / -# location / { -# include uwsgi_params; -# wsgi_pass unix:/run/uwsgi/app/ctfd/socket; -# } -[uwsgi] -# Where you've put CTFD -chdir = /var/www/ctfd/ -# If SCRIPT_ROOT is not / -#mount = /ctf=wsgi.py -# SCRIPT_ROOT is / -mount = /=wsgi.py - -# You shouldn't need to change anything past here -plugin = python -module = wsgi - -master = true -processes = 1 -threads = 1 - -vacuum = true - -manage-script-name = true -wsgi-file = wsgi.py -callable = app - -die-on-term = true - -# If you're not on debian/ubuntu, replace with uid/gid of web user -uid = www-data -gid = www-data - diff --git a/docker-compose.yml b/docker-compose.yml index 4fffb76..9f36b6d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,8 @@ services: - UPLOAD_FOLDER=/var/uploads - LOG_FOLDER=/var/log/CTFd - DATABASE_URL=mysql+pymysql://root:ctfd@db/ctfd + - REDIS_URL=redis://cache:6379 + - WORKERS=4 volumes: - .data/CTFd/logs:/var/log/CTFd - .data/CTFd/uploads:/var/uploads @@ -32,7 +34,15 @@ services: networks: internal: # This command is required to set important mariadb defaults - command: [mysqld, --character-set-server=utf8mb4, --collation-server=utf8mb4_unicode_ci, --wait_timeout=28800] + command: [mysqld, --character-set-server=utf8mb4, --collation-server=utf8mb4_unicode_ci, --wait_timeout=28800, --log-warnings=0] + + cache: + image: redis:4 + restart: always + volumes: + - .data/redis:/data + networks: + internal: networks: default: diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 4fb8bd9..8cc99f8 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -1,23 +1,35 @@ #!/bin/sh +# Check that a .ctfd_secret_key file or SECRET_KEY envvar is set +if [ ! -f .ctfd_secret_key ] && [ -z "$SECRET_KEY" ]; then + if [ $WORKERS -gt 1 ]; then + echo "[ ERROR ] You are configured to use more than 1 worker." + echo "[ ERROR ] To do this, you must define the SECRET_KEY environment variable or create a .ctfd_secret_key file." + echo "[ ERROR ] Exiting..." + exit 1 + fi +fi + +# Check that the database is available if [ -n "$DATABASE_URL" ] then - # https://stackoverflow.com/a/29793382 - echo "Waiting on MySQL" - while ! mysqladmin ping -h db --silent; do + database=`echo $DATABASE_URL | awk -F[@//] '{print $4}'` + echo "Waiting for $database to be ready" + while ! mysqladmin ping -h $database --silent; do # Show some progress echo -n '.'; sleep 1; done - echo "Ready" + echo "$database is ready" # Give it another second. sleep 1; fi +# Start CTFd echo "Starting CTFd" gunicorn 'CTFd:create_app()' \ --bind '0.0.0.0:8000' \ - --workers 1 \ + --workers $WORKERS \ --worker-class 'gevent' \ --access-logfile "${LOG_FOLDER:-/opt/CTFd/CTFd/logs}/access.log" \ --error-logfile "${LOG_FOLDER:-/opt/CTFd/CTFd/logs}/error.log" diff --git a/wsgi.py b/wsgi.py deleted file mode 100644 index 67b0172..0000000 --- a/wsgi.py +++ /dev/null @@ -1,3 +0,0 @@ -from CTFd import create_app - -app = create_app()