-
Notifications
You must be signed in to change notification settings - Fork 4
/
m2devbox-init.sh
executable file
·402 lines (343 loc) · 10.3 KB
/
m2devbox-init.sh
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
#!/bin/bash
rm -f data/options
rm -f data/ports
generate_container_name () {
local service=$1
local number=1
local name="magento2devbox_${service}_${number}"
while [[ `docker ps -a -q --filter="name=$name"` ]]; do
((number++))
name="magento2devbox_${service}_${number}"
done
echo $name
}
get_data () {
local file_name=$1
local folder_path='data'
local file_path="$folder_path/$file_name"
local contents
if [ -f $file_path ]; then
contents=$(cat $file_path)
fi
echo $contents
}
store_data () {
local file_name=$1
local value=$2
local delimiter=$3
local key=$4
local key_value_delimiter=$5
local prefix=$6
local suffix=$7
local folder_path='data'
local file_path="$folder_path/$file_name"
local contents=$(get_data $file_name)
if [[ $contents ]]; then
contents="$contents$delimiter"
fi
contents="$contents$prefix"
if [[ $key ]]; then
contents="$contents$key$key_value_delimiter"
fi
contents="$contents$value$suffix"
mkdir -p $folder_path && echo $contents > $file_path
echo $value
}
store_option () {
local key=$1
local value=$2
store_data 'options' $value ' ' $key '=' '--' &> /dev/null
echo $value
}
get_free_port () {
local port=$1
local used_ports=$(get_data 'ports')
while [[ ! $port ]] || [[ $(lsof -i tcp:$port | grep "(LISTEN)") ]] || [[ $used_ports == *"|$port|"* ]]; do
port=$(jot -r 1 1 65000)
done
store_data 'ports' $port '' '' '' '|' '|' &> /dev/null
echo $port
}
request () {
local varName=$1
local question=$2
local isBoolean=$3
local defaultValue=$4
local value
local output
if [[ $isBoolean = 1 ]]; then
if [[ $defaultValue = 1 ]]; then
question="$question [Y/n]"
else
question="$question [y/N]"
fi
else
if [[ $defaultValue != '' ]]; then
question="$question [default: $defaultValue]"
fi
fi
read -p "$question: " value #prompt value
value=$(echo $value | xargs) #trim input
if [[ ${#value} > 0 ]]; then
if [[ $isBoolean = 1 ]]; then
if $(echo $value | grep -Eiq '^(?:[1y]|yes|true)$'); then
value=1
output='yes'
else
value=0
output='no'
fi
else
output=$value
fi
else
if [[ $isBoolean = 1 ]]; then
if [[ $defaultValue = 1 ]]; then
value=1
output='yes'
else
value=0
output='no'
fi
else
value=$defaultValue
output=$value
fi
fi
export $varName=$value
echo $output
}
if [ -f docker-compose.yml ]; then
echo 'Stopping current containers'
docker stop $(docker-compose ps -q) >> /dev/null
fi
echo 'Creating docker-compose config'
#Database
db_container=$(generate_container_name 'db')
db_host=$(store_option 'db-host' 'db')
db_user=$(store_option 'db-user' 'root')
db_password=$(store_option 'db-password' 'root')
db_name=$(store_option 'db-name' 'magento2')
db_port=$(store_option 'db-port' 3306)
db_home_port=$(get_free_port 1345)
db_path='/var/lib/mysql'
db_logs_path='/var/log/mysql'
db_home_logs_path='./shared/logs/mysql'
if [[ ! $db_home_path ]]; then
db_home_path='./shared/db'
fi
#RabbitMQ
rabbitmq_container=$(generate_container_name 'rabbit')
rabbitmq_host=$(store_option 'rabbitmq-host' 'rabbit')
rabbitmq_port=$(store_option 'rabbitmq-port' 5672)
rabbitmq_admin_port=15672
rabbitmq_home_port=$(get_free_port $rabbitmq_port)
rabbitmq_home_admin_port=$(get_free_port 8282)
#Redis
redis_container=$(generate_container_name 'redis')
redis_host=$(store_option 'redis-host' 'redis')
#Varnish
varnish_container=$(generate_container_name 'varnish')
varnish_host='varnish'
varnish_port=6081
varnish_home_port=$(get_free_port 1749) && $(store_option 'varnish-home-port' $varnish_home_port) &> /dev/null
varnish_config_dir='/home/magento2/configs/varnish'
varnish_config_path=$(store_option 'varnish-config-path' "$varnish_config_dir/default.vcl")
varnish_home_path="./shared/configs/varnish"
varnish_container_config_path='/etc/varnish/default'
#Elastic Search
elastic_container=$(generate_container_name 'elastic')
elastic_host=$(store_option 'elastic-host' 'elasticsearch')
elastic_port=$(store_option 'elastic-port' 9200)
elastic_home_port=$(get_free_port 9200)
#Web Server
webserver_container=$(generate_container_name 'web')
webserver_host=$(store_option 'webserver-host' 'web')
webserver_port=$(store_option 'webserver-port' 80)
webserver_ssh_port=22
webserver_home_port=$(get_free_port 1748) && $(store_option 'webserver-home-port' $webserver_home_port) &> /dev/null
webserver_home_ssh_port=$(get_free_port 2222)
webserver_apache_logs_path='/var/log/apache2'
webserver_phpfpm_logs_path='/var/log/php-fpm'
webserver_home_apache_logs_path='./shared/logs/apache2'
webserver_home_phpfpm_logs_path='./shared/logs/php-fpm'
#Magento
magento_host=$(store_option 'magento-host' 'localhost')
magento_path=$(store_option 'magento-path' '/var/www/magento2')
magento_path_shared='/home/magento2/magento2'
magento_cloud_path='/root/.magento-cloud'
magento_cloud_home_path='./shared/.magento-cloud'
#Composer
composer_path='/home/magento2/.composer'
composer_home_path='./shared/.composer'
#SSH
ssh_path='/home/magento2/.ssh'
ssh_home_path='./shared/.ssh'
while [ $# -gt 0 ]; do
case $1 in
-it)
interactive=1
shift
;;
--*)
key=$(echo $1 | sed -e 's/^--\([^=]*\)=[^=]*$/\1/g')
var_name=$(echo $key | sed -e 's/-/_/g')
value=$(echo $1 | sed -e 's/^[^=]*=//g')
export $var_name=$value
store_option $key $value &> /dev/null
shift
;;
*)
echo "Error: Unexpected argument \"$1\"!"
exit
;;
esac
done
if [[ $magento_home_path ]]; then
magento_sources_reuse=1
else
magento_home_path='./shared/webroot'
if [[ ! $magento_sources_reuse ]]; then
request 'magento_sources_reuse' 'Do you want to use custom location for Magento sources on local machine?' 1
fi
if [[ $magento_sources_reuse = 1 ]]; then
magento_default_home_path=$(get_data 'magento_home_path')
request 'magento_home_path' \
'Please provide full path to the Magento sources on local machine' \
0 \
$magento_default_home_path
if [[ ! $magento_home_path ]]; then
echo "Error: Magento folder was not specified!"
exit
fi
fi
rm -f data/magento_home_path
store_data 'magento_home_path' $magento_home_path &> /dev/null
fi
store_option 'magento-sources-reuse' $magento_sources_reuse &> /dev/null
cat > docker-compose.yml <<- EOM
##
# Services needed to run Magento2 application on Docker
#
# Docker Compose defines required services and attach them together through aliases
##
version: '2'
services:
$webserver_host:
container_name: $webserver_container
restart: always
image: magento/magento2devbox_web:latest
# build: web
volumes:
- "$magento_home_path:$magento_path_shared"
- "$composer_home_path:$composer_path"
- "$ssh_home_path:$ssh_path"
- "$webserver_home_apache_logs_path:$webserver_apache_logs_path"
- "$webserver_home_phpfpm_logs_path:$webserver_phpfpm_logs_path"
- "$varnish_home_path:$varnish_config_dir"
- "$magento_cloud_home_path:$magento_cloud_path"
environment:
- USE_SHARED_WEBROOT=0
- SHARED_CODE_PATH="$magento_path_shared"
ports:
- "$webserver_home_port:$webserver_port"
- "$webserver_home_ssh_port:$webserver_ssh_port"
$db_host:
container_name: $db_container
restart: always
image: mysql:5.6
ports:
- "$db_home_port:$db_port"
environment:
- MYSQL_ROOT_PASSWORD=$db_password
- MYSQL_DATABASE=$db_name
volumes:
- "$db_home_path:$db_path"
- "$db_home_logs_path:$db_logs_path"
$varnish_host:
container_name: $varnish_container
restart: always
depends_on:
- "$webserver_host"
image: magento/magento2devbox_varnish:latest
# build: varnish
volumes:
- "$varnish_home_path:$varnish_container_config_path"
ports:
- "$varnish_home_port:$varnish_port"
$redis_host:
container_name: $redis_container
restart: always
image: redis:3.0.7
$rabbitmq_host:
container_name: $rabbitmq_container
restart: always
image: rabbitmq:3-management
ports:
- "$rabbitmq_home_admin_port:$rabbitmq_admin_port"
- "$rabbitmq_home_port:$rabbitmq_port"
$elastic_host:
container_name: $elastic_container
restart: always
image: elasticsearch:latest
ports:
- "$elastic_home_port:$elastic_port"
EOM
echo 'Creating shared folders'
mkdir -p $composer_home_path
mkdir -p $ssh_home_path
mkdir -p $magento_home_path
mkdir -p $db_home_path
mkdir -p $webserver_home_apache_logs_path
mkdir -p $webserver_home_phpfpm_logs_path
mkdir -p $db_home_logs_path
mkdir -p $varnish_home_path
echo 'Build docker images'
docker-compose up --build -d
cat > m2devbox.sh <<- EOM
#!/bin/bash
case \$1 in
exec|show-name)
command=\$1
shift
;;
*)
echo 'Wrong command: only "exec" and "show-name" are supported'
exit
;;
esac
while [ \$# -gt 0 ]; do
options="\$options \$1"
shift
done
if [[ \$command = 'exec' ]]; then
docker exec -it --privileged -u magento2 $webserver_container\$options
fi
if [[ \$command = 'show-name' ]]; then
echo $webserver_container
fi
EOM
chmod +x m2devbox.sh
options=$(get_data 'options')
if [[ $interactive != 1 ]]; then
options="$options --no-interaction"
fi
rm -f data/ports
./m2devbox.sh exec php -f /home/magento2/scripts/m2init magento:install $options
cat > m2devbox-debug-test.sh <<- EOM
#!/bin/bash
case \$1 in
unit|integration)
command=\$1
;;
*)
echo 'Wrong test type: only unit and integration are supported'
exit
;;
esac
ssh -p$webserver_home_ssh_port [email protected] /usr/local/bin/php -dxdebug.remote_autostart=1 \\
/var/www/magento2/vendor/phpunit/phpunit/phpunit --configuration \\
/var/www/magento2/dev/tests/\$command/phpunit.xml.dist \\
\$2
EOM
chmod +x debug-test.sh