-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
156 lines (129 loc) · 5.68 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
#Variables
PYTHON_PATH_ENV_APP=$(shell which python)
POETRY = poetry
SRC_DIR = src
TEST_DIR = tests
POETRY_RUN = poetry run
DOCKER_TARGET ?= development
ifeq ($(DOCKER_TARGET), development)
APP_DOCKER_COMMAND='./wait-for-mysql.sh python manage.py runserver 0.0.0.0:8000'
else
APP_DOCKER_COMMAND='gunicorn --workers 3 -c /usr/local/etc/gunicorn/app.py nau_financial_manager.wsgi:application'
endif
# or use in future the 'pytest' directly
TEST_CMD = $(POETRY_RUN) python manage.py test --settings nau_financial_manager.test
TEST_MYSQL_CMD = $(POETRY_RUN) python manage.py test --settings nau_financial_manager.test_mysql
# TEST_CMD = $(POETRY_RUN) pytest
LINT_CMD = $(POETRY_RUN) black .
PRE_COMMIT = $(POETRY_RUN) pre-commit run --all-files
RUN_CMD = $(POETRY_RUN) python manage.py runserver --settings=nau_financial_manager.settings
CREATE_TOKEN = $(POETRY_RUN) python manage.py drf_create_token admin
FLUSH_DB = $(POETRY_RUN) python manage.py flush
POPULATE_DB = $(POETRY_RUN) python apps/util/populate_db.py
RESET_MIGRATIONS = find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
MAKE_MIGRATIONS = $(POETRY_RUN) python manage.py makemigrations --settings=nau_financial_manager.settings
MIGRATE = $(POETRY_RUN) python manage.py migrate --settings=nau_financial_manager.settings
COMPOSE_FILE := docker/docker-compose-dependencies.yml
RUN_APP ?= true
ifeq ($(RUN_APP), true)
COMPOSE_FILE := $(COMPOSE_FILE):docker/docker-compose-app.yml
endif
DOCKER_COMPOSE = APP_DOCKER_COMMAND=$(APP_DOCKER_COMMAND) DOCKER_TARGET=$(DOCKER_TARGET) COMPOSE_FILE=$(COMPOSE_FILE) docker compose
RUN_DOCKER_DEV = $(DOCKER_COMPOSE) up -d --remove-orphans
STOP_DOCKER_DEV = $(DOCKER_COMPOSE) down
KILL_DOCKER_DEV = $(DOCKER_COMPOSE) kill
BUILD_DOCKER_DEV = $(DOCKER_COMPOSE) build
LOGS_DOCKER_DEV = $(DOCKER_COMPOSE) logs
PRUNE_DOCKER = docker system prune -af
CREATESUPERUSER = $(POETRY_RUN) python manage.py add_superuser --no-input --settings=nau_financial_manager.settings
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
.DEFAULT_GOAL := help
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
test: ## run tests, all or a specific test, example: 'make test apps.billing.tests.test_receipt_host_service' or 'pytest apps/billing/tests/test_receipt_host_service.py -k test_get_document_transaction_not_found'
@args="$(filter-out $@,$(MAKECMDGOALS))" && $(TEST_CMD) $${args:-${1}}
.PHONY: test
test-mysql: ## run tests, all or a specific test, example: 'make test apps.billing.tests.test_receipt_host_service' or 'pytest apps/billing/tests/test_receipt_host_service.py -k test_get_document_transaction_not_found'
@args="$(filter-out $@,$(MAKECMDGOALS))" && $(TEST_MYSQL_CMD) $${args:-${1}}
.PHONY: test-mysql
lint: ## use black to format code
$(LINT_CMD)
.PHONY: lint
pre-commit: ## use pre-commit to check best practices
$(PRE_COMMIT)
.PHONY: pre-commit
run: ## run DB's inside docker and run django server in your host
RUN_APP=false make run-docker
$(RUN_CMD)
.PHONY: run
run-app: ## run django server in your host
$(RUN_DOCKER_DEV)
$(RUN_CMD)
.PHONY: run-app
populate: ## populate the database initially with fake data
$(POPULATE_DB)
.PHONY: populate
reset-migrations: ## reset all migrations from all apps
$(RESET_MIGRATIONS)
.PHONY: reset-migrations
migrations: ## create migrations (app is an option parameter | make migrations {app_name})
@args="$(filter-out $@,$(MAKECMDGOALS))" && $(MAKE_MIGRATIONS) $${args:-${1}}
.PHONY: migrations
migrate: ## apply all available migrations
$(MIGRATE)
.PHONY: migrate
flush: ## delete all data from database
$(FLUSH_DB) --noinput
.PHONY: flush
superuser: ## create django super user admin (username and password are option parameters | make superuser {username} {password})
@args="$(filter-out $@,$(MAKECMDGOALS))" && $(CREATESUPERUSER) $${args:+--username=$${args%% *} --password=$${args##* }}
.PHONY: superuser
run-docker: ## run django server in docker in dev mode
$(BUILD_DOCKER_DEV)
$(RUN_DOCKER_DEV)
$(MIGRATE)
ifeq ($(RUN_APP), true)
@echo "The app should be running on 8000 port and you can access it from nginx on http://localhost:8081"
endif
.PHONY: run-docker
stop-docker: ## stop docker containers
$(STOP_DOCKER_DEV)
.PHONY: stop
kill: ## stop django server in your host
killall manage.py || echo "Couldn't find the app to kill"
.PHONY: kill
kill-docker: ## stop django server in docker in dev mode
$(KILL_DOCKER_DEV)
.PHONY: kill-docker
stop: | stop-docker kill kill-docker ## stop everything
@echo "Everything stopped or killed"
.PHONY: stop
hr-docker: ## remake complete docker environment (destroy dockers, prune docker, create dockers, migrate, superuser, populate)
$(MAKE) kill-docker
$(PRUNE_DOCKER)
$(RUN_DOCKER_DEV)
@echo "Waiting for MySQL server to start..."
docker logs -f nau-database-mysql 2>&1 | grep -q "/usr/sbin/mysqld: ready for connections. Version: '8.1.0' socket: '/var/run/mysqld/mysqld.sock'" && sleep 15 && echo "MySQL server is ready"
$(MAKE) migrate
$(MAKE) superuser
$(MAKE) populate
.PHONY: hr-docker
create-token: ## create token for admin user
$(CREATE_TOKEN)
.PHONY: create-token
install-poetry: ## Install Poetry
@echo "Installing Poetry..."
@curl -sSL https://install.python-poetry.org | python -
.PHONY: install-poetry
install-packages: ## Install all project dependencies
@echo "Installing project dependencies..."
@$(POETRY) install
.PHONY: install-packages
logs: ## display docker app logs (follow mode)
@$(LOGS_DOCKER_DEV) -f nau-financial-app
.PHONY: logs
run-database: ## Run database in docker
@echo "Running database..."
@$(DOCKER_COMPOSE) -f docker/docker-compose-dependencies.yml up -d database-mysql
.PHONY: run-database