forked from OceanDataTools/openrvdas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_openrvdas.sh
executable file
·1479 lines (1279 loc) · 55.7 KB
/
install_openrvdas.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 -e
# OpenRVDAS is available as open source under the MIT License at
# https:/github.com/oceandatatools/openrvdas
#
# This script installs and configures OpenRVDAS to run. It
# is designed to be run as root. It should take a (relatively) clean
# Linux/MacOS installation and install and configure all the components
# to run the full OpenRVDAS system.
#
# It should be re-run whenever the code has been refresh. Preferably
# by first running 'git pull' to get the latest copy of the script,
# and then running 'utils/build_openrvdas_centos7.sh' to run that
# script.
#
# The script has been designed to be idempotent, that is, if can be
# run over again with no ill effects.
#
# If you have selected "yes" to running OpenRVDAS as a service then,
# once this script has completed, you should be able to point a
# browser to http://[hostname] and see the OpenRVDAS control
# console.
#
# If you selected "no" when asked whether to run OpenRVDAS as a
# service on boot, you will need to manually start the servers:
#
# supervisorctl start all
#
# Regardless, running
#
# supervisorctl status
#
# should show you which services are running.
#
# This script has been tested on a variety of architectures and operating
# systems, but not exhaustively. Bug reports, and even better, bug
# fixes, will be greatly appreciated.
PREFERENCES_FILE='.install_openrvdas_preferences'
# Define this here, even though it's just for MacOS, so that it's defined
# when it's referenced down in install_packages, and doesn't have to
# be defined twice.
###########################################################################
###########################################################################
function exit_gracefully {
echo Exiting.
# Try deactivating virtual environment, if it's active
if [ -n "$INSTALL_ROOT" ];then
deactivate
fi
return -1 2> /dev/null || exit -1 # exit correctly if sourced/bashed
}
#########################################################################
#########################################################################
# Return a normalized yes/no for a value
yes_no() {
QUESTION=$1
DEFAULT_ANSWER=$2
while true; do
read -p "$QUESTION ($DEFAULT_ANSWER) " yn
case $yn in
[Yy]* )
YES_NO_RESULT=yes
break;;
[Nn]* )
YES_NO_RESULT=no
break;;
"" )
YES_NO_RESULT=$DEFAULT_ANSWER
break;;
* ) echo "Please answer yes or no.";;
esac
done
}
###########################################################################
###########################################################################
function get_os_type {
if [[ `uname -s` == 'Darwin' ]];then
OS_TYPE=MacOS
OS_VERSION=`uname -p`
if [[ $HARDWARE_VERSION == 'arm' ]];then
echo
echo "WARNING: detected MacOS ARM architecture. Will install Rosetta emulator and"
echo "rerun this script emulating X86 architecture."
echo
read -p "Hit return to continue or Ctrl-C to exit. " DUMMY_VAR
echo
softwareupdate --install-rosetta --agree-to-license
# Recursively run this script, but now as X86_64
THIS_SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
arch -x86_64 /bin/bash $THIS_SCRIPT_PATH
# Exit quietly after recursive run
return -1 2> /dev/null || exit -1 # exit correctly if sourced/bashed
fi
elif [[ `uname -s` == 'Linux' ]];then
if [[ ! -z `grep "NAME=\"Ubuntu\"" /etc/os-release` ]];then
OS_TYPE=Ubuntu
if [[ ! -z `grep "VERSION_ID=\"18" /etc/os-release` ]];then
OS_VERSION=18
elif [[ ! -z `grep "VERSION_ID=\"20" /etc/os-release` ]];then
OS_VERSION=20
elif [[ ! -z `grep "VERSION_ID=\"21" /etc/os-release` ]];then
OS_VERSION=21
elif [[ ! -z `grep "VERSION_ID=\"22" /etc/os-release` ]];then
OS_VERSION=22
elif [[ ! -z `grep "VERSION_ID=\"23" /etc/os-release` ]];then
OS_VERSION=23
elif [[ ! -z `grep "VERSION_ID=\"24" /etc/os-release` ]];then
OS_VERSION=24
else
echo "Sorry - unknown Ubuntu OS Version! - exiting."
exit_gracefully
fi
# With Debian (Raspbian) we're mapping to Ubuntu. Not clear that
# version 10 -> 18 is the right map, but 11 (bullseye)-> 20 seems to work
elif [[ ! -z `grep "NAME=\"Debian" /etc/os-release` ]] || [[ ! -z `grep "NAME=\"Raspbian" /etc/os-release` ]];then
OS_TYPE=Ubuntu
if [[ ! -z `grep "VERSION_ID=\"10" /etc/os-release` ]];then
OS_VERSION=18
elif [[ ! -z `grep "VERSION_ID=\"11" /etc/os-release` ]];then
OS_VERSION=20
elif [[ ! -z `grep "VERSION_ID=\"12" /etc/os-release` ]];then
OS_VERSION=22
else
echo "Sorry - unknown Debian OS Version! - exiting."
exit_gracefully
fi
# CentOS/RHEL
elif [[ ! -z `grep "NAME=\"CentOS Stream\"" /etc/os-release` ]] || [[ ! -z `grep "NAME=\"CentOS Linux\"" /etc/os-release` ]] || [[ ! -z `grep "NAME=\"Red Hat Enterprise Linux\"" /etc/os-release` ]] || [[ ! -z `grep "NAME=\"Rocky Linux\"" /etc/os-release` ]];then
OS_TYPE=CentOS
if [[ ! -z `grep "VERSION_ID=\"7" /etc/os-release` ]];then
OS_VERSION=7
elif [[ ! -z `grep "VERSION_ID=\"8" /etc/os-release` ]];then
OS_VERSION=8
elif [[ ! -z `grep "VERSION_ID=\"9" /etc/os-release` ]];then
OS_VERSION=9
# Rocky Linux uses different format in /etc/os-release
elif [[ ! -z `grep "VERSION=\"7" /etc/os-release` ]];then
OS_VERSION=7
elif [[ ! -z `grep "VERSION=\"8" /etc/os-release` ]];then
OS_VERSION=8
elif [[ ! -z `grep "VERSION=\"9" /etc/os-release` ]];then
OS_VERSION=9
else
echo "Sorry - unknown CentOS/RHEL Version! - exiting."
exit_gracefully
fi
else
echo "Sorry - unknown Linux variant!"
exit_gracefully
fi
else
echo Unknown OS type: `uname -s`
exit_gracefully
fi
}
###########################################################################
###########################################################################
# Read any pre-saved default variables from file
function set_default_variables {
# Defaults that will be overwritten by the preferences file, if it
# exists.
DEFAULT_HOSTNAME=$HOSTNAME
DEFAULT_INSTALL_ROOT=/opt
#DEFAULT_HTTP_PROXY=proxy.lmg.usap.gov:3128 #$HTTP_PROXY
DEFAULT_HTTP_PROXY=$http_proxy
DEFAULT_OPENRVDAS_REPO=https://github.com/oceandatatools/openrvdas
DEFAULT_OPENRVDAS_BRANCH=master
DEFAULT_NONSSL_SERVER_PORT=80
DEFAULT_SSL_SERVER_PORT=443
DEFAULT_USE_SSL=no
DEFAULT_HAVE_SSL_CERTIFICATE=no
DEFAULT_SSL_CRT_LOCATION=
DEFAULT_SSL_KEY_LOCATION=
DEFAULT_RVDAS_USER=rvdas
DEFAULT_INSTALL_FIREWALLD=no
DEFAULT_OPENRVDAS_AUTOSTART=yes
DEFAULT_INSTALL_SIMULATE_NBP=no
DEFAULT_RUN_SIMULATE_NBP=no
DEFAULT_INSTALL_GUI=yes
DEFAULT_SUPERVISORD_WEBINTERFACE=no
DEFAULT_SUPERVISORD_WEBINTERFACE_AUTH=no
DEFAULT_SUPERVISORD_WEBINTERFACE_PORT=9001
DEFAULT_INSTALL_DOC_MARKDOWN=no
# Read in the preferences file, if it exists, to overwrite the defaults.
if [ -e $PREFERENCES_FILE ]; then
echo "#####################################################################"
echo Reading pre-saved defaults from "$PREFERENCES_FILE"
source $PREFERENCES_FILE
fi
}
###########################################################################
###########################################################################
# Save defaults in a preferences file for the next time we run.
function save_default_variables {
cat > $PREFERENCES_FILE <<EOF
# Defaults written by/to be read by install_openrvdas.sh
DEFAULT_HOSTNAME=$HOSTNAME
DEFAULT_INSTALL_ROOT=$INSTALL_ROOT
#DEFAULT_HTTP_PROXY=proxy.lmg.usap.gov:3128 #$HTTP_PROXY
DEFAULT_HTTP_PROXY=$HTTP_PROXY
DEFAULT_OPENRVDAS_REPO=$OPENRVDAS_REPO
DEFAULT_OPENRVDAS_BRANCH=$OPENRVDAS_BRANCH
DEFAULT_NONSSL_SERVER_PORT=$NONSSL_SERVER_PORT
DEFAULT_SSL_SERVER_PORT=$SSL_SERVER_PORT
DEFAULT_USE_SSL=$USE_SSL
DEFAULT_HAVE_SSL_CERTIFICATE=$HAVE_SSL_CERTIFICATE
DEFAULT_SSL_CRT_LOCATION=$SSL_CRT_LOCATION
DEFAULT_SSL_KEY_LOCATION=$SSL_KEY_LOCATION
DEFAULT_RVDAS_USER=$RVDAS_USER
DEFAULT_INSTALL_FIREWALLD=$INSTALL_FIREWALLD
DEFAULT_OPENRVDAS_AUTOSTART=$OPENRVDAS_AUTOSTART
DEFAULT_INSTALL_GUI=$INSTALL_GUI
DEFAULT_INSTALL_SIMULATE_NBP=$INSTALL_SIMULATE_NBP
DEFAULT_RUN_SIMULATE_NBP=$RUN_SIMULATE_NBP
DEFAULT_SUPERVISORD_WEBINTERFACE=$SUPERVISORD_WEBINTERFACE
DEFAULT_SUPERVISORD_WEBINTERFACE_AUTH=$SUPERVISORD_WEBINTERFACE_AUTH
DEFAULT_SUPERVISORD_WEBINTERFACE_PORT=$SUPERVISORD_WEBINTERFACE_PORT
DEFAULT_INSTALL_DOC_MARKDOWN=$INSTALL_DOC_MARKDOWN
EOF
}
###########################################################################
###########################################################################
# Set hostname
function set_hostname {
HOSTNAME=$1
# If we're on MacOS
if [ $OS_TYPE == 'MacOS' ]; then
echo Not changing hostname on MacOS
#sudo scutil --set HostName $HOSTNAME
#sudo scutil --set LocalHostName $HOSTNAME
#sudo scutil --set ComputerName $HOSTNAME
#dscacheutil -flushcache
# If we're on CentOS/RHEL
elif [ $OS_TYPE == 'CentOS' ]; then
sudo hostnamectl set-hostname $HOSTNAME
sudo echo "HOSTNAME=$HOSTNAME" > /etc/sysconfig/network || echo "Unable to update /etc/sysconfig/network"
# Ubuntu/Debian
elif [ $OS_TYPE == 'Ubuntu' ]; then
sudo hostnamectl set-hostname $HOSTNAME
sudo echo $HOSTNAME > /etc/hostname || echo "Unable to update /etc/hostname"
fi
ETC_HOSTS_LINE="127.0.1.1 $HOSTNAME"
if grep -q "$ETC_HOSTS_LINE" /etc/hosts ; then
echo Hostname already in /etc/hosts
else
echo Skipping adding to /etc/hosts
sudo echo "$ETC_HOSTS_LINE" >> /etc/hosts || echo "Unable to update /etc/hosts"
fi
}
###########################################################################
###########################################################################
# Create user
function create_user {
RVDAS_USER=$1
echo Checking if user $RVDAS_USER exists yet
if id -u $RVDAS_USER > /dev/null; then
echo User "$RVDAS_USER" exists
else
# MacOS
if [ $OS_TYPE == 'MacOS' ]; then
echo No such pre-existing user: $RVDAS.
echo On MacOS, must install for pre-existing user. Exiting.
exit_gracefully
# CentOS/RHEL
elif [ $OS_TYPE == 'CentOS' ]; then
echo Creating $RVDAS_USER
sudo adduser $RVDAS_USER
sudo passwd $RVDAS_USER
# Ubuntu/Debian
elif [ $OS_TYPE == 'Ubuntu' ]; then
echo Creating $RVDAS_USER
sudo adduser --gecos "" $RVDAS_USER
fi
fi
# Set up user permissions, whether or not pre-existing.
# For MacOS we don't change anything
if [ $OS_TYPE == 'CentOS' ]; then
sudo usermod -a -G tty $RVDAS_USER
sudo usermod -a -G wheel $RVDAS_USER
# Ubuntu/Debian
elif [ $OS_TYPE == 'Ubuntu' ]; then
sudo usermod -a -G tty $RVDAS_USER
sudo usermod -a -G dialout $RVDAS_USER
sudo usermod -a -G sudo $RVDAS_USER
fi
}
###########################################################################
###########################################################################
# Install and configure required packages
function install_packages {
# MacOS
if [ $OS_TYPE == 'MacOS' ]; then
# Install Homebrew - note: reinstalling is idempotent
echo 'Installing XCode Tools'
xcode-select --install || echo "XCode Tools already installed"
pushd /tmp
HOMEBREW_VERSION=4.1.21
HOMEBREW_TARGET=Homebrew-${HOMEBREW_VERSION}.pkg
HOMEBREW_PATH=https://github.com/Homebrew/brew/releases/download/${HOMEBREW_VERSION}/${HOMEBREW_TARGET}
curl -O -L ${HOMEBREW_PATH}
# The -target / specifies that the package should be installed on the root volume.
sudo installer -pkg ${HOMEBREW_TARGET} -target /
popd
brew install python git nginx supervisor
#brew upgrade openssh nginx supervisor || echo Upgraded packages
#brew link --overwrite python || echo Linking Python
# CentOS/RHEL
elif [ $OS_TYPE == 'CentOS' ]; then
if [ $OS_VERSION == '7' ]; then
sudo yum install -y deltarpm
fi
sudo yum install -y epel-release
sudo yum -y update
echo Installing required packages
sudo yum install -y wget git nginx gcc supervisor \
zlib-devel openssl-devel readline-devel libffi-devel \
sqlite libsqlite3x-devel
#sqlite-devel \
#python3 python3-devel python3-pip
# Django 3+ requires a more recent version of sqlite3 than is
# included in CentOS 7. So instead of yum installing python3 and
# sqlite3, we build them from scratch. Painfully slow, but hey -
# isn't that par for CentOS builds?
export LD_LIBRARY_PATH=/usr/local/lib
export LD_RUN_PATH=/usr/local/lib
# Check if correct SQLite3 is installed
SQLITE_VERSION=3320300
#required_version="3.32.3"
if ! command -v sqlite3 &> /dev/null
then
echo "SQLite3 is not installed. Installing ..."
sudo yum install -y sqlite sqlite-devel
# else
# # Get the current version of SQLite3
# current_version=$(sqlite3 --version | awk '{print $1}')
#
# # Compare the current version with the required version
# if [[ "$current_version" != "$required_version" ]]
# then
# echo "SQLite3 version $required_version is required, but version $current_version is installed. Installing version $required_version..."
# sudo yum install -y sqlite-$required_version sqlite-devel
# else
# echo "SQLite3 version $required_version is already installed."
# fi
fi
if [ $OS_VERSION == '7' ]; then
# Build Python, too, if we don't have right version
PYTHON_VERSION='3.8.3'
if [ "`/usr/local/bin/python3 --version`" == "Python $PYTHON_VERSION" ]; then
echo Already have appropriate version of Python3
else
cd /var/tmp
PYTHON_BASE=Python-${PYTHON_VERSION}
PYTHON_TGZ=${PYTHON_BASE}.tgz
[ -e $PYTHON_TGZ ] || wget https://www.python.org/ftp/python/${PYTHON_VERSION}/${PYTHON_TGZ}
tar xvf ${PYTHON_TGZ}
cd ${PYTHON_BASE}
sh ./configure # --enable-optimizations
sudo make altinstall
sudo ln -s -f /usr/local/bin/python3.8 /usr/local/bin/python3
sudo ln -s -f /usr/local/bin/pip3.8 /usr/local/bin/pip3
fi
elif [ $OS_VERSION == '8' ] || [ $OS_VERSION == '9' ]; then
sudo yum install -y python3 python3-devel
else
echo "Install error: unknown OS_VERSION should have been caught earlier?!?"
echo "OS_VERSION = \"$OS_VERSION\""
exit_gracefully
fi
# Ubuntu/Debian
elif [ $OS_TYPE == 'Ubuntu' ]; then
sudo apt-get update
sudo apt install -y git nginx libreadline-dev \
python3-dev python3-pip python3-venv libsqlite3-dev \
openssh-server supervisor libssl-dev
fi
}
###########################################################################
###########################################################################
# Install OpenRVDAS
function install_openrvdas {
# Expect the following shell variables to be appropriately set:
# INSTALL_ROOT - path where openrvdas/ is
# RVDAS_USER - valid userid
# OPENRVDAS_REPO - path to OpenRVDAS repo
# OPENRVDAS_BRANCH - branch of rep to install
if [ ! -d $INSTALL_ROOT ]; then
echo "Making install directory \"$INSTALL_ROOT\""
sudo mkdir -p $INSTALL_ROOT
sudo chown ${RVDAS_USER} $INSTALL_ROOT
fi
cd $INSTALL_ROOT
if [ ! -e openrvdas ]; then
echo "Making openrvdas directory."
sudo mkdir openrvdas
sudo chown ${RVDAS_USER} openrvdas
fi
if [ -e openrvdas/.git ] ; then # If we've already got an installation
cd openrvdas
git pull
git checkout $OPENRVDAS_BRANCH
git pull
else # If we don't already have an installation
sudo rm -rf openrvdas # in case there's a non-git dir there
sudo mkdir openrvdas
sudo chown ${RVDAS_USER} openrvdas
git clone -b $OPENRVDAS_BRANCH $OPENRVDAS_REPO
cd openrvdas
fi
# Copy widget settings into place and customize for this machine
cp display/js/widgets/settings.js.dist \
display/js/widgets/settings.js
sed -i -e "s/= 'ws'/= '${WEBSOCKET_PROTOCOL}'/g" display/js/widgets/settings.js
sed -i -e "s/localhost/${HOSTNAME}/g" display/js/widgets/settings.js
sed -i -e "s/ = 80/ = ${SERVER_PORT}/g" display/js/widgets/settings.js
# Copy the database settings.py.dist into place so that other
# routines can make the modifications they need to it.
cp database/settings.py.dist database/settings.py
sed -i -e "s/DEFAULT_DATABASE_USER = 'rvdas'/DEFAULT_DATABASE_USER = '${RVDAS_USER}'/g" database/settings.py
sed -i -e "s/DEFAULT_DATABASE_PASSWORD = 'rvdas'/DEFAULT_DATABASE_PASSWORD = '${RVDAS_DATABASE_PASSWORD}'/g" database/settings.py
}
###########################################################################
###########################################################################
# Set up Python packages
function setup_python_packages {
# Expect the following shell variables to be appropriately set:
# INSTALL_ROOT - path where openrvdas/ is
# Set up virtual environment
VENV_PATH=$INSTALL_ROOT/openrvdas/venv
## Bit of a challenge here - if our new install has a newer version of
## Python or something, reusing the existing venv can cause subtle
## havoc. But deleting and rebuilding it each time is a mess. Commenting
## out the delete for now...
##
# We'll rebuild the virtual environment each time to avoid version skew
#if [ -d $VENV_PATH ];then
# mv $VENV_PATH ${VENV_PATH}.bak.$$
#fi
#if [ -e '${HOMEBREW_BASE}/bin/python3' ];then
# eval "$(${HOMEBREW_BASE}/bin/brew shellenv)"
# PYTHON_PATH=${HOMEBREW_BASE}/bin/python3
#elif [ -e '/usr/local/bin/python3' ];then
# PYTHON_PATH=/usr/local/bin/python3
#elif [ -e '/usr/bin/python3' ];then
# PYTHON_PATH=/usr/bin/python3
#else
# echo 'No python3 found?!?'
# exit_gracefully
#fi
echo "Creating virtual environment"
cd $INSTALL_ROOT/openrvdas
python3 -m venv $VENV_PATH
source $VENV_PATH/bin/activate # activate virtual environment
echo "Installing Python packages - please enter sudo password if prompted."
# For some reason, locked down RHEL8 boxes require sudo here, and require
# us to execute pip via python. Lord love a duck...
venv/bin/python venv/bin/pip3 install \
--trusted-host pypi.org --trusted-host files.pythonhosted.org \
--upgrade pip
venv/bin/python venv/bin/pip3 install \
--trusted-host pypi.org --trusted-host files.pythonhosted.org \
wheel
venv/bin/python venv/bin/pip3 install -r utils/requirements.txt
}
###########################################################################
###########################################################################
# Set up NGINX
function setup_nginx {
# CentOS/RHEL or Debian/Ubuntu
if [ $OS_TYPE == 'CentOS' ] || [ $OS_TYPE == 'Ubuntu' ]; then
# Disable because we're going to run it via supervisor
systemctl stop nginx
systemctl disable nginx # NGINX seems to be enabled by default?
fi
if [ "$USE_SSL" == "yes" ]; then
SERVER_PROTOCOL='ssl'
SSL_COMMENT='' # don't comment out SSL stuff
else
SERVER_PROTOCOL='default_server'
SSL_COMMENT='#' # do comment out SSL stuff
fi
if [ $INSTALL_DOC_MARKDOWN == 'yes' ];then
MARKDOWN_COMMENT='' # We uncomment the Strapdown-related lines in conf
else
MARKDOWN_COMMENT='#'
fi
# Now create the nginx conf file in place and link it up
cat > $INSTALL_ROOT/openrvdas/django_gui/openrvdas_nginx.conf<<EOF
# openrvdas_nginx.conf
worker_processes auto;
events {
worker_connections 1024;
}
http {
#include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
# the upstream component nginx needs to connect to
upstream django {
server unix://${INSTALL_ROOT}/openrvdas/django_gui/openrvdas.sock; # for a file socket
}
# OpenRVDAS HTTPS server
server {
# the port your site will be served on; typically 443
listen *:${SERVER_PORT} ${SERVER_PROTOCOL};
server_name _; # accept any host name
charset utf-8;
# Section will be commented out if we're not using SSL
${SSL_COMMENT}ssl_certificate $SSL_CRT_LOCATION;
${SSL_COMMENT}ssl_certificate_key $SSL_KEY_LOCATION;
${SSL_COMMENT}ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
${SSL_COMMENT}ssl_ciphers HIGH:!aNULL:!MD5;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias ${INSTALL_ROOT}/openrvdas/media; # project media files
}
location /display {
alias ${INSTALL_ROOT}/openrvdas/display/html; # display pages
autoindex on;
}
location /js {
alias /${INSTALL_ROOT}/openrvdas/display/js; # display pages
default_type application/javascript;
}
location /css {
alias /${INSTALL_ROOT}/openrvdas/display/css; # display pages
default_type text/css;
}
location /static {
alias ${INSTALL_ROOT}/openrvdas/static; # project static files
autoindex on;
}
location /docs {
alias ${INSTALL_ROOT}/openrvdas/docs; # project doc files
autoindex on;
}
# Added by KPed so Markdown renders in the browser
# See https://gist.github.com/shukebeta/b7435d02892cb2ad2b9c8d56572adb2b
${MARKDOWN_COMMENT}location ~ /.*\.md {
${MARKDOWN_COMMENT} root /opt/openrvdas;
${MARKDOWN_COMMENT} default_type text/html;
${MARKDOWN_COMMENT} charset UTF-8;
${MARKDOWN_COMMENT} add_before_body /static/StrapDown.js/prepend;
${MARKDOWN_COMMENT} add_after_body /static/StrapDown.js/postpend;
${MARKDOWN_COMMENT}}
# Internally, Cached Data Server operates on port 8766; we proxy
# it externally, serve cached data server at $SERVER_PORT/cds-ws
location /cds-ws {
if ($request_method = OPTIONS) {
return 204;
}
proxy_pass http://localhost:8766;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host \$host;
}
# Finally, send all non-CDS, non-media requests to the Django server.
location / {
uwsgi_pass django;
include ${INSTALL_ROOT}/openrvdas/django_gui/uwsgi_params;
}
}
}
EOF
}
###########################################################################
###########################################################################
# Set up certificate files, if requested
function setup_ssl_certificate {
echo "Certificate will be placed in ${SSL_CRT_LOCATION}"
echo "Key will be placed in ${SSL_KEY_LOCATION}"
echo "Please answer the following prompts to continue:"
echo
openssl req \
-newkey rsa:2048 -nodes -keyout ${SSL_KEY_LOCATION} \
-x509 -days 365 -out ${SSL_CRT_LOCATION}
}
###########################################################################
###########################################################################
# Set up Django
function setup_django {
# Expect the following shell variables to be appropriately set:
# RVDAS_USER - valid userid
# RVDAS_DATABASE_PASSWORD - string to use for Django password
cd ${INSTALL_ROOT}/openrvdas
source ${INSTALL_ROOT}/openrvdas/venv/bin/activate
cp django_gui/settings.py.dist django_gui/settings.py
sed -i -e "s/WEBSOCKET_PROTOCOL = 'ws'/WEBSOCKET_PROTOCOL = '${WEBSOCKET_PROTOCOL}'/g" django_gui/settings.py
sed -i -e "s/WEBSOCKET_PORT = 80/WEBSOCKET_PORT = ${SERVER_PORT}/g" django_gui/settings.py
sed -i -e "s/'USER': 'rvdas'/'USER': '${RVDAS_USER}'/g" django_gui/settings.py
sed -i -e "s/'PASSWORD': 'rvdas'/'PASSWORD': '${RVDAS_DATABASE_PASSWORD}'/g" django_gui/settings.py
# NOTE: we're still inside virtualenv, so we're getting the python
# that was installed under it.
python3 manage.py makemigrations django_gui
python3 manage.py migrate
rm -rf static
python3 manage.py collectstatic --no-input --clear --link -v 0
chmod -R og+rX static
# A temporary hack to allow the display/ pages to be accessed by Django
# in their old location of static/widgets/
cd static;ln -s html widgets;cd ..
# Bass-ackwards way of creating superuser $RVDAS_USER, as the
# createsuperuser command won't work from a script
python3 manage.py shell <<EOF
from django.contrib.auth.models import User
try:
User.objects.get(username='${RVDAS_USER}').delete()
except User.DoesNotExist:
pass
User.objects.create_superuser('${RVDAS_USER}',
'${RVDAS_USER}@example.com',
'${RVDAS_DATABASE_PASSWORD}')
EOF
}
###########################################################################
###########################################################################
# Set up UWSGI
function setup_uwsgi {
# Expect the following shell variables to be appropriately set:
# HOSTNAME - name of host
# INSTALL_ROOT - path where openrvdas/ is
# MacOS
if [ $OS_TYPE == 'MacOS' ]; then
ETC_HOME=/opt/homebrew/etc
# CentOS/RHEL and Ubuntu/Debian
elif [ $OS_TYPE == 'CentOS' ] || [ $OS_TYPE == 'Ubuntu' ]; then
ETC_HOME=/etc
fi
cp $ETC_HOME/nginx/uwsgi_params $INSTALL_ROOT/openrvdas/django_gui
cat > ${INSTALL_ROOT}/openrvdas/django_gui/openrvdas_uwsgi.ini <<EOF
# openrvdas_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = ${INSTALL_ROOT}/openrvdas
# Django's wsgi file
module = django_gui.wsgi
# Where to find bin/python
home = ${INSTALL_ROOT}/openrvdas/venv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = ${INSTALL_ROOT}/openrvdas/django_gui/openrvdas.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664
chmod-socket = 666
# clear environment on exit
vacuum = true
EOF
# Make vassal directory and copy symlink in
[ -e $ETC_HOME/uwsgi/vassals ] || mkdir -p $ETC_HOME/uwsgi/vassals
sudo ln -sf ${INSTALL_ROOT}/openrvdas/django_gui/openrvdas_uwsgi.ini \
$ETC_HOME/uwsgi/vassals/
}
###########################################################################
###########################################################################
# Set up supervisord files
function setup_supervisor {
# Expect the following shell variables to be appropriately set:
# RVDAS_USER - valid username
# INSTALL_ROOT - path where openrvdas/ is found
# OPENRVDAS_AUTOSTART - 'true' if we're to autostart, else 'false'
# If we're not installing the web GUI, comment out those bits of
# the supervisor config that run them.
if [[ "$INSTALL_GUI" == "yes" ]];then
GUI_COMMENT=''
else
GUI_COMMENT=';'
fi
VENV_BIN=${INSTALL_ROOT}/openrvdas/venv/bin
if [ $OPENRVDAS_AUTOSTART = 'yes' ]; then
AUTOSTART=true
else
AUTOSTART=false
fi
# Whether the simulation script is commented out, and if not,
# whether it should autorun on boot
SIMULATE_NBP_COMMENT=';'
AUTOSTART_SIMULATE_NBP='false'
if [ $INSTALL_SIMULATE_NBP = 'yes' ]; then
SIMULATE_NBP_COMMENT=''
fi
if [ $RUN_SIMULATE_NBP = 'yes' ]; then
AUTOSTART_SIMULATE_NBP='true'
fi
# MacOS
if [ $OS_TYPE == 'MacOS' ]; then
ETC_HOME=/usr/local/etc
HTTP_HOST=127.0.0.1
NGINX_BIN=/usr/local/bin/nginx
SUPERVISOR_DIR=/usr/local/etc/supervisor.d/
SUPERVISOR_SUFFIX='ini'
SUPERVISOR_SOCK=/usr/local/var/run/supervisor.sock
COMMENT_SOCK_OWNER=';'
# CentOS/RHEL
elif [ $OS_TYPE == 'CentOS' ]; then
ETC_HOME=/etc
HTTP_HOST='*'
NGINX_BIN=/usr/sbin/nginx
SUPERVISOR_DIR=/etc/supervisord.d
SUPERVISOR_SUFFIX='ini'
SUPERVISOR_SOCK=/var/run/supervisor/supervisor.sock
COMMENT_SOCK_OWNER=''
# Ubuntu/Debian
elif [ $OS_TYPE == 'Ubuntu' ]; then
ETC_HOME=/etc
HTTP_HOST='*'
NGINX_BIN=/usr/sbin/nginx
SUPERVISOR_DIR=/etc/supervisor/conf.d
SUPERVISOR_SUFFIX='conf'
SUPERVISOR_SOCK=/var/run/supervisor.sock
COMMENT_SOCK_OWNER=''
fi
SUPERVISOR_FILE=$SUPERVISOR_DIR/openrvdas.${SUPERVISOR_SUFFIX}
LOGGER_MANAGER_FILE=$SUPERVISOR_DIR/openrvdas_logger_manager.${SUPERVISOR_SUFFIX}
CACHED_DATA_FILE=$SUPERVISOR_DIR/openrvdas_cached_data.${SUPERVISOR_SUFFIX}
DJANGO_FILE=$SUPERVISOR_DIR/openrvdas_django.${SUPERVISOR_SUFFIX}
SIMULATE_FILE=$SUPERVISOR_DIR/openrvdas_simulate.${SUPERVISOR_SUFFIX}
sudo mkdir -p $SUPERVISOR_DIR
#######################################################
# Write out the overall supervisor file, filling in variables
TEMP_FILE=/tmp/openrvdas_tmp.ini
sudo rm -f $TEMP_FILE
cat > $TEMP_FILE <<EOF
; First, override the default socket permissions to allow user
; $RVDAS_USER to run supervisorctl
[unix_http_server]
file=$SUPERVISOR_SOCK ; (the path to the socket file)
chmod=0770 ; socket file mode (default 0700)
${COMMENT_SOCK_OWNER}chown=nobody:${RVDAS_GROUP}
EOF
if [ $SUPERVISORD_WEBINTERFACE == 'yes' ]; then
cat >> $TEMP_FILE <<EOF
[inet_http_server]
port=${SUPERVISORD_WEBINTERFACE_PORT}
EOF
if [ $SUPERVISORD_WEBINTERFACE_AUTH == 'yes' ]; then
SUPERVISORD_WEBINTERFACE_HASH=`echo -n ${SUPERVISORD_WEBINTERFACE_PASS} | sha1sum | awk '{printf("{SHA}%s",$1)}'`
cat >> $TEMP_FILE <<EOF
username=${SUPERVISORD_WEBINTERFACE_USER}
password=${SUPERVISORD_WEBINTERFACE_HASH} ; echo -n "<password>" | sha1sum | awk '{printf("{SHA}%s",\$1)}'
EOF
fi
fi
sudo mv $TEMP_FILE $SUPERVISOR_FILE
#######################################################
# Write out the Logger Manager file
cat > $TEMP_FILE <<EOF
; Supervisor configurations for LoggerManager
[program:logger_manager]
command=${VENV_BIN}/python server/logger_manager.py --database django --data_server_websocket :8766 -v -V --no-console
environment=PATH="${VENV_BIN}:/usr/bin:/usr/local/bin"
directory=${INSTALL_ROOT}/openrvdas
autostart=$AUTOSTART
autorestart=true
startretries=3
killasgroup=true
stderr_logfile=/var/log/openrvdas/logger_manager.stderr
stderr_logfile_maxbytes=10000000 ; 10M
stderr_logfile_maxbackups=100
user=$RVDAS_USER
EOF
sudo mv $TEMP_FILE $LOGGER_MANAGER_FILE
#######################################################
# Write out the Cached Data Server file
cat > $TEMP_FILE <<EOF
; Supervisor configurations for LoggerManager and CachedDataServer
[program:cached_data_server]
command=${VENV_BIN}/python server/cached_data_server.py --port 8766 --disk_cache /var/tmp/openrvdas/disk_cache --max_records 8640 -v
directory=${INSTALL_ROOT}/openrvdas
autostart=$AUTOSTART
autorestart=true
startretries=3
killasgroup=true
stderr_logfile=/var/log/openrvdas/cached_data_server.stderr
stderr_logfile_maxbytes=10000000 ; 10M
stderr_logfile_maxbackups=100
user=$RVDAS_USER
EOF
sudo mv $TEMP_FILE $CACHED_DATA_FILE
#######################################################
# Write out the Django GUI files, filling in variables
cat > $TEMP_FILE <<EOF
; Supervisor configurations for Django GUI
${GUI_COMMENT}[program:nginx]
${GUI_COMMENT}command=${NGINX_BIN} -g 'daemon off;' -c ${INSTALL_ROOT}/openrvdas/django_gui/openrvdas_nginx.conf
${GUI_COMMENT}directory=${INSTALL_ROOT}/openrvdas
${GUI_COMMENT}autostart=$AUTOSTART
${GUI_COMMENT}autorestart=true
${GUI_COMMENT}startretries=3
${GUI_COMMENT}killasgroup=true
${GUI_COMMENT}stderr_logfile=/var/log/openrvdas/nginx.stderr
${GUI_COMMENT}stderr_logfile_maxbytes=10000000 ; 10M
${GUI_COMMENT}stderr_logfile_maxbackups=100
${GUI_COMMENT};user=$RVDAS_USER
${GUI_COMMENT}[program:uwsgi]
${GUI_COMMENT}command=${VENV_BIN}/uwsgi ${INSTALL_ROOT}/openrvdas/django_gui/openrvdas_uwsgi.ini --thunder-lock --enable-threads
${GUI_COMMENT}stopsignal=INT
${GUI_COMMENT}directory=${INSTALL_ROOT}/openrvdas
${GUI_COMMENT}autostart=$AUTOSTART
${GUI_COMMENT}autorestart=true
${GUI_COMMENT}startretries=3
${GUI_COMMENT}killasgroup=true
${GUI_COMMENT}stderr_logfile=/var/log/openrvdas/uwsgi.stderr
${GUI_COMMENT}stderr_logfile_maxbytes=10000000 ; 10M
${GUI_COMMENT}stderr_logfile_maxbackups=100
${GUI_COMMENT}user=$RVDAS_USER
${GUI_COMMENT}[group:django]
${GUI_COMMENT}programs=nginx,uwsgi
EOF
sudo mv $TEMP_FILE $DJANGO_FILE
#######################################################
# Write out the simulator commands, filling in variables
cat > $TEMP_FILE <<EOF
; Supervisor configurations for OpenRVDAS data simulator
${SIMULATE_NBP_COMMENT}[program:simulate_nbp]
${SIMULATE_NBP_COMMENT}command=${VENV_BIN}/python logger/utils/simulate_data.py --config test/NBP1406/simulate_NBP1406.yaml
${SIMULATE_NBP_COMMENT}directory=${INSTALL_ROOT}/openrvdas
${SIMULATE_NBP_COMMENT}autostart=${AUTOSTART_SIMULATE_NBP}
${SIMULATE_NBP_COMMENT}autorestart=true
${SIMULATE_NBP_COMMENT}startretries=3
${SIMULATE_NBP_COMMENT}killasgroup=true
${SIMULATE_NBP_COMMENT}stderr_logfile=/var/log/openrvdas/simulate_nbp.stderr
${SIMULATE_NBP_COMMENT}stderr_logfile_maxbytes=10000000 ; 10M
${SIMULATE_NBP_COMMENT}stderr_logfile_maxbackups=100
${SIMULATE_NBP_COMMENT}user=$RVDAS_USER
${SIMULATE_NBP_COMMENT}[group:simulate]
${SIMULATE_NBP_COMMENT}programs=simulate_nbp
EOF
sudo cp $TEMP_FILE $SIMULATE_FILE
}
###########################################################################
###########################################################################
# CentOS/RHEL ONLY - Set up firewall daemon and open relevant ports
function setup_firewall {
if [ $OS_TYPE == 'MacOS' ] || [ $OS_TYPE == 'Ubuntu' ]; then
echo "No firewall setup on $OS_TYPE"
return
fi
# All this is CentOS/RHEL only
yum install -y firewalld
echo "#####################################################################"
echo "Setting SELINUX permissions and firewall ports"
echo "This could take a while..."
# The old way of enabling things...
# (sed -i -e 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config) || echo UNABLE TO UPDATE SELINUX! Continuing...
# The new way of more selectively enabling things
setsebool -P nis_enabled 1
setsebool -P use_nfs_home_dirs 1
setsebool -P httpd_can_network_connect 1
semanage permissive -a httpd_t
# Set up the firewall and open some holes in it
systemctl start firewalld
systemctl enable firewalld