-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
207 lines (162 loc) · 5.51 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
# -- General
SHELL := /bin/bash
# -- Docker
COMPOSE = bin/compose
COMPOSE_EXEC = $(COMPOSE) exec
COMPOSE_RUN = $(COMPOSE) run --rm --no-deps
COMPOSE_RUN_API = $(COMPOSE_RUN) api
COMPOSE_RUN_MAIL = $(COMPOSE_RUN) mail-generator
# -- MySQL
EDX_MYSQL_DB_HOST = mysql
EDX_MYSQL_DB_PORT = 3306
# -- MongoDB
EDX_MONGO_DB_HOST = mongo
EDX_MONGO_DB_PORT = 27017
# -- Postgresql
DB_HOST = postgresql
DB_PORT = 5432
# -- Mork
MORK_IMAGE_NAME ?= mork
MORK_IMAGE_TAG ?= development
MORK_IMAGE_BUILD_TARGET ?= development
MORK_SERVER_PORT ?= 8100
MORK_TEST_DB_NAME ?= test-mork-db
# -- Celery
MORK_CELERY_SERVER_PORT ?= 5555
# -- Mail
MAIL_YARN = $(COMPOSE_RUN_MAIL) yarn
# ==============================================================================
# RULES
default: help
.env:
cp .env.dist .env
.git/hooks/pre-commit:
ln -sf ../../bin/git-hook-pre-commit .git/hooks/pre-commit
git-hook-pre-commit: ## Install git pre-commit hook
git-hook-pre-commit: .git/hooks/pre-commit
@echo "Git pre-commit hook linked"
.PHONY: git-hook-pre-commit
# -- Docker/compose
bootstrap: ## bootstrap the project for development
bootstrap: \
.env \
build \
run \
migrate \
seed-edx-databases \
mails-install \
mails-build
.PHONY: bootstrap
build: ## build the app containers
build: \
build-docker-api
.PHONY: build
build-docker-api: ## build the api container
build-docker-api: .env
MORK_IMAGE_BUILD_TARGET=$(MORK_IMAGE_BUILD_TARGET) \
MORK_IMAGE_NAME=$(MORK_IMAGE_NAME) \
MORK_IMAGE_TAG=$(MORK_IMAGE_TAG) \
$(COMPOSE) build api
.PHONY: build-docker-api
down: ## stop and remove all containers
@$(COMPOSE) down
.PHONY: down
logs: ## display frontend/api logs (follow mode)
@$(COMPOSE) logs -f api
.PHONY: logs
logs-celery: ## display celery logs (follow mode)
@$(COMPOSE) logs -f celery
.PHONY: logs-celery
purge-celery: ## purge celery tasks
@$(COMPOSE_EXEC) celery celery -A mork.celery.celery_app purge
.PHONY: purge-celery
flower: ## run flower
@$(COMPOSE_EXEC) celery celery -A mork.celery.celery_app flower
.PHONY: flower
run: ## run the whole stack
run: \
run-api \
run-celery
.PHONY: run
run-celery: ## run the celery server (development mode)
@$(COMPOSE) up -d celery
@echo "Waiting for celery to be up and running..."
@$(COMPOSE_RUN) dockerize -wait tcp://$(DB_HOST):$(DB_PORT) -timeout 60s
.PHONY: run-celery
run-api: ## run the api server (development mode)
@$(COMPOSE) up -d api
@echo "Waiting for api to be up and running..."
@$(COMPOSE_RUN) dockerize -wait tcp://api:$(MORK_SERVER_PORT) -timeout 60s
.PHONY: run-api
status: ## an alias for "docker compose ps"
@$(COMPOSE) ps
.PHONY: status
stop: ## stop all servers
@$(COMPOSE) stop
.PHONY: stop
seed-edx-databases: ## seed the edx MySQL and MongoDB databases with test data
@echo "Waiting for mysql to be up and running…"
@$(COMPOSE_RUN) dockerize -wait tcp://$(EDX_MYSQL_DB_HOST):$(EDX_MYSQL_DB_PORT) -timeout 60s
@echo "Waiting for mongodb to be up and running…"
@$(COMPOSE_RUN) dockerize -wait tcp://$(EDX_MONGO_DB_HOST):$(EDX_MONGO_DB_PORT) -timeout 60s
@echo "Seeding the edx database…"
@$(COMPOSE) exec -T celery python /opt/src/seed_edx_database.py
.PHONY: seed-edx-databases
# -- Provisioning
create-test-db: ## create test database
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$(DB_HOST):$(DB_PORT)/postgres" -c "create database \"$(MORK_TEST_DB_NAME)\";"' || echo "Duly noted, skipping database creation."
.PHONY: create-test-db
drop-test-db: ## drop test database
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$(DB_HOST):$(DB_PORT)/postgres" -c "drop database \"$(MORK_TEST_DB_NAME)\";"' || echo "Duly noted, skipping database deletion."
.PHONY: drop-test-db
migrate: ## run alembic database migrations for the mork database
@echo "Running database engine…"
@$(COMPOSE) up -d postgresql
@$(COMPOSE_RUN) dockerize -wait tcp://$(DB_HOST):$(DB_PORT) -timeout 60s
@echo "Create mork service database…"
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$(DB_HOST):$(DB_PORT)/postgres" -c "create database \"mork\";"' || echo "Duly noted, skipping database creation."
@echo "Running migrations for mork service…"
@bin/alembic upgrade head
.PHONY: migrate
# -- Linters
lint: ## lint python sources
lint: \
lint-black \
lint-ruff
.PHONY: lint
lint-black: ## lint python sources with black
@echo 'lint:black started…'
@$(COMPOSE_RUN_API) black mork
.PHONY: lint-black
lint-ruff: ## lint python sources with ruff
@echo 'lint:ruff started…'
@$(COMPOSE_RUN_API) ruff check .
.PHONY: lint-ruff
lint-ruff-fix: ## lint and fix python sources with ruff
@echo 'lint:ruff-fix started…'
@$(COMPOSE_RUN_API) ruff check . --fix
.PHONY: lint-ruff-fix
## -- Tests
test: ## run api tests
test: \
run \
create-test-db
bin/pytest
.PHONY: test
# -- Mail generator
mails-build: ## Convert mjml files to html and text
@$(MAIL_YARN) build
.PHONY: mails-build
mails-build-html-to-plain-text: ## Convert html files to text
@$(MAIL_YARN) build-html-to-plain-text
.PHONY: mails-build-html-to-plain-text
mails-build-mjml-to-html: ## Convert mjml files to html and text
@$(MAIL_YARN) build-mjml-to-html
.PHONY: mails-build-mjml-to-html
mails-install: ## mail-generator yarn install
@$(MAIL_YARN) install
.PHONY: mails-install
# -- Misc
help:
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
.PHONY: help