forked from openfoodfacts/openfoodfacts-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
324 lines (255 loc) · 12.1 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
#!/usr/bin/make
NAME = "ProductOpener"
ENV_FILE ?= .env
MOUNT_POINT ?= /mnt
DOCKER_LOCAL_DATA ?= /srv/off/docker_data
HOSTS=127.0.0.1 world.productopener.localhost fr.productopener.localhost static.productopener.localhost ssl-api.productopener.localhost fr-en.productopener.localhost
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
UID ?= $(shell id -u)
export USER_UID:=${UID}
export CPU_COUNT=$(shell nproc || echo 1)
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
DOCKER_COMPOSE_TEST=COMPOSE_PROJECT_NAME=po_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/**/*
#----------#
# Services #
#----------#
build_lang:
@echo "🥫 Rebuild language"
# Run build_lang.pl
${DOCKER_COMPOSE} run --rm 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 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} 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} 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
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
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 openfoodfacts-mongodbdump.tar.gz || true )
@echo "🥫 Downloading full MongoDB dump from production …"
wget --no-verbose https://static.openfoodfacts.org/data/openfoodfacts-mongodbdump.tar.gz
@echo "🥫 Copying the dump to MongoDB container …"
docker cp openfoodfacts-mongodbdump.tar.gz $(shell docker-compose ps -q mongodb):/data/db
@echo "🥫 Restoring the MongoDB dump …"
${DOCKER_COMPOSE} exec -T mongodb //bin/sh -c "cd /data/db && tar -xzvf openfoodfacts-mongodbdump.tar.gz && mongorestore --batchSize=1 && rm openfoodfacts-mongodbdump.tar.gz"
rm openfoodfacts-mongodbdump.tar.gz
#--------#
# Checks #
#--------#
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 --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
${DOCKER_COMPOSE_TEST} up -d memcached postgres mongodb backend dynamicfront
# 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"
# usage: make test-unit test=test-name.t
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 tests/unit/${test}
# usage: make test-int test=test-name.t
test-int: guard-test # usage: make test-one test=t/test-file.t
@echo "🥫 Running test: 'tests/integration/${test}' …"
${DOCKER_COMPOSE_TEST} up -d memcached postgres mongodb backend dynamicfront
${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
# 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:
@echo "🥫 build taxonomies on ${CPU_COUNT} procs"
${DOCKER_COMPOSE} run --no-deps --rm backend make -C taxonomies -j ${CPU_COUNT}
rebuild_taxonomies:
@echo "🥫 re-build all taxonomies on ${CPU_COUNT} procs"
${DOCKER_COMPOSE} run --rm backend make -C taxonomies all_taxonomies -j ${CPU_COUNT}
#------------#
# 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
#---------#
# 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:
( 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 )
( 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;