-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
124 lines (94 loc) · 3.28 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
#
# Создание шаблона БД
# template database Makefile
#
SHELL = /bin/bash
CFG = .env
# on alpine use su-exec
GOSU ?= gosu
DB_NAME ?= tpro-template
DB_LOCALE ?= ru_RU.UTF-8
# dcape container name prefix
DCAPE_PROJECT_NAME ?= dcape
# dcape postgresql container name
DCAPE_DB ?= $(DCAPE_PROJECT_NAME)_db_1
define CONFIG_DEF
# ------------------------------------------------------------------------------
# pg-skel settings
# Template database name
DB_NAME=$(DB_NAME)
# Template database locale
DB_LOCALE=$(DB_LOCALE)
# dcape postgresql container name
DCAPE_DB=$(DCAPE_DB)
endef
export CONFIG_DEF
# ------------------------------------------------------------------------------
# Create script
define EXP_SCRIPT
DB_NAME=$$1 ; \
[[ "$$DB_NAME" ]] || { echo "DB_NAME not set. Exiting" ; exit 1 ; } ; \
DB_LOC=$$2 ; \
[[ "$$DB_LOC" ]] && DB_LOC="-l $$DB_LOC" ; \
SRC=/opt/share/$$DB_NAME ; \
D=/usr/local/share/postgresql ; \
echo "Copy data files to $$D..." ; \
cp -prf $$SRC/tsearch_data/ $$D/ ; \
if psql -U postgres -lqt | cut -d \| -f 1 | grep -qw $$DB_NAME; then \
echo "Database '$$DB_NAME' already exists, exiting" ; exit 0 ; \
fi ; \
echo "Creating $$DB_NAME..." && su -c "createdb -T template0 $$DB_LOC $$DB_NAME" postgres && \
echo "Updating $$DB_NAME extensions..." && psql -d $$DB_NAME -U postgres -f $$SRC/setup.sql ; \
echo "Done"
endef
export EXP_SCRIPT
# ------------------------------------------------------------------------------
-include $(CFG)
export
.PHONY: all $(CFG) start start-hook stop update docker-wait db-create db-drop help
##
## Цели:
##
all: help
# ------------------------------------------------------------------------------
# webhook commands
start: db-create
start-hook: db-create
stop: db-drop
update: db-create
# ------------------------------------------------------------------------------
# docker
# Wait for postgresql container start
docker-wait:
@echo -n "Checking PG is ready..."
@until [[ `docker inspect -f "{{.State.Health.Status}}" $$DCAPE_DB` == healthy ]] ; do sleep 1 ; echo -n "." ; done
@echo "Ok"
# ------------------------------------------------------------------------------
# DB operations
## create db and load sql
db-create: docker-wait
@echo "*** $@ ***" ; \
docker cp ./fts/tsearch_data $$DCAPE_DB:/opt/shared ; \
docker exec -i $$DCAPE_DB shared-sync.sh ; \
[[ "$$DB_LOCALE" ]] && DB_LOCALE="-l $$DB_LOCALE" ; \
echo "Creating $$DB_NAME..." && \
docker exec -i $$DCAPE_DB $(GOSU) postgres createdb -T template0 $$DB_LOCALE $$DB_NAME || db_exists=1 ; \
if [[ ! "$$db_exists" ]] ; then \
cat setup.sql | docker exec -i $$DCAPE_DB psql -U postgres -d $$DB_NAME -f - ; \
fi
## drop database
db-drop: docker-wait
@echo "*** $@ ***"
@docker exec -i $$DCAPE_DB psql -U postgres -c "UPDATE pg_database SET datistemplate = FALSE WHERE datname = '$$DB_NAME';"
@docker exec -i $$DCAPE_DB psql -U postgres -c "DROP DATABASE \"$$DB_NAME\";" || true
# ------------------------------------------------------------------------------
## create initial config
$(CFG):
@echo "$$CONFIG_DEF" > $@
# ------------------------------------------------------------------------------
## List Makefile targets
help:
@grep -A 1 "^##" Makefile | less
##
## Press 'q' for exit
##