Python Django Buildpack for Cloud Foundry

Django is a powerful python framework to rapidly create apps. This post provides you with a buildpack that will help you accelerate the Django framework installation on cloudfoundry.

This post assumes the following:

  • You have created an app using the cf tools
  • You have created a postgres database service using the cf tools
  • You have bound the app and the postgres database service.

After you create your Django project, using the Terminal on Mac (or) a command prompt on your OS, navigate to the Django folder and then follow the steps listed below:

#1. Create the requirements.txt

In the Terminal on Mac (or) the command prompt on your OS, type:

Pip Freeze > requirements.txt

You will see a requirements.txt file in the project folder. This file contains the list of all the dependencies that will be installed on CloudFoundry in support of your Django instance.

#2. Create Procfile

In the Procfile enter the following lines. This is to indicate the worker threads that need to be launched to run your Django framework. The buildpack will say hellodjango.wsgi.

web: gunicorn <yourapp>.wsgi --workers 2

#3. Create run.sh batch file

The batch file should include the following lines with the sequence. This code will enable you to launch your CloudFoundry Django App via Port 80, make all the migrations, conduct the migrations, create an admin user with admin as the user name and password as the password. You can certainly change these values as you deem fit.

Note: You can add other batch processes like compile messages, make messages into the run.sh file.

 #!/bin/bash
 if [ -z "$VCAP_APP_PORT" ];
 then SERVER_PORT=80;
 else SERVER_PORT="$VCAP_APP_PORT";
 fi
 echo port is $SERVER_PORT

 echo "make migrations"
 python manage.py makemigrations

 echo "migrate"
 python manage.py migrate

 echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@email.io', 'password')" | python manage.py shell

 echo [$0] Starting Django Server...
 python manage.py runserver 0.0.0.0:$SERVER_PORT --noreload

#4. Create the manifest.yml

In the manifest.yml, include the following text. You can customize it to your respective app. The most important fields are enclused in “<>”. You can get all the values by using the cf env <appname> command on your command prompt.

---
applications:
- name: <APP NAME>
  memory: 128MB
  disk_quota: 256MB
  random-route: true
  buildpack: python_buildpack
  command: bash ./run.sh
  declared-services:
    <cf servicename>:
      label: <service name label>
      plan: <plan name>
  services:
    - <cf servicename>

#5. Update your Settings.py

The buildpack ROOT_URLCONF points to hellodjango. You need to change this to say:

ROOT_URLCONF = '<app_name>.urls'

The wsgi application variable points to the project as well. Change hellodjango to your app name.

WSGI_APPLICATION = '<app_name>.wsgi.application'

In the terminal or the command prompt on your OS, run the cloud foundry command cf env <appname>. You can extract the database details from JSON and update your settings.py accordingly

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': '<DB_ID from cf env <appname>>',
        'USER': '<username form cf env <appname>> command',
        'PASSWORD': '<password from the cf <appname>> command',
        'HOST': '<host name from the cf <appname>> command',
        'PORT': '5432',
    }
}

#6. Do the cf push

After all of the above is setup, use the cf push to setup your App on Cloud Foundry. Fingers crossed, everything should work. The most typical error would be “refused to accept connection” which really means that you have to recheck the values and the identifiers. If everything goes well, you should be able to launch Django and see the classic Django screen.

This buildpack should work on IBM Bluemix and GE Predix.

#7. Enjoy Djangoing…

You can download the CloudFoundry buildpack from this github location. Please note that the requirements.txt is a bit exhaustive. Please run the pip on your project folder or virtualenv to create a more accurate version of the requirements file.

If you are using this template to create your Django Application, please ensure that in your manage.py, you change the settings filename to what ever is appropriate for your project.

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings") //Change hellodjango.settings

GitHub Location For Files

Leave a Reply

%d bloggers like this: