[PATCH] settings: Use environment variables by default

Stephen Finucane stephen.finucane at intel.com
Mon Feb 15 03:30:56 AEDT 2016


The Django documentation suggests using environment variables as a way
to allow devs to commit settings files via source control without
including confidential information therein [1]. As such, change the
provided 'production' settings to do this. This should promote best
practices while also ensuring provisioning tools like
'ansible-django-stack' [2] work as expected.

[1] https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
[2] https://github.com/jcalazan/ansible-django-stack

Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>
---
 patchwork/settings/production.example.py |   24 ++++++++++++++++++------
 patchwork/wsgi.py                        |    1 -
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/patchwork/settings/production.example.py b/patchwork/settings/production.example.py
index 5530bf9..195ffcc 100644
--- a/patchwork/settings/production.example.py
+++ b/patchwork/settings/production.example.py
@@ -9,6 +9,8 @@ Design based on:
 
 from __future__ import absolute_import
 
+import os
+
 from .base import *  # noqa
 
 #
@@ -25,20 +27,26 @@ from .base import *  # noqa
 #      chars = string.letters + string.digits + string.punctuation
 #      print repr("".join([random.choice(chars) for i in range(0,50)]))
 
-# SECRET_KEY = '00000000000000000000000000000000000000000000000000'
+SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
 
 # Email
 #
 # Replace this with your own details
 
-ADMINS = (
-    #    ('Jeremy Kerr', 'jk at ozlabs.org'),
-)
+EMAIL_HOST = os.getenv('EMAIL_HOST', 'localhost')
+EMAIL_PORT = os.getenv('EMAIL_PORT', 25)
+EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER', '')
+EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD', '')
+EMAIL_USE_TLS = True
 
 DEFAULT_FROM_EMAIL = 'Patchwork <patchwork at patchwork.example.com>'
 SERVER_EMAIL = DEFAULT_FROM_EMAIL
 NOTIFICATION_FROM_EMAIL = DEFAULT_FROM_EMAIL
 
+ADMINS = (
+    ('Jeremy Kerr', 'jk at ozlabs.org'),
+)
+
 # Database
 #
 # If you're using a postgres database, connecting over a local unix-domain
@@ -48,7 +56,11 @@ NOTIFICATION_FROM_EMAIL = DEFAULT_FROM_EMAIL
 DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.postgresql_psycopg2',
-        'NAME': 'patchwork',
+        'NAME': os.environ.get('DATABASE_NAME', ''),
+        'USER': os.environ.get('DATABASE_USER', ''),
+        'PASSWORD': os.environ.get('DATABASE_PASSWORD', ''),
+        'HOST': os.environ.get('DATABASE_HOST', ''),
+        'PORT': os.environ.get('DATABASE_PORT', ''),
     },
 }
 
@@ -57,4 +69,4 @@ DATABASES = {
 # https://docs.djangoproject.com/en/1.7/ref/settings/#static-files
 #
 
-STATIC_ROOT = '/srv/patchwork/htdocs/static'
+STATIC_ROOT = os.environ.get('STATIC_ROOT', '/srv/patchwork/htdocs/static')
diff --git a/patchwork/wsgi.py b/patchwork/wsgi.py
index c304830..714ea12 100644
--- a/patchwork/wsgi.py
+++ b/patchwork/wsgi.py
@@ -22,7 +22,6 @@
 # Released under the GNU General Public License v2 or later.
 
 import os
-import sys
 
 from django.core.wsgi import get_wsgi_application
 
-- 
1.7.4.1



More information about the Patchwork mailing list