forked from lablivre-unb/cs-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
260 lines (213 loc) · 6.13 KB
/
tasks.py
File metadata and controls
260 lines (213 loc) · 6.13 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
import subprocess
import sys
import threading
from time import sleep
import os
from python_boilerplate.tasks import *
from python_boilerplate.tasks import django
from python_boilerplate.tasks import js
ns.add_collection(js)
ns.add_collection(django)
sys.path += ['src']
#
# Convenience
#
@task
def develop(ctx):
"""
Prepares environment for development.
"""
# Install [dev]
print('Installing Python dev dependencies...')
ctx.run('pip install .[dev] -r requirements.txt')
# Js configurations
js.install()
print()
js.build()
# Run manage.py commands
# django_manage('makemigrations')
# django_manage('migrate')
# createsuperuser(ctx)
@task
def run(ctx, production=False, port='8000'):
"""
Runs the development server.
"""
if not production:
ctx.run('python3 manage.py runserver %s' % port, pty=True)
else:
ctx.run('python3 manage.py makemigrations --no-input')
ctx.run('python3 manage.py migrate --no-input')
ctx.run('python3 manage.py clean_orphan_obj_perms')
ctx.run('python3 manage.py check_permissions')
ctx.run('python3 manage.py clean_expired')
ctx.run('python3 manage.py fixtree')
ctx.run('gunicorn codeschool.wsgi -b unix:///tmp/sock/webapp.sock '
'--reload -w 4')
@task
def db(ctx, run=False, hard_reset=False):
"""
Executes the makemigrations and migrate commands.
"""
ctx.run('python manage.py makemigrations', pty=True)
if hard_reset:
ctx.run('rm -y local/db/db.sqlite3')
ctx.run('python manage.py migrate', pty=True)
if run:
ctx.run('python manage.py runserver', pty=True)
@task
def shell(ctx, run=False):
"""
Executes shell.
"""
ctx.run('ipython -ic "from codeschool.all import *"', pty=True)
#
# Translations
#
@task
def makemessages(ctx):
"""
Runs the manage.py makemessages command with sane defaults.
"""
paths = os.listdir(os.path.dirname(__file__))
paths.remove('src')
globs = [repr('%s/*' % f if os.path.isdir(f) else f) for f in paths]
ignore_patterns = globs * 2
ignore_patterns[::2] = ['-i'] * len(globs)
cmd = ['python', 'manage.py', 'makemessages', '-i', 'codeschool/vendor/*']
cmd.extend(ignore_patterns)
ctx.run(' '.join(cmd), echo=True, pty=True)
@task
def compilemessages(ctx):
"""
Runs the manage.py makemessages command with sane defaults.
"""
paths = os.listdir(os.path.dirname(__file__))
paths.remove('src')
globs = [repr('%s*' % f if os.path.isdir(f) else f) for f in paths]
ignore_patterns = globs * 2
ignore_patterns[::2] = ['-i'] * len(globs)
cmd = ['python', 'manage.py', 'makemessages', '-i', 'codeschool/vendor/*']
cmd.extend(ignore_patterns)
ctx.run(' '.join(cmd), echo=True, pty=True)
#
# Docker
#
@task
def docker_build(ctx, rebuild_static=False):
if rebuild_static:
ctx.run('tar czpf static.tar.gz collect/static/')
ctx.run('docker build -f docker/Dockerfile.production '
'-t cslms/codeschool .', pty=True)
@task
def docker_run(ctx, deploy=False, shell=False):
"""
Run dev:
docker run -it -p 8080:80 -v /app/codeschool/src:/app/src/ -v /app/db:/app/db -v /app/collect/media:/var/www/media -e PYTHONPATH=/app/src/ codeschool:deploy shell
Run production:
docker run -p 80:80 -v /app/codeschool/src:/app/src/ -v /app/db:/app/db -v /app/collect/media:/var/www/media -e PYTHONPATH=/app/src/ codeschool:deploy
"""
cmd = (
'docker run -ti -p {port}:80 '
'-v {src}:/app/src/ '
'-v {db}:/app/db '
'-v {collect}/media:/var/www/media '
'-e PYTHONPATH=/app/src/ '
'codeschool:deploy{tail}'
)
kwargs = {
'src': os.path.abspath('src'),
'db': os.path.abspath('db'),
'collect': os.path.abspath('collect'),
'tail': ' shell' if shell else '',
}
def run(cmd):
print('sh: %s' % cmd)
ctx.run(cmd, pty=True)
if deploy:
run(cmd.format(port=80, **kwargs))
else:
run(cmd.format(port=8080, **kwargs))
#
# Redis
#
def is_redis_running(port=None):
"""
Check if redis is running in the given port.
"""
import redis
conn = redis.Connection(port=port or 6379)
try:
conn.connect()
return True
except redis.ConnectionError:
return False
@task
def redis(ctx, docker=False, port=0, verbose=False, sentinel=False):
"""
Run redis server (possibly using version in docker hub).
"""
if is_redis_running(port or None):
print('Redis is already running at port %s' % (port or 6379))
print('Bye!')
return
if docker:
print('Running dockerized redis-server')
cmd = 'docker run redis:alpine'
else:
result = ctx.run('redis-server -v', hide=True)
if not result.ok:
print('There is no redis-server installed in your system')
print('Please install it using apt-get install redis or any other '
'means your distribution provides.')
raise SystemExit
cmd = 'redis-server'
if port:
cmd += ' --port %s' % port
if verbose:
cmd += ' --loglevel verbose'
if sentinel:
cmd += ' --sentinel'
ctx.run(cmd)
@task
def celery(ctx):
"""
Runs celery.
"""
ctx.run('celery --app=codeschool.celery:app worker --loglevel=INFO')
#
# Frontend
#
@task
def frontend(ctx):
"""
Starts frontend development.
"""
# Prepare sub-processes
sass = subprocess.Popen(
['sass', 'main.scss:main.css', '--watch'],
cwd='frontend/src/scss'
)
sass_thread = threading.Thread(
target=sass.communicate,
args=('',),
daemon=True,
)
# Prepare sub-processes
elm = subprocess.Popen(
['elm-app', 'start'],
cwd='frontend/'
)
elm_thread = threading.Thread(
target=elm.communicate,
args=('',),
daemon=True,
)
try:
sass_thread.start()
elm_thread.start()
while True:
sleep(0.1)
finally:
sass_thread.kill()
elm_thread.kill()