-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
493 lines (405 loc) · 16.1 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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# -- General
SHELL := /bin/bash
# -- Docker
COMPOSE = bin/compose
COMPOSE_UP = $(COMPOSE) up -d
COMPOSE_RUN = $(COMPOSE) run --rm --no-deps
COMPOSE_RUN_API = $(COMPOSE_RUN) api
COMPOSE_RUN_API_PIPENV = $(COMPOSE_RUN_API) pipenv run
COMPOSE_RUN_CLIENT = $(COMPOSE_RUN) client
COMPOSE_RUN_PREFECT_PIPENV = $(COMPOSE_RUN) prefect pipenv run
COMPOSE_RUN_DASHBOARD_PIPENV = $(COMPOSE_RUN) dashboard pipenv run
# -- Tools
CURL = $(COMPOSE_RUN) curl
# -- Ressources
AFIREV_CHARGING_DATASET_URL = https://afirev.fr/en/liste-des-identifiants-attribues/
# ==============================================================================
# RULES
default: help
# -- Files
data:
mkdir -p data
data/afirev-charging.csv: data
@echo "You should download CSV file from $(AFIREV_CHARGING_DATASET_URL)"
# -- Docker/compose
bootstrap: ## bootstrap the project for development
bootstrap: \
build \
migrate-api \
create-api-test-db \
create-metabase-db \
create-prefect-db \
migrate-prefect \
create-dashboard-db \
migrate-dashboard \
seed-metabase \
seed-oidc \
create-api-superuser \
create-dashboard-superuser \
seed-dashboard \
jupytext--to-ipynb \
seed-api
.PHONY: bootstrap
build: ## build services image
$(COMPOSE) build
.PHONY: build
build-api: ## build the api image
$(COMPOSE) build api
.PHONY: build-api
build-client: ## build the client image
$(COMPOSE) build client
.PHONY: build-client
build-notebook: ## build custom jupyter notebook image
@$(COMPOSE) build notebook
.PHONY: build-notebook
build-opendata: ## build opendata image
@$(COMPOSE) build opendata
.PHONY: build-opendata
build-prefect: ## build prefect image
@$(COMPOSE) build prefect
.PHONY: build-prefect
build-dashboard: ## build dashboard image
@$(COMPOSE) build dashboard
.PHONY: build-dashboard
down: ## stop and remove all containers
@$(COMPOSE) down
.PHONY: down
logs: ## display all services logs (follow mode)
@$(COMPOSE) logs -f
.PHONY: logs
logs-api: ## display API server logs (follow mode)
@$(COMPOSE) logs -f api
.PHONY: logs-api
logs-notebook: ## display notebook logs (follow mode)
@$(COMPOSE) logs -f notebook
.PHONY: logs-notebook
logs-opendata: ## display opendata logs (follow mode)
@$(COMPOSE) logs -f opendata
.PHONY: logs-opendata
logs-prefect: ## display prefect logs (follow mode)
@$(COMPOSE) logs -f prefect prefect-worker
.PHONY: logs-prefect
logs-dashboard: ## display dashboard logs (follow mode)
@$(COMPOSE) logs -f dashboard
.PHONY: logs-dashboard
run: ## run the api server (and dependencies)
$(COMPOSE_UP) --wait api
.PHONY: run
run-all: ## run the whole stack
$(COMPOSE_UP) api keycloak metabase notebook opendata dashboard
.PHONY: run-all
run-metabase: ## run the metabase service
$(COMPOSE_UP) metabase
.PHONY: run-metabase
run-notebook: ## run the notebook service
$(COMPOSE_UP) notebook
.PHONY: run-notebook
run-opendata: ## run the opendata service
$(COMPOSE_UP) opendata
.PHONY: run-opendata
run-prefect: ## run the prefect service
$(COMPOSE_UP) --wait prefect
$(COMPOSE_UP) prefect-worker
.PHONY: run-prefect
run-dashboard: ## run the dashboard service
$(COMPOSE_UP) dashboard
.PHONY: run-dashboard
status: ## an alias for "docker compose ps"
@$(COMPOSE) ps
.PHONY: status
stop: ## stop all servers
@$(COMPOSE) stop
.PHONY: stop
# -- Provisioning
data/qualicharge-api-schema.sql:
$(COMPOSE) exec postgresql pg_dump -s -Z 9 -U qualicharge -F c qualicharge-api > data/qualicharge-api-schema.sql
data/qualicharge-api-data.sql:
$(COMPOSE) exec postgresql pg_dump -a -Z 9 -U qualicharge -F c qualicharge-api > data/qualicharge-api-data.sql
backup-api-db: ## create API database backup
backup-api-db: \
data/qualicharge-api-schema.sql \
data/qualicharge-api-data.sql
.PHONY: backup-api-db
restore-api-db-data: ## restore API database backup data
restore-api-db-data: data/qualicharge-api-data.sql
cat data/qualicharge-api-data.sql | \
$(COMPOSE) exec -T postgresql pg_restore -a -U qualicharge -F c -d qualicharge-api
.PHONY: restore-api-db-data
restore-api-db-schema: ## restore API database backup schema
restore-api-db-schema: data/qualicharge-api-schema.sql
cat data/qualicharge-api-schema.sql | \
$(COMPOSE) exec -T postgresql pg_restore -s -U qualicharge -F c -d qualicharge-api
.PHONY: restore-api-db-schema
restore-api-db: ## restore API database backup
restore-api-db: \
restore-api-db-schema \
restore-api-db-data
.PHONY: restore-api-db
create-api-test-db: ## create API test database
@echo "Creating api service test database…"
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$${QUALICHARGE_DB_HOST}:$${QUALICHARGE_DB_PORT}/postgres" -c "create database \"$${QUALICHARGE_TEST_DB_NAME}\";"' || echo "Duly noted, skipping database creation."
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$${QUALICHARGE_DB_HOST}:$${QUALICHARGE_DB_PORT}/$${QUALICHARGE_TEST_DB_NAME}" -c "create extension postgis;"' || echo "Duly noted, skipping extension creation."
.PHONY: create-api-test-db
create-metabase-db: ## create metabase database
@echo "Creating metabase service database…"
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$${QUALICHARGE_DB_HOST}:$${QUALICHARGE_DB_PORT}/postgres" -c "create database \"$${MB_DB_DBNAME}\";"' || echo "Duly noted, skipping database creation."
.PHONY: create-metabase-db
create-prefect-db: ## create prefect database
@echo "Creating prefect service database…"
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$${QUALICHARGE_DB_HOST}:$${QUALICHARGE_DB_PORT}/postgres" -c "create database \"$${PREFECT_API_DATABASE_NAME}\";"' || echo "Duly noted, skipping database creation."
.PHONY: create-prefect-db
create-dashboard-db: ## create dashboard database
@echo "Creating dashboard service database…"
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$${QUALICHARGE_DB_HOST}:$${QUALICHARGE_DB_PORT}/postgres" -c "create database \"$${DASHBOARD_DB_NAME}\";"' || echo "Duly noted, skipping database creation."
.PHONY: create-dashboard-db
drop-api-test-db: ## drop API test database
@echo "Droping api service test database…"
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$${QUALICHARGE_DB_HOST}:$${QUALICHARGE_DB_PORT}/postgres" -c "drop database \"$${QUALICHARGE_TEST_DB_NAME}\";"' || echo "Duly noted, skipping database deletion."
.PHONY: drop-api-test-db
drop-api-db: ## drop API database
@echo "Droping api service database…"
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$${QUALICHARGE_DB_HOST}:$${QUALICHARGE_DB_PORT}/postgres" -c "drop database \"$${QUALICHARGE_DB_NAME}\";"' || echo "Duly noted, skipping database deletion."
.PHONY: drop-api-db
drop-metabase-db: ## drop Metabase database
@echo "Droping metabase service database…"
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$${QUALICHARGE_DB_HOST}:$${QUALICHARGE_DB_PORT}/postgres" -c "drop database \"$${MB_DB_DBNAME}\";"' || echo "Duly noted, skipping database deletion."
.PHONY: drop-metabase-db
drop-dashboard-db: ## drop dashboard database
@echo "Droping dashboard service database…"
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$${QUALICHARGE_DB_HOST}:$${QUALICHARGE_DB_PORT}/postgres" -c "drop database \"$${DASHBOARD_DB_NAME}\";"' || echo "Duly noted, skipping database deletion."
.PHONY: drop-dashboard-db
dump-metabase: ## dump metabase objects
bin/pg_dump -a --inserts \
-t Report_Card \
-t Report_Dashboard \
-t Report_DashboardCard \
-t Dashboard_Tab \
-t Setting \
-U qualicharge \
metabaseappdb > src/metabase/custom.sql
.PHONY: dump-metabase
migrate-api: ## run alembic database migrations for the api service
@echo "Running api service database engine…"
@$(COMPOSE_UP) --wait postgresql
@echo "Creating api service database…"
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$${QUALICHARGE_DB_HOST}:$${QUALICHARGE_DB_PORT}/postgres" -c "create database \"$${QUALICHARGE_DB_NAME}\";"' || echo "Duly noted, skipping database creation."
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$${QUALICHARGE_DB_HOST}:$${QUALICHARGE_DB_PORT}/$${QUALICHARGE_DB_NAME}" -c "create extension postgis;"' || echo "Duly noted, skipping extension creation."
@echo "Running migrations for api service…"
@bin/alembic upgrade head
.PHONY: migrate-api
migrate-dashboard: ## create dashboard database
@echo "Running dashboard service database engine…"
@$(COMPOSE_UP) --wait postgresql
@echo "Migrating dashboard database…"#
@bin/manage migrate
.PHONY: migrate-dashboard
migrate-prefect: ## run prefect database migrations
@echo "Running prefect service database engine…"
@$(COMPOSE_UP) --wait postgresql
@echo "Running migrations for prefect service…"
@$(COMPOSE_RUN_PREFECT_PIPENV) prefect server database upgrade -y
.PHONY: migrate-prefect
post-deploy-prefect: ## run prefect post-deployment script
@echo "Running prefect service…"
@$(COMPOSE_UP) --wait prefect
@echo "Running postdeploy script for prefect service…"
@$(COMPOSE) exec prefect pipenv run honcho start postdeploy
.PHONY: post-deploy-prefect
create-api-superuser: ## create api super user
@echo "Creating super user…"
@$(COMPOSE_RUN_API_PIPENV) python -m qualicharge create-user \
admin \
--email [email protected] \
--password admin \
--is-active \
--is-superuser \
--is-staff \
--force
.PHONY: create-api-superuser
create-dashboard-superuser: ## create dashboard super user
@echo "Running dashboard service database engine…"
@$(COMPOSE_UP) --wait postgresql
@echo "Creating dashboard super user…"
@bin/manage createsuperuser --noinput
.PHONY: create-dashboard-superuser
jupytext--to-md: ## convert local ipynb files into md
bin/jupytext --to md work/src/notebook/**/*.ipynb
.PHONY: jupytext--to-md
jupytext--to-ipynb: ## convert remote md files into ipynb
bin/jupytext --to ipynb work/src/notebook/**/*.md
.PHONY: jupytext--to-ipynb
reset-db: ## Reset the PostgreSQL database
$(COMPOSE) stop
$(COMPOSE) down postgresql metabase
$(MAKE) migrate-api
$(MAKE) create-api-superuser
$(MAKE) create-api-test-db
$(MAKE) create-metabase-db
$(MAKE) seed-metabase
$(MAKE) create-prefect-db
$(MAKE) migrate-prefect
$(MAKE) create-dashboard-db
$(MAKE) migrate-dashboard
$(MAKE) create-dashboard-superuser
$(MAKE) seed-dashboard
.PHONY: reset-db
seed-api: ## seed the API database (static data)
seed-api: run
zcat data/irve-statique.json.gz | \
bin/qcc static bulk --chunk-size 1000
.PHONY: seed-api
seed-metabase: ## seed the Metabase server
@echo "Running metabase service …"
@$(COMPOSE_UP) --wait metabase
@echo "Create metabase initial admin user…"
bin/metabase-init
@echo "Create API data source…"
$(COMPOSE_RUN) terraform init
$(COMPOSE_RUN) terraform apply -auto-approve
cat src/metabase/custom.sql | \
bin/psql \
-U qualicharge \
-d metabaseappdb
.PHONY: seed-metabase
seed-oidc: ## seed the OIDC provider
@echo 'Starting OIDC provider…'
@$(COMPOSE_UP) keycloak
@$(COMPOSE_RUN) dockerize -wait http://keycloak:8080 -timeout 60s
@echo 'Seeding OIDC client…'
@$(COMPOSE) exec keycloak /usr/local/bin/kc-init
.PHONY: seed-oidc
seed-dashboard: ## seed dashboard
@echo "Running dashboard service database engine…"
@$(COMPOSE_UP) --wait postgresql
@echo "Seeding dashboard…"#
@bin/manage loaddata dashboard/fixtures/dsfr_fixtures.json
.PHONY: seed-dashboard
# -- API
lint: ## lint all sources
lint: \
lint-api \
lint-client \
lint-prefect \
lint-dashboard
.PHONY: lint
lint-api: ## lint api python sources
lint-api: \
lint-api-black \
lint-api-ruff \
lint-api-mypy
.PHONY: lint-api
lint-client: ## lint client python sources
lint-client: \
lint-client-black \
lint-client-ruff \
lint-client-mypy
.PHONY: lint-client
lint-prefect: ## lint prefect python sources
lint-prefect: \
lint-prefect-black \
lint-prefect-ruff \
lint-prefect-mypy
.PHONY: lint-prefect
lint-dashboard: ## lint dashboard python sources
lint-dashboard: \
lint-dashboard-black \
lint-dashboard-ruff \
lint-dashboard-mypy \
lint-dashboard-djlint
.PHONY: lint-dashboard
lint-api-black: ## lint api python sources with black
@echo 'lint:black started…'
@$(COMPOSE_RUN_API_PIPENV) black qualicharge tests
.PHONY: lint-api-black
lint-api-ruff: ## lint api python sources with ruff
@echo 'lint:ruff started…'
@$(COMPOSE_RUN_API_PIPENV) ruff check qualicharge tests
.PHONY: lint-api-ruff
lint-api-ruff-fix: ## lint and fix api python sources with ruff
@echo 'lint:ruff-fix started…'
@$(COMPOSE_RUN_API_PIPENV) ruff check --fix qualicharge tests
.PHONY: lint-api-ruff-fix
lint-api-mypy: ## lint api python sources with mypy
@echo 'lint:mypy started…'
@$(COMPOSE_RUN_API_PIPENV) mypy qualicharge tests
.PHONY: lint-api-mypy
lint-client-black: ## lint api python sources with black
@echo 'lint:black started…'
@$(COMPOSE_RUN_CLIENT) black qcc tests
.PHONY: lint-client-black
lint-client-ruff: ## lint api python sources with ruff
@echo 'lint:ruff started…'
@$(COMPOSE_RUN_CLIENT) ruff check qcc tests
.PHONY: lint-client-ruff
lint-client-ruff-fix: ## lint and fix api python sources with ruff
@echo 'lint:ruff-fix started…'
@$(COMPOSE_RUN_CLIENT) ruff check --fix qcc tests
.PHONY: lint-client-ruff-fix
lint-client-mypy: ## lint api python sources with mypy
@echo 'lint:mypy started…'
@$(COMPOSE_RUN_CLIENT) mypy qcc tests
.PHONY: lint-client-mypy
lint-prefect-black: ## lint prefect python sources with black
@echo 'lint:black started…'
@$(COMPOSE_RUN_PREFECT_PIPENV) black indicators tests
.PHONY: lint-prefect-black
lint-prefect-ruff: ## lint prefect python sources with ruff
@echo 'lint:ruff started…'
@$(COMPOSE_RUN_PREFECT_PIPENV) ruff check indicators tests
.PHONY: lint-prefect-ruff
lint-prefect-ruff-fix: ## lint and fix prefect python sources with ruff
@echo 'lint:ruff-fix started…'
@$(COMPOSE_RUN_PREFECT_PIPENV) ruff check --fix indicators tests
.PHONY: lint-prefect-ruff-fix
lint-prefect-mypy: ## lint prefect python sources with mypy
@echo 'lint:mypy started…'
@$(COMPOSE_RUN_PREFECT_PIPENV) mypy indicators tests
.PHONY: lint-prefect-mypy
lint-dashboard-black: ## lint dashboard python sources with black
@echo 'lint:black dashboard started…'
@$(COMPOSE_RUN_DASHBOARD_PIPENV) black dashboard apps tests
.PHONY: lint-dashboard-black
lint-dashboard-ruff: ## lint dashboard python sources with ruff
@echo 'lint:ruff dashboard started…'
@$(COMPOSE_RUN_DASHBOARD_PIPENV) ruff check dashboard apps tests
.PHONY: lint-dashboard-ruff
lint-dashboard-ruff-fix: ## lint and fix dashboard python sources with ruff
@echo 'lint:ruff-fix dashboard started…'
@$(COMPOSE_RUN_DASHBOARD_PIPENV) ruff check --fix dashboard apps tests
.PHONY: lint-dashboard-ruff-fix
lint-dashboard-mypy: ## lint dashboard python sources with mypy
@echo 'lint:mypy dashboard started…'
@$(COMPOSE_RUN_DASHBOARD_PIPENV) mypy dashboard apps tests
.PHONY: lint-dashboard-mypy
lint-dashboard-djlint: ## lint dashboard html sources with djlint
@echo 'lint:djlint dashboard started…'
@$(COMPOSE_RUN_DASHBOARD_PIPENV) djlint -
.PHONY: lint-dashboard-djlint
lint-dashboard-djlint-reformat: ## lint and reformat dashboard html sources with djlint
@echo 'lint:djlint-reformat dashboard started…'
@$(COMPOSE_RUN_DASHBOARD_PIPENV) djlint - --reformat
.PHONY: lint-dashboard-djlint-reformat
test: ## run all services tests
test: \
test-api \
test-client \
test-prefect \
test-dashboard
.PHONY: test
test-api: ## run API tests
SERVICE=api bin/pytest
.PHONY: test-api
test-client: ## run client tests
SERVICE=client bin/pytest
.PHONY: test-client
test-prefect: ## run prefect tests
SERVICE=prefect-test bin/pytest
.PHONY: test-prefect
test-dashboard: ## run dashboard tests
@echo "Run dashboard tests…"
SERVICE=dashboard bin/pytest
.PHONY: test-dashboard
# -- 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