-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Makefile
206 lines (162 loc) · 6.28 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
MOUNT_POINT ?= /mnt
ENV_FILE ?= .env
# for dev we need to align user uid with the one in the container
# this is handled through build args
UID ?= $(shell id -u)
export USER_UID:=${UID}
# prefer to use docker buildkit
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
# we need COMPOSE_PROJECT_NAME for some commands
# take it form env, or from env file
COMPOSE_PROJECT_NAME ?= $(shell grep COMPOSE_PROJECT_NAME ${ENV_FILE} | cut -d '=' -f 2)
# load env variables
# also takes into account envrc (direnv file)
ifneq (,$(wildcard ./${ENV_FILE}))
-include ${ENV_FILE}
-include .envrc
export
endif
DOCKER_COMPOSE=docker compose --env-file=${ENV_FILE}
DOCKER_COMPOSE_TEST=COMPOSE_PROJECT_NAME=search_test docker compose --env-file=${ENV_FILE}
.PHONY: build create_external_volumes livecheck up down test test_front test_front_watch test_api import-dataset import-taxonomies sync-scripts build-translations generate-openapi check check_front check_translations lint lint_back lint_front
#------------#
# Production #
#------------#
create_external_volumes:
@echo "🔎 Creating external volumes (production only) …"
@for vol_name in esdata01 esdata02 es_synonyms; \
do \
vol_name=${COMPOSE_PROJECT_NAME}_$$vol_name; \
echo creating docker volume $$vol_name \
# create volume \
# this bind mount a folder, it will happen when volume will be used \
docker volume create --driver=local "$$vol_name" ; \
done;
livecheck:
@echo "🔎 livecheck services…" ; \
exit_code=0; \
services=`${DOCKER_COMPOSE} config --service | tr '\n' ' '`; \
for service in $$services; do \
if [ -z `docker compose ps -q $$service` ] || [ -z `docker ps -q --no-trunc | grep $$(${DOCKER_COMPOSE} ps -q $$service)` ]; then \
echo "$$service: DOWN"; \
exit_code=1; \
else \
echo "$$service: UP"; \
fi \
done; \
[ $$exit_code -eq 0 ] && echo "Success !"; \
exit $$exit_code;
#-------------------#
# Compose shortcuts #
#-------------------#
build:
@echo "🔎 building docker (for dev)"
${DOCKER_COMPOSE} build --progress=plain ${args}
up: _ensure_network
ifdef service
${DOCKER_COMPOSE} up -d ${service} 2>&1
else
${DOCKER_COMPOSE} up -d 2>&1
endif
down:
@echo "🔎 Bringing down containers …"
${DOCKER_COMPOSE} down
_ensure_network:
docker network inspect ${COMMON_NET_NAME} >/dev/null || docker network create -d bridge ${COMMON_NET_NAME}
#--------#
# Checks #
#--------#
check:
@echo "🔎 Running all pre-commit hooks"
pre-commit run --all-files
# note: this is called by pre-commit
check_front: _ensure_network
${DOCKER_COMPOSE} run --rm -T search_nodejs npm run check
# note: this is called by pre-commit, it will also extract translations
check_translations:
@echo "🔎 Checking translations …"
cd frontend && npm install && npm run translations:extract
lint: lint_back lint_front
lint_back:
@echo "🔎 Running linters for backend code..."
pre-commit run black --all-files
lint_front:
@echo "🔎 Running linters for frontend code..."
${DOCKER_COMPOSE} run --rm search_nodejs npm run format
tsc_watch:
@echo "🔎 Running front-end tsc in watch mode..."
${DOCKER_COMPOSE} run --rm search_nodejs npm run build:watch
update_poetry_lock:
@echo "🔎 Updating poetry.lock"
${DOCKER_COMPOSE} run --rm api poetry lock --no-update
#-------#
# Tests #
#-------#
test: _ensure_network check_poetry_lock test_api test_front
check_poetry_lock:
@echo "🔎 Checking poetry.lock"
# we have to mount whole project folder for pyproject will be checked
${DOCKER_COMPOSE} run -v $$(pwd):/project -w /project --rm api poetry check --lock
test_api: test_api_unit test_api_integration
test_api_unit:
@echo "🔎 Running API unit tests..."
${DOCKER_COMPOSE_TEST} run --rm api pytest ${args} tests/ --ignore=tests/int
# you can use keep_es=1 to avoid stopping elasticsearch after tests (useful during development)
test_api_integration:
@echo "🔎 Running API integration tests..."
${DOCKER_COMPOSE_TEST} up -d es01 es02 elasticvue
${DOCKER_COMPOSE_TEST} run --rm api pytest ${args} tests/ --ignore=tests/unit
test -z "${keep_es}" && ${DOCKER_COMPOSE_TEST} stop es01 es02 elasticvue || true
test_front:
@echo "🔎 Running front-end tests..."
${DOCKER_COMPOSE_TEST} run --rm search_nodejs npm run test
test_front_watch:
@echo "🔎 Running front-end tests..."
${DOCKER_COMPOSE_TEST} run --rm search_nodejs npm run test:watch
test_clean:
@echo "🔎 Cleaning tests instances..."
${DOCKER_COMPOSE_TEST} down -v
#-----------#
# 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;
import-dataset: guard-filepath
@echo "🔎 Importing data …"
${DOCKER_COMPOSE} run --rm api python3 -m app import /opt/search/data/${filepath} ${args} --num-processes=2
import-taxonomies:
@echo "🔎 Importing taxonomies …"
${DOCKER_COMPOSE} run --rm api python3 -m app import-taxonomies ${args}
sync-scripts:
@echo "🔎 Syncing scripts …"
${DOCKER_COMPOSE} run --rm api python3 -m app sync-scripts
build-translations:
@echo "🔎 Building translations …"
${DOCKER_COMPOSE} run --rm search_nodejs npm run translations:build
cleanup-indexes:
@echo "🔎 Cleaning indexes …"
${DOCKER_COMPOSE} run --rm api python3 -m app cleanup-indexes ${args}
generate-openapi: _ensure_network
@echo "🔎 Generating OpenAPI spec …"
${DOCKER_COMPOSE} run --rm api python3 -m app export-openapi /opt/search/data/searchalicious-openapi.yml
generate-custom-elements: _ensure_network
@echo "🔎 Generating custome-elements.json …"
${DOCKER_COMPOSE} run --rm search_nodejs npm run analyze
generate-config-schema: _ensure_network
@echo "🔎 Generating config-schema.yml …"
${DOCKER_COMPOSE} run --rm api python3 -m app export-config-schema /opt/search/data/searchalicious-config-schema.yml
generate-settings-schema: _ensure_network
@echo "🔎 Generating settings-schema.yml …"
${DOCKER_COMPOSE} run --rm api python3 -m app export-settings-schema /opt/search/data/searchalicious-settings-schema.yml
#-------#
# Tests #
#-------#
unit-tests:
@echo "🔎 Running unit tests …"
# change project name to run in isolation
${DOCKER_COMPOSE_TEST} run --rm api poetry run pytest --cov-report xml --cov=app tests/unit