flask-continuous-env
flask-continuous-env copied to clipboard
Continuous Integration and Deployment Environment for Flask & Travis CI.
Continuous Integration and Deployment with Flask & Travis CI
Build environment for Flask development with Grunt for frontend development.
Testing and integration handled by Travis CI.
Zero-downtime deployment stack with Nginx and Gunicorn, configured easily with Fabric locally or from Travis CI.
Build flow based off of Batista Harahap's configuration
Setup
- Install the following:
- Run
npm install -g bower -g lessto install bower & lessc - Run
bower installto install browser packages from bower.json into /static/bower_components/ - Install virtualenv using
pip install virtualenv(Note: see flask virtualenv install docs for more info) - Setup virtualenv:
- Run
virtualenv env - On Windows:
env\scripts\activate; On Linux:. env/bin/activate
- Run
- Install python packages with
pip install -r requirements.txt(NOTE: Make sure you have activated the python virtualenv prior.) - Copy
flask_site/config/config_sample.ymltoflask_site/config/config.ymland DO NOT ADD IT TO GIT (do not deleteconfig_sample.yml, it is used for testing) - Change
secret_keyinconfig.yml
Develop
- New Flask routes are added to
routes.ymlwith associated controllers in thecontrollerssub-package - Install JS and CSS plugin resources with bower:
- Use
bower search PACKAGEto find a package. - Use
bower install PACKAGE --saveto install and remember package inbower.json(i.e.bower install animate.css --save) - Add resources to bundles in
bundles.ymlso they are compiled by Flask-Assets
- Use
- If you install a new python package with pip, run:
pip freeze > requirements.txt - See Testing Flask Applications for useful info on how to make tests comprehensive.
- If you make any changes to
flask_site/config/config.yml, the file must be manually uploaded to/home/$USER/blue-green/config/config.yml
Run
Run python start.py development to start Flask locally at 127.0.0.1:5000.
Run python start.py to test the production configuration.
Test
Run nosetests --with-xunit --with-xcoverage --cover-package=flask_site --cover-erase tests
Deploy
Fabric is used to easily setup and push code to deployment servers. Deployment configuration is based off of the 0-downtime blue-green deployment style.
Tested on Debian wheezy, but with minor alterations to fabfile.py it should work with other distros.
Variables
Several variables are referenced in this section. Following is their descriptions:
$DEPLOY_HOSTS- the hosts you will deploy to (comma separated - I've only tested with one host)$DEPLOY_PASS- the ssh password you will set for your deployment user (default:admin) - NOT THE ROOT PASSWORD$LIVE_SERVER_URL- the URL people will use to access your server (i.e. example.com)$NEXT_SERVER_URL- the URL you can use to test changes before going live (i.e. dev.example.com)
Setting up
This only has to be done once. If you'd like to use a different user besides admin, change env.user in fabfile.py
From your remote machine as root:
apt-get updateapt-get install -y sudo(this is only necessary if it's not already installed)adduser admin, and set the password to something feisty.adduser admin sudo
From your local machine:
pip install fabric gitricfab -H $DEPLOY_HOSTS -p $DEPLOY_PASS --set LIVE_SERVER_URL=$LIVE_SERVER_URL,NEXT_SERVER_URL=$NEXT_SERVER_URL prod setup_machine
Deploy automatically using Travis CI
See .travis.yml if you're interested in exactly what's going on.
If you'd like to automatically deploy but manually switch from the new version to live, remove cutover from .travis.yml and
skip step 1 of the "Deploy Manually" section.
- Navigate to Travis CI and enable this repository to be built (login with your Github credentials).
- In settings, add the following environment variables (make sure they are all set to not display in log):
DEPLOY_HOSTSDEPLOY_PASS
- Commit or push some changes to
masterbranch.
Deploy Manually
fab -H $DEPLOY_HOSTS -p $DEPLOY_PASS prod deploy, enter$DEPLOY_PASSagain when prompted- Alternatively, you can:
- add your local public key to
~/.ssh/authorized_keyson your production servers, or - use
--set SSH_PUB_KEY_FILE=$YOUR_PUB_KEY_LOCATIONto automatically add and remove the key during deployment.
- add your local public key to
- Alternatively, you can:
- (optional) test from
$NEXT_SERVER_URL fab -H $DEPLOY_HOSTS -p $DEPLOY_PASS prod cutover
If you don't intend to test the server before going live, you can run the commands at the same time:
fab -H $DEPLOY_HOSTS -p $DEPLOY_PASS prod deploy cutover
Notes
- To skip Travis builds, include [ci skip] in the commit message.
- For a nice git branching model: http://nvie.com/posts/a-successful-git-branching-model/
- Because of Flask-Assets, the first user to visit your newly-deployed site will take a long time to load (while resources compile).
Circumvent this by visiting
$NEXT_SERVER_URLbefore runningfab cutover