forked from skilld-labs/skilld-docker-container
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
235 lines (205 loc) · 10.9 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
# Add utility functions and scripts to the container
include scripts/makefile/*.mk
.PHONY: all fast allfast provision si exec exec0 down clean dev drush info phpcs phpcbf hooksymlink clang cinsp compval watchdogval drupalrectorval upgradestatusval behat sniffers tests front front-install front-build clear-front lintval lint storybook back behatdl behatdi browser_driver browser_driver_stop statusreportval contentgen newlineeof localize local-settings redis-settings content patchval diff
.DEFAULT_GOAL := help
# https://stackoverflow.com/a/6273809/1826109
%:
@:
# Prepare enviroment variables from defaults
$(shell false | cp -i \.env.default \.env 2>/dev/null)
$(shell false | cp -i \.\/docker\/docker-compose\.override\.yml\.default \.\/docker\/docker-compose\.override\.yml 2>/dev/null)
include .env
$(shell sed -i -e '/COMPOSE_PROJECT_NAME=/ s/=.*/=$(shell echo "$(COMPOSE_PROJECT_NAME)" | tr -cd '[a-zA-Z0-9]' | tr '[:upper:]' '[:lower:]')/' .env)
# Get user/group id to manage permissions between host and containers
LOCAL_UID := $(shell id -u)
LOCAL_GID := $(shell id -g)
# Evaluate recursively
CUID ?= $(LOCAL_UID)
CGID ?= $(LOCAL_GID)
# Define current directory only once
CURDIR=$(shell pwd)
# Define network name.
COMPOSE_NET_NAME := $(COMPOSE_PROJECT_NAME)_front
SDC_SERVICES=$(shell docker-compose config --services)
# Determine database data directory if defined
DB_MOUNT_DIR=$(shell echo $(CURDIR))/$(shell basename $(DB_DATA_DIR))
ifeq ($(findstring mysql,$(SDC_SERVICES)),mysql)
DB_MOUNT_DIR=$(shell echo $(CURDIR))/$(shell basename $(DB_DATA_DIR))/$(COMPOSE_PROJECT_NAME)_mysql
endif
ifeq ($(findstring postgresql,$(SDC_SERVICES)),postgresql)
DB_MOUNT_DIR=$(shell echo $(CURDIR))/$(shell basename $(DB_DATA_DIR))/$(COMPOSE_PROJECT_NAME)_pgsql
endif
# Execute php container as regular user
php = docker-compose exec -T --user $(CUID):$(CGID) php ${1}
# Execute php container as root user
php-0 = docker-compose exec -T --user 0:0 php ${1}
ADDITIONAL_PHP_PACKAGES := tzdata graphicsmagick # php81-intl php81-redis wkhtmltopdf gnu-libiconv php81-pdo_pgsql postgresql-client postgresql-contrib
DC_MODULES := project_default_content better_normalizers default_content hal serialization
MG_MODULES := migrate_generator migrate migrate_plus migrate_source_csv migrate_tools
## Full site install from the scratch
all: | provision back front si localize hooksymlink info
# Install for CI deploy:review. Back & Front tasks are run in a dedicated previous step in order to leverage CI cache
all_ci: | provision si localize hooksymlink info
# Full site install from the scratch with DB in ram (makes data NOT persistant)
allfast: | fast provision back front si localize hooksymlink info
## Update .env to build DB in ram (makes data NOT persistant)
fast:
$(shell sed -i "s|^#DB_URL=sqlite:///dev/shm/db.sqlite|DB_URL=sqlite:///dev/shm/db.sqlite|g" .env)
$(shell sed -i "s|^DB_URL=sqlite:./../.cache/db.sqlite|#DB_URL=sqlite:./../.cache/db.sqlite|g" .env)
## Provision enviroment
provision:
# Check if enviroment variables has been defined
ifeq ($(strip $(COMPOSE_PROJECT_NAME)),projectname)
$(eval COMPOSE_PROJECT_NAME = $(strip $(shell read -p "- Please customize project name: " REPLY;echo -n $$REPLY)))
$(shell sed -i -e '/COMPOSE_PROJECT_NAME=/ s/=.*/=$(shell echo "$(COMPOSE_PROJECT_NAME)" | tr -cd '[a-zA-Z0-9]' | tr '[:upper:]' '[:lower:]')/' .env)
$(info - Run `make all` again.)
@echo
exit 1
endif
ifdef DB_MOUNT_DIR
$(shell [ ! -d $(DB_MOUNT_DIR) ] && mkdir -p $(DB_MOUNT_DIR) && chmod 777 $(DB_MOUNT_DIR))
endif
make -s down
@echo "Build and run containers..."
docker-compose up -d --remove-orphans
ifneq ($(strip $(ADDITIONAL_PHP_PACKAGES)),)
$(call php-0, apk add --no-cache $(ADDITIONAL_PHP_PACKAGES))
endif
# Set up timezone
$(call php-0, cp /usr/share/zoneinfo/Europe/Paris /etc/localtime)
# Install newrelic PHP extension if NEW_RELIC_LICENSE_KEY defined
make -s newrelic
$(call php-0, kill -USR2 1)
## Install backend dependencies
back:
ifneq ($(strip $(ADDITIONAL_PHP_PACKAGES)),)
$(call php-0, apk add --no-cache $(ADDITIONAL_PHP_PACKAGES))
endif
@echo "Installing composer dependencies, without dev ones"
$(call php, composer install --no-interaction --prefer-dist -o --no-dev)
$(call php, composer create-required-files)
$(eval TESTER_NAME := tester)
$(eval TESTER_ROLE := contributor)
## Install drupal
si:
@echo "Installing from: $(PROJECT_INSTALL)"
make -s local-settings
ifeq ($(PROJECT_INSTALL), config)
$(call php, drush si --existing-config --db-url="$(DB_URL)" --account-name="$(ADMIN_NAME)" --account-mail="$(ADMIN_MAIL)" -y)
# install_import_translations() overwrites config translations so we need to reimport.
$(call php, drush cim -y)
else
$(call php, drush si $(PROFILE_NAME) --db-url="$(DB_URL)" --account-name="$(ADMIN_NAME)" --account-mail="$(ADMIN_MAIL)" -y --site-name="$(SITE_NAME)" --site-mail="$(SITE_MAIL)" install_configure_form.site_default_country=FR install_configure_form.date_default_timezone=Europe/Paris)
endif
make content
#make -s redis-settings
$(call php, drush user:create "$(TESTER_NAME)")
$(call php, drush user:role:add "$(TESTER_ROLE)" "$(TESTER_NAME)")
content:
ifneq ($(strip $(DC_MODULES)),)
$(call php, drush en $(DC_MODULES) -y)
$(call php, drush pmu $(DC_MODULES) -y)
endif
ifneq ($(strip $(MG_MODULES)),)
$(call php, drush en $(MG_MODULES) -y)
$(call php, drush migrate_generator:generate_migrations /var/www/html/content --update)
$(call php, drush migrate:import --all --group=mgg)
$(call php, drush migrate_generator:clean_migrations mgg)
$(call php, drush pmu $(MG_MODULES) -y)
endif
local-settings:
ifneq ("$(wildcard settings/settings.local.php)","")
@echo "Turn on settings.local"
$(call php, chmod +w web/sites/default)
$(call php, cp settings/settings.local.php web/sites/default/settings.local.php)
$(call php-0, sed -i "/settings.local.php';/s/# //g" web/sites/default/settings.php)
endif
REDIS_IS_INSTALLED := $(shell grep "redis.connection" web/sites/default/settings.php 2> /dev/null | tail -1 | wc -l || echo "0")
redis-settings:
ifeq ($(REDIS_IS_INSTALLED), 1)
@echo "Redis settings already installed, nothing to do"
else
@echo "Turn on Redis settings"
$(call php-0, chmod -R +w web/sites/)
$(call php, cat settings/settings.redis.php >> web/sites/default/settings.php)
endif
## Import online & local translations
localize:
@echo "Checking & importing online translations..."
$(call php, drush locale:check)
$(call php, drush locale:update)
@echo "Importing custom translations..."
$(call php, drush locale:import:all /var/www/html/translations/ --type=customized --override=all)
@echo "Localization finished"
## Display project's information
info:
$(info )
$(info Containers for "$(COMPOSE_PROJECT_NAME)" info:)
$(eval CONTAINERS = $(shell docker ps -f name=$(COMPOSE_PROJECT_NAME) --format "{{ .ID }}" -f 'label=traefik.enable=true'))
$(foreach CONTAINER, $(CONTAINERS),$(info http://$(shell printf '%-19s \n' $(shell docker inspect --format='{{(index .NetworkSettings.Networks "$(COMPOSE_NET_NAME)").IPAddress}}:{{index .Config.Labels "traefik.port"}} {{range $$p, $$conf := .NetworkSettings.Ports}}{{$$p}}{{end}} {{.Name}}' $(CONTAINER) | rev | sed "s/pct\//,pct:/g" | sed "s/,//" | rev | awk '{ print $0}')) ))
$(info )
ifdef REVIEW_DOMAIN
$(eval BASE_URL := $(MAIN_DOMAIN_NAME))
else
$(eval BASE_URL := $(shell docker inspect --format='{{(index .NetworkSettings.Networks "$(COMPOSE_NET_NAME)").IPAddress}}:{{index .Config.Labels "traefik.port"}}' $(COMPOSE_PROJECT_NAME)_web))
endif
$(info Login as System Admin: http://$(shell printf '%-19s \n' $(shell echo "$(BASE_URL)"$(shell $(call php, drush user:login --name="$(ADMIN_NAME)" /admin/content/ | awk -F "default" '{print $$2}')))))
$(info Login as Contributor: http://$(shell printf '%-19s \n' $(shell echo "$(BASE_URL)"$(shell $(call php, drush user:login --name="$(TESTER_NAME)" /admin/content/ | awk -F "default" '{print $$2}')))))
$(info )
ifneq ($(shell diff .env .env.default -q),)
@echo -e "\x1b[33mWARNING\x1b[0m - .env and .env.default files differ. Use 'make diff' to see details."
endif
ifneq ($(shell diff docker/docker-compose.override.yml docker/docker-compose.override.yml.default -q),)
@echo -e "\x1b[33mWARNING\x1b[0m - docker/docker-compose.override.yml and docker/docker-compose.override.yml.default files differ. Use 'make diff' to see details."
endif
## Output diff between local and versioned files
diff:
diff -u0 --color .env .env.default || true; echo ""
diff -u0 --color docker/docker-compose.override.yml docker/docker-compose.override.yml.default || true; echo ""
## Run shell in PHP container as regular user
exec:
docker-compose exec --user $(CUID):$(CGID) php ash
## Run shell in PHP container as root
exec0:
docker-compose exec --user 0:0 php ash
down:
@echo "Removing network & containers for $(COMPOSE_PROJECT_NAME)"
@docker-compose down -v --remove-orphans --rmi local
@if [ ! -z "$(shell docker ps -f 'name=$(COMPOSE_PROJECT_NAME)_chrome' --format '{{.Names}}')" ]; then \
echo 'Stoping browser driver.' && make -s browser_driver_stop; fi
DIRS = web/core web/libraries web/modules/contrib web/profiles/contrib web/sites web/themes/contrib vendor
## Totally remove project build folder, docker containers and network
clean: info
make -s down
ifdef CURDIR
$(eval SCAFFOLD = $(shell docker run --rm -v $(CURDIR):/mnt -w /mnt --user $(CUID):$(CGID) $(IMAGE_PHP) composer run-script list-scaffold-files | grep -P '^(?!>)'))
@docker run --rm --user 0:0 -v $(CURDIR):/mnt -w /mnt -e RMLIST="$(addprefix web/,$(SCAFFOLD)) $(DIRS)" $(IMAGE_PHP) sh -c 'for i in $$RMLIST; do rm -fr $$i && echo "Removed $$i"; done'
endif
ifdef DB_MOUNT_DIR
@echo "Clean-up database data from $(DB_MOUNT_DIR) ..."
docker run --rm --user 0:0 -v $(shell dirname $(DB_MOUNT_DIR)):/mnt $(IMAGE_PHP) sh -c "rm -fr /mnt/`basename $(DB_MOUNT_DIR)`"
endif
ifeq ($(CLEAR_FRONT_PACKAGES), yes)
make clear-front
endif
## Enable development mode and disable caching
dev:
@echo "Dev tasks..."
$(call php, composer install --no-interaction --prefer-dist -o)
@$(call php-0, chmod +w web/sites/default)
@$(call php, cp web/sites/default/default.services.yml web/sites/default/services.yml)
@$(call php, sed -i -e 's/debug: false/debug: true/g' web/sites/default/services.yml)
@$(call php, cp web/sites/example.settings.local.php web/sites/default/settings.local.php)
@echo "Including settings.local.php."
@$(call php-0, sed -i "/settings.local.php';/s/# //g" web/sites/default/settings.php)
@$(call php, drush -y -q config-set system.performance css.preprocess 0)
@$(call php, drush -y -q config-set system.performance js.preprocess 0)
@echo "Enabling devel module."
@$(call php, drush -y -q en devel devel_generate)
@echo "Disabling caches."
@$(call php, drush -y -q pm-uninstall dynamic_page_cache page_cache)
@$(call php, drush cr)
## Run drush command in PHP container. To pass arguments use double dash: "make drush dl devel -- -y"
drush:
$(call php, $(filter-out "$@",$(MAKECMDGOALS)))
$(info "To pass arguments use double dash: "make drush en devel -- -y"")