-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
282 lines (200 loc) · 7.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
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
import os
import sys
from fabric.api import local, abort, warn_only
from fabric.context_managers import lcd, hide
from fabric.state import env
from fabric.contrib.console import confirm
from fabric import utils
import time
root_dir = os.path.dirname(os.path.realpath(__file__))
def _supervisor(command, *args, **kwargs):
capture = kwargs.get('capture', False)
with lcd(root_dir):
if args:
args = list(args)
# Allow "worker" as shorthand for "worker:*" (all workers)
if 'worker' in args:
args[args.index('worker')] = 'worker:*'
local('supervisorctl %s %s' % (command, ' '.join(args)), capture=capture)
elif command == 'status':
local('supervisorctl %s' % command, capture=capture)
else:
local('supervisorctl %s all' % command, capture=capture)
def _backup_file(file_path):
with warn_only():
local('cp %s %s~ 2> /dev/null || :' % (file_path, file_path))
def _jinja_render(template_path, values):
"""Render a template. Expects path relative to root_dir."""
from jinja2 import Template, Environment, FileSystemLoader
environment = Environment(loader=FileSystemLoader(root_dir))
template = environment.get_template(template_path)
return template.render(values)
def _read_env():
sys.path.append(root_dir)
from twitter_feels import env_file
return env_file.read()
def manage(*args):
"""Run manage.py with the given arguments"""
with lcd(root_dir):
local('python ./manage.py %s' % (' '.join(args)))
def dev_web():
"""Runs the Django development webserver"""
# Stop the gunicorn process
stop('web')
env = _read_env()
manage('runserver', env.get('PORT', ''))
def rq_dashboard():
"""Starts the RQ Dashboard webserver"""
# Stop the web process
stop('web')
env = _read_env()
with lcd(root_dir):
local('rq-dashboard -p %s -u %s' % (env.get('PORT', '9181'), env.get('REDIS_URL', 'redis://localhost:6379')))
def jasmine():
"""Run the jasmine test server. Stops the web process first to open the port."""
stop('web')
env = _read_env()
with lcd(root_dir):
local('jasmine -p %s' % env.get('PORT', ''))
def stop(*args):
"""Stop one or more supervisor processes"""
if len(args) == 0:
if not confirm("Are you sure you want to stop ALL processes?", False):
abort("Nevermind then.")
_supervisor('stop', *args)
def start(*args):
"""Start one or more supervisor processes"""
_supervisor('start', *args)
def restart(*args):
"""Restart one or more supervisor processes"""
_supervisor('restart', *args)
def status(*args):
"""Get the status of one or more supervisor processes"""
_supervisor('status', *args)
def scale_worker(number):
"""Scale the workers to the given number of processes."""
with lcd(root_dir):
local(
r'sed -i -r -e "/^\[program:%(process)s\]/ { n ; s/^numprocs=[[:digit:]]+/numprocs=%(number)s/ }" supervisord.conf' % {
'process': 'worker',
'number': number
})
_supervisor('update')
def refresh_web():
"""Trigger's Gunicorn's 'hot refresh' feature."""
with lcd(root_dir):
pid = local('supervisorctl pid web', capture=True)
local('kill -HUP %s' % pid)
def count_web():
"""Get the current gunicorn web worker count"""
with lcd(root_dir):
with hide('running'):
count = int(local('ps -C gunicorn --no-headers | wc -l', capture=True))
if count > 0:
count -= 1
if count == 1:
print ("There is %d gunicorn worker running" % count)
else:
print ("There are %d gunicorn workers running" % count)
return count
def scale_web(direction='up'):
"""Scale up or down the gunicorn web workers"""
with lcd(root_dir):
before = count_web()
with hide('running'):
pid = local('supervisorctl pid web', capture=True)
if direction == 'up':
print ("Scaling up gunicorn workers for master pid %s..." % pid)
local('kill -TTIN %s' % pid)
elif direction == 'down':
if before > 1:
print ("Scaling down gunicorn workers for master pid %s..." % pid)
local('kill -TTOU %s' % pid)
else:
print ("There is only one web worker running. Use fab stop:web to kill gunicorn.")
return
else:
raise Exception("Direction argument must be 'up' or 'down'")
time.sleep(1)
count = count_web()
if count == before:
print ("Changes may not be immediately reflected. Check 'fab count_web' in a moment.")
def clear_logs():
"""Empties the log files"""
with lcd(root_dir):
local('echo "" | tee logs/*.log > /dev/null')
def watch(process):
"""Watch the log output from a process using tail -f."""
with lcd(root_dir):
local('tail -f logs/%s.log -s 0.5' % process)
def pip_refresh(file='requirements/dev.txt'):
"""Refresh pip from the requirements file"""
with lcd(root_dir):
local('pip install -r %s' % file)
def dump_key(file='.twitter_api_key.json'):
"""Dump a Twitter API key to a fixture file (defaults to .twitter_api_key.json)"""
manage('dumpdata', 'twitter_stream.ApiKey', '--indent=3', '>', file)
def load_key(file='.twitter_api_key.json'):
"""Load a Twitter API key from a previously-exported fixture (defaults to .twitter_api_key.json)"""
with lcd(root_dir):
# Make sure it is a legit key file
try:
with open(file, 'rb') as jsonfile:
if not 'twitter_stream.apikey' in jsonfile.read():
utils.abort('File %s does not contain an Api Key' % file)
except IOError, e:
utils.abort('Cannot read file %s: %s' % (file, e))
manage('loaddata', file)
print "You may need to restart your twitter stream now"
def dev_data():
"""Loads some fixtures for getting the development environment set up fast."""
manage('loaddata', 'thermometer_devdata')
manage('loaddata', 'vagrant_superuser')
def init_south(app):
"""Set up South migrations on an app"""
manage('syncdb')
manage('convert_to_south', app)
def new_migration(app):
"""Auto-generate a new migration for an app."""
manage('schemamigration', app, '--auto')
def updatedb(*args):
"""Runs syncdb and migrate."""
manage('syncdb', *args)
manage('migrate', *args)
def shell():
"""Open a Django shell"""
manage('shell')
def generate_supervisor_conf(user=None, app=None, **kwargs):
"""
Generates a new supervisord.conf file.
"""
if user is None:
import getpass
user = getpass.getuser()
if app is None:
app = os.path.split(root_dir)[1]
path = os.environ.get('PATH', None)
if not path:
raise Exception("No $PATH set???")
processes = {
'worker': 1,
'web': 1,
'stream': 1,
'scheduler': 1,
}
processes.update(kwargs)
with lcd(root_dir):
virtualenv_bin = os.path.dirname(os.path.realpath(sys.executable))
newconf = _jinja_render('scripts/templates/supervisord.conf', {
'project_dir': root_dir,
'app_name': app,
'virtualenv_bin': virtualenv_bin,
'user_name': user,
'processes': processes,
'path': path
})
# Back up first
_backup_file('supervisord.conf')
with open('supervisord.conf', 'w') as outfile:
outfile.write(newconf)
print ("Saved to supervisord.conf (old file backed up to supervisord.conf~)")