forked from yvoronoy/m2install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m2install.sh
executable file
·2760 lines (2465 loc) · 76.9 KB
/
m2install.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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Magento 2 Bash Install Script
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# @copyright Copyright (c) 2015-2019 by Yaroslav Voronoy ([email protected])
# @license http://www.gnu.org/licenses/
GLOBAL_ARGS="$@"
VERBOSE=1
CURRENT_DIR_NAME=$(basename "$(pwd)")
STEPS=
HTTP_HOST=http://mage2.dev/
BASE_PATH=${CURRENT_DIR_NAME}
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=
ELASTICSEARCH_HOST=
ELASTICSEARCH_PORT=
MAGENTO_VERSION=2.3.7
DB_NAME=
USE_SAMPLE_DATA=
EE_PATH=magento2ee
INSTALL_EE=
INSTALL_B2B=
CONFIG_NAME=.m2install.conf
USE_WIZARD=1
GIT_CE_REPO="[email protected]:magento/magento2.git"
GIT_CE_SD_REPO="[email protected]:magento/magento2-sample-data.git"
GIT_EE_REPO=
GIT_EE_SD_REPO=
GIT_B2B_REPO=
GIT_CE_SD_PATH=magento2-sample-data
GIT_EE_SD_PATH=magento2-sample-data-ee
GIT_B2B_PATH=magento2b2b
SOURCE=
FORCE=
MAGE_MODE=dev
BIN_PHP=${BIN_PHP:-"php"}
BIN_MAGE="-d memory_limit=4G bin/magento"
BIN_COMPOSER=$(command -v composer)
BIN_MYSQL="mysql"
BIN_GIT="git"
BACKEND_FRONTNAME="admin"
ADMIN_NAME="admin"
ADMIN_PASSWORD="123123q"
ADMIN_FIRSTNAME="Admin"
ADMIN_LASTNAME="Test"
ADMIN_EMAIL="[email protected]"
TIMEZONE="America/Chicago"
LANGUAGE="en_US"
CURRENCY="USD"
REMOTE_DB=
REMOTE_DB_HOST=""
REMOTE_DB_PASSWORD=""
REMOTE_HOST=""
REMOTE_KEY=""
LOCAL_PORT=""
BUNDLED_EXTENSION=(
amzn/amazon-pay-and-login-magento-2-module
dotmailer/dotmailer-magento2-extension
klarna/module-core
klarna/module-kp
klarna/module-ordermanagement
temando/module-shipping-m2
vertex/module-tax
)
M2INSTALL_CSV_LOG=${M2INSTALL_CSV_LOG:-}
function printVersion()
{
printString "1.0.5"
}
function getScriptDirectory()
{
echo "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
return 0;
}
function getCsvLogFile()
{
local path="$(getScriptDir)/m2install.csv"
[[ "$M2INSTALL_CSV_LOG" ]] && path="$M2INSTALL_CSV_LOG"
touch "$csvFile" 2>/dev/null || csvFile=/tmp/m2install.csv
echo "$path"
return 0;
}
function getErrorLogFile()
{
local path="$(getScriptDir)/error.csv"
touch "$errorLogFile" 2>/dev/null || errorLogFile=/tmp/m2install.error.log
echo "$path"
return 0;
}
function writeCsvMetricRow()
{
local csvFile="$(getCsvLogFile)"
[ -s "$csvFile" ] || echo "datetime, mode, home_response_code, home_url, admin_response_code, admin_url, duration, user, dir, script, args" >> "$csvFile"
echo "$@" >> $csvFile
return 0
}
function writeCsvErrorRow()
{
local errorLogFile="$(getErrorLogFile)"
[ -s "$errorLogFile" ] || echo "datetime, error_code, user, dir, script, arguments" >> "$errorLogFile"
echo "$(date '+%Y-%m-%d %H:%M:%S'), $1, $(whoami), $(pwd), $BASH_SOURCE, \"$GLOBAL_ARGS\"" >> $errorLogFile
return 0
}
# Get Script Directory with resolving symlink
function getScriptDir()
{
local source=
local dir=
local source="${BASH_SOURCE[0]}"
while [ -h "$source" ]; do # resolve $SOURCE until the file is no longer a symlink
local dir="$( cd -P "$( dirname "$source" )" && pwd )"
source="$(readlink "$source")"
[[ $source != /* ]] && source="$dir/$source" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
dir="$( cd -P "$( dirname "$source" )" && pwd )"
echo "$dir";
return 0;
}
function checkDependencies()
{
DEPENDENCIES=(
php
composer
mysql
mysqladmin
git
cat
basename
tar
gunzip
sed
grep
mkdir
cp
mv
rm
find
chmod
date
)
for util in "${DEPENDENCIES[@]}"
do
hash "${util}" &>/dev/null || printError "'${util}' is not found on this system" || exit 1
done;
}
function askValue()
{
MESSAGE="$1"
READ_DEFAULT_VALUE="$2"
READVALUE=
if [ "${READ_DEFAULT_VALUE}" ]
then
MESSAGE="${MESSAGE} (default: ${READ_DEFAULT_VALUE})"
fi
MESSAGE="${MESSAGE}: "
read -r -p "$MESSAGE" READVALUE
if [[ $READVALUE = [Nn] ]]
then
READVALUE=''
return
fi
if [ -z "${READVALUE}" ] && [ "${READ_DEFAULT_VALUE}" ]
then
READVALUE=${READ_DEFAULT_VALUE}
fi
}
function askConfirmation() {
if [ "$FORCE" ]
then
return 0;
fi
read -r -p "${1:-Are you sure? [y/N]} " response
case $response in
[yY][eE][sS]|[yY])
retval=0
;;
*)
retval=1
;;
esac
return $retval
}
function printString()
{
if [[ "$VERBOSE" -eq 1 ]]
then
echo "$@";
fi
}
function printError()
{
>&2 echo "ERROR: $@";
return 1;
}
function printLine()
{
if [[ "$VERBOSE" -eq 1 ]]
then
echo "--------------------------------------------------"
fi
}
function setRequest()
{
local _key=$1
local _value=$2
local expression="REQUEST_${_key}=${_value}"
eval "${expression}";
}
function getRequest()
{
local _key=$1
local _variableName="REQUEST_${_key}";
if [[ "${!_variableName:-}" ]]
then
echo "${!_variableName}"
return 0;
fi
echo "";
return 1;
}
function runCommand()
{
local _prefixMessage=${1:-};
local _suffixMessage=${2:-}
if [[ "$VERBOSE" -eq 1 ]]
then
echo "${_prefixMessage}${CMD}${_suffixMessage}"
fi
# shellcheck disable=SC2086
eval ${CMD};
}
function extract()
{
if [ -f "$EXTRACT_FILENAME" ] ; then
case $EXTRACT_FILENAME in
*.tar.*|*.t*z*)
CMD="tar $(getStripComponentsValue ${EXTRACT_FILENAME}) -xf ${EXTRACT_FILENAME} $1"
;;
*.gz) CMD="gunzip $EXTRACT_FILENAME" ;;
*.zip) CMD="unzip -qu -x $EXTRACT_FILENAME" ;;
*) printError "'$EXTRACT_FILENAME' cannot be extracted"; exit 1; CMD='' ;;
esac
runCommand
else
printError "'$EXTRACT_FILENAME' is not a valid file"
fi
}
function getStripComponentsValue()
{
local stripComponents=
local slashCount=
slashCount=$(tar -tf "$1" | grep -v vendor | fgrep pub/index.php | sed 's/pub[/]index[.]php//' | sort | head -1 | tr -cd '/' | wc -m | tr -d ' ')
if [[ "$slashCount" -gt 0 ]]
then
stripComponents="--strip-components=$slashCount"
fi
echo "$stripComponents";
}
function mysqlQuery()
{
CMD="${BIN_MYSQL} -h${DB_HOST} -u${DB_USER} --password=\"${DB_PASSWORD}\" --execute=\"${SQLQUERY}\"";
runCommand
}
function generateDBName()
{
if [ -z "$DB_NAME" ]
then
prepareBasePath
DB_NAME=${DB_USER}_${CURRENT_DIR_NAME}
fi
DB_NAME=$(sed -e "s/\//_/g; s/-/_/g; s/[^a-zA-Z0-9_]//g" <(${BIN_PHP} -r "print strtolower('$DB_NAME');"));
}
function prepareBasePath()
{
BASE_PATH=$(echo "${BASE_PATH}" | sed "s/^\///g" | sed "s/\/$//g" );
}
function checkIfBasedOnDevelopBranch()
{
if [ "$SOURCE" == 'git' ] && [ "${MAGENTO_VERSION}" == '2.4-develop' ]
then
return 0
fi
if [ "$(ls -A ./)" ] && [ -d ".git" ]
then
${BIN_GIT} rev-parse --abbrev-ref HEAD | grep -q '2.4-develop'
if [ 0 = $? ]
then
return 0
fi
fi
return 1
}
function prepareBaseURL()
{
prepareBasePath
HTTP_HOST=$(echo ${HTTP_HOST}/ | sed "s/\/\/$/\//g" );
BASE_URL="${HTTP_HOST}${BASE_PATH}/"
BASE_URL=$(echo ${BASE_URL} | sed "s/\/\/$/\//g" )
if isPubRequired
then
BASE_URL="${BASE_URL}pub/"
fi
BASE_URL=$(echo "$BASE_URL" | sed "s/\/\/$/\//g" );
}
function isPubRequired()
{
if versionIsHigherThan "$(getMagentoVersion)" "2.4.2"
then
return 0
fi
if checkIfBasedOnDevelopBranch
then
return 0
fi
if versionIsHigherThan "$MAGENTO_VERSION" "2.4.2"
then
return 0
fi
if foundSupportBackupFiles
then
if ! tar -tf $(getCodeDumpFilename) | grep '^index.php'
then
return 0
fi
fi
#return false/failure
return 255
}
function initQuietMode()
{
if [[ "$VERBOSE" -eq 1 ]]
then
return;
fi
BIN_MAGE="${BIN_MAGE} --quiet"
BIN_COMPOSER="${BIN_COMPOSER} --quiet"
BIN_GIT="${BIN_GIT} --quiet"
FORCE=1
}
function getCodeDumpFilename()
{
local codeDumpFilename="";
if [[ -f "$(getRequest codedump)" ]]
then
codeDumpFilename="$(getRequest codedump)";
echo "$codeDumpFilename";
return 0;
fi
codeDumpFilename=$(find . -maxdepth 1 -name '*.tbz2' -o -name '*.tar.bz2' | head -n1)
if [ "${codeDumpFilename}" == "" ]
then
codeDumpFilename=$(find . -maxdepth 1 -name '*.tar.gz' | grep -v 'logs.tar.gz' | head -n1)
fi
if [ ! "$codeDumpFilename" ]
then
codeDumpFilename=$(find . -maxdepth 1 -name '*.tgz' | head -n1)
fi
if [ ! "$codeDumpFilename" ]
then
codeDumpFilename=$(find . -maxdepth 1 -name '*.zip' | head -n1)
fi
echo "$codeDumpFilename";
return 0;
}
function getDbDumpFilename()
{
local dbDumpFilename="";
if [[ -f "$(getRequest dbdump)" ]]
then
dbDumpFilename="$(getRequest dbdump)";
echo "$dbDumpFilename";
return 0;
fi
dbdumpFilename=$(find . -maxdepth 1 -name '*.sql.gz' | head -n1)
if [ ! "$dbdumpFilename" ]
then
dbdumpFilename=$(find . -maxdepth 1 -name '*_db.gz' | head -n1)
fi
if [ ! "$dbdumpFilename" ]
then
dbdumpFilename=$(find . -maxdepth 1 -name '*.sql' | head -n1)
fi
echo "$dbdumpFilename";
return 0;
}
function foundSupportBackupFiles()
{
if [ -z getCodeDumpFilename ]
then
return 1;
fi
if [[ "$REMOTE_DB" ]]
then
return 0;
fi
if [ -z getDbDumpFilename ]
then
return 1;
fi
if [ ! -f "$(getCodeDumpFilename)" ] || [ ! -f "$(getDbDumpFilename)" ]
then
return 1;
fi
validateDatabaseDumpArchive
return 0;
}
function validateDatabaseDumpArchive()
{
local minSizeLimit=2
local dbDumpFilenamePath="$(getDbDumpFilename)"
local codeDumpFilenamePath="$(getCodeDumpFilename)"
local dbDumpFileSize="$(wc -c ${dbDumpFilenamePath} | awk '{print $1}')"
local codeDumpFileSize="$(wc -c ${codeDumpFilenamePath} | awk '{print $1}')"
[ "$dbDumpFileSize" -lt "$minSizeLimit" ] && { printErrorAndExit 255 "MySQL DB Dump is corrupt. For on-prem, please request a new MySQL Dump from the merchant and ensure it is created using the mysqldump utility and not bin/magento support:db:backup. For Magento-Cloud, please regenerate a new MySQL Dump by using the ZD Dump Widget / cloud-teleport."; }
[ "$codeDumpFileSize" -lt "$minSizeLimit" ] && { printErrorAndExit 256 "Code Dump is corrupt. For on-prem, please request a new Code Dump from the merchant. For Magento-Cloud, please regenerate a new MySQL Dump by using the ZD Dump Widget / cloud-teleport."; }
}
function printErrorAndExit()
{
printError $2
writeCsvErrorRow "$1"
exit $1
}
function wizard()
{
askValue "Enter Server Name of Document Root" "${HTTP_HOST}"
HTTP_HOST=${READVALUE}
askValue "Enter Base Path" "${BASE_PATH}"
BASE_PATH=${READVALUE}
askValue "Enter DB Host" "${DB_HOST}"
DB_HOST=${READVALUE}
askValue "Enter DB User" "${DB_USER}"
DB_USER=${READVALUE}
askValue "Enter DB Password" "${DB_PASSWORD}"
DB_PASSWORD=${READVALUE}
generateDBName
askValue "Enter DB Name" "${DB_NAME}"
DB_NAME=${READVALUE}
if foundSupportBackupFiles
then
return;
fi
if askConfirmation "Do you want to install Sample Data (y/N)"
then
USE_SAMPLE_DATA=1
fi
}
function noSourceWizard()
{
if [[ "$SOURCE" ]]
then
return;
fi
if [[ ! "$SOURCE" ]] && askConfirmation "Do you want install Enterprise Edition (y/N)"
then
INSTALL_EE=1
fi
if [[ "$INSTALL_EE" ]] && askConfirmation "Do you want install B2B Extension (y/N)"
then
INSTALL_B2B=1
fi
}
function printConfirmation()
{
printComposerConfirmation
printGitConfirmation
prepareBaseURL
printString "BASE URL: ${BASE_URL}"
printString "BASE PATH: ${BASE_PATH}"
printString "DB PARAM: ${DB_USER}@${DB_HOST}"
printString "DB NAME: ${DB_NAME}"
printString "DB PASSWORD: ********"
printString "MAGE MODE: ${MAGE_MODE}"
printString "BACKEND FRONTNAME: ${BACKEND_FRONTNAME}"
printString "ADMIN NAME: ${ADMIN_NAME}"
printString "ADMIN PASSWORD: ${ADMIN_PASSWORD}"
printString "ADMIN FIRSTNAME: ${ADMIN_FIRSTNAME}"
printString "ADMIN LASTNAME: ${ADMIN_LASTNAME}"
printString "ADMIN EMAIL: ${ADMIN_EMAIL}"
printString "TIMEZONE: ${TIMEZONE}"
printString "LANGUAGE: ${LANGUAGE}"
printString "CURRENCY: ${CURRENCY}"
if [[ "$REMOTE_DB" ]]
then
printString "REMOTE DB HOST: ${REMOTE_DB_HOST}"
printString "REMOTE HOST: ${REMOTE_HOST}"
printString "REMOTE KEY: ${REMOTE_KEY}"
printString "LOCAL PORT: ${LOCAL_PORT}"
printString "REMOTE DB: ${REMOTE_DB}"
printString "REMOTE DB PASSWORD: ${REMOTE_DB_PASSWORD}"
fi
if foundSupportBackupFiles
then
return;
fi
if [ "${USE_SAMPLE_DATA}" ]
then
printString "Sample Data will be installed."
else
printString "Sample Data will NOT be installed."
fi
if [ "${INSTALL_EE}" ]
then
printString "Magento EE will be installed."
else
printString "Magento EE will NOT be installed."
fi
if [ "${INSTALL_B2B}" ]
then
printString "Magento B2B will be installed."
else
printString "Magento B2B will NOT be installed."
fi
}
function showWizard()
{
I=1;
while [ "$I" -eq 1 ]
do
if [ "$USE_WIZARD" -eq 1 ]
then
showComposerWizzard
showWizzardGit
noSourceWizard
wizard
fi
printLine
printConfirmation
if askConfirmation "Confirm That the Entered Data Is Correct? (y/N)"
then
I=0
else
USE_WIZARD=1
fi
done
}
function getConfigFiles()
{
local configPaths[0]="$HOME/$CONFIG_NAME"
configPaths[1]="$HOME/${CONFIG_NAME}.override"
local recursiveconfigs=$( (find "$(pwd)" -maxdepth 1 -name "${CONFIG_NAME}" ;\
x=$(pwd);\
while [ "$x" != "/" ] ;\
do x=$(dirname "$x");\
find "$x" -maxdepth 1 -name "${CONFIG_NAME}";\
done) | sed '1!G;h;$!d')
configPaths=("${configPaths[@]}" "${recursiveconfigs[@]}" "./$(basename ${CONFIG_NAME})" "$(getScriptDir)/master.conf");
echo "${configPaths[@]} "
return 0;
}
function loadConfigFile()
{
local filePath=
local configPaths=("$@");
for filePath in "${configPaths[@]}"
do
if [ -f "${filePath}" ]
then
source "$filePath"
USE_WIZARD=0
fi
done
generateDBName
}
function promptSaveConfig()
{
if [ "$FORCE" ]
then
return;
fi
_local=$(dirname "$BASE_PATH")
if [ "$_local" == "." ]
then
_local=
else
_local=$_local/
fi
if [ "$_local" != '/' ]
then
_local=${_local}\$CURRENT_DIR_NAME
fi
_configContent=$(cat << EOF
HTTP_HOST=$HTTP_HOST
BASE_PATH=$_local
DB_HOST=$DB_HOST
DB_NAME=$DB_NAME
DB_USER=$DB_USER
DB_PASSWORD=$DB_PASSWORD
MAGENTO_VERSION=$MAGENTO_VERSION
INSTALL_EE=$INSTALL_EE
INSTALL_B2B=$INSTALL_B2B
GIT_CE_REPO=$GIT_CE_REPO
GIT_EE_REPO=$GIT_EE_REPO
MAGE_MODE=$MAGE_MODE
BACKEND_FRONTNAME=$BACKEND_FRONTNAME
ADMIN_NAME=$ADMIN_NAME
ADMIN_PASSWORD=$ADMIN_PASSWORD
ADMIN_FIRSTNAME=$ADMIN_FIRSTNAME
ADMIN_LASTNAME=$ADMIN_LASTNAME
ADMIN_EMAIL=$ADMIN_EMAIL
TIMEZONE=$TIMEZONE
LANGUAGE=$LANGUAGE
CURRENCY=$CURRENCY
REMOTE_DB_HOST=$REMOTE_DB_HOST
REMOTE_HOST=$REMOTE_HOST
REMOTE_KEY=$REMOTE_KEY
LOCAL_PORT=$LOCAL_PORT
REMOTE_DB=$REMOTE_DB
REMOTE_DB_PASSWORD=$REMOTE_DB_PASSWORD
ELASTICSEARCH_HOST=$ELASTICSEARCH_HOST
ELASTICSEARCH_PORT=$ELASTICSEARCH_PORT
EOF
)
if [ "$(getConfigFiles)" ]
then
_currentConfigContent=$(cat "$HOME/$CONFIG_NAME")
if [ "$_configContent" == "$_currentConfigContent" ]
then
return;
fi
fi
configSavePath="$HOME/$CONFIG_NAME"
if [ -f "${configSavePath}" ]
then
configSavePath="./$CONFIG_NAME"
fi
if askConfirmation "Do you want save config to ${configSavePath} (y/N)"
then
cat << EOF > ${configSavePath}
$_configContent
EOF
printString "Config file has been created in ${configSavePath}";
fi
_local=
configSavePath=
}
function dropES()
{
# in general, the assumption is to take no care about if an index is deleted
# the goal here is only to request index deletion for any valid config we can find
local es_engine es_host es_port es_prefix elasticsuite version versions=("" "5" "6" "7")
for version in "${versions[@]}"
do
es_host=$(getConfig "catalog/search/elasticsearch${version}_server_hostname" "value");
es_port=$(getConfig "catalog/search/elasticsearch${version}_server_port" "value");
es_prefix=$(getConfig "catalog/search/elasticsearch${version}_index_prefix" "value");
dropEsIndex "$es_host" "$es_port" "$es_prefix"
done
es_host=$(getConfig "amasty_elastic/connection/server_hostname" "value");
es_port=$(getConfig "amasty_elastic/connection/server_port" "value");
es_prefix=$(getConfig "amasty_elastic/connection/index_prefix" "value");
dropEsIndex "$es_host" "$es_port" "$es_prefix"
elasticsuite=$(getConfig "smile_elasticsuite_core_base_settings/es_client/servers" "value");
es_host=${elasticsuite%%:*}
es_port=${elasticsuite/*:/}
es_prefix=$(getConfig "smile_elasticsuite_core_base_settings/indices_settings/alias" "value");
dropEsIndex "$es_host" "$es_port" "$es_prefix"
}
function dropEsIndex()
{
local host="$1" port="$2" index="$3"
if [[ -z "$host" ]] || [[ -z "$port" ]] || [[ -z "$index" ]]; then
return 0
fi
curl -S -s -o /dev/null -X DELETE "$host:$port/$index*"
return 0
}
function dropDB()
{
SQLQUERY="DROP DATABASE IF EXISTS ${DB_NAME}";
mysqlQuery
}
function createNewDB()
{
SQLQUERY="CREATE DATABASE IF NOT EXISTS ${DB_NAME}";
mysqlQuery
}
function restore_db()
{
dropDB
createNewDB
CMD="gunzip -cf \"$(getDbDumpFilename)\""
if which pv > /dev/null
then
CMD="pv \"$(getDbDumpFilename)\" | gunzip -cf";
fi
# Don't be confused by double gunzip in following command. Some poorly
# configured web servers can gzip everything including gzip files
CMD="${CMD} | gunzip -cf | sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/'
| sed -e 's/TRIGGER[ ][\`][A-Za-z0-9_]*[\`][.]/TRIGGER /'
| sed -e 's/AFTER[ ]\(INSERT\)\{0,1\}\(UPDATE\)\{0,1\}\(DELETE\)\{0,1\}[ ]ON[ ][\`][A-Za-z0-9_]*[\`][.]/AFTER \1\2\3 ON /'
| grep -v 'mysqldump: Couldn.t find table' | grep -v 'mysqldump: Couldn.t execute' | grep -v 'Warning: Using a password'
| ${BIN_MYSQL} -h${DB_HOST} -u${DB_USER} --password=\"${DB_PASSWORD}\" --force $DB_NAME";
runCommand
validateDatabaseDumpDataExists
}
function validateDatabaseDumpDataExists()
{
local isError=
if [ -z "$(getAllTables \"$(getTablePrefix)store\")" ]
then
printError "The store table is not found"
isError="1"
fi
if [ -z "$(getAllStores)" ]
then
printError "The store table missing data"
isError="1"
fi
if [ -z "$(getAllWebsites)" ]
then
printError "The store_website table missing data"
isError="1"
fi
[[ "$isError" ]] && { printErrorAndExit 257 "MySQL DB Dump is corrupt. For on-prem, please request a new MySQL Dump from the merchant and ensure it is created using the mysqldump utility and not bin/magento support:db:backup. For Magento-Cloud, please regenerate a new MySQL Dump by using the ZD Dump Widget / cloud-teleport." "Missing data DB Dump"; }
}
function restore_code()
{
EXTRACT_FILENAME="$(getCodeDumpFilename)"
extract
CMD="mkdir -p var pub/media pub/static"
runCommand
}
function configure_files()
{
CMD="find -L ./pub -type l -delete"
runCommand
updateMagentoEnvFile
overwriteOriginalFiles
#CMD="find . -type d -exec chmod 775 {} \; && find . -type f -exec chmod 664 {} \;"
CMD="chmod -R 775 ."
runCommand
CMD="${BIN_PHP} ${BIN_COMPOSER} dump-autoload"
runCommand
patchDumps
}
function add_remote()
{
updateEnvFileRemote
patchRemote
}
function getRemoteDBUser()
{
local user=(${REMOTE_DB//_/ })
echo ${user[0]} ;
}
function updateEnvFileRemote()
{
local deployConfigurator=$(cat << EOF
<?php
\$dbName = '${REMOTE_DB}';
\$dbUser = '$(getRemoteDBUser)';
\$dbPassword = '${REMOTE_DB_PASSWORD}';
\$localPort = '${LOCAL_PORT}';
EOF
);
deployConfigurator+=$(cat << 'EOF'
function updateDbConnection($envConfig, $connectionDetails)
{
unset($envConfig['db']['slave_connection']);
foreach ($envConfig['db'] as $key => $connections) {
if ($key != 'connection') {
continue;
}
foreach ($connections as $connectionName => $connectionParams) {
$envConfig['db'][$key][$connectionName] = $connectionDetails;
}
}
return $envConfig;
}
$envConfig = require 'app/etc/env.php';
$envConfig = updateDbConnection($envConfig, array(
'host' => "127.0.0.1:$localPort",
'dbname' => $dbName,
'username' => $dbUser,
'password' => "$dbPassword",
'model' => 'mysql4',
'engine' => 'innodb',
'initStatements' => 'SET NAMES utf8;',
'active' => '1'
));
echo "<?php\nreturn " . var_export($envConfig, true) . "\n;";
EOF
);
echo "$deployConfigurator" | ${BIN_PHP} > app/etc/env.php.generated
mv app/etc/env.php.generated app/etc/env.php
}
function addToBootstrap()
{
echo "$1" >> app/bootstrap.php;
}
function patchRemote()
{
local sshKey=''
if [[ "$REMOTE_KEY" ]]
then
sshKey="-i ${REMOTE_KEY} "
fi
addToBootstrap "//patched by m2install."
local ssh_command="ssh ${sshKey}-o ConnectTimeout=10 -o StrictHostKeyChecking=no -4fN -L ${LOCAL_PORT}:${REMOTE_DB_HOST} ${REMOTE_HOST}"
if ! pgrep -f -x "${ssh_command}" > /dev/null
then
echo "Start tunnel"
eval $ssh_command >> /dev/null
fi
SQLQUERY="SELECT code FROM ${REMOTE_DB}.$(getTablePrefix)store WHERE code != 'admin';";
local stores=$(mysql -h127.0.0.1 -N -u$(getRemoteDBUser) -P${LOCAL_PORT} --execute="${SQLQUERY}")
echo "$stores" | while IFS= read -r line ;
do
addToBootstrap "\$_ENV['CONFIG__STORES__${line}__WEB__SECURE__BASE_URL'] = '${BASE_URL}';"
addToBootstrap "\$_ENV['CONFIG__STORES__${line}__WEB__UNSECURE__BASE_URL'] = '${BASE_URL}';"
done
addToBootstrap "\$_ENV['CONFIG__DEFAULT__WEB__UNSECURE__BASE_URL'] = '${BASE_URL}';"
addToBootstrap "\$_ENV['CONFIG__DEFAULT__WEB__SECURE__BASE_URL'] = '${BASE_URL}';"
addToBootstrap "\$command = '$ssh_command';"
addToBootstrap 'exec("ps aux | grep -v \" grep\" | grep \"$command\" | tr -s \" \" | cut -d \" \" -f 2", $pids);'
addToBootstrap 'if (count($pids) === 0) {'
addToBootstrap ' exec($command . " >> /dev/null", $output, $exitCode);';
addToBootstrap ' if ($exitCode > 0) {'
addToBootstrap ' throw new \Exception("Remote Host ${REMOTE_HOST} is unavailable, check your network settings or VPN connection");'
addToBootstrap ' }'
addToBootstrap ' exec("ps aux | grep -v \" grep\" | grep \"$command\" | tr -s \" \" | cut -d \" \" -f 2", $pids);'
addToBootstrap '}'
addToBootstrap 'file_put_contents("kill_tunnel.sh", PHP_EOL . "kill " . implode(" ", $pids));'
addToBootstrap ""
}
function patchDumps()
{
patch -p1 <<'EOF'
diff --git a/vendor/magento/module-backend/Block/Dashboard/Orders/Grid.php b/vendor/magento/module-backend/Block/Dashboard/Orders/Grid.php
index 5027978..9df3c24 100644
--- a/vendor/magento/module-backend/Block/Dashboard/Orders/Grid.php
+++ b/vendor/magento/module-backend/Block/Dashboard/Orders/Grid.php
@@ -92,6 +92,11 @@ class Grid extends \Magento\Backend\Block\Dashboard\Grid
protected function _afterLoadCollection()
{
foreach ($this->getCollection() as $item) {
+ // patched by m2install.
+ // To revert patch remove next lines from 95 to 99
+ if (is_null($item->getBillingAddress())) {
+ return $this;
+ }
$item->getCustomer() ?: $item->setCustomer($item->getBillingAddress()->getName());
}
return $this;
EOF
}
function appConfigImport()
{
if ${BIN_PHP} bin/magento | grep -q app:config:import
then
CMD="$BIN_PHP $BIN_MAGE app:config:import -n"
runCommand
fi
}
function validateDeploymentFromDumps()
{
local files=(
'composer.json'
'composer.lock'
'pub/index.php'
'pub/static.php'
);
if ! isPubRequired
then
files+=('index.php')
fi