From f20c370d73a7c34876d51367712f6b2ff05a29c6 Mon Sep 17 00:00:00 2001
From: echoduck <bekaluteshome@gmail.com>
Date: Oct 25 2018 11:20:06 +0000
Subject: [PATCH 1/3] Added Dockerfile, docker-compose.yml, and changed celery settings to allow running Celery as a worker container


---

diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..7dcfbd2
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,21 @@
+FROM python:2-alpine
+
+#Install required system packages
+RUN apk add --update --no-cache build-base bash readline libffi-dev ncurses-dev openssl-dev
+
+# Install required Python packages
+COPY ./requirements /requirements
+RUN pip install -r /requirements/dev.txt
+
+# Set correct DJANGO_SETTINGS_MODULE
+ENV DJANGO_SETTINGS_MODULE=happinesspackets.settings.dev
+
+# Copy project files into container
+COPY . /
+
+RUN ./manage.py collectstatic --noinput
+RUN python manage.py migrate
+
+# Expose Django port
+EXPOSE 8000 
+
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..b392ffb
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,18 @@
+version: "3"
+services:
+  web:
+    build: .
+    command: python manage.py runserver 0.0.0.0:8000
+    ports:
+      - "8000:8000"
+    links:
+      - redis
+  redis:
+    image: redis:alpine
+    ports:
+      - "6379:6379"
+  celery:
+    build: .
+    command: celery worker -A happinesspackets -l info
+    links:
+      - redis
diff --git a/happinesspackets/__init__.py b/happinesspackets/__init__.py
index b35eb93..77a4c6d 100644
--- a/happinesspackets/__init__.py
+++ b/happinesspackets/__init__.py
@@ -1 +1,9 @@
+from __future__ import absolute_import, unicode_literals
+
+# This will make sure the app is always imported when
+# Django starts so that shared_task will use this app.
+from happinesspackets._celery import app as celery_app
+
+__all__ = ('celery_app',)
+
 __author__ = 'erik'
diff --git a/happinesspackets/_celery.py b/happinesspackets/_celery.py
index d61111f..aac1011 100644
--- a/happinesspackets/_celery.py
+++ b/happinesspackets/_celery.py
@@ -1,10 +1,11 @@
+from __future__ import absolute_import
 import os
 from celery import Celery
 
 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'happinesspackets.settings')
 
 app = Celery('happinesspackets')
-app.config_from_object('django.conf:settings')
+app.config_from_object('django.conf:settings', namespace='CELERY')
 
 # Load task modules from all registered Django app configs.
 app.autodiscover_tasks()
diff --git a/happinesspackets/settings/dev.py b/happinesspackets/settings/dev.py
index f3c25ed..3ebeb09 100644
--- a/happinesspackets/settings/dev.py
+++ b/happinesspackets/settings/dev.py
@@ -1,5 +1,7 @@
 # -*- coding: utf-8 -*-
 # noinspection PyUnresolvedReferences
+import json
+
 from .base import *  # noqa
 
 DEBUG = True
@@ -13,13 +15,18 @@ DATABASES = {
     }
 }
 
-# EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
-EMAIL_HOST = 'localhost'
-EMAIL_PORT = 2525
+EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" 
+
+
 SECRET_KEY = 'only-for-testing'
 
 INTERNAL_IPS = ('127.0.0.1',)
 
+DEBUG_TOOLBAR_CONFIG = {
+    "SHOW_TOOLBAR_CALLBACK" : lambda request: DEBUG,
+}
+
+
 SECURE_SSL_REDIRECT = False
 SESSION_COOKIE_SECURE = False
 CSRF_COOKIE_SECURE = False
@@ -42,3 +49,17 @@ INSTALLED_APPS += (
 )
 
 SELENIUM_SCREENSHOT_DIR = PROJECT_DIR.child('selenium-screenshots')
+
+
+# Uses a separate Docker container to act as the Redis server
+CELERY_BROKER_URL = 'redis://redis:6379/0'
+CELERY_RESULT_BACKEND = 'redis://redis:6379/0'
+
+# Loads OIDC Client ID and Secret from client_secrets.json
+
+with open("client_secrets.json") as f:
+    secrets = json.load(f)["web"]
+    OIDC_RP_CLIENT_ID = secrets["client_id"]
+    OIDC_RP_CLIENT_SECRET = secrets["client_secret"]
+
+

From 4bbc7113a5f36f3d7a31020d53045592bc6305cc Mon Sep 17 00:00:00 2001
From: echoduck <bekaluteshome@gmail.com>
Date: Oct 25 2018 11:20:06 +0000
Subject: [PATCH 2/3] Changed README for use with Docker


---

