forked from tmiland/invidious-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invidious_installer.sh
executable file
·1170 lines (1044 loc) · 39.1 KB
/
invidious_installer.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
#!/usr/bin/env bash
ls
#shellcheck disable=2059,1091,2166,2015,2129,2221,2222,2140,2046,2034
#shellcheck source=./src/slib.sh
## Author: Tommy Miland (@tmiland) - Copyright (c) 2022
######################################################################
#### Invidious Installer.sh ####
#### Automatic install script for Invidious ####
#### Script to install Invidious ####
#### Maintained by @tmiland ####
######################################################################
## 13
VERSION='2.0.1' # Must stay on line 14 for updater to fetch the numbers
## 15
#------------------------------------------------------------------------------#
#
# MIT License
#
# Copyright (c) 2022 Tommy Miland
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#------------------------------------------------------------------------------#
## Uncomment for debugging purpose
#set -o errexit
#set -o pipefail
#set -o nounset
#set -o xtrace
#timestamp
# time_stamp=$(date)
# Detect absolute and full path as well as filename of this script
cd "$(dirname "$0")" || exit
CURRDIR=$(pwd)
#SCRIPT_FILENAME=$(basename "$0")
cd - > /dev/null || exit
sfp=$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || greadlink -f "${BASH_SOURCE[0]}" 2>/dev/null)
[[ -z "$sfp" ]] && sfp=${BASH_SOURCE[0]}
#SCRIPT_DIR=$(dirname "${sfp}")
ARROW='➜' #WARNING='⚠' # Icons used for printing
REPO_NAME="tmiland/invidious-installer" # Repo name
IN_REPO=${IN_REPO:-iv-org/invidious} # Invidious repo name
USER_NAME=invidious # Set username
USER_DIR="/home/invidious" # Set userdir
REPO_DIR=$USER_DIR/invidious # Set repo Dir
IN_CONFIG=${REPO_DIR}/config/config.yml # Set config file path
SERVICE_NAME=invidious.service # Service name
IN_BRANCH=master # Default branch
DOMAIN=${DOMAIN:-} # Default domain
IP=${IP:-localhost} # Default ip
PORT=${PORT:-3000} # Default port
PSQLDB=${PSQLDB:-invidious} # Default dbname
PSSQLPASS_GEN=$(openssl rand -base64 32 | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1) # Generate db password
PSQLPASS=${PSQLPASS:-$PSSQLPASS_GEN} # Default dbpass (generated)
HTTPS_ONLY=${HTTPS_ONLY:-n} # Default https only
EXTERNAL_PORT=${EXTERNAL_PORT:-} # Default external port
ADMINS=${ADMINS:-} # Default admins
CAPTCHA_KEY=${CAPTCHA_KEY:-} # Default Captcha Key
SWAP_OPTIONS=${SWAP_OPTIONS:-n} # Default Swap option
LOGFILE=$CURRDIR/invidious_installer.log # Logfile
VERBOSE=0 # Console output level; ignore debug level messages.
BANNERS=1 # Enable banners
usage() {
printf "Usage: %s %s [options]\\n" "${CYAN}" $(basename "$0")
echo
echo " If called without arguments, installs Invidious."
echo
printf " ${YELLOW}--help|-h${NORMAL} display this help and exit\\n"
printf " ${YELLOW}--verbose|-v${NORMAL} increase verbosity\\n"
printf " ${YELLOW}--banners|-b${NORMAL} disable banners\\n"
printf " ${YELLOW}--repo|-r${NORMAL} select custom repo. E.G: user/invidious\\n"
echo
}
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
--help | -h)
usage
exit 0
;;
--verbose | -v)
shift
VERBOSE=1
;;
--banners | -b)
shift
BANNERS=0
;;
--repo | -r) # Bash Space-Separated (e.g., --option argument)
IN_REPO="$2" # Source: https://stackoverflow.com/a/14203146
shift # past argument
shift # past value
;;
--uninstall | -u)
shift
mode="uninstall"
;;
-*|--*)
printf "Unrecognized option: $1\\n\\n"
usage
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
# Include functions
if [[ -f ./src/slib.sh ]]; then
. ./src/slib.sh
else
SLIB_URL=https://raw.githubusercontent.com/tmiland/invidious-installer/main/src/slib.sh
if [[ $(command -v 'curl') ]]; then
source <(curl -sSLf $SLIB_URL)
elif [[ $(command -v 'wget') ]]; then
. <(wget -qO - $SLIB_URL)
else
echo -e "${RED}${BALLOT_X} This script requires curl or wget.\nProcess aborted${NORMAL}"
exit 0
fi
fi
LOG_PATH="${LOGFILE}" # Setup slog
RUN_LOG="${LOGFILE}" # Setup run_ok
RUN_ERRORS_FATAL=1 # Exit on any failure during shell stage
# Console output level; ignore debug level messages.
if [[ "$VERBOSE" = "1" ]]; then
LOG_LEVEL_STDOUT="DEBUG"
else
LOG_LEVEL_STDOUT="INFO"
fi
LOG_LEVEL_LOG="DEBUG" # Log file output level; catch literally everything.
# log_fatal calls log_error
log_fatal() {
log_error "$1"
}
fatal() {
echo
log_fatal "Fatal Error Occurred: $1"
printf "${RED}Cannot continue installation.${NORMAL}\\n"
log_fatal "If you are unsure of what went wrong, you may wish to review the log"
log_fatal "in ${LOGFILE}"
exit 1
}
success() {
log_success "$1 Succeeded."
}
read_sleep() {
read -rt "$1" <> <(:) || :
}
# Start with a clean log
if [[ -f "${LOGFILE}" ]]; then
rm "${LOGFILE}"
fi
# Architecture support
ARCH_CHK=$(uname -m)
if [[ ! "${ARCH_CHK}" == 'x86_64' ]]; then
echo -e "${RED}${BALLOT_X}Error: Sorry, your OS ($ARCH_CHK) is not supported.${NORMAL}"
exit 1;
fi
## Distro support
shopt -s nocasematch
if [[ -f /etc/debian_version ]]; then
DISTRO=$(cat /etc/issue.net)
elif [[ -f /etc/redhat-release ]]; then
DISTRO=$(cat /etc/redhat-release)
elif [[ -f /etc/os-release ]]; then
DISTRO=$( \
cat < /etc/os-release \
| grep "PRETTY_NAME" \
| sed 's/PRETTY_NAME=//g' \
| sed 's/["]//g' \
| awk '{print $1}' \
)
fi
case "$DISTRO" in
Debian*|Ubuntu*|LinuxMint*|PureOS*|Pop*|Devuan*)
PKGCMD="apt-get -o Dpkg::Progress-Fancy="1" install -qq"
LSB=lsb-release
DISTRO_GROUP=Debian
;;
CentOS*)
PKGCMD="yum install -y"
LSB=redhat-lsb
DISTRO_GROUP=RHEL
;;
Fedora*)
PKGCMD="dnf install -y"
LSB=redhat-lsb
DISTRO_GROUP=RHEL
;;
Arch*|Manjaro*)
PKGCMD="yes | LC_ALL=en_US.UTF-8 pacman -S"
LSB=lsb-release
DISTRO_GROUP=Arch
;;
*)
echo -e "${RED}${BALLOT_X} unknown distro: '$DISTRO'${NORMAL}"
exit 1
;;
esac
if ! lsb_release -si 1>/dev/null 2>&1; then
echo ""
echo -e "${RED}${BALLOT_X} Looks like ${LSB} is not installed!${NORMAL}"
echo ""
read -r -p "Do you want to download ${LSB}? [y/n]? " ANSWER
echo ""
case $ANSWER in
[Yy]* )
echo -e "${GREEN}${ARROW} Installing ${LSB} on ${DISTRO}...${NORMAL}"
su -s "$(which bash)" -c "${PKGCMD} ${LSB}" || echo -e "${RED}${BALLOT_X} Error: could not install ${LSB}!${NORMAL}"
echo -e "${GREEN}${CHECK} Done${NORMAL}"
read_sleep 3
#indexit
;;
[Nn]* )
exit 1;
;;
* )
echo "Enter Y, N, please."
;;
esac
fi
SUDO=""
UPDATE=""
#UPGRADE=""
INSTALL=""
UNINSTALL=""
PURGE=""
CLEAN=""
PKGCHK=""
PGSQL_SERVICE=""
SYSTEM_CMD=""
shopt -s nocasematch
## Debian
if [[ "${DISTRO_GROUP}" == "Debian" ]]; then
export DEBIAN_FRONTEND=noninteractive
SUDO="sudo"
UPDATE="apt-get -o Dpkg::Progress-Fancy="1" update -qq"
# UPGRADE="apt-get -o Dpkg::Progress-Fancy="1" upgrade -qq"
INSTALL="apt-get -o Dpkg::Progress-Fancy="1" install -qq"
UNINSTALL="apt-get -o Dpkg::Progress-Fancy="1" remove -qq"
PURGE="apt-get purge -o Dpkg::Progress-Fancy="1" -qq"
CLEAN="apt-get clean && apt-get autoremove -qq"
PKGCHK="dpkg -s"
PRE_INSTALL_PKGS="apt-transport-https git curl sudo gnupg" # Pre-install packages
INSTALL_PKGS="crystal libssl-dev libxml2-dev libyaml-dev libgmp-dev libreadline-dev librsvg2-bin postgresql libsqlite3-dev zlib1g-dev libpcre3-dev libevent-dev" # Install packages
UNINSTALL_PKGS="crystal libssl-dev libxml2-dev libyaml-dev libgmp-dev libreadline-dev librsvg2-bin libsqlite3-dev zlib1g-dev libpcre3-dev libevent-dev" #Uninstall packages
pgsql_service="postgresql" # postgresql service
SYSTEM_CMD="systemctl" # System cmd
pgsql_config_folder=$(find "/etc/postgresql/" -maxdepth 1 -type d -name "*" | sort -V | tail -1) # Postgresql config folder
## CentOS
elif [[ $(lsb_release -si) == "CentOS" ]]; then
SUDO="sudo"
UPDATE="yum update -q"
# UPGRADE="yum upgrade -q"
INSTALL="yum install -y -q"
UNINSTALL="yum remove -y -q"
PURGE="yum purge -y -q"
CLEAN="yum clean all -y -q"
PKGCHK="rpm --quiet --query"
PRE_INSTALL_PKGS="epel-release git curl sudo dnf-plugins-core" # Pre-install packages
INSTALL_PKGS="crystal openssl-devel libxml2-devel libyaml-devel gmp-devel readline-devel librsvg2-tools sqlite-devel postgresql postgresql-server zlib-devel gcc libevent-devel" # Install packages
UNINSTALL_PKGS="crystal openssl-devel libxml2-devel libyaml-devel gmp-devel readline-devel librsvg2-tools sqlite-devel zlib-devel gcc libevent-devel" #Uninstall packages
PGSQL_SERVICE="postgresql" # PostgreSQL Service
SYSTEM_CMD="systemctl" # System cmd
pgsql_config_folder=$(find "/etc/postgresql/" -maxdepth 1 -type d -name "*" | sort -V | tail -1) # Postgresql config folder
## Fedora
elif [[ $(lsb_release -si) == "Fedora" ]]; then
SUDO="sudo"
UPDATE="dnf update -q"
# UPGRADE="dnf upgrade -q"
INSTALL="dnf install -y -q"
UNINSTALL="dnf remove -y -q"
PURGE="dnf purge -y -q"
CLEAN="dnf clean all -y -q"
PKGCHK="rpm --quiet --query"
PRE_INSTALL_PKGS="git curl sudo" # Pre-install packages
INSTALL_PKGS="crystal openssl-devel libxml2-devel libyaml-devel gmp-devel readline-devel librsvg2-tools sqlite-devel postgresql postgresql-server zlib-devel gcc libevent-devel" # Install packages
UNINSTALL_PKGS="crystal openssl-devel libxml2-devel libyaml-devel gmp-devel readline-devel librsvg2-tools sqlite-devel zlib-devel gcc libevent-devel" #Uninstall packages
PGSQL_SERVICE="postgresql" # PostgreSQL Service
SYSTEM_CMD="systemctl" # System cmd
pgsql_config_folder=$(find "/etc/postgresql/" -maxdepth 1 -type d -name "*" | sort -V | tail -1) # Postgresql config folder
## Arch
elif [[ "${DISTRO_GROUP}" == "Arch" ]]; then
SUDO="sudo"
UPDATE="pacman -Syu"
INSTALL="pacman -S --noconfirm --needed"
UNINSTALL="pacman -R"
PURGE="pacman -Rs"
CLEAN="pacman -Sc"
PKGCHK="pacman -Qs"
PRE_INSTALL_PKGS="git curl sudo" # Pre-install packages
INSTALL_PKGS="base-devel shards crystal librsvg postgresql" # Install packages
UNINSTALL_PKGS="base-devel shards crystal librsvg" #Uninstall packages
PGSQL_SERVICE="postgresql" # PostgreSQL Service
SYSTEM_CMD="systemctl" # System cmd
pgsql_config_folder="/var/lib/postgres/data" # Postgresql config folder
## Other distros
else
echo -e "${RED}${BALLOT_X} Error: Sorry, your OS is not supported.${NORMAL}"
exit 1;
fi
# Check if systemd is installed on Devuan
if [[ $(lsb_release -si) == "Devuan" ]]; then
if ( ! $SYSTEM_CMD 2>/dev/null); then
echo -e "${RED}${BALLOT_X} Error: Sorry, you need systemd to run this script.${NORMAL}"
exit 1;
fi
fi
# Make sure that the script runs with root permissions
chk_permissions() {
if [[ $EUID -ne 0 ]]; then
printf "${RED}Sorry, you need to run this as root${NORMAL}\n"
exit 1
fi
}
ADD_SWAP_URL=https://raw.githubusercontent.com/tmiland/swap-add/master/swap-add.sh
add_swap() {
if [[ $(command -v 'curl') ]]; then
#shellcheck disable=1090
source <(curl -sSLf $ADD_SWAP_URL)
elif [[ $(command -v 'wget') ]]; then
#shellcheck disable=1090
. <(wget -qO - $ADD_SWAP_URL)
else
echo -e "${RED}${BALLOT_X} This script requires curl or wget.\nProcess aborted${NORMAL}"
exit 0
fi
read_sleep 3
}
## get total free memory size in megabytes(MB)
free=$(free -mt | grep Total | awk '{print $4}')
if [[ "$free" -le 2048 ]]; then
echo -e "${YELLOW}Advice: Free memory: $free MB is less than recommended to build Invidious${NORMAL}"
case $SWAP_OPTIONS in
[Yy]* )
add_swap
;;
[Nn]* )
return 0
;;
esac
fi
# Show service status - @FalconStats
show_status() {
declare -a services=(
"invidious"
"postgresql"
)
#fi
declare -a serviceName=(
"Invidious"
"PostgreSQL"
)
declare -a serviceStatus=()
for service in "${services[@]}"
do
serviceStatus+=("$($SYSTEM_CMD is-active "$service")")
done
echo ""
echo "Services running:"
for i in "${!serviceStatus[@]}"
do
if [[ "${serviceStatus[$i]}" == "active" ]]; then
line+="${GREEN}${NORMAL}${serviceName[$i]}: ${GREEN}● ${serviceStatus[$i]}${NORMAL} "
else
line+="${serviceName[$i]}: ${RED}▲ ${serviceStatus[$i]}${NORMAL} "
fi
done
echo -e "$line"
}
if ( $SYSTEM_CMD -q is-active ${SERVICE_NAME}); then
SHOW_STATUS=$(show_status)
fi
# BANNERS
# Header
header() {
echo -e "${GREEN}\n"
echo ' ╔═══════════════════════════════════════════════════════════════════╗'
echo ' ║ '"${SCRIPT_NAME}"' ║'
echo ' ║ Automatic install script for Invidious ║'
echo ' ║ Maintained by @tmiland ║'
echo ' ║ version: '${VERSION}' ║'
echo ' ╚═══════════════════════════════════════════════════════════════════╝'
echo -e "${NORMAL}"
}
# Preinstall banner
show_preinstall_banner() {
clear
header
echo "Thank you for using the ${SCRIPT_NAME} script."
echo ""
echo ""
echo ""
echo -e "Documentation for this script is available here: ${YELLOW}\n ${ARROW} https://github.com/${REPO_NAME}${NORMAL}\n"
}
# Install banner
show_install_banner() {
#clear
header
echo ""
echo ""
echo ""
echo "Thank you for using the ${SCRIPT_NAME} script."
echo ""
echo ""
echo ""
echo -e "${GREEN}${CHECK} Invidious install done.${NORMAL} Now visit http://${IP}:${PORT}"
echo ""
echo ""
echo ""
echo ""
echo -e "Documentation for this script is available here: ${YELLOW}\n ${ARROW} https://github.com/${REPO_NAME}${NORMAL}\n"
}
# Banner
show_banner() {
#clear
header
echo "Welcome to the ${SCRIPT_NAME} script."
echo ""
echo ""
echo "${SHOW_STATUS} "
echo ""
echo ""
echo ""
echo -e "Documentation for this script is available here: ${YELLOW}\n ${ARROW} https://github.com/${REPO_NAME}${NORMAL}\n"
}
# Exit Script
exit_script() {
#header
echo -e "${GREEN}"
echo ' ____ _ ___ '
echo ' / _/___ _ __(_)___/ (_)___ __ _______ '
echo ' / // __ \ | / / / __ / / __ \/ / / / ___/ '
echo ' _/ // / / / |/ / / /_/ / / /_/ / /_/ (__ ) '
echo ' /___/_/_/_/|___/_/\__,_/_/\____/\__,_/____/ __ '
echo ' / _/___ _____/ /_____ _/ / /__ __________/ /_ '
echo ' / // __ \/ ___/ __/ __ `/ / / _ \/ ___/ ___/ __ \ '
echo ' _/ // / / (__ ) /_/ /_/ / / / __/ / (__ ) / / / '
echo ' /___/_/ /_/____/\__/\__,_/_/_/\___/_(_)/____/_/ /_/ '
echo -e ' ' "${NORMAL}"
echo -e "
This script runs on coffee ☕
${GREEN}${CHECK}${NORMAL} ${BBLUE}Paypal${NORMAL} ${ARROW} ${YELLOW}https://paypal.me/milanddata${NORMAL}
${GREEN}${CHECK}${NORMAL} ${BBLUE}BTC${NORMAL} ${ARROW} ${YELLOW}33mjmoPxqfXnWNsvy8gvMZrrcG3gEa3YDM${NORMAL}
"
echo -e "Documentation for this script is available here: ${YELLOW}\n${ARROW} https://github.com/${REPO_NAME}${NORMAL}\n"
echo -e "${YELLOW}${ARROW} Goodbye.${NORMAL} ☺"
echo ""
}
# Check Git repo
chk_git_repo() {
# Check if the folder is a git repo
if [[ -d "${REPO_DIR}/.git" ]]; then
echo ""
log_fatal "Looks like Invidious is already installed!"
echo ""
log_warning "If you want to reinstall, please remove Invidious first!"
echo ""
read_sleep 3
#indexit
exit 0
fi
}
# Set permissions
set_permissions() {
${SUDO} chown -R $USER_NAME:$USER_NAME $USER_DIR 1>/dev/null 2>&1
${SUDO} chmod -R 755 $USER_DIR 1>/dev/null 2>&1
}
# Update config
update_config() {
# Update config.yml with new info from user input
BAKPATH="/home/backup/$USER_NAME/config"
OLDPASS="password: kemal"
NEWPASS="password: $PSQLPASS" # Lets change the default password
OLDDBNAME="dbname: invidious"
NEWDBNAME="dbname: $PSQLDB" # Lets change the default database name
OLDDOMAIN="domain:"
NEWDOMAIN="domain: $DOMAIN" # Lets change the default domain
OLDHTTPS="https_only: false"
NEWHTTPS="https_only: $HTTPS_ONLY" # Lets change https_only value
OLDEXTERNAL="external_port:"
NEWEXTERNAL="external_port: $EXTERNAL_PORT" # Lets change external_port
DPATH="${IN_CONFIG}"
BPATH="$BAKPATH"
TFILE="/tmp/config.yml"
[[ ! -d $BPATH ]] && mkdir -p $BPATH || :
for f in $DPATH
do
if [[ -f $f && -r $f ]]; then
/bin/cp -f $f $BPATH
sed -i "11i\external_port:" "$f" > $TFILE # Add external_port: to config on line 13
sed -i "12i\check_tables: true" "$f" > $TFILE
sed -i "13i\port: $PORT" "$f" > $TFILE
sed -i "14i\host_binding: $IP" "$f" > $TFILE
sed -i "15i\admins: \n- $ADMINS" "$f" > $TFILE
sed -i "17i\captcha_key: $CAPTCHA_KEY" "$f" > $TFILE
sed -i "18i\captcha_api_url: https://api.anti-captcha.com" "$f" > $TFILE
sed "s/$OLDPASS/$NEWPASS/g; s/$OLDDBNAME/$NEWDBNAME/g; s/$OLDDOMAIN/$NEWDOMAIN/g; s/$OLDHTTPS/$NEWHTTPS/g; s/$OLDEXTERNAL/$NEWEXTERNAL/g;" "$f" > $TFILE &&
mv $TFILE "$f" >>"${RUN_LOG}" 2>&1
else
log_fatal "Error: Cannot read $f"
fi
done
if [[ -e "${TFILE}" ]]; then
/bin/rm "${TFILE}" >>"${RUN_LOG}" 2>&1
fi
# Done updating config.yml with new info!
# Source: https://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/
}
# Systemd install
systemd_install() {
# Setup Systemd Service
shopt -s nocasematch
if [[ "${DISTRO_GROUP}" == "RHEL" ]]; then
cp ${REPO_DIR}/${SERVICE_NAME} /etc/systemd/system/${SERVICE_NAME} >>"${RUN_LOG}" 2>&1
else
cp ${REPO_DIR}/${SERVICE_NAME} /lib/systemd/system/${SERVICE_NAME} >>"${RUN_LOG}" 2>&1
fi
${SUDO} $SYSTEM_CMD enable ${SERVICE_NAME} >>"${RUN_LOG}" 2>&1 # Enable invidious start at boot
${SUDO} $SYSTEM_CMD daemon-reload >>"${RUN_LOG}" 2>&1 # Reload Systemd
${SUDO} $SYSTEM_CMD start ${SERVICE_NAME} >>"${RUN_LOG}" 2>&1 # Restart Invidious
if ( $SYSTEM_CMD -q is-active ${SERVICE_NAME})
then
${SUDO} $SYSTEM_CMD status ${SERVICE_NAME} --no-pager >>"${RUN_LOG}" 2>&1
read_sleep 5
else
${SUDO} journalctl -u ${SERVICE_NAME} >>"${RUN_LOG}" 2>&1
read_sleep 5
fi
}
logrotate_install() {
if [[ -d /etc/logrotate.d ]]; then
echo "Adding logrotate configuration..."
echo "/home/invidious/invidious/invidious.log {
rotate 4
weekly
notifempty
missingok
compress
minsize 1048576
}" | ${SUDO} tee /etc/logrotate.d/invidious.logrotate >>"${RUN_LOG}" 2>&1
chmod 0644 /etc/logrotate.d/invidious.logrotate >>"${RUN_LOG}" 2>&1
fi
}
# Get Crystal
get_crystal() {
shopt -s nocasematch
if [[ "${DISTRO_GROUP}" == "Debian" ]]; then
if [[ ! -e /etc/apt/sources.list.d/crystal.list ]]; then
curl -fsSL https://crystal-lang.org/install.sh | ${SUDO} bash >>"${RUN_LOG}" 2>&1
fi
elif [[ "${DISTRO_GROUP}" == "RHEL" ]]; then
if [[ ! -e /etc/yum.repos.d/crystal.repo ]]; then
curl -fsSL https://crystal-lang.org/install.sh | ${SUDO} bash >>"${RUN_LOG}" 2>&1
fi
elif [[ $(lsb_release -si) == "Darwin" ]]; then
exit 1;
elif [[ "${DISTRO_GROUP}" == "Arch" ]]; then
echo "Arch/Manjaro Linux... Skipping manual crystal install" >>"${RUN_LOG}" 2>&1
else
log_fatal "Error: Sorry, your OS is not supported."
exit 1;
fi
}
# Create new config.yml
create_config() {
if [[ ! -f "$IN_CONFIG" ]]; then
echo "channel_threads: 1
feed_threads: 1
db:
user: kemal
password: kemal
host: localhost
port: 5432
dbname: invidious
full_refresh: false
https_only: false
domain:" | ${SUDO} tee ${IN_CONFIG} >>"${RUN_LOG}" 2>&1
fi
}
# Backup config file
backupConfig() {
# Set config backup path
ConfigBakPath="/home/backup/$USER_NAME/config"
# If directory is not created
[ ! -d $ConfigBakPath ] && mkdir -p $ConfigBakPath || :
configBackup=${IN_CONFIG}
backupConfigFile=$(date +%F).config.yml
/bin/cp -f $configBackup $ConfigBakPath/"$backupConfigFile" >>"${RUN_LOG}" 2>&1
}
# Checkout Master branch to branch master (to avoid detached HEAD state)
GetMaster() {
create_config
backupConfig
git checkout origin/${IN_BRANCH} -B ${IN_BRANCH} >>"${RUN_LOG}" 2>&1
}
# Get dbname from config file (used in db maintenance and uninstallation)
get_dbname() {
echo "$(sed -n 's/.*dbname *: *\([^ ]*.*\)/\1/p' "$1")"
}
uninstall_invidious() {
# Set default uninstallation parameters
RM_PostgreSQLDB=${RM_PostgreSQLDB:-y}
RM_RE_PGSQLDB=${RM_RE_PGSQLDB:-n}
RM_PACKAGES=${RM_PACKAGES:-n}
RM_PURGE=${RM_PURGE:-n}
RM_FILES=${RM_FILES:-y}
RM_USER=${RM_USER:-n}
PGSQLDB_BAK_PATH="/home/backup/$USER_NAME" # Set db backup path
RM_PSQLDB=$(get_dbname "${IN_CONFIG}") # Get dbname from config.yml
read -p "Express uninstall ? [y/n]: " EXPRESS_UNINSTALL
if [[ ! $EXPRESS_UNINSTALL = "y" ]]; then
echo ""
read -e -i "$RM_PostgreSQLDB" -p " Remove database for Invidious ? [y/n]: " RM_PostgreSQLDB
if [[ $RM_PostgreSQLDB = "y" ]]; then
echo -e " ${YELLOW}${WARNING} (( A backup will be placed in ${ARROW} $PGSQLDB_BAK_PATH ))${NORMAL}"
echo -e " Your Invidious database name: $RM_PSQLDB"
fi
if [[ $RM_PostgreSQLDB = "y" ]]; then
echo -e " ${YELLOW}${WARNING} (( If yes, only data will be dropped ))${NORMAL}"
read -e -i "$RM_RE_PGSQLDB" -p " Do you intend to reinstall?: " RM_RE_PGSQLDB
fi
read -e -i "$RM_PACKAGES" -p " Remove Packages ? [y/n]: " RM_PACKAGES
if [[ $RM_PACKAGES = "y" ]]; then
read -e -i "$RM_PURGE" -p " Purge Package configuration files ? [y/n]: " RM_PURGE
fi
echo -e " ${YELLOW}${WARNING} (( This option will remove ${ARROW} ${REPO_DIR} ))${NORMAL}"
read -e -i "$RM_FILES" -p " Remove files ? [y/n]: " RM_FILES
if [[ "$RM_FILES" = "y" ]]; then
echo -e " ${RED}${WARNING} (( This option will remove ${ARROW} $USER_DIR ))${NORMAL}"
echo -e " ${YELLOW}${WARNING} (( Not needed for reinstall ))${NORMAL}"
read -e -i "$RM_USER" -p " Remove user ? [y/n]: " RM_USER
fi
echo ""
echo -e "${GREEN}${ARROW} Invidious is ready to be uninstalled${NORMAL}"
echo ""
read -n1 -r -p "press any key to continue or Ctrl+C to cancel..."
echo ""
fi
# Remove PostgreSQL database if user ANSWER is yes
if [[ "$RM_PostgreSQLDB" = 'y' ]]; then
# Stop and disable invidious
${SUDO} $SYSTEM_CMD stop ${SERVICE_NAME}
read_sleep 1
${SUDO} $SYSTEM_CMD restart ${PGSQL_SERVICE}
read_sleep 1
# If directory is not created
if [[ ! -d $PGSQLDB_BAK_PATH ]]; then
echo -e "${YELLOW}${ARROW} Backup Folder Not Found, adding folder${NORMAL}"
${SUDO} mkdir -p $PGSQLDB_BAK_PATH
fi
echo ""
echo -e "${GREEN}${ARROW} Running database backup${NORMAL}"
echo ""
${SUDO} -i -u postgres pg_dump ${RM_PSQLDB} > ${PGSQLDB_BAK_PATH}/${RM_PSQLDB}.sql
read_sleep 2
${SUDO} chown -R 1000:1000 "/home/backup"
if [[ "$RM_RE_PGSQLDB" != 'n' ]]; then
echo ""
echo -e "${RED}${ARROW} Dropping Invidious PostgreSQL data${NORMAL}"
echo ""
${SUDO} -i -u postgres psql -c "DROP OWNED BY kemal CASCADE;"
echo ""
echo -e "${YELLOW}${CHECK} Data dropped and backed up to ${ARROW} ${PGSQLDB_BAK_PATH}/${RM_PSQLDB}.sql ${NORMAL}"
echo ""
fi
if [[ "$RM_RE_PGSQLDB" != 'y' ]]; then
echo ""
echo -e "${RED}${ARROW} Dropping Invidious PostgreSQL database${NORMAL}"
echo ""
${SUDO} -i -u postgres psql -c "DROP DATABASE $RM_PSQLDB"
echo ""
echo -e "${YELLOW}${CHECK} Database dropped and backed up to ${ARROW} ${PGSQLDB_BAK_PATH}/${RM_PSQLDB}.sql ${NORMAL}"
echo ""
echo -e "${RED}${ARROW} Removing user kemal${NORMAL}"
${SUDO} -i -u postgres psql -c "DROP ROLE IF EXISTS kemal;"
fi
fi
# Reload Systemd
${SUDO} $SYSTEM_CMD daemon-reload
# Remove packages installed during installation
if [[ "$RM_PACKAGES" = 'y' ]]; then
echo ""
echo -e "${YELLOW}${ARROW} Removing packages installed during installation."
echo ""
echo -e "Note: PostgreSQL will not be removed due to unwanted complications${NORMAL}"
echo ""
if ${PKGCHK} $UNINSTALL_PKGS >/dev/null 2>&1; then
for i in $UNINSTALL_PKGS; do
echo ""
echo -e "${YELLOW}${ARROW} removing packages.${NORMAL}"
echo ""
${UNINSTALL} $i 2> /dev/null
done
fi
echo ""
echo -e "${GREEN}${CHECK} done.${NORMAL}"
echo ""
fi
# Remove conf files
if [[ "$RM_PURGE" = 'y' ]]; then
# Removing invidious files and modules files
echo ""
echo -e "${YELLOW}${ARROW} Removing invidious files and modules files.${NORMAL}"
echo ""
if [[ "${DISTRO_GROUP}" == "Debian" ]]; then
rm -r \
/lib/systemd/system/${SERVICE_NAME} \
/etc/apt/sources.list.d/crystal.list
elif [[ "${DISTRO_GROUP}" == "RHEL" ]]; then
rm -r \
/usr/lib/systemd/system/${SERVICE_NAME} \
/etc/yum.repos.d/crystal.repo
fi
if ${PKGCHK} $UNINSTALL_PKGS >/dev/null 2>&1; then
for i in $UNINSTALL_PKGS; do
echo ""
echo -e "${YELLOW}${ARROW} purging packages.${NORMAL}"
echo ""
${PURGE} $i 2> /dev/null
done
fi
echo ""
echo -e "${YELLOW}${ARROW} cleaning up.${NORMAL}"
echo ""
${CLEAN}
echo ""
echo -e "${GREEN}${CHECK} done.${NORMAL}"
echo ""
fi
if [[ "$RM_FILES" = 'y' ]]; then
# If directory is present, remove
if [[ -d ${REPO_DIR} ]]; then
echo -e "${YELLOW}${ARROW} Folder Found, removing folder${NORMAL}"
rm -r ${REPO_DIR}
fi
fi
# Remove user and settings
if [[ "$RM_USER" = 'y' ]]; then
# Stop and disable invidious
${SUDO} $SYSTEM_CMD stop ${SERVICE_NAME}; read_sleep 1
${SUDO} $SYSTEM_CMD restart ${PGSQL_SERVICE}; read_sleep 1
${SUDO} $SYSTEM_CMD daemon-reload; read_sleep 1
grep $USER_NAME /etc/passwd >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
echo ""
echo -e "${YELLOW}${ARROW} User $USER_NAME Found, removing user and files${NORMAL}"
echo ""
shopt -s nocasematch
if [[ "${DISTRO_GROUP}" == "Debian" ]]; then
${SUDO} deluser --remove-home $USER_NAME
fi
if [[ "${DISTRO_GROUP}" == "RHEL" ]]; then
/usr/sbin/userdel -r $USER_NAME
fi
fi
fi
if [[ -d /etc/logrotate.d ]]; then
rm /etc/logrotate.d/invidious.logrotate
fi
# We're done !
echo ""
echo -e "${GREEN}${CHECK} Un-installation done.${NORMAL}"
echo ""
read_sleep 3
tput cnorm
exit 0
}
if [[ "$mode" = "uninstall" ]]; then
uninstall_invidious
fi
install_invidious() {
chk_git_repo
if [[ "$BANNERS" = "1" ]]; then
show_preinstall_banner
fi
case $HTTPS_ONLY in
[Yy]* )
HTTPS_ONLY=true
EXTERNAL_PORT=443
;;
[Nn]* )
HTTPS_ONLY=false
EXTERNAL_PORT=
;;
esac
PSQLDB=$(printf '%s\n' "$PSQLDB" | LC_ALL=C tr '[:upper:]' '[:lower:]')
log_info "Started installation log in ${LOGFILE}"
echo
printf "${YELLOW}▣${CYAN}□□□${NORMAL} Phase ${YELLOW}1${NORMAL} of ${GREEN}4${NORMAL}: Setup packages\\n"
log_info "Install options: "
log_info " ${GREEN}${CHECK}${NORMAL} ${YELLOW}branch${NORMAL} : ${BBLUE}$IN_BRANCH${NORMAL}"
log_info " ${GREEN}${CHECK}${NORMAL} ${YELLOW}domain${NORMAL} : ${BBLUE}$DOMAIN${NORMAL}"
log_info " ${GREEN}${CHECK}${NORMAL} ${YELLOW}ip address${NORMAL} : ${BBLUE}$IP${NORMAL}"
log_info " ${GREEN}${CHECK}${NORMAL} ${YELLOW}port${NORMAL} : ${BBLUE}$PORT${NORMAL}"
if [[ -n "$EXTERNAL_PORT" ]]; then
log_info " ${GREEN}${CHECK}${NORMAL} ${YELLOW}external port${NORMAL} : ${BBLUE}$EXTERNAL_PORT${NORMAL}"
fi
log_info " ${GREEN}${CHECK}${NORMAL} ${YELLOW}dbname${NORMAL} : ${BBLUE}$PSQLDB${NORMAL}"
log_info " ${GREEN}${CHECK}${NORMAL} ${YELLOW}dbpass${NORMAL} : ${BBLUE}$PSQLPASS${NORMAL}"
log_info " ${GREEN}${CHECK}${NORMAL} ${YELLOW}https only${NORMAL} : ${BBLUE}$HTTPS_ONLY${NORMAL}"
if [[ -n "$ADMINS" ]]; then
log_info " ${GREEN}${CHECK}${NORMAL} ${YELLOW}admins${NORMAL} : ${BBLUE}$ADMINS${NORMAL}"
fi
if [[ -n "$CAPTCHA_KEY" ]]; then
log_info " ${GREEN}${CHECK}${NORMAL} ${YELLOW}captcha key${NORMAL} : ${BBLUE}$CAPTCHA_KEY${NORMAL}"
fi
echo
# Setup Dependencies
log_debug "Configuring package manager for ${DISTRO_GROUP} .."
if ! ${PKGCHK} "$PRE_INSTALL_PKGS" 1>/dev/null 2>&1; then
log_debug "Updating packages"
run_ok "${UPDATE}" "Updating package repo"
for i in $PRE_INSTALL_PKGS; do
log_debug "Installing pre-install packages $i"
#shellcheck disable=2086
${INSTALL} $i >>"${RUN_LOG}" 2>&1
done
fi
log_debug "Installing Crystal packages"
run_ok "get_crystal" "Installing Crystal"
if ! ${PKGCHK} "$INSTALL_PKGS" 1>/dev/null 2>&1; then
log_debug "Updating packages"
run_ok "${SUDO} ${UPDATE}" "Updating package repo"
for i in $INSTALL_PKGS; do
log_debug "Installing required packages $i"
#shellcheck disable=2086
${SUDO} ${INSTALL} ${i} >>"${RUN_LOG}" 2>&1
done
fi
log_success "Package Setup Finished"
# Reap any clingy processes (like spinner forks)
# get the parent pids (as those are the problem)
allpids="$(ps -o pid= --ppid $$) $allpids"
for pid in $allpids; do
kill "$pid" 1>/dev/null 2>&1
done
# Next step is configuration. Wait here for a moment, hopefully letting any
# apt processes disappear before we start, as they're huge and memory is a
# problem. XXX This is hacky. I'm not sure what's really causing random fails.
read_sleep 1
echo
# Setup Repository
log_debug "Phase 2 of 4: Repository Configuration"
printf "${GREEN}▣${YELLOW}▣${CYAN}□□${NORMAL} Phase ${YELLOW}2${NORMAL} of ${GREEN}4${NORMAL}: Setup Repository\\n"
# https://stackoverflow.com/a/51894266
# grep $USER_NAME /etc/passwd 1>/dev/null 2>&1
# if [[ ! $? -eq 0 ] ; then
#echo -e "${YELLOW}${ARROW} User $USER_NAME Not Found, adding user${NORMAL}"
if ! id -u "$USER_NAME" 1>/dev/null 2>&1; then
log_debug "Checking if $USER_NAME exists"
${SUDO} useradd -m $USER_NAME >>"${RUN_LOG}" 2>&1
fi
# If directory is not created
if [[ ! -d $USER_DIR ]]; then
#echo -e "${YELLOW}${ARROW} Folder Not Found, adding folder${NORMAL}"
log_debug "Checking if $USER_DIR exists"
mkdir -p $USER_DIR >>"${RUN_LOG}" 2>&1
fi
log_debug "Setting folder permissions"
run_ok "set_permissions" "Setting folder permissions"
if [[ -d $USER_DIR ]]; then
(
cd $USER_DIR >>"${RUN_LOG}" 2>&1 || exit 1
log_debug "Download Invidious from github.com/${IN_REPO}"
run_ok "sudo -i -u invidious \
git clone https://github.com/${IN_REPO}" "Cloning Invidious from github.com/${IN_REPO}"
cd - 1>/dev/null 2>&1 || exit 1
)
fi
if [[ -d $REPO_DIR ]]; then
(
cd $REPO_DIR >>"${RUN_LOG}" 2>&1 || exit 1
# Checkout
log_debug "Checking out master branch"
run_ok "GetMaster" "Checking out master branch"
log_debug "Setting folder permissions again to be sure"
run_ok "set_permissions" "Setting folder permissions again to be sure"
log_success "Repository Setup Finished"
cd - 1>/dev/null 2>&1 || exit 1
)
fi