From 91bf8656d5e1be91823f6e1e479c4dcc86a31214 Mon Sep 17 00:00:00 2001 From: Adar Nimrod Date: Sun, 29 Nov 2015 11:09:29 +0200 Subject: [PATCH] Fiddling around with Honcho, Fabric and gunicorn. --- .env | 0 .env.example | 9 +++++++++ .gitignore | 1 + Procfile | 1 + README.rst | 19 ++++++++++++++++++- Realestate/{Realestate => }/__init__.py | 0 Realestate/{Realestate => }/settings.py | 25 ++++++++++++++++++++----- Realestate/{Realestate => }/urls.py | 0 Realestate/{Realestate => }/wsgi.py | 0 Vagrantfile | 8 +------- fabfile.py | 12 ++++++++++++ Realestate/manage.py => manage.py | 0 requirements.txt | 1 + 13 files changed, 63 insertions(+), 13 deletions(-) delete mode 100644 .env create mode 100644 .env.example rename Realestate/{Realestate => }/__init__.py (100%) rename Realestate/{Realestate => }/settings.py (80%) rename Realestate/{Realestate => }/urls.py (100%) rename Realestate/{Realestate => }/wsgi.py (100%) create mode 100755 fabfile.py rename Realestate/manage.py => manage.py (100%) diff --git a/.env b/.env deleted file mode 100644 index e69de29..0000000 diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..463e650 --- /dev/null +++ b/.env.example @@ -0,0 +1,9 @@ +SECRET_KEY='7t^^#zw41ol8v*to-ti*jx7)iq@-=l9jquq1!5f!e02o!j2xyu' +DEBUG="True" +ALLOWED_HOSTS="*" +DB_NAME="realestate" +DB_USER="realestate" +DB_PASSWORD="password" +DB_HOST="localhost" +APP_HOST="localhost" +#SECURE_COOKIES="False" diff --git a/.gitignore b/.gitignore index 7f1c056..bf41971 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ ~* *.pyc .vagrant +.env diff --git a/Procfile b/Procfile index e69de29..fe5d7eb 100644 --- a/Procfile +++ b/Procfile @@ -0,0 +1 @@ +app: gunicorn diff --git a/README.rst b/README.rst index 95999ce..c5c4052 100644 --- a/README.rst +++ b/README.rst @@ -1,4 +1,21 @@ django-realestate ***************** -A Django application for managing real estate listings. +A Django application for managing real estate listings. This is also an +excersice in building a 12-factor application. + +Requirements +------------ + +- Python3 + +Usage +----- + +TODO +---- + +- Provisioning (Fabric for DB hosts, App hosts, maybe Cache hosts, using + environment variables, SystemD service for App with setting under + /etc/default). +- Investigate default enviroment in Honcho. diff --git a/Realestate/Realestate/__init__.py b/Realestate/__init__.py similarity index 100% rename from Realestate/Realestate/__init__.py rename to Realestate/__init__.py diff --git a/Realestate/Realestate/settings.py b/Realestate/settings.py similarity index 80% rename from Realestate/Realestate/settings.py rename to Realestate/settings.py index 405ddab..c9f078b 100644 --- a/Realestate/Realestate/settings.py +++ b/Realestate/settings.py @@ -20,12 +20,18 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = '7t^^#zw41ol8v*to-ti*jx7)iq@-=l9jquq1!5f!e02o!j2xyu' +SECRET_KEY = os.environ['SECRET_KEY'] # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +try: + DEBUG = bool(os.environ['DEBUG']) +except KeyError: + pass -ALLOWED_HOSTS = [] +try: + ALLOWED_HOSTS = os.environ['ALLOWED_HOSTS'].split(' ') +except KeyError: + pass # Application definition @@ -76,8 +82,11 @@ WSGI_APPLICATION = 'Realestate.wsgi.application' DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': os.environ['DB_NAME'], + 'USER': os.environ['DB_USER'], + 'PASSWORD': os.environ['DB_PASSWORD'], + 'HOST': os.environ['DB_HOST'] } } @@ -100,3 +109,9 @@ USE_TZ = True # https://docs.djangoproject.com/en/1.8/howto/static-files/ STATIC_URL = '/static/' + +try: + SESSION_COOKIE_SECURE = bool(os.environ['SECURE_COOKIES']) + CSRF_COOKIE_SECURE = bool(os.environ['SECURE_COOKIES']) +except KeyError: + pass diff --git a/Realestate/Realestate/urls.py b/Realestate/urls.py similarity index 100% rename from Realestate/Realestate/urls.py rename to Realestate/urls.py diff --git a/Realestate/Realestate/wsgi.py b/Realestate/wsgi.py similarity index 100% rename from Realestate/Realestate/wsgi.py rename to Realestate/wsgi.py diff --git a/Vagrantfile b/Vagrantfile index 35dece8..5cb3287 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -3,15 +3,9 @@ Vagrant.configure(2) do |config| config.vm.box = "debian/jessie64" - if Vagrant.has_plugin?("landrush") - config.landrush.enabled = true - config.landrush.tld = "vagrant" - end config.vm.synced_folder ".", "/vagrant", disabled: true - config.vm.synced_folder "./", "/opt/realestate" config.vm.provision "shell", inline: <<-SHELL sudo apt-get update - sudo apt-get install -yf python3-pip python3-dev postgresql postgresql-client python3-psycopg2 - sudo pip3 install -r /opt/realestate/requirements.txt + sudo apt-get install -yf postgresql redis-server SHELL end diff --git a/fabfile.py b/fabfile.py new file mode 100755 index 0000000..254bd52 --- /dev/null +++ b/fabfile.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +from fabric.api import env, task, hosts +import os + + +env.use_ssh_config = True + + +@task +@hosts(os.environ['APP_HOST']) +def deploy(): + raise NotImplemented diff --git a/Realestate/manage.py b/manage.py similarity index 100% rename from Realestate/manage.py rename to manage.py diff --git a/requirements.txt b/requirements.txt index f80ddb0..4ab59e7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ Django>=1.8 gunicorn honcho +fabric -- GitLab