-
Notifications
You must be signed in to change notification settings - Fork 8
/
Makefile.app
229 lines (191 loc) · 5.95 KB
/
Makefile.app
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# dcape application Makefile
# included by separate dcape app
SHELL = /bin/bash
CFG ?= .env
DOT := .
DASH := -
APP_NAME ?= $(lastword $(subst /, ,$(PWD)))
#- Site host
APP_SITE ?= $(APP_NAME).$(DCAPE_DOMAIN)
#- Unique traefik router name
#- Container name prefix
#- Value is optional, derived from APP_SITE if empty
APP_TAG ?= $(subst $(DOT),$(DASH),$(APP_SITE))
#- Enable tls in traefik
#- Values: [false]|true
USE_TLS ?= false
#- Attach database
#- Values: [no]|yes
USE_DB ?= no
#- Add user account data to config
ADD_USER ?= no
#- dc root
DCAPE_ROOT ?= /opt/dcape
#- dcape stack traefik tag
DCAPE_TAG ?= dcape
#- dcape stack network
DCAPE_NET ?= $(DCAPE_TAG)
# Docker compose project name (container name prefix)
PROJECT_NAME ?= $(APP_TAG)
# arg for --profile
DC_PROFILE ?=
# add DCAPE_DC_YML
USE_DCAPE_DC ?= yes
# dcape-app base docker-compose.yml
DCAPE_DC_YML ?= $(DCAPE_ROOT)/docker-compose.app.yml
# config added unconditionally
DCAPE_APP_DC_YML ?= docker-compose.yml
# DB(Postgredsql) container
DB_CONTAINER ?= $(DCAPE_TAG)-db-1
all: help
DCAPE_ROOT ?= $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
-include $(DCAPE_ROOT)/.dcape.env
include $(DCAPE_ROOT)/Makefile.common
# ------------------------------------------------------------------------------
# used in URL generation
ifeq ($(USE_TLS),false)
HTTP_PROTO ?= http
else
HTTP_PROTO ?= https
endif
# ------------------------------------------------------------------------------
## Docker operations
#:
## (re)start container(s)
up: CMD=up -d
up: dc
## restart container
reup: CMD=up --force-recreate -d
reup: dc
## stop (and remove) container(s)
down: CMD=rm -f -s
down: dc
#down: CMD=down
## Build docker image
docker-build: CMD=build --no-cache app
docker-build: dc
## Remove docker image & temp files
docker-clean:
[ "$$(docker images -q $(DC_IMAGE) 2> /dev/null)" = "" ] || docker rmi $(DC_IMAGE)
dc:
@[ "$(USE_DCAPE_DC)" != yes ] || args="-f $(DCAPE_DC_YML)" ; \
[ "$(DC_PROFILE)" == "" ] || args="$$args --profile $(DC_PROFILE)" ; \
docker compose $$args -f $(DCAPE_APP_DC_YML) \
-p $(PROJECT_NAME) --project-directory $$PWD \
$(CMD)
# ------------------------------------------------------------------------------
## DB operations
#:
## create database and user
db-create:
ifeq ($(USE_DB),yes)
@echo "*** $@ ***" ; \
$(MAKE) -s .lib-db-create
else
@echo "Target '$@' is disabled in app config"
endif
## drop database and user
db-drop:
ifeq ($(USE_DB),yes)
@echo "*** $@ ***"
$(MAKE) -s .lib-db-drop
else
@echo "Target '$@' is disabled in app config"
endif
db-cr-ext:
ifeq ($(USE_DB),yes)
@echo "*** $@ ***"
$(MAKE) -s .lib-db-cr-ext
else
@echo "Target '$@' is disabled in app config"
endif
db-grant:
ifeq ($(USE_DB),yes)
@echo "*** $@ ***"
$(MAKE) -s .lib-db-grant
else
@echo "Target '$@' is disabled in app config"
endif
## exec psql inside db container
psql: docker-wait
ifeq ($(USE_DB),yes)
@echo "*** $@ ***"
@docker exec -it $${DB_CONTAINER:?Must be set} psql -d $$PGDATABASE -U $$PGUSER
else
@echo "Target '$@' is disabled in app config"
endif
## run local psql
## (Add DB_PORT_LOCAL to .env before use)
psql-local:
ifeq ($(USE_DB),yes)
@echo "*** $@ ***"
psql -p $(DB_PORT_LOCAL)
# $(MAKE) -s .lib-db-psql
else
@echo "Target '$@' is disabled in app config"
endif
#
# ------------------------------------------------------------------------------
# CI/CD operations
# run app by CICD
# use inside .woodpecker.yml only
.default-deploy: .config-link
@echo "*** $@ ***" ; \
[ "$$USE_DB" != "yes" ] || $(MAKE) -s db-create ; \
$(MAKE) -s .up
# internal target, do not used
.deploy-vars:
@echo "DCAPE_ROOT_BASE=$${DCAPE_ROOT_BASE}"
@echo "DCAPE_ROOT_LOCAL=$${DCAPE_ROOT_LOCAL}"
@echo "DCAPE_TAG:$${DCAPE_TAG}"
@echo "DCAPE_NET:$${DCAPE_NET}"
@echo "DCAPE_ROOT:$${DCAPE_ROOT}"
@echo "DCAPE_DOMAIN:$${DCAPE_DOMAIN}"
@echo "DCAPE_COMPOSE:$${DCAPE_COMPOSE}"
@echo "DCAPE_SCM:$${DCAPE_SCM}"
# internal target for .default-deploy
.up: KEEP_ROOT = $(findstring $(APP_ROOT_OPTS),keep)
.up:
@if [ ! -z "$$PERSIST_FILES" ] ; then \
echo "Got persist ($$PERSIST_FILES) for $$APP_ROOT.." ; \
root=$$(grep APP_ROOT= $(CFG)) ; rootvar=$${root#*=} ; myroot=$${rootvar#$(DCAPE_ROOT_BASE)/} ; \
dir=$${DCAPE_ROOT_LOCAL}/$$myroot ; \
echo "Local dir: $$dir" >&2 ; \
if [ -d $$dir ] && [ -z "$(KEEP_ROOT)" ]; then \
echo -n "Clean dir.. " >&2 ; \
rm -rf $$dir ; \
fi ; \
if [ ! -d $$dir ]; then \
echo -n "Create dir.. " >&2 ; \
mkdir -p $$dir ; \
fi ; \
cp -rf $$PERSIST_FILES $$dir ; \
fi ; \
echo "Starting.. " >&2 ; \
[ "$(USE_DCAPE_DC)" != yes ] || args="-f $(DCAPE_DC_YML)" ; \
[ "$(DC_PROFILE)" == "" ] || args="$$args --profile $(DC_PROFILE)" ; \
docker compose -p $(APP_TAG) --env-file $(CFG) -f $(DCAPE_APP_DC_YML) $$args up -d --force-recreate
# build app by CICD
# use inside .woodpecker.yml only
.build:
[ "$(USE_DCAPE_DC)" != yes ] || args="-f $(DCAPE_DC_YML)" ; \
[ "$(DC_PROFILE)" == "" ] || args="$$args --profile $(DC_PROFILE)" ; \
docker compose -p $(APP_TAG) -f $(DCAPE_APP_DC_YML) $$args build
# setup .env by CICD
# used inside .woodpecker.yml by .default-deploy
.config-link: ENFIST_BRANCH ?= $(CI_COMMIT_BRANCH)
.config-link: ENFIST_TAG ?= $(CI_REPO_OWNER)--$(CI_REPO_NAME)--$(ENFIST_BRANCH)
.config-link: APP_ROOT = $(DCAPE_ROOT_BASE)/$(ENFIST_TAG)
.config-link:
@echo -n "Setup config for $${ENFIST_TAG}... " ; \
if curl -gsS "http://config:8080/rpc/tag_vars?code=$$ENFIST_TAG" | jq -er '.' > $(CFG).temp ; then \
mv $(CFG).temp $(CFG) ; echo "Ok" ; \
else \
rm $(CFG).temp # here will be `null` if tag does not exists ; \
echo "NOT FOUND" ; \
[ -f $(CFG).sample ] || $(MAKE) -s $(CFG).sample ; \
jq -R -sc ". | {\"code\":\"$$ENFIST_TAG.sample\",\"data\":.}" < $(CFG).sample \
| curl -gsd \@- "http://config:8080/rpc/tag_set" | jq '.' ; \
echo "Edit config $$ENFIST_TAG.sample and rename it to $$ENFIST_TAG" ; \
exit 1 ; \
fi