-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Makefile
185 lines (139 loc) · 4.5 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
#!/usr/bin/make
ifneq (,$(wildcard ./.env))
-include .env
-include .envrc
export
endif
MOUNT_POINT ?= /mnt
DOCKER_LOCAL_DATA ?= /srv/off/docker_data
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
DOCKER_COMPOSE=docker compose --env-file=${ENV_FILE}
DOCKER_COMPOSE_TEST=COMPOSE_PROJECT_NAME=open_prices_test COMMON_NET_NAME=po_test docker compose --env-file=${ENV_FILE}
# avoid target corresponding to file names, to depends on them
.PHONY: *
#-----------#
# 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;
#------------#
# Production #
#------------#
test:
echo ${ENV_FILE}
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;
build:
@echo "🥫 building docker (for dev)"
${DOCKER_COMPOSE} build
up:
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
hdown:
@echo "🥫 Bringing down containers and associated volumes …"
${DOCKER_COMPOSE} down -v
# pull images from image repository
pull:
${DOCKER_COMPOSE} pull
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 api update-listener
#------------#
# Quality #
#------------#
toml-check:
${DOCKER_COMPOSE} run --rm --no-deps api poetry run toml-sort --check pyproject.toml
toml-lint:
${DOCKER_COMPOSE} run --rm --no-deps api poetry run toml-sort --in-place pyproject.toml
flake8:
${DOCKER_COMPOSE} run --rm --no-deps api flake8
black-check:
${DOCKER_COMPOSE} run --rm --no-deps api black --check .
black:
${DOCKER_COMPOSE} run --rm --no-deps api black .
mypy:
${DOCKER_COMPOSE} run --rm --no-deps api mypy .
isort-check:
${DOCKER_COMPOSE} run --rm --no-deps api isort --check .
isort:
${DOCKER_COMPOSE} run --rm --no-deps api isort .
docs:
@echo "🥫 Generationg doc…"
${DOCKER_COMPOSE} run --rm --no-deps api ./build_mkdocs.sh
checks: toml-check flake8 black-check mypy isort-check docs
tests: django-tests
django-tests:
@echo "🥫 Running tests …"
# change project name to run in isolation
${DOCKER_COMPOSE_TEST} run --rm api poetry run python3 manage.py test
django-tests-single: guard-args
@echo "🥫 Running specific tests …"
${DOCKER_COMPOSE_TEST} run --rm api poetry run python3 manage.py test -v 2 ${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 open_prices_postgres-data
docker volume create open_prices_images
docker volume create open_prices_data-dump
create_external_networks:
@echo "🥫 Creating external networks if needed … (dev only)"
( docker network create ${COMMON_NET_NAME} || true )
# for tests
( docker network create po_test || true )
cp-static-files:
@echo "🥫 Copying static files from api container to the host …"
docker cp open_prices-api-1:/opt/open-prices/static www/
migrate-db:
@echo "🥫 Migrating database …"
${DOCKER_COMPOSE} run --rm --no-deps api python3 manage.py migrate
cli: guard-args
${DOCKER_COMPOSE} run --rm --no-deps api python3 manage.py ${args}
makemigrations: guard-args
${DOCKER_COMPOSE} run --rm --no-deps api python3 manage.py makemigrations ${args}
#---------#
# Cleanup #
#---------#
prune:
@echo "🥫 Pruning unused Docker artifacts (save space) …"
docker system prune -af
prune_cache:
@echo "🥫 Pruning Docker builder cache …"
docker builder prune -f