-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
165 lines (127 loc) · 4.33 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
#!/usr/bin/make
# nice way to have our .env in environment for use in makefile
# see https://lithic.tech/blog/2020-05/makefile-dot-env
# Note: this will mask environment variable as opposed to docker-compose priority
# yet most developper shouldn't bump into this
ifneq (,$(wildcard ./.env))
-include .env
-include .envrc
export
endif
NAME = "openfoodfacts_exports"
ENV_FILE ?= .env
DOCKER_COMPOSE=docker compose --env-file=${ENV_FILE}
DOCKER_COMPOSE_TEST=COMPOSE_PROJECT_NAME=openfoodfacts_exports_test docker compose --env-file=${ENV_FILE}
# Use bash shell for variable substitution
SHELL := /bin/bash
.DEFAULT_GOAL := dev
# avoid target corresponding to file names, to depends on them
.PHONY: *
#------#
# Info #
#------#
info:
@echo "${NAME}"
hello:
@echo "🥫 Welcome to the openfoodfacts-exports dev environment setup!"
@echo ""
goodbye:
@echo "🥫 Cleaning up dev environment (remove containers, remove local folder binds, prune Docker system) …"
#-------#
# Local #
#-------#
dev: hello build up
#----------------#
# Docker Compose #
#----------------#
up:
# creates a docker network and runs docker-compose
@echo "🥫 Building and starting containers …"
ifdef service
${DOCKER_COMPOSE} up -d ${service} 2>&1
else
${DOCKER_COMPOSE} up -d 2>&1
endif
# pull images from image repository
pull:
${DOCKER_COMPOSE} pull
build:
${DOCKER_COMPOSE} build scheduler 2>&1
down:
@echo "🥫 Bringing down containers …"
${DOCKER_COMPOSE} down
hdown:
@echo "🥫 Bringing down containers and associated volumes …"
${DOCKER_COMPOSE} down -v
restart:
@echo "🥫 Restarting containers …"
${DOCKER_COMPOSE} restart
status:
@echo "🥫 Getting container status …"
${DOCKER_COMPOSE} ps
log:
@echo "🥫 Reading logs (docker-compose) …"
${DOCKER_COMPOSE} logs -f --tail 100 scheduler workers
#------------#
# Quality #
#------------#
toml-check:
${DOCKER_COMPOSE} run --rm --no-deps api poetry run toml-sort --check poetry.toml pyproject.toml
toml-lint:
${DOCKER_COMPOSE} run --rm --no-deps api poetry run toml-sort --in-place poetry.toml pyproject.toml
mypy:
${DOCKER_COMPOSE} run --rm --no-deps api mypy .
docs:
@echo "🥫 Generationg doc…"
${DOCKER_COMPOSE} run --rm --no-deps api ./build_mkdocs.sh
checks: toml-check flake8 black-check mypy isort-check docs
lint: toml-lint isort black
tests: unit-tests integration-tests
quality: lint checks tests
unit-tests:
@echo "🥫 Running tests …"
# run tests in worker to have more memory
# also, change project name to run in isolation
${DOCKER_COMPOSE_TEST} run --rm scheduler pytest --cov-report xml:.cov/coverage.xml --cov-report html:.cov/html --cov=openfoodfacts_exports tests/unit
integration-tests:
@echo "🥫 Running integration tests …"
${DOCKER_COMPOSE_TEST} run --rm scheduler pytest --cov-report xml:.cov/coverage.xml --cov-report html:.cov/html --cov=openfoodfacts_exports tests/integration ${args}
# interactive testings
# usage: make pytest args='test/unit/my-test.py --pdb'
pytest: guard-args
@echo "🥫 Running test: ${args} …"
${DOCKER_COMPOSE_TEST} run --rm worker_1 poetry run pytest ${args}
#------------#
# Production #
#------------#
# Create all external volumes needed for production. Using external volumes is useful to prevent data loss (as they are not deleted when performing docker down -v)
create_external_volumes:
@echo "🥫 Creating external volumes (production only) …"
docker volume create off-exports_tmp
docker volume create off-exports_cache
docker volume create off-exports_redis-data
docker volume create off-exports_datasets
#---------#
# 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: goodbye hdown prune prune_cache
# clean tests, remove containers and volume (useful if you changed env variables, etc.)
clean_tests:
${DOCKER_COMPOSE_TEST} down -v --remove-orphans
#-----------#
# Utilities #
#-----------#
guard-%: # guard clause for targets that require an environment variable (usually used as an argument)
@ if [ "${${*}}" = "" ]; then \
echo "Environment variable '$*' is mandatory"; \
echo use "make ${MAKECMDGOALS} $*=you-args"; \
exit 1; \
fi;
cli: guard-args
${DOCKER_COMPOSE} run --rm --no-deps scheduler python -m openfoodfacts_exports ${args}