-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·973 lines (862 loc) · 32 KB
/
install.sh
File metadata and controls
executable file
·973 lines (862 loc) · 32 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
#!/bin/bash
APP_VERSION="3.1.0dev"
# usage: prints the command line help and usage examples.
usage() {
cat << EOF
This script install and upgrade the relecov-platform app.
usage : $0 --upgrade --git_revision --conf
Optional input data:
--install | Install relecov-platform full/dep/app
--upgrade | Upgrade relecov-platform full/dep/app
--stage | Stage app files only (install/upgrade) without DB work. Internal/container use.
--bootstrap | Run DB/bootstrap steps only (install/upgrade) against an existing staged app. Internal/container use.
--git_revision | Git revision name to run (branch, tag, commit SHA, or 'current' to use copied local sources as-is)
--conf | Select custom configuration file. Default: ./install_settings.txt
--tables | Load the first inital tables (from conf folder)
--skip_tables | Skip loading initial tables (even during install)
--script | Run a migration script after migrations.
--script_before | Run a migration script before migrations.
--script_after | Run a migration script after migrations (same as --script).
--ren_app | Rename apps required for the upgrade migration to 3.0.0
--docker | Deprecated. Use --skip_apache_restart to avoid Apache checks/restart.
Examples:
Install relecov-platform only dep
sudo $0 --install dep
Install only relecov-platform app
$0 --install app
Upgrade using develop code
$0 --upgrade full --git_revision develop
Upgrade running migration script and update initial tables
$0 --upgrade full --script <migration_script> --tables
Stage application files during a container image build
$0 --stage install --git_revision main --conf conf/docker_production_settings.txt
Bootstrap database/static using an already staged container image
$0 --bootstrap upgrade --git_revision main --conf conf/docker_production_settings.txt
Make adjustments for apps renaming in upgrade 2.3.0 to 2.3.1
$0 --upgrade full --ren_app --script <migration_script> --tables
Upgrade running pre/post migration scripts:
$0 --upgrade app --script_before <pre_script> --script_after <post_script>
EOF
}
# log: write timestamped log entries to stdout.
_log_compose_entry() {
local level="$1"; shift
local message="$*"
local timestamp
timestamp="$(date '+%Y-%m-%d %H:%M:%S')"
printf "%s [%s] %s" "$timestamp" "$level" "$message"
}
log() {
local level="$1"; shift
local message="$*"
local entry
entry="$(_log_compose_entry "$level" "$message")"
printf "%s\n" "$entry"
}
# db_check: verifies connectivity to the configured MySQL instance using mysqladmin/mysqlshow.
db_check(){
log "INFO" "Checking database connectivity against $DB_SERVER_IP:$DB_PORT"
local mysqladmin_bin
local mysqlshow_bin
mysqladmin_bin="$(command -v mysqladmin || command -v mariadb-admin || true)"
mysqlshow_bin="$(command -v mysqlshow || command -v mariadb-show || true)"
if [ -z "$mysqladmin_bin" ] || [ -z "$mysqlshow_bin" ]; then
log "ERROR" "mysql client tools not found (mysqladmin/mysqlshow or mariadb-admin/mariadb-show)."
exit 1
fi
"$mysqladmin_bin" -h $DB_SERVER_IP -u$DB_USER -p$DB_PASS -P$DB_PORT processlist > /dev/null
if ! [ $? -eq 0 ]; then
log "ERROR" "Unable to connect to database. Check if your database is running and accessible"
exit 1
fi
RESULT=`"$mysqlshow_bin" --user=$DB_USER --password=$DB_PASS --host=$DB_SERVER_IP --port=$DB_PORT | grep -o $DB_NAME`
if ! [ "$RESULT" == "$DB_NAME" ] ; then
log "ERROR" "relecov-platform database is not defined yet"
log "ERROR" "Create relecov-platform database on your mysql server and run again the installation script"
exit 1
fi
}
# apache_check: ensures apache/httpd service is running depending on distribution.
apache_check(){
if [[ $linux_distribution == "Ubuntu" ]]; then
if ! pidof apache2 > /dev/null ; then
log "WARN" "Apache Server is down... Trying to restart Apache"
systemctl restart apache2.service
sleep 10
if pidof apache2 > /dev/null ; then
log "INFO" "Apache Server is up"
else
log "ERROR" "Unable to start Apache"
log "ERROR" "Solve the issue with Apache server and run again the installation script"
exit 1
fi
fi
elif [[ $linux_distribution == "CentOs" || $linux_distribution == "RedHatEnterprise" ]]; then
if ! pidof httpd > /dev/null ; then
log "WARN" "Apache Server is down... Trying to restart Apache"
systemctl restart httpd
sleep 10
if pidof httpd > /dev/null ; then
log "INFO" "Apache Server is up"
else
log "ERROR" "Unable to start Apache"
log "ERROR" "Solve the issue with Apache server and run again the installation script"
exit 1
fi
fi
fi
}
# python_check: confirm required Python version is available in PYTHON_BIN_PATH.
python_check(){
python_version=$(su -c $PYTHON_BIN_PATH --version $user 2>&1)
if [[ $python_version == "" ]]; then
log "ERROR" "Python3 is not found in your system"
log "ERROR" "Solve the issue with Python and run again the installation script"
exit 1
fi
p_version=$(echo $python_version | cut -d"." -f2)
if (( $p_version < 7 )); then
log "ERROR" "Application requires at least version 3.7.x of Python3"
log "ERROR" "Solve the issue with python and run again the installation script"
exit 1
fi
}
# root_check: enforce running privileged sections as root.
root_check(){
if [[ $EUID -ne 0 ]]; then
log "ERROR" "Exiting installation. This script must be run as root"
exit 1
fi
}
# update_settings_and_urls: rewrite Django settings and urls with deployment values.
update_settings_and_urls(){
log "INFO" "Updating settings.py and urls.py with deployment values"
local project_dir="$INSTALL_PATH/$PROJECT_NAME"
grep ^SECRET "$project_dir/settings.py" > ~/.secret
cp conf/template_settings.py "$project_dir/settings.py"
cp conf/urls.py "$project_dir"
if [ -f conf/routing.py ]; then
cp conf/routing.py "$project_dir"
fi
sed -i "/^SECRET/c\\$(cat ~/.secret)" "$project_dir/settings.py"
sed -i "s/djangouser/${DB_USER}/g" "$project_dir/settings.py"
sed -i "s/djangopass/${DB_PASS}/g" "$project_dir/settings.py"
sed -i "s/djangohost/${DB_SERVER_IP}/g" "$project_dir/settings.py"
sed -i "s/djangoport/${DB_PORT}/g" "$project_dir/settings.py"
sed -i "s/djangodbname/${DB_NAME}/g" "$project_dir/settings.py"
sed -i "s/emailhostserver/${EMAIL_HOST_SERVER}/g" "$project_dir/settings.py"
sed -i "s/emailport/${EMAIL_PORT}/g" "$project_dir/settings.py"
sed -i "s/emailhostuser/${EMAIL_HOST_USER}/g" "$project_dir/settings.py"
sed -i "s/emailhostpassword/${EMAIL_HOST_PASSWORD}/g" "$project_dir/settings.py"
sed -i "s/emailhosttls/${EMAIL_USE_TLS}/g" "$project_dir/settings.py"
sed -i "s/localserverip/${LOCAL_SERVER_IP}/g" "$project_dir/settings.py"
sed -i "s/localhost/${DNS_URL}/g" "$project_dir/settings.py"
}
# restore_git_ref: reset repository to branch/tag/commit active before script ran.
restore_git_ref() {
echo "Restoring to initial git reference: $initial_git_ref"
git checkout "$initial_git_ref" --quiet
}
# load_tables: wrapper to call Django loaddata with optional verbosity.
load_tables() {
# Function parameters
local data_file="${1:-conf/first_install_tables.json}"
local verbose="${2:-false}"
# Check if the file exists
if [[ ! -f "$data_file" ]]; then
echo "Error: The data file '$data_file' does not exist."
return 1
fi
# Conditional message based on verbose mode
if [[ "$verbose" == true ]]; then
echo "Loading pre-filled tables from file: $data_file"
fi
# Load pre-filled tables
python manage.py loaddata "$data_file"
if [[ $? -eq 0 ]]; then
echo "Tables loaded successfully from '$data_file'."
else
echo "Error loading tables from '$data_file'."
return 1
fi
if [[ "$verbose" == true ]]; then
echo "Table loading process completed."
fi
}
# ensure_git_safe_directory: avoid Git "dubious ownership" failures in containerized installs.
ensure_git_safe_directory() {
local repo_dir
repo_dir="$(pwd -P)"
if [ -d "$repo_dir/.git" ] || [ -f "$repo_dir/.git" ]; then
git config --global --add safe.directory "$repo_dir" >/dev/null 2>&1 || true
git config --system --add safe.directory "$repo_dir" >/dev/null 2>&1 || true
fi
}
# Ensure to recover current git branch/tag/SHA on script exit
ensure_git_safe_directory
initial_git_ref=$(git rev-parse --abbrev-ref HEAD || git rev-parse HEAD)
trap restore_git_ref EXIT
#================================================================
#SET TEMINAL COLORS
#================================================================
YELLOW='\033[0;33m'
WHITE='\033[0;37m'
CYAN='\033[0;36m'
BLUE='\033[0;34m'
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
ORANGE='\033[0;33m'
# log_section: print a visually separated header in both console and log.
log_section() {
local message="$1"
log "INFO" "$message"
printf "\n\n%s\n" "${YELLOW}------------------${NC}"
printf "%b\n" "${YELLOW}${message}${NC}"
printf "%s\n\n" "${YELLOW}------------------${NC}"
}
# log_info: convenience helper for blue info messages (console only).
log_info() {
printf "%b\n" "${BLUE}$(_log_compose_entry "INFO" "$1")${NC}"
}
# log_warn: emit warning text in cyan for terminal visibility.
log_warn() {
printf "%b\n" "${CYAN}$(_log_compose_entry "WARN" "$1")${NC}"
}
# log_error: emit error text in red for terminal visibility.
log_error() {
printf "%b\n" "${RED}$(_log_compose_entry "ERROR" "$1")${NC}"
}
# abort_install: log an error and exit with optional status.
abort_install() {
log_error "$1"
exit "${2:-1}"
}
chown_if_root() {
if [ "$EUID" -eq 0 ]; then
chown "$@"
else
log_warn "Skipping chown (requires root): chown $*"
fi
}
detect_apache_group() {
if command -v lsb_release >/dev/null 2>&1; then
linux_distribution=$(lsb_release -i | cut -f 2-)
else
linux_distribution=$(awk -F= '/^ID=/{gsub(/"/,""); print $2}' /etc/os-release)
fi
if [[ $linux_distribution == "Ubuntu" || $linux_distribution == "ubuntu" ]]; then
apache_group="www-data"
else
apache_group="apache"
fi
}
ensure_file_exists() {
local file_path="$1"
local friendly_name="${2:-$1}"
if [ ! -f "$file_path" ]; then
abort_install "Required file '$friendly_name' not found."
fi
}
# load_install_config: source the selected install_settings file.
load_install_config() {
ensure_file_exists "$conf" "$conf"
# shellcheck disable=SC1090
. "$conf"
INSTALL_PATH="${APP_INSTALL_PATH:-$INSTALL_PATH}"
}
# checkout_git_revision: ensure desired git revision exists and check it out safely.
checkout_git_revision() {
if [[ "$git_branch" == "current" ]]; then
printf "${YELLOW}Using copied local working tree without git checkout.${NC}\n"
return 0
fi
if git rev-parse --verify "$git_branch" >/dev/null 2>&1; then
if [[ $git_branch != $initial_git_ref ]]; then
local local_changes
local_changes=$(git status --porcelain)
if [[ -n $local_changes ]]; then
abort_install "Unable to switch to $git_branch. Commit or stash local changes first."
fi
printf "${YELLOW}Switching to revision %s.${NC}\n" "$git_branch"
git checkout "$git_branch" --quiet
else
printf "${YELLOW}Using current revision: '%s'.${NC}\n" "$git_branch"
fi
else
abort_install "Git reference $git_branch is not defined in ${PWD}."
fi
}
# check_requirements: run Python/DB/Apache/root validations before install/upgrade.
check_requirements() {
log_section "Checking main requirements"
python_check
log_info "Valid version of Python"
if [[ "$operation_scope" == "full" || "$operation_scope" == "app" ]]; then
db_check
log_info "Successful check for database"
if [ "$restart_apache" = true ]; then
apache_check
log_info "Successful check for apache"
fi
fi
if [ "$install_type" == "full" ] || [ "$install_type" == "dep" ] || [ "$upgrade_type" == "full" ] || [ "$upgrade_type" == "dep" ]; then
log_warn "Checking requirement of root user when installation is full or dep"
root_check
log_info "Successful checking of root user"
fi
}
check_stage_requirements() {
log_section "Checking requirements for staged app preparation"
python_check
log_info "Valid version of Python"
}
check_bootstrap_requirements() {
log_section "Checking requirements for application bootstrap"
python_check
log_info "Valid version of Python"
db_check
log_info "Successful check for database"
}
# install_system_packages: install InterOp and distro-specific OS packages required by relecov-platform.
install_system_packages() {
if [ "${SKIP_SYSTEM_PACKAGES:-}" = "1" ]; then
echo "Skipping system package installation (SKIP_SYSTEM_PACKAGES=1)"
return
fi
echo "Installing Interop"
if [ -d /opt/interop ]; then
echo "There is already an interop installation"
echo "Skipping Interop installation"
else
cd /opt
echo "Downloading interop software"
wget https://github.com/Illumina/interop/releases/download/v1.1.15/InterOp-1.1.15-Linux-GNU.tar.gz
tar -xf InterOp-1.1.15-Linux-GNU.tar.gz
ln -s InterOp-1.1.15-Linux-GNU interop
rm InterOp-1.1.15-Linux-GNU.tar.gz
echo "Interop is now installed"
cd -
fi
if command -v lsb_release >/dev/null 2>&1; then
linux_distribution=$(lsb_release -i | cut -f 2-)
else
linux_distribution=$(awk -F= '/^ID=/{gsub(/"/,""); print $2}' /etc/os-release)
fi
if [[ $linux_distribution == "Ubuntu" || $linux_distribution == "ubuntu" ]]; then
echo "Software installation for Ubuntu"
apt-get update && apt-get upgrade -y
apt-get install -y \
apt-utils wget \
libmysqlclient-dev \
python3-venv \
libpq-dev \
python3-dev python3-pip python3-wheel \
apache2-dev cifs-utils \
gnuplot
elif [[ $linux_distribution == "CentOS" || $linux_distribution == "RedHatEnterprise" || $linux_distribution == "centos" || $linux_distribution == "rhel" || $linux_distribution == "fedora" ]]; then
echo "Software installation for Centos/RedHat"
yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel openssl-devel \
wget httpd-devel mysql-libs sqlite sqlite-devel \
mariadb-devel libffi-devel \
gnuplot cifs-utils
fi
}
# run_django_deploy: execute makemigrations/migrate and optional fixture/superuser steps.
run_django_deploy() {
local mode="${1:-install}"
if [ "$run_script_before" = true ]; then
for val in "${migration_script_before[@]}"; do
if [[ $val = *","* ]]; then
parameters=(${val//,/ })
echo "Running pre-migration script: ${parameters[0]}"
./manage.py runscript ${parameters[0]} --script-args ${parameters[1]}
echo "Done pre-migration script: ${parameters[0]}"
else
echo "Running pre-migration script: $val"
./manage.py runscript $val
echo "Done pre-migration script: $val"
fi
done
fi
if [ "$mode" = "upgrade" ]; then
echo "Applying migrations in fake-initial mode"
if ! python manage.py migrate --noinput --fake-initial; then
abort_install "migrate --fake-initial failed"
fi
# Second pass ensures non-initial migrations are applied after fake-initial.
echo "Applying migrations"
if ! python manage.py migrate --noinput; then
abort_install "migrate failed after fake-initial"
fi
else
echo "Applying migrations"
if ! python manage.py migrate --noinput; then
abort_install "migrate failed"
fi
fi
if [ "$tables" = true ]; then
echo "Loading pre-filled tables..."
load_tables "$prefilled_tables" true
echo "Done loading pre-filled tables..."
fi
if [ "$run_script" = true ]; then
for val in "${migration_script[@]}"; do
if [[ $val = *","* ]]; then
parameters=(${val//,/ })
echo "Running post-migration script: ${parameters[0]}"
./manage.py runscript ${parameters[0]} --script-args ${parameters[1]}
echo "Done post-migration script: ${parameters[0]}"
else
echo "Running post-migration script: $val"
./manage.py runscript $val
echo "Done post-migration script: $val"
fi
done
fi
if [ "$mode" = "install" ]; then
echo "Creating super user "
python manage.py createsuperuser --username admin
fi
}
refresh_static_files() {
echo "Deleting static files..."
if [ -d "$INSTALL_PATH/static" ]; then
if command -v mountpoint >/dev/null 2>&1 && mountpoint -q "$INSTALL_PATH/static"; then
echo "Static directory is a mount point. Skipping delete."
else
rm -rf "$INSTALL_PATH/static" || echo "Skipping static removal (busy)."
fi
fi
echo "Running collect statics..."
python manage.py collectstatic --noinput
echo "Done collect statics"
}
# sync_requirements_file: copy repository requirements into the target installation path.
sync_requirements_file() {
mkdir -p $INSTALL_PATH/conf
rsync -rlv conf/requirements.txt $INSTALL_PATH/conf/requirements.txt
}
# setup_virtualenv: create or refresh the Python virtualenv depending on mode.
setup_virtualenv() {
local mode="$1"
cd $INSTALL_PATH
if [ "$mode" = "install" ]; then
if [ -d virtualenv ]; then
echo "There already is a virtualenv for relecov-platform in $INSTALL_PATH."
read -p "Do you want to remove current virtualenv and reinstall? (Y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]] ; then
rm -rf $INSTALL_PATH/virtualenv
bash -c "$PYTHON_BIN_PATH -m venv virtualenv"
else
echo "virtualenv already defined. Skipping."
fi
else
bash -c "$PYTHON_BIN_PATH -m venv virtualenv"
fi
else
if [ -d virtualenv ]; then
read -p "Do you want to remove current virtualenv and reinstall? (Y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]] ; then
rm -rf $INSTALL_PATH/virtualenv
bash -c "$PYTHON_BIN_PATH -m venv virtualenv"
fi
else
read -p "There is no virtualenv. Do you want to create a new one? (Y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]] ; then
bash -c "$PYTHON_BIN_PATH -m venv virtualenv"
else
echo "Exiting..."
exit 0
fi
fi
fi
cd -
}
# prepare_documents_structure: ensure document directories and templates exist with correct permissions.
prepare_documents_structure() {
echo "Created documents structure"
mkdir -p $INSTALL_PATH/documents/schemas
chown_if_root -R "$user:$apache_group" "$INSTALL_PATH/documents"
chmod 775 $INSTALL_PATH/documents
}
# install_python_requirements: activate the venv and install required Python packages.
install_python_requirements() {
cd $INSTALL_PATH
echo "activate the virtualenv"
source virtualenv/bin/activate
echo "Installing required python packages"
python -m pip install --upgrade pip
python -m pip install wheel
python -m pip install -r conf/requirements.txt
cd -
}
ensure_virtualenv_ready() {
if [ ! -d "$INSTALL_PATH/virtualenv" ]; then
log_warn "Virtualenv missing. INSTALL_PATH=$INSTALL_PATH"
ls -la "$INSTALL_PATH" || true
abort_install "Virtualenv not found at $INSTALL_PATH/virtualenv. Run --install dep first."
fi
}
# restart_apache_service: restart Apache/HTTPD unless running inside Docker or explicitly skipped.
restart_apache_service() {
if command -v lsb_release >/dev/null 2>&1; then
linux_distribution=$(lsb_release -i | cut -f 2-)
else
linux_distribution=$(awk -F= '/^ID=/{gsub(/"/,""); print $2}' /etc/os-release)
fi
if [[ $linux_distribution == "Ubuntu" ]]; then
apache_daemon="apache2"
else
apache_daemon="httpd"
fi
if ! systemctl restart $apache_daemon; then
echo -e "${ORANGE}Apache server restart failed. trying with sudo${NC}"
sudo systemctl restart $apache_daemon
fi
}
# run_dependency_stage: execute the dependency portion (system packages + venv + pip) for install or upgrade.
run_dependency_stage() {
local mode="$1"
if [ "$mode" = "install" ]; then
log_section "Preparing dependency environment for installation"
if [ -d $INSTALL_PATH ]; then
echo "There already is an installation of relecov-platform in $INSTALL_PATH."
read -p "Do you want to remove current installation and reinstall? (Y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]] ; then
echo "Exiting without running relecov-platform installation"
exit 1
else
rm -rf $INSTALL_PATH
fi
fi
install_system_packages
mkdir -p $INSTALL_PATH
detect_apache_group
chown_if_root -R "$user:$apache_group" "$INSTALL_PATH"
chmod 775 $INSTALL_PATH
else
log_section "Preparing dependency environment for upgrade"
if [ ! -d $INSTALL_PATH ]; then
abort_install "Unable to start the upgrade. Folder $INSTALL_PATH does not exist."
fi
install_system_packages
fi
sync_requirements_file
setup_virtualenv "$mode"
install_python_requirements
}
# upgrade_application_files: sync code/config and run upgrade-specific tasks (renames, migrations).
upgrade_application_files() {
stage_upgrade_application_files
bootstrap_application_runtime "upgrade"
log_section "Successfuly upgrade of relecov-platform version: ${APP_VERSION}"
}
# stage_upgrade_application_files: sync code/config into INSTALL_PATH without DB work.
stage_upgrade_application_files() {
if [ ! -d $INSTALL_PATH ]; then
abort_install "Unable to start the upgrade. Folder $INSTALL_PATH does not exist."
fi
log_section "Starting relecov-platform Upgrade version: ${APP_VERSION}"
detect_apache_group
echo "Copying files to installation folder"
rsync -rlv conf/ $INSTALL_PATH/conf/
rsync -rlv --fuzzy --delay-updates --delete-delay \
--exclude "logs" --exclude "documents" --exclude "__pycache__" \
README.md LICENSE test conf $REQUIRED_MODULES $INSTALL_PATH
cd $INSTALL_PATH
ensure_virtualenv_ready
echo "activate the virtualenv"
source virtualenv/bin/activate
if [ ! -f "$INSTALL_PATH/manage.py" ]; then
echo "manage.py not found. Creating ${PROJECT_NAME} project"
"$INSTALL_PATH/virtualenv/bin/python" -m django startproject "$PROJECT_NAME" .
fi
echo "Update settings and url file."
update_settings_and_urls
prepare_documents_structure
cd -
}
# install_application_files: deploy Django project files, update settings, and run initial migrations.
install_application_files() {
stage_install_application_files
bootstrap_application_runtime "install"
log_section "Successfuly relecov-platform Installation version: ${APP_VERSION}"
echo "Installation completed"
}
# stage_install_application_files: copy app files into INSTALL_PATH without DB work.
stage_install_application_files() {
log_section "Starting relecov-platform install version: ${APP_VERSION}"
user=${SUDO_USER:-$USER}
group=$(groups | cut -d" " -f1)
detect_apache_group
if [ "$install_type" == "full" ] || [ "$install_type" == "app" ]; then
if [ $LOG_TYPE == "symbolic_link" ]; then
if [ -d $LOG_PATH ]; then
if [ -e "$INSTALL_PATH/logs" ]; then
echo "Log target $INSTALL_PATH/logs already exists. Leaving it unchanged."
else
echo "Creating symbolic link to log folder"
ln -s "$LOG_PATH" "$INSTALL_PATH/logs"
chmod 775 "$LOG_PATH"
fi
else
echo "Log folder path: $LOG_PATH does not exist. Fix it in the install_settings.txt and run again."
exit 1
fi
else
if [ ! -d $INSTALL_PATH/logs ]; then
mkdir -p $INSTALL_PATH/logs
chown_if_root "$user:$apache_group" "$INSTALL_PATH/logs"
chmod 775 $INSTALL_PATH/logs
else
echo "Log folder path: $INSTALL_PATH/logs already exist."
fi
fi
rsync -rlv README.md LICENSE test conf $REQUIRED_MODULES $INSTALL_PATH
cd $INSTALL_PATH
prepare_documents_structure
ensure_virtualenv_ready
echo "activate the virtualenv"
source virtualenv/bin/activate
echo "Creating ${PROJECT_NAME} project"
"$INSTALL_PATH/virtualenv/bin/python" -m django startproject "$PROJECT_NAME" .
update_settings_and_urls
cd -
fi
}
# bootstrap_application_runtime: run DB/bootstrap tasks against an already staged INSTALL_PATH.
bootstrap_application_runtime() {
local mode="$1"
if [ ! -d "$INSTALL_PATH" ]; then
abort_install "Unable to bootstrap application. Folder $INSTALL_PATH does not exist."
fi
cd $INSTALL_PATH
ensure_virtualenv_ready
echo "activate the virtualenv"
source virtualenv/bin/activate
if [ ! -f "$INSTALL_PATH/manage.py" ]; then
abort_install "manage.py not found at $INSTALL_PATH/manage.py. Stage application files first."
fi
run_django_deploy "$mode"
refresh_static_files
cd -
}
# translate long options to short
reset=true
for arg in "$@"
do
if [ -n "$reset" ]; then
unset reset
set -- # this resets the "$@" array so we can rebuild it
fi
case "$arg" in
# OPTIONAL
--install) set -- "$@" -i ;;
--upgrade) set -- "$@" -u ;;
--stage) set -- "$@" -j ;;
--bootstrap) set -- "$@" -l ;;
--script) set -- "$@" -s ;;
--script_before) set -- "$@" -p ;;
--script_after) set -- "$@" -o ;;
--tables) set -- "$@" -t ;;
--skip_tables) set -- "$@" -b ;;
--git_revision) set -- "$@" -g ;;
--conf) set -- "$@" -c ;;
--ren_app) set -- "$@" -r ;;
--docker) set -- "$@" -k ;;
--skip_apache_restart) set -- "$@" -a ;;
# ADITIONAL
--help) set -- "$@" -h ;;
--version) set -- "$@" -v ;;
# PASSING VALUE IN PARAMETER
*) set -- "$@" "$arg" ;;
esac
done
# SETTING DEFAULT VALUES
ren_app=false
tables=false
git_branch=$initial_git_ref
conf="./install_settings.txt"
install=true
install_type="full"
upgrade=false
upgrade_type="full"
workflow="standard"
workflow_mode=""
docker=false
prefilled_tables="conf/first_install_tables.json"
restart_apache=true
run_script=false
run_script_before=false
migration_script=()
migration_script_before=()
skip_tables=false
# PARSE VARIABLE ARGUMENTS WITH getops
options=":c:s:i:u:j:l:r:g:tdbkvhao:p:"
while getopts $options opt; do
case $opt in
i )
workflow="standard"
install=true
upgrade=false
if [[ "$OPTARG" == "full" || "$OPTARG" == "dep" || "$OPTARG" == "app" ]]; then
install_type=$OPTARG
upgrade_type=$OPTARG
else
echo "Install is not set to one valid option. Use: --install full/app/dep"
exit 1
fi
;;
u )
workflow="standard"
install=false
upgrade=true
if [[ "$OPTARG" == "full" || "$OPTARG" == "dep" || "$OPTARG" == "app" ]]; then
upgrade_type=$OPTARG
install_type=$OPTARG
else
echo "Upgrade is not set to one valid option. Use: --upgrade full/app/dep"
exit 1
fi
;;
j )
workflow="stage"
workflow_mode=$OPTARG
if [[ "$workflow_mode" != "install" && "$workflow_mode" != "upgrade" ]]; then
echo "Stage is not set to one valid option. Use: --stage install/upgrade"
exit 1
fi
;;
l )
workflow="bootstrap"
workflow_mode=$OPTARG
if [[ "$workflow_mode" != "install" && "$workflow_mode" != "upgrade" ]]; then
echo "Bootstrap is not set to one valid option. Use: --bootstrap install/upgrade"
exit 1
fi
;;
s )
run_script=true
migration_script+=("$OPTARG")
;;
p )
run_script_before=true
migration_script_before+=("$OPTARG")
;;
o )
run_script=true
migration_script+=("$OPTARG")
;;
t )
tables=true
;;
b )
tables=false
skip_tables=true
;;
r )
ren_app=true
;;
g )
git_branch=$OPTARG
;;
c )
conf=$OPTARG
;;
k )
docker=true
restart_apache=false
;;
a )
restart_apache=false
;;
h )
usage
exit 1
;;
v )
echo $APP_VERSION
exit 1
;;
\?)
echo "Invalid Option: -$OPTARG" 1>&2
usage
exit 1
;;
: )
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
* )
echo "Unimplemented option: -$OPTARG" >&2;
exit 1
;;
esac
done
shift $((OPTIND-1))
operation="install"
operation_scope="$install_type"
if [ $upgrade == true ]; then
operation="upgrade"
operation_scope="$upgrade_type"
fi
if [ "$workflow" != "standard" ]; then
operation="$workflow_mode"
operation_scope="app"
fi
# Default to loading initial tables on installs unless explicitly skipped.
if [ "$operation" = "install" ] && [ "$skip_tables" = false ] && [ "$tables" = false ]; then
tables=true
fi
load_install_config
PROJECT_NAME="${PROJECT_NAME:-relecov_platform}"
checkout_git_revision
user=${SUDO_USER:-$USER}
if [ "$workflow" = "stage" ]; then
check_stage_requirements
if [ "$workflow_mode" = "install" ]; then
stage_install_application_files
else
stage_upgrade_application_files
fi
exit 0
fi
if [ "$workflow" = "bootstrap" ]; then
check_bootstrap_requirements
bootstrap_application_runtime "$workflow_mode"
exit 0
fi
check_requirements
if [[ "$operation_scope" == "full" || "$operation_scope" == "dep" ]]; then
run_dependency_stage "$operation"
if [ "$operation_scope" = "dep" ]; then
log_info "Dependency stage completed."
exit 0
fi
fi
if [[ "$operation_scope" == "full" || "$operation_scope" == "app" ]]; then
if [ "$operation" = "install" ]; then
install_application_files
else
upgrade_application_files
fi
if [ $restart_apache == true ]; then
restart_apache_service
fi
exit 0
fi
printf "\n\n%s"
printf "${RED}------------------${NC}\n"
printf "%s"
printf "${RED}Invalid installation parameters${NC}\n"
printf "%s"
printf "${RED}------------------${NC}\n\n"
echo "See the usage examples"
usage
exit 1