This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Makefile
361 lines (306 loc) · 11.6 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
.ONESHELL:
.NOTPARALLEL:
OS ?= $(shell uname -s | tr '[:upper:]' '[:lower:]')
SELF ?= $(MAKE)
SHELL = /bin/bash
THIS_FILE := $(lastword $(MAKEFILE_LIST))
THIS_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
## from https://github.com/buildroot/buildroot/blob/master/Makefile
MAKE_VERS := $(MAKE_VERSION)
MAKE_MIN_VERS := 3.81
ifneq ($(firstword $(sort $(MAKE_VERS) $(MAKE_MIN_VERS))),$(MAKE_MIN_VERS))
$(error GNU make >= $(MAKE_MIN_VERS) is required, installed version is $(MAKE_VERS))
endif
BOLD = $(shell tput -Txterm bold)
RED = $(shell tput -Txterm setaf 1)
GREEN = $(shell tput -Txterm setaf 2)
YELLOW = $(shell tput -Txterm setaf 3)
BLUE = $(shell tput -Txterm setaf 4)
RESET = $(shell tput -Txterm sgr0)
SUDO := $(shell command -v /usr/bin/sudo 2>/dev/null)
SED := $(shell command -v /usr/bin/sed 2>/dev/null)
CURL := $(shell command -v /usr/bin/curl 2>/dev/null)
TAR := $(shell command -v /usr/bin/tar 2>/dev/null)
GREP := $(shell command -v /usr/bin/grep 2>/dev/null)
PERL := $(shell command -v /usr/bin/perl 2>/dev/null)
SECURITY := $(shell command -v /usr/bin/security 2>/dev/null)
GIT := $(shell command -v git 2>/dev/null)
DOCKER := $(shell command -v docker 2>/dev/null)
OPENSSL := $(shell command -v openssl 2>/dev/null)
EXECUTABLES = SUDO SED CURL TAR GREP PERL SECURITY GIT DOCKER OPENSSL
K := $(foreach exec,$(EXECUTABLES),\
$(if $($(exec)),OK,$(error "No $(exec) in $$PATH")))
OPENSSL_VERS := $(shell $(OPENSSL) version | $(PERL) -n -e'/^OpenSSL ([0-9.]+)/ && print $$1')
OPENSSL_MIN_VERS := 1.1.1
ifneq ($(firstword $(sort $(OPENSSL_VERS) $(OPENSSL_MIN_VERS))),$(OPENSSL_MIN_VERS))
$(warning OpenSSL >= $(OPENSSL_MIN_VERS) is required, installed version is $(OPENSSL_VERS))
$(warning Run 'brew install openssl@1.1' and follow instructions to update $$PATH)
$(error Minimum requirements not met)
endif
define assert-set
@[ -n "$($1)" ] || (echo "$(1) not defined in $(@)"; exit 1)
endef
define assert-unset
@[ -z "$($1)" ] || (echo "$(1) should not be defined in $(@)"; exit 1)
endef
ifneq ($(shell test -e .env && echo -n HASENV),HASENV)
$(error .env file is missing, copy .env.sample to .env, modify, and try again)
endif
MAKEENV := $(shell bash -c "$(GREP) -vE '^\#' $(THIS_DIR)/.env | sed -e 's/=/?=/' -e '/^\$$/d' -e 's/^/export /;' > $(THIS_DIR)/.env.make")
include .env.make
DOMAIN := $(if $(DOMAIN),$(DOMAIN),$(VIPGO_DOMAIN))
CACHE_DIR := $(HOME)/.local/share/vip-gold
default:: help
@exit 0
.PHONY: help
help:
@echo "$(BOLD)usage:$(RESET)"
@echo " make <command>"
@echo ""
@echo "$(BOLD)commands:$(RESET)"
@echo " init - sets up environment requirements/dependencies"
@echo " dev/up - alias of docker compose up -d"
@echo " dev/down - alias of docker compose down"
@echo " dev/upgrade - update all container images and provided mu-plugins"
@echo " dev/restart - stop and start the environment"
@echo ""
@echo " dev/reset - $(YELLOW)WARNING!$(RESET) removes app/ and data/"
@echo " dev/xdebug/on - enable xdebug support"
@echo " dev/xdebug/off - disable xdebug support"
@echo ""
@echo " hosts/add - add VIPGO_DOMAIN to /etc/hosts"
@echo " hosts/remove - remove VIPGO_DOMAIN from /etc/hosts"
@echo ""
@echo " tls/ca - generate certificate authority, add to keychain"
@echo " tls/domain - generate cert + key"
@echo " defaults to VIPGO_DOMAIN, override with DOMAIN"
@echo " ex: DOMAIN=differenttld.com make tls/domain"
@echo " tls/reset - remove certificate authority and VIPGO_DOMAIN cert+key"
@echo ""
@echo " db/import - imports file.sql into mariadb instance"
@echo " ex: SQL=file.sql make db-import"
@echo " db/export - exports mariadb DB into VIPGO_WP_DB_NAME.sql"
@echo ""
.PHONY: init
init: $(DOCKER)
@echo "$(BLUE)[+] Initialize Development Environment$(RESET)"
@$(SELF) -f $(THIS_FILE) -s init/wordpress
@$(SELF) -f $(THIS_FILE) -s init/nginx
@$(SELF) -f $(THIS_FILE) -s init/mariadb
@echo "$(GREEN)[+] DONE! run 'make dev/up' to start the environment$(RESET)"
.PHONY: init/mariadb
init/mariadb: $(DOCKER) | data/mariadb
@echo "$(BLUE) ⠿ Initialized: data/mariadb$(RESET)"
data/mariadb:
@echo "[+] Initialize: data/mariadb"
@mkdir -p data/mariadb
$(DOCKER) compose up -d mariadb
@iter=1; \
max_wait=15; \
while [[ $$iter != $$max_wait ]]; \
do \
res=$$($(DOCKER) compose logs mariadb 2>/dev/null | $(GREP) -cE "mysqld: ready for connections"); \
if [[ "$$res" == "2" ]]; then \
break; \
fi; \
echo " waiting... ($$iter/$$max_wait)"; \
sleep 6; \
iter=$$((iter+1)); \
done; \
if [[ $$iter == $$max_wait ]]; then \
printf "%b" "$(RED)ERROR: mariadb didnt start successfully$(RESET)\n"; \
exit 1; \
fi;
sleep 3;
@$(SELF) -f $(THIS_FILE) -s dev/down
@exit 0
.PHONY: init/nginx
init/nginx: $(DOCKER) conf/nginx/conf.d/upstream-media-host
mkdir -p data/nginx
@echo "$(BLUE) ⠿ Initialized: data/nginx$(RESET)"
conf/nginx/conf.d/upstream-media-host:
@echo 'set $$upstream_media_host "$(VIPGO_UPSTREAM_MEDIA_HOST)";' > conf/nginx/conf.d/upstream-media-host
@echo "$(BLUE) ⠿ Initialized: conf/nginx$(RESET)"
.PHONY: init/wordpress
init/wordpress: $(DOCKER) | app/wp-content $(CACHE_DIR)/mu-plugins
@echo "$(BLUE) ⠿ Initialized: $(CACHE_DIR)/mu-plugins$(RESET)"
@echo "$(BLUE) ⠿ Initialized: app/wp-content$(RESET)"
$(CACHE_DIR)/mu-plugins: $(TAR) | $(CACHE_DIR)/vip-go-mu-plugins.tar.gz
@echo "[+] Initialize: $(CACHE_DIR)/mu-plugins"
mkdir -p $(CACHE_DIR)/mu-plugins
$(TAR) -xzvf $(CACHE_DIR)/vip-go-mu-plugins.tar.gz --strip-components=1 -C $(CACHE_DIR)/mu-plugins
app/wp-content: $(DOCKER)
@echo "[+] Initialize: app/wp-content"
$(GIT) clone "$(VIPGO_REPOSITORY)" app/wp-content
mkdir -p app/wp-content/{client-mu-plugins,images,languages,plugins,themes,vip-config,uploads}
$(CACHE_DIR)/vip-go-mu-plugins.tar.gz: $(CURL)
$(CURL) -sL --fail -o $(CACHE_DIR)/vip-go-mu-plugins.tar.gz $(VIPGO_MUPLUGINS)
.PHONY: dev/upgrade
dev/upgrade: $(DOCKER)
@$(SELF) -f $(THIS_FILE) -s dev/down
@echo "[+] Update: GoLD"
@$(GIT) pull
@echo "[+] Update: GoLD Docker Images"
@$(DOCKER) compose pull -q
@rm -rf $(CACHE_DIR)/vip-go-mu-plugins.tar.gz $(CACHE_DIR)/mu-plugins
@$(SELF) -f $(THIS_FILE) -s $(CACHE_DIR)/mu-plugins
@$(SELF) -f $(THIS_FILE) -s dev/up
.PHONY: dev/reset
dev/reset: $(DOCKER)
@$(SELF) -f $(THIS_FILE) -s dev/down
@rm -rf data app .env .env.make
@echo "$(BLUE) ⠿ Deleted: app/$(RESET)"
@echo "$(BLUE) ⠿ Deleted: data/$(RESET)"
@echo "$(BLUE) ⠿ Deleted: .env$(RESET)"
@echo "$(YELLOW)[!] To re-initialize:"
@echo " - Run: cp .env.sample .env"
@echo " - Reconfigure .env"
@echo " - Run: make init$(RESET)"
@echo "$(GREEN)[+] DONE!$(RESET)"
.PHONY: dev/up
dev/up: $(DOCKER)
@echo "$(BLUE)[+] Starting Development Environment$(RESET)"
@$(SELF) -f $(THIS_FILE) -s tls/domain
@$(DOCKER) compose up -d
@$(SELF) -f $(THIS_FILE) -s hosts/add
.PHONY: dev/down
dev/down: $(DOCKER)
@echo "$(BLUE)[+] Stopping Development Environment$(RESET)"
@$(DOCKER) compose down
@echo -n "$(BLUE)[+] Removing volume: $(RESET)"
@$(DOCKER) volume rm --force "$$(basename $${PWD})_wp"
@$(SELF) -f $(THIS_FILE) -s hosts/remove
.PHONY: dev/restart
dev/restart: $(DOCKER)
@$(SELF) -f $(THIS_FILE) -s dev/down
@$(SELF) -f $(THIS_FILE) -s dev/up
.PHONY: dev/xdebug/on
dev/xdebug/on: $(DOCKER)
@$(PERL) -pi -e 's/^\;zend_extension/zend_extension/' conf/wordpress/conf.d/docker-php-ext-xdebug.ini
@$(DOCKER) compose restart wordpress
.PHONY: dev/xdebug/off
dev/xdebug/off: $(DOCKER)
@$(PERL) -pi -e 's/^zend_extension/\;zend_extension/' conf/wordpress/conf.d/docker-php-ext-xdebug.ini
@$(DOCKER) compose restart wordpress
.PHONY: hosts/add
hosts/add:
@echo "$(BLUE)[+] Updating /etc/hosts (enter password if prompted)$(RESET)"
@$(GREP) -qE '^127.0.0.1\s+$(VIPGO_DOMAIN)' /etc/hosts \
|| $(SUDO) $(PERL) -i -pe "eof && do{print qq[\$$_\n127.0.0.1 $(VIPGO_DOMAIN)]; exit;}" /etc/hosts
.PHONY: hosts/remove
hosts/remove:
@echo "$(BLUE)[+] Updating /etc/hosts (enter password if prompted)$(RESET)"
@$(GREP) -qvE '^127.0.0.1\s+$(VIPGO_DOMAIN)' /etc/hosts \
&& $(SUDO) $(PERL) -0777 -i -pe "s/\n127.0.0.1\s+$(VIPGO_DOMAIN)\$$//gsm" /etc/hosts
.PHONY: tls/ca
tls/ca: $(SUDO) $(SECURITY) | $(CACHE_DIR)/ca.key $(CACHE_DIR)/ca.crt
@echo "$(BLUE) ⠿ Initialized: $(CACHE_DIR)/ca.key$(RESET)"
@echo "$(BLUE) ⠿ Initialized: $(CACHE_DIR)/ca.crt$(RESET)"
@echo "$(BLUE)[+] Certificate Authority exists in login.keychain-db$(RESET)"
$(CACHE_DIR)/ca.key: $(OPENSSL)
@echo "[+] Initialize: $(@)"
@mkdir -pv $(shell dirname $@)
$(OPENSSL) genrsa -out "$(@)" 2048
$(CACHE_DIR)/ca.crt: $(OPENSSL) | $(CACHE_DIR)/ca.key
@echo "[+] Initialize: $(@)"
$(OPENSSL) req \
-config $${PWD}/conf/nginx/certs/openssl.cnf \
-x509 \
-new \
-nodes \
-key "$(CACHE_DIR)/ca.key" \
-days 3650 \
-out "$(@)" \
-extensions v3_ca \
-subj "/CN=VIP Go Local Development (GoLD)"
-$(SUDO) $(SECURITY) delete-certificate -c "VIP Go Local Development (GoLD)"
@echo
$(SUDO) $(SECURITY) add-trusted-cert \
-d \
-r trustRoot \
-k $(HOME)/Library/Keychains/login.keychain-db \
$(@)
@echo "$(YELLOW)[!] If using Mozilla Firefox:"
@echo " - Navigate to: about:config"
@echo " - Search: security.enterprise_roots.enabled"
@echo " - Double click: security.enterprise_roots.enabled to set to 'true'"
@echo " - Restart: Mozilla Firefox$(RESET)"
@echo "$(GREEN)[+] DONE!$(RESET)"
.PHONY: tls/domain
tls/domain: tls/ca \
conf/nginx/certs/$(DOMAIN).key \
conf/nginx/certs/$(DOMAIN).csr \
conf/nginx/certs/$(DOMAIN).crt
@echo "$(BLUE) ⠿ Initialized: conf/nginx/certs/$(DOMAIN).key$(RESET)"
@echo "$(BLUE) ⠿ Initialized: conf/nginx/certs/$(DOMAIN).csr$(RESET)"
@echo "$(BLUE) ⠿ Initialized: conf/nginx/certs/$(DOMAIN).crt$(RESET)"
@echo "$(BLUE)[+] Generated TLS certificate for $(DOMAIN)$(RESET)"
conf/nginx/certs/$(DOMAIN).key: $(OPENSSL)
@echo "[+] Initialize: $(@)"
$(OPENSSL) genrsa -out $(@) 2048
conf/nginx/certs/$(DOMAIN).csr: $(OPENSSL) | conf/nginx/certs/$(DOMAIN).key
@echo "[+] Initialize: $(@)"
$(OPENSSL) req \
-new \
-key $${PWD}/conf/nginx/certs/$(DOMAIN).key \
-out $(@) \
-subj "/CN=$(DOMAIN)" \
-addext "subjectAltName = DNS:$(DOMAIN)" \
-config $${PWD}/conf/nginx/certs/openssl.cnf
conf/nginx/certs/$(DOMAIN).crt: $(OPENSSL) | conf/nginx/certs/$(DOMAIN).csr
@echo "[+] Initialize: $(@)"
$(OPENSSL) x509 \
-req \
-in conf/nginx/certs/$(DOMAIN).csr \
-CA $(CACHE_DIR)/ca.crt \
-CAkey $(CACHE_DIR)/ca.key \
-CAcreateserial \
-out $(@) \
-days 365 \
-extensions v3_req \
-extensions SAN \
-extfile <( \
cat $${PWD}/conf/nginx/certs/openssl.cnf \
<(printf "\n[SAN]\nsubjectAltName=DNS:$(DOMAIN)") \
)
.PHONY: tls/reset
tls/reset: $(SUDO) $(SECURITY)
@$(SELF) -f $(THIS_FILE) -s dev/down
@rm -fv \
$(CACHE_DIR)/ca.key \
$(CACHE_DIR)/ca.crt \
$${PWD}/conf/nginx/certs/$(DOMAIN).key \
$${PWD}/conf/nginx/certs/$(DOMAIN).csr \
$${PWD}/conf/nginx/certs/$(DOMAIN).crt
-$(SUDO) $(SECURITY) delete-certificate -c "VIP Go Local Development (GoLD)"
@echo
@echo "$(GREEN)[+] DONE!$(RESET)"
.PHONY: db/import
db/import: $(DOCKER)
$(call assert-set,SQL)
@if [ ! -f $(SQL) ]; then \
echo "$(SQL) doesn't exist!"; \
exit 1; \
fi;
@echo "[+] Importing: $(SQL)"
@$(DOCKER) compose exec \
--no-TTY \
mariadb \
mysql \
-uroot \
-p$(VIPGO_MYSQL_ROOT_PASSWORD)\
$(VIPGO_WP_DB_NAME) \
< $(SQL)
@echo "$(GREEN)[+] DONE!$(RESET)"
.PHONY: db/export
db/export: $(DOCKER)
@echo "[+] Exporting: $(VIPGO_WP_DB_NAME).sql"
@$(DOCKER) compose exec \
mariadb \
mysqldump \
--opt \
-uroot \
-p$(VIPGO_MYSQL_ROOT_PASSWORD)\
$(VIPGO_WP_DB_NAME) \
> $(VIPGO_WP_DB_NAME).sql
@echo "$(GREEN)[+] DONE!$(RESET)"