Flask Hello World on OpenShift

By | 25/01/2014


Building further on the very simple Hello World Flask app that I created, I wanted to see how difficult it was to do the same on OpenShift. Luckily, the Openshift team has prepared a template app for deploying Flask apps on Openshift. That makes life a bit easier.

$ rhc app create hello python-2.7
Application Options
Domain:     wymedia
Cartridges: python-2.7
Gear Size:  default
Scaling:    no

Creating application 'hello' ... done
Waiting for your DNS name to be available ... done
Cloning into 'hello'...
The authenticity of host 'hello-wymedia.rhcloud.com (' can't be established.
RSA key fingerprint is cf:ee:77:cb:0e:fc:02:d7:72:7e:ae:80:c0:90:88:a7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'hello-wymedia.rhcloud.com,' (RSA) to the list of known hosts.

Your application 'hello' is now available.

  URL:        http://hello-wymedia.rhcloud.com/
  SSH to:     53fb256e4382ec3a4e0001ad@hello-wymedia.rhcloud.com
  Git remote: ssh://53fb256e4382ec3a4e0001ad@hello-wymedia.rhcloud.com/~/git/hello.git/
  Cloned to:  /home/cisco/Documents/training/Flask_Todo_SQLAlchemy/hello

To see a bit more information about the app, we can use the show-app command from Openshift.

$ rhc show-app hello
hello @ http://hello-wymedia.rhcloud.com/ (uuid: 53fb256e4382ec3a4e0001ad)
  Domain:     wymedia
  Created:    2:00 PM
  Gears:      1 (defaults to small)
  Git URL:    ssh://53fb256e4382ec3a4e0001ad@hello-wymedia.rhcloud.com/~/git/hello.git/
  SSH:        53fb256e4382ec3a4e0001ad@hello-wymedia.rhcloud.com
  Deployment: auto (on git push)

  python-2.7 (Python 2.7)
    Gears: 1 small

Next, we will add the upstream repository to our local git config and we will pul the sample app to our local app.

$ git remote add upstream -m master git://github.com/openshift/flask-example.git
$ git pull -s recursive -X theirs upstream master
warning: no common commits
remote: Counting objects: 122, done.
remote: Total 122 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (122/122), 16.29 KiB, done.
Resolving deltas: 100% (53/53), done.
From git://github.com/openshift/flask-example
 * branch            master     -> FETCH_HEAD
Auto-merging setup.py
Merge made by the 'recursive' strategy.
 .gitignore                          |    4 +++
 .openshift/action_hooks/build       |    5 ++++
 .openshift/action_hooks/deploy      |    5 ++++
 .openshift/action_hooks/post_deploy |    4 +++
 .openshift/action_hooks/pre_build   |    5 ++++
 README                              |   55 +++++++++++++++++++++++++++++++++++
 README.md                           |   39 +++++++++++++++++++++++++
 setup.py                            |    4 +--
 wsgi/application                    |   22 ++++++++++++++
 wsgi/myflaskapp.py                  |   10 +++++++
 wsgi/static/README                  |   12 ++++++++
 11 files changed, 163 insertions(+), 2 deletions(-)
 create mode 100644 .gitignore
 create mode 100755 .openshift/action_hooks/build
 create mode 100755 .openshift/action_hooks/deploy
 create mode 100755 .openshift/action_hooks/post_deploy
 create mode 100755 .openshift/action_hooks/pre_build
 create mode 100644 README
 create mode 100644 README.md
 create mode 100644 data/.gitkeep
 create mode 100644 libs/.gitkeep
 create mode 100644 wsgi/application
 create mode 100644 wsgi/myflaskapp.py
 create mode 100644 wsgi/static/README

The folder structure that we will have after pulling the repository will look like:


Next, we will push the local git to our Openshift application.

$ git push
Counting objects: 129, done.
Compressing objects: 100% (64/64), done.
....[output from git]
remote: Starting Python 2.7 cartridge (Apache+mod_wsgi)
remote: Application directory "wsgi/" selected as DocumentRoot
remote: Application "wsgi/application" selected as default WSGI entry point
remote: -------------------------
remote: Git Post-Receive Result: success
remote: Activation status: success
remote: Deployment completed with status: success
To ssh://53fb256e4382ec3a4e0001ad@hello-wymedia.rhcloud.com/~/git/hello.git/
   96583b2..409254b  master -> master

We can then go to our Openshift application hello-.rhcloud.com to see the results. See the screenshot below. We have now our Hello World running on Openshift.


I have added the full code to Github