-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMakefile
69 lines (48 loc) · 2.38 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
.PHONY: help
.DEFAULT_GOAL := help
SHELL := /bin/bash
DOCKER_COMPOSE_DEV=docker-compose-local.yml
DOCKER_COMPOSE_DEV_SSL=docker-compose-local-ssl.yml
DOCKER_COMPOSE_FLAGS := -f $(DOCKER_COMPOSE_DEV)
ifdef SSL
# If SSL is set, append the SSL compose file
DOCKER_COMPOSE_FLAGS += -f $(DOCKER_COMPOSE_DEV_SSL)
endif
DB_CONTAINER = postgres
help: # http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
# ===================================================
# DEV ENVIRONMENT COMMANDS
# ===================================================
start: ## Create and start containers.
docker compose $(DOCKER_COMPOSE_FLAGS) up -d --force-recreate
restart: ## Restart service containers.
docker compose $(DOCKER_COMPOSE_FLAGS) restart
stop: ## Stop all containers.
docker compose $(DOCKER_COMPOSE_FLAGS) stop
down: ## Stop all containers.
docker compose $(DOCKER_COMPOSE_FLAGS) down
ps: ## List containers.
docker compose $(DOCKER_COMPOSE_FLAGS) ps
kill: # Force stop of containers.
docker compose $(DOCKER_COMPOSE_FLAGS) kill
logs: ## Show all containers logs.
docker compose $(DOCKER_COMPOSE_FLAGS) logs -f
shell: ## Run shell inside django container.
docker compose $(DOCKER_COMPOSE_FLAGS) run --rm django /bin/bash
psql: ## Start postgres command-line client.
docker compose $(DOCKER_COMPOSE_FLAGS) run --rm django python3 manage.py dbshell
db_restore: ## Restore data to postgres
@BACKUP_FILE_PATH=$(shell readlink -f $(file)); \
docker compose $(DOCKER_COMPOSE_FLAGS) cp $$BACKUP_FILE_PATH $(DB_CONTAINER):/backup.dump
CPU_CORES=$$(nproc); \
PERCENTAGE=90; \
NUM_JOBS=$$(echo "scale=0; if ($$CPU_CORES * $$PERCENTAGE / 100 < 1) 1 else $$CPU_CORES * $$PERCENTAGE / 100" | bc -l); \
NUM_JOBS=$$(printf "%.0f" $$NUM_JOBS); \
docker compose $(DOCKER_COMPOSE_FLAGS) exec -T $(DB_CONTAINER) bash -c "while ! pg_isready -U postgres -d postgres; do sleep 1; done && pg_restore -Fc -U postgres -d postgres -j $$NUM_JOBS /backup.dump" || true
docker compose $(DOCKER_COMPOSE_FLAGS) exec -T $(DB_CONTAINER) rm /backup.dump
clean_data: ## Remove any data
docker compose $(DOCKER_COMPOSE_FLAGS) down $(DB_CONTAINER) --volumes
clean_docker: ## Remove all container and images.
docker rm $(docker ps -a -q)
docker rmi $(docker image ls -q)