-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
210 lines (177 loc) · 7.92 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
include ./make/print.lib.mk
include ./make/dynamic-recipe.lib.mk
#------------------------------
# dynamic recipes
#
# marked with # dynamic
# A named recipe (eg. manage) will set CMD, and call it
# if it's the last recipe being called (eg. make manage).
# A recipe caught by .DEFAULT will add the recipe name
# (eg. changepassword) to a list of ARGS, and call CMD with
# the list of ARGS if it's the last recipe being called
# (eg. make manage changepassword youruser).
#
# NOTE: don't create any recipes with the same name as any
# dynamic recipe args (eg. changepassword)
#------------------------------
#------------------------------
# vars
#------------------------------
SHELL := /bin/bash
CMD := ""
POS_ARGS := ""
ARGS := ""
SSH_AUTH_SOCK_RSA_MOUNT_VOLUMES = -v ~/.ssh/id_rsa:/id_rsa:ro -v ~/.ssh/id_rsa.pub:/id_rsa.pub:ro
SSH_AUTH_SOCK_ED25519_MOUNT_VOLUMES = -v ~/.ssh/id_ed25519:/id_ed25519:ro -v ~/.ssh/id_ed25519.pub:/id_ed25519.pub:ro
DOCKER_RUN_ARGS_FOR_SSH_AUTH_SOCK_RSA := --entrypoint= --rm --tty --interactive --env SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock -v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock ${SSH_AUTH_SOCK_RSA_MOUNT_VOLUMES}
DOCKER_RUN_ARGS_FOR_SSH_AUTH_SOCK_ED25519 := --entrypoint= --rm --tty --interactive --env SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock -v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock ${SSH_AUTH_SOCK_ED25519_MOUNT_VOLUMES}
SERVICE_TAG :=mscheremetjew/levenshtein-distance-service
WEB_SERVICE_NAME :=web
#------------------------------
# helpers
#------------------------------
COMMA := ,
#------------------------------
# help
#------------------------------
.PHONY: help
help:
$(call print_h1,"AVAILABLE","OPTIONS")
$(call print_space)
$(call print_h2,"code")
$(call print_options,"lint","Run code lint checks.")
$(call print_options,"format","Automatically format code where possible.")
$(call print_space)
$(call print_h2,"docker")
$(call print_options,"build-service","Build the docker image")
$(call print_options,"up","Launch all containers including web server in http://localhost:8080".)
$(call print_options,"buildup","Rebuild and launch all containers including web server in http://localhost:8080".)
$(call print_space)
$(call print_h2,"data")
$(call print_options,"makemigrations","Generate new database migration if exists$(COMMA) after you will need to do 'sudo chown -R myusername .'.")
$(call print_options,"migrate","Executes missing migrations.")
$(call print_space)
$(call print_h2,"dependency")
$(call print_options,"pip-compile-rsa","Compile requirements.txt from requirements.in without upgrading the packages and build the images using RSA SSH key.")
$(call print_options,"pip-compile-ed-25519","Compile requirements.txt from requirements.in without upgrading the packages and build the images with ed-25519 SSH key.")
$(call print_space)
$(call print_h2,"test")
$(call print_options,"pytest","Run all pytests (takes args additional via ARGS=\"...\" eg. \`\`make pytest ARGS=\"entertainment/tests/ --reuse-db\"\`\` or \`\`make pytest ARGS=\"-m \'mark1 and not mark2\'\"\`\`).")
$(call print_options,"pytest-h","Show pytest help")
$(call print_space)
$(call print_h2,"dynamic recipes")
$(call print_h3,"accepts any number of additional positional args as well as --args via ARGS=\"...\"")
#------------------------------
# code
#------------------------------
.PHONY: lint
lint: build-service
$(call print_h1,"LINTING","CODE")
@docker-compose run --rm --entrypoint=sh $(WEB_SERVICE_NAME) -c "flake8"
@docker-compose run --rm --entrypoint=sh $(WEB_SERVICE_NAME) -c "isort --recursive --check-only"
@docker-compose run --rm --entrypoint=sh $(WEB_SERVICE_NAME) -c "black --check ./"
$(call print_h1,"LINTING","COMPLETE")
.PHONY: format
format: build-service
$(call print_h1,"FORMATTING","CODE")
@docker-compose run --rm --entrypoint=sh $(WEB_SERVICE_NAME) -c "isort --recursive --apply"
@docker-compose run --rm --entrypoint=sh $(WEB_SERVICE_NAME) -c "black ./"
$(call print_h1,"FORMATTING","COMPLETE")
#------------------------------
# docker helpers
#------------------------------
# runs docker compose with the provided args,
# and prints out the full command being run
define dockercompose
$(call print,"docker-compose $(1)")
@docker-compose $(1)
endef
# checks for 'local' command and if local config file exists,
# and if so runs docker-compose using the local file,
# if not prints a warning and runs with default config.
define dockercomposelocal
@$(eval TARGETING_LOCAL := $(if $(filter-out local,$(lastword $(MAKECMDGOALS))),,""))
@$(eval LOCAL_FILE_EXISTS := $(if $(wildcard $(FILE_PATH_DOCKER_COMPOSE_LOCAL)),"",))
@$(eval DOCKER_COMPOSE_FILE_ARG := $(if $(and $(TARGETING_LOCAL),$(LOCAL_FILE_EXISTS)),"-f $(FILE_PATH_DOCKER_COMPOSE_LOCAL) ",))
@$(if $(TARGETING_LOCAL),$(if $(LOCAL_FILE_EXISTS),,$(call print_warning,"Local config missing$(COMMA) please create: $(FILE_PATH_DOCKER_COMPOSE_LOCAL)$(COMMA) using default...")),)
$(call dockercompose,"$(DOCKER_COMPOSE_FILE_ARG)$(1)")
endef
define build-service
$(call print_h1,"BUILDING","IMAGES","FROM","SCRATCH")
@docker build --ssh default -t $(SERVICE_TAG) -f Dockerfile .
$(call print_h1,"IMAGES","BUILT","FROM","SCRATCH")
endef
#------------------------------
# docker
#------------------------------
.PHONY: build-service
build-service:
$(call build-service)
.PHONY: up
up: build-service
$(call print_h1,"LAUNCHING","ALL","DOCKER","CONTAINERS")
$(call dockercomposelocal,"up -d")
.PHONY: buildup
buildup: build-service
$(call print_h1,"REBUILDING","AND","LAUNCHING","ALL DOCKER CONTAINERS")
$(call dockercomposelocal,"up --build")
#------------------------------
# dependency
#------------------------------
.PHONY: pip-compile-rsa pip-compile-ed-25519
pip-compile-rsa: build-service
$(call print_h1,"COMPILING","REQUIREMENTS")
@-docker run $(DOCKER_RUN_ARGS_FOR_SSH_AUTH_SOCK_RSA) -v ${PWD}:/var/levenshtein-distance-service/ $(SERVICE_TAG) sh -c "ssh-add /id_rsa && pip-compile --no-header --output-file=requirements.txt"
$(call build-service)
$(call print_h1,"REQUIREMENTS","COMPILED")
pip-compile-ed-25519: build-service
$(call print_h1,"COMPILING","REQUIREMENTS")
@-docker run $(DOCKER_RUN_ARGS_FOR_SSH_AUTH_SOCK_ED25519) -v ${PWD}:/var/levenshtein-distance-service/ $(SERVICE_TAG) sh -c "ssh-add /id_ed25519 && pip-compile --no-header --output-file=requirements.txt"
$(call build-service)
$(call print_h1,"REQUIREMENTS","COMPILED")
#------------------------------
# data
#------------------------------
.PHONY: makemigrations
makemigrations:
$(call print_h1,"MAKING","MIGRATIONS")
@docker-compose run --rm web python manage.py makemigrations
$(call print_h1,"MIGRATIONS","MADE")
.PHONY: migrate
migrate: up
$(call print_h1,"RUNNING","MIGRATIONS")
@docker-compose exec web python manage.py migrate
@docker-compose exec web python manage.py migrate django_celery_results
$(call print_h1,"MIGRATIONS","COMPLETE")
.PHONY: createsuperuser
createsuperuser: up
$(call print_h1,"RUNNING","CREATESUPERUSER")
@docker-compose exec web python manage.py createsuperuser
$(call print_h1,"CREATESUPERUSER","COMPLETE")
#------------------------------
# utility
#------------------------------
.PHONY: shell-web
shell-web:
$(call print_h1,"ENTERING","SHELL")
@docker-compose exec web /bin/bash
#------------------------------
# Q&A
#------------------------------
.PHONY: pytest
pytest:
$(call print_h1,"RUNNING","PYTEST","TESTS")
@$(eval CMD := "docker-compose run --rm --entrypoint=sh $(WEB_SERVICE_NAME) -c 'pytest -vv'")
$(call run_if_last,${CMD} ${POS_ARGS} ${ARGS})
.PHONY: pytest-h
pytest-h:
$(call print_h1,"SHOWING","PYTEST","HELP")
@docker-compose run --rm --entrypoint=sh $(WEB_SERVICE_NAME) -c "pytest --help"
#------------------------------
# dynamic functionality
#------------------------------
# adds recipe name (eg. changepassword) to POS_ARGS, calls CMD with ARGS and POS_ARGS if last
.DEFAULT:
@$(eval POS_ARGS += $@)
@$(eval ARGS += $(ARGS))
$(call run_if_last,${CMD} ${ARGS} ${POS_ARGS})