-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
100 lines (79 loc) · 2.46 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
MOUNT_POINT ?= /mnt
DOCKER_LOCAL_DATA ?= /srv/off/docker_data
ENV_FILE ?= .env
# for dev we need to align user uid with the one in the container
# this is handled through build args
UID ?= $(shell id -u)
export USER_UID:=${UID}
# prefer to use docker buildkit
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
# we need COMPOSE_PROJECT_NAME for some commands
# take it form env, or from env file
COMPOSE_PROJECT_NAME ?= $(shell grep COMPOSE_PROJECT_NAME ${ENV_FILE} | cut -d '=' -f 2)
DOCKER_COMPOSE=docker-compose --env-file=${ENV_FILE}
#------------#
# Production #
#------------#
test:
echo ${ENV_FILE} ${COMPOSE_PROJECT_NAME}
livecheck:
@echo "🥫 livecheck services…" ; \
exit_code=0; \
services=`${DOCKER_COMPOSE} config --service | tr '\n' ' '`; \
for service in $$services; do \
if [ -z `docker-compose ps -q $$service` ] || [ -z `docker ps -q --no-trunc | grep $$(${DOCKER_COMPOSE} ps -q $$service)` ]; then \
echo "$$service: DOWN"; \
exit_code=1; \
else \
echo "$$service: UP"; \
fi \
done; \
[ $$exit_code -eq 0 ] && echo "Success !"; \
exit $$exit_code;
build:
@echo "🥫 building docker (for dev)"
docker-compose build
# pull images from image repository
pull:
${DOCKER_COMPOSE} pull
up:
ifdef service
${DOCKER_COMPOSE} up -d ${service} 2>&1
else
${DOCKER_COMPOSE} up -d 2>&1
endif
down:
@echo "🥫 Bringing down containers …"
${DOCKER_COMPOSE} down
# Create all external volumes needed for production. Using external volumes is useful to prevent data loss (as they are not deleted when performing docker down -v)
create_external_volumes:
@echo "🥫 Creating external volumes (production only) …"
docker volume create nutripatrol-postgres-data
#-----------#
# Utilities #
#-----------#
guard-%: # guard clause for targets that require an environment variable (usually used as an argument)
@ if [ "${${*}}" = "" ]; then \
echo "Environment variable '$*' is mandatory"; \
echo use "make ${MAKECMDGOALS} $*=you-args"; \
exit 1; \
fi;
#------------#
# Database #
#------------#
# apply DB migrations
migrate-db:
${DOCKER_COMPOSE} run --rm --no-deps api python -m app migrate-db
# add a new DB revision
add-revision: guard-name
${DOCKER_COMPOSE} run --rm --no-deps api python -m app add-revision ${name}
#---------#
# Cleanup #
#---------#
prune:
@echo "🥫 Pruning unused Docker artifacts (save space) …"
docker system prune -af
prune_cache:
@echo "🥫 Pruning Docker builder cache …"
docker builder prune -f