-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
188 lines (143 loc) · 5.4 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
DOCKER_COMPOSE = docker compose -f docker-compose.yml -f docker-compose.override.yml
FLASK_RUN = $(DOCKER_COMPOSE) run --rm flask
DOCKER_REGISTRY = ghcr.io
# Separate environment for running integration tests
TESTING_COMPOSE_PROJECT_NAME = park-api_integrationtests
TESTING_DOCKER_COMPOSE = $(DOCKER_COMPOSE) -p $(TESTING_COMPOSE_PROJECT_NAME)
# Include local file with custom variables/rules (only if the file exists)
-include Makefile.env
# Default target when running `make`
.PHONY: all
all: docker-up
# Configuration
# -------------
.PHONY: config
config: .env config.yaml
# Create .env file to set the UID/GID for the docker containers to run as to the current user
.env:
echo "DOCKER_LOCAL_USER=$(shell id -u):$(shell id -g)" >> .env
# Create config file from config_dist_dev.yaml if it does not exist yet
config.yaml:
cp config_dist_dev.yaml config.yaml
# Container management
# --------------------
.PHONY: first-start
first-start: config docker-login docker-build apply-migrations
$(DOCKER_COMPOSE) down
@echo
@echo 'Database is all set up! \o/'
@echo 'You can now start the project with "make docker-up"'
# Login to Docker registry
.PHONY: docker-login
docker-login:
docker login $(DOCKER_REGISTRY)
# Builds and starts all docker containers
.PHONY: docker-up
docker-up: config docker-build
$(DOCKER_COMPOSE) up $(SERVICE)
# Start containers in background (or recreate containers while they are running attached to another terminal)
.PHONY: docker-up-detached
docker-up-detached: config docker-build
$(DOCKER_COMPOSE) up --detach $(SERVICE)
.PHONY: docker-down
docker-down: .env
$(DOCKER_COMPOSE) down --remove-orphans
$(TESTING_DOCKER_COMPOSE) down --remove-orphans
.PHONY: docker-testing-down
docker-testing-down: .env
$(TESTING_DOCKER_COMPOSE) down --remove-orphans --volumes
# Restart all containers (default) or only the containers specified by SERVICE (e.g. `make docker-restart SERVICE=flask`)
.PHONY: docker-restart
docker-restart: .env
$(DOCKER_COMPOSE) restart $(SERVICE)
# Tear down all containers and delete all volumes
.PHONY: docker-purge
docker-purge: .env
$(DOCKER_COMPOSE) down --remove-orphans --volumes
$(TESTING_DOCKER_COMPOSE) down --remove-orphans --volumes
# Build the Docker image for the flask service
.PHONY: docker-build
docker-build: .env
$(DOCKER_COMPOSE) build flask
# Force a rebuild of all images (including pulling the base images)
.PHONY: docker-rebuild
docker-rebuild: .env
$(DOCKER_COMPOSE) build --no-cache --pull flask
# Pull all images except for locally built images
.PHONY: docker-pull
docker-pull: .env
$(DOCKER_COMPOSE) pull --ignore-buildable
# Show application logs, optionally with `make docker-logs SERVICE=flask` only for specified containers
.PHONY: docker-logs
docker-logs: .env
$(DOCKER_COMPOSE) logs -f $(SERVICE)
# Run arbitrary commands in the flask docker container
.PHONY: docker-run
docker-run: config
@test -n "$(CMD)" || ( echo 'Usage: make docker-run CMD="insert command here"'; exit 1 )
$(FLASK_RUN) $(CMD)
# Start a shell (bash) in the flask docker container
.PHONY: docker-shell
docker-shell: config
$(FLASK_RUN) bash
# Start an interactive Python shell with Flask application context in a docker container
.PHONY: flask-shell
flask-shell: config
$(FLASK_RUN) flask shell
# Database management
# -------------------
# Runs database migrations to upgrade the database to the current version (with "migrate" as an shorter alias)
.PHONY: apply-migrations migrate
migrate: apply-migrations
apply-migrations: config
$(FLASK_RUN) flask db upgrade
# Runs database migrations to DOWNGRADE the database to the previous version
.PHONY: downgrade-migrations
downgrade-migrations: config
$(FLASK_RUN) flask db downgrade
# Auto-generates a new database migration (requires a revision message as MSG)
.PHONY: generate-migration
generate-migration: config
@test -n "$(MSG)" || ( echo 'Usage: make generate-migration MSG="Example revision message"'; exit 1 )
$(FLASK_RUN) flask db migrate -m "$(MSG)"
# Cleanup
# -------
# Clean up "volatile" files (caches, test reports, venv, generated assets, ...)
.PHONY: clean
clean: docker-down
rm -rf node_modules/ logs/ venv/ static/js/ static/webpack-assets.json reports/ .pytest_cache .coverage .npm
# Clean up whole environment (like "clean", but also removes config files and database files)
.PHONY: clean-all
clean-all: docker-purge clean
rm config.yaml .env
# Test suites
# -----------
# Run unit tests only
.PHONY: test
test: test-unit
# Run all test suites (unit and integration tests)
.PHONY: test-all
test-all: test-unit test-integration
# Run unit tests only and generate coverage report in HTML format
.PHONY: test-unit
test-unit: config
$(DOCKER_COMPOSE) run --rm flask python -m pytest tests/unit
$(DOCKER_COMPOSE) down
# Run integration tests in a separate environment
.PHONY: test-integration
test-integration: config
$(TESTING_DOCKER_COMPOSE) run --rm flask python -m pytest tests/integration
$(TESTING_DOCKER_COMPOSE) down
# Open coverage report in browser (determined by BROWSER env variable, defaults to firefox)
.PHONY: open-coverage
open-coverage:
@test -f ./reports/coverage_html/index.html || make test-unit
$(or $(BROWSER),firefox) ./reports/coverage_html/index.html
.PHONY: lint-fix
lint-fix:
$(FLASK_RUN) ruff format ./webapp
$(FLASK_RUN) ruff check --fix ./webapp ./tests ./migrations
.PHONY: lint-check
lint-check:
$(FLASK_RUN) ruff format --check --diff webapp
$(FLASK_RUN) ruff check ./webapp ./tests ./migrations