This repository has been archived by the owner on Mar 23, 2020. It is now read-only.
/
fabfile.py
201 lines (165 loc) · 5.7 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import time
import urllib2
from fabric.api import run, local, settings, cd, sudo, task, output, puts
from fabric.contrib.project import upload_project
from fabric.contrib.files import upload_template
APPS = 'api courses courses_viz scheduler'.split(' ')
USER = 'www-data'
GROUP = 'www-data'
PYTHON = '/www/yacs/virtualenv/bin/python'
PIP = '/www/yacs/virtualenv/bin/pip'
@task
def verbose():
output['everything'] = True
def exists(name):
with settings(warn_only=True):
return not run('[ -e "%s" ]' % name).failed
def remote_vars(*keys):
sb = []
for key in keys:
value = run('echo $' + key).strip()
sb.append('='.join([key, '"%s"' % value.replace('"', '\\"')]))
return ' '.join(sb)
def upload_monit_conf():
"Uploads the monit conf for gunicorn."
if not exists('/etc/monit/conf.d/'):
puts('monit missing... skipping')
return
puts('Uploading monit config...')
context = dict(
projectpath='/www/yacs/django/',
user=USER,
gunicorn='/www/yacs/virtualenv/bin/gunicorn',
workers=4,
logs='/www/yacs/logs/',
wsgi='yacs.wsgi:application',
pid='/tmp/yacs.pid',
env=remote_vars('YACS_DATABASE_URL', 'YACS_SECRET_KEY'),
)
upload_template('yacs.monit', '/etc/monit/conf.d/yacs.conf',
context=context, use_sudo=True, backup=False)
def update_crontab():
context = dict(
projectpath='/www/yacs/django/',
python='/www/yacs/virtualenv/bin/python',
user=USER,
logpath='/www/yacs/logs/',
)
upload_template('yacs.cron', 'yacs_cron', context=context, backup=False)
sudo('crontab -u {0} yacs_cron'.format(USER))
sudo('rm -f yacs_cron')
def managepy(command, prefix_cmd=''):
sudo('%s %s manage.py %s' % (prefix_cmd, PYTHON, command), user=USER)
@task
def deploy(upgrade=1):
"""Deploys to the given system.
Use salt, chef, or puppet to configure the outside packages.
Things required to be set up:
- python
- database driver
- virtualenv
- coffeescript
- java
- pip
- database (postgres; postgres user)
- created database & user
- webserver (nginx; www-data user)
- webserver config to proxypass to gunicorn (nginx)
- memcached
"""
upload_monit_conf()
clean()
with cd('/www/yacs/'):
if not exists('virtualenv'):
puts('Creating Virtual Environment...')
sudo('virtualenv --distribute virtualenv', user=USER)
puts('Uploading to remote...')
with settings(warn_only=True):
run('rm -rf tmp')
run('mkdir tmp')
upload_project(remote_dir='tmp')
yacs_dirname = sudo('ls tmp').strip()
sudo('mv -f tmp/%s /www/yacs/tmp' % yacs_dirname)
sudo('chown -R %s /www/yacs/tmp' % USER)
sudo('chgrp -R %s /www/yacs/tmp' % GROUP)
run('rm -rf tmp')
with cd('/www/yacs/'):
puts('Replacing remote codebase...')
sudo('rm -rf django', user=USER)
sudo('mv -f tmp django', user=USER)
with cd('/www/yacs/django'):
puts('Removing extra files...')
with settings(warn_only=True):
sudo('find . -name ".*" | xargs rm -r', user=USER)
sudo('rm yacs.db', user=USER)
puts('Installing dependencies...')
pip_prefix = '--upgrade'
if not int(upgrade):
pip_prefix = ''
sudo(PIP + ' install %s -r requirements.txt' % pip_prefix, user=USER)
envs = remote_vars('YACS_ENV', 'YACS_SECRET_KEY', 'YACS_DATABASE_URL')
puts('Running migrations...')
managepy('syncdb --noinput', envs)
managepy('migrate --noinput', envs)
puts('Gathering static files...')
managepy('collectstatic --noinput', envs)
puts("Clearing caches...")
sudo('service memcached restart')
managepy('clear_cache', envs)
puts('Restarting gunicorn...')
sudo('service monit restart')
sudo('monit restart yacs')
update_crontab()
puts('Done!')
@task
def fetch():
"Tells the deployed system to fetch course data."
with cd('/www/yacs/django'):
envs = remote_vars('YACS_ENV', 'YACS_SECRET_KEY', 'YACS_DATABASE_URL')
envs += ' '
puts('Getting course data from SIS...')
sudo(envs + PYTHON + ' manage.py import_course_data')
puts('Fetching catalog data...')
sudo(envs + PYTHON + ' manage.py import_catalog_data')
puts('Generating conflict cache...')
sudo(envs + PYTHON + ' manage.py create_section_cache')
@task
def clean():
"Removes local python cache files."
puts('Removing local object files and caches...')
with settings(warn_only=True):
local('find . -name "*.pyc" | xargs rm')
local('find . -name "*.pyo" | xargs rm')
local('find . -name "__pycache__" -type d | xargs rm -r')
local('rm -r yacs/static/root')
def wait_for_url(url, timeout=30):
while timeout > 0:
handle = None
try:
handle = urllib2.urlopen(url, timeout=timeout)
handle.getcode()
return
except urllib2.URLError:
time.sleep(1)
timeout -= 1
finally:
if handle:
handle.close()
@task
def jasmine(port=6856):
local('jasmine-ci')
@task
def pep8():
local('pep8 . --exclude=migrations,south_migrations,.ropeproject --statistics --count --ignore=E501')
@task
def test():
"Runs tests."
clean()
verbose()
local('python manage.py test --failfast ' + ' '.join(APPS))
pep8()
local('python manage.py collectstatic --noinput')
clean()
@task
def server(port=8000):
local('python manage.py run_gunicorn -b "127.0.0.1:' + str(port) + '" -w 2')