forked from RubinLab/epad-dist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepad_manage.sh
executable file
·2296 lines (1966 loc) · 91.6 KB
/
epad_manage.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
#!/bin/bash
# $1 expected install, start, stop, update
# $2 expected a version master,latest,v0.1, v0.2
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Blue='\033[0;34m' # Blue
Purple='\033[0;35m' # Purple
Cyan='\033[0;36m' # Cyan
White='\033[0;37m' # White
Color_Off='\033[0m'
var_global_response=""
var_global_specialchar_flag=""
var_path=$(pwd)
var_os_type=""
global_var_container_exist=""
var_reinstalling="false"
dokcerprocessrsult_formariacredentials=()
var_array_allEpadContainerNames=(epad_lite epad_js epad_dicomweb epad_keycloak epad_couchdb epad_mariadb)
#echo "epad will be installed in : $var_path"
# sytem configuration variables
var_ip=""
var_local_docker_gid=999
var_version="master"
var_epadDistLocation="epad-dist"
var_epadLiteDistLocation="epad_lite_dist"
var_response=""
var_host=""
var_mode="lite" # or thick
var_config="environment" # or nothing
var_container_mode="image" # or build
var_couchdb_location="..\/couchdbloc"
var_mariadb_location="..\/mariadbloc"
var_branch="master" # used for epad_lite and epad_js containers
var_branch_dicomweb="master" #new
var_branch_epadlite="master" #new
var_branch_epadjs="master" #new
# user name and passwords for containers
var_keycloak_user="admin"
var_keycloak_pass="admin"
var_keycloak_useremail="[email protected]"
var_keycloak_port=8899
var_maria_user="admin"
var_maria_pass="admin"
var_maria_rootpass="admin"
var_maria_port=3306
var_maria_rootpass_old=""
var_maria_user_old=""
var_maria_user_pass_old=""
# keycloak eport settings
# ! keep /tmp/ in case you want to edit the file name epad_realm.josn
var_keycloak_exportfolder="/tmp/epad_realm.json"
var_realmName="ePad"
var_provider="singleFile"
var_couchdb_user="admin"
var_couchdb_pass="admin"
var_couchdb_port=8888
var_dicomweb_port=8090
var_epadlite_port=8080
var_epadjs_port=80
var_specialchar_flag=""
# functions
handle_errors(){
# takes 3 params $1 $2 $3
# $1 error value ($?)
# $2 script needs exit or not (noexit (""), exit)
# $3 message to explain, or instructions to the user or just print empty string ""
if [[ $? > $1 ]]; then
if [[ -n $3 ]]; then
echo $3
echo -e "${Purple}$3"
echo -e "${Color_Off}"
fi
if [[ $2 == "exit" ]]; then
exit 1
fi
fi
}
create_epad_folders(){
local localvar_couchdbloc=""
local localvar_folderrights=""
local localvar_errcnt=0
echo -e "${Yellow}process: creating ePad Folders for couchdb, pluginData, tmp, data"
echo -e "${Color_Off}"
find_os_type
if [[ "$var_os_type" == "linux" ]]; then
# in linux needs sudo rights let to the user to edit the folder rights
echo $var_path
cd $var_path
if [[ ! -d pluginData ]]; then
echo "pluginData not found. Creating"
mkdir "pluginData"
handle_errors $? "" "Creating pluginData folder failed. You may need sudo right."
chmod 777 "pluginData"
handle_errors $? "" "Giving public rights(777) to pluginData failed. You may need sudo right."
else
echo "folder found:pluginData"
localvar_folderrights=$(ls -ld "pluginData" | cut -d" " -f1 | cut -c 8,9,10)
if [[ $localvar_folderrights != "rwx" ]]; then
echo -e "${Purple}You need to give public rights(777) manually to pluginData folder in $var_path and then retry installing ePad"
echo -e "${Color_Off}"
localvar_errcnt=$(($localvar_errcnt + 1))
fi
fi
if [[ ! -d tmp ]]; then
echo "tmp folder not found. Creating"
mkdir "tmp"
handle_errors $? "" "Creating tmp folder failed. You may need sudo right."
chmod 777 "tmp"
handle_errors $? "" "Giving public rights(777) to tmp folder failed. You may need sudo right."
else
echo "folder found:tmp"
localvar_folderrights=$(ls -ld "tmp" | cut -d" " -f1 | cut -c 8,9,10)
if [[ $localvar_folderrights != "rwx" ]]; then
echo -e "${Purple}You need to give public rights(777) manually to tmp folder in $var_path and then retry installing ePad"
echo -e "${Color_Off}"
localvar_errcnt=$(($localvar_errcnt + 1))
fi
fi
if [[ ! -d data ]]; then
echo "data folder not found. Creating"
mkdir "data"
handle_errors $? "" "Creating data folder failed. You may need sudo right."
chmod 777 "data"
handle_errors $? "" "Giving public rights(777) to data folder failed. You may need sudo right."
else
echo "folder found:data"
localvar_folderrights=$(ls -ld "data" | cut -d" " -f1 | cut -c 8,9,10)
if [[ $localvar_folderrights != "rwx" ]]; then
echo -e "${Purple}You need to give public rights(777) manually to data folder in $var_path and then retry installing ePad"
echo -e "${Color_Off}"
localvar_errcnt=$(($localvar_errcnt + 1))
fi
fi
parse_yml_sections
localvar_couchdbloc=$(find_val_fromsections "couchdb" "dblocation" | sed 's/"//g' )
localvar_couchdbloca=$( remove_backslash_tofolderpath $localvar_couchdbloc)
cd "$var_path/$var_epadDistLocation"
if [[ ! -d "$localvar_couchdbloca" ]]; then
echo "$localvar_couchdbloca folder not found. Creating"
mkdir "$localvar_couchdbloca"
handle_errors $? "" "Creating $localvar_couchdbloca folder failed. You may need sudo right."
chmod 777 "$localvar_couchdbloca"
handle_errors $? "" "Giving public rights(777) to $localvar_couchdbloca folder failed. You may need sudo right."
else
echo "folder found:$localvar_couchdbloca"
localvar_folderrights=$(ls -ld "$localvar_couchdbloca" | cut -d" " -f1 | cut -c 8,9,10)
if [[ $localvar_folderrights != "rwx" ]]; then
echo -e "${Purple}You need to give public rights(777) manually to $localvar_couchdbloca folder and then retry installing ePad"
echo -e "${Color_Off}"
localvar_errcnt=$(($localvar_errcnt + 1))
fi
fi
if [[ $localvar_errcnt > 0 ]]; then
echo "exiting operation."
exit 1
fi
elif [[ "$var_os_type" == "mac" ]]; then
echo "process: folder validation result : No need to create folders for macs. Docker will create."
else
echo -e "${Purple}Unsupported operating system. Please contact ePad Team if you encounter issues."
echo -e "${Color_Off}"
fi
}
check_epadyml_needs_update(){
local localvar_actual_yml=""
local localvar_latest_yml=""
local localvar_counter=0
local localvar_result=0
if [[ -d "$var_path/$var_epadDistLocation" ]]; then
if [[ -f "$var_path/$var_epadDistLocation/epad.yml" ]]; then
localvar_actual_yml="$var_path/$var_epadDistLocation/epad.yml"
#echo "testing value localvar_actual_yml : $localvar_actual_yml"
else
echo "error: couldn't find epad.yml file in $var_path/$var_epadDistLocation"
exit 1
fi
# ${my_array[@]}
#echo "sections : ${var_array_fromyml_seections[@]}"
#echo "sections : ${var_array_fromyml_couchdb[@]}"
#echo "var_array_fromyml_seections[@] : $var_array_fromyml_seections[@]"
for i in ${!var_array_fromyml_seections[@]}; do
if [[ ${var_array_fromyml_seections[$i]} == "cache" || ${var_array_fromyml_seections[$i]} == "compression" ]]; then
#echo ${var_array_fromyml_seections[$i]}
localvar_counter=$(($localvar_counter + 1))
fi
done
localvar_result=""
#echo "result : $localvar_counter"
if [[ $localvar_counter == 2 ]]; then
localvar_counter=0
# checking the latest keys for couchdb is in epad.yml
for i in ${!var_array_fromyml_couchdb[@]}; do
if [[ $( echo ${var_array_fromyml_couchdb[$i]} | cut -d":" -f1 ) == "user" || $( echo ${var_array_fromyml_couchdb[$i]} | cut -d":" -f1 ) == "password" ]]; then
localvar_counter=$(($localvar_counter + 1))
fi
done
#echo "result : $localvar_counter"
if [[ $localvar_counter == 2 ]]; then
localvar_counter=0
# checking the latest keys for mariadb is in epad.yml
for i in ${!var_array_fromyml_mariadb[@]}; do
if [[ $( echo ${var_array_fromyml_mariadb[$i]} | cut -d":" -f1 ) == "user" || $( echo ${var_array_fromyml_mariadb[$i]} | cut -d":" -f1 ) == "password" || $( echo ${var_array_fromyml_mariadb[$i]} | cut -d":" -f1 ) == "rootpassword" ]]; then
localvar_counter=$(($localvar_counter + 1))
fi
done
#echo "result : $localvar_counter"
if [[ $localvar_counter == 3 ]]; then
localvar_counter=0
echo "uptodate"
else
echo "pull"
fi
else
echo "pull"
fi
else
echo "pull"
fi
else
echo "error: couldn't find a valid $var_epadDistLocation folder in $var_path"
exit 1
fi
}
parse_yml_sections(){
var_array_fromyml_seections=()
var_array_fromyml_keycloak=()
var_array_fromyml_couchdb=()
var_array_fromyml_dicomweb=()
var_array_fromyml_epadlite=()
var_array_fromyml_epadjs=()
var_array_fromyml_mariadb=()
if [[ -d "$var_path/$var_epadDistLocation" ]]; then
if [[ -f "$var_path/$var_epadDistLocation/epad.yml" ]];then
local input="$var_path/$var_epadDistLocation/epad.yml"
if [[ -f "$input" ]]; then
local section=""
local response=""
while IFS= read -r line
do
response=$(echo $line | grep ":$")
if [ ! -z $response ]; then
#section=$response
section=$(echo $response | cut -d":" -f1)
var_array_fromyml_seections+=($section)
#echo "section : $section "
fi
#echo "response : $response"
# echo "first : $response"
if [[ -z $response ]]; then
#eachline=$(echo $line | cut -d":" -f1)
eachline=$(echo $line | sed -e 's/[[:space:]]//g')
#echo "merge : $section-$eachline"
case $section in
keycloak)
#echo -n "keycloak"
var_array_fromyml_keycloak+=($eachline)
;;
couchdb)
#echo -n "couchdb"
var_array_fromyml_couchdb+=($eachline)
;;
dicomweb)
#echo -n "dicomweb"
var_array_fromyml_dicomweb+=($eachline)
;;
epadlite)
#echo -n "epadlite"
var_array_fromyml_epadlite+=($eachline)
;;
epadjs)
#echo -n "epadjs"
var_array_fromyml_epadjs+=($eachline)
;;
mariadb)
#echo -n "mariadb"
var_array_fromyml_mariadb+=($eachline)
;;
esac
fi
done < "$input"
#echo "*******************"
#echo "*******************"
# echo "${var_array_fromyml_keycloak[@]}"
#echo "*******************"
#echo "*******************"
#echo "${var_array_fromyml_couchdb[@]}"
# echo "*******************"
#echo "*******************"
#echo "${var_array_fromyml_dicomweb[@]}"
# echo "*******************"
#echo "*******************"
#echo "${var_array_fromyml_epadlite[@]}"
# echo "*******************"
#echo "*******************"
#echo "${var_array_fromyml_epadjs[@]}"
# echo "*******************"
#echo "*******************"
#echo "${var_array_fromyml_mariadb[@]}"
fi
else
echo "error: couldn't find epad.yml file in $var_path/$var_epadDistLocation"
exit 1
fi
fi
}
find_val_fromsections(){
local found=""
case $1 in
keycloak)
#echo "finding in keycloak"
#echo "${var_array_fromyml_keycloak[@]}"
for i in ${!var_array_fromyml_keycloak[@]}; do
found=$(echo "${var_array_fromyml_keycloak[$i]}" | grep -w $2)
if [ ! -z $found ];then
found=$(echo $found | cut -d":" -f2)
break
fi
done
echo $found
;;
couchdb)
#echo "finding in couchdb"
#echo "${var_array_fromyml_couchdb[@]}"
for i in ${!var_array_fromyml_couchdb[@]}; do
found=$(echo "${var_array_fromyml_couchdb[$i]}" | grep -w $2)
if [ ! -z $found ];then
found=$(echo $found | cut -d":" -f2)
break
fi
done
echo $found
;;
dicomweb)
#echo "finding in dicomweb"
#echo "${var_array_fromyml_dicomweb[@]}"
for i in ${!var_array_fromyml_dicomweb[@]}; do
found=$(echo "${var_array_fromyml_dicomweb[$i]}" | grep -w $2)
if [ ! -z $found ];then
found=$(echo $found | cut -d":" -f2)
break
fi
done
echo $found
;;
epadlite)
#echo "finding in epadlite"
#echo "${var_array_fromyml_epadlite[@]}"
for i in ${!var_array_fromyml_epadlite[@]}; do
found=$(echo "${var_array_fromyml_epadlite[$i]}" | grep -w $2)
if [ ! -z $found ];then
found=$(echo $found | cut -d":" -f2)
break
fi
done
echo $found
;;
epadjs)
#echo "finding in epadjs"
#echo "${var_array_fromyml_epadjs[@]}"
for i in ${!var_array_fromyml_epadjs[@]}; do
found=$(echo "${var_array_fromyml_epadjs[$i]}" | grep -w $2)
if [ ! -z $found ];then
found=$(echo $found | cut -d":" -f2)
echo $found
break
fi
done
echo $found
;;
mariadb)
#echo "finding in mariadb"
#echo "${var_array_fromyml_mariadb[@]}"
for i in ${!var_array_fromyml_mariadb[@]}; do
found=$(echo "${var_array_fromyml_mariadb[$i]}" | grep -w $2)
if [ ! -z $found ];then
found=$(echo $found | cut -d":" -f2)
break
fi
done
echo $found
;;
esac
}
rollback_epadyml_formariadb_credentials(){
local var_check_failed=""
local var_check_success=""
echo "parameter passed : $1"
# needs to check if user credentials failed for mariadb?
#check the array if contains fails.
#${dokcerprocessrsult_formariacredentials[@]}
echo "ROOLBACK phase -: verifying which credential need to be rolled back : ${dokcerprocessrsult_formariacredentials[@]}"
var_check_failed=$(echo "${dokcerprocessrsult_formariacredentials[@]}" | grep "FAILED")
var_check_success=$(echo "${dokcerprocessrsult_formariacredentials[@]}" | grep "SUCCESS")
echo "rollback necessary for the following situations : $var_check_failed"
if [[ ! -z $var_check_failed ]]; then
for i in ${!arrayImages[@]}; do
if [[ ${arrayImages[$i]} == "updaterootpassLocalhostFAILED" ]] || [[ ${arrayImages[$i]} == "updaterootpassFAILED" ]]; then
# edit epad.yml
sed -i -e "s/rootpassword:.*/rootpassword: $var_maria_rootpass_old/g" "$var_path/$var_epadDistLocation/epad.yml"
echo -e "${Yellow}process: Rolled back mariadb root pass from ->$var_maria_rootpass to -> $var_maria_rootpass_old for epad.yml"
echo -e "${Color_Off}"
# edit the compose file also
sed -i -e "s/ARG_EPAD_DOCKER_GID:.*/ARG_EPAD_DOCKER_GID: $var_local_docker_gid/g" "$var_path/$var_epadLiteDistLocation/docker-compose.yml"
fi
if [[ ${arrayImages[$i]} == "updateuserFAILED" ]]; then
awk -v var_awk="user: $var_maria_user_old" '/user:.*/{c++; if (c==3) { sub("user:.*",var_awk) } }1' "$var_path/$var_epadDistLocation/epad.yml" > "$var_path/$var_epadDistLocation/tempEpad.yml" && mv "$var_path/$var_epadDistLocation/tempEpad.yml" "$var_path/$var_epadDistLocation/epad.yml"
echo -e "${Yellow}process: Rolled back mariadb user from ->$var_maria_user to -> $var_maria_user_old for epad.yml"
echo -e "${Color_Off}"
fi
if [[ ${arrayImages[$i]} == "updateuserpassFAILED" ]]; then
awk -v var_awk="password: $var_maria_user_pass_old" '/password:.*/{c++; if (c==3) { sub("password:.*",var_awk) } }1' "$var_path/$var_epadDistLocation/epad.yml" > "$var_path/$var_epadDistLocation/tempEpad.yml" && mv "$var_path/$var_epadDistLocation/tempEpad.yml" "$var_path/$var_epadDistLocation/epad.yml"
echo -e "${Yellow}process: Rolled back mariadb user password from ->$var_maria_pass to -> $var_maria_user_pass_old for epad.yml"
echo -e "${Color_Off}"
fi
done
# updaterootpassLocalhostFAILED updaterootpassFAILED updateuserFAILED updateuserpassFAILED
echo -e "${Yellow}process: Rolling back epad.yml finished. Recreating epad_lite_dist"
echo -e "${Color_Off}"
create_epad_lite_dist
if [ -d "$var_path/$var_epadLiteDistLocation" ]; then
cd "$var_path/$var_epadLiteDistLocation"
echo -e "${Yellow}process: Restarting ePad containers to reflect the old credentials to the containers"
echo -e "${Color_Off}"
docker-compose up -d > "$var_path/epad_manage.log"
else
echo -e "${Yellow}process: Could not locate epad_lite_dist folder at location : $var_path/$var_epadLiteDistLocation to restart containers with rolled back mariadb credentials"
echo -e "${Color_Off}"
fi
# docker restart epad_lite
else
echo -e "${Yellow}process: check failed status: empty. Nothing to rollback"
echo -e "${Color_Off}"
fi
if [[ ! -z $var_check_success ]]; then
echo -e "${Yellow}process: restarting epad_mariadb"
echo -e "${Color_Off}"
docker restart epad_mariadb
fi
}
update_mariadb_usersandpass(){
# new : need testing
local edited=0
local maria_container_exist=""
local result=""
local verifyfirst=""
local var_sql_socket_ready=""
#var_maria_rootpass_old="admin"
#var_maria_rootpass="cavit"
echo -e "${Yellow}process: updating mariadb users and passwords"
echo -e "${Color_Off}"
#echo "old root pass:$var_maria_rootpass_old"
#echo "new root pass:$var_maria_rootpass"
#echo "old user:$var_maria_user_old"
#echo "new user:$var_maria_user"
#echo "old user pass : $var_maria_user_pass_old"
#echo "new user pass : $var_maria_pass"
#if [[ $var_reinstalling == "true" ]]; then
maria_container_exist=$(docker ps -a --filter "name=\bepad_mariadb\b" --format "table {{.Status}}" | grep Up)
if [[ ! -z $maria_container_exist ]]; then
if [[ ! -z $var_maria_rootpass ]] && [[ ! -z $var_maria_rootpass_old ]] && [[ ! -z $var_maria_pass ]] && [[ ! -z $var_maria_user_pass_old ]] && [[ ! -z $var_maria_user ]] && [[ ! -z $var_maria_user_old ]]; then
# docker exec --user root -it epad_mariadb apt-get update
if [[ $var_maria_rootpass_old != $var_maria_rootpass ]]; then
echo -e "${Yellow}process: editing root password in epad_mariadb container"
echo -e "${Color_Off}"
# we need to wait for the sockek to be ready
var_sql_socket_ready="socket"
while [[ ! -z "$var_sql_socket_ready" ]]; do
echo "waiting for sql socket"
var_sql_socket_ready=$(docker exec -it epad_mariadb mysql -uroot -p$var_maria_rootpass -e "use mysql;show databases;")
var_sql_socket_ready=$(echo "$var_sql_socket_ready" | grep "socket")
echo "1 var_sql_socket_ready : $var_sql_socket_ready"
if [[ -z "$var_sql_socket_ready" ]]; then
var_sql_socket_ready=$(docker exec -i epad_mariadb mysql -uroot -p$var_maria_rootpass_old -e "use mysql;show databases;" )
var_sql_socket_ready=$(echo "$var_sql_socket_ready" | grep "socket")
echo "2 in var_sql_socket_ready : $var_sql_socket_ready"
#var_sql_socket_ready="socket"
fi
sleep 5
done
echo -e "${Yellow}process: mariadb socket ready for the root credentials update..."
echo -e "${Color_Off}"
verifyfirst="$(docker exec -it epad_mariadb mysql -uroot -p$var_maria_rootpass -e "use mysql;show databases;" || echo "failed")"
echo "root user verifyfirst :$verifyfirst"
if [[ "$verifyfirst"=="failed" ]]; then
echo "new root pass failed so need to update"
result=$(docker exec -i epad_mariadb mysql -uroot -p$var_maria_rootpass_old <<< "use mysql; ALTER USER 'root'@'localhost' IDENTIFIED BY '"$var_maria_rootpass"';FLUSH PRIVILEGES;" && echo "updaterootpasslocalhostSUCCESS" || echo "updaterootpassLocalhostFAILED") # > result
dokcerprocessrsult_formariacredentials+=($result)
echo "a1 : $result"
#result=$(docker exec -i epad_mariadb mysql -uroot -p$var_maria_rootpass <<< "use mysql; ALTER USER 'root'@'%' IDENTIFIED BY '"$var_maria_rootpass"';FLUSH PRIVILEGES;" && echo "updaterootpassSUCCESS" || echo "updaterootpassFAILED") # > result
result=$(docker exec -i epad_mariadb mysql -uroot -p$var_maria_rootpass <<< "use mysql; ALTER USER 'root'@'%' IDENTIFIED BY '"$var_maria_rootpass"';FLUSH PRIVILEGES;" && echo "updaterootpassSUCCESS" || echo "updaterootpassFAILED") # > result
dokcerprocessrsult_formariacredentials+=($result)
echo "a2 : $result"
edited=1
fi
else
echo "no need to update root password in mariadb"
fi
if [[ $var_maria_user != $var_maria_user_old ]] || [[ $var_maria_pass != $var_maria_user_pass_old ]]; then
echo -e "${Yellow}process: editing mariadb user and password in epad_mariadb container"
echo -e "${Color_Off}"
# we need to wait for the sockek to be ready
var_sql_socket_ready="socket"
while [[ ! -z $var_sql_socket_ready ]]; do
echo "waiting for sql socket"
var_sql_socket_ready=$(docker exec -it epad_mariadb -u$var_maria_user -p$var_maria_pass -e "use epaddb;show tables;" )
var_sql_socket_ready=$(echo "$var_sql_socket_ready" | grep "socket")
echo "3 var_sql_socket_ready : $var_sql_socket_ready"
if [[ -z $var_sql_socket_ready ]]; then
var_sql_socket_ready=$(docker exec -i epad_mariadb mysql -u$var_maria_user_old -p$var_maria_user_pass_old -e "use epaddb;show tables;" )
var_sql_socket_ready=$(echo "$var_sql_socket_ready" | grep "socket")
echo "4 var_sql_socket_ready : $var_sql_socket_ready"
#var_sql_socket_ready="socket"
fi
sleep 5
done
# var_sql_socket_ready="socket"
# while [[ -z $var_sql_socket_ready ]]; do
# echo "waiting for sql socket"
# var_sql_socket_ready=$(docker exec -it epad_mariadb mysqladmin -u$var_maria_user -p$var_maria_pass -e "use mysql;show databases;" )
# echo "var_sql_socket_ready : $var_sql_socket_ready"
# if [[ -z $var_sql_socket_ready ]]; then
# var_sql_socket_ready=$(docker exec -it epad_mariadb mysqladmin -u$var_maria_user_old -p$var_maria_user_pass_old -e "use mysql;show databases;" )
# fi
# sleep 5
# done
echo -e "${Yellow}process: mariadb socket ready for the user credentials update..."
echo -e "${Color_Off}"
verifyfirst="$(docker exec -it epad_mariadb mysql -u$var_maria_user -p$var_maria_pass -e "use epaddb;show tables;" || echo "failed")"
echo "regular user verifyfirst :$verifyfirst"
if [[ "$verifyfirst"=="failed" ]]; then
#dokcerprocessrsult_formariacredentials+=($())
result=$(docker exec -i epad_mariadb mysql -uroot -p$var_maria_rootpass <<< "use mysql; update user set User = '"$var_maria_user"' where User='"$var_maria_user_old"' ;FLUSH PRIVILEGES;" && echo "updateuserSUCCESS" || echo "updateuserFAILED")
dokcerprocessrsult_formariacredentials+=($result)
echo "a3 : $result"
if [[ "$result"=="updateuserSUCCESS" ]]; then
result=$(docker exec -i epad_mariadb mysql -uroot -p$var_maria_rootpass <<< "use mysql; GRANT ALL ON epaddb.* TO '"$var_maria_user"'@'%'; ;FLUSH PRIVILEGES;" && echo "updateusertableprivillegeSUCCESS" || echo "updateusertableprivillegeFAILED")
dokcerprocessrsult_formariacredentials+=($result)
echo "aX : $result"
fi
#GRANT ALL ON epaddb.* TO 'cavit'@'%';
result=$(docker exec -i epad_mariadb mysql -uroot -p$var_maria_rootpass <<< "use mysql; ALTER USER '"$var_maria_user"'@'%' IDENTIFIED BY '"$var_maria_pass"';FLUSH PRIVILEGES;" && echo "updateuserpassSUCCESS" || echo "updateuserpassFAILED")
dokcerprocessrsult_formariacredentials+=($result)
echo "a4 : $result"
edited=2
fi
else
echo "no need to update user name or password in mariadb"
fi
if [[ $edited > 0 ]]; then
echo -e "${Yellow}process: restarting epad_mariadb container "
echo -e "${Color_Off}"
rollback_epadyml_formariadb_credentials
# if credentials update fails need to rollback epad.yml for mariadb user credentials .Same for docker-compose.yml
fi
else
echo "user or password is empty for user or root. Mariadb credentials left as is."
rollback_epadyml_formariadb_credentials
fi
else
echo "could not locate epad_mariadb container"
echo "rolling back epad.yml with old mariadb credentials"
rollback_epadyml_formariadb_credentials "no_container_rollback"
fi
#fi
#echo "mariadb credential update check : ${dokcerprocessrsult_formariacredentials[@]}"
failvalue=$(echo "${dokcerprocessrsult_formariacredentials[@]}" | grep "FAILED")
#echo "failed ? : $failvalue"
succesvalue=$(echo "${dokcerprocessrsult_formariacredentials[@]}" | grep "SUCCESS")
#echo "success ? : $succesvalue"
}
delete_epad_data(){
# new : need testing
# this section will delete couchdb and maria db folders
echo -e "${Yellow}process: deleting epad data"
echo -e "${Color_Off}"
local var_resp_delete="n"
read -p " Your couchdb and mariadb data will be erased. Do you want to proceed (y/n) default answer is (n) : " var_resp_delete
# if [[ -z $var_resp_delete ]]; then
# echo "empty"
# echo "var_resp_delete:$var_resp_delete"
# else
# echo "not empty"
# echo "var_resp_delete:$var_resp_delete"
# fi
}
delete_dangling_images(){
echo -e "${Yellow}process: deleting dangling images"
echo -e "${Color_Off}"
docker rmi -f $(docker images -f "dangling=true" -q)
}
remove_epad_images(){
local arrayImages=()
echo -e "${Yellow}process: deleting ePad images"
echo -e "${Color_Off}"
arrayImages+=($(docker ps -a --filter "name=\bepad_js\b" --format "table {{.Image}}" | awk '{ getline; print $0;}'))
arrayImages+=($(docker ps -a --filter "name=\bepad_lite\b" --format "table {{.Image}}" | awk '{ getline; print $0;}'))
arrayImages+=($(docker ps -a --filter "name=\bepad_dicomweb\b" --format "table {{.Image}}" | awk '{ getline; print $0;}'))
arrayImages+=($(docker ps -a --filter "name=\bepad_keycloak\b" --format "table {{.Image}}" | awk '{ getline; print $0;}'))
arrayImages+=($(docker ps -a --filter "name=\bepad_couchdb\b" --format "table {{.Image}}" | awk '{ getline; print $0;}'))
arrayImages+=($(docker ps -a --filter "name=\bepad_mariadb\b" --format "table {{.Image}}" | awk '{ getline; print $0;}'))
stop_containers_all
remove_containers_all
echo "removing epad images : ${arrayImages[@]}"
for i in ${!arrayImages[@]}; do
docker rmi ${arrayImages[$i]} > "$var_path/epad_manage.log"
done
# docker rmi $("${arrayImages[@]}")
#docker rmi $(docker ps -a --filter "name=\bepad_js\b" --format "table {{.Image}}" | awk '{ getline; print $0;}')
#docker rmi $(docker ps -a --filter "name=\bepad_lite\b" --format "table {{.Image}}" | awk '{ getline; print $0;}')
#docker rmi $(docker ps -a --filter "name=\bepad_dicomweb\b" --format "table {{.Image}}" | awk '{ getline; print $0;}')
#docker rmi $(docker ps -a --filter "name=\bepad_couchdb\b" --format "table {{.Image}}" | awk '{ getline; print $0;}')
#docker rmi $(docker ps -a --filter "name=\bepad_couchdb\b" --format "table {{.Image}}" | awk '{ getline; print $0;}')
#docker rmi $(docker ps -a --filter "name=\bepad_couchdb\b" --format "table {{.Image}}" | awk '{ getline; print $0;}')
}
check_ifallcontainers_created(){
local var_counter=0
local var_result=""
var_result=$(docker ps -a | grep "\bepad_js\b")
if [[ ! -z $var_result ]]; then
var_counter=$(($var_counter + 1))
fi
var_result=$(docker ps -a | grep "\bepad_lite\b")
if [[ ! -z $var_result ]]; then
var_counter=$(($var_counter + 1))
fi
var_result=$(docker ps -a | grep "\bepad_dicomweb\b")
if [[ ! -z $var_result ]]; then
var_counter=$(($var_counter + 1))
fi
var_result=$(docker ps -a | grep "\bepad_keycloak\b")
if [[ ! -z $var_result ]]; then
var_counter=$(($var_counter + 1))
fi
var_result=$(docker ps -a | grep "\bepad_couchdb\b")
if [[ ! -z $var_result ]]; then
var_counter=$(($var_counter + 1))
fi
var_result=$(docker ps -a | grep "\bepad_mariadb\b")
if [[ ! -z $var_result ]]; then
var_counter=$(($var_counter + 1))
fi
if [[ $var_counter < 6 ]]; then
global_var_container_exist="doesnt"
else
global_var_container_exist="exist"
fi
}
check_existance_couchdb_usercred(){
local var_couchdb_lineno=0
local var_string=""
var_couchdb_lineno=$(grep -n 'couchdb:' "$var_path/$var_epadDistLocation/epad.yml" | cut -d":" -f1 )
var_couchdb_lineno=$(($var_couchdb_lineno + 3))
var_string=$(cat "$var_path/$var_epadDistLocation/epad.yml" | grep "couchuser")
if [[ -z $var_string ]]; then
awk 'NR=='$var_couchdb_lineno'{print " couchuser: \"'$var_couchuser'\" " }1' "$var_path/$var_epadDistLocation/epad.yml" > "$var_path/$var_epadDistLocation/tempEpad.yml" && mv "$var_path/$var_epadDistLocation/tempEpad.yml" "$var_path/$var_epadDistLocation/epad.yml"
var_couchdb_lineno=$(grep -n 'couchuser' "$var_path/$var_epadDistLocation/epad.yml" | cut -d":" -f1 )
else
var_couchdb_lineno=$(grep -n 'couchuser' "$var_path/$var_epadDistLocation/epad.yml" | cut -d":" -f1 )
fi
var_string=$(cat "$var_path/$var_epadDistLocation/epad.yml" | grep "couchpassword")
if [[ -z $var_string ]]; then
var_couchdb_lineno=$(($var_couchdb_lineno + 1))
awk 'NR=='$var_couchdb_lineno'{print " couchpassword: \"'$var_couchpassword'\" " }1' "$var_path/$var_epadDistLocation/epad.yml" > "$var_path/$var_epadDistLocation/tempEpad.yml" && mv "$var_path/$var_epadDistLocation/tempEpad.yml" "$var_path/$var_epadDistLocation/epad.yml"
fi
}
check_container_situation(){
# local_situation can be these values-> ok, healty , fail
local var_total_fail=0
local var_container_situation=""
local var_failed_container_names=""
local var_counter=0
var_container_situation=$(docker ps -a --filter "name=\bepad_lite\b" --format "table {{.Status}}" )
if [[ "$var_container_situation" == "STATUS" ]]; then
var_total_fail=$(( $var_total_fail + 1))
var_failed_container_names="$var_failed_container_names epad_lite"
else
var_container_situation=$(docker ps -a --filter "name=\bepad_lite\b" --format "table {{.Status}}" | grep 'Exited\|unhealthy' )
if [[ ! -z $var_container_situation ]]; then
var_total_fail=$(( $var_total_fail + 1))
fi
fi
var_container_situation=$(docker ps -a --filter "name=\bepad_js\b" --format "table {{.Status}}" )
if [[ "$var_container_situation" == "STATUS" ]]; then
var_total_fail=$(( $var_total_fail + 1))
var_failed_container_names="$var_failed_container_names epad_js"
else
var_container_situation=$(docker ps -a --filter "name=\bepad_js\b" --format "table {{.Status}}" | grep 'Exited\|unhealthy')
if [[ ! -z $var_container_situation ]]; then
var_total_fail=$(( $var_total_fail + 1))
fi
fi
var_container_situation=$(docker ps -a --filter "name=\bepad_dicomweb\b" --format "table {{.Status}}" )
if [[ "$var_container_situation" == "STATUS" ]]; then
var_total_fail=$(( $var_total_fail + 1))
var_failed_container_names="$var_failed_container_names epad_dicomweb"
else
var_container_situation=$(docker ps -a --filter "name=\bepad_dicomweb\b" --format "table {{.Status}}" | grep 'Exited\|unhealthy' )
if [[ ! -z $var_container_situation ]]; then
var_total_fail=$(( $var_total_fail + 1))
fi
fi
var_container_situation=$(docker ps -a --filter "name=\bepad_keycloak\b" --format "table {{.Status}}" )
if [[ "$var_container_situation" == "STATUS" ]]; then
var_total_fail=$(( $var_total_fail + 1))
var_failed_container_names="$var_failed_container_names epad_keycloak"
else
var_container_situation=$(docker ps -a --filter "name=\bepad_keycloak\b" --format "table {{.Status}}" | grep 'Exited\|unhealthy' )
if [[ ! -z $var_container_situation ]]; then
var_total_fail=$(( $var_total_fail + 1))
fi
fi
var_container_situation=$(docker ps -a --filter "name=\bepad_couchdb\b" --format "table {{.Status}}" )
if [[ "$var_container_situation" == "STATUS" ]]; then
var_total_fail=$(( $var_total_fail + 1))
var_failed_container_names="$var_failed_container_names epad_couchdb"
else
var_container_situation=$(docker ps -a --filter "name=\bepad_couchdb\b" --format "table {{.Status}}" | grep 'Exited\|unhealthy' )
if [[ ! -z $var_container_situation ]]; then
var_total_fail=$(( $var_total_fail + 1))
fi
fi
var_container_situation=$(docker ps -a --filter "name=\bepad_mariadb\b" --format "table {{.Status}}" )
if [[ "$var_container_situation" == "STATUS" ]]; then
var_total_fail=$(( $var_total_fail + 1))
var_failed_container_names="$var_failed_container_names epad_mariadb"
else
var_container_situation=$(docker ps -a --filter "name=\bepad_mariadb\b" --format "table {{.Status}}" | grep 'Exited\|unhealthy' )
if [[ ! -z $var_container_situation ]]; then
var_total_fail=$(( $var_total_fail + 1))
fi
fi
# docker ps -a -f status=exited | grep "\bepad_lite\b"
# echo $var_total_fail
if [[ $var_total_fail > 0 ]]; then
echo "there are failed containers"
# docker ps -a -f 'health=unhealthy'
# health=unhealthy
var_counter=$(docker ps -a -f 'status=exited' | wc -l | awk ' {print $1}')
# echo "var_counter $var_counter"
if [[ $var_counter == 0 ]]; then
var_counter=$(docker ps -a -f 'health=unhealthy' | wc -l | awk ' {print $1}')
fi
if [[ $var_counter > 0 ]]; then
docker ps -a -f 'status=exited'
docker ps -a -f 'health=unhealthy'
fi
# echo "Please contact epad team!"
if [[ "$var_failed_container_names" != "" ]]; then
echo "ePad couldn't find following containers : $var_failed_container_names . Please install ePad or contact ePad team"
fi
exit 1
fi
}
check_keycloak_container_situation(){
local var_container_situation=""
var_container_situation=$(docker ps -a --filter "name=\bepad_keycloak\b" --format "table {{.Status}}" )
if [[ "$var_container_situation" != "STATUS" ]]; then
# echo "container epad_keycloak does not exist. Please start epad first "
#exit 1
var_container_situation=$(docker ps -a --filter "name=\bepad_keycloak\b" --format "table {{.Status}}" | grep Up )
if [[ -z $var_container_situation ]]; then
echo "container epad_keycloak is not running. Please start epad first "
exit 1
fi
else
echo "noop"
fi
}
find_os_type(){
echo -e "${Yellow}process: finding os type"
echo -e "${Color_Off}"
if [[ $OSTYPE == "linux"* ]]; then
var_os_type="linux"
elif [[ $OSTYPE == "darwin"* ]]; then
var_os_type="mac"
else
echo " your operating system is not supported or ostype env. variable is not defined. Only linux and macs are supported "i
exit 1
fi
}
add_backslash_tofolderpath(){
#$1 is the parameter passed to this function
#echo "adding backslash"
var_tmp_txt=$(echo $1 | sed 's-/-\\/-g')
echo $var_tmp_txt
}
remove_backslash_tofolderpath(){
#$1 is the parameter passed to this function
#echo "removing backslash"
#var_a="\/home\/epad\/epadDistScript\/test\/epad-dist"
var_tmp_txt=$( echo $1 | tr -d '\\' )
echo $var_tmp_txt
#iecho $(echo cavi=/cat | sed 's-/-\\/-g')
}
find_val_intext(){
# first param : text : string
# second param : occourrence : number
#echo "finding values"
#echo "Parameter #1 is $1"
#echo "Parameter #2 is $2"
if [[ -f "$var_path/$var_epadDistLocation/epad.yml" ]]; then
var_tmp_txt=$( awk "/$1/{i++}i==$2{print; exit}" "$var_path/$var_epadDistLocation/epad.yml")
var_tmp_txt=$( echo $var_tmp_txt | cut -d: -f2)
#hostname loaded from epad.yml to variable
echo $var_tmp_txt
else
echo ""
fi
#echo "hostname from file :$var_host"
}
find_ip(){
#echo "process: finding machine ip "
if [[ "$var_os_type" == "linux"* ]]; then
var_ip=$(hostname -I | cut -d" " -f1)
else
var_ip=$(ifconfig | grep -w inet | cut -d " " -f2 | tail -1)
fi
if [[ "$var_ip" == "" ]]; then
echo "we couldn't find an ip please contact your server admin or epad team"
exit 1
fi
echo $var_ip
}
doublecheck_ipmapping_onstart(){
find_os_type
local var_hostname_from_epadyml=""
local var_ip_frometc=""
local var_fresh_ip=""
local iffixed_hostname_etchosts="epadvm"