forked from cytopia/devilbox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dvl.sh
executable file
·793 lines (697 loc) · 24.4 KB
/
dvl.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
#!/usr/bin/env bash
# Use colors, but only if connected to a terminal, and that terminal
# supports them.
if which tput >/dev/null 2>&1; then
ncolors=$(tput colors)
fi
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
PURPLE=$(tput setaf 5)
CYAN=$(tput setaf 6)
LIGHT_GRAY=$(tput setaf 7)
DARK_GRAY=$(tput setaf 0)
BOLD="$(tput bold)"
NORMAL="$(tput sgr0)"
else
RED=""
GREEN=""
YELLOW=""
BLUE=""
PURPLE=""
CYAN=""
LIGHT_GRAY=""
DARK_GRAY=""
BOLD=""
NORMAL=""
fi
## Basic wrappers around exit codes
OK_CODE=0
KO_CODE=1
was_success() {
local exit_code=$?
[ "$exit_code" -eq "$OK_CODE" ]
}
was_error() {
local exit_code=$?
[ "$exit_code" -eq "$KO_CODE" ]
}
die() {
local exit_code=$1
if [ -n "$exit_code" ]; then
exit "$exit_code"
else
exit "$?"
fi
}
error() {
local message=$1
printf "%s %s\n" "${RED}[✘]" "${NORMAL}$message" >&2
die "$KO_CODE"
}
success() {
local message=$1
printf "%s %s\n" "${GREEN}[✔]" "${NORMAL}$message"
}
info() {
local message=$1
printf "%s %s\n" "${YELLOW}[!]" "${NORMAL}$message"
}
question() {
local message=$1
printf "%s %s\n" "${CYAN}[?]" "${NORMAL}$message"
}
safe_cd() {
local path=$1
local error_msg=$2
if [[ ! -d "$path" ]]; then
error "$error_msg"
fi
cd "$path" >/dev/null || error "$error_msg"
}
function get_workspace_path() {
if [[ ! -z "$DEVILBOX_PATH" ]]; then
printf %s "${DEVILBOX_PATH}"
else
printf %s "$HOME/.devilbox"
fi
}
function get_yq_version() {
printf %s "$( \
curl -sS 'https://github.com/mikefarah/yq/releases' \
| grep -Eo '/mikefarah/yq/releases/tag/v?[.0-9]+"' \
| grep -Eo 'v?[.0-9]+' \
| sort -V \
| tail -1 \
)"
}
function download_yq() {
local uname
local aarch
local DEB_HOST_ARCH
uname="$(uname)"
aarch="$(uname -m)"
if [ "${uname}" = "Linux" ]; then
if ! command -v dpkg-architecture 2>&1 >/dev/null; then
DEB_HOST_ARCH="$aarch"
else
DEB_HOST_ARCH="$( dpkg-architecture --query DEB_HOST_ARCH )"
fi
elif [ "${uname}" = "Darwin" ]; then
DEB_HOST_ARCH="$aarch"
fi
if [ "${aarch}" = "i386" ] || [ "$aarch" = "x86_64" ]; then
DEB_HOST_ARCH="amd64"
fi
if [ "${DEB_HOST_ARCH}" = "amd64" ] || [ "${DEB_HOST_ARCH}" = "arm64" ]; then
YQ_UNAME=$(echo "$uname" | tr '[:upper:]' '[:lower:]')
if [[ ! -d "$DEVILBOX_PATH/.tests/binaries/" ]]; then
mkdir -p "$DEVILBOX_PATH/.tests/binaries/"
fi
YQ_URL="https://github.com/mikefarah/yq/releases/download/$(get_yq_version)/yq_${YQ_UNAME}_${DEB_HOST_ARCH}" \
&& curl -sS -L --fail "${YQ_URL}" > "$DEVILBOX_PATH/.tests/binaries/yq"
fi
chmod +x "$DEVILBOX_PATH/.tests/binaries/yq";
}
# Checker
if [[ -z "$DEVILBOX_PATH" ]]; then
error "Devilbox not found, please make sure it is installed in your home directory or use DEVILBOX_PATH in your profile."
fi
#safe_cd "$(get_workspace_path)" "Devilbox not found, please make sure it is installed in your home directory or use DEVILBOX_PATH in your profile."
DVLBOX_PATH="$( cd "${DEVILBOX_PATH}" && pwd -P )"
SCRIPT_PATH="$( cd "${DVLBOX_PATH}/.tests/scripts" && pwd -P )"
# shellcheck disable=SC1090
source "${SCRIPT_PATH}/.lib.sh"
# Check and download yq binary
if [[ ! -f "$DEVILBOX_PATH/.tests/binaries/yq" ]]; then
download_yq
fi
# -------------------------------------------------------------------------------------------------
# ENTRYPOINT
# -------------------------------------------------------------------------------------------------
###
### Get required env values
###
HTTPD_SERVER="$( "${SCRIPT_PATH}/env-getvar.sh" "HTTPD_SERVER" )"
HTTPD_TEMPLATE_DIR="$( "${SCRIPT_PATH}/env-getvar.sh" "HTTPD_TEMPLATE_DIR" )"
HTTPD_DOCROOT_DIR="$( "${SCRIPT_PATH}/env-getvar.sh" "HTTPD_DOCROOT_DIR" )"
TLD_SUFFIX="$( "${SCRIPT_PATH}/env-getvar.sh" "TLD_SUFFIX" )"
MYSQL_ROOT_PASSWORD="$( "${SCRIPT_PATH}/env-getvar.sh" "MYSQL_ROOT_PASSWORD" )"
WEBAPP_DIR="$(get_workspace_path)/data/www"
HTTPD_WORKDIR="/shared/httpd"
BACKUP_WORKDIR="/shared/backups"
AWS_BASEDIR="src"
WEBAPP_STACK=""
MAGE_MODE=""
MAGE_INFRA=""
WEB_MULTI="N"
APPNAME="$$$"
PARENT_APPNAME="$$$"
APPREPOSITORY=""
DBNAME="$$$"
APPDOMAINS=""
APPDOMAINS_CRT=""
PUBLICPATH="current"
PHP_VERSION=""
PROXY_PORT="3000"
CURRENT_DIR="$(pwd)"
PROJECT_DIR="${CURRENT_DIR/$WEBAPP_DIR\///}"
TARGET_WORKDIR="$HTTPD_WORKDIR$PROJECT_DIR"
CONFIG_FILE=".devilbox.yaml"
TEMPLATE_CONFIG="$DEVILBOX_PATH/.tests/devilbox-template-config.yaml"
YQ_BINARY="$DEVILBOX_PATH/.tests/binaries/yq"
# Read-only variables
readonly VERSION="1.2.0"
function main {
if [[ $# -eq 0 ]] ; then
Usage --ansi
echo "${BOLD}"
$YQ_BINARY --version
echo "${NORMAL}"
else
case "$1" in
up|start)
shift;
StartServices "$@"
;;
down|stop)
shift;
StopServices "$@"
;;
restart)
shift;
RestartServices "$@"
;;
reset)
ResetServices "$@"
;;
doctor)
DoctorForBox "$@"
;;
init)
shift;
InitializeProject "$@"
;;
exec)
shift;
ExecShell "$@"
;;
db:import|db-import)
shift;
DatabaseImport "$@"
;;
magento)
shift;
MagentoCommand "$@"
;;
magerun)
shift;
MagerunCommand "$@"
;;
composer)
shift;
ComposerCommand "$@"
;;
shell)
shift;
OpenShell "$@"
;;
generate-yaml)
shift;
CreateYamlConf "$@"
;;
sync-httpd)
shift;
SyncHttpdConf "$@"
;;
update:docroot|update-docroot)
shift;
UpdateDocRoots "$@"
;;
--no-ansi)
Usage --no-ansi
;;
help|-h|--help|--ansi)
Usage --ansi
;;
*)
error "Unknown command $1, see -h for help."
;;
esac
fi
}
function __get_default_containers {
if [[ ! -z "$DEVILBOX_CONTAINERS" ]]; then
printf %s "${DEVILBOX_CONTAINERS}"
else
printf %s "bind httpd php php74 php81 php82 php83 mysql redis elastic mailhog"
fi
}
function BaseCommand {
(cd "$DEVILBOX_PATH"; docker "$@")
}
function BaseComposeCommand {
if hash docker-compose 2>/dev/null; then
(cd "$DEVILBOX_PATH"; docker-compose "$@")
else
(cd "$DEVILBOX_PATH"; docker compose "$@")
fi
}
function StartServices {
BaseComposeCommand up $(__get_default_containers) -d
}
function StopServices {
BaseComposeCommand down && BaseComposeCommand rm -f
}
function RestartServices {
if [[ -n "$*" ]]; then
BaseCommand restart "$@"
else
StopServices && StartServices
fi
}
function OpenShell {
if [[ -z "$*" ]]; then
BaseComposeCommand exec --user devilbox php bash -l
else
BaseComposeCommand exec --user devilbox "$1" bash -l
fi
}
function ExecShell {
info "Workdir: $TARGET_WORKDIR"
BaseComposeCommand exec --user devilbox php bash -c "cd $TARGET_WORKDIR; $*"
}
function ExecShellTTY {
BaseComposeCommand exec --no-TTY --user devilbox php bash -c "$@"
}
function MagentoCommand {
BaseComposeCommand exec --workdir "$TARGET_WORKDIR" --user devilbox php bash -c "php -dmemory_limit=-1 bin/magento $*"
}
function MagerunCommand {
BaseComposeCommand exec --workdir "$TARGET_WORKDIR" --user devilbox php bash -c "php -dmemory_limit=-1 magerun $*"
}
function ComposerCommand {
BaseComposeCommand exec --workdir "$TARGET_WORKDIR" --user devilbox php bash -c "composer $*"
}
function DatabaseImport {
if [[ "$#" -lt 2 ]]; then
info "Ensure your backup file exists in ./backups directory on host machine."
error "Missing required arguments filename /or database name. E.g dvl db:import <filename> <database_name>"
fi
local filename="$1"
local dbname="$2"
if [[ "$filename" == *.sql ]]; then
echo -ne "${YELLOW}${BOLD}[!] Importing $filename into $dbname..."
(ExecShellTTY "mysql --host=mysql --user=root --password='$MYSQL_ROOT_PASSWORD' -e 'CREATE DATABASE IF NOT EXISTS ${dbname}'" && ExecShellTTY "mysql --host=mysql --user=root --password='$MYSQL_ROOT_PASSWORD' $dbname < $BACKUP_WORKDIR/$filename") &
spinner
echo -ne "...${NORMAL} ${GREEN}DONE ✔${NORMAL}"
echo ""
elif [[ "$filename" == *.sql.gz ]]; then
echo -ne "${YELLOW}${BOLD}[!] Extracting $filename and importing it into $dbname..."
(ExecShellTTY "mysql --host=mysql --user=root --password='$MYSQL_ROOT_PASSWORD' -e 'CREATE DATABASE IF NOT EXISTS ${dbname}'" && ExecShellTTY "zcat $BACKUP_WORKDIR/$filename | mysql --host=mysql --user=root --password='$MYSQL_ROOT_PASSWORD' $dbname") &
spinner
echo -ne "...${NORMAL} ${GREEN}DONE ✔${NORMAL}"
echo ""
else
error "File type (name: ${filename}) does not supported yet."
fi
}
function InteractiveQuestions {
# Define the app name
while [[ $APPNAME =~ [^-.a-z0-9] ]] || [[ $APPNAME == '' ]]
do
read -r -p "${CYAN}Please enter your webapp name (lowercase, alphanumeric):${NORMAL} " APPNAME
done
APPDOMAINS="https://$APPNAME.$TLD_SUFFIX"
echo -ne "${YELLOW}Your webapp name set to: $APPNAME"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
echo -ne "${YELLOW}Domain of webapp set to: $APPDOMAINS"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
# Choose a web application stack
read -r -p "${CYAN}Please choose web application stack (magento, nodejs, laravel, shopify, bigcommerce, phpweb)? [magento]${NORMAL} " response
case "$response" in
nodejs)
WEBAPP_STACK="nodejs"
echo -ne "${YELLOW}Node.js General Application"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
;;
laravel)
WEBAPP_STACK="laravel"
echo -ne "${YELLOW}Laravel"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
;;
shopify)
WEBAPP_STACK="shopify"
echo -ne "${YELLOW}Shopify"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
;;
bigcommerce)
WEBAPP_STACK="bigcommerce"
echo -ne "${YELLOW}BigCommerce"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
;;
phpweb)
WEBAPP_STACK="phpweb"
echo -ne "${YELLOW}General PHP Application"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
;;
magento|*)
WEBAPP_STACK="magento"
echo -ne "${YELLOW}Magento 2 (Pre-configured for production-grade Magento 2 application)"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
;;
esac
# Setup subdomain configuration
read -r -p "${CYAN}Is this webapp a sub-domain of another web application (Y/N)? [N]${NORMAL} " response
case "$response" in
[yY][eE][sS]|[yY])
WEB_MULTI="Y"
echo -ne "${YELLOW}Your current webapp is configured to be a sub-domain of another web application."
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
;;
[nN][oO]|[nN]|*)
WEB_MULTI="N"
echo -ne "${YELLOW}Your current webapp is not configured to be a sub-domain of another web application."
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
;;
esac
if [[ "$WEB_MULTI" == "Y" ]]; then
while [[ $PARENT_APPNAME =~ [^-.a-z0-9] ]] || [[ $PARENT_APPNAME == '' ]]
do
read -r -p "${CYAN}What is the main-entry webapp name?${NORMAL} " PARENT_APPNAME
done
echo -ne "${YELLOW}Your parent webapp name set to: $PARENT_APPNAME"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
fi
if [[ "$WEBAPP_STACK" == "magento" ]]; then
# Set Magento Mode
read -r -p "${CYAN}Which deploy mode you would like to setup (developer, production)? [production]${NORMAL} " response
case "$response" in
developer|dev)
MAGE_MODE="developer"
echo -ne "${YELLOW}Your Magento application mode has been set to ${MAGE_MODE}"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
;;
production|prod|*)
MAGE_MODE="production"
echo -ne "${YELLOW}Your Magento application mode has been set to ${MAGE_MODE}"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
;;
esac
# Set Infra Mode
read -r -p "${CYAN}What is the infrastructure of your Magento project (aws, cloud)? [cloud]${NORMAL} " response
case "$response" in
aws)
MAGE_INFRA="aws"
echo -ne "${YELLOW}Your infrastructure type has been set to ${MAGE_INFRA}"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
;;
cloud|*)
MAGE_INFRA="cloud"
echo -ne "${YELLOW}Your infrastructure type has been set to ${MAGE_INFRA}"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
;;
esac
fi
if [[ "$WEB_MULTI" == "N" ]]; then
# Define the app repository
while [[ $APPREPOSITORY == '' ]]
do
read -r -p "${CYAN}Please enter your webapp repository ([email protected]:org/repo.git):${NORMAL} " APPREPOSITORY
done
APPREPOSITORY=${APPREPOSITORY//"git clone "}
echo -ne "${YELLOW}Your webapp repository set to: $APPREPOSITORY"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
fi
# Choose PHP version
read -r -p "${CYAN}Please choose PHP version of your webapp? [8.1]${NORMAL} " response
case "$response" in
5.2|52|5.3|53|5.4|54|5.5|55|5.6|56)
PHP_VERSION=$(awk '{gsub(/[.]/,"");print $NF}' <<< "php$response")
;;
7.0|70|7.1|71|7.2|72|7.3|73|7.4|74)
PHP_VERSION=$(awk '{gsub(/[.]/,"");print $NF}' <<< "php$response")
;;
8.0|80|8.2|82|8.3|83)
PHP_VERSION=$(awk '{gsub(/[.]/,"");print $NF}' <<< "php$response")
;;
8.1|81|*)
PHP_VERSION="php81"
;;
esac
echo -ne "${YELLOW}PHP version of webapp set to $PHP_VERSION"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
if [[ "$WEBAPP_STACK" != "magento" ]] && [[ "$WEBAPP_STACK" != "phpweb" ]] && [[ "$WEBAPP_STACK" != "laravel" ]]; then
# Define port proxy
read -r -p "${CYAN}Please choose port proxy of your webapp? [3000]${NORMAL} " response
PROXY_PORT=$response
echo -ne "${YELLOW}Proxy port of webapp set to $PROXY_PORT"
echo -ne "...${NORMAL} ${GREEN}DONE${NORMAL}"
echo ""
fi
}
function InitializeProject {
if [[ ! -f "$CURRENT_DIR/$CONFIG_FILE" ]]; then
InteractiveQuestions
fi
BootstrapWebApplication "$WEBAPP_STACK"
GenerateYamlConf "$WEBAPP_STACK" "$APPNAME" "$APPDOMAINS" "$WEB_MULTI" "$MAGE_INFRA" "$APPREPOSITORY" "$PHP_VERSION" "$PROXY_PORT"
}
function UpdateConfig {
local filePath="${@: -1}"
local nullInput=""
if [[ ! -f "$filePath" ]]; then
nullInput=" --null-input"
fi
"$YQ_BINARY$nullInput" "$@"
}
function GenerateYamlConf {
local baseAppDir="$WEBAPP_DIR/$2"
local filePath="$baseAppDir/$HTTPD_DOCROOT_DIR/$CONFIG_FILE"
if [[ "$1" == "magento" ]] && [[ "$5" == "aws" ]]; then
filePath="$baseAppDir/$CONFIG_FILE"
fi
if [[ ! -f "$filePath" ]]; then
\cp "$TEMPLATE_CONFIG" "$filePath"
current_stack="$1" \
app_name="$2" \
domain="$3" \
is_subdomain="$4" \
infra_type="${5:-generic}" \
repo_url="${6:-N/A}" \
php_version="$7" \
proxy_port="$8" \
UpdateConfig '(.. | select(tag == "!!str")) |= envsubst(ne, ff)' "$TEMPLATE_CONFIG" > "$filePath"
fi
}
function BootstrapWebApplication {
# Start configuring everything
echo -ne "${YELLOW}Please wait, we are configuring your web application"
local devilboxConfDir="$WEBAPP_DIR/$APPNAME/$HTTPD_TEMPLATE_DIR"
local currentStack="$1"
local templateType
# Creating dirs
if [[ ! -d "$WEBAPP_DIR" ]]; then
mkdir -p "$WEBAPP_DIR"
fi
if [[ -d "$WEBAPP_DIR/$APPNAME" ]]; then
echo -ne "... The target appname already exists in $WEBAPP_DIR${NORMAL} ${RED}FAILURE[✘]${NORMAL}"
echo ""
exit 1;
fi
if [[ "$WEB_MULTI" == "N" ]] && [[ "$MAGE_INFRA" == "aws" ]]; then
git clone --quiet "$APPREPOSITORY" "$WEBAPP_DIR/$APPNAME" > /dev/null
if [[ "$AWS_BASEDIR" != "$HTTPD_DOCROOT_DIR" ]]; then
(cd "$WEBAPP_DIR/$APPNAME" || exit; ln -snf "$AWS_BASEDIR" "$HTTPD_DOCROOT_DIR" > /dev/null)
fi
fi
mkdir -p "$WEBAPP_DIR/$APPNAME"
mkdir -p "$devilboxConfDir"
# General Configuration
if [[ -f "$DEVILBOX_PATH/cfg/vhost-gen/backend.cfg-example-php-multi" ]] && [[ $currentStack != "nodejs" ]] && [[ $currentStack != "bigcommerce" ]] && [[ $currentStack != "shopify" ]]; then
cat "$DEVILBOX_PATH/cfg/vhost-gen/backend.cfg-example-php-multi" | sed "s/PHP_VERSION/$PHP_VERSION/g" > "$devilboxConfDir/backend.cfg"
fi
if [[ -f "$DEVILBOX_PATH/cfg/vhost-gen/backend.cfg-example-rproxy-multi" ]] && [[ $currentStack != "magento" ]] && [[ $currentStack != "laravel" ]] && [[ $currentStack != "phpweb" ]]; then
cat "$DEVILBOX_PATH/cfg/vhost-gen/backend.cfg-example-rproxy-multi" | sed "s/PHP_VERSION/$PHP_VERSION/g" | sed "s/PROXY_PORT/$PROXY_PORT/g" > "$devilboxConfDir/backend.cfg"
fi
if [[ "$WEB_MULTI" == "N" ]]; then
if [[ "$MAGE_INFRA" == "cloud" ]] || [[ "$MAGE_INFRA" == "" ]]; then
git clone --quiet "$APPREPOSITORY" "$WEBAPP_DIR/$APPNAME/$HTTPD_DOCROOT_DIR" > /dev/null
fi
elif [[ "$WEB_MULTI" == "Y" ]]; then
(cd "$WEBAPP_DIR" || exit; ln -snf "../$PARENT_APPNAME/$HTTPD_DOCROOT_DIR" "$WEBAPP_DIR/$APPNAME/$HTTPD_DOCROOT_DIR" > /dev/null)
fi
case "$currentStack" in
magento)
templateType="magento2"
;;
nodejs)
templateType="rproxy"
;;
shopify)
templateType="rproxy"
;;
bigcommerce)
templateType="rproxy"
;;
laravel)
templateType="vhost"
;;
phpweb|*)
templateType="vhost"
;;
esac
if [[ "$HTTPD_SERVER" =~ "nginx" ]]; then
cp "$DEVILBOX_PATH/cfg/vhost-gen/nginx.yml-example-$templateType" "$devilboxConfDir/nginx.yml"
elif [[ "$HTTPD_SERVER" = "apache-2.2" ]]; then
cp "$DEVILBOX_PATH/cfg/vhost-gen/apache22.yml-example-$templateType" "$devilboxConfDir/apache22.yml"
elif [[ "$HTTPD_SERVER" = "apache-2.4" ]]; then
cp "$DEVILBOX_PATH/cfg/vhost-gen/apache24.yml-example-$templateType" "$devilboxConfDir/apache24.yml"
fi
echo -ne "...${NORMAL} ${GREEN}DONE ✔${NORMAL}"
echo ""
}
function CreateYamlConf {
if [[ -f "$CURRENT_DIR/$CONFIG_FILE" ]]; then
error "File $CONFIG_FILE already exists in current directory!"
fi
local checkForGitDir
checkForGitDir=$(find "$CURRENT_DIR" -maxdepth 1 -type d -name '*.git*' -print -quit)
if [[ -z "$checkForGitDir" ]]; then
error "Please ensure you are on root of project directory (where exists .git directory)"
fi
InteractiveQuestions
GenerateYamlConf "$WEBAPP_STACK" "$APPNAME" "$APPDOMAINS" "$WEB_MULTI" "$MAGE_INFRA" "$APPREPOSITORY" "$PHP_VERSION" "$PROXY_PORT"
}
function UpdateDocRoots {
if [[ "$#" -lt 1 ]]; then
error "Missing required argument: old document root dir. E.g dvl update:docroot <old_htdoc_root_dir>"
fi
local oldHttpdDocRoot="$1"
local hasEnvDir
echo "${YELLOW}${BOLD}Please wait, we are update your document roots..."
echo ""
for appName in "$WEBAPP_DIR"/*; do
hasEnvDir=$(find "$appName" -maxdepth 1 -type d -name '*env*' -print -quit)
for fileOrDir in "$appName"/*; do
if [[ -L "$fileOrDir" ]] || [[ -d "$fileOrDir" ]]; then
if [[ $(basename "$fileOrDir") == "$oldHttpdDocRoot" ]] && [[ $(basename "$fileOrDir") != "$HTTPD_DOCROOT_DIR" ]]; then
echo -ne "${YELLOW}${BOLD}[!] Update symbolic links for $appName..."
if [[ "$hasEnvDir" =~ "env" ]]; then
(cd "$appName" || exit; ln -snf "$AWS_BASEDIR" "$HTTPD_DOCROOT_DIR" > /dev/null) &
spinner
else
(cd "$appName" || exit; mv "$(basename "$fileOrDir")" "$HTTPD_DOCROOT_DIR" > /dev/null) &
spinner
fi
echo -ne "...${NORMAL} ${GREEN}DONE ✔${NORMAL}"
echo ""
fi
fi
# TODO: Update content of symbolic links - planned for 1.2.2
# if [[ -L "$fileOrDir" ]]; then
# echo $fileOrDir
# echo "$(readlink "$fileOrDir")"
# fi
done
done
}
function SyncHttpdConf {
echo -ne "${YELLOW}Please wait, we are syncing your Httpd configuration"
for appName in "$WEBAPP_DIR"/*; do
if [[ "$HTTPD_SERVER" =~ "nginx" ]]; then
\cp "$DEVILBOX_PATH/cfg/vhost-gen/nginx.yml-example-magento2" "$appName/$HTTPD_TEMPLATE_DIR/nginx.yml"
elif [[ "$HTTPD_SERVER" = "apache-2.2" ]]; then
\cp "$DEVILBOX_PATH/cfg/vhost-gen/apache22.yml-example-magento2" "$appName/$HTTPD_TEMPLATE_DIR/apache22.yml"
elif [[ "$HTTPD_SERVER" = "apache-2.4" ]]; then
\cp "$DEVILBOX_PATH/cfg/vhost-gen/apache24.yml-example-magento2" "$appName/$HTTPD_TEMPLATE_DIR/apache24.yml"
fi
done
echo -ne "...${NORMAL} ${GREEN}DONE ✔${NORMAL}"
echo ""
}
function DoctorForBox {
/bin/bash "$DEVILBOX_PATH/check-config.sh"
}
function Usage {
case "$1" in
--ansi)
echo "${YELLOW}DevilBox v${VERSION}${NORMAL}"
echo
echo "${YELLOW}Usage:"
echo "${NORMAL}" "dvl [commands] [options]"
echo
echo "${YELLOW}Options:${NORMAL}"
echo "${GREEN}" "--version${NORMAL}(-v) Display current version."
echo "${GREEN}" "--help${NORMAL}(-h) Display this help message."
echo "${GREEN}" "--quiet${NORMAL}(-q) Do not output any message."
echo "${GREEN}" "--ansi${NORMAL} Force ANSI output."
echo "${GREEN}" "--no-ansi${NORMAL} Disable ANSI output."
echo
echo "${YELLOW}Available commands:${NORMAL}"
echo "${GREEN}" "up${NORMAL}(start) Start designated devilbox services."
echo "${GREEN}" "down${NORMAL}(stop) Stop all Devilbox services."
echo "${GREEN}" "restart${NORMAL} Restart Devilbox service(s). Leave empty to restart all."
echo "${GREEN}" "doctor${NORMAL} Check your system for potential problems. Will exit with a non-zero status if any potential problems are found."
echo "${GREEN}" "reset${NORMAL} Shutdown and reset everyhing (USE with CAUTION)."
echo "${GREEN}" "init${NORMAL} Initialize a new project using DevilBox."
echo "${GREEN}" "shell${NORMAL} Open shell (php version as args)"
echo "${GREEN}" "exec${NORMAL} Exec a command directly from shell (command executed on main PHP container)"
echo "${GREEN}" "db-import${NORMAL} Restore a backup to database from ./backups directory"
echo "${GREEN}" "magento${NORMAL} Run Magento command from the current project directory"
echo "${GREEN}" "magerun${NORMAL} Run Magerun2 command from the current project directory"
echo "${GREEN}" "composer${NORMAL} Run Composer command from the current project directory"
echo "${GREEN}" "update-docroot${NORMAL} Update new document root for all current webapps"
echo "${GREEN}" "sync-httpd${NORMAL} Sync Httpd configuration to all current webapps"
;;
--no-ansi)
echo "DevilBox v${VERSION}"
echo
echo "Usage:"
echo " dvl [commands] [options]"
echo
echo "Options:"
echo " --version${NORMAL}(-v) Display current version."
echo " --help${NORMAL}(-h) Display this help message."
echo " --quiet${NORMAL}(-q) Do not output any message."
echo " --ansi${NORMAL} Force ANSI output."
echo " --no-ansi${NORMAL} Disable ANSI output."
echo
echo "Available commands:"
echo " up${NORMAL}(start) Start designated devilbox services."
echo " down${NORMAL}(stop) Stop all Devilbox services."
echo " restart${NORMAL} Restart Devilbox service(s). Leave empty to restart all."
echo " doctor${NORMAL} Check your system for potential problems. Will exit with a non-zero status if any potential problems are found."
echo " reset${NORMAL} Shutdown and reset everyhing (USE with CAUTION)."
echo " init${NORMAL} Initialize a new project using DevilBox."
echo " shell${NORMAL} Open shell (php version as args)"
echo " exec${NORMAL} Exec a command directly from shell (command executed on main PHP container)"
echo " db-import${NORMAL} Restore a backup to database from ./backups directory"
echo " magento${NORMAL} Run Magento command from the current project directory"
echo " magerun${NORMAL} Run Magerun2 command from the current project directory"
echo " composer${NORMAL} Run Composer command from the current project directory"
echo " update-docroot${NORMAL} Update new document root for all current webapps"
echo " sync-httpd${NORMAL} Sync Httpd configuration to all current webapps"
;;
esac
}
main "$@"