forked from openfoodfacts/openfoodfacts-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
395 lines (312 loc) · 15 KB
/
Makefile
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/make
ifeq ($(findstring cmd.exe,$(SHELL)),cmd.exe)
$(error "We do not suppport using cmd.exe on Windows, please run in a 'git bash' console")
endif
# use bash everywhere !
SHELL := /bin/bash
# some vars
ENV_FILE ?= .env
NAME = "ProductOpener"
MOUNT_POINT ?= /mnt
DOCKER_LOCAL_DATA ?= /srv/off/docker_data
OS := $(shell uname)
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
UID ?= $(shell id -u)
export USER_UID:=${UID}
ifeq ($(OS), Darwin)
export CPU_COUNT=$(shell sysctl -n hw.logicalcpu || echo 1)
else
export CPU_COUNT=$(shell nproc || echo 1)
endif
export MSYS_NO_PATHCONV=1
# load env variables
# also takes into account envrc (direnv file)
ifneq (,$(wildcard ./${ENV_FILE}))
-include ${ENV_FILE}
-include .envrc
export
endif
HOSTS=127.0.0.1 world.productopener.localhost fr.productopener.localhost static.productopener.localhost ssl-api.productopener.localhost fr-en.productopener.localhost
# commands aliases
DOCKER_COMPOSE=docker-compose --env-file=${ENV_FILE}
# we run tests in a specific project name to be separated from dev instances
# we also publish mongodb on a separate port to avoid conflicts
# we also enable the possibility to fake services in po_test_runner
DOCKER_COMPOSE_TEST=ROBOTOFF_URL="http://backend:8881/" GOOGLE_CLOUD_VISION_API_URL="http://backend:8881/" COMPOSE_PROJECT_NAME=${COMPOSE_PROJECT_NAME}_test PO_COMMON_PREFIX=test_ MONGO_EXPOSE_PORT=27027 docker-compose --env-file=${ENV_FILE}
.DEFAULT_GOAL := dev
# this target is always to build, see https://www.gnu.org/software/make/manual/html_node/Force-Targets.html
_FORCE:
#------#
# Info #
#------#
info:
@echo "${NAME} version: ${VERSION}"
hello:
@echo "🥫 Welcome to the Open Food Facts dev environment setup!"
@echo "🥫 Note that the first installation might take a while to run, depending on your machine specs."
@echo "🥫 Typical installation time on 8GB RAM, 4-core CPU, and decent network bandwith is about 10 min."
@echo "🥫 Thanks for contributing to Open Food Facts!"
@echo ""
goodbye:
@echo "🥫 Cleaning up dev environment (remove containers, remove local folder binds, prune Docker system) …"
#-------#
# Local #
#-------#
dev: hello build init_backend _up import_sample_data create_mongodb_indexes refresh_product_tags
@echo "🥫 You should be able to access your local install of Open Food Facts at http://world.openfoodfacts.localhost/"
@echo "🥫 You have around 100 test products. Please run 'make import_prod_data' if you want a full production dump (~2M products)."
edit_etc_hosts:
@grep -qxF -- "${HOSTS}" /etc/hosts || echo "${HOSTS}" >> /etc/hosts
# TODO: Figure out events => actions and implement live reload
# live_reload:
# @echo "🥫 Installing when-changed …"
# pip3 install when-changed
# @echo "🥫 Watching directories for change …"
# when-changed -r lib/
# when-changed -r . -lib/ -html/ -logs/ -c "make restart_apache"
# when-changed . -x lib/ -x html/ -c "make restart_apache"
# when-changed -r docker/ docker-compose.yml .env -c "make restart" # restart backend container on compose changes
# when-changed -r lib/ -c "make restart_apache" # restart Apache on code changes
# when-changed -r html/ -r css/ -r scss/ -r icons/ -r Dockerfile Dockerfile.frontend package.json -c "make up" # rebuild containers
#----------------#
# Docker Compose #
#----------------#
build:
@echo "🥫 Building containers …"
${DOCKER_COMPOSE} build 2>&1
# this is needed for CI
build_backend:
@echo "🥫 Building backend container …"
${DOCKER_COMPOSE} build backend 2>&1
_up:
@echo "🥫 Starting containers …"
${DOCKER_COMPOSE} up -d 2>&1
@echo "🥫 started service at http://openfoodfacts.localhost"
up: build _up
down:
@echo "🥫 Bringing down containers …"
${DOCKER_COMPOSE} down
hdown:
@echo "🥫 Bringing down containers and associated volumes …"
${DOCKER_COMPOSE} down -v
reset: hdown up
restart:
@echo "🥫 Restarting frontend & backend containers …"
${DOCKER_COMPOSE} restart backend frontend
@echo "🥫 started service at http://openfoodfacts.localhost"
restart_db:
@echo "🥫 Restarting MongoDB database …"
${DOCKER_COMPOSE} restart mongodb
status:
@echo "🥫 Getting container status …"
${DOCKER_COMPOSE} ps
livecheck:
@echo "🥫 Running livecheck …"
docker/docker-livecheck.sh
log:
@echo "🥫 Reading logs (docker-compose) …"
${DOCKER_COMPOSE} logs -f
tail:
@echo "🥫 Reading logs (Apache2, Nginx) …"
tail -f logs/**/*
cover:
@echo "🥫 running …"
${DOCKER_COMPOSE_TEST} up -d memcached postgres mongodb
${DOCKER_COMPOSE_TEST} run --rm backend perl -I/opt/product-opener/lib -I/opt/perl/local/lib/perl5 /opt/product-opener/scripts/build_lang.pl
${DOCKER_COMPOSE_TEST} run --rm -e HARNESS_PERL_SWITCHES="-MDevel::Cover" backend prove -l tests/unit
${DOCKER_COMPOSE_TEST} stop
codecov:
@echo "🥫 running …"
${DOCKER_COMPOSE_TEST} run --rm backend cover -report codecovbash
coverage_txt:
${DOCKER_COMPOSE_TEST} run --rm backend cover
#----------#
# Services #
#----------#
build_lang:
@echo "🥫 Rebuild language"
# Run build_lang.pl
# Languages may build taxonomies on-the-fly so include GITHUB_TOKEN so results can be cached
${DOCKER_COMPOSE} run --rm -e GITHUB_TOKEN=${GITHUB_TOKEN} backend perl -I/opt/product-opener/lib -I/opt/perl/local/lib/perl5 /opt/product-opener/scripts/build_lang.pl
build_lang_test:
# Run build_lang.pl in test env
${DOCKER_COMPOSE_TEST} run --rm -e GITHUB_TOKEN=${GITHUB_TOKEN} backend perl -I/opt/product-opener/lib -I/opt/perl/local/lib/perl5 /opt/product-opener/scripts/build_lang.pl
# use this in dev if you messed up with permissions or user uid/gid
reset_owner:
@echo "🥫 reset owner"
${DOCKER_COMPOSE_TEST} run --rm --no-deps --user root backend chown www-data:www-data -R /opt/product-opener/ /mnt/podata /var/log/apache2 /var/log/httpd || true
${DOCKER_COMPOSE_TEST} run --rm --no-deps --user root frontend chown www-data:www-data -R /opt/product-opener/html/images/icons/dist /opt/product-opener/html/js/dist /opt/product-opener/html/css/dist
init_backend: build_lang build_taxonomies
create_mongodb_indexes:
@echo "🥫 Creating MongoDB indexes …"
docker cp conf/mongodb/create_indexes.js $(shell docker-compose ps -q mongodb):/data/db
${DOCKER_COMPOSE} exec -T mongodb //bin/sh -c "mongo off /data/db/create_indexes.js"
refresh_product_tags:
@echo "🥫 Refreshing products tags (update MongoDB products_tags collection) …"
# get id for mongodb container
docker cp scripts/refresh_products_tags.js $(shell docker-compose ps -q mongodb):/data/db
${DOCKER_COMPOSE} exec -T mongodb //bin/sh -c "mongo off /data/db/refresh_products_tags.js"
import_sample_data:
@echo "🥫 Importing sample data (~200 products) into MongoDB …"
${DOCKER_COMPOSE} run --rm backend bash /opt/product-opener/scripts/import_sample_data.sh
import_more_sample_data:
@echo "🥫 Importing sample data (~2000 products) into MongoDB …"
${DOCKER_COMPOSE} run --rm backend bash /opt/product-opener/scripts/import_more_sample_data.sh
# this command is used to import data on the mongodb used on staging environment
import_prod_data:
@echo "🥫 Importing production data (~2M products) into MongoDB …"
@echo "🥫 This might take up to 10 mn, so feel free to grab a coffee!"
@echo "🥫 Removing old archive in case you have one"
( rm -f ./html/data/openfoodfacts-mongodbdump.gz || true ) && ( rm -f ./html/data/gz-sha256sum || true )
@echo "🥫 Downloading full MongoDB dump from production …"
wget --no-verbose https://static.openfoodfacts.org/data/openfoodfacts-mongodbdump.gz -P ./html/data
wget --no-verbose https://static.openfoodfacts.org/data/gz-sha256sum -P ./html/data
cd ./html/data && sha256sum --check gz-sha256sum
@echo "🥫 Restoring the MongoDB dump …"
${DOCKER_COMPOSE} exec -T mongodb //bin/sh -c "cd /data/db && mongorestore --quiet --drop --gzip --archive=/import/openfoodfacts-mongodbdump.gz"
rm html/data/openfoodfacts-mongodbdump.tar.gz && rm html/data/gz-sha256sum
#--------#
# Checks #
#--------#
front_npm_update:
COMPOSE_PATH_SEPARATOR=";" COMPOSE_FILE="docker-compose.yml;docker/dev.yml;docker/jslint.yml" docker-compose run --rm dynamicfront npm update
front_lint:
COMPOSE_PATH_SEPARATOR=";" COMPOSE_FILE="docker-compose.yml;docker/dev.yml;docker/jslint.yml" docker-compose run --rm dynamicfront npm run lint
front_build:
COMPOSE_PATH_SEPARATOR=";" COMPOSE_FILE="docker-compose.yml;docker/dev.yml;docker/jslint.yml" docker-compose run --rm dynamicfront npm run build
checks: front_build front_lint check_perltidy check_perl_fast check_critic
lint: lint_perltidy
tests: build_lang_test unit_test integration_test
unit_test:
@echo "🥫 Running unit tests …"
${DOCKER_COMPOSE_TEST} up -d memcached postgres mongodb
${DOCKER_COMPOSE_TEST} run -T --rm backend prove -l --jobs ${CPU_COUNT} -r tests/unit
${DOCKER_COMPOSE_TEST} stop
@echo "🥫 unit tests success"
integration_test:
@echo "🥫 Running unit tests …"
# we launch the server and run tests within same container
# we also need dynamicfront for some assets to exists
# this is the place where variables are important
${DOCKER_COMPOSE_TEST} up -d memcached postgres mongodb backend dynamicfront incron
# note: we need the -T option for ci (non tty environment)
${DOCKER_COMPOSE_TEST} exec -T backend prove -l -r tests/integration
${DOCKER_COMPOSE_TEST} stop
@echo "🥫 integration tests success"
# stop all tests dockers
test-stop:
@echo "🥫 Stopping test dockers"
${DOCKER_COMPOSE_TEST} stop
# usage: make test-unit test=test-name.t
# you can add args= to pass options, like args="-d" to debug
test-unit: guard-test
@echo "🥫 Running test: 'tests/unit/${test}' …"
${DOCKER_COMPOSE_TEST} up -d memcached postgres mongodb
${DOCKER_COMPOSE_TEST} run --rm backend perl ${args} tests/unit/${test}
# usage: make test-int test=test-name.t
test-int: guard-test # usage: make test-one test=test-file.t
@echo "🥫 Running test: 'tests/integration/${test}' …"
${DOCKER_COMPOSE_TEST} up -d memcached postgres mongodb backend dynamicfront incron
${DOCKER_COMPOSE_TEST} exec backend perl tests/integration/${test}
# better shutdown, for if we do a modification of the code, we need a restart
${DOCKER_COMPOSE_TEST} stop backend
# stop all docker tests containers
stop_tests:
${DOCKER_COMPOSE_TEST} stop
# clean tests, remove containers and volume (useful if you changed env variables, etc.)
clean_tests:
${DOCKER_COMPOSE_TEST} down -v --remove-orphans
update_tests_results:
@echo "🥫 Updated expected test results with actuals for easy Git diff"
${DOCKER_COMPOSE_TEST} up -d memcached postgres mongodb backend dynamicfront incron
${DOCKER_COMPOSE_TEST} exec -T -w /opt/product-opener/tests backend bash update_tests_results.sh
${DOCKER_COMPOSE_TEST} stop
bash:
@echo "🥫 Open a bash shell in the test container"
${DOCKER_COMPOSE_TEST} run --rm -w /opt/product-opener backend bash
# check perl compiles, (pattern rule) / but only for newer files
%.pm %.pl: _FORCE
if [ -f $@ ]; then perl -c -CS -Ilib $@; else true; fi
# TO_CHECK look at changed files (compared to main) with extensions .pl, .pm, .t
# the ls at the end is to avoid removed files.
# We have to finally filter out "." as this will the output if we have no file
TO_CHECK=$(shell git diff origin/main --name-only | grep '.*\.\(pl\|pm\|t\)$$' | xargs ls -d 2>/dev/null | grep -v "^.$$" )
check_perl_fast:
@echo "🥫 Checking ${TO_CHECK}"
test -z "${TO_CHECK}" || ${DOCKER_COMPOSE} run --rm backend make -j ${CPU_COUNT} ${TO_CHECK}
check_translations:
@echo "🥫 Checking translations"
${DOCKER_COMPOSE} run --rm backend scripts/check-translations.sh
# check all perl files compile (takes time, but needed to check a function rename did not break another module !)
check_perl:
@echo "🥫 Checking all perl files"
${DOCKER_COMPOSE_TEST} up -d memcached postgres mongodb
${DOCKER_COMPOSE_TEST} run --rm --no-deps backend make -j ${CPU_COUNT} cgi/*.pl scripts/*.pl lib/*.pl lib/ProductOpener/*.pm
${DOCKER_COMPOSE_TEST} stop
# check with perltidy
# we exclude files that are in .perltidy_excludes
TO_TIDY_CHECK = $(shell echo ${TO_CHECK}| tr " " "\n" | grep -vFf .perltidy_excludes)
check_perltidy:
@echo "🥫 Checking with perltidy ${TO_TIDY_CHECK}"
test -z "${TO_TIDY_CHECK}" || ${DOCKER_COMPOSE} run --rm --no-deps backend perltidy --assert-tidy -opath=/tmp/ --standard-error-output ${TO_TIDY_CHECK}
# same as check_perltidy, but this time applying changes
lint_perltidy:
@echo "🥫 Linting with perltidy ${TO_TIDY_CHECK}"
test -z "${TO_TIDY_CHECK}" || ${DOCKER_COMPOSE} run --rm --no-deps backend perltidy --standard-error-output -b -bext=/ ${TO_TIDY_CHECK}
#Checking with Perl::Critic
# adding an echo of search.pl in case no files are edited
check_critic:
@echo "🥫 Checking with perlcritic"
test -z "${TO_CHECK}" || ${DOCKER_COMPOSE} run --rm --no-deps backend perlcritic ${TO_CHECK}
#-------------#
# Compilation #
#-------------#
build_taxonomies: build_lang # build_lang generates the nutrient_level taxonomy source file
@echo "🥫 build taxonomies"
# GITHUB_TOKEN might be empty, but if it's a valid token it enables pushing taxonomies to build cache repository
${DOCKER_COMPOSE} run --no-deps --rm -e GITHUB_TOKEN=${GITHUB_TOKEN} backend /opt/product-opener/scripts/build_tags_taxonomy.pl ${name}
rebuild_taxonomies: build_taxonomies
#------------#
# Production #
#------------#
create_external_volumes:
@echo "🥫 Creating external volumes (production only) …"
# zfs replications
docker volume create --driver=local -o type=none -o o=bind -o device=${MOUNT_POINT}/data html_data
docker volume create --driver=local -o type=none -o o=bind -o device=${MOUNT_POINT}/users users
docker volume create --driver=local -o type=none -o o=bind -o device=${MOUNT_POINT}/products products
docker volume create --driver=local -o type=none -o o=bind -o device=${MOUNT_POINT}/product_images product_images
docker volume create --driver=local -o type=none -o o=bind -o device=${MOUNT_POINT}/orgs orgs
# local data
docker volume create --driver=local -o type=none -o o=bind -o device=${DOCKER_LOCAL_DATA}/podata podata
create_external_networks:
@echo "🥫 Creating external networks (production only) …"
docker network create --driver=bridge --subnet="172.30.0.0/16" ${COMPOSE_PROJECT_NAME}_webnet \
|| echo "network already exists"
#---------#
# Cleanup #
#---------#
prune:
@echo "🥫 Pruning unused Docker artifacts (save space) …"
docker system prune -af
prune_cache:
@echo "🥫 Pruning Docker builder cache …"
docker builder prune -f
clean_folders: clean_logs
( rm html/images/products || true )
( rm -rf node_modules/ || true )
( rm -rf html/data/i18n/ || true )
( rm -rf html/{css,js}/dist/ || true )
( rm -rf tmp/ || true )
clean_logs:
( rm -f logs/* logs/apache2/* logs/nginx/* || true )
clean: goodbye hdown prune prune_cache clean_folders
#-----------#
# Utilities #
#-----------#
guard-%: # guard clause for targets that require an environment variable (usually used as an argument)
@ if [ "${${*}}" = "" ]; then \
echo "Environment variable '$*' is not set"; \
exit 1; \
fi;