-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
429 lines (347 loc) · 10.4 KB
/
Copy pathMakefile
File metadata and controls
429 lines (347 loc) · 10.4 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
.SILENT:
.DEFAULT_GOAL := help
include makefiles/colors.mk
include makefiles/help.mk
include makefiles/deps.mk
export PROJECT := githubcontrib
export APP := $(PROJECT)
export BUCKET := scrap-db-backups
export DOCKER_SECRETS_ENV_FILE := docker_secrets.env
ENV_FILE := env.sh
ENV_CUSTOM_FILE := env_custom.sh
ENV_SECRETS_FILE := env_secrets.sh
DB_ENV_PROD_FILE := db_env_prod.sh
SHELL := /bin/bash
VENV_DIR := .venv
SOURCE_CMDS := source $(VENV_DIR)/bin/activate && source $(ENV_FILE) && source $(ENV_CUSTOM_FILE) && source $(ENV_SECRETS_FILE)
CMD_FRONTEND := cd frontend && source $(ENV_FILE)
PYTHON_VERSION := 3.12
PYTHON := python$(PYTHON_VERSION)
#------------------------------------
# Installation
#------------------------------------
.PHONY: install-linters-binaries
## Install linters binaries | Installation
install-linters-binaries: .install-hadolint .install-actionlint .install-shellcheck
.PHONY: install-deps
## Install dependencies
install-deps: install-linters-binaries
sudo apt install $(PYTHON) $(PYTHON)-venv $(PYTHON)-dev -y
sudo apt install libmysqlclient-dev -y
pip3 install uv tox
.PHONY: create-venv
## Create venv and sync dependencies
create-venv:
uv venv --python $(PYTHON_VERSION) --allow-existing
uv sync
.PHONY: create-tox-venv
## Create tox venv and sync dependencies
create-tox-venv:
tox -e py-requirements
.PHONY: create-venvs
## Create venv and tox venv and sync dependencies
create-venvs: create-venv create-tox-venv
.PHONY: yarn-install-locked
## Run frontend yarn install using lockfile
yarn-install-locked:
$(CMD_FRONTEND) && yarn install --immutable
.PHONY: yarn-install
## Run frontend yarn install
yarn-install:
$(CMD_FRONTEND) && yarn install
.PHONY: create-db
## Create db
create-db:
source $(ENV_FILE) && scripts/create_db.sh
.PHONY: bootstrap
## Bootstrap project
bootstrap: yarn-install-locked create-env-files create-venvs create-db migrate build
.PHONY: create-env-files
## Create env files
create-env-files: $(ENV_CUSTOM_FILE) $(ENV_SECRETS_FILE) $(DB_ENV_PROD_FILE) $(DOCKER_SECRETS_ENV_FILE)
$(DOCKER_SECRETS_ENV_FILE):
cp $(DOCKER_SECRETS_ENV_FILE).tpl $(DOCKER_SECRETS_ENV_FILE)
$(ENV_CUSTOM_FILE):
cp $(ENV_CUSTOM_FILE).tpl $(ENV_CUSTOM_FILE)
$(ENV_SECRETS_FILE):
cp $(ENV_SECRETS_FILE).tpl $(ENV_SECRETS_FILE)
$(DB_ENV_PROD_FILE):
cp $(DB_ENV_PROD_FILE).tpl $(DB_ENV_PROD_FILE)
#------------------------------------
# Docker commands
#------------------------------------
export DOCKER_ENV_FILE := docker.env
.PHONY: docker-build-dev
## Build docker images | Docker
docker-build-dev: docker-build-backend docker-build-frontend-dev
.PHONY: docker-build-backend
## Build docker backend image
docker-build-backend:
$(call print,Building Docker backend image)
@docker build -t "${PROJECT}:backend" .
.PHONY: docker-build-frontend-dev
## Build docker frontend image
docker-build-frontend-dev:
$(call print,Building Docker frontend image)
docker build --build-arg VITE_BACKEND_URL="http://localhost:8000/" -t "${PROJECT}:frontend" ./frontend
.PHONY: docker-run
## Run server in docker
docker-run: collectstatic
$(call print,Running server in docker)
@docker-compose up
.PHONY: docker-sh
## Run docker shell
docker-sh:
$(call print,Running docker shell)
@docker run -ti --env-file ${DOCKER_ENV_FILE} --env-file $(DOCKER_SECRETS_ENV_FILE) ${PROJECT}:backend sh
#------------------------------------
#------------------------------------
# Scripts
#------------------------------------
.PHONY: backup-db
backup-db:
scripts/backup_db.sh
.PHONY: flush-cdn-cache
flush-cdn-cache:
scripts/flush_cdn_cache.sh
#------------------------------------
# Tests
#------------------------------------
.PHONY: test
## Run tests | Tests
test: shellcheck hadolint actionlint tox eslint prettier-json-lint prettier-scss-lint prettier-yaml-lint \
prettier-ts-lint prettier-vue-lint test-frontend ts-lint
.PHONY: test-python
## Run Python tests
test-python: ruff mypy pytest
.PHONY: tox
## Run tox
tox:
tox
.PHONY: rstlint
## Run rstlint linter
rstlint:
tox -e py-rstlint
.PHONY: safety
## Run safety linter
safety:
tox -e py-safety
.PHONY: pytest
## Run pytest
pytest:
tox -e py-pytest
.PHONY: ruff
## Run ruff linter
ruff:
tox -e py-ruff
.PHONY: mypy
## Run mypy linter
mypy:
tox -e py-mypy
.PHONY: test-frontend
## Run frontend tests
test-frontend:
$(CMD_FRONTEND) && yarn run test:run
.PHONY: eslint
## Run frontend eslint
eslint:
$(CMD_FRONTEND) && yarn run eslint src/**/*.ts src/*.ts ./*.ts src/App.vue src/components/*.vue src/views/*.vue
.PHONY: ts-lint
## Run frontend TypeScript linter
ts-lint:
$(CMD_FRONTEND) && npx vue-tsc --noEmit
.PHONY: shellcheck
## Run shellcheck linter
shellcheck:
shellcheck scripts/*.sh ./*.sh frontend/*.sh
.PHONY: hadolint
## Run hadolint linter
hadolint:
hadolint Dockerfile frontend/Dockerfile
.PHONY: actionlint
## Run actionlint linter
actionlint:
actionlint
.PHONY: prettier-ts-lint
## Run prettier check for ts
prettier-ts-lint:
$(CMD_FRONTEND) && yarn run prettier --check "src/**/*.ts" "src/*.ts" "./*.ts"
.PHONY: prettier-scss-lint
## Run prettier check for scss
prettier-scss-lint:
$(CMD_FRONTEND) && yarn run prettier --check "./src/styles/*.scss"
.PHONY: prettier-json-lint
## Run prettier check for json
prettier-json-lint:
$(CMD_FRONTEND) && yarn run prettier --check "../*.json" "./**/*.json"
.PHONY: prettier-yaml-lint
## Run prettier check for yaml
prettier-yaml-lint:
$(CMD_FRONTEND) && yarn run prettier --check "../deployment/*.yaml" "../.github/**/*.yaml"
.PHONY: prettier-vue-lint
## Run prettier check for vue
prettier-vue-lint:
$(CMD_FRONTEND) && yarn run prettier --check "src/App.vue" "src/components/*.vue" "src/views/*.vue"
#------------------------------------
# Development
#------------------------------------
.PHONY: update-venvs
## Sync packages in venv and tox venv | Development
update-venvs:
uv sync
tox -e py-requirements
.PHONY: delete-venvs
delete-venvs:
rm -rf $(VENV_DIR)
rm -rf .tox
.PHONY: recreate-venvs
## Recreate venvs
recreate-venvs: delete-venvs create-venvs
.PHONY: yarn-build
## Run frontend build
yarn-build:
$(CMD_FRONTEND) && yarn build
.PHONY: build
## Run frontend build
build: yarn-build
.PHONY: dev
## Run frontend development server
dev:
$(CMD_FRONTEND) && yarn dev
.PHONY: yarn-build-prod
## Run frontend production build
yarn-build-prod: yarn-build
.PHONY: drop-db
## Drop db
drop-db:
source $(ENV_FILE) && scripts/drop_db.sh
.PHONY: load-db
## Load db from today's backup
load-db: drop-db create-db
source $(ENV_FILE) && ./scripts/load_db.sh
.PHONY: uv-update
## Update python packages
uv-update:
uv lock --upgrade
.PHONY: uv-show-outdated
## Show outdated python packages
uv-show-outdated:
uv pip list --outdated
#------------------------------------
# Formatting
#------------------------------------
.PHONY: format
## Format Python code | Formatting
format:
$(SOURCE_CMDS) && ruff format src
.PHONY: format-json
## Format json files
format-json:
$(CMD_FRONTEND) && yarn run prettier --write "../*.json" "./**/*.json"
.PHONY: format-scss
## Format scss files
format-scss:
$(CMD_FRONTEND) && yarn run prettier --write "./src/**/*.scss"
.PHONY: format-ts
## Format ts files
format-ts:
$(CMD_FRONTEND) && yarn run prettier --write "src/**/*.ts" "src/*.ts" "./*.ts"
.PHONY: format-vue
## Format vue files
format-vue:
$(CMD_FRONTEND) && yarn run prettier --write "src/App.vue" "src/components/*.vue" "src/views/*.vue"
.PHONY: format-yaml
## Format yaml files
format-yaml:
$(CMD_FRONTEND) && yarn run prettier --write "../deployment/*.yaml" "../.github/**/*.yaml"
.PHONY: format-all
## Format code
format-all: format format-ts format-vue format-scss format-sh format-json format-yaml
#------------------------------------
# Django management commands
#------------------------------------
MANAGE_CMD := src/manage.py
.PHONY: runserver
## Run backend server for development
runserver:
$(SOURCE_CMDS) && $(MANAGE_CMD) runserver 0.0.0.0:8000
.PHONY: run
## Run backend server for development
run: runserver
.PHONY: migrate
## Run data migration
migrate:
$(SOURCE_CMDS) && $(MANAGE_CMD) migrate
.PHONY: collectstatic
## Collect backend static files
collectstatic:
$(SOURCE_CMDS) && export IS_DEV= && $(MANAGE_CMD) collectstatic --no-input
.PHONY: createsuperuser
## Create super user
createsuperuser:
$(SOURCE_CMDS) && $(MANAGE_CMD) createsuperuser
.PHONY: shell
## Run shell
shell:
$(SOURCE_CMDS) && $(MANAGE_CMD) shell
.PHONY: manage
## Run management command. `make manage command_name arguments="arg1 arg2"`
manage:
$(SOURCE_CMDS) && $(MANAGE_CMD) $(command_name) $(arguments)
#------------------------------------
# Production commands
#------------------------------------
.PHONY: prod-create-db
## Create prod db | Production
prod-create-db:
source $(DB_ENV_PROD_FILE) \
&& scripts/create_db.sh
.PHONY: prod-drop-db
## Drop prod db
prod-drop-db:
source $(DB_ENV_PROD_FILE) \
&& scripts/drop_db.sh
.PHONY: prod-load-db
## Load db to prod from today's backup
prod-load-db: prod-drop-db prod-create-db
source $(DB_ENV_PROD_FILE) \
&& ./scripts/load_db.sh
.PHONY: prod-connect-db
## Connect to prod db
prod-connect-db:
source $(DB_ENV_PROD_FILE) \
&& scripts/connect_db.sh
ifeq (prod-manage,$(firstword $(MAKECMDGOALS)))
# Use the rest as arguments
PROD_MANAGE_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
# Turn them into do-nothing targets
$(eval $(PROD_MANAGE_ARGS):;@:)
endif
.PHONY: prod-manage
## Run management command in prod. Usage: make prod-manage [command] arguments="[arguments]"
prod-manage:
scripts/run_management_command_prod.sh $(PROD_MANAGE_ARGS) $(arguments)
ifeq (prod-manage-interactive,$(firstword $(MAKECMDGOALS)))
# Use the rest as arguments
PROD_MANAGE_INTERACTIVE_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
# Turn them into do-nothing targets
$(eval $(PROD_MANAGE_INTERACTIVE_ARGS):;@:)
endif
.PHONY: prod-manage-interactive
## Run management command in prod (interactive). Usage: make prod-manage [command] arguments="[arguments]"
prod-manage-interactive:
scripts/run_management_command_interactive_prod.sh $(PROD_MANAGE_INTERACTIVE_ARGS) $(arguments)
.PHONY: prod-shell
## Run shell in prod
prod-shell:
scripts/run_shell_prod.sh
.PHONY: prod-migrate
## Run data migration for prod
prod-migrate:
scripts/run_management_command_prod.sh migrate
.PHONY: prod-enable-debug
## Enable debug in prod. It will be reset with the next deployment
prod-enable-debug:
yq eval '.data.DEBUG="True"' deployment/configmap.yaml | kubectl apply -f -
kubectl rollout restart deployment/$(PROJECT)
#------------------------------------