Python Flask app deployed to OPenShift using Wercker CI
-
Upload
bruno-rocha -
Category
Software
-
view
1.754 -
download
3
Transcript of Python Flask app deployed to OPenShift using Wercker CI
Python & Flask on OpenShiftdeployed bywercker CI
1. Flask creating an API2. Github and webhooks3. OpenShift what is and how it works w/ Python4. Wercker CI and deployment targets
Objective:
This talk will show you how to create a really simple Flask API application. Keep its source code on Github and have wercker CI to automatically deploy successful commits to an OpenShift application.
pip install -r requirements.txt
# The Flask Framework
flask
# A helper to run wsgi apps on OPenShift
shiftpy
# Test runner
pytest
pytest-flask
'/calc/<string:op>/<int:number>/<int:other>'
"sum", "sub", "mul", "div"
$ python api.py - running on localhost:5000
api.pyfrom flask import Flask, jsonify
api = Flask(__name__)
@api.route('/calc/<string:op>/<int:number>/<int:other>')
def calc(op, number, other):
operations = {"sum": lambda a, b: a + b,
"mul": lambda a, b: a * b,
"sub": lambda a, b: a - b,
"div": lambda a, b: a / b}
return jsonify({"result": operations[op](number, other)})
if __name__ == "__main__":
api.run(debug=True)
tests.pyimport pytest
from flask import jsonify
from api import calc, api
@pytest.fixture
def app():
return api
def test_sum(app):
assert calc('sum', 1, 2).data == jsonify({'result': 3}).data
def test_mul(app):
assert calc('mul', 5, 3).data == jsonify({'result': 15}).data
def test_sub(app):
assert calc('sub', 10, 5).data == jsonify({'result': 5}).data
def test_div(app):
assert calc('div', 10, 2).data == jsonify({'result': 5}).data
py.test tests.py
====== test session starts =======plugins: flaskcollected 4 items tests.py ....==== 4 passed in 0.13 seconds ====
STEP 1: Create a github repository
Push to github:
$ ls api_project.. api.py tests.py requirements.txt$ git init$ git remote add origin https://github.com/username/repo$ git commit -am"first commit"$ git push -u origin master
DEPLOY:
$ ssh [email protected]$ sudo apt-get install <all_deps>$ git clone https://github.com/repo$ # run tests on production environment$ # setup paths and virtualenv$ # setup webserver$ # setup firewall
DEPLOY IN 1997:
$ ssh [email protected]$ sudo apt-get install <all_deps>$ git clone https://github.com/repo$ # run tests on production environment$ # setup paths and virtualenv$ # setup webserver$ # setup firewall
wercker.ymlbox: wercker/python
build:
steps:
- virtualenv:
name: My virtualenv
install_wheel: false
- script:
name: Install main requirements
code: pip install -r requirements.txt
- script:
name: Run Tests
code: py.test tests.py
Add wercker to project:
$ git add wercker.yml$ git commit -m"added wercker CI"$ git push -u origin master
wsgi.py or app.py
from app import app
from shiftpy.wsgi_utils import envify
app = application = envify(app)
.openshift/action_hooks/deploy
#!/bin/bash
source ${OPENSHIFT_PYTHON_DIR}virtenv/bin/activate
cd $OPENSHIFT_REPO_DIR
echo "installing requirements"
pip install -r requirements.txt --upgrade
Add OpenShift to project:
$ git add wsgi.py$ git add .openshift$ git commit -m"added openshift"$ git push -u origin master
box: wercker/python
build:
steps:
- virtualenv:
name: My virtualenv
install_wheel: false
- script:
name: Install main requirements
code: pip install -r requirements.txt
- script:
name: Run Tests
code: py.test tests.py
deploy:
steps:
- script:
name: production
code: fab -R prod_server deploy
Custom Deploy Target - fabric, ansible, chef, puppet etc
DEPLOY TO
seunome
https://openshift.redhat.com/app/console/keys/new
Lets break the tests
@api.route('/calc/<string:op>/<int:number>/<int:other>')
def calc(op, number, other):
operations = {"sum": lambda a, b: a + b,
"mul": lambda a, b: a * b,
"sub": lambda a, b: a - b,
"div": lambda a, b: a / b}
# return jsonify({"result": operations[op](number, other)})
# Break the tests!!! return jsonify({"result": "error"})
$ git commit -m"Breaking the tests!!!"$ git push -u origin master
Fix [email protected]('/calc/<string:op>/<int:number>/<int:other>')
def calc(op, number, other):
operations = {"sum": lambda a, b: a + b,
"mul": lambda a, b: a * b,
"sub": lambda a, b: a - b,
"div": lambda a, b: a / b}
return jsonify({"result": operations[op](number, other)})
$ git commit -m"Fixing the tests!!!"$ git push -u origin master
http://calculator-quokkaproject.rhcloud.com/calc/sum/2/2
http://calculator-quokkaproject.rhcloud.com/apidocs/index.html
https://github.com/rochacbruno/flask-openshift-wercker