-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-service
executable file
·357 lines (306 loc) · 10.2 KB
/
docker-service
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
#!/bin/bash
set -o nounset
set -o errexit
# Reset in case getopts has been used previously in the shell.
OPTIND=1
# Initialize variables
env=''
# Get cli options
while getopts "he:" opt; do
case $opt in
h)
show_help
exit 0
;;
e)
env=".$OPTARG"
;;
*)
show_help >&2
exit 1
;;
esac
done
# Shift off the options and optional --.
shift "$((OPTIND-1))"
if [ "$env" == '' ]; then
[ ! -f .env ] && cp .env.dist .env;
else
[ ! -f .env ] && cp .env"$env" .env;
fi
source .env
# Common operations
initialize() {
down
pullImages
start
install
populate
jwtKeys
}
localinit() {
build
start
install
populate
jwtKeys
}
build() {
docker build docker/nginx -t nginx
docker build docker/php -t php
docker-compose -f docker-compose"$env".yml build
}
start() {
docker-compose -f docker-compose"$env".yml up -d --remove-orphans
sleep 1
docker-compose -f docker-compose"$env".yml ps
}
jwtKeys() {
if [ ! -f config/jwt/private.pem ]; then
echo "JWT keys do not exist, generate them"
docker-compose -f docker-compose"$env".yml exec php bin/console lexik:jwt:generate-keypair
else
echo "JWT keys already exist, skip"
fi
}
install() {
docker-compose -f docker-compose"$env".yml run -T --rm --user=www-data --no-deps --entrypoint="/bin/bash -c" php 'until nc -z db 3306; do sleep 1; echo "Waiting for DB to come up..."; done'
docker-compose -f docker-compose"$env".yml run -T --rm --user=www-data --no-deps --entrypoint="/bin/bash -c" php "php -d memory_limit=-1 /usr/local/bin/composer install --prefer-dist --no-interaction --optimize-autoloader &&
bin/console doctrine:database:create --if-not-exists &&
bin/console doctrine:migration:migrate -n &&
php -d memory_limit=-1 bin/console cache:clear --env=prod --no-debug &&
php -d memory_limit=-1 bin/console cache:warmup --env=prod --no-debug"
# Load CSS & JS compilation
docker-compose -f docker-compose"$env".yml run -T --rm --user=www-data --no-deps --entrypoint="/bin/bash -c" php "npm install && npm run build"
}
installDb() {
docker-compose -f docker-compose"$env".yml run -T --rm --user=www-data --no-deps --entrypoint="/bin/bash -c" php "bin/console doctrine:database:drop --force &&
bin/console doctrine:database:create &&
bin/console doctrine:migration:migrate -n"
}
populate() {
docker-compose -f docker-compose"$env".yml run -T --rm --user=www-data --no-deps --entrypoint="/bin/bash -c" php "php -d memory_limit=-1 bin/console hautelook:fixtures:load -n"
}
serve() {
if [ "$#" -ge 1 ]; then
docker-compose -f docker-compose"$env".yml run -T --rm --user=www-data --no-deps --entrypoint="/bin/bash -c" php "npm && npm run dev --$1"
fi
docker-compose -f docker-compose"$env".yml run -T --rm --user=www-data --no-deps --entrypoint="/bin/bash -c" php "npm && npm run dev"
}
stop() {
docker-compose -f docker-compose"$env".yml stop
}
down() {
docker-compose -f docker-compose"$env".yml down --remove-orphans
}
csFixer() {
paths=""
config=".php_cs.dist"
if [ "$#" -ge 1 ]; then
paths="$1"
docker run --rm --user www-data --env-file=.env.test -v ${PWD}/:/var/www/gestion-rgpd/ -w /var/www/gestion-rgpd/ $DOCKER_IMAGE_PHP bash -c "php -l $paths"
if [ "$#" -eq 2 ]; then
config="$2"
fi
fi
docker run --rm --user www-data --env-file=.env.test -v ${PWD}/:/var/www/gestion-rgpd/ -w /var/www/gestion-rgpd/ $DOCKER_IMAGE_PHP bash -c "vendor/bin/php-cs-fixer fix --config=$config $paths -vv"
}
phpStan() {
docker run --rm --user www-data --env-file=.env.test -v ${PWD}/:/var/www/gestion-rgpd/ -w /var/www/gestion-rgpd/ $DOCKER_IMAGE_PHP bash -c "vendor/bin/phpstan analyse --error-format=raw -c phpstan.neon -l2 src"
}
# Tests
tests() {
unitTests
qualityTests
functionalTests
}
unitTests() {
echo -e "\n\n\nUnit Tests"
make tu
}
security() {
echo -e "\n\n\nSecurity check"
make security
}
qualityTests() {
docker-compose -f docker-compose.test.yml up -d php
# Code Style
echo -e "\n\n\nCs Fixer :"
docker-compose -f docker-compose.test.yml exec -T --user www-data php vendor/bin/php-cs-fixer fix --config=.php_cs.dist --dry-run --diff -vv
# Lint PHP files
docker-compose -f docker-compose.test.yml exec -T --user www-data php php -l src/
docker-compose -f docker-compose.test.yml exec -T --user www-data php php -l features/
# Lint YAML files
docker-compose -f docker-compose.test.yml exec -T --user www-data php bin/console lint:yaml --env=prod config/
docker-compose -f docker-compose.test.yml exec -T --user www-data php bin/console lint:yaml --env=prod src/
# Lint Twig files
#docker-compose -f docker-compose.test.yml exec -T --user www-data php bin/console lint:twig --env=prod app/Resources/views/
# Bundles security check
docker-compose -f docker-compose.test.yml exec -T --user www-data php bin/console security:check
docker-compose -f docker-compose.test.yml down
}
functionalTests() {
if [ "$#" -ne 1 ]; then
format='progress'
paths=''
else
format='pretty'
paths="$1"
fi
docker-compose -f docker-compose.test.yml up -d benevoles-back
docker-compose -f docker-compose.test.yml ps
docker-compose -f docker-compose.test.yml exec -T --user www-data php bash -c 'until nc -z db 3306; do sleep 1; echo "Waiting for DB to come up..."; done'
docker-compose -f docker-compose.test.yml exec -T --user www-data php bash -c "bin/console doctrine:database:create --if-not-exists"
docker-compose -f docker-compose.test.yml exec -T --user www-data php bash -c "bin/console doctrine:schema:update --force"
docker-compose -f docker-compose.test.yml exec -T --user www-data php bash -c "vendor/bin/behat $paths -v --format=$format"
docker-compose -f docker-compose.test.yml down
}
debug(){
docker-compose -f docker-compose"$env".yml run -T --user=www-data --no-deps --entrypoint="/bin/bash -c" -e XDEBUG_CONFIG="idekey=PHPSTORM" -e PHP_IDE_CONFIG="serverName=localhost" php "php -dxdebug.remote_host=docker.for.mac.host.internal $@"
}
# Images management
pushImages() {
docker login --username gitlab-ci-token --password $CI_JOB_TOKEN $CI_REGISTRY
pushImagePhp
pushImageNginx
}
pushImagePhp() {
docker pull $DOCKER_IMAGE_PHP || true
docker build --cache-from $DOCKER_IMAGE_PHP -t $DOCKER_IMAGE_PHP docker/php
docker push $DOCKER_IMAGE_PHP
}
pushImageNginx() {
docker pull $DOCKER_IMAGE_NGINX || true
docker build --cache-from $DOCKER_IMAGE_NGINX -t $DOCKER_IMAGE_NGINX docker/nginx
docker push $DOCKER_IMAGE_NGINX
}
pullImages() {
docker pull $DOCKER_IMAGE_PHP
docker pull $DOCKER_IMAGE_NGINX
}
# Usage info
show_help() {
cat << EOF
Usage: ${0##*/} [-e] [COMMAND]
Options:
-e string Specify env ("test"|"dev") (default "dev")
Commands:
initialize Start the project no matter what state it is in
localinit Build the containers and start
build Build docker containers
start Start docker containers
install Run app installation scripts
jwtKeys Generate JWT keys
installDb Reset database
populate Load data fixtures in app
serve Serve Webpack Encore files for local dev
stop Stop docker containers
down Remove docker containers
debug XDebug
csFixer Execute php-cs-fixer and php lint
phpStan Execute phpStan
tests Run unit tests and functional tests
unitTests Run unit tests
qualityTests Run quality tests
functionalTests Run functional tests
pushImages Update registry image
pushImagePhp Update registry php image
pushImageNginx Update registry nginx api image
pullImages Pull latest images from private repository
securityCheck Check vulnerabilities in project libraries
EOF
}
# Show help if no argument was supplied
if [ $# -eq 0 ]
then
show_help >&2
exit 1
fi
case "$1" in
initialize)
initialize
;;
localinit)
localinit
;;
build)
build
;;
start)
start
;;
stop)
stop
;;
down)
down
;;
restart)
stop
start
;;
install)
install
;;
installDb)
installDb
;;
populate)
populate
;;
jwtKeys)
jwtKeys
;;
serve)
if [ "$#" -ne 2 ]; then
serve
else
serve $2
fi
;;
tests)
tests
;;
unitTests)
unitTests
;;
qualityTests)
qualityTests
;;
functionalTests)
if [ "$#" -ne 2 ]; then
functionalTests
else
functionalTests $2
fi
;;
csFixer)
if [ "$#" -ne 2 ]; then
csFixer
else
csFixer $2
fi
;;
phpStan)
phpStan
;;
pushImages)
pushImages
;;
pushImagePhp)
pushImagePhp
;;
pushImageNginx)
pushImageNginx
;;
pullImages)
pullImages
;;
securityCheck)
security
;;
*)
show_help >&2
exit 1
esac
exit 0