August 28th, 2012

Getting started with RedHat's OpenShift

Tutorial for: Django

Looking for an affordable way to get started in cloud computing? Look no further than RedHat's OpenShift platform.

My previous blog post about RedHat OpenShift platform was bland to say the least and did not provide the service it's dues. This tutorial aims on getting you, the reader up and running with OpenShift and Django on this remarkably easy to use platform. This tutorial will focus on using the online control panel to create and manage the app, rather than the rhc command-line tool.

First, you need to create a new account to use the service, so go ahead and do that on their sign-up page, it's free, so what are you waiting for?

Once your account is created and activated, you will be brought to the Create a new Application wizard, where you will be prompted to choose a language that your app will be using. Go ahead and choose Python 2.6. OpenShift doesn't appear to yet support Python 2.7, or Python 3.x yet, but it is possible, if your up to the effort. For now, just choose Python 2.6 for this tutorial, as it is the easiest method to get up and running with Django on OpenShift.

The second page of the wizard will ask you for an app name, which is used as part of the sub-domain for your application. You can configure your app to use a full-fledged domain name later on, refer to the FAQ on getting this configured.

The final page will provide you with information on uploading your application to OpenShift. Go ahead and use the provided instructions to create a local version of your application. Once you have a local version ready on your machine, go into the directory and use the follow git command to get Django placed in there:

$ git remote add upstream -m master git://github.com/openshift/django-example.git
$ git pull -s recursive -X theirs upstream master

At this point, you should go ahead and configure your Django project and add any apps you need. If you also added a Database cartridge, you should change the Database setting in the setting.py before doing your first push. During the first push, it will go ahead and create all the tables and create you an admin user for your Django project. When you are ready to go live, push your application by using:

$ git push

You will see lots of information go by, including your admin password. If your project requires any additional Python deps to work, you can configure this inside the following file: $GIT_ROOT/setup.py:

#!/usr/bin/env python

from setuptools import setup

setup(
    name='YourAppName',
    version='1.0',
    description='OpenShift App',
    author='Your Name',
    author_email='example@example.com',
    url='http://www.python.org/sigs/distutils-sig/',
    install_requires=['Django>=1.3','South==0.7.6'],
)

You really should modify the setup.py file before doing your first push, as it will save you some headaches. If you do plan on using South, you should also modify $GIT_ROOT/.openshift/action_hooks/depoy. By the default it contains a shell script:

#!/bin/bash
# This deploy hook gets executed after dependencies are resolved and the
# build hook has been run but before the application has been started back
# up again.  This script gets executed directly, so it could be python, php,
# ruby, etc.

source $OPENSHIFT_GEAR_DIR/virtenv/bin/activate

if [ ! -f $OPENSHIFT_DATA_DIR/sqlite3.db ]
then
    echo "Copying $OPENSHIFT_REPO_DIR/wsgi/openshift/sqlite3.db to $OPENSHIFT_DATA_DIR"
    cp "$OPENSHIFT_REPO_DIR"wsgi/openshift/sqlite3.db $OPENSHIFT_DATA_DIR
    python "$OPENSHIFT_REPO_DIR".openshift/action_hooks/secure_db.py
else
    echo "Executing 'python $OPENSHIFT_REPO_DIR/wsgi/openshift/manage.py syncdb --noinput'"
    python "$OPENSHIFT_REPO_DIR"wsgi/openshift/manage.py syncdb --noinput
fi

echo "Executing 'python $OPENSHIFT_REPO_DIR/wsgi/openshift/manage.py collectstatic --noinput'"
python "$OPENSHIFT_REPO_DIR"wsgi/openshift/manage.py collectstatic --noinput

As you can see, if you do add South, then you should add something like this to the mix:

echo "Executing 'python $OPENSHIFT_REPO_DIR/wsgi/openshift/manage.py migrate'"
python "$OPENSHIFT_REPO_DIR"wsgi/openshift/manage.py migrate --noinput

OpenShift will execute the contents of this file on every push of the application. You can add other management scripts in here as well.

About Me

My Photo
Names Kevin, hugely into UNIX technologies, not just Linux. I've dabbled with the demons, played with the Sun, and now with the Penguins.




Kevin Veroneau Consulting Services
Do you require the services of a Django contractor? Do you need both a website and hosting services? Perhaps I can help.

This Month

If you like what you read, please consider donating to help with hosting costs, and to fund future books to review.

Python Powered | © 2012-2014 Kevin Veroneau