/
fabfile.py
362 lines (255 loc) · 8.25 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
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
# -*- coding: utf-8 -*-
import os
import time
from fabric.api import cd, env, local, run, task, put
from fabric.context_managers import prefix
from fabric.contrib import project
from fabric.contrib.console import prompt
HERE = os.path.abspath(os.path.dirname(__file__))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
# os.environ.setdefault("PRODUCTION_HOSTS", "")
# env = environ.Env()
# environ.Env.read_env()
# PRODUCTION_HOSTS = env('PRODUCTION_HOSTS', None)
# django.settings_module('config.settings.local')
env.hosts = ['root@10.7.7.22']
env.excludes = (
"*.pyc", "*.db", ".DS_Store", ".coverage", ".git", ".hg", ".tox", ".idea/",
'assets/', 'runtime/', 'node_modules', 'db.sqlite3', '*.ipynb')
env.remote_dir = '/home/apps/bankeys'
env.local_dir = '.'
env.database = 'bankeys'
@task
def venv():
with prefix('source /usr/local/bin/virtualenvwrapper.sh'):
run('mkvirtualenv bankeys')
with prefix('workon bankeys'), cd(env.remote_dir):
run('pip install -r requirements_dev.txt')
@task
def conf():
put('bankeys_server.conf', '/etc/supervisor/conf.d/')
put('bankeys_chatbot.conf', '/etc/supervisor/conf.d/')
@task
def cron(action='check'):
with prefix('workon bankeys'), cd(env.remote_dir):
run('python schedule.py %s' % action)
@task
def d2u():
local('find . -name "*.html" -exec dos2unix {} \;')
local('find . -name "*.py" -exec dos2unix {} \;')
local('find . -name "*.css" -exec dos2unix {} \;')
local('find . -name "*.js" -exec dos2unix {} \;')
@task
def cert():
local('mkdir private')
local('mkdir server')
local('mkdir client')
local('openssl genrsa -out private/ca-key.pem 1024')
local('openssl req -new -subj $SUBJECT -out private/ca-req.csr -key private/ca-key.pem')
local('openssl x509 -req -in private/ca-req.csr -out private/ca-cert.pem -signkey private/ca-key.pem -days 3650')
local('openssl pkcs12 -export -clcerts -in private/ca-cert.pem -inkey private/ca-key.pem -out private/ca.p12')
@task
def urls():
local('python manage.py show_urls')
@task
def runs(script=None):
local('python manage.py runscript %s' % script)
@task
def test(task=''):
local('''DJANGO_SETTINGS_MODULE=config.settings.tests python manage.py test %s''' % task)
@task
def static():
with prefix('workon bankeys'), cd(env.remote_dir):
run('python manage.py collectstatic --dry-run -c --noinput')
@task
def rsync(static=None):
local_dir = os.getcwd() + os.sep
return project.rsync_project(remote_dir=env.remote_dir, local_dir=local_dir, exclude=env.excludes, delete=True)
@task
def pull():
with prefix('workon bankeys'), cd(env.remote_dir):
run('git pull')
run('pip install -r requirements.txt')
migrate()
run('/usr/bin/supervisorctl stop bankeys')
run('/usr/bin/supervisorctl start bankeys')
@task
def push(static=None):
rsync(static)
@task
def migrate():
with prefix('workon bankeys'), cd(env.remote_dir):
run('''DJANGO_SETTINGS_MODULE='config.settings.local' python manage.py migrate''')
@task(alias='rr')
def restart():
with prefix('workon bankeys'), cd(env.remote_dir):
run('/usr/bin/supervisorctl restart bankeys')
@task
def stop():
run('/usr/bin/supervisorctl stop bankeys')
@task
def ur(static=None):
rsync(static)
restart()
@task
def um(static=None):
rsync(static)
migrate()
@task
def uu(static=None):
rsync(static)
migrate()
restart()
@task
def reload():
run('/root/python/bin/supervisorctl reload')
run('/root/python/bin/supervisorctl restart all')
@task
def prod(port=8000):
local('''MODE=prod gunicorn --worker-class=gevent config.wsgi:application -b 0.0.0.0:%s &''' % port)
@task
def build():
local('''python -m compileall .''')
@task
def clean(migrate=None):
clean_temp()
clean_static()
migrate and clean_migrate()
@task
def clean_temp(migrate=None):
local('find . -name "*.sql" | xargs rm -rf')
local('find . -name "*.pyc" | xargs rm -rf')
local('find . -name "*.bak" | xargs rm -rf')
local('find . -name "*.log" | xargs rm -rf')
local('find . -name ".DS_Store" | xargs rm -rf')
@task
def clean_static(migrate=None):
local('rm -rf assets/static/*')
@task
def clean_migrate():
local('find . -name "migrations" | xargs rm -rf')
@task
def distclean():
clean_temp()
clean_static()
clean_migrate()
@task
def mm():
local('python manage.py makemigrations')
local('python manage.py migrate')
@task
def pack(time=None):
local('tar zcfv ./release.tgz '
'--exclude=.git '
'--exclude=.tox '
'--exclude=.svn '
'--exclude=.idea '
'--exclude=*.tgz '
'--exclude=*.pyc '
'--exclude=.vagrant '
'--exclude=tests '
'--exclude=storage '
'--exclude=database '
'--exclude=.DS_Store '
'--exclude=.phpintel '
'--exclude=.template '
'--exclude=db.sqlite3 '
'--exclude=Vagrantfile .')
@task
def setup():
local('python manage.py migrate --settings config.settings.prod')
@task
def dumpdata(remote=None):
num = 1
for fixture in env.fixtures:
if not remote:
local('python manage.py dumpdata {} > database/fixtures/00{}_{}.json'.format(fixture, num, fixture))
else:
with prefix('workon bankeys'), cd(env.remote_dir):
run('python manage.py dumpdata {} > database/fixtures/00{}_{}.json'.format(fixture, num, fixture))
num += 1
@task
def loaddata(remote=None):
num = 1
for fixture in env.fixtures:
if not remote:
local('python manage.py loaddata database/fixtures/00{}_{}.json'.format(num, fixture))
else:
with (prefix('workon bankeys'), cd(env.remote_dir)):
run(
'DJANGO_SETTINGS_MODULE=config.settings.prod python manage.py loaddata database/fixtures/00{}_{}.json'.format(
num, fixture))
num += 1
@task
def restdb():
local('dropdb {database}'.format(database=env.database))
local('createdb {database} -O {database} -E UTF8 -e'.format(database=env.database))
local('python manage.py migrate --noinput')
# local('python manage.py loaddata database/fixtures/*.json')
loaddata()
@task
def deploy():
question = '你确实要这么做?'
prompt(question, default=True)
@task
def backdb():
backdir = 'database/backups/%s' % time.strftime('%Y%m%d%H', time.localtime())
if not os.path.isdir(backdir):
os.makedirs(backdir)
for num, fixture in enumerate(env.fixtures):
local('python manage.py dumpdata {} > {}/00{}_{}.json'.format(fixture, backdir, num, fixture))
@task
def syncdb(action='down'):
if action == 'up':
remote = False
upload = True
else:
remote = True
upload = False
if remote:
dumpdata(remote=remote)
project.rsync_project(
remote_dir=env.remote_dir + '/database/fixtures',
local_dir='./database',
exclude=env.excludes,
upload=upload)
# if action == 'up':
# with prefix('workon bankeys'), cd(env.remote_dir):
# run('python manage.py loaddata database/fixtures/*.json')
# else:
# restdb()
@task
def dbmigrate():
# backup data
run('manage.py dumpdata --format=json > db.json')
# rsync files.
local('rsync -ave ssh rsync -ave ssh root@101.200.136.70:/home/apps/bankeys /home/apps')
# stop service
local('/usr/bin/supervisorctl stop bankeys')
# migrate db
local('dropdb {database}'.format(database=env.database))
local('createdb {database} -O {database} -E UTF8 -e'.format(database=env.database))
local('python manage.py migrate --noinput')
local('python manage.py loaddata db.json')
# start service
local('/usr/bin/supervisorctl start bankeys')
@task
def req():
with prefix('workon bankeys'), cd(env.remote_dir):
run('pip install -r requirements/prod.txt')
@task
def check():
local('python manage.py check')
@task
def init():
setup()
loaddata()
@task
def start(name):
local('git flow feature start %s' % name)
@task
def publish(name):
local('git flow feature publish %s' % name)
@task
def finish(name):
local('git flow feature finish %s' % name)