Python Flask app deployed to OPenShift using Wercker CI

45

Transcript of Python Flask app deployed to OPenShift using Wercker CI

Page 1: Python Flask app deployed to OPenShift using Wercker CI
Page 2: Python Flask app deployed to OPenShift using Wercker CI

Python & Flask on OpenShiftdeployed bywercker CI

Page 3: Python Flask app deployed to OPenShift using Wercker CI

1. Flask creating an API2. Github and webhooks3. OpenShift what is and how it works w/ Python4. Wercker CI and deployment targets

Page 4: Python Flask app deployed to OPenShift using Wercker CI

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.

Page 5: Python Flask app deployed to OPenShift using Wercker CI

pip install -r requirements.txt

# The Flask Framework

flask

# A helper to run wsgi apps on OPenShift

shiftpy

# Test runner

pytest

pytest-flask

Page 6: Python Flask app deployed to OPenShift using Wercker CI

'/calc/<string:op>/<int:number>/<int:other>'

"sum", "sub", "mul", "div"

$ python api.py - running on localhost:5000

Page 7: Python Flask app deployed to OPenShift using Wercker CI

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)

Page 8: Python Flask app deployed to OPenShift using Wercker CI

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

Page 9: Python Flask app deployed to OPenShift using Wercker CI

py.test tests.py

====== test session starts =======plugins: flaskcollected 4 items tests.py ....==== 4 passed in 0.13 seconds ====

Page 10: Python Flask app deployed to OPenShift using Wercker CI

STEP 1: Create a github repository

Page 11: Python Flask app deployed to OPenShift using Wercker CI

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

Page 12: Python Flask app deployed to OPenShift using Wercker CI

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

Page 13: Python Flask app deployed to OPenShift using Wercker CI

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

Page 14: Python Flask app deployed to OPenShift using Wercker CI
Page 15: Python Flask app deployed to OPenShift using Wercker CI
Page 16: Python Flask app deployed to OPenShift using Wercker CI

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

Page 17: Python Flask app deployed to OPenShift using Wercker CI
Page 18: Python Flask app deployed to OPenShift using Wercker CI

Add wercker to project:

$ git add wercker.yml$ git commit -m"added wercker CI"$ git push -u origin master

Page 19: Python Flask app deployed to OPenShift using Wercker CI
Page 20: Python Flask app deployed to OPenShift using Wercker CI

wsgi.py or app.py

from app import app

from shiftpy.wsgi_utils import envify

app = application = envify(app)

Page 21: Python Flask app deployed to OPenShift using Wercker CI

.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

Page 22: Python Flask app deployed to OPenShift using Wercker CI

Add OpenShift to project:

$ git add wsgi.py$ git add .openshift$ git commit -m"added openshift"$ git push -u origin master

Page 23: Python Flask app deployed to OPenShift using Wercker CI
Page 24: Python Flask app deployed to OPenShift using Wercker CI
Page 25: Python Flask app deployed to OPenShift using Wercker CI
Page 26: Python Flask app deployed to OPenShift using Wercker CI

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

Page 27: Python Flask app deployed to OPenShift using Wercker CI

DEPLOY TO

Page 28: Python Flask app deployed to OPenShift using Wercker CI
Page 29: Python Flask app deployed to OPenShift using Wercker CI
Page 30: Python Flask app deployed to OPenShift using Wercker CI

seunome

Page 31: Python Flask app deployed to OPenShift using Wercker CI

https://openshift.redhat.com/app/console/keys/new

Page 32: Python Flask app deployed to OPenShift using Wercker CI
Page 33: Python Flask app deployed to OPenShift using Wercker CI

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"})

Page 34: Python Flask app deployed to OPenShift using Wercker CI

$ git commit -m"Breaking the tests!!!"$ git push -u origin master

Page 35: Python Flask app deployed to OPenShift using Wercker CI
Page 36: Python Flask app deployed to OPenShift using Wercker CI
Page 37: Python Flask app deployed to OPenShift using Wercker CI
Page 38: Python Flask app deployed to OPenShift using Wercker CI
Page 39: Python Flask app deployed to OPenShift using Wercker CI

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

Page 40: Python Flask app deployed to OPenShift using Wercker CI
Page 41: Python Flask app deployed to OPenShift using Wercker CI
Page 42: Python Flask app deployed to OPenShift using Wercker CI
Page 45: Python Flask app deployed to OPenShift using Wercker CI