diff --git a/README.md b/README.md
index 7dd90d4..357dcc2 100644
--- a/README.md
+++ b/README.md
@@ -4,39 +4,26 @@ This project contains the codebase for fedora hosted version of happinesspackets
 
 # Setup
 
-To run this project or the tests, you need to set up a virtualenv, install the dev requirements and set
-the correct ``DJANGO_SETTINGS_MODULE``, for example with::
-
-    virtualenv --no-site-packages --prompt='(happinesspackets)' virtualenv/
-    source virtualenv/bin/activate
-    pip install -r requirements/dev.txt
-    export DJANGO_SETTINGS_MODULE=happinesspackets.settings.dev
-
-Before running the server locally, you must collect all the static files and perform a database migration.
-
-    ./manage.py collectstatic
-    python manage.py migrate
+Make sure you have Docker and Docker Compose installed.
 
 In order for the login and send views to work, you must supply an OpenID Connect Client ID and Client Secret:
 
     oidc-register https://iddev.fedorainfracloud.org/openidc/ http://localhost:8000/oidc/callback/ 
 
-``oidc-register`` outputs to ``client_secrets.json``. Export the client ID as ``OIDC_RP_CLIENT_ID`` and the secret as ``OIDC_RP_CLIENT_SECRET``
-    
-    export OIDC_RP_CLIENT_ID=<client id>
-    export OIDC_RP_CLIENT_SECRET=<client secret>
+To run on http://localhost:8000/ :
 
-To run on http://127.0.0.1:8000/ :
+    docker-compose up
 
-    python manage.py runserver
+After making any changes to the code, make sure to rebuild the container:
 
-Don't forget to start the mail server:
+    docker-compose up --build
 
-    python -m smtpd -n -c DebuggingServer localhost:2525
 
 The ``t`` command is a very short shell script that runs the tests with the correct settings and reports on coverage.
 
 To run it:
+    
+    docker-compose exec web sh
     ./t
 
 To run the integration tests::

From 1895add9d445f09d99e48a7668c1df62c9999593 Mon Sep 17 00:00:00 2001
From: echoduck <bekaluteshome@gmail.com>
Date: Oct 25 2018 20:30:43 +0000
Subject: [PATCH 3/3] Added wrapper script to detect if client_secrets.json is present, and to generate it if not.


---

diff --git a/Dockerfile b/Dockerfile
index 7dcfbd2..ebff58d 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -13,6 +13,12 @@ ENV DJANGO_SETTINGS_MODULE=happinesspackets.settings.dev
 # Copy project files into container
 COPY . /
 
+
+# Check if client_secrets.json is present, and generate if not
+RUN apk add --update --no-cache curl
+RUN chmod +x generate_client_secrets.sh
+RUN ./generate_client_secrets.sh
+
 RUN ./manage.py collectstatic --noinput
 RUN python manage.py migrate
 
diff --git a/README.md b/README.md
index 357dcc2..10a18f0 100644
--- a/README.md
+++ b/README.md
@@ -8,7 +8,8 @@ Make sure you have Docker and Docker Compose installed.
 
 In order for the login and send views to work, you must supply an OpenID Connect Client ID and Client Secret:
 
-    oidc-register https://iddev.fedorainfracloud.org/openidc/ http://localhost:8000/oidc/callback/ 
+    chmod +x generate_client_secrets.sh 
+    ./generate_client_secrets.sh 
 
 To run on http://localhost:8000/ :
 
diff --git a/generate_client_secrets.sh b/generate_client_secrets.sh
new file mode 100755
index 0000000..112b8f9
--- /dev/null
+++ b/generate_client_secrets.sh
@@ -0,0 +1,7 @@
+#!/bin/sh
+if [ ! -f "client_secrets.json" ] 
+then
+	echo "client_secrets.json not found, generating..."
+ 	curl --request POST --header "Content-Type: application/json" --data '{"redirect_uris": ["http://localhost:8000/oidc/callback/"], "application_type": 
+"native","token_endpoint_auth_method": "client_secret_post"}' https://iddev.fedorainfracloud.org/openidc/Registration -o client_secrets.json
+fi
diff --git a/happinesspackets/settings/dev.py b/happinesspackets/settings/dev.py
index 3ebeb09..a7ccc0b 100644
--- a/happinesspackets/settings/dev.py
+++ b/happinesspackets/settings/dev.py
@@ -58,7 +58,7 @@ CELERY_RESULT_BACKEND = 'redis://redis:6379/0'
 # Loads OIDC Client ID and Secret from client_secrets.json
 
 with open("client_secrets.json") as f:
-    secrets = json.load(f)["web"]
+    secrets = json.load(f)
     OIDC_RP_CLIENT_ID = secrets["client_id"]
     OIDC_RP_CLIENT_SECRET = secrets["client_secret"]
 

