Openshift and Flask with Virtualenv on OS X

Create a flask app for Openshift with a matching local python virtualenv to perform local testing.

In this case we'll use Python 2.7 on Mac OS X 10.9.

Steps

  1. Install Homebrew
  2. Install Python Development Environment on Mac OS X
  3. Install and Configure a Python Flask for OpenShift

Installing Homebrew

Ready the system for Homebrew

  • First unhide ~/Library folder.

    1. Open Finder
    2. Press shift-command-H
    3. Press command-J
    4. Check Show Library Folder
  • Now Setup shell environment Some of these settings are only relevant to later steps, but go ahead and put them all in now.

    • vim ~/.bash_profile
# Set architecture flags
export ARCHFLAGS="-arch x86_64"
# Ensure user-installed binaries take precedence
export PATH=/usr/local/bin:$PATH

# Load brew bash completeion if it exists
if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion
fi

# pip should only run if there is a virtualenv currently activated
export PIP_REQUIRE_VIRTUALENV=true
# cache pip-installed packages to avoid re-downloading
export PIP_DOWNLOAD_CACHE=$HOME/.pip/cache

# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc
  • Source those changes. Ignore the -bash: brew: command not found error.
. ~/.bash_profile
  • Install command line developer tools or Xcode. You'll need to be on the actual console, so don't do this step over SSH.
xcode-select --install

This will prompt you with a GUI dialog asking you to install the command line developer tools. Click the Install button.

Install Homebrew

  • Install Homebrew
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"

==> This script will install: /usr/local/bin/brew /usr/local/Library/... /usr/local/share/man/man1/brew.1 ==> The following directories will be made group writable: /usr/local/. /usr/local/share ==> The following directories will have their group set to admin: /usr/local/. /usr/local/share

Press RETURN to continue or any other key to abort ==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/. /usr/local/share

WARNING: Improper use of the sudo command could lead to data loss or the deletion of important system files. Please double-check your typing when using sudo. Type "man sudo" for more information.

To proceed, enter your password, or type Ctrl-C to abort.

Password: ==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/. /usr/local/share ==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew ==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew ==> Downloading and installing Homebrew...

Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.

==> Installation successful! ==> Next steps Run brew doctor before you install anything Run brew help to get started

  • Inspect and update brew install.
# This step will possibly point out permissions problems to be fixed.
brew doctor
brew update
  • Install some brew packages
brew install bash-completion ssh-copy-id wget

Install Python

Install the latest 2.7.x Python with Homebrew.

brew install python --with-brewed-openssl

Setup Virtenv

Use pip to install virtualenv.

# override the requirement we set in .bash_profile above. just this once.
PIP_REQUIRE_VIRTUALENV=false pip install virtualenv
mkdir -p ~/src ~/Projects ~/Virtualenvs

Setup Openshift

Interaction with Openshift is via the website and via the rhc client tool.

  • Install rhc
sudo gem install rhc
  • Configure rhc.

You'll need to have a username and login for Openshift before proceeding.

rhc setup

Create an Openshift App and Configure for Virtualenv

It is helpful to have a virtualenv on your development machine(s) which matches the environment of your Openshift app. This enables much quicker turnaround time for quick tests of changes, and doesn't require a git commit and push to see the effect of a change.

  • Create an Openshift app named Flaskapp.
cd ~/src
rhc app create flaskapp python-2.7
  • Clone App locally into ~/src/flaskapp if you created the app using the web site instead of rhc.
rhc git-clone flaskapp
  • Setup venv inside flaskapp. This
cd ~/src/flaskapp/wsgi/
# create venv/ dir
virualenv venv --python-python2.7
# activate this virtual env
. venv/bin/activate
  • Tell git to ignore your venv/ dir.
echo 'venv/' >> ~/src/flaskapp/.gitignore
  • Install Flask in the new venv.
pip install flask flask-wtf flask-babel markdown flup 
  • Tell our app's setup.py about our python module requirements.
cd ~/src/flaskapp
vim setup.py
  • Modify the install_requires line to look like this:
install_requires=['Flask','flask-wtf','flask-babel','markdown','flup'],

Create Hello World Flask App

Create the required directories

cd ~/src/flaskapp/wsgi
mkdir -p app/{static,templates}
mkdir tmp
cd ~/src/flaskapp/wsgi/app

Create applications files. Pay attention Openshift has some particular requirements.

  • ~/src/flaskapp/wsgi/app/__init__.py
from flask import Flask  
app = Flask(__name__)  
from app import views
  • ~/src/flaskapp/wsgi/app/views.py
from app import app
@app.route('/')
@app.route('/index')
def index():
    return "Hello, World!"
  • ~/src/flaskapp/wsgi/application - This application file is required by OpenShift
#!/usr/bin/python
import os
import sys
sys.path.insert(0, os.path.dirname(__file__) or '.')
PY_DIR = os.path.join(os.environ['OPENSHIFT_HOMEDIR'], "python")
virtenv = PY_DIR + '/virtenv/'
PY_CACHE = os.path.join(virtenv, 'lib', os.environ['OPENSHIFT_PYTHON_VERSION'], 'site-packages')
os.environ['PYTHON_EGG_CACHE'] = os.path.join(PY_CACHE)
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')

try:
    exec(open(virtualenv).read(), dict(__file__=virtualenv))
except IOError:
    pass

from run import app as application
  • ~/src/flaskapp/wsgi/run.py - Called by application.
from app import app
if __name__ == "__main__":
    app.run(debug = True) #We will set debug false in production 

Test Flaskapp on localhost

Activate venv and run the app.

cd ~/src/flaskapp/
. venv/bin/activate
python run.py
curl http://localhost:5000/index

Deploy Flaskapp to Openshift

After making local changes, commit them to git, and push them to the origin. Openshift will then automagically install the required flask modules and spin up your Flaskapp.

cd ~/src/flaskapp
git add .
git commit -a -m 'Firstsies'
git push

Now go check out your new app on Openshift

References

Syndicate

Subscribe to Syndicate