From 2cffca4023bf15d84165f9e4722c4d596d60cf6c Mon Sep 17 00:00:00 2001 From: Maxime Bloch Date: Thu, 23 Jul 2020 14:07:31 +0200 Subject: [PATCH] Add crude basis for docker --- Dockerfile | 12 ++++++++++ KeRS/celery.py | 5 +++-- KeRS/settings.py | 37 +++++++++++++++++++++--------- docker-compose.yml | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 12 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c9f3db7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.8-alpine + +RUN mkdir code +WORKDIR code + +RUN apk add --no-cache gcc musl-dev linux-headers mariadb-dev + + +COPY requirements.txt requirements.txt +RUN pip install -r requirements.txt + +COPY . . diff --git a/KeRS/celery.py b/KeRS/celery.py index 87043b6..95627c1 100644 --- a/KeRS/celery.py +++ b/KeRS/celery.py @@ -1,4 +1,5 @@ from __future__ import absolute_import, unicode_literals +from django.conf import settings import os @@ -8,8 +9,8 @@ from celery import Celery os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'KeRS.settings') app = Celery('KeRS', - broker='redis://localhost:6379/0', - backend='redis://localhost:6379/1' + broker=settings.CELERY_BROKER, + backend=settings.CELERY_BACKEND ) # Using a string here means the worker doesn't have to serialize diff --git a/KeRS/settings.py b/KeRS/settings.py index 28e4841..0a854d7 100644 --- a/KeRS/settings.py +++ b/KeRS/settings.py @@ -12,6 +12,18 @@ https://docs.djangoproject.com/en/3.0/ref/settings/ import os +from django.core.exceptions import ImproperlyConfigured + + +def get_env_value(env_variable, default): + try: + return os.environ[env_variable] + except KeyError: + return default + # error_msg = 'Set the {} environment variable'.format(env_variable) + # raise ImproperlyConfigured(error_msg) + + # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -32,6 +44,7 @@ OWN_APPS = [ 'events', 'users', 'oauth', + 'core' ] INSTALLED_APPS = [ @@ -81,11 +94,11 @@ WSGI_APPLICATION = 'KeRS.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', - 'NAME': 'kers', - 'USER': 'kers', - 'PASSWORD': 'kers', - 'HOST': '127.0.0.1', - 'PORT': '3306', + 'NAME': get_env_value('DATABASE_NAME', 'kers'), + 'USER': get_env_value('DATABASE_USER', 'kers'), + 'PASSWORD': get_env_value('DATABASE_PASSWORD', 'kers'), + 'HOST': get_env_value('DATABASE_HOST', '127.0.0.1'), + 'PORT': get_env_value('DATABASE_PORT', '3306'), } } @@ -128,6 +141,10 @@ STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"), ] +# Celery + +CELERY_BROKER = get_env_value('CELERY_BROKER', 'redis://localhost:6379/0') +CELERY_BACKEND = get_env_value('CELERY_BACKEND', 'redis://localhost:6379/1') # Custom stuff SERVER_URL = 'http://localhost:8000' @@ -137,10 +154,10 @@ AUTH_USER_MODEL = 'users.CustomUser' _BASE_OAUTH_URL = 'https://adams.ugent.be/oauth' OAUTH = { - 'USER_API_URI': f'{_BASE_OAUTH_URL}/api/current_user/', + 'USER_API_URI': f'{_BASE_OAUTH_URL}/api/current_user/', 'ACCESS_TOKEN_URI': f'{_BASE_OAUTH_URL}/oauth2/token/', - 'AUTHORIZE_URI': f'{_BASE_OAUTH_URL}/oauth2/authorize/', - 'REDIRECT_URI': f'{SERVER_URL}/login/zeus/authorized', - 'CLIENT_ID': 'tomtest', - 'CLIENT_SECRET': 'blargh', + 'AUTHORIZE_URI': f'{_BASE_OAUTH_URL}/oauth2/authorize/', + 'REDIRECT_URI': f'{SERVER_URL}/login/zeus/authorized', + 'CLIENT_ID': 'tomtest', + 'CLIENT_SECRET': 'blargh', } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..cf70fbc --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,56 @@ +version: '3' +services: + web: + build: . + ports: + - "8000:8000" + environment: + DATABASE_HOST: 'db' + CELERY_BROKER: 'redis://redis:6379/0' + CELERY_BACKEND: 'redis://redis:6379/1' + depends_on: + - db + command: > + sh -c "sleep 5 && + python manage.py migrate && + python manage.py runserver 0.0.0.0:8000" + celery-worker: + build: . + environment: + DATABASE_HOST: 'db' + CELERY_BROKER: 'redis://redis:6379/0' + CELERY_BACKEND: 'redis://redis:6379/1' + depends_on: + - web + - redis + command: > + sh -c "celery -A KeRS worker -l info" + celery-beat: + build: . + environment: + DATABASE_HOST: 'db' + CELERY_BROKER: 'redis://redis:6379/0' + CELERY_BACKEND: 'redis://redis:6379/1' + depends_on: + - web + - redis + command: > + sh -c "celery -A KeRS beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler" + redis: + image: redis + # Db stuff + db: + image: mysql + command: --default-authentication-plugin=mysql_native_password + environment: + MYSQL_ROOT_PASSWORD: example + MYSQL_DATABASE: kers + MYSQL_USER: kers + MYSQL_PASSWORD: kers + # DB visual panel + adminer: + image: adminer + depends_on: + - db + ports: + - 8080:8080