Fun with OpenShift
February 3rd, 2012 • 1 CommentI’ve been playing around with OpenShift for the past hour. More specifically OpenShift Express which allows you to quickly develop apps Java, Ruby, PHP, Perl and Python and run them in the ‘Cloud’.
As a first exercise I wanted to check that I was able to get a simple Task board up and running. A good tutorial to create such a simple application with Pyramid can be found here.
After signing into OpenShift you can create a new application in the dashboard. Make sure that before doing so you have your namespace setup and your public ssh key configured in OpenShift. After creating the application a git repository is created for you. Simple clone this so you have a local working copy on your machine.
Now have a look inside you local working copy. You will find the directories data, libs, wsgi and some files like a README and the python setuptools related setup.py. Let’s start with editing the setup.py file:
from setuptools import setup setup(name='A simple TODO Application to test OpenShift Features.', version='1.0', description='An OpenShift App', author='Thijs Metsch', author_email='thijs.metsch@opensolaris.org', install_requires=['pyramid'], classifiers=[ 'Operating System :: OS Independent', 'Programming Language :: Python', ], )
Please note the install_required parameter. Using this parameter will ensure that during deployment of you app the necessary requirements get installed – in this case Pyramid.
The following steps are pretty straight forward. The file application in the wsgi directory is as the name states a simple WSGI app. What we need to do is edit this file and let it call the todo app:
#!/usr/bin/python import os virtenv = os.environ['APPDIR'] + '/virtenv/' os.environ['PYTHON_EGG_CACHE'] = os.path.join(virtenv, 'lib/python2.6/site-packages') virtualenv = os.path.join(virtenv, 'bin/activate_this.py') try: execfile(virtualenv, dict(__file__=virtualenv)) except IOError: pass from todo import application
Please note that the file has no ‘.py’ extension.
Now we are ready to create the finally application. Please have a look at this tutorial as the code will almost be identical.
Create a file todo.py and follow the tutorial as described. Make sure you application runs locally. Put the ‘*.mako’ files in the subdirectory static under the wsgi directory. You can place the schema.sql file in the data directory. We will be using a sqlite database which will also be place in there.
The final step is to alter the todo.py file and remove the __main__ part. Instead we will introduce the following application function:
here = os.environ['OPENSHIFT_APP_DIR'] def application(environ, start_response): ''' A WSGI application. Configure pyramid, add routes and finally serve the app. environ -- the environment. start_response -- the response object. ''' settings = {} settings['reload_all'] = True settings['debug_all'] = True settings['mako.directories'] = os.path.join(here, 'static') settings['db'] = os.path.join(here, '..', 'data', 'tasks.db') session_factory = UnencryptedCookieSessionFactoryConfig('itsaseekreet') config = Configurator(settings=settings, session_factory=session_factory) config.add_route('list', '/') config.add_route('new', '/new') config.add_route('close', '/close/{id}') config.add_static_view('static', os.path.join(here, 'static')) config.scan() return config.make_wsgi_app()(environ, start_response)
When done we can push the code to the repository. When done you can visit you application at the given URL.
With this I’ve been able to create the app http://todo-sample.rhcloud.com/ and I also deployed my OCCI example service: http://occi-sample.rhcloud.com/. As you can see pretty straight forward & fast and of course fun!
What would be fun is to create a simple unittesting framework for OpenShift. For now you can find the code pieces at github.
[…] my last post I demoed how OpenShift can be used to deploy WSGI based Python application. But since OpenShift also […]