Tech Monger

Programming, Web Development and Computer Science.

Skip to main content| Skip to information by topic

How to create simple Flask application on Openshift

In this tutorial we will learn deployment of flask application on openshift. I assume that you have some working knowledge of openshift, python, flask and git.

Prerequisites

Make sure you answer affirmatively to the following things before we proceed with this tutorial.

  • Have working Openshift account.

  • Have created Openshift namespace.

  • Have created Python application on Openshift.

  • Can ssh into application repository using

    • ssh app-id@app-name-namespace.rhcloud.com
    • ssh 5845734b0c1e66a7fd00002f@blog-monger.rhcloud.com
  • Have cloned application git repository locally using

    • git clone ssh://application-id@app-name-namespace.rhcloud.com/~/git/tech.git/
    • git clone ssh://5845734b0c1e66a7fd00002f@blog-monger.rhcloud.com/~/git/blog.git/
  • Can see default openshift python web page at

    • https://app-name-namespace.rhcloud.com

    • https://blog-monger.rhcloud.com

If you have answered Yes to above questions then let’s start with Hello World Flask App.

Creating Application Locally

Before deploying we will create flask application locally. You should have flask package installed locally in your python setup. You can either install flask globally or better install it in virtual environment using

pip install flask

Create file web_app.py in your project folder and copy following hello world app code into the file.

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

Make sure you are able to see Hello World! in your browser by running created program using

python web_app.py

If you face error in above make sure flask is installed correctly and try again.

Local Openshift Repository

In the cloned openshift repository you would see following three files, each has its purpose.

  • requirements.txt Application Dependencies
  • setup.py Application Metadata and Required Packages
  • wsgi.py WSGI Interface

Note that the default openshift python web page code stored in wsgi.py file.

Editing Local Repository

Update file setup.py with metadata and required installation packages. Your setup.py file should have following content

from setuptools import setup

setup(name='HelloWorld',
      version='1.0',
      description='Simple flask openshift application',
      author='Tech Monger',
      author_email='techmonger@example.com',
      url='http://www.python.org/sigs/distutils-sig/',
      install_requires=['Flask=0.11.1'],
     )
 

Note that we have removed Django from install_requires and added flask.

Now add web_app.py application file that we have created earlier into this repository.

Remove existing default openshift python code from wsgi.py and import web application in wsgi.py. Your wsgi.py file should have following content.

#!/usr/bin/python
import os

virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/'
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
    execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
    pass
#
# IMPORTANT: Put any additional includes below this line.  If placed above this
# line, it's possible required libraries won't be in your searchable path
#

# Importing Hello World application

from web_app import app as application

If you take a look at content of wsgi.py file then you would find that openshift internally uses virtual environment for your application and activates same before starting of the application.

Keep requirements.txt empty as we will discuss more about it further.

Deploying Application

Before we push local changes make sure you have committed the same. To commit changes enter into the local repository folder and do following git operations.

git add .
git commit -m “Hello World App”
git push

Once pushed you would see changes getting deployed on openshift by application restart.

Now you will be able to find your application up and running at application url https://app-name-namespace.rhcloud.com.

Manually Installing Packages

You can also add your dependencies into requirements.txt instead of setup.py but openshift would not install dependencies automatically due to the way directory permissions on openshift are configured. There is workaround to install dependencies manually.

Your requirements.txt file should have following content. You can get these dependencies by doing

pip freeze
Flask==0.11.1
Jinja2==2.8
MarkupSafe==0.23
Werkzeug==0.11.11
argparse==1.2.1
click==6.6
itsdangerous==0.24
wsgiref==0.1.2 

Add, commit and push local changes.

git add .
git commit -m “Manual dependency installation”
git push

Upon pushing you would see following error.

remote: The directory '/var/lib/openshift/5855124a7628e1df1800006e/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

While building package you would get following error.

remote: OSError: [Errno 13] Permission denied: '/var/lib/openshift/585505782d5271c9f400013d/.cache'

To install dependencies manually perform the following

  1. ssh openshift application like below for each package.

    • ssh app-id@app-name-namespace.rhcloud.com
    • ssh 5845734b0c1e66a7fd00002f@blog-monger.rhcloud.com
  2. Once logged in, activate virtual environment of your application remotely on openshift

    • Navagate to openshift python directory using following environment variable

      • cd $OPENSHIFT_PYTHON_DIR
    • Activate Virtual Environment

      • . virtenv/bin/activate
  3. Install dependencies from requirements.txt

    • Navigate to application repository using following environment variable

      • cd $OPENSHIFT_REPO_DIR
    • Install packages using pip with no-cache flag

      • pip install -r requirements.txt --no-cache

Now you shall see that your all dependencies are installed and application is up and running.

Tagged Under : Flask Linux Python Web