This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
justfile
286 lines (224 loc) · 8.36 KB
/
justfile
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
set dotenv-load := false
IS_PROD := env_var_or_default("PROD", "")
IS_CI := env_var_or_default("CI", "")
DC_USER := env_var_or_default("DC_USER", "opener")
# Show all available recipes
default:
@just --list --unsorted
###########
# Helpers #
###########
# Sleep for given time showing the given message as long as given condition is met
@_loop condition message timeout="5m" time="5":
timeout --foreground {{ timeout }} bash -c 'while [ {{ condition }} ]; do \
echo "{{ message }}" && sleep {{ time }}; \
done'; \
EXIT_CODE=$?; \
if [ $EXIT_CODE -eq 124 ]; then \
echo "Timed out"; \
exit $EXIT_CODE; \
fi
##########
# Docker #
##########
DOCKER_FILE := "-f " + (
if IS_PROD == "true" { "ingestion_server/docker-compose.yml" }
else { "docker-compose.yml" }
)
# Run `docker-compose` configured with the correct files and environment
dc *args:
@{{ if IS_CI != "" { "just env" } else { "true" } }}
docker-compose {{ DOCKER_FILE }} {{ args }}
# Build all (or specified) services
build *args:
just dc build {{ args }}
# Bring all Docker services up
up *flags="":
just dc up -d {{ flags }}
# Take all Docker services down
down flags="":
just dc down {{ flags }}
# Recreate all volumes and containers from scratch
recreate:
@just down -v
@just up "--force-recreate --build"
@just init
# Show logs of all, or named, Docker services
logs services="" args=(if IS_CI != "" { "" } else { "-f" }):
just dc logs {{ args }} {{ services }}
# Attach to the specificed `service`. Enables interacting with the TTY of the running service.
attach service:
docker attach $(docker-compose ps | grep {{ service }} | awk '{print $1}')
EXEC_DEFAULTS := if IS_CI == "" { "" } else { "-T" }
# Execute statement in service containers using Docker Compose
exec +args:
docker-compose exec -u {{ DC_USER }} {{ EXEC_DEFAULTS }} {{ args }}
########
# Init #
########
# Create .env files from templates
env:
cp api/env.template api/.env
cp ingestion_server/env.template ingestion_server/.env
# Ensure all services are up and running
@_api-up:
just up
just wait-for-ing
just wait-for-web
# Load sample data into the Docker Compose services
init: _api-up
./load_sample_data.sh
#######
# Dev #
#######
# Install Python dependencies in Pipenv environments
@install:
just _api-install
just _ing-install
# Setup pre-commit as a Git hook
precommit:
#!/usr/bin/env bash
set -eo pipefail
if [ -z "$SKIP_PRE_COMMIT" ] && [ ! -f ./pre-commit.pyz ]; then
echo "Downloading pre-commit";
curl \
https://api.github.com/repos/pre-commit/pre-commit/releases/latest \
| grep -o 'https://.*\.pyz' -m 1 \
| xargs \
| xargs curl -fsJo pre-commit.pyz -L;
echo "Installing pre-commit";
python3 pre-commit.pyz install -t pre-push -t pre-commit;
else
echo "Skipping pre-commit installation";
fi
# Run pre-commit to lint and reformat all files
lint:
python3 pre-commit.pyz run --all-files
# Make locally trusted certificates
cert:
mkdir -p nginx/certs/
mkcert \
-cert-file nginx/certs/openverse.crt \
-key-file nginx/certs/openverse.key \
dev.openverse.test localhost 127.0.0.1 ::1
#################
# Elasticsearch #
#################
# Check the health of Elasticsearch
@es-health es_host:
-curl -s -o /dev/null -w '%{http_code}' 'http://{{ es_host }}/_cluster/health'
# Wait for Elasticsearch to be healthy
@wait-for-es es_host="localhost:50292":
just _loop \
'"$(just es-health {{ es_host }})" != "200"' \
"Waiting for Elasticsearch to be healthy..."
@check-index index="image":
-curl -sb -H "Accept:application/json" "http://localhost:50292/_cat/indices/{{ index }}" | grep -o "{{ index }}" | wc -l | xargs
# Wait for the media to be indexed in Elasticsearch
@wait-for-index index="image":
just _loop \
'"$(just check-index {{ index }})" != "1"' \
"Waiting for index '{{ index }}' to be ready..."
####################
# Ingestion server #
####################
# Install dependencies for ingestion-server
_ing-install:
cd ingestion_server && pipenv install --dev
# Perform the given action on the given model by invoking the ingestion-server API
_ing-api data port="50281":
curl \
-X POST \
-H 'Content-Type: application/json' \
-d '{{ data }}' \
-w "\n" \
'http://localhost:{{ port }}/task'
# Check the health of the ingestion-server
@ing-health ing_host:
-curl -s -o /dev/null -w '%{http_code}' 'http://{{ ing_host }}/'
# Wait for the ingestion-server to be healthy
@wait-for-ing ing_host="localhost:50281":
just _loop \
'"$(just ing-health {{ ing_host }})" != "200"' \
"Waiting for the ingestion-server to be healthy..."
# Load QA data into QA indices in Elasticsearch
@load-test-data model="image":
just _ing-api '{"model": "{{ model }}", "action": "LOAD_TEST_DATA"}'
# Load sample data into temp table in API and new index in Elasticsearch
@ingest-upstream model="image" suffix="init":
just _ing-api '{"model": "{{ model }}", "action": "INGEST_UPSTREAM", "index_suffix": "{{ suffix }}"}'
# Promote temp table to prod in API and new index to primary in Elasticsearch
@promote model="image" suffix="init" alias="image":
just _ing-api '{"model": "{{ model }}", "action": "PROMOTE", "index_suffix": "{{ suffix }}", "alias": "{{ alias }}"}'
# Delete an index in Elasticsearch
@delete model="image" suffix="init" alias="image":
just _ing-api '{"model": "{{ model }}", "action": "DELETE_INDEX", "index_suffix": "{{ suffix }}"}'
# Run ingestion-server tests locally
ing-testlocal *args:
cd ingestion_server && pipenv run ./test/run_test.sh {{ args }}
#######
# API #
#######
# Install dependencies for API
_api-install:
cd api && pipenv install --dev
# Check the health of the API
@web-health:
-curl -s -o /dev/null -w '%{http_code}' 'http://localhost:50280/healthcheck/'
# Wait for the API to be healthy
@wait-for-web:
just _loop \
'"$(just web-health)" != "200"' \
"Waiting for the API to be healthy..."
# Run smoke test for the API docs
api-doctest: _api-up
curl --fail 'http://localhost:50280/v1/?format=openapi'
# Run API tests inside Docker
api-test *args: _api-up
just exec web ./test/run_test.sh {{ args }}
# Run Django administrative commands locally
dj-local +args:
cd api && pipenv run python manage.py {{ args }}
# Run Django administrative commands in the docker container
@dj +args="": _api-up
just exec web python manage.py {{ args }}
# Make a test cURL request to the API
stats media="images":
curl "http://localhost:50280/v1/{{ media }}/stats/"
# Get Django shell with IPython
ipython:
just dj shell
# Run `collectstatic` to prepare for building the `nginx` Dockerfile target.
@collectstatic:
# The `STATIC_ROOT` setting is relative to the directory in which the Django
# container runs (i.e., the `api` directory at the root of the repository).
# The resulting output will be at `api/static` and is git ignored for convenience.
@STATIC_ROOT="./static" just dj collectstatic --noinput
# Run the nginx image locally
nginx upstream_url='api.openverse.engineering': collectstatic
# upstream_url can also be set to 172.17.0.1:50280 for local testing
cd api && docker build --target nginx . -t openverse-api-nginx:latest
@echo "--> NGINX server will be run at http://localhost:9090, upstream at {{ upstream_url }}"
@echo "--> Try a static URL like http://localhost:9090/static/admin/css/base.css to test"
docker run --rm -p 9090:8080 -it \
-e DJANGO_NGINX_UPSTREAM_URL="{{ upstream_url }}" \
-e DJANGO_NGINX_GIT_REVISION="$(git rev-parse HEAD)" \
openverse-api-nginx:latest
# Launch a psql shell in the web container
@dbshell:
just exec web python manage.py dbshell
# Launch a pgcli shell in the web container (require typing credentials)
@pgcli db_host="db":
just exec web pgcli -h {{ db_host }} openledger deploy
##########
# Sphinx #
##########
# Compile Sphinx documentation into HTML output
sphinx-make: _api-up
just exec web sphinx-build -M html docs/ build/
# Serve Sphinx documentation via a live-reload server
sphinx-live: _api-up
just exec web sphinx-autobuild --host 0.0.0.0 --port 3000 docs/ build/html/
# Serve the Sphinx documentation from the HTML output directory
sphinx-serve dir="api" port="50231":
cd {{ dir }}/build/html && pipenv run python -m http.server {{ port }}