Python 2.7 Tornado with Flask on OpenShift
Simple DIY cartridge to add Python 2.7, automatic installing from requirements.txt support on OpenShift.
Setting up Openshift
$ rhc app create <appname> diy-0.1 $ cd <appname> $ git remote add upstream -m master git:github.com/campusdebate/openshift-name-coming-soon.git $ git pull -s recursive -X theirs upstream master $ git push
Application
To install additional packages just edit `requirements.txt`.
Additional deploy actions may be performed in the `.openshift/action_hooks/post_deploy` script.
Notes
If you experience problems with downloading (or building) tools on first push or want to use different versions of them then you may want to update URLs of the tools in the `.openshift/action_hooks/build` or versions in the `misc/openshift/config`.
Tornado is an open source version of the scalable, non-blocking web server and tools that power FriendFeed.
This article describes how to deploy a Tornado web application on OpenShift. It has been updated to leverage the DIY cartridge introduced in late March. We will deploy Tornado in a custom tailored virtualenv.
To create our new app on OpenShift:
rhc app create hellotornado -t diy-0.1 --from-code=https://github.com/campusdebate/openshift-tornado-cdb cd hellotornado
Code from the remote repository will be automatically cloned to your current directory.
Go at http://hellotornado-$YOURDOMAIN.rhcloud.com and enjoy!
You can achieve the same by doing the following steps manually. Create a virtualenv in `misc/` (and don't forget to create this on a python2.6 system, as that is the version of python hosted on OpenShift):
virtualenv --no-site-packages misc/virtenv
Activate your virtualenv and install the needed modules:
source misc/virtenv/bin/activate pip install tornado pip install futures pip install pycurl
Now, assuming your app is in app.py, create your start file (diy/hellotornado.py):
#!/usr/bin/env python import os cwd = os.path.dirname(os.path.abspath(__file__)) os.environ['PYTHON_EGG_CACHE'] = os.path.join(cwd, '..', 'misc/virtenv/lib/python2.6/site-packages') virtualenv = os.path.join(cwd, '..', 'misc/virtenv/bin/activate_this.py') execfile(virtualenv, dict(__file__=virtualenv)) import app app.main(os.environ['OPENSHIFT_DIY_IP'])
Make sure your binary file is executable:
chmod a+x diy/hellotornado.py
Delete the `diy/index.html` file or you'll always get only the standard welcome page after pushing the code:
rm diy/index.html
Create the start/stop scripts (these are examples to start with):
$ more .openshift/action_hooks/start #!/bin/bash # The logic to start up your application should be put in this # script. The application will work only if it binds to # $OPENSHIFT_DIY_IP:8080 nohup ${OPENSHIFT_REPO_DIR}/diy/hellotornado.py > ${OPENSHIFT_DIY_LOG_DIR}/hellotornado.log 2>&1 &
$ more .openshift/action_hooks/stop #!/bin/bash # The logic to stop your application should be put in this script. kill `ps -ef | grep hellotornado | grep -v grep | awk '{ print $2 }'` > /dev/null 2>&1 exit 0
Finally, commit and push your project:
git add . git commit -a -m "Initial commit" git push
To run and test you application locally before committing, just source the virtualenv activate file and start with the custom made start/stop scripts.
Have fun!