-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
181 lines (134 loc) · 6.64 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
.PHONY: build-docs clean load-fixtures help build no-deps
.DEFAULT_GOAL := help
# Variables ---------------------------------------------------------------------------------------
SELF := $(abspath $(lastword $(MAKEFILE_LIST)))
PWD := $(patsubst %/,%,$(dir $(SELF)))
CWD := /opt/services/djangoapp/src
DOCKER := docker
DOCKER_COMPOSE := docker-compose
COMPOSE_FILE := docker-compose.yml
#ifdef GITLAB_CI
# DJANGO_SETTINGS_MODULE := logan.settings.ci
#else
ifndef HOSTNAME
HOSTNAME := $(shell hostname)
endif
# ifeq ($(HOSTNAME), logan)
# DJANGO_SETTINGS_MODULE := logan.settings.prod
# COMPOSE_FILE := compose-production.yml
# else ifeq ($(HOSTNAME), logan-staging)
# DJANGO_SETTINGS_MODULE := logan.settings.staging
# COMPOSE_FILE := compose-staging.yml
# else
# DJANGO_SETTINGS_MODULE := logan.settings.local
# COMPOSE_FILE := compose-development.yml
# endif
#endif
DJANGO_SETTINGS_MODULE := logan.settings
DOCKER_COMPOSE += -f docker/$(COMPOSE_FILE) --project-directory .
RUN_DJANGOAPP = $(DOCKER_COMPOSE) run $(RUN_OPTS) --rm djangoapp
MAKE_IN = $(RUN_DJANGOAPP) make -f Makefile.in-container
DOCKER0_ADDRESS := $(shell ifconfig | grep docker0 -A 1 | grep -Eo 'inet add?r:[^ ]*' | cut -d: -f2)
export DJANGO_SETTINGS_MODULE
export DOCKER0_ADDRESS
# Information -------------------------------------------------------------------------------------
$(info ----------------------------------------------------)
$(info HOSTNAME = $(HOSTNAME))
$(info COMPOSE_FILE = $(COMPOSE_FILE))
$(info DOCKER0_ADDRESS = $(DOCKER0_ADDRESS))
$(info DJANGO_SETTINGS_MODULE = $(DJANGO_SETTINGS_MODULE))
$(info ----------------------------------------------------)
$(info )
# Building rules ----------------------------------------------------------------------------------
all: build up-no-start build-database load-fixtures build-superuser up ## Build images, create tables, insert fixtures, create super user and start application.
build: ## Build the Docker images.
$(DOCKER_COMPOSE) build
build-django: ## Build the image for Django.
$(DOCKER_COMPOSE) build djangoapp
build-database: ## Migrate the application database (create or alter tables).
@$(MAKE_IN) build-database
build-initial-migrations: no-deps ## Create the initial database migrations.
@$(MAKE_IN) build-initial-migrations
build-superuser: ## Create a super user in the application database.
@$(MAKE_IN) build-superuser
build-docs: ## Build the documentation.
@$(MAKE_IN) build-docs
# Running rules -----------------------------------------------------------------------------------
up: ## Start the application.
$(DOCKER_COMPOSE) up
up-no-start: ## Create the containers without starting the application.
$(DOCKER_COMPOSE) up --no-start
up-daemon: ## Start the application in background.
$(DOCKER_COMPOSE) up -d
down: ## Stop the application.
$(DOCKER_COMPOSE) down
shell: ## Launch a shell in the djangoapp container.
@$(RUN_DJANGOAPP) bash
python: ## Launch a Python shell in the djangoapp container.
@$(MAKE_IN) python
# Updating rules ----------------------------------------------------------------------------------
load-static-files: delete-django-image delete-static-volume build-django ## Collect static files.
load-fixtures: ## Install the fixtures in the database.
@$(MAKE_IN) load-fixtures
update-migrations: ## Update the database migrations.
@$(MAKE_IN) update-migrations
create-data-migration: ## Create a new, empty data migration for app APP.
@$(MAKE_IN) create-data-migration APP=$(APP)
reset-migrations: delete-migrations build-initial-migrations ## Delete all migrations and re-create the initial migrations.
# Backup rules ------------------------------------------------------------------------------------
backup-databases: ## Backup the databases.
@$(MAKE_IN) backup-databases
# Testing / Linting rules -------------------------------------------------------------------------
check: check-safety check-bandit check-pylint check-isort check-docs-links check-docs-spelling ## Run all the check jobs.
test: up-daemon pytest down ## Run the test jobs within the whole container infrastructure.
pytest: ## Run the test suite.
@$(MAKE_IN) pytest
no-deps:
$(eval RUN_OPTS = --no-deps)
check-safety: no-deps ## Run safety on the dependencies.
@$(MAKE_IN) check-safety
check-bandit: no-deps ## Run bandit on the code.
@$(MAKE_IN) check-bandit
check-pylint: no-deps ## Run pylint on the code.
@$(MAKE_IN) check-pylint
isort: no-deps ## Run isort (write files) on the code.
@$(MAKE_IN) isort
check-isort: no-deps ## Run isort check on the code.
@$(MAKE_IN) check-isort
check-docs-links: no-deps ## Check the documentation (spelling, URLs).
@$(MAKE_IN) check-docs-links
check-docs-spelling: no-deps ## Check the documentation (spelling, URLs).
@$(MAKE_IN) check-docs-spelling
# Cleaning rules ----------------------------------------------------------------------------------
delete-database-volumes: down ## Delete the database volumes. Don't do this in production!!
$(DOCKER) volume rm -f data
delete-django-image: down ## Delete the Docker image for the djangoapp service.
$(DOCKER) image rm logan_djangoapp || true
delete-static-volume: down ## Delete the Docker volume storing static assets.
$(DOCKER) volume rm logan_static || true
delete-migrations: no-deps ## Delete the migrations.
@$(MAKE_IN) delete-migrations
clean: ## Remove artifacts (build/docs dirs, coverage files, etc.)
rm -rf build/* 2>/dev/null
rm -rf .cache 2>/dev/null
rm -rf .pytest_cache 2>/dev/null
find . -type f -name "*.pyc" -delete 2>/dev/null
find . -type d -name __pycache__ -delete 2>/dev/null
clean-docker: down ## Stop and remove containers, volumes and networks.
$(DOCKER) system prune -f || true
# Monitoring rules --------------------------------------------------------------------------------
goaccess: ## Run goaccess web interface to monitor NginX logs.
@mkdir nginx-report 2>/dev/null || true
@$(DOCKER) logs -f logan_nginx_1 2>/dev/null | \
goaccess --log-format=COMBINED -o nginx-report/index.html --real-time-html - &
@xdg-open nginx-report/index.html >/dev/null 2>/dev/null &
goaccess-syslog: ## Run goaccess web interface to monitor NginX logs.
@mkdir nginx-report 2>/dev/null || true
@sudo tail -n+1 -f /var/log/syslog | \
grep 'logan-nginx' | \
sed 's/^.*logan-nginx\[[0-9]*\]: //' | \
goaccess --log-format=COMBINED -o nginx-report/index.html --real-time-html - &
@xdg-open nginx-report/index.html >/dev/null 2>/dev/null &
# Usage rules -------------------------------------------------------------------------------------
help: ## Print this help.
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort