-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
437 lines (342 loc) · 11.9 KB
/
tasks.py
File metadata and controls
437 lines (342 loc) · 11.9 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
from __future__ import division, absolute_import
from __future__ import print_function, unicode_literals
from datetime import datetime
from invoke import task
from os import symlink, remove
from os.path import expanduser, join, dirname, lexists
class BaseConfig():
def __init__(self, context, *args, **kwargs):
self.context = context
def get_key(self):
return getattr(self.context, 'original_host', 'localhost')
@property
def mapper(self):
return self.context.mapper.get(self.get_key(), {})
@property
def lmapper(self):
'''mapper for localhost: special case of above'''
return self.context.mapper.get('localhost', {})
@property
def home(self):
return self.mapper.get('home', '/home/navin')
@property
def lhome(self):
return self.lmapper.get('home', '/home/navin')
@property
def project(self):
'''Defaults to original_host'''
return self.mapper.get('project', self.get_key())
@property
def lproject(self):
'''Defaults to project'''
return self.lmapper.get('project', self.project)
@property
def venv(self):
'''defaults to project'''
return self.mapper.get('venv', self.project)
@property
def lvenv(self):
'''defaults to lproject'''
return self.lmapper.get('venv', self.lproject)
@property
def managepy_subdir(self):
return getattr(self.context, 'managepy_subdir', '')
@property
def backups_dir(self):
return getattr(self.context, 'backups_dir',
expanduser('~/Backups/websites'))
class DjangoConfig(BaseConfig):
@property
def projdir(self):
'''Directory for git commands'''
raise NotImplementedError
@property
def python(self):
raise NotImplementedError
@property
def managepydir(self):
if self.managepy_subdir:
return join(self.projdir, self.managepy_subdir)
else:
return self.projdir
@property
def proj_backup_dir(self):
return join(self.backups_dir, self.project)
def backup_file(self, relpath):
return join(self.proj_backup_dir, relpath)
def timestamped_backup_file(self, prefix, ext):
timestamp = datetime.now().strftime('%d%b%Y')
return self.backup_file(prefix + timestamp + ext)
@property
def media_backup_dir(self):
return self.backup_file('media')
@property
def dumpdb_relfile(self):
return join('u', self.project + '.sql.gz')
@property
def mediagz_file(self):
return join(self.lhome, 'u', self.project + '-media.tgz')
@property
def mediagz_tsfile(self):
return self.timestamped_backup_file('media', '.tgz')
@property
def project_path(self):
'''Used currently for replacedb
Except for twit6, others have only one element in this list
'''
return list(set([self.projdir, self.managepydir]))
@property
def restart_commands(self):
raise NotImplementedError
class WFConfig(DjangoConfig):
@property
def projdir(self):
return join(self.home, 'webapps', self.project, 'myproject')
@property
def python(self):
return join(self.home, '.v', self.venv, 'bin', 'python')
@property
def restart_commands(self):
return [join(self.home, 'webapps', self.project,
'apache2', 'bin', 'restart')]
class OpalConfig(DjangoConfig):
@property
def projdir(self):
return join(self.home, 'apps', self.project, 'myproject')
@property
def python(self):
return join(self.home, 'apps', self.project, 'env', 'bin', 'python')
@property
def restart_commands_alt(self):
return [join(self.home, 'apps', self.project, 'stop'),
join(self.home, 'apps', self.project, 'start')]
@property
def restart_commands(self):
return [f'touch {join(self.projdir, "wsgi.py")}']
class LocalConfig(DjangoConfig):
@property
def projdir(self):
return join(self.lhome, self.lproject)
@property
def python(self):
return join(self.lhome, '.v', self.lvenv, 'bin', 'python')
def lrun(self, cmd, *args, **kwargs):
try:
return self.context.local(cmd, *args, **kwargs)
except AttributeError:
if getattr(self.context, 'host', 'localhost') != 'localhost':
raise Exception('This is a local-only command')
return self.context.run(cmd, *args, **kwargs)
def autoconfig(c):
c.lconfig = LocalConfig(c)
hoststr = getattr(c, 'host', 'localhost')
if 'webfaction' in hoststr:
c.rconfig = WFConfig(c)
elif 'opalstack' in hoststr:
c.rconfig = OpalConfig(c)
elif 'localhost' in hoststr:
c.rconfig = c.lconfig
else:
raise Exception('Unknown host: {}'.format(c.host))
@task
def test(c, dir=None):
autoconfig(c)
print('projdir is', c.rconfig.projdir)
print('hostname is ', end='')
c.run('hostname')
print('pwd is ', end='')
c.run('pwd')
print('home is', c.rconfig.home)
print('project is', c.rconfig.project)
@task
def restart(c):
autoconfig(c)
for cmd in c.rconfig.restart_commands:
c.run(cmd, echo=True)
@task
def managepy(c, command, local=False):
'''Run managepy. Remote by default, but locally if local=True'''
autoconfig(c)
cfg = c.lconfig if local else c.rconfig
runner = cfg.lrun if local else c.run
with c.cd(cfg.managepydir):
result = runner("{python} manage.py {command}".format(
python=cfg.python, command=command), echo=True)
return result.stdout
@task
def dumpdb(c, dest_file):
autoconfig(c)
managepy(c, 'dumpdb --output={}'.format(dest_file))
@task
def dumpmedia(c, dest_file=None, tarfile=False):
autoconfig(c)
media_rdir = managepy(c, 'mediadir').strip()
rsync_src = '{host}:{media_rdir}'.format(host=c.host, media_rdir=media_rdir)
rsync_dest = c.rconfig.media_backup_dir
mediagz_tsfile = c.rconfig.mediagz_tsfile
c.local("rsync -avz -e ssh {rsync_src} {rsync_dest}".format(
rsync_src=rsync_src,
rsync_dest=rsync_dest), echo=True)
site_media_symlink = join(c.lconfig.managepydir, 'site_media')
if lexists(site_media_symlink):
remove(site_media_symlink)
symlink('{rsync_dest}/site_media'.format(rsync_dest=rsync_dest),
site_media_symlink)
if tarfile:
# tar.gz the media for backup purposes
# do this in the background because it takes a long time
# We used to do this everyday, but removed it because
# it would take a long time
c.local("tar -czf {mediagz_tsfile} --directory {rsync_dest} .".format(
mediagz_tsfile=mediagz_tsfile,
rsync_dest=rsync_dest), disown=True, echo=True)
local_mediagz = c.rconfig.mediagz_file
if lexists(local_mediagz):
remove(local_mediagz)
symlink(mediagz_tsfile, local_mediagz)
@task
def getdbonly(c):
autoconfig(c)
dumpdb_relfile = c.rconfig.dumpdb_relfile
rdumpdb_file = join(c.rconfig.home, dumpdb_relfile)
dumpdb(c, rdumpdb_file)
ldumpdb_tsfile = c.rconfig.timestamped_backup_file('db', '.sql.gz')
# soft link appropriately
ldumpdb_file = join(c.rconfig.lhome, dumpdb_relfile)
if lexists(ldumpdb_file):
remove(ldumpdb_file)
print('Getting {}'.format(dumpdb_relfile))
c.get(rdumpdb_file, ldumpdb_tsfile)
symlink(ldumpdb_tsfile, ldumpdb_file)
return ldumpdb_file
@task
def runcmd(c, script, args=''):
'''
Call managepy::runcmd with args as a comma-separated arg list
remember: managepy::runcmd runs a standalone script with django initialized
This is not to run a managepy django command
managepy::runcmd expects arguments of the form a0 a1 kw1=kwarg1 etc
This runcmd takes same arguments but comma separated
fab -H rsh runcmd scripts.needs_attention a1,a2,kw1=kwarg1,kw2=kwarg2
'''
autoconfig(c)
managepy(c, command='runcmd {} {}'.format(script,
' '.join(args.split(','))))
@task
def gitpull(c):
autoconfig(c)
with c.cd(c.rconfig.projdir):
c.run('git pull')
@task
def collectstatic(c):
autoconfig(c)
managepy(c, 'collectstatic --noinput')
@task
def upgrade_no_restart(c):
autoconfig(c)
with c.cd(c.rconfig.projdir):
c.run('git pull')
c.run('git submodule update')
managepy(c, 'migrate -v 0')
managepy(c, 'collectstatic --noinput')
@task
def upgrade(c):
autoconfig(c)
upgrade_no_restart(c)
restart(c)
@task
def getdb(c, nomigs=False):
# getdbonly will do autoconfig
dbfile = getdbonly(c)
dumpmedia(c)
replacedb(c, dbfile, nomigs=nomigs)
def forcelocal(c):
autoconfig(c)
if 'localhost' not in c.host:
raise Exception('This is a local-only task')
@task
def tags(c):
'''Re-build tags table for emacs'''
forcelocal(c)
with c.cd(c.lconfig.projdir):
c.run('find . -path "*migrations" -prune '
r'-o -name \*.html -print '
r'-o -name \*.py -print '
r'-o -name \*.js -print '
r'-o -name \*.sass -print '
'| etags -')
c.run('find . -path ./autoevals -prune -path "*migrations" -prune '
r'-o -name \*.html -print '
r'-o -name \*.py -print '
r'-o -name \*.js -print '
r'-o -name \*.sass -print '
'| etags -o TAGS_NOEVALS -')
c.run('find . -path "*migrations" -prune '
r'-o -name \*.py -print '
r'-o -name \*.sass -print '
'| etags -o TAGS_NOHTMLNOJS -')
c.run('find . -path ./autoevals -prune -path "*migrations" -prune '
r'-o -name \*.js -print '
r'-o -name \*.sass -print '
r'-o -name \*.html -print '
'| etags -o TAGS_ONLYJSHTML -')
c.run('find . -path ./autoevals -prune -path "*migrations" -prune '
r'-o -name \*.py -print '
r'-o -name \*.sass -print '
'| etags -o TAGS_ONLYPY -')
c.run('find . -path ./autoevals -prune -path "*migrations" -prune '
r'-o -name \*.py -print '
r'-o -name \*.html -print '
r'-o -name \*.sass -print '
'| etags -o TAGS_ONLYPYHTML -')
@task
def findmigs(c, appname=''):
'''find migrations'''
forcelocal(c)
managepy(c, 'makemigrations {appname}'.format(appname=appname))
@task
def migrate(c, appname=''):
'''apply migrations'''
forcelocal(c)
managepy(c, 'migrate -v 0 {appname}'.format(appname=appname))
@task
def compass(c, compass_directory='base/static'):
forcelocal(c)
with c.cd(c.lconfig.projdir):
c.run('cd {} && compass compile'.format(compass_directory))
@task
def regevals(c, company=None):
'''register evaluators for company (or all companies if None)'''
forcelocal(c)
company_arg = company or "reliscore"
managepy(c, "register_evaluators -f -c {}".format(company_arg))
@task
def force_copysearch(c):
managepy(c, 'index_solutions')
managepy(c, 'copy_search')
@task
def precompute_attention(c):
managepy(c, 'precompute_attention')
@task
def replacedb(c, dbfile=None, nomigs=False, verbose=False):
'''Replace db
nomigs: don't run migrations
'''
autoconfig(c)
dbfile = dbfile or c.rconfig.project
replacedb_path = join(dirname(__file__), 'replacedb.py')
args = ''
args += ' -p ' + ' '.join(c.lconfig.project_path)
if getattr(c, 'django_settings_module', None):
'''Unused?'''
args += ' -s ' + c.django_settings_module
if nomigs:
args += ' -n'
if verbose:
args += ' -d'
args += ' -v'
args += ' -- ' + dbfile
cmd = '{python} {replacedb} {args}'.format(
python=c.lconfig.python, replacedb=replacedb_path, args=args)
with c.cd(c.lconfig.projdir):
c.lconfig.lrun(cmd, echo=True)