├── .gitignore ├── .travis.yml ├── Dockerfile ├── LICENSE ├── README.md ├── Testfile ├── app.py ├── config.yml ├── docker-compose.yml ├── requirements.txt ├── stack ├── flask-app-prod.yml ├── flask-app-staging.yml └── haproxy.yml ├── test.yml ├── tests ├── __init__.py ├── integration │ └── __init__.py └── unit │ ├── __init__.py │ └── test_web.py └── web ├── __init__.py ├── db.py └── views.py /.gitignore: -------------------------------------------------------------------------------- 1 | gists/ 2 | 3 | # Byte-compiled / optimized / DLL files 4 | __pycache__/ 5 | *.py[cod] 6 | *$py.class 7 | 8 | # C extensions 9 | *.so 10 | 11 | # Distribution / packaging 12 | .Python 13 | env/ 14 | build/ 15 | develop-eggs/ 16 | dist/ 17 | downloads/ 18 | eggs/ 19 | .eggs/ 20 | lib/ 21 | lib64/ 22 | parts/ 23 | sdist/ 24 | var/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .coverage 43 | .coverage.* 44 | .cache 45 | nosetests.xml 46 | coverage.xml 47 | *,cover 48 | .hypothesis/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | 58 | # Flask stuff: 59 | instance/ 60 | .webassets-cache 61 | 62 | # Scrapy stuff: 63 | .scrapy 64 | 65 | # Sphinx documentation 66 | docs/_build/ 67 | 68 | # PyBuilder 69 | target/ 70 | 71 | # IPython Notebook 72 | .ipynb_checkpoints 73 | 74 | # pyenv 75 | .python-version 76 | 77 | # celery beat schedule file 78 | celerybeat-schedule 79 | 80 | # dotenv 81 | .env 82 | 83 | # virtualenv 84 | venv/ 85 | ENV/ 86 | 87 | # Spyder project settings 88 | .spyderproject 89 | 90 | # Rope project settings 91 | .ropeproject 92 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | 3 | language: python 4 | cache: pip 5 | python: 6 | - '2.7' 7 | - '3.5' 8 | - '3.6-dev' 9 | 10 | services: docker 11 | install: docker-compose -f test.yml build 12 | script: docker-compose -f test.yml run test /bin/bash -c "curl -s https://codecov.io/env -o cc_env.sh; bash cc_env.sh; python -m pytest --cov=web/ tests; curl -s https://codecov.io/bash -o cc_bash.sh; bash cc_bash.sh -t 813d8877-27f0-49a6-98da-3e6bc71b12aa" 13 | 14 | notifications: 15 | slack: 16 | secure: YGoOJ+3fJUS9ryiOH1zUVl+meldex5lf/x1z08YxEY2EU2uASCQkXvdk5206bzVYOFr+n83le3vcM5okgfcTRfukSG0IKlPCk5ermS4p5KhqbHMo4ATYM9ZdAofQEXaNhp3sVVVtHAJrGBYhK/3Mp0gc7/rQ5FrbdFxzt2RzterA41ZR0cZWJlIDJ9GKmJzypGe+lmXx/Ng2D1vAup9H/eD/J+q3Byrz5iAJzk/1cLVKCVGNMEj9EkRvZ1iyReT91wMgIwSYbJg0qWnOsCk2OuwE7BEZT78UUYSRoeusrk7kgKHUpzyv7IIeYImR38aBQwvULewp7nKPfjXAkP+k5uSv5NdtaEgkhxnwFIHtg8KhCdLvewxgMbt7mLVWznmZ3BwsAv70RbrsnfhFESvO02q2iSBdu8eAYrsR1T2z6k9IVGhOATrapimSRBLBbMrr5K+SC4zAY4KNp2Ka4Hkz5ddflfyNcTq1/LFexBbXOMk8+kzgKbokeb1hMV7+GMTtr0rL/zIO69T7RPRxeoCJBhKYmsrkLzD1lLpdkcSXqd8Joqbn9QdKNYm13FzEAcrN9fxb12xqmY4tfhDP1xaYRuaeacchxNuUVPykQwCGxc+8+6cGpKpHJ7WVXI0nosTVp7FhD2viCpzyTZWNCK2yLnIgAmWnJehExNLOCU7vPOg= 17 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3-alpine 2 | 3 | # RUN apk add --update bash curl git && rm -rf /var/cache/apk/* 4 | 5 | COPY . /usr/src/app 6 | WORKDIR /usr/src/app 7 | 8 | RUN pip install -r requirements.txt 9 | CMD ["python", "app.py"] 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Brennan Vincello 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | []() 2 | [](https://requires.io/github/brennv/flask-app/requirements/?branch=master) 3 | [](https://travis-ci.org/brennv/flask-app) 4 | [](https://codecov.io/gh/brennv/flask-app) 5 | [](https://codeclimate.com/github/brennv/flask-app) 6 | []() 7 | 8 | # flask-app 9 | 10 | Example app for demonstrating continuos integration/continuos deployment (CI/CD) workflows -- inspired by [dockercloud-quickstart-python](https://github.com/docker/dockercloud-quickstart-python). 11 | 12 | The example flask app connects to a [redis](http://redis.io/) instance and displays a simple visit counter and the hostname of the docker container serving the app. 13 | 14 | ## Getting started 15 | 16 | Install [docker](https://docs.docker.com/engine/installation/) and run: 17 | 18 | ```shell 19 | docker-compose up 20 | # docker-compose stop 21 | ``` 22 | 23 | Otherwise, for the standalone web service: 24 | 25 | ```shell 26 | pip install -r requirements.txt 27 | python app.py 28 | ``` 29 | 30 | Visit [http://localhost:5000](http://localhost:5000) 31 | 32 | ## Development 33 | 34 | Create a new branch off the **develop** branch for features or fixes. 35 | 36 | After making changes rebuild images and run the app: 37 | 38 | ```shell 39 | docker-compose build 40 | docker-compose run -p 5000:5000 web python app.py 41 | # docker stop flaskapp_redis_1 42 | ``` 43 | 44 | ## Tests 45 | 46 | Standalone unit tests run with: 47 | 48 | ```shell 49 | pip install pytest pytest-cov pytest-flask 50 | pytest --cov=web/ --ignore=tests/integration tests 51 | ``` 52 | 53 | Integration and unit tests run with: 54 | 55 | ```shell 56 | docker-compose -f test.yml -p ci build 57 | docker-compose -f test.yml -p ci run test python -m pytest --cov=web/ tests 58 | # docker stop ci_redis_1 ci_web_1 59 | ``` 60 | 61 | Commits tested via [travis-ci.org](https://travis-ci.org/brennv/flask-app). Test coverage reported to [codecov.io](https://codecov.io/gh/brennv/flask-app). Code quality reported via [codeclimate.com](https://codeclimate.com/github/brennv/flask-app). Requirements inspected with [requires.io](https://requires.io/github/brennv/flask-app/requirements). 62 | 63 | After testing, submit a pull request to merge changes with **develop**. 64 | 65 | ## Automated builds and redeploys 66 | 67 | [Docker images](https://hub.docker.com/r/brenn/flask-app/tags/) are automatically built from changes to repo branches and tags via [docker hub autobuilds](https://docs.docker.com/docker-hub/github/). 68 | 69 | Using a cluster provisioned on [docker cloud](https://cloud.docker.com/), services are created as stacks from `stack/` to nodes tagged *infra* or *compute*. Setting stack option `autoredeploy: true` continuously redeploys new images built from recent commits. 70 | 71 | Image tagging and deployment scheme: 72 | 73 | - `flask-app:latest` follows the **master** branch and deploys to **production** at [http://flask-app.example.com](http://flask-app.beta.build) 74 | - `flask-app:develop` follows the **develop** branch and deploys to **staging** at [http://staging.flask-app.example.com](http://staging.flask-app.beta.build) 75 | 76 | *Note:* To create sites at subdomains using virtual hosts as shown in `stack/`, assumes domain records have been configured with: 77 | 78 | - `CNAME` record `*` to `example.com.` 79 | - `A` record `@` to the (floating) IP of the haproxy load balancer 80 | 81 | ## Monitoring, log aggregation and scaling 82 | 83 | Agent containers by [sematext](https://github.com/sematext/sematext-agent-docker) deployed to each node. Alert thresholds trigger web hooks to scale services under load. 84 | 85 | ## Notifications 86 | 87 | Updates and alerts pushed via Slack: 88 | 89 | - github 90 | - travis-ci 91 | - docker 92 | - sematext 93 | -------------------------------------------------------------------------------- /Testfile: -------------------------------------------------------------------------------- 1 | FROM python:3-alpine 2 | 3 | RUN apk add --update bash curl git && rm -rf /var/cache/apk/* 4 | 5 | COPY . /usr/src/app 6 | WORKDIR /usr/src/app 7 | 8 | RUN pip install -r requirements.txt 9 | RUN pip install pytest pytest-cov pytest-flask 10 | 11 | # RUN python -m pytest --cov=web/ tests 12 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from web import create_app 2 | 3 | 4 | if __name__ == '__main__': 5 | app = create_app(debug=False) 6 | app.run(host='0.0.0.0', port=5000) 7 | -------------------------------------------------------------------------------- /config.yml: -------------------------------------------------------------------------------- 1 | # App configs 2 | name: world 3 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | # For local development 2 | web: 3 | build: . 4 | dockerfile: Dockerfile 5 | links: 6 | - redis 7 | ports: 8 | - "5000" 9 | redis: 10 | image: redis 11 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==0.11.1 2 | PyYAML==3.12 3 | redis==2.10.5 4 | -------------------------------------------------------------------------------- /stack/flask-app-prod.yml: -------------------------------------------------------------------------------- 1 | # For creating stack 'flask-app-prod' on nodes tagged 'compute' 2 | flask-app-prod: 3 | autoredeploy: true 4 | deployment_strategy: high_availability 5 | environment: 6 | - VIRTUAL_HOST=flask-app.example.com 7 | expose: 8 | - '80' 9 | image: 'brenn/flask-app:latest' 10 | links: 11 | - redis 12 | ports: 13 | - '80:5000' 14 | restart: always 15 | sequential_deployment: true 16 | tags: 17 | - compute 18 | target_num_containers: 2 19 | # environment: 20 | # - HELLO_NAME=world 21 | redis: 22 | image: 'redis:latest' 23 | -------------------------------------------------------------------------------- /stack/flask-app-staging.yml: -------------------------------------------------------------------------------- 1 | # For creating stack 'flask-app-staging' on nodes tagged 'compute' 2 | flask-app-staging: 3 | autoredeploy: true 4 | environment: 5 | - VIRTUAL_HOST=staging.flask-app.example.com 6 | expose: 7 | - '80' 8 | image: 'brenn/flask-app:develop' 9 | ports: 10 | - '80:5000' 11 | tags: 12 | - compute 13 | links: 14 | - redis 15 | # environment: 16 | # - HELLO_NAME=world 17 | redis: 18 | image: 'redis:latest' 19 | -------------------------------------------------------------------------------- /stack/haproxy.yml: -------------------------------------------------------------------------------- 1 | # For creating stack 'haproxy' on a dedicated node tagged 'infra' 2 | haproxy: 3 | image: 'dockercloud/haproxy:latest' 4 | links: 5 | - 'flask-app-prod.flask-app-prod:flask-app-prod' 6 | - 'flask-app-staging.flask-app-staging:flask-app-staging' 7 | ports: 8 | - '80:80' 9 | restart: always 10 | roles: 11 | - global 12 | tags: 13 | - infra 14 | -------------------------------------------------------------------------------- /test.yml: -------------------------------------------------------------------------------- 1 | # Image 'test' uses Testfile to run `pytest --cov=web/ tests` via travis-ci 2 | test: 3 | build: . 4 | dockerfile: Testfile 5 | links: 6 | - redis 7 | - web 8 | web: 9 | build: . 10 | dockerfile: Dockerfile 11 | links: 12 | - redis 13 | ports: 14 | - "5000" 15 | redis: 16 | image: redis 17 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brennv/flask-app/bf51731ea4085bafbcd27db9461a3a5174f5bbdd/tests/__init__.py -------------------------------------------------------------------------------- /tests/integration/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brennv/flask-app/bf51731ea4085bafbcd27db9461a3a5174f5bbdd/tests/integration/__init__.py -------------------------------------------------------------------------------- /tests/unit/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brennv/flask-app/bf51731ea4085bafbcd27db9461a3a5174f5bbdd/tests/unit/__init__.py -------------------------------------------------------------------------------- /tests/unit/test_web.py: -------------------------------------------------------------------------------- 1 | import os 2 | from flask import Flask, url_for 3 | import pytest 4 | import yaml 5 | 6 | from web import create_app, load_config 7 | 8 | 9 | @pytest.fixture 10 | def app(): 11 | app = create_app() 12 | return app 13 | 14 | 15 | def test_app(client): 16 | response = client.get(url_for('api.index')) 17 | assert response.status_code == 200 18 | name = os.environ.get('HELLO_NAME') or load_config('name') 19 | assert 'hello ' + name in str(response.data) 20 | assert 'fail' not in str(response.data) 21 | -------------------------------------------------------------------------------- /web/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | from .views import api, load_config 3 | 4 | 5 | def create_app(debug=False): 6 | app = Flask(__name__) 7 | app.debug = debug 8 | app.register_blueprint(api) 9 | return app 10 | -------------------------------------------------------------------------------- /web/db.py: -------------------------------------------------------------------------------- 1 | from redis import Redis, RedisError 2 | 3 | redis = Redis(host="redis", db=0) 4 | -------------------------------------------------------------------------------- /web/views.py: -------------------------------------------------------------------------------- 1 | import os 2 | import socket 3 | from flask import Blueprint 4 | from .db import redis, RedisError 5 | import yaml 6 | 7 | api = Blueprint('api', __name__) 8 | 9 | 10 | def load_config(value): 11 | with open('config.yml') as f: 12 | data = yaml.load(f) 13 | return data[value] 14 | 15 | 16 | @api.route("/") 17 | def index(): 18 | try: 19 | visits = redis.incr('counter') 20 | except RedisError: 21 | visits = "redis connection error" 22 | name = os.environ.get('HELLO_NAME') or load_config('name') 23 | hostname = socket.gethostname() 24 | html = "