Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhea0 committed Oct 25, 2014
0 parents commit 885af0f
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.pyc
*.DS_Store
env
readme.md
8 changes: 8 additions & 0 deletions config/flask_project
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
server {
location / {
proxy_pass http://127.0.0.1:8000;
}
location /static {
alias /home/www/flask_project/static/;
}
}
4 changes: 4 additions & 0 deletions config/flask_project.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[program:flask_project]
command = gunicorn app:app -b localhost:8000
directory = /home/www/flask_project
user = root
2 changes: 2 additions & 0 deletions config/post-receive
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
GIT_WORK_TREE=/home/www/flask_project git checkout -f
152 changes: 152 additions & 0 deletions fabfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
###############
### imports ###
###############

from fabric.api import run, cd, env, lcd, put, prompt, local
from fabric.contrib.files import exists


##############
### config ###
##############

local_app_dir = './flask_project'
local_config_dir = './config'

remote_app_dir = '/home/www'
remote_flask_dir = remote_app_dir + '/flask_project'
remote_nginx_dir = '/etc/nginx/sites-enabled'
remote_supervisor_dir = '/etc/supervisor/conf.d'

env.hosts = ['ADD_IP/DOMAIN_HERE'] # replace with IP address or hostname
env.user = 'root'
# env.password = 'password'


#############
### tasks ###
#############

def install_requirements():
""" Install required packages. """
run('apt-get update')
run('apt-get install -y python')
run('apt-get install -y python-pip')
run('apt-get install -y python-virtualenv')
run('apt-get install -y nginx')
run('apt-get install -y gunicorn')
run('apt-get install -y supervisor')
run('apt-get install -y git')


def install_flask():
"""
1. Create project directories
2. Create and activate a virtualenv
3. Copy Flask files to remote host
"""
if exists(remote_app_dir) is False:
run('mkdir ' + remote_app_dir)
if exists(remote_flask_dir) is False:
run('mkdir ' + remote_flask_dir)
with lcd(local_app_dir):
with cd(remote_app_dir):
run('virtualenv env')
run('source env/bin/activate')
run('pip install Flask==0.10.1')
with cd(remote_flask_dir):
put('*', './')


def configure_nginx():
"""
1. Remove default nginx config file
2. Create new config file
3. Setup new symbolic link
4. Copy local config to remote config
5. Restart nginx
"""
run('/etc/init.d/nginx start')
if exists('/etc/nginx/sites-enabled/default'):
run('rm /etc/nginx/sites-enabled/default')
if exists('/etc/nginx/sites-enabled/flask_project') is False:
run('touch /etc/nginx/sites-available/flask_project')
run('ln -s /etc/nginx/sites-available/flask_project' +
' /etc/nginx/sites-enabled/flask_project')
with lcd(local_config_dir):
with cd(remote_nginx_dir):
put('./flask_project', './')
run('/etc/init.d/nginx restart')


def configure_supervisor():
"""
1. Create new supervisor config file
2. Copy local config to remote config
3. Register new command
"""
if exists('/etc/supervisor/conf.d/flask_project.conf') is False:
with lcd(local_config_dir):
with cd(remote_supervisor_dir):
put('./flask_project.conf', './')
run('supervisorctl reread')
run('supervisorctl update')


def configure_git():
"""
1. Setup bare Git repo
2. Create post-receive hook
"""
if exists('/' + env.user + '/flask_project.git') is False:
with cd('/' + env.user):
run('mkdir flask_project.git')
with cd('flask_project.git'):
run('git init --bare')
with lcd(local_config_dir):
with cd('hooks'):
put('./post-receive', './')
run('chmod +x post-receive')


def run_app():
""" Run the app! """
with cd(remote_flask_dir):
run('supervisorctl start flask_project')


def deploy():
"""
1. Copy new Flask files
2. Restart gunicorn via supervisor
"""
with lcd(local_app_dir):
local('git add -A')
commit_message = prompt("Commit message?")
local('git commit -am "{0}"'.format(commit_message))
local('git push production master')
run('supervisorctl restart flask_project')


def rollback():
"""
1. Quick rollback in case of error
2. Restart gunicorn via supervisor
"""
with lcd(local_app_dir):
local('git revert master --no-edit')
local('git push production master')
run('supervisorctl restart flask_project')


def status():
""" Is our app live? """
run('supervisorctl status')


def create():
install_requirements()
install_flask()
configure_nginx()
configure_supervisor()
configure_git()
1 change: 1 addition & 0 deletions flask_project
Submodule flask_project added at 312dbb
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flask==0.10.1
Jinja2==2.7.3
MarkupSafe==0.23
Werkzeug==0.9.6
itsdangerous==0.24
wsgiref==0.1.2

0 comments on commit 885af0f

Please sign in to comment.