O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Python Flask app deployed to OPenShift using Wercker CI

1.997 visualizações

Publicada em

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

CODE on github: https://github.com/rochacbruno/flask-openshift-wercker

Publicada em: Software
  • Seja o primeiro a comentar

Python Flask app deployed to OPenShift using Wercker CI

  1. 1. Python & Flask on OpenShift deployed by wercker CI
  2. 2. 1. Flask creating an API 2. Github and webhooks 3. OpenShift what is and how it works w/ Python 4. Wercker CI and deployment targets
  3. 3. 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.
  4. 4. pip install -r requirements.txt # The Flask Framework flask # A helper to run wsgi apps on OPenShift shiftpy # Test runner pytest pytest-flask
  5. 5. '/calc/<string:op>/<int:number>/<int:other>' "sum", "sub", "mul", "div" $ python api.py - running on localhost:5000
  6. 6. api.py from 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)
  7. 7. tests.py import 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
  8. 8. py.test tests.py ====== test session starts ======= plugins: flask collected 4 items tests.py .... ==== 4 passed in 0.13 seconds ====
  9. 9. STEP 1: Create a github repository
  10. 10. 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
  11. 11. DEPLOY: $ ssh user@yourserver.com $ 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
  12. 12. DEPLOY IN 1997: $ ssh user@yourserver.com $ 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
  13. 13. wercker.yml 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
  14. 14. Add wercker to project: $ git add wercker.yml $ git commit -m"added wercker CI" $ git push -u origin master
  15. 15. wsgi.py or app.py from app import app from shiftpy.wsgi_utils import envify app = application = envify(app)
  16. 16. .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
  17. 17. Add OpenShift to project: $ git add wsgi.py $ git add .openshift $ git commit -m"added openshift" $ git push -u origin master
  18. 18. 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
  19. 19. DEPLOY TO
  20. 20. seunome
  21. 21. https://openshift.redhat.com/app/console/keys/new
  22. 22. 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"})
  23. 23. $ git commit -m"Breaking the tests!!!" $ git push -u origin master
  24. 24. Fix it! @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)}) $ git commit -m"Fixing the tests!!!" $ git push -u origin master
  25. 25. http://calculator-quokkaproject.rhcloud.com/calc/sum/2/2 http://calculator-quokkaproject.rhcloud.com/apidocs/index.html
  26. 26. https://github.com/rochacbruno/flask-openshift-wercker

×