Newer
Older
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
#
# koel application
# https://gist.github.com/bplower/613a99156d603abac083
#
# License
# =======
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
VARIANTS='full full-vim media'
KOEL_DESCRIPTION=$'To upload music files install Syncthing then copy album directories into ~/Sync/music_upload, then within Koel go to settings and scan the directory /music'
koel_variables=(ONION_ONLY
KOEL_DOMAIN_NAME
KOEL_CODE
DDNS_PROVIDER
MY_EMAIL_ADDRESS
MY_USERNAME)
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
function koel_upload_from_syncthing {
if [ ! -d /music ]; then
mkdir /music
fi
{ echo '#!/bin/bash';
echo '';
echo "syncdir=/home/$MY_USERNAME/Sync/music_upload";
echo '';
echo 'while true';
echo 'do';
echo " if [ -d \$syncdir ]; then";
echo " cd /home/$MY_USERNAME/Sync";
echo " if [ ! \"\$syncdir_base\" ]; then";
echo " syncdir_base=\$(ls -ld music_upload/*)";
echo ' fi';
echo '';
echo ' while read -r line; do';
echo " curr_dir=\$(echo \"\$line\" | awk -F '/' '{print \$2}')";
echo " if [ -d \$syncdir/\$curr_dir ]; then";
echo " if [ ! -d /music/\$curr_dir ]; then";
echo " mv \$syncdir/\$curr_dir /music";
echo ' fi';
echo ' fi';
echo " done <<< \"\$syncdir_base\"";
echo '';
echo " syncdir_base=\$(ls -ld music_upload/*)";
echo ' fi';
echo ' sleep 10';
echo 'done'; } > /usr/bin/koelsync
chmod +x /usr/bin/koelsync
if [ ! -f /etc/systemd/system/koel_syncthing.service ]; then
{ echo '[Unit]';
echo 'Description=Koel integration with syncthing';
echo 'After=syslog.target';
echo 'After=network.target';
echo '';
echo '[Service]';
echo 'Type=simple';
echo 'User=root';
echo "WorkingDirectory=/music";
echo "ExecStart=/usr/bin/koelsync";
echo 'Restart=on-failure';
echo '';
echo '[Install]';
echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/koel_syncthing.service
systemctl enable koel_syncthing.service
systemctl daemon-reload
fi
systemctl restart koel_syncthing.service
}
function logging_on_koel {
echo -n ''
}
function logging_off_koel {
echo -n ''
}
cd "/var/www/${KOEL_DOMAIN_NAME}/htdocs" || exit 83
sed -i "s|www.gravatar.com|${KOEL_DOMAIN_NAME}|g" node_modules/browser-sync-ui/public/js/app.js.map
sed -i "s|www.gravatar.com|${KOEL_DOMAIN_NAME}|g" public/js/app.*.js
if ! grep -q "//Vue.set(user, 'avatar'" resources/assets/js/stores/user.js; then
sed -i "s|Vue.set(user, 'avatar'|//Vue.set(user, 'avatar'|g" resources/assets/js/stores/user.js
fi
}
}
function add_user_koel {
new_username="$1"
new_user_password="$2"
"${PROJECT_NAME}-pass" -u "$new_username" -a koel -p "$new_user_password"
ONION_ONLY='no'
fi
if [[ $ONION_ONLY != "no" ]]; then
KOEL_DOMAIN_NAME='koel.local'
else
KOEL_DETAILS_COMPLETE=
while [ ! $KOEL_DETAILS_COMPLETE ]
do
dialog --backtitle $"Freedombone Configuration" \
--title $"Koel Configuration" \
--form $"\\nPlease enter your Koel details. The background image URL can be left blank.\\n\\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 14 65 2 \
$"Domain:" 1 1 "$(grep 'KOEL_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
2> "$data"
1) rm -f "$data"
exit 1;;
255) rm -f "$data"
exit 1;;
KOEL_DOMAIN_NAME=$(sed -n 1p < "$data")
if [ "$KOEL_DOMAIN_NAME" ]; then
if [[ "$KOEL_DOMAIN_NAME" == "$HUBZILLA_DOMAIN_NAME" ]]; then
KOEL_DOMAIN_NAME=""
fi
TEST_DOMAIN_NAME=$KOEL_DOMAIN_NAME
validate_domain_name
KOEL_DOMAIN_NAME=
dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
fi
fi
if [ $KOEL_DOMAIN_NAME ]; then
KOEL_DETAILS_COMPLETE="yes"
fi
done
fi
write_config_param "KOEL_DOMAIN_NAME" "$KOEL_DOMAIN_NAME"
APP_INSTALLED=1
}
function change_password_koel {
curr_username="$1"
new_user_password="$2"
read_config_param 'KOEL_DOMAIN_NAME'
"${PROJECT_NAME}-pass" -u "$curr_username" -a koel -p "$new_user_password"
if [ -f "$IMAGE_PASSWORD_FILE" ]; then
KOEL_ADMIN_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
if [ ! "$KOEL_ADMIN_PASSWORD" ]; then
KOEL_ADMIN_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
data=$(mktemp 2>/dev/null)
dialog --title "Choose a directory containing music" --dselect "/home/$MY_USERNAME/" 30 60 2> "$data"
selected_dir=$(cat "$data")
rm -f "$data"
if [[ "$selected_dir" == '/music' ]]; then
return
fi
return
fi
if [[ "$selected_dir" == "/home/$MY_USERNAME/" ]]; then
return
fi
if [[ "$selected_dir" == "/home/$MY_USERNAME/."* ]]; then
return
fi
if [[ "$selected_dir" == *"/Maildir" || "$selected_dir" == *"/Sync" ]]; then
return
fi
dialog --title $"Import music directory" \
--backtitle $"Freedombone Control Panel" \
--defaultno \
--yesno $"\\nImport the directory:\\n\\n $selected_dir" 12 75
sel=$?
case $sel in
1) return;;
255) return;;
esac
dialog --title $"Import music directory" \
--msgbox $"Import success. You may need to re-sync within Koel." 6 40
}
function koel_import_from_usb {
USB_DRIVE=$(detect_connected_drives)
dialog --title $"Import music from USB drive" --msgbox $'No USB drive found' 6 50
return
fi
backup_mount_drive "${USB_DRIVE}"
if [ ! -d "$USB_MOUNT/Music" ]; then
dialog --title $"Import music from USB drive" --msgbox $'No Music directory found on USB drive' 6 50
dialog --title $"Import music from USB drive" --msgbox $'Import complete. You may now remove the USB drive' 6 50
USB_DRIVE=$(detect_connected_drives)
dialog --title $"Export music to USB drive" --msgbox $'No USB drive found' 6 50
return
fi
backup_mount_drive "${USB_DRIVE}"
if [ ! -d "$USB_MOUNT/Music" ]; then
mkdir -p "$USB_MOUNT/Music"
cp -ru /music/* "$USB_MOUNT/Music"
backup_unmount_drive "${USB_DRIVE}"
dialog --title $"Export music to USB drive" --msgbox $'Export complete. You may now remove the USB drive' 6 50
}
USB_DRIVE=$(detect_connected_drives)
dialog --title $"Format USB drive $USB_DRIVE for music storage" \
--backtitle $"Freedombone Control Panel" \
--defaultno \
--yesno $"\\nPlease confirm that you wish to format drive\\n\\n ${USB_DRIVE}\\n\\nAll current data on the drive will be lost, and you will be prompted to give a password used to encrypt the drive.\\n\\nDANGER: If you screw up here and format the wrong drive it's your own fault!" 16 60
sel=$?
case $sel in
1) return;;
255) return;;
esac
clear
echo ''
echo $"Formatting drive $USB_DRIVE. ALL CONTENTS WILL BE LOST."
echo ''
dialog --title $"Format USB drive $USB_DRIVE for music storage" --msgbox $'Format complete. You may now export music or remove the USB drive' 6 50
}
W=(1 $"Import music from directory"
2 $"Import music from USB drive"
3 $"Export music to USB drive"
4 $"Format a USB drive for music storage")
# shellcheck disable=SC2068
selection=$(dialog --backtitle $"Freedombone Administrator Control Panel" --title $"Koel" --menu $"Choose an operation, or ESC to exit:" 12 60 4 "${W[@]}" 3>&2 2>&1 1>&3)
if [ ! "$selection" ]; then
break
fi
case $selection in
1) koel_import_from_directory;;
2) koel_import_from_usb;;
KOEL_DOMAIN_NAME=$(get_completion_param "koel domain")
fi
CURR_KOEL_COMMIT=$(get_completion_param "koel commit")
if [[ "$CURR_KOEL_COMMIT" == "$KOEL_COMMIT" ]]; then
return
fi
set_repo_commit "/var/www/$KOEL_DOMAIN_NAME/htdocs" "koel commit" "$KOEL_COMMIT" $KOEL_REPO
cd "/var/www/${KOEL_DOMAIN_NAME}/htdocs" || exit 73
chown -R www-data:www-data "/var/www/${KOEL_DOMAIN_NAME}/htdocs/"*
}
function backup_local_koel {
KOEL_DOMAIN_NAME='koel'
KOEL_DOMAIN_NAME=$(get_completion_param "koel domain")
fi
source_directory="/var/www/${KOEL_DOMAIN_NAME}/htdocs"
if [ -d "$source_directory" ]; then
backup_directory_to_usb "$source_directory" "$dest_directory"
function_check backup_database_to_usb
backup_database_to_usb koel
function_check restart_site
restart_site
return
fi
KOEL_DOMAIN_NAME=$(get_completion_param "koel domain")
temp_restore_dir=/root/tempkoel
function_check koel_create_database
koel_create_database
if [ -d $temp_restore_dir ]; then
rm -rf $temp_restore_dir
fi
MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
cd "/var/www/$KOEL_DOMAIN_NAME/htdocs" || exit 27
sed -i "s|DB_PASSWORD=.*|DB_PASSWORD=$MARIADB_PASSWORD|g" .env
MARIADB_PASSWORD=
systemctl start koel
}
function remove_koel {
if [ ${#KOEL_DOMAIN_NAME} -eq 0 ]; then
return
fi
systemctl stop koel
systemctl disable koel
if [ -f /etc/systemd/system/koel.service ]; then
rm /etc/systemd/system/koel.service
fi
if [ -f /etc/systemd/system/koel_syncthing.service ]; then
systemctl stop koel_syncthing
systemctl disable koel_syncthing
rm /etc/systemd/system/koel_syncthing.service
fi
function_check remove_nodejs
remove_nodejs koel
read_config_param "KOEL_DOMAIN_NAME"
read_config_param "MY_USERNAME"
echo "Removing $KOEL_DOMAIN_NAME"
nginx_dissite "$KOEL_DOMAIN_NAME"
remove_certs "$KOEL_DOMAIN_NAME"
if [ -d "/var/www/$KOEL_DOMAIN_NAME" ]; then
rm -rf "/var/www/$KOEL_DOMAIN_NAME"
if [ -f "/etc/nginx/sites-available/$KOEL_DOMAIN_NAME" ]; then
rm "/etc/nginx/sites-available/$KOEL_DOMAIN_NAME"
fi
function_check drop_database
drop_database koel
function_check remove_onion_service
remove_onion_service koel ${KOEL_ONION_PORT}
remove_app koel
remove_completion_param install_koel
if [ -f /usr/bin/koelsync ]; then
rm /usr/bin/koelsync
fi
fi
if [[ $(app_is_installed koel_main) == "1" ]]; then
return
fi
function_check install_mariadb
install_mariadb
function_check get_mariadb_password
get_mariadb_password
function_check repair_databases_script
repair_databases_script
$INSTALL_PACKAGES php-gettext php-curl php-gd php-mysql git curl php-zip
$INSTALL_PACKAGES php-memcached php-intl exiftool libfcgi0ldbl
$INSTALL_PACKAGES ffmpeg
if [ -d "/var/www/$KOEL_DOMAIN_NAME" ]; then
rm -rf "/var/www/$KOEL_DOMAIN_NAME"
cd "/var/www/$KOEL_DOMAIN_NAME/htdocs" || exit 23
git pull
else
function_check git_clone
cd "/var/www/$KOEL_DOMAIN_NAME/htdocs" || exit 83
#git checkout "$KOEL_COMMIT" -b "$KOEL_COMMIT"
git checkout "$KOEL_COMMIT"
chown -R www-data:www-data "/var/www/$KOEL_DOMAIN_NAME/htdocs"
function_check koel_create_database
koel_create_database
function_check add_ddns_domain
add_ddns_domain "$KOEL_DOMAIN_NAME"
KOEL_ONION_HOSTNAME=$(add_onion_service koel 80 ${KOEL_ONION_PORT})
koel_nginx_site=/etc/nginx/sites-available/$KOEL_DOMAIN_NAME
if [[ $ONION_ONLY == "no" ]]; then
function_check nginx_http_redirect
nginx_http_redirect "$KOEL_DOMAIN_NAME" "index index.php"
{ echo 'server {';
echo ' listen 443 ssl;';
echo ' #listen [::]:443 ssl;';
echo " server_name $KOEL_DOMAIN_NAME;";
echo ''; } >> "$koel_nginx_site"
nginx_compress "$KOEL_DOMAIN_NAME"
{ echo ' gzip_comp_level 9;';
echo '';
echo ' # Security'; } >> "$koel_nginx_site"
function_check nginx_security_options
nginx_security_options "$KOEL_DOMAIN_NAME"
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
{ echo ' add_header Strict-Transport-Security max-age=15768000;';
echo '';
echo ' # Logs';
echo ' access_log /dev/null;';
echo ' error_log /dev/null;';
echo '';
echo ' # Root';
echo " root /var/www/$KOEL_DOMAIN_NAME/htdocs;";
echo '';
echo ' # Index';
echo ' index index.php;';
echo '';
echo ' # Whitelist only index.php, robots.txt, and those start with public/ or api/';
echo " if (\$request_uri !~ ^/\$|index\\.php|robots\\.txt|api/|public/) {";
echo ' return 404;';
echo ' }';
echo '';
echo ' location /media/ {';
echo ' internal;';
echo '';
echo ' # A X-Media-Root should be set to media_path settings from upstream';
echo " alias \$upstream_http_x_media_root;";
echo '';
echo ' }';
echo '';
echo ' # PHP';
echo ' location ~ \.php {';
echo ' include snippets/fastcgi-php.conf;';
echo " fastcgi_pass unix:/var/run/php/php${PHP_VERSION}-fpm.sock;";
echo ' fastcgi_read_timeout 30;';
echo ' }';
echo '';
echo ' # Location';
echo ' location / {'; } >> "$koel_nginx_site"
nginx_limits "$KOEL_DOMAIN_NAME" '15m'
{ echo " try_files \$uri \$uri/ @koel;";
echo ' }';
echo '';
echo ' # Fancy URLs';
echo ' location @koel {';
echo " rewrite ^(.*)\$ /index.php?p=\$1 last;";
echo ' }';
echo '';
echo ' # Restrict access that is unnecessary anyway';
echo ' location ~ /\.(ht|git) {';
echo ' deny all;';
echo ' }';
echo '';
echo '}'; } >> "$koel_nginx_site"
else
echo -n '' > "$koel_nginx_site"
fi
if [[ "$ONION_ONLY" == 'no' ]]; then
{ echo 'server {';
echo " listen 127.0.0.1:$KOEL_ONION_PORT default_server;";
echo " server_name $KOEL_DOMAIN_NAME;";
echo ''; } >> "$koel_nginx_site"
{ echo 'server {';
echo " listen 127.0.0.1:$KOEL_ONION_PORT default_server;";
echo " server_name $KOEL_ONION_HOSTNAME;";
echo ''; } >> "$koel_nginx_site"
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
nginx_compress "$KOEL_DOMAIN_NAME"
{ echo ' gzip_comp_level 9;';
echo '';
echo ' # Logs';
echo ' access_log /dev/null;';
echo ' error_log /dev/null;';
echo '';
echo ' # Root';
echo " root /var/www/$KOEL_DOMAIN_NAME/htdocs;";
echo '';
echo ' # Index';
echo ' index index.php;';
echo '';
echo ' # Whitelist only index.php, robots.txt, and those start with public/ or api/';
echo " if (\$request_uri !~ ^/\$|index\\.php|robots\\.txt|api/|public/) {";
echo ' return 404;';
echo ' }';
echo '';
echo ' location /media/ {';
echo ' internal;';
echo '';
echo ' # A X-Media-Root should be set to media_path settings from upstream';
echo " alias \$upstream_http_x_media_root;";
echo '';
echo ' }';
echo '';
echo ' # PHP';
echo ' location ~ \.php {';
echo ' include snippets/fastcgi-php.conf;';
echo " fastcgi_pass unix:/var/run/php/php${PHP_VERSION}-fpm.sock;";
echo ' fastcgi_read_timeout 30;';
echo ' }';
echo '';
echo ' # Location';
echo ' location / {'; } >> "$koel_nginx_site"
nginx_limits "$KOEL_DOMAIN_NAME" '15m'
{ echo " try_files \$uri \$uri/ @koel;";
echo ' }';
echo '';
echo ' # Fancy URLs';
echo ' location @koel {';
echo " rewrite ^(.*)\$ /index.php?p=\$1 last;";
echo ' }';
echo '';
echo ' # Restrict access that is unnecessary anyway';
echo ' location ~ /\.(ht|git) {';
echo ' deny all;';
echo ' }';
echo '';
echo '}'; } >> "$koel_nginx_site"
sed -i 's|gzip_types.*|gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json;|g' "$koel_nginx_site"
sed -i 's|DENY;|SAMEORIGIN;|g' "$koel_nginx_site"
function_check configure_php
configure_php
function_check create_site_certificate
# Ensure that the database gets backed up locally, if remote
# backups are not being used
function_check backup_databases_script_header
backup_databases_script_header
function_check backup_database_local
backup_database_local koel
function_check nginx_ensite
# shellcheck disable=SC2086
systemctl restart php${PHP_VERSION}-fpm
"${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a koel -p "$KOEL_ADMIN_PASSWORD"
set_completion_param "koel domain" "$KOEL_DOMAIN_NAME"
install_completed koel_main
}
function install_koel {
if [ ! $ONION_ONLY ]; then
ONION_ONLY='no'
fi
cd "/var/www/$KOEL_DOMAIN_NAME/htdocs" || exit 24
increment_app_install_progress
function_check get_mariadb_password
get_mariadb_password
cp .env.example .env
sed -i "s/ADMIN_EMAIL=.*/ADMIN_EMAIL=$MY_EMAIL_ADDRESS/g" .env
sed -i "s/ADMIN_NAME=.*/ADMIN_NAME=$MY_USERNAME/g" .env
sed -i "s/ADMIN_PASSWORD=.*/ADMIN_PASSWORD=$KOEL_ADMIN_PASSWORD/g" .env
sed -i 's/DB_CONNECTION=.*/DB_CONNECTION=mysql/g' .env
sed -i 's/DB_HOST=.*/DB_HOST=127.0.0.1/g' .env
sed -i 's/DB_DATABASE=.*/DB_DATABASE=koel/g' .env
sed -i 's/DB_USERNAME=.*/DB_USERNAME=root/g' .env
sed -i 's/MAIL_HOST=.*/MAIL_HOST=localhost/g' .env
sed -i 's/MAIL_PORT=.*/MAIL_PORT=25/g' .env
sed -i 's|FFMPEG_PATH=.*|FFMPEG_PATH=/usr/bin/ffmpeg|g' .env
if ! grep -q 'MEDIA_PATH=' .env; then
echo 'MEDIA_PATH=/music|g' >> .env
else
sed -i 's|MEDIA_PATH=.*|MEDIA_PATH=/music|g' .env
fi
if ! php artisan koel:init --no-interaction; then
chown -R www-data:www-data "/var/www/$KOEL_DOMAIN_NAME/htdocs"
{ echo '[Unit]';
echo 'Description=Koel (music player)';
echo 'After=syslog.target';
echo 'After=network.target';
echo '';
echo '[Service]';
echo 'Type=simple';
echo 'User=www-data';
echo 'Group=www-data';
echo "WorkingDirectory=/var/www/$KOEL_DOMAIN_NAME/htdocs";
echo "ExecStart=/usr/bin/php artisan serve --port=$KOEL_PORT";
echo 'Restart=on-failure';
echo 'PrivateTmp=true';
echo 'PrivateDevices=false';
echo 'NoNewPrivileges=true';
echo 'CapabilityBoundingSet=~CAP_SYS_ADMIN';
echo '';
echo '[Install]';
echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/koel.service
systemctl enable koel.service
systemctl daemon-reload
systemctl start koel.service
if [ ! -d /music ]; then
mkdir /music
fi
chown -R www-data:www-data /music
increment_app_install_progress