-
Notifications
You must be signed in to change notification settings - Fork 17
/
tasks.py
161 lines (128 loc) · 4.12 KB
/
tasks.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
import os
import sys
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']
@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)
@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 docker_build(ctx, rebuild_static=False):
if rebuild_static:
ctx.run('tar czpf static.tar.gz collect/static/')
ctx.run('docker build -t codeschool:deploy .', 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))
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')