forked from greenpeace/planet4-docker-compose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
485 lines (380 loc) · 14.3 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# include optional configuration (used for NRO configuration)
-include Makefile.include
SHELL := /bin/bash
CONTENT_DB_VERSION ?= 0.1.64
CONTENT_IMAGE_VERSION ?= 1-25
SCALE_OPENRESTY ?=1
SCALE_APP ?=1
export SCALE_APP
export SCALE_OPENRESTY
APP_HOSTPATH ?= test
# YAML interprets 'empty' values as 'nil'
ifeq ($(APP_HOSTPATH),<nil>)
# So if APP_HOSTPATH is set, but blank, clean this value
APP_HOSTPATH :=
endif
DOCKER_COMPOSE_FILE ?= docker-compose.yml
DOCKER_COMPOSE_TOOLS_FILE ?= docker-compose.tools.yml
MYSQL_USER := $(shell grep MYSQL_USER db.env | cut -d'=' -f2)
MYSQL_PASS := $(shell grep MYSQL_PASSWORD db.env | cut -d'=' -f2)
ROOT_PASS := $(shell grep MYSQL_ROOT_PASSWORD db.env | cut -d'=' -f2)
WP_ADMIN_USER := admin
WP_ADMIN_PASS := admin
WP_USER ?= $(shell whoami)
WP_USER_EMAIL ?= $(shell git config --get user.email)
ELASTICSEARCH_HOST ?= http://elasticsearch:9200/
PROJECT ?= $(shell basename "$(PWD)" | sed 's/[.-]//g')
export PROJECT
# These vars are read by docker-compose
# See https://docs.docker.com/compose/reference/envvars/
export COMPOSE_FILE = $(PWD)/$(DOCKER_COMPOSE_FILE)
export COMPOSE_PROJECT_NAME=$(PROJECT)
COMPOSE_ENV := COMPOSE_FILE=$(COMPOSE_FILE) COMPOSE_PROJECT_NAME=$(COMPOSE_PROJECT_NAME)
NGINX_HELPER_JSON := $(shell cat options/rt_wp_nginx_helper_options.json)
REWRITE := /%category%/%post_id%/%postname%/
# ============================================================================
CONTENT_PATH := defaultcontent
export CONTENT_PATH
CONTENT_BASE ?= https://storage.googleapis.com/planet4-default-content
CONTENT_DB ?= planet4-defaultcontent_wordpress-v$(CONTENT_DB_VERSION).sql.gz
CONTENT_IMAGES ?= planet4-default-content-$(CONTENT_IMAGE_VERSION)-images.zip
export CONTENT_DB
REMOTE_DB := $(CONTENT_BASE)/$(CONTENT_DB)
REMOTE_IMAGES := $(CONTENT_BASE)/$(CONTENT_IMAGES)
LOCAL_DB := $(CONTENT_PATH)/$(CONTENT_DB)
LOCAL_IMAGES := $(CONTENT_PATH)/$(CONTENT_IMAGES)
# ============================================================================
# Check necessary commands exist
CIRCLECI := $(shell command -v circleci 2> /dev/null)
DOCKER := $(shell command -v docker 2> /dev/null)
ENVSUBST := $(shell command -v envsubst 2> /dev/null)
COMPOSER := $(shell command -v composer 2> /dev/null)
SHELLCHECK := $(shell command -v shellcheck 2> /dev/null)
YAMLLINT := $(shell command -v yamllint 2> /dev/null)
# ============================================================================
.DEFAULT_GOAL := all
.PHONY: all
all: build config status
@echo "Ready"
.PHONY: init
init: .git/hooks/pre-commit
.git/hooks/%:
@chmod 755 .githooks/*
@find .git/hooks -type l -exec rm {} \;
@find .githooks -type f -exec ln -sf ../../{} .git/hooks/ \;
# ============================================================================
# SELF TESTS
.PHONY : lint
lint: init
@$(MAKE) -j lint-docker lint-sh lint-yaml lint-ci
lint-docker: db/Dockerfile
ifndef DOCKER
$(error "docker is not installed: https://docs.docker.com/install/")
endif
@docker run --rm -i hadolint/hadolint < db/Dockerfile
lint-sh:
ifndef SHELLCHECK
$(error "shellcheck is not installed: https://github.com/koalaman/shellcheck")
endif
@find . ! -path './persistence/*' -type f -name '*.sh' | xargs shellcheck
lint-yaml:
ifndef YAMLLINT
$(error "yamllint is not installed: https://github.com/adrienverge/yamllint")
endif
@find . ! -path './persistence/*' -type f -name '*.yml' | xargs yamllint
lint-ci:
ifndef CIRCLECI
$(error "circleci is not installed: https://circleci.com/docs/2.0/local-cli/#installation")
endif
@circleci config validate >/dev/null
# ============================================================================
ifdef NRO_REPO
# gives us the basename of the repo e.g. "planet4-netherlands"
NRO_DIRNAME := $(shell echo $(NRO_REPO) | sed 's/^.*\///g; s/\.git$$//g')
NRO_BRANCH ?= develop
NRO_APP_HOSTNAME ?= www.planet4.test
NRO_APP_HOSTPATH ?=
endif
.PHONY: env
env:
@echo export $(COMPOSE_ENV)
# ============================================================================
# CLEAN GENERATED ASSETS
.PHONY : clean
clean:
./clean.sh
# ============================================================================
# DEFAULT CONTENT TASKS
$(CONTENT_PATH):
mkdir -p $@
$(LOCAL_DB): $(CONTENT_PATH)
curl --fail $(REMOTE_DB) > $@
$(LOCAL_IMAGES): $(CONTENT_PATH)
curl --fail $(REMOTE_IMAGES) > $@
.PHONY: getdefaultcontent
getdefaultcontent: $(LOCAL_DB) $(LOCAL_IMAGES)
.PHONY: cleandefaultcontent
cleandefaultcontent:
@rm -rf $(CONTENT_PATH)
.PHONY: updatedefaultcontent
updatedefaultcontent: cleandefaultcontent getdefaultcontent
.PHONY: unzipimages
unzipimages:
@unzip $(LOCAL_IMAGES) -d persistence/app/public/wp-content/uploads
# ============================================================================
# BUILD AND RUN: THE MEAT AND POTATOS
.PHONY: hosts
hosts:
@if ! grep -q "127.0.0.1[[:space:]]\+www.planet4.test" /etc/hosts; then \
cp /etc/hosts hosts.backup; \
echo "Your hosts file has been backed up to $(PWD)/'hosts.backup'"; \
echo ""; \
echo "May require sudo password to configure the /etc/hosts file ..."; \
echo -e "\n# Planet4 local development environment\n127.0.0.1\twww.planet4.test pma.www.planet4.test traefik.www.planet4.test" | sudo tee -a /etc/hosts; \
else echo "Hosts file already configured"; fi
.PHONY: build
build: hosts run unzipimages config elastic flush
.PHONY: run
run:
@$(MAKE) -j init getdefaultcontent db/Dockerfile
cp ci/scripts/duplicate-db.sh defaultcontent/duplicate-db.sh
@./go.sh
@./wait.sh
# ============================================================================
# DEVELOPER ENVIRONMENT
.PHONY: dev
dev: hosts run unzipimages config installnpm repos elastic flush status
@echo "Ready"
.PHONY: repos
repos:
rm -fr persistence/app/public/wp-content/themes/planet4-master-theme
rm -fr persistence/app/public/wp-content/plugins/planet4-plugin-gutenberg-blocks
rm -fr persistence/app/public/wp-content/plugins/planet4-plugin-gutenberg-engagingnetworks
@./repos.sh
docker-compose exec -T php-fpm /app/source/tasks/other/install-deps.sh
dev-install-xdebug:
ifeq (Darwin, $(shell uname -s))
$(eval export XDEBUG_REMOTE_HOST=$(shell ipconfig getifaddr en0))
else
$(eval export XDEBUG_REMOTE_HOST=$(shell docker network inspect ${PROJECT}_local --format '{{(index .IPAM.Config 0).Gateway }}'))
endif
ifndef ENVSUBST
$(error Command: 'envsubst' not found, please install using your package manager)
endif
docker-compose exec php-fpm sh -c 'apt-get update && apt-get install php-xdebug'
envsubst < dev-templates/xdebug.tmpl > dev-templates/xdebug.out
docker cp dev-templates/xdebug.out $(shell $(COMPOSE_ENV) docker-compose ps -q php-fpm):/tmp/20-xdebug.ini
docker-compose exec php-fpm sh -c 'mv /tmp/20-xdebug.ini /etc/php/$${PHP_MAJOR_VERSION}/fpm/conf.d/20-xdebug.ini'
docker-compose exec php-fpm sh -c 'service php$${PHP_MAJOR_VERSION}-fpm reload'
# ============================================================================
# ELASTICSEARCH
.PHONY: elastic
elastic: elastic-index flush
elastic-index:
docker-compose exec php-fpm wp elasticpress index --setup --quiet --url=www.planet4.test
# ============================================================================
# CONTINUOUS INTEGRATION TASKS
.PHONY: ci
ci: export DOCKER_COMPOSE_FILE := docker-compose.ci.yml
ci:
ifndef APP_IMAGE
$(error APP_IMAGE is not set)
endif
ifndef OPENRESTY_IMAGE
$(error OPENRESTY_IMAGE is not set)
endif
@$(MAKE) lint run config ci-copyimages elastic flush
db/Dockerfile:
ifndef ENVSUBST
$(error Command: 'envsubst' not found, please install using your package manager)
endif
envsubst < [email protected] > $@
.PHONY: ci-%
ci-%: export DOCKER_COMPOSE_FILE := docker-compose.ci.yml
artifacts/codeception:
@mkdir -p $@
.PHONY: ci-extract-artifacts
ci-extract-artifacts: artifacts/codeception
@docker cp $(shell $(COMPOSE_ENV) docker-compose ps -q php-fpm):/app/source/tests/_output/. $^
@echo Extracted artifacts into $^
.PHONY: ci-copyimages
ci-copyimages: $(LOCAL_IMAGES)
$(eval TMPDIR := $(shell mktemp -d))
mkdir -p "$(TMPDIR)/images"
@unzip $(LOCAL_IMAGES) -d "$(TMPDIR)/images"
@docker cp "$(TMPDIR)/images/." $(shell $(COMPOSE_ENV) docker-compose ps -q php-fpm):/app/source/public/wp-content/uploads
@docker cp "$(TMPDIR)/images/." $(shell $(COMPOSE_ENV) docker-compose ps -q openresty):/app/source/public/wp-content/uploads
@echo "Copied images into php-fpm+openresty:/app/source/public/wp-content/uploads"
@rm -fr "$TMPDIR"
# ============================================================================
# CODECEPTION TASKS
test: install-codeception test-env-info test-codeception
.PHONY: install-codeception
install-codeception:
@docker-compose exec php-fpm bash -c 'cd tests && composer install --prefer-dist --no-progress'
.PHONY: test-codeception-unit
test-codeception-unit:
@docker-compose exec php-fpm tests/vendor/bin/codecept run wpunit --xml=junit.xml --html --debug
.PHONY: test-codeception-acceptance
test-codeception-acceptance:
@docker-compose exec php-fpm tests/vendor/bin/codecept run acceptance --xml=junit.xml --html
.PHONY: test-codeception
test-codeception: test-codeception-acceptance
.PHONY: test-codeception-failed
test-codeception-failed:
@docker-compose exec php-fpm tests/vendor/bin/codecept run -g failed --xml=junit.xml --html
.PHONY: test-env-info
test-env-info:
@docker-compose exec php-fpm sh -c 'echo "Wp core information" && wp core version --extra'
@docker-compose exec php-fpm sh -c 'echo "Themes" && wp theme list'
@docker-compose exec php-fpm sh -c 'echo "Plugins" && wp plugin list'
@docker-compose exec php-fpm sh -c 'echo "Greenpeace Packages" && wp option get greenpeace_packages --format=yaml'
# ============================================================================
# KITCHEN SINK
.PHONY : pull
pull:
docker-compose pull
persistence/app:
mkdir -p persistence/app
.PHONY: appdata
appdata: persistence/app
docker cp $(shell docker create $(APP_IMAGE) | tee .tmp-id):/app/source persistence
docker rm -v $(shell cat .tmp-id)
rm -fr persistence/app
mv persistence/source persistence/app
.PHONY : stop
stop:
./stop.sh
.PHONY : stateless
stateless: clean getdefaultcontent start-stateless config config-stateless status
.PHONY: start-stateless
start-stateless:
DOCKER_COMPOSE_FILE=docker-compose.stateless.yml \
./go.sh
./wait.sh
# ============================================================================
# POST INSTALL AND CONFIGURATION TASKS
.PHONY: config
config:
docker-compose exec -T php-fpm wp option set rt_wp_nginx_helper_options '$(NGINX_HELPER_JSON)' --format=json
docker-compose exec -T php-fpm wp rewrite structure $(REWRITE)
docker-compose exec php-fpm wp option patch insert planet4_options cookies_field "Planet4 Cookie Text"
docker-compose exec php-fpm wp user update $(WP_ADMIN_USER) --user_pass=$(WP_ADMIN_PASS) --role=administrator
docker-compose exec php-fpm wp plugin deactivate wp-stateless
docker-compose exec php-fpm wp option update ep_host $(ELASTICSEARCH_HOST)
$(MAKE) flush
.PHONY: config-stateless
config-stateless:
@docker-compose exec php-fpm wp plugin activate wp-stateless
.PHONY : pass
pass:
@make pmapass
@make wppass
.PHONY : wppass
wppass:
@printf "Wordpress credentials:\n"
@printf "User: %s\n" $(WP_ADMIN_USER)
@printf "Pass: %s\n" $(WP_ADMIN_PASS)
@printf "\n"
.PHONY : pmapass
pmapass:
@printf "Database credentials:\n"
@printf "User: %s\n" $(MYSQL_USER)
@printf "Pass: %s\n----\n" $(MYSQL_PASS)
@printf "User: root\n"
@printf "Pass: %s\n----\n" $(ROOT_PASS)
.PHONY : wpadmin
wpadmin:
@docker-compose exec -T php-fpm wp user create ${WP_USER} ${WP_USER_EMAIL} --role=administrator
.PHONY: status
status:
@docker-compose ps
@echo
@$(MAKE) pass
@echo
@echo " Frontend - http://www.planet4.test"
@echo " Backend - http://www.planet4.test/admin"
@echo
@echo "Execute the following command to configure your local shell environment"
@echo
@$(MAKE) env
@echo
.PHONY: flush
flush:
@docker-compose exec redis redis-cli flushdb
.PHONY: php-shell
php-shell:
@docker-compose exec php-fpm bash
.PHONY: installnpm
installnpm:
# Update packages
docker-compose exec php-fpm apt update
# Install NPM
docker-compose exec php-fpm apt install npm -y
# Update Node version
docker-compose exec php-fpm sh -c 'npm cache clean -f && npm install -g n && n stable'
.PHONY: assets
assets:
docker-compose exec -T php-fpm /app/source/tasks/other/build-assets.sh
.PHONY: watch
watch:
docker-compose exec -T php-fpm /app/source/tasks/other/watch.sh
.PHONY: revertdb
revertdb:
@docker stop $(shell $(COMPOSE_ENV) docker-compose ps -q db)
@docker rm $(shell $(COMPOSE_ENV) docker-compose ps -q db)
@docker volume rm $(COMPOSE_PROJECT_NAME)_db
@docker-compose up -d
.PHONY: nro-enable
nro-enable:
@[[ ! -z "$(NRO_REPO)" ]] || (\
echo "You need to specify some variables before you can use this!" && \
echo && \
echo "Create/edit a file called Makefile.include, then add:" && \
echo && \
echo "NRO_REPO := <put the Git URL for the NRO repo here>" && \
echo "NRO_THEME := <put the theme name for the NRO here>" && \
echo "NRO_BRANCH := <optionally, the branch you want, default is develop>" && \
echo && \
exit 1 \
)
@echo "NRO repo $(NRO_REPO)"
@echo "NRO theme $(NRO_THEME)"
@echo "NRO dirname $(NRO_DIRNAME)"
@docker-compose exec php-fpm sh -c " \
mkdir -p sites && \
(cd sites && (test -d $(NRO_DIRNAME) || git clone $(NRO_REPO) $(NRO_DIRNAME))) && \
(cd "sites/$(NRO_DIRNAME)" && git checkout $(NRO_BRANCH)) && \
composer config extra.merge-plugin.require "sites/$(NRO_DIRNAME)/composer-local.json" && \
composer update && \
composer run-script copy:themes && \
composer run-script copy:plugins && \
composer run-script plugin:activate && \
composer run-script theme:activate $(NRO_THEME) \
"
@make flush
.PHONY: nro-disable
nro-disable:
@docker-compose exec php-fpm sh -c " \
composer config extra.merge-plugin.require "composer-local.json" && \
composer update && \
composer run-script copy:themes && \
composer run-script copy:plugins && \
composer run-script plugin:activate && \
composer run-script theme:activate planet4-master-theme \
"
@make flush
.PHONY: nro-test-codeception
nro-test-codeception:
@docker-compose \
-f $(DOCKER_COMPOSE_FILE) \
-f $(DOCKER_COMPOSE_TOOLS_FILE) \
run \
-e APP_HOSTNAME=$(NRO_APP_HOSTNAME) \
-e APP_HOSTPATH=$(NRO_APP_HOSTPATH) \
--user $(id -u):$(id -g) --rm --no-deps \
codeception sh -c '\
cd sites/$(NRO_DIRNAME) && \
codeceptionify.sh . && \
codecept run --xml=junit.xml --html \
'