Follow

How to Deploy Django/Python Applications to Atos Cloud Foundry

Overview

Python applications are supported through the WSGI protocol. Gunicorn is used as the web server serving WSGI applications, including Django. Non-Django applications must have a top-level wsgi.py exposing an application variable that points to a WSGI application.

Dependencies

Python package prerequisites are defined in a top level requirements.txt file which is used by pip to install dependencies. An example is as follows:

Django==1.6.3
gunicorn==18.0
psycopg2==2.5.2
wsgiref==0.1.2
dj-database-url==0.2.2
dj-static==0.0.5
static==0.4
vobject==0.6.6
pytz==2014.3

If you get an error saying 'TypeError: must be encoded string without NULL bytes' then you need to ensure that your requirements.txt file is saved in ANSI encoding.

Database Access

Binded service credentials can be accessed through the VCAP_SERVICES environment variables as a JSON object. The following is an example snippet for settings.py in a Django application where a PostgreSQL service is attached. 

if 'VCAP_SERVICES' in os.environ:
  import json
  vcap_services = json.loads(os.environ['VCAP_SERVICES'])
  psql_srv = vcap_services['postgresql-9.3'][0]
  cred = psql_srv = psql_srv['credentials']
  DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': cred['name'],
    'USER': cred['user'],
    'PASSWORD': cred['password'],
    'HOST': cred['host'],
    'PORT': cred['port'],
  }
}

else:
  DATABASES = {
    "default": {
    "ENGINE": "django.db.backends.sqlite3",
    "NAME": "dev.db",
    "USER": "",
    "PASSWORD": "",
    "HOST": "",
    "PORT": "",
  }
}

If you are using MySQL you will need to use the following instead:

if 'VCAP_SERVICES' in os.environ:
  import json
  vcap_services = json.loads(os.environ['VCAP_SERVICES'])
  mysql_srv = vcap_services['p-mysql'][0]
  cred = mysql_srv = mysql_srv['credentials']
  DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': cred['name'],
    'USER': cred['username'],
    'PASSWORD': cred['password'],
    'HOST': cred['hostname'],
    'PORT': cred['port'],
  }
}

Deploying

Push your app through Atos Cloud Foundry using the command line.

Atos Cloud Foundry will automatically detect the programming language and choose the appropriate buildpack to deploy your Python application.

All you need to do, once you have logged in and targeted a space, is to navigate to the directory containing your application and use the cf push command in the following format:

 cf push app-name

Django Specific

Creating superusers through the syncdb command will fail due to syncdb being run non-interactively. A possible workaround is to get your code to create a superuser so you don't need to do it interactively:

1. Create a new app in your project (e.g. common)

2. Add the app to INSTALLED_APPS in settings.py

3. Add the following to models.py for the new app

from django.conf import settings
from django.contrib.auth import models as auth_models
from django.contrib.auth.management import create_superuser
from django.db.models import signals

# Prevent interactive question about wanting a superuser created. (This code
# has to go in this otherwise empty "models" module so that it gets processed by
# the "syncdb" command during database creation.)
signals.post_syncdb.disconnect(
create_superuser,
sender=auth_models,
dispatch_uid='django.contrib.auth.management.create_superuser')

# Create our own test user automatically.
def create_testuser(app, created_models, verbosity, **kwargs):
  if not settings.DEBUG:
    return
  try:
    auth_models.User.objects.get(username='test')
  except auth_models.User.DoesNotExist:
    print '*' * 80
    print 'Creating test user -- login: test, password: test'
    print '*' * 80
    assert auth_models.User.objects.create_superuser('test', 'x@x.com', 'test')
  else:
    print 'Test user already exists.'

signals.post_syncdb.connect(create_testuser,
sender=auth_models, dispatch_uid='common.models.create_testuser')

4. Perform a syncdb by pushing with a starting command of python manage.py syncdb and a superuser will automatically be created with credentials 'test' for both username and password

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments