forked from censusreporter/censusreporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
80 lines (63 loc) · 3.29 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
from fabric.api import *
from fabric.contrib.files import *
root_dir = '/home/www-data'
host = 'censusreporter.org'
code_dir = '%s/%s_app' % (root_dir, host)
virtualenv_name = '%s_venv' % host
virtualenv_dir = '%s/%s' % (root_dir, virtualenv_name)
newrelic_app_name = 'Census Reporter Django'
def install_newrelic(api_key):
""" Install the New Relic Python and Server agents using the specified API key. """
with cd(code_dir):
with prefix('source %s/bin/activate' % virtualenv_dir):
sudo('newrelic-admin generate-config %s %s/newrelic.ini' % (api_key, code_dir), user='www-data')
sudo("sed -i \"s/Python Application/%s/g\" %s/newrelic.ini" % (newrelic_app_name, code_dir), user='www-data')
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 deploy(branch='master'):
"Deploy the specified 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):
sudo('pip --quiet --no-cache-dir install -r requirements.txt', user='www-data')
# Restart gunicorn
sudo('restart %s' % host)