-
Notifications
You must be signed in to change notification settings - Fork 12
/
fabfile.py
executable file
·101 lines (79 loc) · 3.9 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from fabric.api import *
from fabric.contrib.files import *
from fabric.context_managers import shell_env, prefix, env
from fabric.colors import green
import os
root_dir = '/home/ubuntu'
host = 'janaganana'
code_dir = '%s/%s' % (root_dir, host)
virtualenv_name = 'venv'
virtualenv_dir = '%s/%s' % (root_dir, virtualenv_name)
env.user = 'fadmin'
env.password = os.environ.get('fab_pwd','ubuntu')
def initial_config():
""" Configure the remote host to run Census Reporter. """
sudo('mkdir -p %s' % root_dir)
sudo('chown www-data:www-data %s' % root_dir)
# Install up to virtualenv
sudo('apt-get install -q -y python-setuptools')
sudo('easy_install pip')
sudo('pip --quiet install virtualenv')
# Create virtualenv and add our Flask app to it
sudo('virtualenv --no-site-packages %s' % virtualenv_dir, user='www-data')
sudo('rm -f %s/lib/python2.7/site-packages/censusreporter.pth' % virtualenv_dir, user='www-data')
append('%s/lib/python2.7/site-packages/censusreporter.pth' % virtualenv_dir, '%s/censusreporter' % code_dir, use_sudo=True)
append('%s/lib/python2.7/site-packages/censusreporter.pth' % virtualenv_dir, '%s/censusreporter/apps' % code_dir, use_sudo=True)
sudo('chown www-data:www-data %s/lib/python2.7/site-packages/censusreporter.pth' % virtualenv_dir)
# Install and set up gunicorn in the virtualenv
with prefix('source %s/bin/activate' % virtualenv_dir):
sudo('pip --quiet install gunicorn futures', user='www-data')
sudo('rm -f /etc/init/%s.conf' % host)
upload_template('./server/upstart.conf.template', '/etc/init/%s.conf' % host, use_sudo=True, context={
'domainname': host,
'project_path': code_dir,
'virtualenv_dir': virtualenv_dir,
})
# Configure nginx to proxy requests to gunicorn
sudo('rm -f /etc/nginx/sites-enabled/default')
sudo('rm -f /etc/nginx/sites-enabled/%s' % host)
sudo('rm -f /etc/nginx/sites-available/%s' % host)
upload_template('./server/nginx.site.template', '/etc/nginx/sites-available/%s' % host, use_sudo=True, context={
'domainname': host,
'project_path': code_dir,
})
sudo('ln -s /etc/nginx/sites-available/%s /etc/nginx/sites-enabled' % host)
with settings(warn_only=True):
if sudo('test -d %s' % code_dir, user='www-data').failed:
sudo('git clone git://github.com/censusreporter/censusreporter.git %s' % code_dir, user='www-data')
# Start gunicorn
sudo('start %s' % host)
# Restart nginx
sudo('service nginx restart')
def _install_libgdal():
""" Install the latest libgdal-dev package. """
sudo('apt-get -q update')
sudo('apt-get -q -y install libgdal-dev postgresql-server-dev-9.5')
def _install_nginx():
""" Install and set up nginx. """
sudo('apt-get install -q -y nginx')
def deploy(branch='master'):
""" Deploy the branch to the remote host. """
with cd(code_dir):
sudo('find . -name \'*.pyc\' -delete', user='www-data')
sudo('git pull origin %s' % branch, user='www-data')
# Install pip requirements
with prefix('source %s/bin/activate' % virtualenv_dir):
with shell_env(CPLUS_INCLUDE_PATH='/usr/include/gdal', C_INCLUDE_PATH='/usr/include/gdal'):
sudo('pip --quiet --no-cache-dir install -r requirements.txt', user='www-data')
# Restart gunicorn
sudo('restart %s' % host)
from fabric.api import run
def host_type():
run('uname -s')
def restart():
with prefix('source %s/bin/activate' % virtualenv_dir):
with cd(code_dir):
local('python manage.py collectstatic -c --no-input', shell='/bin/bash')
local('service nginx restart',shell='/bin/bash')
local('find . -name \'*.pyc\' -delete',shell='/bin/bash')
local('gunicorn --worker-class gevent wazimap.wsgi:application -t 120 --bind localhost:8001 -c guni_config.py',shell='/bin/bash')