forked from lookit/lookit-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
253 lines (196 loc) · 7.66 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
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
""" invoke tasks for local development.
tasks.py is a set of invoke tasks for automatically installing and configuring all the dependencies necessary
for local development.
Example:
invoke task_name
where task_name can be rabbitmq, celery, or any other dependencies that is represented by a func
Requirement:
invoke library
Attributes:
HOSTNAME (str): This holds the value of the current hostname. Usually 'localhost'.
PLATFORM (str): holds OS details of the working env. ex: 'Linux'
MESSAGE_FAILED (str): holds the message that displays when a package fails to install
MESSAGE_WRONG_PLATFORM (str): holds a message that displays when the OS being used is not supported by the tasks.
MESSAGE_ALREADY_INSTALLED (str): holds a message that displays when the package is already available in the system.
"""
import json
import os
import platform
import shutil
from pathlib import Path
from invoke import run, task
HOSTNAME = "localhost"
PLATFORM = platform.system()
MESSAGE_FAILED = 'failed to install. Please, use "invoke task_name --verbose" to check out the stderr and stdout responses.'
MESSAGE_OK = "successfully installed"
MESSAGE_WRONG_PLATFORM = "Unsupported Platform. Only Ubuntu (16.04+), Debian (9+), Mac OS. Your system is {}".format(
platform.platform()
)
MESSAGE_ALREADY_INSTALLED = "already installed"
@task
def dotenv(_):
"""Dotenv invoke task.
Copies env_dist to .env if .env doesn't exist.
Args:
c (obj): Context-aware API wrapper & state-passing object.
Returns:
None.
"""
if not Path(".env").exists():
shutil.copy("env_dist", ".env")
@task
def rabbitmq(c):
"""Rabbitmq invoke task.
This func creates users and queues for the API.
Args:
c (obj): Context-aware API wrapper & state-passing object.
"""
# Get list of users and check if we've created our 'lookit-admin' yet.
users = c.run("rabbitmqctl list_users --formatter json", hide="stdout").stdout
users = json.loads(users)
if not any(u["user"] == "lookit-admin" for u in users):
c.run("rabbitmqctl add_user lookit-admin admin")
c.run("rabbitmqctl set_user_tags lookit-admin administrator", hide="stdout")
c.run("rabbitmqctl set_permissions -p / lookit-admin '.*' '.*' '.*'", hide="stdout")
c.run("rabbitmq-plugins enable rabbitmq_management", hide="stdout")
c.run("rabbitmqadmin declare queue --vhost=/ name=email", hide="stdout")
c.run("rabbitmqadmin declare queue --vhost=/ name=builds", hide="stdout")
c.run("rabbitmqadmin declare queue --vhost=/ name=cleanup", hide="stdout")
@task
def postgresql(c):
"""Postgresql invoke task.
Runs django db migrations.
Args:
c (obj): Context-aware API wrapper & state-passing object.
"""
c.run("python manage.py migrate", hide="stdout")
@task
def ssl_certificate(c, verbose=False):
"""Ssl-certificate invoke task.
this func sets up local https development env.
Args:
c (obj): Context-aware API wrapper & state-passing object.
verbose (bool): states whether stdout should be printed.
Returns:
None.
However, this func echoes MESSAGE_FAILED, MESSAGE_OK, or MESSAGE_ALREADY_INSTALLED
depending on the state of the installation process.
Note:
For debugging purposes, set verbose (bool) to True to print the stdout responses for the run process.
Usage:
invoke ssl-certificate or invoke ssl-certificate --verbose
"""
if PLATFORM == "Linux":
if run("command -v mkcert", warn=True, hide=not verbose).ok:
run('echo "===> mkcert {}"'.format(MESSAGE_ALREADY_INSTALLED))
else:
if run("brew install mkcert", warn=True, hide=not verbose).ok:
run(
"apt-get update && apt install libnss3-tools",
warn=True,
hide=not verbose,
)
run("mkcert -install", warn=True, hide=not verbose)
run('echo "===> mkcert {}"'.format(MESSAGE_OK))
else:
run('echo "===> mkcert {}"'.format(MESSAGE_FAILED))
run("mkdir certs", warn=True, hide=not verbose)
if run(
"cd certs && mkcert local_lookit.mit.edu", warn=True, hide=not verbose
).ok:
run(
'echo "certificates successfully created at {}/certs"'.format(
Path.cwd()
)
)
else:
run('echo "certificates {}"'.format(MESSAGE_FAILED))
elif PLATFORM == "Darwin":
if run("command -v mkcert", warn=True, hide=not verbose).ok:
run('echo "===> mkcert {}"'.format(MESSAGE_ALREADY_INSTALLED))
else:
if run("brew install mkcert", warn=True, hide=not verbose).ok:
run("mkcert -install", warn=True, hide=not verbose)
run('echo "===> mkcert {}"'.format(MESSAGE_OK))
else:
run('echo "===> mkcert {}"'.format(MESSAGE_FAILED))
run("mkdir certs", warn=True, hide=not verbose)
if run(
"cd certs && mkcert local_lookit.mit.edu", warn=True, hide=not verbose
).ok:
run(
'echo "=====> certificates successfully created at {}/certs"'.format(
Path.cwd()
)
)
else:
run('echo "=====> certificates {}"'.format(MESSAGE_FAILED))
else:
run("echo {}".format(MESSAGE_WRONG_PLATFORM))
@task
def server(c, https=False):
"""Serving invoke task.
This func serves django application server.
Args:
c (obj): Context-aware API wrapper & state-passing object.
https (bool, optional): Use the ssl version of the local development server. Defaults to False.
"""
certs_path = Path.cwd() / "certs"
key = certs_path / "local_lookit.mit.edu-key.pem"
certificate = certs_path / "local_lookit.mit.edu.pem"
if https and os.listdir(certs_path):
c.run(
f"echo -e '\a Serving at https://{HOSTNAME}:8000\a'", hide=False,
)
c.run(
f"python manage.py runsslserver --certificate {certificate} --key {key}",
hide=False,
)
else:
c.run(f"echo -e '\a Serving at http://{HOSTNAME}:8000\a'", hide=False)
c.run("python manage.py runserver", hide=False)
@task
def ngrok_service(c):
"""Ngrok-service invoke task.
This func serves ngrok.
Args:
c (obj): Context-aware API wrapper & state-passing object.
"""
c.run(f"ngrok http https://{HOSTNAME}:8000")
@task
def celery_service(c):
"""Celery-service invoke task
This func serves celery.
Args:
c (obj): Context-aware API wrapper & state-passing object.
"""
c.run("celery worker --app=project --loglevel=INFO -Q builds,email,cleanup")
@task
def pre_commit_hooks(c):
"""Add pre-commit git hooks
Args:
c (Context): Context-aware API wrapper & state-passing object.
"""
c.run("poetry run pre-commit install --install-hooks")
@task(dotenv, postgresql, rabbitmq, ssl_certificate, pre_commit_hooks)
def setup(_):
"""Setup invoke task.
This func runs the tasks specified in the task decorator.
Args:
c (obj): Context-aware API wrapper & state-passing object.
"""
@task
def coverage(c):
"""Generate test coverage data.
Args:
c (Context): Context-aware API wrapper & state-passing object.
"""
c.run("poetry run coverage run --source='.' manage.py test")
coverage_report(c)
@task
def coverage_report(c):
"""View basic CLI test coverage report.
Args:
c (Context): Context-aware API wrapper & state-passing object.
"""
c.run("poetry run coverage report -i")