Just finished setting up a staging server on openshift. Here are the lessons learnt(and by extension the reason it too the better part of a week) getting it done.
1st, a small note about the 'cloud'. This is a generally misunderstood concept but at the heart of it, it boils down to someone else setting up the computer infrastructure you need to deploy your solution. It might be in the form of Infrasture (IaaS), Platform(PaaS) or Software(SaaS). With all of these however, you must understand the limitations and implications of different vendors because(believe me) they are different.
Openshift provides Platform as a Service and with that comes a runtime environment(e.g Python), Databases(e.g MySQL) and other server related stuff like a shell(yaay). That said, openshift however enforces restrictions, the most important (to us) being:
1. Restricted ports. You are NOT allowed to arbitrarily bind to ports from your application. You may only listen in on internal ports ranging 15000-35530 and even then, these ports are only visible internally.
2. WSGI compliance - By default, the python instance runs on a WSGI compliant server which presents a real headache when you want to run your own custom server instance(e.g Twisted).
With that understood, this how to replicate a Gru Instance setup.
1. Create an account with openshift(obviously)
2. Setup a Python(2.7) app
3. Add the necessary pieces needed i.e MySQL, phpmyadmin
Now clone the repo to your localhost and you'll see a file name wsgi.py in there. This the default file that openshift loads your app with and has 2 end points
a) Health - just returns 1 if the server is up and running
b) env - Returns a list of environment variables on the server. This should obviously be turned off on a production server.
Before continuing, it's imperative to understand that openshift does NOT maintain a system-wide installation of 3rd party modules. If your app uses any 3rd party(like ours does), the needed modules need to be added to setup.py so that everytime your app is being built, these dependencies will be installed.
Add the needed dependencies in the "install_requires" list.
Selector is a great 3rd party module for routing URLs in an easy way -- https://github.com/lukearno/selector/
Now, what we need to do is replace wsgi.py with our very own custom app.py. Openshift will instead load our app.py each time a request comes through.
Of importance to note is
Line 31 -- Where you point to your own python application(in this case index.py)
Note that the host for MySQLdb is the internal openshift IP(in shell $ echo $OPENSHIFT_MYSQL_DB_HOST).
Also take great note of how to call and return a method (view def status) as described here -- http://webpython.codepoint.net/wsgi_application_interface
As mentioned previously, the 'Selector' is a great 3rd party module for routing URLs as demonstrated by lines 53-58
No comments:
Post a Comment