forked from LearningLocker/deploy
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeployll.sh
More file actions
executable file
·2283 lines (2005 loc) · 85.3 KB
/
deployll.sh
File metadata and controls
executable file
·2283 lines (2005 loc) · 85.3 KB
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
#
# Copyright (C) 2017-2019 HT2 Labs, modifications 2025 by internetlehrer GmbH
#
# This program is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program.
# If not, see http://www.gnu.org/licenses/.
#########################################
# PACKING FUNCTIONS #
#########################################
# $1 is the path to the tmp dir
function apt_package ()
{
echo "creating apt package"
}
function yum_package ()
{
echo "[LL] creating yum package"
}
#########################################
# GENERIC FUNCTIONS IRRESPECTIVE OF OS #
#########################################
# creating 4GB swp space
function create_swap_space ()
{
output "Creating 4GB swapfile and adding to fstab to automatically mount on boot..."
fallocate -l 4GB /swapfile
chmod 600 /swapfile
mkswap /swapfile >> $OUTPUT_LOG
swapon /swapfile
echo "/swapfile none swap sw 0 0" | tee -a /etc/fstab >> $OUTPUT_LOG
output "done!"
}
# simple function to symlink useful commands
function symlink_commands ()
{
output_log "setting up symlink commands"
alias ll-pm2-logs="su - ${LOCAL_USER} -c 'pm2 logs'"
}
function determine_os_version ()
{
VERSION_FILE=/etc/issue.net
REDHAT_FILE=/etc/redhat-release
CENTOS_FILE=/etc/centos-release
OS_SUBVER=false
OS_VERSION=false
OS_VNO=false
# Make sure that the version is one we know about - if not, there'll likely be some strangeness with the package names
if [ ! -f $VERSION_FILE ]; then
echo "[LL] Couldn't determine version from $VERSION_FILE, file doesn't exist"
exit 0
fi
RAW_OS_VERSION=$(cat $VERSION_FILE)
if [[ $RAW_OS_VERSION == *"Amazon"* ]]; then
OS_VERSION="Redhat"
OS_SUBVER="Amazon"
OS_ARCH=$(uname -a | awk '{print $12}')
output "Sorry, we don't support ${OS_VERSION}, subversion: ${OS_SUBVER} at the moment."
echo
exit 0
elif [[ $RAW_OS_VERSION == *"Debian"* ]]; then
OS_VERSION="Debian"
output "Sorry, we don't support ${OS_VERSION} at the moment."
echo
exit 0
elif [[ $RAW_OS_VERSION == *"Ubuntu"* ]]; then
OS_VERSION="Ubuntu"
OS_VNO=`lsb_release -a 2>/dev/null | grep Release | awk '{print $2}'`
if [[ $OS_VNO == "" ]]; then
OS_VNO=$(cat /etc/os-release | grep "VERSION_ID" | sed 's?VERSION_ID=??' | sed 's?"??g')
fi
output_log "Detected OS: ${OS_VERSION}, subver:${OS_SUBVER}, arch:${OS_ARCH}, vno:${OS_VNO}, NodeOverride: ${NODE_OVERRIDE}, PM2Override:${PM2_OVERRIDE}"
elif [[ -f $REDHAT_FILE ]]; then
RAW_OS_VERSION=$(cat $REDHAT_FILE)
OS_ARCH=$(uname -a | awk '{print $12}')
# centos detection
if [[ $RAW_OS_VERSION == *"CentOS"* ]]; then
OS_VERSION="Redhat"
OS_SUBVER="CentOS"
output "Sorry, we don't support ${OS_VERSION}, subversion: ${OS_SUBVER} at the moment."
echo
exit 0
# RHEL
elif [[ $RAW_OS_VERSION == *"Red Hat Enterprise Linux"* ]]; then
OS_VERSION="Redhat"
OS_SUBVER="RHEL"
output "Sorry, we don't support ${OS_VERSION}, subversion: ${OS_SUBVER} at the moment."
echo
exit 0
# Fedora
elif [[ $RAW_OS_VERSION == *"Fedora"* ]]; then
OS_VERSION="Redhat"
OS_SUBVER="Fedora"
output "Sorry, we don't support ${OS_VERSION}, subversion: ${OS_SUBVER} at the moment."
echo
exit 0
# unknown redhat - bail
else
output "Only set up for debian/ubuntu at the moment I'm afraid - exiting"
exit 0
fi
fi
if [[ $OS_VERSION != "Ubuntu" ]]; then
printf "||---------------------------------------------------------------||\n"
printf "|| ----------------------------------------------------------- ||\n"
printf "|| NOTE: THIS SCRIPT ONLY FULLY SUPPORTS UBUNTU 24.04 ||\n"
printf "|| it may work on other systems, but this is not guaranteed ||\n"
printf "|| ||\n"
printf "|| Press any key to acknowledge this message. ||\n"
printf "|| ----------------------------------------------------------- ||\n"
printf "||---------------------------------------------------------------||\n"
if [[ $BYPASSALL == false ]]; then
read -r -s -n 1 n
fi
return 0
fi
if [[ ! $OS_VERSION ]]; then
output "Couldn't determind version from $VERSION_FILE, unknown OS: $RAW_OS_VERSION"
exit 0
fi
}
function show_help ()
{
echo "stub for help text"
echo "CLI options:"
echo " -b {branch name} : specific branch to check out"
exit 0
}
function show_install_end_text ()
{
echo
echo "Thanks for installing Learning Locker v2"
echo
echo "If you need to add a new superuser (other than one created by the install process) then you can run this command:"
echo ' cd ${LOCAL_PATH}; node cli/dist/server createSiteAdmin "EMAIL" "ORG-NAME" "PASSWD"'
echo " with replacing the values in capitals with ones you want"
echo
echo "If you need to restart the services this script has installed then you can run this command:"
echo " service pm2-${LOCAL_USER} restart"
echo
}
function print_spinner ()
{
pid=$!
s='-\|/'
i=0
while kill -0 $pid 2>/dev/null; do
i=$(( (i+1) %4 ));
printf "\b${s:$i:1}";
sleep .1;
done
printf "\b.";
if [[ $1 == true ]]; then
output "done!" false true
fi
}
function output_log ()
{
if [[ -f $OUTPUT_LOG ]]; then
echo $1 >> $OUTPUT_LOG
fi
}
# $1 is the message
# $2 is true/false to supress newlines
# $3 is true/false to supress the [LL] block
# $4 is the number of whitespace characters to insert before the text (not working yet)
function output ()
{
if [[ $2 == true ]]; then
if [[ $3 == true ]]; then
echo -n $1
output_log "$1"
else
MSG="[LL] $1"
echo -n $MSG
output_log "$MSG"
fi
else
if [[ $3 == true ]]; then
echo $1
output_log "$1"
else
MSG="[LL] $1"
echo $MSG
output_log "$MSG"
fi
fi
}
# simple function to check if the version is greater than a specific other version
# $1 is the version to check
# $2 is the version to check against
# returns '1' if $1>=$2 or '2' otherwise
function version_check ()
{
if [[ $1 == $2 ]]
then
return 1
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
# simple function to check if a webserver is running on this machine on port 80 (our default setup). If it it, we should warn and/or present options to the user
function webserver_check ()
{
# first off check if we've got anything running locally
if [[ `netstat -lnt | grep ":80 "` ]]; then
output "You currently have a webserver running on port 80 on this server. This will need removing / resetting before our nginx config will work."
output " If this is an upgrade of an existing install then this is nothing to worry about. Press any key to continue"
read n
fi
}
#################################################################################
# LEARNINGLOCKER FUNCTIONS #
#################################################################################
# $1 is the path to the install directory
# $2 is the username to run under
function setup_init_script ()
{
if [[ ! -d $1 ]]; then
output "path to install directory (${1}) wasn't a valid directory in setup_init_script(), exiting"
exit 0
fi
if [[ ! `command -v pm2` ]]; then
output "Didn't install pm2 or can't find it - the init script will need to be set up by hand. Press any key to continue"
if [[ $BYPASSALL == false ]]; then
read n
fi
return
fi
output "starting base processes...." true
su - $2 -c "cd ${1}/${WEBAPP_SUBDIR}; pm2 start all.json"
output "done" true true
output "starting xapi process...." true
su - $2 -c "cd ${1}/${XAPI_SUBDIR}; pm2 start xapi.json"
output "done" true true
su - $2 -c "pm2 save"
# I'm going to apologise here for the below line - for some reason when executing the resultant command
# from the output of pm2 startup, the system $PATH doesn't seem to be set so we have to force it to be
# an absolute path before running the command. It also needs to go into a variable and be run rather than
# be run within backticks or the path still isn't substituted correctly. I know, right? it's a pain.
output "setting up PM2 startup"
#Begin fix mongo
echo "start fix mongodb removing ?replicaSet=rs0"
sed -i "s|mongodb://127.0.0.1:27017/learninglocker_v2?replicaSet=rs0|mongodb://127.0.0.1:27017/learninglocker_v2|g" /usr/local/$2/current/xapi/.env
sed -i "s|mongodb://localhost:27017/learninglocker_v2?replicaSet=rs0|mongodb://127.0.0.1:27017/learninglocker_v2|g" /usr/local/$2/current/xapi/.env
sudo -u $2 pm2 kill
if [[ $PM2_OVERRIDE != false ]]; then
output "using PM2 startup override of $PM2_OVERRIDE"
PM2_STARTUP=$(su - $2 -c "pm2 startup $PM2_OVERRIDE | grep sudo | sed 's?sudo ??' | sed 's?\$PATH?$PATH?'")
else
PM2_STARTUP=$(su - $2 -c "pm2 startup | grep sudo | sed 's?sudo ??' | sed 's?\$PATH?$PATH?'")
fi
echo $PM2_STARTUP
CHK=$($PM2_STARTUP)
service pm2-$2 restart >> $OUTPUT_LOG
systemctl status pm2-$2.service >> $OUTPUT_LOG
}
# this function is needed to fix the lack of a CDN in the unicode-json node module. Essentially we check for a unicode
# file in a set of directories and if it doesn't exist, we create a file
# $1 is the absolute path to the unicode file we want to copy in
function unicode_definition_install ()
{
if [[ -f /usr/share/unicode/UnicodeData.txt ]]; then
return 0
fi
if [[ -f /usr/share/unicode-data/UnicodeData.txt ]]; then
return 0
fi
if [[ -f /usr/share/unicode/ucd/UnicodeData.txt ]]; then
return 0
fi
if [[ ! -f $1 ]]; then
output "the path for the unicode file wasn't passed to unicode_definition_install correctly (${1})"
sleep 5
return 1
fi
mkdir -p /usr/share/unicode
cp $1 /usr/share/unicode/UnicodeData.txt
chmod 644 /usr/share/unicode/UnicodeData.txt
}
function base_install ()
{
# if the checkout dir exists, prompt the user for what to do
DEFAULT_RM_TMP=y
DO_BASE_INSTALL=true
if [[ -d ${WEBAPP_SUBDIR} ]]; then
while true; do
output "Temp directory already exists for checkout - should I delete? [y|n] (enter is the default of ${DEFAULT_RM_TMP})"
if [[ $JUSTDOIT == true ]]; then
output "bypass defaulting to 'y'"
rm -R ${WEBAPP_SUBDIR}
break
fi
read -r -s -n 1 n
if [[ $n == "" ]]; then
n=$DEFAULT_RM_TMP
fi
if [[ $n == "y" ]]; then
output "Ok, deleting temp directory...." true
rm -R ${WEBAPP_SUBDIR}
output "done!" false true
break
elif [[ $n == "n" ]]; then
output "ok, not removing it - could cause weirdness though so be warned"
sleep 5
DO_BASE_INSTALL=false
break
fi
done
fi
if [[ ! `command -v git` ]]; then
echo
output "Can't find git - can't continue"
echo
exit 0
fi
# check if git is too far out of date
GIT_VERSION=`git --version | awk '{print $3}'`
MIN_GIT_VERSION="1.7.10"
MIN_GIT_VERSION_4PT="1.7.10.0"
output "Git version: ${GIT_VERSION}, minimum: $MIN_GIT_VERSION"
version_check $GIT_VERSION $MIN_GIT_VERSION
VCHK=$?
RV=`echo $GIT_VERSION | awk -F "." '{print NF-1}'`
if [[ $VCHK == 2 ]] && [[ $RV -gt 3 ]]; then
# stupid double check here but pre-v2 of git had some 4-point releases which won't match the above
version_check $GIT_VERSION $MIN_GIT_VERSION_4PT
VCHK=$?
fi
if [[ $VCHK == 2 ]]; then
output "Sorry but your version of git is too old. You should be running a minimum of $MIN_GIT_VERSION"
exit 0
fi
output "Will now try and clone the git repo for the main learninglocker software. May take some time...."
# in a while loop to capture the case where a user enters the user/pass incorrectly
if [[ $DO_BASE_INSTALL -eq true ]]; then
while true; do
output_log "running git clone"
if [[ $ENTERPRISE == true ]]; then
MAIN_REPO=https://github.com/HT2-Labs/ll-enterprise
if [[ $GIT_USER != false ]]; then
MAIN_REPO=https://${GIT_USER}:${GIT_PASS}@github.com/HT2-Labs/ll-enterprise
output_log "Cloning main repo with user: ${GIT_USER}"
fi
else
MAIN_REPO=https://github.com/internetlehrer/learninglocker
fi
# clone repo
git clone -q -b ${GIT_BRANCH} $MAIN_REPO ${WEBAPP_SUBDIR}
# nano ${WEBAPP_SUBDIR}/package.json
# clear the history in case we passed in the user/pass
if [[ $GIT_USER != false ]]; then
history -c
fi
if [[ -d ${WEBAPP_SUBDIR} ]]; then
output_log "no ${WEBAPP_SUBDIR} dir after git - problem"
break
fi
done
fi
cd ${WEBAPP_SUBDIR}
GIT_REV=`git rev-parse --verify HEAD`
if [[ ! -f .env ]]; then
cp .env.example .env
if [[ $UPDATE_MODE == false ]]; then
output "Copied example env to .env - This will need editing by hand"
fi
APP_SECRET=`openssl rand -base64 32`
sed -i "s?APP_SECRET=?APP_SECRET=${APP_SECRET}?" .env
fi
#outdated; file does not exist anymore
#output "checking UnicodeData is present..." true
#unicode_definition_install $PWD/UnicodeData.txt
output "done!" false true
# yarn install
output "running yarn install...." true
npm_config_build_from_source=true yarn install --ignore-engines >> $OUTPUT_LOG 2>>$ERROR_LOG &
print_spinner true
# pm2 - done under npm rather than yarn as yarn does weird stuff on global installs on debian
output "adding pm2...." true
npm install -g pm2 >> $OUTPUT_LOG 2>>$ERROR_LOG &
print_spinner true
# yarn build-all
output "running yarn build-all (this can take a little while - don't worry, it's not broken)...." true
yarn build-all >>$OUTPUT_LOG 2>>$ERROR_LOG &
print_spinner true
# pm2 logrotate
output "installing pm2 logrotate...." true
pm2 install pm2-logrotate >> $OUTPUT_LOG 2>>$ERROR_LOG &
print_spinner true
output "setting up pm2 logrotate...." true
pm2 set pm2-logrotate:compress true >> $OUTPUT_LOG 2>>$ERROR_LOG &
print_spinner true
}
function xapi_install ()
{
output "Will now try and clone the git repo for XAPI. May take some time...."
# not checking for presence of 'git' command as done in git_clone_base()
DO_XAPI_CHECKOUT=true;
if [[ -d xapi ]]; then
DEFAULT_RM_TMP="y"
while true; do
output "Tmp directory already exists for checkout of xapi - should I delete? [y|n] (enter is the default of ${DEFAULT_RM_TMP})"
if [[ $JUSTDOIT == true ]]; then
output "bypass defaulting to 'y'"
rm -R ${XAPI_SUBDIR}
break
fi
read n
output_log "user entered '${n}'"
if [[ $n == "" ]]; then
n=$DEFAULT_RM_TMP
fi
if [[ $n == "y" ]]; then
rm -R ${XAPI_SUBDIR}
break
elif [[ $n == "n" ]]; then
output "ok, not removing it - could cause weirdness though so be warned"
DO_XAPI_CHECKOUT=false
sleep 5
break
fi
done
fi
# do the checkout in a loop in case the users enters user/pass incorrectly
if [[ $DO_XAPI_CHECKOUT -eq true ]]; then
# TODO - make this do a max itteration of say 3 attempts to clone
while true; do
output_log "attempting git clone for xapi, branch: $XAPI_BRANCH"
git clone -q -b ${XAPI_BRANCH} https://github.com/internetlehrer/ll-xapi-service.git ${XAPI_SUBDIR}
if [[ ! -d ${XAPI_SUBDIR} ]]; then
output_log "git clone appears to have failed"
break
else
output_log "git clone succeeded"
break
fi
done
fi
cd ${XAPI_SUBDIR}/
# sort out .env
if [[ ! -f .env ]]; then
cp .env.example .env
if [[ $UPDATE_MODE == false ]]; then
output "Copied example env to .env - This will need editing by hand"
fi
fi
# npm
#output "running npm install...." true
#npm install >> $OUTPUT_LOG 2>>$ERROR_LOG &
#print_spinner true
#output "running npm run build...." true
#npm run build >> $OUTPUT_LOG 2>>$ERROR_LOG &
#print_spinner true
# yarn
output "running yarn install...." true
yarn install --ignore-engines >> $OUTPUT_LOG 2>>$ERROR_LOG &
print_spinner true
output "running yarn build...." true
yarn build >> $OUTPUT_LOG 2>>$ERROR_LOG &
print_spinner true
cd ../
}
# $1 is the file to reprocess
# $2 is the path to the install dir
# $3 is the log path - if not passed in we'll assume it's $2/logs/
# $4 is the pid path - if not passed in we'll assume it's $2/pids/
function reprocess_pm2 ()
{
if [[ ! $1 ]]; then
output "no file name passed to reprocess_pm2"
return
elif [[ ! -f $1 ]]; then
output "file '${1}' passed to reprocess_pm2() appears to not exist."
sleep 10
return
fi
if [[ ! $2 ]]; then
output "no install path passed to reprocess_pm2 - exiting"
exit 0
fi
LOG_DIR=$2/logs
PID_DIR=$2/pids
if [[ $3 ]]; then
LOG_DIR=$3
fi
if [[ $4 ]]; then
PID_DIR=$4
fi
output_log "pm2 - setting pid dir to $PID_DIR"
output_log "pm2 - setting install path to $2"
output_log "pm2 - setting log dir to $LOG_DIR"
sed -i "s?{INSTALL_DIR}?${2}?g" $1
sed -i "s?{LOG_DIR}?${LOG_DIR}?g" $1
sed -i "s?{PID_DIR}?${PID_DIR}?g" $1
}
# central method to read variables from the .env and overwrite into the nginx config
# $1 is the nginx config
# $2 is the .env to over-write
# $3 is the xapi .env
# $4 is the path to the install - typically this should be the path to the symlink directory rather than the release dir
function setup_nginx_config ()
{
output "Setting up nginx config"
if [[ ! -f $1 ]]; then
output "Warning :: nginx config in $1 can't be found - will need to be edited manually. Press any key to continue"
if [[ $BYPASSALL == false ]]; then
read -r -s -n 1 n
fi
return 0
fi
if [[ ! -f $2 ]]; then
output "Warning :: .env in $2 can't be found, can't set up nginx config correctly - will need to be edited manually. Press any key to continue"
if [[ $BYPASSALL == false ]]; then
read -r -s -n 1 n
fi
return 0
fi
if [[ ! -f $3 ]]; then
output "Warning :: xapi .env in $3 can't be found, can't set up nginx config correctly - will need to be edited manually. Press any key to continue"
if [[ $BYPASSALL == false ]]; then
read -r -s -n 1 n
fi
return 0
fi
UI_PORT=`egrep '^UI_PORT(\s)?=' $2 | tail -1 | sed -r 's/UI_PORT(\s)?=(\s)?//' | sed 's/\r//' `
API_PORT=`egrep '^API_PORT(\s)?=' $2 | tail -1 | sed -r 's/API_PORT(\s)?=(\s)?//' | sed 's/\r//' `
XAPI_PORT=`egrep '^EXPRESS_PORT(\s)?=' $3 | tail -1 | sed -r 's/EXPRESS_PORT(\s)?=(\s)?//' | sed 's/\r//' `
output_log "nginx - setting ui port to $UI_PORT"
output_log "nginx - setting api port to $API_PORT"
output_log "nginx - setting xapi port to $XAPI_PORT"
output_log "nginx - setting site root to $4"
sed -i "s/UI_PORT/${UI_PORT}/" $1
sed -i "s/:API_PORT/:${API_PORT}/" $1
sed -i "s/XAPI_PORT/${XAPI_PORT}/" $1
sed -i "s?/SITE_ROOT?${4}?" $1
sed -i "s/listen/#listen/g" /etc/nginx/sites-enabled/default
}
# $1 is the path to the nginx config file
# $2 is the path to the install dir (symlink dir)
function setup_nginx_enterprise ()
{
output "Running nginx enterprise setup"
if [[ ! -f $1 ]]; then
output "Warning :: nginx config in $1 can't be found - will need to be edited manually. Press any key to continue"
if [[ $BYPASSALL == false ]]; then
read -r -s -n 1 n
fi
return 0
fi
output_log "nginx - setting site root to $2"
sed -i "s?/SITE_ROOT?${2}?" $1
}
function find_clam_config ()
{
CLAMD_CONFIG=/etc/clamd.conf
if [[ -f /etc/clamav/clamd.conf ]]; then
CLAMD_CONFIG=/etc/clamav/clamd.conf
fi
}
#################################################################################
# DEBIAN / UBUNTU FUNCTIONS #
#################################################################################
function debian_install ()
{
export DEBIAN_FRONTEND=noninteractive
# debian & ubuntu have a package called cmdtest which we need to uninstall as it'll conflict with yarn
apt-get remove cmdtest >> $OUTPUT_LOG 2>>$ERROR_LOG &
# we run an apt-get update here in case the distro is out of date
if [[ ! `command -v python` ]] || [[ ! `command -v curl` ]] || [[ ! `command -v wget` ]] || [[ ! `command -v git` ]] || [[ ! `command -v gcc` ]] || [[ ! `command -v g++` ]]; then
apt-get update >> $OUTPUT_LOG 2>>$ERROR_LOG
update-ca-certificates >> $OUTPUT_LOG 2>>$ERROR_LOG
apt-get -y -qq install net-tools curl wget git python3 build-essential xvfb apt-transport-https apt-utils >> $OUTPUT_LOG 2>>$ERROR_LOG
fi
if [[ ! `command -v pwgen ` ]]; then
if [[ $AUTOSETUPUSER == true ]]; then
apt-get -y -qq install pwgen
fi
fi
if [[ ! `command -v python` ]]; then
if [[ `command -v python3` ]]; then
output "Symlinking python3 to python for Yarn"
ln -s `command -v python3` /usr/bin/python
else
output "Something seems to have gone wrong in installing basic software, can't find python or python3 - exiting"
exit 0
fi
fi
INSTALL_NODE=false
if [[ ! `command -v node` ]]; then
INSTALL_NODE=true
output_log "installing node"
elif [[ `node --version | cut -d'.' -f 1` != $NODE_VERSION_STRING ]]; then
INSTALL_NODE=true
output_log "updating node"
else
CUR_NODE_VERSION=`node --version | cut -d'.' -f 1`
output_log "current node version is found as ${CUR_NODE_VERSION}"
fi
if [[ $INSTALL_NODE == true ]]; then
#curl -sL https://deb.nodesource.com/setup_${NODE_VERSION} | bash - >> $OUTPUT_LOG 2>>$ERROR_LOG
apt-get -y -qq install nodejs >> $OUTPUT_LOG 2>>$ERROR_LOG
apt-get -y -qq install npm >> $OUTPUT_LOG 2>>$ERROR_LOG
else
output "Node.js already installed"
fi
if [[ `node --version | cut -d'.' -f 1` != $NODE_VERSION_STRING ]]; then
output "Something went wrong in installing/updating node. This is likely a fault in your apt config. Can't continue"
exit 0
fi
INSTALLED_NODE_VERSION=`node --version`
if [[ $INSTALLED_NODE_VERSION == "" ]]; then
output "ERROR :: node doesn't seem to be installed - exiting"
exit 1
fi
output "node version - $INSTALLED_NODE_VERSION"
if [[ ! `command -v yarn` ]]; then
# curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnpkg-archive-keyring.gpg > /dev/null
# echo "deb [signed-by=/usr/share/keyrings/yarnpkg-archive-keyring.gpg] https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
# apt-get -qq update >> $OUTPUT_LOG 2>>$ERROR_LOG
# apt-get -y -qq install yarn >> $OUTPUT_LOG 2>>$ERROR_LOG
npm install --global yarn
yarn --version >> $OUTPUT_LOG 2>>$ERROR_LOG
else
output "yarn already installed"
fi
}
# $1 is temp path to the webapp subdir
# $2 is the symlink path to the webappsubdir
#
function debian_nginx ()
{
if [[ ! -d $1 ]]; then
output "No temp directory passed to debian_nginx() '${1}', should be impossible - exiting"
exit 0
fi
while true; do
output "The next part of the install process will install nginx and remove any default configs - press 'y' to continue or 'n' to abort (press 'enter' for the default of 'y')"
if [[ $BYPASSALL == true ]]; then
output "bypassing to 'y'"
break
fi
read -r -s -n 1 n
output_log "user entered '${n}'"
if [[ $n == "" ]]; then
n="y"
fi
if [[ $n == "y" ]]; then
break
elif [[ $n == "n" ]]; then
output "Can't continue - you'll need to do this step by hand"
sleep 5
return
fi
done
output "installing nginx..."
apt-get update >> $OUTPUT_LOG 2>>$ERROR_LOG
apt-get -qq -y install nginx >> $OUTPUT_LOG 2>>$ERROR_LOG
print_spinner true
if [[ ! -f ${1}/nginx.conf.example ]]; then
output "default learninglocker nginx config doesn't exist - can't continue. Press any key to continue"
if [[ $BYPASSALL == false ]]; then
read n
fi
return
fi
NGINX_CONFIG=/etc/nginx/conf.d/learninglocker.conf
XAPI_ENV=${PWD}/${XAPI_SUBDIR}/.env
BASE_ENV=${PWD}/${WEBAPP_SUBDIR}/.env
# remove default config if it exists
if [[ -f /etc/nginx/conf.d/default.conf ]]; then
rm /etc/nginx/conf.d/default.conf
fi
mv ${1}/nginx.conf.example $NGINX_CONFIG
# Update default /etc/nginx/nginx.conf if updated configuration supplied
if [[ -f ${1}/__etc__nginx__nginx.conf ]]; then
mv ${1}/__etc__nginx__nginx.conf /etc/nginx/nginx.conf
fi
# sub in variables from the .envs to the nginx config
if [[ $ENTERPRISE == true ]]; then
setup_nginx_enterprise $NGINX_CONFIG $2
else
setup_nginx_config $NGINX_CONFIG $BASE_ENV $XAPI_ENV $2
service nginx restart
fi
}
function debian_mongo ()
{
D_M_I=false
if [[ $OS_VERSION == "Ubuntu" ]]; then
output "Setting up mongo repo for: ${OS_VNO}"
#apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4 >> $OUTPUT_LOG 2>>$ERROR_LOG
#curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
#echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
if [[ $OS_VNO == "22.04" ]]; then
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
fi
if [[ $OS_VNO == "24.04" ]]; then
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
fi
apt-get update >> $OUTPUT_LOG 2>>$ERROR_LOG
systemctl unmask mongod
apt-get -qq -y install mongodb-org >> $OUTPUT_LOG 2>>$ERROR_LOG &
print_spinner true
# Attempt to start via both services - one will likely fail but
output "Attempting to start mongod service...."
output "If this fails you will need to check how the Mongo service is setup for your system and manually start it"
#systemctl enable mongod.service
systemctl start mongod
systemctl enable mongod
D_M_I=true
fi
if [[ $D_M_I == false ]]; then
output "installing mongodb...." true
apt-get -y -qq install mongodb >> $OUTPUT_LOG 2>>$ERROR_LOG &
print_spinner true
fi
}
function debian_redis ()
{
output "installing redis..."
redisInstallOldPath=$(pwd)
apt-get update >> $OUTPUT_LOG 2>>$ERROR_LOG
apt-get install -y -qq redis-server >> $OUTPUT_LOG 2>>$ERROR_LOG
sed -i "s/^supervised .*/supervised systemd/" /etc/redis/redis.conf
sed -i "s/^dir .*/dir \/var\/lib\/redis/" /etc/redis/redis.conf | grep '^dir'
redisServiceFile="/etc/systemd/system/redis.service"
printf "[Unit]\n" >> $redisServiceFile && printf "Description=Redis In-Memory Data Store\n" >> $redisServiceFile && printf "After=network.target\n\n" >> $redisServiceFile && printf "[Service]\n" >> $redisServiceFile && printf "User=redis\n" >> $redisServiceFile && printf "Group=redis\n" >> $redisServiceFile && printf "ExecStop=/usr/local/bin/redis-cli shutdown\n" >> $redisServiceFile && printf "Restart=always\n\n" >> $redisServiceFile && printf "[Install]\n" >> $redisServiceFile && printf "WantedBy=multi-user.target\n" >> $redisServiceFile
adduser --system --group --no-create-home redis >> $OUTPUT_LOG 2>>$ERROR_LOG && chown redis:redis /var/lib/redis && chmod 770 /var/lib/redis
systemctl daemon-reload
systemctl enable redis-server >> $OUTPUT_LOG 2>>$ERROR_LOG
service redis start
cd ${redisInstallOldPath}
}
function debian_clamav ()
{
output "Installing ClamAV...." true
apt-get -y -qq install clamav clamav-daemon >> $OUTPUT_LOG 2>>$ERROR_LOG &
print_spinner true
CLAM_INSTALLED=true
}
#################################################################################
#################################################################################
#################################################################################
# #
# END OF FUNCTIONS #
# #
#################################################################################
#################################################################################
#################################################################################
#################################################################################
# DEFAULT VALUES #
#################################################################################
UPI=false
LOCAL_INSTALL=false
PACKAGE_INSTALL=false
DEFAULT_USER=learninglocker
DEFAULT_SYMLINK_PATH=/usr/local/learninglocker/current
DEFAULT_LOCAL_RELEASE_PATH=/usr/local/learninglocker/releases
DEFAULT_PID_PATH=/var/run
DEFAULT_INSTALL_TYPE=l
LOCAL_PATH=false
LOCAL_USER=false
TMPDIR=$_TD/.tmpdist
GIT_BRANCH="master"
GIT_USER=false
GIT_PASS=false
XAPI_BRANCH="master"
MIN_REDIS_VERSION="2.8.11"
MIN_MONGO_VERSION="4.0.0"
BUILDDIR="${_TD}/learninglocker"
MONGO_INSTALLED=false
REDIS_INSTALLED=false
PM2_OVERRIDE=false
NODE_OVERRIDE=false
NODE_VERSION=18.x
NODE_VERSION_STRING=v18
UPDATE_MODE=false
GIT_ASK=false
GIT_REV=false
RELEASE_PATH=false
SYMLINK_PATH=false
MIN_MEMORY=970
LOG_PATH=/var/log/learninglocker
OUTPUT_LOG=${LOG_PATH}/install.log
CLAM_INSTALL=false
CLAM_PATH=false
WEBAPP_SUBDIR="webapp"
XAPI_SUBDIR="xapi"
ERROR_LOG=$OUTPUT_LOG # placeholder - only want one file for now, may be changed later
JUSTDOIT=false # variable set from CLI via the -y flag to just say yes to all the defaults
BYPASSALL=false # if -y is set to '2' then we bypass any and all questions
AUTOSETUPUSER=false # if -y is set to '3' then we also automatically run through the user setup if we have to
WRITE_AUTOSETUP=false # if -y is set to '4' then we will write to the output file (bottom of the script) for the auto generated credentials
SETUP_AMI=false # if -y is set to '5' then we bypass all questions, don't set up user but do clone out the deploy repo and prep for an AMI setup
ENTERPRISE=false # if true then do things specific to enterprise (ie: don't set up mongo or redis).
# this is designed to be mostly the same as the OS model so search for this variable to see differences
# Can be set with '-e 1' as a command line param - you'll need to have github access to the private repos for it to work
ENTERPRISE_IGNORE_STARTUP=false
FORCE_MONGO_NOINSTALL=false
FORCE_REDIS_NOINSTALL=false
#################################################################################
# START CHECKS #
#################################################################################
# cleanup, just in case
if [ -d $TMPDIR ]; then
rm -R $TMPDIR
fi
if [ -d "${BUILDDIR}" ]; then
echo "clearing old tmp dir"
rm -R ${BUILDDIR}
fi
if [ -d $TMPDIR ]; then
output "tmp directory from prior install (${TMPDIR}) still exists - please clear by hand"
exit 0
fi
# check if root user
if [[ `whoami` != "root" ]]; then
output "Sorry, you need to be root to run this script (currently normal user)"
exit 0
fi
if [[ $EUID > 0 ]]; then
# THIS EUID check checks if we're sudo - I don't want the script to run a sudo for the time being
output "Sorry, you need to be root to run this script (currently sudo)"
exit 0
fi
if [[ ! `command -v openssl` ]]; then
output "Sorry but you need openssl installed to continue"
exit 0
fi
# check system memory (in MB)
SYSTEM_MEMORY=$(free -m | awk '/^Mem/' | awk '{print $2}')
if [[ $SYSTEM_MEMORY -lt $MIN_MEMORY ]]; then
output "You need a minimum of ${MIN_MEMORY}Mb of system memory to install Learning Locker - can't continue"
exit 0