-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathez
executable file
·2796 lines (2211 loc) · 61.1 KB
/
ez
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
# This script was generated by bashly 1.1.10 (https://bashly.dannyb.co)
# Modifying it manually is not recommended
# :wrapper.bash3_bouncer
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
printf "bash version 4 or higher is required\n" >&2
exit 1
fi
# :command.master_script
# :command.version_command
version_command() {
echo "$version"
}
# :command.usage
ez_usage() {
if [[ -n $long_usage ]]; then
printf "ez - easy to setup, robust and production ready environment for Laravel using Docker, Docker Compose and bash script.\n"
echo
else
printf "ez - easy to setup, robust and production ready environment for Laravel using Docker, Docker Compose and bash script.\n"
echo
fi
printf "%s\n" "Usage:"
printf " ez COMMAND\n"
printf " ez [COMMAND] --help | -h\n"
printf " ez --version | -v\n"
echo
# :command.usage_commands
printf "%s\n" "Commands:"
printf " %s Docker Commands\n" "docker "
printf " %s Shared containers Commands\n" "shared "
printf " %s Laravel containers Commands\n" "laravel"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
printf " %s\n" "--version, -v"
printf " Show version number\n"
echo
# :command.usage_examples
printf "%s\n" "Examples:"
printf " ez docker install\n"
printf " ez d i\n"
echo
fi
}
# :command.usage
ez_docker_usage() {
if [[ -n $long_usage ]]; then
printf "ez docker - Docker Commands\n"
echo
else
printf "ez docker - Docker Commands\n"
echo
fi
printf "Alias: d\n"
echo
printf "%s\n" "Usage:"
printf " ez docker COMMAND\n"
printf " ez docker [COMMAND] --help | -h\n"
echo
# :command.usage_commands
printf "%s\n" "Commands:"
printf " %s add docker repository to apt sources, then install docker engine\n" "install "
printf " %s uninstall docker engine\n" "uninstall"
printf " %s removes all images, containers, and volumes, (You have to delete any edited configuration files manually)\n" "remove "
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
# :command.usage
ez_docker_install_usage() {
if [[ -n $long_usage ]]; then
printf "ez docker install - add docker repository to apt sources, then install docker engine\n"
echo
else
printf "ez docker install - add docker repository to apt sources, then install docker engine\n"
echo
fi
printf "Alias: i\n"
echo
printf "%s\n" "Usage:"
printf " ez docker install\n"
printf " ez docker install --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
# :command.usage
ez_docker_uninstall_usage() {
if [[ -n $long_usage ]]; then
printf "ez docker uninstall - uninstall docker engine\n"
echo
else
printf "ez docker uninstall - uninstall docker engine\n"
echo
fi
printf "Alias: u\n"
echo
printf "%s\n" "Usage:"
printf " ez docker uninstall\n"
printf " ez docker uninstall --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
# :command.usage
ez_docker_remove_usage() {
if [[ -n $long_usage ]]; then
printf "ez docker remove - removes all images, containers, and volumes, (You have to delete any edited configuration files manually)\n"
echo
else
printf "ez docker remove - removes all images, containers, and volumes, (You have to delete any edited configuration files manually)\n"
echo
fi
printf "Alias: r\n"
echo
printf "%s\n" "Usage:"
printf " ez docker remove\n"
printf " ez docker remove --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
# :command.usage
ez_shared_usage() {
if [[ -n $long_usage ]]; then
printf "ez shared - Shared containers Commands\n"
echo
else
printf "ez shared - Shared containers Commands\n"
echo
fi
printf "Alias: s\n"
echo
printf "%s\n" "Usage:"
printf " ez shared COMMAND\n"
printf " ez shared [COMMAND] --help | -h\n"
echo
# :command.usage_commands
printf "%s\n" "Commands:"
printf " %s build and start shared containers\n" "deploy "
printf " %s start shared containers\n" "start "
printf " %s stop shared containers\n" "stop "
printf " %s restart shared containers\n" "restart"
printf " %s remove shared containers\n" "down "
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
# :command.usage
ez_shared_deploy_usage() {
if [[ -n $long_usage ]]; then
printf "ez shared deploy - build and start shared containers\n"
echo
else
printf "ez shared deploy - build and start shared containers\n"
echo
fi
printf "Alias: d\n"
echo
printf "%s\n" "Usage:"
printf " ez shared deploy\n"
printf " ez shared deploy --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
# :command.usage
ez_shared_start_usage() {
if [[ -n $long_usage ]]; then
printf "ez shared start - start shared containers\n"
echo
else
printf "ez shared start - start shared containers\n"
echo
fi
printf "Alias: s\n"
echo
printf "%s\n" "Usage:"
printf " ez shared start\n"
printf " ez shared start --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
# :command.usage
ez_shared_stop_usage() {
if [[ -n $long_usage ]]; then
printf "ez shared stop - stop shared containers\n"
echo
else
printf "ez shared stop - stop shared containers\n"
echo
fi
printf "%s\n" "Usage:"
printf " ez shared stop\n"
printf " ez shared stop --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
# :command.usage
ez_shared_restart_usage() {
if [[ -n $long_usage ]]; then
printf "ez shared restart - restart shared containers\n"
echo
else
printf "ez shared restart - restart shared containers\n"
echo
fi
printf "Alias: r\n"
echo
printf "%s\n" "Usage:"
printf " ez shared restart\n"
printf " ez shared restart --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
# :command.usage
ez_shared_down_usage() {
if [[ -n $long_usage ]]; then
printf "ez shared down - remove shared containers\n"
echo
else
printf "ez shared down - remove shared containers\n"
echo
fi
printf "%s\n" "Usage:"
printf " ez shared down\n"
printf " ez shared down --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
# :command.usage
ez_laravel_usage() {
if [[ -n $long_usage ]]; then
printf "ez laravel - Laravel containers Commands\n"
echo
else
printf "ez laravel - Laravel containers Commands\n"
echo
fi
printf "Alias: l\n"
echo
printf "%s\n" "Usage:"
printf " ez laravel COMMAND\n"
printf " ez laravel [COMMAND] --help | -h\n"
echo
# :command.usage_commands
printf "%s\n" "Commands:"
printf " %s build and start Laravel containers\n" "deploy "
printf " %s start Laravel containers\n" "start "
printf " %s stop Laravel containers\n" "stop "
printf " %s restart Laravel containers\n" "restart"
printf " %s remove Laravel containers\n" "down "
printf " %s remove Laravel containers\n" "new "
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
# :command.usage
ez_laravel_deploy_usage() {
if [[ -n $long_usage ]]; then
printf "ez laravel deploy - build and start Laravel containers\n"
echo
else
printf "ez laravel deploy - build and start Laravel containers\n"
echo
fi
printf "Alias: d\n"
echo
printf "%s\n" "Usage:"
printf " ez laravel deploy APP_NAME APP_ENV\n"
printf " ez laravel deploy --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
# :command.usage_args
printf "%s\n" "Arguments:"
# :argument.usage
printf " %s\n" "APP_NAME"
printf " Laravel app name\n"
echo
# :argument.usage
printf " %s\n" "APP_ENV"
printf " Laravel app environment\n"
printf " Allowed: dev, test, staging, production\n"
echo
fi
}
# :command.usage
ez_laravel_start_usage() {
if [[ -n $long_usage ]]; then
printf "ez laravel start - start Laravel containers\n"
echo
else
printf "ez laravel start - start Laravel containers\n"
echo
fi
printf "Alias: s\n"
echo
printf "%s\n" "Usage:"
printf " ez laravel start APP_NAME APP_ENV [OPTIONS]\n"
printf " ez laravel start --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_flags
# :flag.usage
printf " %s\n" "--update, -u"
printf " update the source code before starting the container\n"
echo
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
# :command.usage_args
printf "%s\n" "Arguments:"
# :argument.usage
printf " %s\n" "APP_NAME"
printf " Laravel app name\n"
echo
# :argument.usage
printf " %s\n" "APP_ENV"
printf " Laravel app environment\n"
printf " Allowed: dev, test, staging, production\n"
echo
fi
}
# :command.usage
ez_laravel_stop_usage() {
if [[ -n $long_usage ]]; then
printf "ez laravel stop - stop Laravel containers\n"
echo
else
printf "ez laravel stop - stop Laravel containers\n"
echo
fi
printf "%s\n" "Usage:"
printf " ez laravel stop APP_NAME APP_ENV\n"
printf " ez laravel stop --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
# :command.usage_args
printf "%s\n" "Arguments:"
# :argument.usage
printf " %s\n" "APP_NAME"
printf " Laravel app name\n"
echo
# :argument.usage
printf " %s\n" "APP_ENV"
printf " Laravel app environment\n"
printf " Allowed: dev, test, staging, production\n"
echo
fi
}
# :command.usage
ez_laravel_restart_usage() {
if [[ -n $long_usage ]]; then
printf "ez laravel restart - restart Laravel containers\n"
echo
else
printf "ez laravel restart - restart Laravel containers\n"
echo
fi
printf "Alias: r\n"
echo
printf "%s\n" "Usage:"
printf " ez laravel restart APP_NAME APP_ENV [OPTIONS]\n"
printf " ez laravel restart --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_flags
# :flag.usage
printf " %s\n" "--update, -u"
printf " update the source code before restarting the container\n"
echo
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
# :command.usage_args
printf "%s\n" "Arguments:"
# :argument.usage
printf " %s\n" "APP_NAME"
printf " Laravel app name\n"
echo
# :argument.usage
printf " %s\n" "APP_ENV"
printf " Laravel app environment\n"
printf " Allowed: dev, test, staging, production\n"
echo
fi
}
# :command.usage
ez_laravel_down_usage() {
if [[ -n $long_usage ]]; then
printf "ez laravel down - remove Laravel containers\n"
echo
else
printf "ez laravel down - remove Laravel containers\n"
echo
fi
printf "%s\n" "Usage:"
printf " ez laravel down APP_NAME APP_ENV\n"
printf " ez laravel down --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
# :command.usage_args
printf "%s\n" "Arguments:"
# :argument.usage
printf " %s\n" "APP_NAME"
printf " Laravel app name\n"
echo
# :argument.usage
printf " %s\n" "APP_ENV"
printf " Laravel app environment\n"
printf " Allowed: dev, test, staging, production\n"
echo
fi
}
# :command.usage
ez_laravel_new_usage() {
if [[ -n $long_usage ]]; then
printf "ez laravel new - remove Laravel containers\n"
echo
else
printf "ez laravel new - remove Laravel containers\n"
echo
fi
printf "Alias: n\n"
echo
printf "%s\n" "Usage:"
printf " ez laravel new\n"
printf " ez laravel new --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
# :command.normalize_input
# :command.normalize_input_function
normalize_input() {
local arg flags passthru
passthru=false
while [[ $# -gt 0 ]]; do
arg="$1"
if [[ $passthru == true ]]; then
input+=("$arg")
elif [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
input+=("${BASH_REMATCH[1]}")
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
input+=("${BASH_REMATCH[1]}")
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
flags="${BASH_REMATCH[1]}"
for ((i = 0; i < ${#flags}; i++)); do
input+=("-${flags:i:1}")
done
elif [[ "$arg" == "--" ]]; then
passthru=true
input+=("$arg")
else
input+=("$arg")
fi
shift
done
}
# :command.inspect_args
inspect_args() {
if ((${#args[@]})); then
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
echo args:
for k in "${sorted_keys[@]}"; do
echo "- \${args[$k]} = ${args[$k]}"
done
else
echo args: none
fi
if ((${#other_args[@]})); then
echo
echo other_args:
echo "- \${other_args[*]} = ${other_args[*]}"
for i in "${!other_args[@]}"; do
echo "- \${other_args[$i]} = ${other_args[$i]}"
done
fi
if ((${#deps[@]})); then
readarray -t sorted_keys < <(printf '%s\n' "${!deps[@]}" | sort)
echo
echo deps:
for k in "${sorted_keys[@]}"; do
echo "- \${deps[$k]} = ${deps[$k]}"
done
fi
if ((${#env_var_names[@]})); then
readarray -t sorted_names < <(printf '%s\n' "${env_var_names[@]}" | sort)
echo
echo "environment variables:"
for k in "${sorted_names[@]}"; do
echo "- \$$k = ${!k:-}"
done
fi
}
# :command.user_lib
# src/lib/ask_question.sh
ask_question() {
local question=$1
local default=$2
local result
local GREEN='\033[0;32m'
local YELLOW='\033[0;33m'
local NC='\033[0m' # No Color
# Prompt the user with the question and default answer
read -p "$(echo -e "${GREEN}$question ${YELLOW}[$default]${NC}: ")" result
# Use the default if the user presses Enter without typing anything
if [[ -z "$result" ]]; then
result=$default
fi
echo $result
}
# src/lib/ask_question_v2.sh
#TODO This is WIP, read, echo does not work
ask_question_v2() {
local question=$1
local default=$2
local regex=$3
local required=$4
local hide_text=$5
local result
local GREEN='\033[0;32m'
local YELLOW='\033[0;33m'
local RED='\033[0;31m'
local NC='\033[0m' # No Color
while true; do
# Prompt the user with the question and default answer
if [ "$hide_text" == "true" ]; then
read -s -p "$(echo -e "${GREEN}$question ${YELLOW}[$default]${NC}: ")" result
echo "" # Move to a new line after hidden input
else
read -p "$(echo -e "${GREEN}$question ${YELLOW}[$default]${NC}: ")" result
fi
# Use the default if the user presses Enter without typing anything
if [[ -z "$result" ]]; then
result=$default
fi
# Check if the result is required
if [ "$required" == "true" ] && [[ -z "$result" ]]; then
echo -e "${RED}This field is required.${NC}"
continue
fi
# Check if the result matches the regex
if [ -n "$regex" ] && ! [[ "$result" =~ $regex ]]; then
echo -e "${RED}Invalid input format, should match regex: '$regex'${NC}"
continue
fi
break
done
echo $result
}
# src/lib/assign_port.sh
assign_port() {
local environment=$1
local base_port=$2
local num_apps default_port app_port
num_apps=$(ls -l apps | grep -c '^d')
default_port=$((base_port + num_apps))
while true; do
app_port=$(ask_question "Enter the ${environment} app port" "$default_port")
if is_port_in_use "$app_port"; then
log_error "Port $app_port is already in use. Please choose a different port." >&2
else
echo "$app_port" # This should be captured in the main script
return 0
fi
done
}
# src/lib/check_containers.sh
check_containers() {
log_header "Checking shared containers health before building the Laravel container."
local containers=("$@")
local retries=4
local delay=10
for container in "${containers[@]}"; do
for attempt in $(seq 1 $((retries + 1))); do
if is_container_running "$container"; then
log_success "Container '$container' is running."
if is_container_healthy "$container"; then
log_success "Container '$container' is healthy."
break
else
log_warning "Container '$container' is not healthy. Attempt $((attempt))/$retries."
fi
else
log_warning "Container '$container' is not running. Attempt $((attempt))/$retries. You can run shared container by './ez shared deploy'"
fi
if [ $attempt -gt $retries ]; then
log_error "Container '$container' is not healthy or not running after $retries attempts."
exit 1
fi
log_info "Retrying in $delay seconds: "
for ((i=0; i<$delay; i++)); do
echo -n "."
sleep 1
done
echo ""
done
done
}
# src/lib/create_docker_env.sh
create_docker_env() {
cat <<EOL > "$DOCKER_ENV_PATH"
SHARED_NETWORK_NAME=$SHARED_NETWORK_NAME
PORT_NGINX_PM=$PORT_NGINX_PM
PORT_PMA=$PORT_PMA
DB_HOST=mysql8
DB_PORT=3306
DB_ROOT_PASSWORD=$DB_ROOT_PASSWORD
EOL
log_success "Saved docker environment variables to $DOCKER_ENV_PATH"
}
# src/lib/create_new_database_and_user.sh
create_new_database_and_user() {
if [ "$#" -ne 3 ]; then
log_error "Invalid Arguments, Usage: $0 <new_db_name> <new_user_name> <new_user_password>"
return 1
fi
# Set variables
NEW_DB_NAME=$1
NEW_USER_NAME=$2
NEW_USER_PASSWORD=$3
log_header "Creating Database: $NEW_DB_NAME with User: $NEW_USER_NAME"
MYSQL_USER="root"
# $DB_HOST & DB_ROOT_PASSWORD is read from docker.env
# Check if database exists (improve error handling)
if ! docker exec -i $DB_HOST mysql -u$MYSQL_USER -p$DB_ROOT_PASSWORD -e "SHOW DATABASES WHERE \`Database\` = '$NEW_DB_NAME';" > /dev/null 2>&1; then
# Create database
if ! docker exec -i $DB_HOST mysql -u$MYSQL_USER -p$DB_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS \`$NEW_DB_NAME\`;"; then
log_error "Failed to create database '$NEW_DB_NAME'"
exit 1
else
log_success "Created Database: $NEW_DB_NAME";
fi
else
log "Database: $NEW_DB_NAME Already Exists";
fi
#TODO how to give access to specific container instead of %?
# Check if user exists
if docker exec -i $DB_HOST mysql -u$MYSQL_USER -p$DB_ROOT_PASSWORD -e "SELECT user FROM mysql.user WHERE user='$NEW_USER_NAME';" --skip-column-names -B | grep -q "$NEW_USER_NAME"; then
log_warning "User '$NEW_USER_NAME' already exists. Updating password."
# Update user password
if ! docker exec -i $DB_HOST mysql -u$MYSQL_USER -p$DB_ROOT_PASSWORD -e "ALTER USER '$NEW_USER_NAME'@'%' IDENTIFIED BY '$NEW_USER_PASSWORD';" || \
! docker exec -i $DB_HOST mysql -u$MYSQL_USER -p$DB_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON \`$NEW_DB_NAME\`.* TO '$NEW_USER_NAME'@'%';" || \
! docker exec -i $DB_HOST mysql -u$MYSQL_USER -p$DB_ROOT_PASSWORD -e "FLUSH PRIVILEGES;"; then
log_error "Failed to update password and/or grant privileges for user '$NEW_USER_NAME'"
exit 1
fi
log_success "User: '$NEW_USER_NAME' password updated for DB: $NEW_DB_NAME with full privileges";
else
# Create user and grant privileges
if ! docker exec -i $DB_HOST mysql -u$MYSQL_USER -p$DB_ROOT_PASSWORD -e "CREATE USER '$NEW_USER_NAME'@'%' IDENTIFIED BY '$NEW_USER_PASSWORD';" || \
! docker exec -i $DB_HOST mysql -u$MYSQL_USER -p$DB_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON \`$NEW_DB_NAME\`.* TO '$NEW_USER_NAME'@'%';" || \
! docker exec -i $DB_HOST mysql -u$MYSQL_USER -p$DB_ROOT_PASSWORD -e "FLUSH PRIVILEGES;"; then
log_error "Failed to create and grant privileges for user '$NEW_USER_NAME'"
exit 1
fi
log_success "User: '$NEW_USER_NAME' created for DB: $NEW_DB_NAME with full privileges";
fi
}
# src/lib/generate_password.sh
generate_password() {
local length=$1
local charset="A-Za-z0-9@%&*"
tr -dc "$charset" </dev/urandom | head -c $length
}
# src/lib/get_prompt_text.sh
get_prompt_text() {
local question=$1
local default=$2
local GREEN='\033[0;32m'
local YELLOW='\033[0;33m'
local NO_COLOR='\033[0m'
echo "${GREEN}$question ${YELLOW}[$default]${NO_COLOR}: "
}
# src/lib/is_container_healthy.sh
is_container_healthy() {
local container_name=$1
local health_status=$(docker inspect --format='{{.State.Health.Status}}' "$container_name")
if [ "$health_status" == "healthy" ]; then
return 0
else
return 1
fi
}
# src/lib/is_container_running.sh