Coder Social home page Coder Social logo

openshift-hellotornado's Introduction

Tornado on OpenShift

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 -a hellotornado -t diy-0.1
cd hellotornado

Add the remote github repo to the existing one with a command like:

git remote add upstream git://github.com/giulivo/openshift-hellotornado.git
git pull -X theirs upstream master

Now let's create the virtualenv (and don't forget to run this step 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 pycurl

Now create your diy/hellotornado.py file:

#!/usr/bin/env python
import os

here = os.path.dirname(os.path.abspath(__file__))
os.environ['PYTHON_EGG_CACHE'] = os.path.join(here, '..', 'misc/virtenv/lib/python2.6/site-packages')
virtualenv = os.path.join(here, '..', 'misc/virtenv/bin/activate_this.py')
execfile(virtualenv, dict(__file__=virtualenv))

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    address = os.environ['OPENSHIFT_INTERNAL_IP']
    application.listen(8080, address=address)
    tornado.ioloop.IOLoop.instance().start()

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_INTERNAL_IP:8080
nohup ${OPENSHIFT_REPO_DIR}/diy/hellotornado.py > ${OPENSHIFT_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

Go at http://hellotornado-$YOURDOMAIN.rhcloud.com and enjoy!

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.