-
Notifications
You must be signed in to change notification settings - Fork 8
/
Makefile.common
300 lines (248 loc) · 10.5 KB
/
Makefile.common
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# dcape application Makefile
# included by Makefile.app and Makefile.dcape
SHELL = /bin/bash
CFG ?= .env
# User who runs DB_INIT_SQL and creates PGDATABASE_EXRA
DB_ADMIN_USER ?= postgres
.PHONY: up reup down dc docker-wait db-create db-drop psql psql-local env-ls env-get env-set help
# ------------------------------------------------------------------------------
# DB vars
PGHOST ?= db
PGDATABASE ?= $(APP_NAME)
PGUSER ?= $(APP_NAME)
PGPASSWORD ?= $(shell openssl rand -hex 16; echo)
PG_DUMP_SOURCE ?=
define CONFIG_DB
# ------------------------------------------------------------------------------
# Database host
PGHOST=$(PGHOST)
# Database name
PGDATABASE=$(PGDATABASE)
# Database user name
PGUSER=$(PGUSER)
# Database user password
PGPASSWORD=$(PGPASSWORD)
# Database dump for import on create
# Used as ${PG_DUMP_SOURCE}.{tar|tgz}
PG_DUMP_SOURCE=$(PG_DUMP_SOURCE)
endef
USER_NAME ?= $(APP_NAME)
USER_EMAIL ?= admin@$(APP_SITE)
USER_PASS ?= $(shell openssl rand -hex 16; echo)
define CONFIG_USER
# ------------------------------------------------------------------------------
# Admin user name
USER_NAME=$(USER_NAME)
# Admin user email
USER_EMAIL=$(USER_EMAIL)
# Admin user password
USER_PASS=$(USER_PASS)
endef
# Wait for postgresql container start
docker-wait:
@echo -n "Checking PG is ready..." ; \
until [[ `docker inspect -f "{{.State.Health.Status}}" $${DB_CONTAINER:?Must be set}` == healthy ]] ; do sleep 1 ; echo -n "." ; done
@echo "Ok"
# Database import script
# DCAPE_DB_DUMP_DEST must be set in pg container
define IMPORT_SCRIPT
[[ "$$DCAPE_DB_DUMP_DEST" ]] || { echo "DCAPE_DB_DUMP_DEST not set. Exiting" ; exit 1 ; } ; \
DB_NAME="$$1" ; DB_USER="$$2" ; DB_PASS="$$3" ; DB_SOURCE="$$4" ; \
dbsrc=$$DCAPE_DB_DUMP_DEST/$$DB_SOURCE ; \
if [ -f $${dbsrc}.tar ] ; then \
echo "Prefer dump file $${dbsrc}.tar found, restoring database..." ; \
PGPASSWORD=$$DB_PASS pg_restore -h localhost -U $$DB_USER -O -d $$DB_NAME $${dbsrc}.tar || exit 1 ; \
elif [ -f $${dbsrc}.tgz ] ; then \
echo "Dump file $${dbsrc}.tgz found, restoring database..." ; \
zcat $${dbsrc}.tgz | PGPASSWORD=$$DB_PASS pg_restore -h localhost -U $$DB_USER -O -Ft -d $$DB_NAME || exit 1 ; \
else \
echo "Dump file $$dbsrc.(tar|tgz) not found" ; \
exit 2 ; \
fi
endef
export IMPORT_SCRIPT
.lib-db-create: docker-wait
@echo "Create db $$PGDATABASE for user $$PGUSER via $$DB_CONTAINER..."
@sql="CREATE USER \"$$PGUSER\" WITH PASSWORD '$$PGPASSWORD'" ; \
docker exec -i $$DB_CONTAINER psql -U postgres -c "$$sql" 2>&1 > .psql.log | grep -v "already exists" > /dev/null || true ; \
cat .psql.log ; \
docker exec -i $$DB_CONTAINER psql -U postgres -c "CREATE DATABASE \"$$PGDATABASE\" OWNER \"$$PGUSER\";" 2>&1 > .psql.log | grep "already exists" > /dev/null || db_created=1 ; \
cat .psql.log ; rm .psql.log ; \
if [ "$$db_created" = "1" ] ; then \
if [ "$$PG_DUMP_SOURCE" ] ; then \
echo "*** db data load" ; \
echo "$$IMPORT_SCRIPT" | docker exec -i $$DB_CONTAINER bash -s - $$PGDATABASE $$PGUSER $$PGPASSWORD $$PG_DUMP_SOURCE \
&& docker exec -i $$DB_CONTAINER psql -U postgres -c "COMMENT ON DATABASE \"$$PGDATABASE\" IS 'SOURCE $$PG_DUMP_SOURCE';" \
|| true ; \
elif [[ "$(DB_INIT_SQL)" && -f "$(DB_INIT_SQL)" ]] ; then \
cat $(DB_INIT_SQL) | docker exec -i $$DB_CONTAINER psql -U $(DB_ADMIN_USER) -d $$PGDATABASE -vPGUSER=$$PGUSER ; \
fi ; \
for pgdb in $$PGDATABASE_EXTRA ; do \
echo "$$pgdb..." ; \
docker exec -i $$DB_CONTAINER psql -U $(DB_ADMIN_USER) -c "CREATE DATABASE \"$$pgdb\" OWNER \"$$PGUSER\";" 2>&1 ; \
done \
fi
.lib-db-cr-ext: docker-wait
@echo "Create PG extension: $(PG_EXTENSION) in db $$PGDATABASE .." ; \
if [[ "$(PG_EXTENSION)" == "" ]] ; then echo "Error: empty name" ; \
else \
docker exec -it $$DB_CONTAINER psql -U postgres -d $$PGDATABASE -c "CREATE EXTENSION $(PG_EXTENSION);" || true ; \
fi
.lib-db-grant: docker-wait
@echo "Grant $(PG_GRANT) to user $$PGUSER .." ; \
if [[ "$(PG_GRANT)" == "" ]] ; then echo "Error: empty name" ; \
else \
docker exec -it $$DB_CONTAINER psql -U postgres -c "GRANT $(PG_GRANT) TO $$PGUSER;" || true ; \
fi
.lib-db-drop: docker-wait
@docker exec -it $$DB_CONTAINER psql -U postgres -c "DROP DATABASE \"$$PGDATABASE\";" || true
@for pgdb in $$PGDATABASE_EXTRA ; do \
echo "$$pgdb..." ; \
docker exec -it $$DB_CONTAINER psql -U postgres -c "DROP DATABASE \"$$pgdb\";" || true ; \
done
@docker exec -it $$DB_CONTAINER psql -U postgres -c "DROP USER \"$$PGUSER\";" || true
.lib-db-psql:
@psql postgresql://$$PGUSER:$$PGPASSWORD@localhost:$(PGPORT)/$$PGDATABASE?sslmode=disable
# ------------------------------------------------------------------------------
## App config storage commands
#:
## get env tag from store, `make env-get TAG=app--config--tag`
env-get:
@[[ "$(TAG)" ]] || { echo "Error: Tag value required" ; exit 1 ;}
@echo "Getting env into $(TAG)"
@docker run --rm -i --network $${DCAPE_NET} $${DCAPE_TAG}-compose curl -gs $${ENFIST_URL}/tag_vars?code=$(TAG) \
| jq -r '.' > $(TAG).env
## list env tags in store
env-ls:
@docker run --rm -i --network $${DCAPE_NET} $${DCAPE_TAG}-compose curl -gs $${ENFIST_URL}/tag \
| jq -r '.[] | .updated_at +" "+.code'
## set env tag in store, `make env-set TAG=app--config--tag`
env-set:
@[[ "$(TAG)" ]] || { echo "Error: Tag value required" ; exit 1 ;}
@echo "Setting $(TAG) from file" \
&& jq -R -sc ". | {\"code\":\"$(TAG)\",\"data\":.}" < $(TAG).env | \
docker run --rm -i --network $${DCAPE_NET} $${DCAPE_TAG}-compose curl -gsd @- $${ENFIST_URL}/tag_set > /dev/null
# ------------------------------------------------------------------------------
## OAuth2 setup
#:
OAUTH_APP_CREATE_URL = $(AUTH_URL)/api/v1/user/applications/oauth2
OAUTH_ORG_CREATE_URL = $(AUTH_URL)/api/v1/admin/users/$(DCAPE_ADMIN_USER)/orgs
define OAUTH_ORG_CREATE
{
"username": "$(ORG)",
"visibility": "limited",
"repo_admin_change_team_access": true
}
endef
define POST_CMD_PRE
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: token $(AUTH_TOKEN)"
endef
define POST_CMD
$(POST_CMD_PRE) \
-H "Sudo: $(DCAPE_ADMIN_ORG)"
endef
ifneq ($(AUTH_TOKEN),)
## create VCS org via VCS API
oauth2-org-create:
@echo -n "create org ($${ORG:?Must be set})... " ; \
[ -z $$DEBUG ] || echo ">>>> POST: $(OAUTH_ORG_CREATE_URL)" ; \
if resp=$$(echo $$OAUTH_ORG_CREATE | curl -gsS -X POST \
$(OAUTH_ORG_CREATE_URL) $(POST_CMD_PRE) -d @-) ; then \
if echo $$resp | jq -re '.id' > /dev/null ; then \
echo "Org $(ORG): Done" ; \
else \
echo -n "ERROR: " ; \
echo $$resp | jq -re '.message' ; \
fi ; \
else false ; fi ; \
[ -z $$DEBUG ] || echo ">>>> RESP: $$resp" ; \
## create OAuth2 app via VCS API
oauth2-app-create:
@echo -n "create OAuth2 app for $(PREFIX)..." ; \
[ -z $$DEBUG ] || echo ">>>> POST: $(OAUTH_APP_CREATE_URL)" ; \
if resp=$$(echo "{\"name\": \"$(HOST)\", \"confidential_client\": true, \"redirect_uris\": [ \"$(DCAPE_SCHEME)://$(HOST)$(URL)\" ]}" | curl -gsS -X POST $(OAUTH_APP_CREATE_URL) $(POST_CMD) -d @-) ; then \
client_id=$$(echo $$resp | jq -r '.client_id') ; \
client_secret=$$(echo $$resp | jq -r '.client_secret') ; \
sed -i "s/\(_CLIENT_ID=\).*/\1$$client_id/ ; s/\(_CLIENT_KEY=\).*/\1$$client_secret/ " $(CFG) ; \
[ ! -f ../../$(CFG) ] || sed -i "s/^\($(PREFIX)_CLIENT_ID=\).*/\1$$client_id/ ; s/^\($(PREFIX)_CLIENT_KEY=\).*/\1$$client_secret/ " ../../$(CFG) ; \
echo " Done" ; \
else false ; fi ; \
[ -z $$DEBUG ] || echo ">>>> RESP: $$resp" ; \
else
oauth2-org-create:
@echo "create org: AUTH_TOKEN must be set"
@echo "Try to refresh it: cd apps/gitea && make token DCAPE_STACK=yes"
oauth2-app-create:
@echo "create OAuth2 app for $(PREFIX): AUTH_TOKEN must be set"
@echo "Try to refresh it: cd apps/gitea && make token DCAPE_STACK=yes"
endif
# ------------------------------------------------------------------------------
## .env operations
#:
# This code generates $(CFG).sample from Makefile vars with previous comment line(s)
# See https://gist.github.com/LeKovr/2697fe02504d7c081b0bf79427c93db6
# Internal: generate config sample data
.env.temp.mk: Makefile
@echo "define CFVAR" > $@
@grep -A 1 -h "^#- " $(MAKEFILE_LIST) | grep -vE "^--" \
| sed -E 's/^([^\n ]+)\ *\??=([^\n]*)$$/\1=$$(\1)\n/ ; s/^(#)-/\1/' >> $@
@echo "endef" >> $@
@echo "export CFVAR" >> $@
ifneq ($(findstring $(MAKECMDGOALS),config config-force config-if config-upgrade $(CFG).sample $(CFG) install ),)
include .env.temp.mk
endif
# Internal: generate config sample
$(CFG).sample: CFDB = $$CONFIG_DB
$(CFG).sample: .env.temp.mk
@echo "# dcape config file, generated by 'make $(MAKECMDGOALS)'" > $@
@echo "# " >> $@
@echo "$$CFVAR" >> $@
@[[ "$(USE_DB)" != "yes" ]] || echo "$$CONFIG_DB" >> $@
@[[ "$(ADD_USER)" != "yes" ]] || echo "$$CONFIG_USER" >> $@
@echo "$$CONFIG_CUSTOM" >> $@
@rm -f $<
## generate sample config
config: $(CFG).sample
## generate sample config and rename it to .env
config-force: config
@mv $(CFG).sample $(CFG)
## generate sample config and rename it to .env if not exists
config-if: config
@[ -f $(CFG) ] || mv $(CFG).sample $(CFG)
# ------------------------------------------------------------------------------
## Other
#:
## Changes from last tag
changelog: RELEASE ?= $(shell git describe --tags --abbrev=0 --always)
changelog:
@echo Changes since $(RELEASE)
@echo
@git log $(RELEASE)..@ --pretty=format:"* %s"
## print config var.
## Sample: make echo-gitea-admin-pass
echo-%:
@x=$@ ; x1=$${x#echo-} ; x2=$${x1//-/_} ; name=$${x2^^} ; val=$${!name} ; \
if [ "$$val" ] ; then echo "$$name: $$val" ; else grep "$$name=" .env ; fi
.stamp:
@echo -n $(shell date --rfc-3339=seconds) ' '
@echo -ne $$_{1..40}'\b='
@echo ' '$(msg)
## delete unused docker images w/o name
## (you should use portainer for this)
clean-noname:
@docker images | grep -q "<none>" && docker rmi $$(docker images | grep "<none>" | awk "{print \$$3}") || echo "None found"
## delete docker dangling volumes
## (you should use portainer for this)
clean-volume:
@vols=$$(docker volume ls -qf dangling=true) ; \
[ $$vols ] && docker volume rm $$vols || echo "None found"
# This code handles group header and target comment with one or two lines only
## list Makefile targets
## (this is default target)
help:
@grep -A 1 -h "^## " $(MAKEFILE_LIST) \
| sed -E 's/^--$$// ; /./{H;$$!d} ; x ; s/^\n## ([^\n]+)\n(## (.+)\n)*(.+):(.*)$$/" " "\4" "\1" "\3"/' \
| sed -E 's/^" " "#" "(.+)" "(.*)"$$/"" "" "" ""\n"\1 \2" "" "" ""/' \
| xargs printf "%s\033[36m%-15s\033[0m %s %s\n"