This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
api-test-tool.sh
executable file
·1370 lines (1270 loc) · 38.7 KB
/
api-test-tool.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
# =========================================================
# =========================================================
#
# APP CONTAINER CREATION
# SPECIFIC TO EACH APP
# !!! DO NOT CHANGE !!!
#
# =========================================================
# =========================================================
function create_app_container
{
echo ""
if [ `$DOCKER ps -a | grep $APP_NAME | wc -l` -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: $APP_NAME container not present. Creating it..."
$DOCKER create \
--security-opt label:disable \
-v $PERSISTANT_FOLDER/$APP_NAME/config.js:/app/config.js:ro \
--link $DB_NAME:mongo \
--name="$APP_NAME" \
--restart="on-failure:5" \
-e "VIRTUAL_HOST=$NODEJS_VHOST" \
-e "LETSENCRYPT_HOST=$NODEJS_VHOST" \
-e "LETSENCRYPT_EMAIL=$LETSENCRYPT_EMAIL" \
$APP_IMG
if [ $? -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: $APP_NAME container is now created."
else
echo -e "${ERRORC}ERROR${NC}: $APP_NAME container can't be created."
fi
else
echo -e "${INFOC}INFO${NC}: $APP_NAME image is already created."
fi
}
# =========================================================
# =========================================================
#
# SYSTEM PARAMETERS
# DO NOT CHANGE!!!
#
# =========================================================
# =========================================================
SCRIPT_CONF=`pwd`"/ah-ref-app.conf"
SCRIPT_NAME="api-test-tool"
APP_NAME="api-test-tool"
APP_IMG="tmunzer/api-test-tool"
OAUTH_CALLBACK="oauth/reg"
LETSENCRYPT_ENABLE=true
LETSENCRYPT_EMAIL=""
# =========================================================
# Email server configuration
# true if email server is needed by the App
EMAIL_SRV_ENABLE=false
# =========================================================
# mongoDB server configuration
# uncomment if needed by the app
DB_FOLDER="mongoDB"
DB_NAME="ah-mongo"
DB_IMG="mongo"
# =========================================================
# NGINX server configuration
# true if email server is needed by the App
NGINX_CERTS_FOLDER="certs"
NGINX_NAME="ah-proxy"
NGINX_IMG="jwilder/nginx-proxy"
LETSENCRYPT_NAME="ah-letsencrypt"
LETSENCRYPT_IMG="jrcs/letsencrypt-nginx-proxy-companion"
DOCKER=""
# =========================================================
# Colors
INFOC='\033[0;32m'
WARNINGC='\033[0;33m'
ERRORC='\033[0;31m'
NC='\033[0m' # No Color
################################################################################
############################ BANNER
################################################################################
function banner
{
echo ""
echo "||============================================================================="
echo "||"
echo "|| $1"
echo "||"
echo "||============================================================================="
}
################################################################################
############################ MANAGE DOCKER CONTAINERS
################################################################################
function result_banner
{
echo ""
echo ""
echo "============================================================================"
echo "============================================================================"
echo " The system is now up and running!"
echo ""
if [ "$DB_NAME" ]
then
echo -e "${INFOC}INFO${NC}: MongoDB files are in $DB_FOLDER"
echo ""
fi
echo -e "${INFOC}INFO${NC}: NGINX SSL/TLS certifcates are in $NGINX_CERTS_FOLDER"
echo ""
echo -e "${INFOC}INFO${NC}: $APP_NAME interface should now be avaible soon"
echo " https://$NODEJS_VHOST"
echo ""
if $LETSENCRYPT_ENABLE == "true" > /dev/null
then
echo -e "${WARNINGC}WARNING${NC}: If you just installed Let's Encrypt service, it can take some time"
echo " to start."
echo " You can still check starting process with the command"
echo " \"docker logs $LETSENCRYPT_NAME\""
echo ""
fi
echo "============================================================================"
echo "============================================================================"
}
################################################################################
############################ SCRIPT CONF
################################################################################
function script_conf
{
# FOLDER PARAMETERS
echo "We will need a persistant folder to store application configuration, data and certificates."
echo "Where do you want to store these data? [$PERSISTANT_FOLDER]"
echo ""
response=""
while ! echo $response | grep -i "y" > /dev/null
do
read -p "PERSISTANT FOLDER: " PERSISTANT_FOLDER
if ! echo $PERSISTANT_FOLDER | grep ^"/" > /dev/null
then
echo -e "${WARNINGC}WARNING${NC}: Incorrect input"
else
read -p "Is \"$PERSISTANT_FOLDER\" correct (y/n)? " response
fi
done
while [ ! -d "$PERSISTANT_FOLDER" ]
do
echo "$PERSISTANT_FOLDER does not exist."
create=""
while ! echo $create | grep -i "[ny]" > /dev/null
do
read -p "Do you want to create it (y/n)? " create
case $create in
"y"|"Y") mkdir -p "$PERSISTANT_FOLDER/$APP_NAME";;
*) exit 0;;
esac
done
done
# LETS ENCRYPT PARAMETERS
response=""
while ! echo $response | grep -i "[yn]" > /dev/null
do
echo ""
echo "Do you want to use Let's Encrypt service (https://letsencrypt.org) to have free trusted HTTPS certificates (Let's Encrypt services has to be able to resolv the Application DNS entry and to reach the docker container on TCP80 and TCP443)?"
echo ""
echo -e "${WARNINGC}WARNING${NC}: Be carreful, some domain names may be blacklisted by Let's Encrypt service. "
echo " This seems to be the case for Microsft and Amazon Web Services."
echo " In this case, you will not be able to use the given FQDN, but you will have to"
echo " use a custom FQDN and create a CNAME DNS entry."
echo ""
read -p "Use Let's Encrypt Service (y/n)? " response
case $response in
"y"|"Y")
LETSENCRYPT_ENABLE=true;
read -p "Let's Encrypt contact email address: " LETSENCRYPT_EMAIL;
;;
"n"|"N") LETSENCRYPT_ENABLE=false;;
esac
done
# VHOST PARAMETER
echo ""
echo ""
# SAVING PARAMETERS
if [ -f $SCRIPT_CONF ]
then
mv $SCRIPT_CONF $SCRIPT_CONF.bak
fi
touch $SCRIPT_CONF
while read line
do
if echo "$line" | grep "VHOST" > /dev/null
then
echo "$line" >> $SCRIPT_CONF
fi
done < $SCRIPT_CONF.bak
echo "" >> $SCRIPT_CONF
echo "PERSISTANT_FOLDER=$PERSISTANT_FOLDER" >> $SCRIPT_CONF
echo "LETSENCRYPT_ENABLE=$LETSENCRYPT_ENABLE" >> $SCRIPT_CONF
echo "LETSENCRYPT_EMAIL=$LETSENCRYPT_EMAIL" >> $SCRIPT_CONF
echo "" >> $SCRIPT_CONF
}
function update_vhost
{
echo "To use the NGINX reverse proxy, we will need a dedicated DNS entry for the application."
echo "Web browsers will access the application interface from this FQDN."
response=""
while ! echo $response | grep -i "y" > /dev/null
do
read -p "Application DNS name: " NODEJS_VHOST
read -p "Are you sure (y/n)? " response
done
echo "$APP_NAME-NODEJS_VHOST=$NODEJS_VHOST" >> $SCRIPT_CONF
echo "" >> $SCRIPT_CONF
}
function init_script_conf
{
if [ ! -f "$SCRIPT_CONF" ]
then
echo "-----=============-----"
echo "--=== SCRIPT INIT ===--"
echo ""
echo "Before starting, here are some questions..."
echo ""
script_conf
response=""
while ! echo $response | grep -i "[y]" > /dev/null
do
echo ""
echo "Current parameters:"
echo ""
cat $SCRIPT_CONF
read -p "Is the configuration correct (y/n)? " response
case $response in
"n"|"N") script_conf;;
esac
done
else
while read line
do
if echo $line | grep "PERSISTANT_FOLDER" > /dev/null
then
PERSISTANT_FOLDER=`echo "$line" | cut -d"=" -f2`
fi
if echo $line | grep "$APP_NAME-NODEJS_VHOST" > /dev/null
then
NODEJS_VHOST=`echo "$line" | cut -d"=" -f2`
fi
if echo $line | grep "LETSENCRYPT_ENABLE" > /dev/null
then
LETSENCRYPT_ENABLE=`echo "$line" | cut -d"=" -f2`
fi
if echo $line | grep "LETSENCRYPT_EMAIL" > /dev/null
then
LETSENCRYPT_EMAIL=`echo "$line" | cut -d"=" -f2`
fi
done < $SCRIPT_CONF
fi
if echo "$PERSISTANT_FOLDER" | grep -i [a-z] > /dev/null
then
DB_FOLDER="$PERSISTANT_FOLDER/$DB_FOLDER"
NGINX_CERTS_FOLDER="$PERSISTANT_FOLDER/$NGINX_CERTS_FOLDER"
echo -e "${INFOC}INFO${NC}: Script configuration loaded succesfully."
else
echo -e "${ERRORC}ERROR${NC}: not able to load Script configuration. Exiting..."
exit 254
fi
if [ ! "$NODEJS_VHOST" ]
then
update_vhost
update_acs
fi
}
function read_script_conf
{
if [ ! -f $SCRIPT_CONF ]
then
echo -e "${WARNINGC}WARNING${NC}: Script configuration file does not exists..."
script_conf
fi
echo ""
echo "Current parameters:"
echo ""
cat $SCRIPT_CONF
}
function menu_script
{
response=""
while ! echo $response | grep -i "[b]" > /dev/null
do
echo ""
echo "1) Change Script parameters"
echo "2) View Script parameters"
echo "b) Back"
echo "Please make a choice"
read response
case $response in
"1") script_conf;;
"2") read_script_conf;;
"b") menu_main;;
esac
done
}
################################################################################
############################ ACS CONF
################################################################################
function save_acs
{
if [ -f "$PERSISTANT_FOLDER/$APP_NAME/config.js" ]
then
mv $PERSISTANT_FOLDER/$APP_NAME/config.js $PERSISTANT_FOLDER/$APP_NAME/config.js.bak
if [ $? -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: old configuration file backed up at $PERSISTANT_FOLDER/$APP_NAME/config.js.bak"
else
echo -e "${ERRORC}ERROR${NC}: can't backup old configuration file."
fi
fi
touch $PERSISTANT_FOLDER/$APP_NAME/config.js
echo "module.exports.appServer = {" >> $PERSISTANT_FOLDER/$APP_NAME/config.js
echo " vhost: \"$NODEJS_VHOST\"" >> $PERSISTANT_FOLDER/$APP_NAME/config.js
echo "};" >> $PERSISTANT_FOLDER/$APP_NAME/config.js
echo "module.exports.devAccount = {" >> $PERSISTANT_FOLDER/$APP_NAME/config.js
if echo $OAUTH_CALLBACK | grep "^/" > /dev/null
then
echo " redirectUrl: \"https://$NODEJS_VHOST$OAUTH_CALLBACK\"," >> $PERSISTANT_FOLDER/$APP_NAME/config.js
else
echo " redirectUrl: \"https://$NODEJS_VHOST/$OAUTH_CALLBACK\"," >> $PERSISTANT_FOLDER/$APP_NAME/config.js
fi
echo " clientSecret: \"$CLIENTSECRET\"," >> $PERSISTANT_FOLDER/$APP_NAME/config.js
echo " clientID: \"$CLIENTID\"" >> $PERSISTANT_FOLDER/$APP_NAME/config.js
echo "}" >> $PERSISTANT_FOLDER/$APP_NAME/config.js
if [ "$DB_NAME" ]
then
echo "module.exports.mongoConfig = {" >> $PERSISTANT_FOLDER/$APP_NAME/config.js
echo " host: \"mongo\",base: \"$APP_NAME\"" >> $PERSISTANT_FOLDER/$APP_NAME/config.js
echo "}" >> $PERSISTANT_FOLDER/$APP_NAME/config.js
fi
echo "" >> $PERSISTANT_FOLDER/$APP_NAME/config.js
if [ `$DOCKER ps | grep -c "$APP_NAME"` -eq 1 ]
then
echo ""
echo -e "${WARNINGC}WARNING${NC}: $APP_NAME is running."
echo "Do you want to recreate the container to use the new configuration?"
response=""
while ! echo $response | grep -i "[ny]" > /dev/null
do
read -p "Recreate (y/n)? " response
case $response in
"Y"|"y")
$DOCKER rm -f $APP_NAME > /dev/null
if [ $? -gt 0 ]
then
echo -e "${ERRORC}ERROR${NC}: Unable to remove old container."
echo " You will have to manually remove it to use the new configuration."
else
echo -e "${INFOC}INFO${NC}: $APP_NAME container removed."
start_container $APP_NAME
fi
;;
*);;
esac
done
elif [ `$DOCKER ps -a | grep -c "$APP_NAME"` -eq 1 ]
then
$DOCKER rm $APP_NAME > /dev/null
if [ $? -gt 0 ]
then
echo -e "${ERRORC}ERROR${NC}: Unable to remove old container."
echo " You will have to manually remove it to use the new configuration."
fi
fi
}
function update_acs
{
if [ -f "$PERSISTANT_FOLDER/$APP_NAME/config.js" ]
then
echo -e "${INFOC}INFO${NC}: Updating ACS configuration with new hostname."
while read line
do
if echo $line | grep "clientSecret:" > /dev/null
then
CLIENTSECRET=`echo "$line" | grep -o "clientSecret:.*\"[^\"]*" | cut -d"\"" -f2`
fi
if echo $line | grep "clientID:" > /dev/null
then
CLIENTID=`echo "$line" | grep -o "clientID:.*\"[^\"]*" | cut -d"\"" -f2`
fi
done < $PERSISTANT_FOLDER/$APP_NAME/config.js
save_acs
fi
}
function acs_conf
{
correct="n"
while ! echo $correct | grep -i "y" > /dev/null
do
echo "To use this APP, you will need an account on the Aerohive Developer portal."
echo "If you don't have any account for now, please go to https://developer.aerohive.com/ and create one."
echo "Otherwise, you can continue and configure the ACS parameters"
echo ""
response=""
while ! echo $response | grep -i "[ny]" > /dev/null
do
echo -n "Continue (y/n)? "
read response
case $response in
"N"|"n") exit 0;;
*);;
esac
done
read -p "ClientID (from dev portal): " CLIENTID
read -p "ClientSecret (from dev portal): " CLIENTSECRET
echo ""
echo "PARAMETERS:"
echo "ClientID: $CLIENTID"
echo "ClientSecret: $CLIENTSECRET"
if echo $OAUTH_CALLBACK | grep "^/" > /dev/null
then
echo "RedirectURL: https://$NODEJS_VHOST$OAUTH_CALLBACK"
else
echo "RedirectURL: https://$NODEJS_VHOST/$OAUTH_CALLBACK"
fi
echo ""
read -p "Is this correct (y/n)? " correct
done
touch $PERSISTANT_FOLDER/$APP_NAME/config.js
if [ $? -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: new configuration file saved at $PERSISTANT_FOLDER/$APP_NAME/config.js"
else
echo -e "${ERRORC}ERROR${NC}: can't save new configuration file."
fi
save_acs
}
function init_acs_conf
{
if [ ! -f "$PERSISTANT_FOLDER/$APP_NAME/config.js" ]
then
acs_conf
else
echo -e "${INFOC}INFO${NC}: ACS configuration loaded succesfully."
fi
}
function acs_configure
{
if [ -f "$PERSISTANT_FOLDER/$APP_NAME/config.js" ]
then
echo "A configuration already exists in $PERSISTANT_FOLDER/$APP_NAME"
response=""
while ! echo $response | grep -i "[ny]" > /dev/null
do
echo -n "Do you want to reconfigure (y/n)? "
read response
case $response in
"Y"|"y") acs_conf;;
*);;
esac
done
else
acs_conf
fi
}
function menu_acs
{
response=""
while ! echo $response | grep -i "[b]" > /dev/null
do
echo ""
echo "1) Change ACS parameters"
echo "2) View ACS parameters"
echo "b) Back"
echo "Please make a choice"
read response
case $response in
"1") acs_configure;;
"2") cat $PERSISTANT_FOLDER/$APP_NAME/config.js;;
"b") menu_main;;
esac
done
}
################################################################################
############################ MAIL_SRV
################################################################################
function email_save
{
if [ -f "$PERSISTANT_FOLDER/$APP_NAME/mailer_config.js" ]
then
mv $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js.bak
if [ $? -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: old configuration file backed up at $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js.bak"
else
echo -e "${ERRORC}ERROR${NC}: can't backup old configuration file."
fi
fi
echo "module.exports.config = {" >> $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
echo " host: '$EMAIL_SRV'," >> $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
echo " port: $EMAIL_SRV_PORT," >> $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
echo " secureConnection: $EMAIL_SRV_TLS," >> $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
echo " tls: {" >> $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
echo " ciphers: 'SSLv3'," >> $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
echo " rejectUnauthorized: false" >> $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
echo " }," >> $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
echo " auth: {" >> $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
echo " user: '$EMAIL_SRV_USER'," >> $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
echo " pass: '$EMAIL_SRV_PWD'" >> $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
echo " }" >> $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
echo "};" >> $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
echo "" >> $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
if [ `$DOCKER ps | grep -c "$APP_NAME"` -eq 1 ]
then
echo ""
echo -e "${WARNINGC}WARNING${NC}: $APP_NAME is running."
echo "Do you want to restart the container to use the new configuration?"
response=""
while ! echo $response | grep -i "[ny]" > /dev/null
do
read -p "Restart (y/n)? " response
case $response in
"Y"|"y")
$DOCKER restart $APP_NAME > /dev/null
if [ $? -gt 0 ]
then
echo -e "${ERRORC}ERROR${NC}: Unable to restart container."
echo " You will have to manually restart it to use the new configuration."
fi
;;
*);;
esac
done
elif [ `$DOCKER ps -a | grep -c "$APP_NAME"` -eq 1 ]
then
echo -e "${ERRORC}ERROR${NC}: $APP_NAME container is not started"
fi
}
function email_conf
{
correct="n"
while ! echo $correct | grep -i "y" > /dev/null
do
echo "To use this APP, you can use an external Email server to send customized emails to users."
echo ""
response=""
while ! echo $response | grep -i "[ny]" > /dev/null
do
echo -n "Do you want to configure your email server (y/n)? "
read response
done
if echo $response | grep -i "n" > /dev/null
then
correct="y"
else
echo $response | grep -i "y" > /dev/null
read -p "Email server IP Address/Hostname: " EMAIL_SRV
read -p "Email server port: " EMAIL_SRV_PORT
while ! echo $EMAIL_SRV_TLS | grep -i "[yn]" > /dev/null
do
read -p "Use TLS connection with Email server (y/n)? " EMAIL_SRV_TLS
done
if echo $EMAIL_SRV_TLS | grep -i "y" > /dev/null
then
EMAIL_SRV_TLS=true
else
EMAIL_SRV_TLS=false
fi
echo ""
read -p "Email server username: " EMAIL_SRV_USER
echo -n "Email server password: "
read -s EMAIL_SRV_PWD
echo "PARAMETERS:"
echo "Email Server: $EMAIL_SRV"
echo "Email Server Port: $EMAIL_SRV_PORT"
echo "Use secured connection: $EMAIL_SRV_TLS"
echo "Email user: $EMAIL_SRV_USER"
echo ""
read -p "Is this correct (y/n)? " correct
fi
done
touch $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js
if [ $? -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: new configuration file saved at $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js"
else
echo -e "${ERRORC}ERROR${NC}: can't save new configuration file."
fi
email_save
}
function email_configure
{
if [ -f "$PERSISTANT_FOLDER/$APP_NAME/mailer_config.js" ]
then
echo "A configuration already exists in $PERSISTANT_FOLDER/$APP_NAME"
response=""
while ! echo $response | grep -i "[ny]" > /dev/null
do
echo -n "Do you want to reconfigure (y/n)? "
read response
case $response in
"Y"|"y") email_conf;;
*);;
esac
done
else
email_conf
fi
}
function menu_email
{
if $EMAIL_SRV_ENABLE == "true" > /dev/null
then
response=""
while ! echo $response | grep -i "[b]" > /dev/null
do
echo ""
echo "1) Change Email Server parameters"
echo "2) View Email Server parameters"
echo "b) Back"
echo "Please make a choice"
read response
case $response in
"1") email_configure;;
"2") cat $PERSISTANT_FOLDER/$APP_NAME/mailer_config.js;;
"b") menu_main;;
esac
done
fi
}
################################################################################
############################ FOLDERS
################################################################################
function check_folder # $name $FOLDER_NAME
{
if [ ! -d $2 ]
then
echo -e "${INFOC}INFO${NC}: $1 folder $2 doesn't exist. Creating it..."
mkdir -p $2
if [ $? -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: $1 folder $2 created."
else
echo ""
echo -e "${ERRORC}ERROR${NC}: Unable to create $1 folder $2."
fi
else
echo -e "${INFOC}INFO${NC}: $1 folder already exists."
fi
}
################################################################################
############################ CERTIFICATES
################################################################################
function check_certificates
{
if $LETSENCRYPT_ENABLE == "true"
then
echo -e "${INFOC}INFO${NC}: Certificates will be managed by Let's Encrypt service."
else
if [ `ls $NGINX_CERTS_FOLDER | grep $NODEJS_VHOST.key | wc -l` -eq 0 ] || [ `ls $NGINX_CERTS_FOLDER | grep $NODEJS_VHOST.crt | wc -l` -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: Certificates for $NODEJS_VHOST doesn't exist."
echo " Creating a self-signed certificate..."
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout $NGINX_CERTS_FOLDER/$NODEJS_VHOST.key -out $NGINX_CERTS_FOLDER/$NODEJS_VHOST.crt
echo -e "${INFOC}INFO${NC}: Certificate for $NODEJS_VHOST created."
else
echo -e "${INFOC}INFO${NC}: Certificate for $NODEJS_VHOST already exists."
fi
fi
}
function new_certificate
{
response="y"
if echo $LETSENCRYPT_ENABLE == "true" > /dev/null
then
echo ""
echo -e "${WARNINGC}WARNING${NC}: Let's Encrypt service is in use. If you generate a self-signed"
echo " certificate, it will remove the Let's encrypt certificates."
echo ""
read -p "Do you want to continue (y/n)? " response
fi
if echo $response | grep -i "y" > /dev/null
then
if [ -f "$NGINX_CERTS_FOLDER/$NODEJS_VHOST.crt" ]
then
echo -e "${INFOC}INFO${NC}: removing $NGINX_CERTS_FOLDER/$NODEJS_VHOST.crt"
rm $NGINX_CERTS_FOLDER/$NODEJS_VHOST.crt
fi
if [ -f "$NGINX_CERTS_FOLDER/$NODEJS_VHOST.key" ]
then
echo -e "${INFOC}INFO${NC}: removing $NGINX_CERTS_FOLDER/$NODEJS_VHOST.key"
rm $NGINX_CERTS_FOLDER/$NODEJS_VHOST.key
fi
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout $NGINX_CERTS_FOLDER/$NODEJS_VHOST.key -out $NGINX_CERTS_FOLDER/$NODEJS_VHOST.crt
echo -e "${INFOC}INFO${NC}: Certificate for $NODEJS_VHOST created."
fi
}
function new_csr
{
openssl req -out $NGINX_CERTS_FOLDER/$NODEJS_VHOST.csr -new -newkey rsa:2048 -nodes -keyout $NGINX_CERTS_FOLDER/$NODEJS_VHOST.key
echo -e "${INFOC}INFO${NC}: new CSR generated. The CSR $NODEJS_VHOST.csr can be found in the folder $NGINX_CERTS_FOLDER"
echo -e "${WARNINGC}WARNING${NC}: To be able to use the application, you will have to sign the CSR with"
echo " your Certificate Authority."
echo " The signed certificate has to be place into the folder"
echo " $NGINX_CERTS_FOLDER with the name $NODEJS_VHOST.crt"
}
function help_certificate
{
echo -e "${INFOC}INFO${NC}: You can replace the self-signed certicate with your own certicate."
echo " To do so, you will have to generate a signed certificate on your own,"
echo " and to place the certificate and its private key into the folder"
echo " $NGINX_CERTS_FOLDER"
echo " The certificate has to be a X509 certificate in PEM format."
echo " At the end, you should have:"
echo " $NGINX_CERTS_FOLDER/$NODEJS_VHOST.crt"
echo " $NGINX_CERTS_FOLDER/$NODEJS_VHOST.key"
}
function read_certificate
{
openssl x509 -in $NGINX_CERTS_FOLDER/$NODEJS_VHOST.crt -noout -text
}
function menu_certificates
{
response=""
while ! echo $response | grep -i "[b]" > /dev/null
do
echo ""
echo "1) Generate new self-signed certificate"
echo "2) Generate CSR"
echo "3) Help to use custom certificate"
echo "4) View current Certificate"
echo "b) Back"
echo "Please make a choice"
read response
case $response in
"1") new_certificate;;
"2") new_csr;;
"3") help_certificate;;
"4") read_certificate;;
"b") menu_main;;
esac
done
}
################################################################################
############################ CREATE DOCKER IMAGES
################################################################################
function pull_image # $Xx_IMG
{
echo ""
if [ `$DOCKER images | cut -d" " -f1 | grep $1$ | wc -l` -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: $1 image is not present. Installing it..."
$DOCKER pull $1
if [ $? -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: $1 image is now installed."
else
echo -e "${ERRORC}ERROR${NC}: $1 image can't be installed."
fi
else
echo -e "${INFOC}INFO${NC}: $1 image is already installed."
fi
}
function check_image #$XX_IMG
{
echo ""
if [ `$DOCKER images | grep "$1" | wc -l` -eq 0 ]
then
echo -e "${WARNINGC}WARNING${NC}: Docker Image $1 is not installed."
echo " Please deploy all the needed images to run the Application"
echo " in a Docker environment."
else
echo -e "${INFOC}INFO${NC}: Docker Image $1 is installed"
fi
}
function deploy_images
{
if [ "$DB_IMG" ]
then
pull_image $DB_IMG
fi
pull_image $NGINX_IMG
if $LETSENCRYPT_ENABLE == "true"
then
pull_image $LETSENCRYPT_IMG
fi
pull_image $APP_IMG
}
function remove_images
{
if [ "$DB_IMG" ]
then
$DOCKER rmi $DB_IMG
fi
$DOCKER rmi $NGINX_IMG
if $LETSENCRYPT_ENABLE == "true"
then
$DOCKER rmi $LETSENCRYPT_IMG
fi
$DOCKER rmi $APP_IMG
}
function check_images
{
if [ "$DB_IMG" ]
then
check_image $DB_IMG
fi
check_image $NGINX_IMG
if $LETSENCRYPT_ENABLE == "true"
then
check_image $LETSENCRYPT_IMG
fi
check_image $APP_IMG
}
function menu_images
{
response="0"
while ! echo $response | grep -i "[b]" > /dev/null
do
echo ""
echo "1) Deploy Docker Images"
echo "2) Remove Application Image"
echo "3) Remove Application and Dependencies Images"
echo "4) Check Docker Images"
echo "b) Back"
echo "Please make a choice"
read response
case $response in
"1") deploy_images;;
"2") $DOCKER rmi $APP_IMG;;
"3") remove_images;;
"4") check_images;;
esac
done
response=""
}
################################################################################
############################ CREATE DOCKER CONTAINERS
################################################################################
function create_mongo_container
{
echo ""
if [ `$DOCKER ps -a | grep $DB_NAME | wc -l` -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: $DB_NAME container not present. Creating it..."
$DOCKER create \
--name $DB_NAME \
-v $DB_FOLDER:/data/db:Z \
$DB_IMG
if [ $? -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: $DB_NAME container is now created."
else
echo -e "${ERRORC}ERROR${NC}: $DB_NAME container can't be created."
fi
else
echo -e "${INFOC}INFO${NC}: $DB_NAME container is already created."
fi
}
function create_nginx_container
{
echo ""
if [ `$DOCKER ps -a | grep $NGINX_NAME | wc -l` -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: $NGINX_NAME container not present. Creating it..."
$DOCKER create \
-p 80:80 \
-p 443:443 \
--security-opt label:disable \
-v $NGINX_CERTS_FOLDER:/etc/nginx/certs:ro \
-v /etc/nginx/vhost.d \
-v /usr/share/nginx/html \
-v /var/run/docker.sock:/tmp/docker.sock:ro \
--name=$NGINX_NAME \
--restart="on-failure:5" \
$NGINX_IMG
if [ $? -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: $NGINX_NAME container is now created."
else
echo -e "${ERRORC}ERROR${NC}: $NGINX_NAME container can't be created."
fi
else
echo -e "${INFOC}INFO${NC}: $NGINX_NAME container is already created."
fi
}
function create_letsencrypt_container
{
echo ""
if [ `$DOCKER ps -a | grep $LETSENCRYPT_NAME | wc -l` -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: $LETSENCRYPT_NAME container not present. Creating it..."
$DOCKER create \
--security-opt label:disable \
-v $NGINX_CERTS_FOLDER:/etc/nginx/certs:rw \
--volumes-from $NGINX_NAME \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
--name=$LETSENCRYPT_NAME \
--restart="on-failure:5" \
$LETSENCRYPT_IMG
if [ $? -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: $LETSENCRYPT_NAME container is now created."
else
echo -e "${ERRORC}ERROR${NC}: $LETSENCRYPT_NAME container can't be created."
fi
else
echo -e "${INFOC}INFO${NC}: $LETSENCRYPT_NAME container is already created."
fi
}
function check_container # $XX_NAME
{
if [ `$DOCKER ps | grep -c "$1"` -gt 0 ]
then
echo "$1: INSTALLED and RUNNING"
elif [ `$DOCKER ps -a | grep -c "$1"` -gt 0 ]
then
echo "$1: INSTALLED and STOPPED"
else
echo "$1: NOT INSTALLED"
fi
}
function start_container # $XX_NAME
{
echo ""
if [ `$DOCKER ps -a | grep $1 | wc -l` -eq 0 ]
then
echo -e "${ERRORC}ERROR${NC}: $1 container is not created. Please create it before."
retval=1
elif [ `$DOCKER ps | grep $1 | wc -l` -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: $1 container is not started. starting it..."
CID=`$DOCKER ps -a | grep $1 | cut -d" " -f1`
$DOCKER start $CID
retval=$?
if [ $retval -eq 0 ]
then
echo -e "${INFOC}INFO${NC}: $1 container is now started."
retval=0
else
echo -e "${ERRORC}ERROR${NC}: $1 container can't be started."
retval=1
fi
else
echo -e "${INFOC}INFO${NC}: $1 container is alreay running."