forked from FinalsClub/karmaworld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
executable file
·288 lines (230 loc) · 6.11 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""Management utilities."""
import os
from contextlib import contextmanager as _contextmanager
from fabric.api import cd, env, lcd, prefix, run, task, local, settings
########## GLOBALS
env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
env.virtualenv = 'venv-kw'
env.activate = 'workon %s' % env.virtualenv
# Using this env var to be able to specify the function
# used to run the commands. By default it's `run`, which
# runs commands remotely, but in the `here` task we set
# env.run to `local` to run commands locally.
env.run = run
env.cd = cd
########## END GLOBALS
########## HELPERS
@_contextmanager
def _virtualenv():
"""
Changes to the proj_dir and activates the virtualenv
"""
with env.cd(env.proj_dir):
with prefix(env.activate):
yield
########## END HELPERS
########## ENVIRONMENTS
@task
def here():
"""
Connection information for the local machine
"""
# This is required, because local doesn't read the user's
# .bashrc file, because it doesn't use an interactive shell
def _custom_local(command):
prefixed_command = '/bin/bash -l -i -c "%s"' % command
return local(prefixed_command)
# This is required for the same reason as above
env.activate = '/bin/bash -l -i -c "workon %s"' % env.virtualenv
env.proj_dir = os.getcwd()
env.proj_root = os.path.dirname(env.proj_dir)
env.run = _custom_local
env.cd = lcd
env.reqs = 'reqs/dev.txt'
env.confs = 'confs/dev/'
env.branch = 'master'
@task
def beta():
"""
Beta connection information
"""
env.user = 'djkarma'
env.hosts = ['beta.karmanotes.org']
env.proj_root = '/var/www/karmaworld'
env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
env.reqs = 'reqs/prod.txt'
env.confs = 'confs/beta/'
env.branch = 'beta'
@task
def prod():
"""
Production connection information
"""
env.user = 'djkarma'
env.hosts = ['karmanotes.org']
env.proj_root = '/var/www/karmaworld'
env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
env.reqs = 'reqs/prod.txt'
env.confs = 'confs/prod/'
env.branch = 'master'
########## END ENVIRONMENTS
########## DATABASE MANAGEMENT
@task
def syncdb():
"""Runs syncdb (along with any pending South migrations)"""
env.run('python manage.py syncdb --noinput --migrate')
########## END DATABASE MANAGEMENT
########## FILE MANAGEMENT
@task
def manage_static():
"""
Collects, compresses and uploads static files.
"""
collect_static()
compress_static()
upload_static()
@task
def collect_static():
"""Collect all static files, and copy them to S3 for production usage."""
env.run('python manage.py collectstatic --noinput')
@task
def compress_static():
"""
Compresses the static files.
"""
pass
@task
def upload_static():
"""
Uploads the static files to the specified host.
"""
pass
########## END FILE MANAGEMENT
########## COMMANDS
@task
def make_virtualenv():
"""
Creates a virtualenv on the remote host
"""
env.run('mkvirtualenv --no-site-packages %s' % env.virtualenv)
@task
def update_reqs():
"""
Makes sure all packages listed in requirements are installed
"""
with _virtualenv():
env.run('pip install -r %s' % env.reqs)
@task
def clone():
"""
Clones the project from the central repository
"""
env.run('git clone %s %s' % (env.proj_repo, env.proj_dir))
@task
def update_code():
"""
Pulls changes from the central repo and checks out the right branch
"""
with env.cd(env.proj_dir):
env.run('git pull && git checkout %s' % env.branch)
@task
def start_supervisord():
"""
Starts supervisord
"""
with _virtualenv():
config_file = os.path.join(env.confs, 'supervisord.conf')
env.run('supervisord -c %s' % config_file)
@task
def stop_supervisord():
"""
Restarts supervisord
"""
with _virtualenv():
config_file = os.path.join(env.confs, 'supervisord.conf')
env.run('supervisorctl -c %s shutdown' % config_file)
@task
def restart_supervisord():
"""
Restarts supervisord
"""
stop_supervisord()
start_supervisord()
def supervisorctl(action, process):
"""
Takes as arguments the name of the process as is
defined in supervisord.conf and the action that should
be performed on it: start|stop|restart.
"""
supervisor_conf = os.path.join(env.confs, 'supervisord.conf')
env.run('supervisorctl -c %s %s %s' % (supervisor_conf, action, process))
@task
def start_celeryd():
"""
Starts the celeryd process
"""
supervisorctl('start', 'celeryd')
@task
def stop_celeryd():
"""
Stops the celeryd process
"""
supervisorctl('stop', 'celeryd')
@task
def restart_celery():
"""
Restarts the celeryd process
"""
supervisorctl('restart', 'celeryd')
@task
def start_gunicorn():
"""
Starts the gunicorn process
"""
supervisorctl('start', 'gunicorn')
@task
def stop_gunicorn():
"""
Stops the gunicorn process
"""
supervisorctl('stop', 'gunicorn')
@task
def restart_gunicorn():
"""
Restarts the gunicorn process
"""
supervisorctl('restart', 'gunicorn')
@task
def first_deploy():
"""
Sets up and deploys the project for the first time.
"""
# If we're on the local machine, there's no point in cloning
# the project, because it's already been cloned. Otherwise
# the user couldn't run this file
if env.run == run:
# We're doing this to filter out the hosts that have
# already been setup and deployed to
with settings(warn_only=True):
if env.run('test -d %s' % env.project).failed:
return
clone()
make_virtualenv()
update_reqs()
syncdb()
# We don't collect the static files and start supervisor on
# development machines
if env.run == run:
manage_static()
start_supervisord()
@task
def deploy():
"""
Deploys the latest changes
"""
update_code()
update_reqs()
syncdb()
manage_static()
restart_supervisord()
########## END COMMANDS