Files
mediamanager/mm-scripts.sh
2023-04-27 07:51:12 -05:00

1401 lines
53 KiB
Bash
Executable File

#!/usr/bin/env bash
# MediaManager Scripts
action="$1"
VERBOSE=true
if [ -f /opt/idssys/settings/mediamanager.conf ]; then
mv /opt/idssys/settings/mediamanager.conf ${MMFOLDER}/settings.conf
fi
source /opt/idssys/defaults/colors.inc
source /opt/idssys/defaults/default.inc
source /opt/idssys/mediamanager/settings.conf
source /opt/idssys/mediamanager/defaults.inc
[ -f ${MMFOLDER}/realwanip ] && source ${MMFOLDER}/realwanip
[ -f ${MMFOLDER}/vers.info ] && source ${MMFOLDER}/vers.info
if [ "${2}" == "q" ]; then
unset idsCL idsBG idsST
idsCL=('')
idsBG=('')
idsST=('')
fi
[ -f ${MMFOLDER}/vpninfo ] && source ${MMFOLDER}/vpninfo
if [ "${action}" != "check" ] && [ "${action}" != "update" ] && [ "${2}" != "noup" ]; then
if [ -f ${MMFOLDER}/update.available ]; then
dispup="${idsCL[Yellow]} - Update Available! (run '${idsCL[LightCyan]}mm update${idsCL[Yellow]}')${idsCL[Default]}"
else
dispup=""
fi
echo
echo -e "${idsCL[LightGreen]} MediaManager ${idsCL[Default]} ${idsCL[DarkGray]}(ver-${VERS})${dispup}${idsCL[Default]}"
DIVIDER . lightGreen
fi
IFS=,
SERVICES_CHECK=(${SERVICESCHECK})
unset IFS
cw_spc=40
MMSTART(){
[ $VERBOSE = true ] && chktmstart=`date +%s`
if [ "${1}" = "start" ] || [ "${1}" = "restart" ] || [ "${1}" = "" ]; then
GOFORSTART=true;
elif [ "${1}" == "check" ] && [ "$(/sbin/ip link | grep "${VPN_INTERFACE}")" == "" ] && [ ! -f ${MMFOLDER}/mm.vpn* ] && [ ! -f ${MMFOLDER}/vpninfo.* ]; then
GOFORSTART=true;
SENDNOTICE "Starting Media Management Systems" "Fresh Startup"
echo "$(date) - Starting Media Management system - (fresh start)" >> $logfile
echo -e "${idsCL[LightCyan]}Fresh Startup${idsCL[Default]}"
elif [ "${1}" = "check" ] && [ "$(/sbin/ip link | grep "${VPN_INTERFACE}")" = "" ] && [ -f ${MMFOLDER}/mm.vpn* ]; then
GOFORSTART=true;
echo "$(date) - Attempting to restart systems after VPN failure..." >> $logfile
echo -e "${idsCL[LightCyan]}Attempting to restart systems after VPN failure...${idsCL[Default]}"
SENDNOTICE "Attempting to start systems after VPN failure..."
else
GOFORSTART=false;
fi
if [ ${GOFORSTART} = true ]; then
mmsv='Starting'
echo
OUT=$(/sbin/ip link | grep "${VPN_INTERFACE}")
if [ ${#OUT} = 0 ]; then
msg="Setting up the VPN Tunnel config"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
VPNCONFIG
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
msg="Starting the VPN Tunnel"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
echo "REALWANIP=\"$(/usr/bin/dig +short +time=2 myip.opendns.com @208.67.222.222)\"" > ${MMFOLDER}/realwanip
source ${MMFOLDER}/realwanip
"${VPN_START[@]}" >/dev/null 2>&1
sleep 5s
else
msg="The VPN Tunnel is already running"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
fi
else
msg="Verifying the VPN Tunnel"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
mmsv='Verifying'
fi
[ $VERBOSE = true ] && echo "$(date) - ${mmsv} VPN..." >> $logfile
OUT=$(/sbin/ip link | grep "$VPN_INTERFACE")
if [ ${#OUT} = 0 ]; then
if [ -f ${MMFOLDER}/mm.vpn.fail ]; then
echo -e "${idsCL[Red]} failed, restarting system${idsCL[Default]}"
echo "$(date) - VPN Failure #2 - restarting system" >> $logfile
SENDNOTICE "VPN Failure" "2nd Failure, will try and restart the VPN" 1
touch ${MMFOLDER}/mm.vpn.fail2
rm -f ${MMFOLDER}/mm.vpn.fail
MMSTOP
# sleep 2m
# "${VPN_START[@]}" >/dev/null 2>&1
# sleep 10s
rm -f ${MMFOLDER}/mm.stop*
[ $VERBOSE = true ] && DEBUGLOGSTOP
exit 1
elif [ -f ${MMFOLDER}/mm.vpn.fail2 ]; then
echo "$(date) - VPN Failure #3 - Shutting down Media Management systems" >> $logfile
SENDNOTICE "VPN Failure" "Final Failure, shutting down system" 2
MMSTOP
[ $VERBOSE = true ] && DEBUGLOGSTOP
exit 1
else
echo -e "${idsCL[Red]} failed, restarting system${idsCL[Default]}"
echo "$(date) - VPN Failure #1 - restarting system" >> $logfile
SENDNOTICE "VPN Failure" "1st Failure, will try and restart the VPN" 1
touch ${MMFOLDER}/mm.vpn.fail
MMSTOP
# sleep 10s
# "${VPN_START[@]}" >/dev/null 2>&1
# sleep 10s
rm -f ${MMFOLDER}/mm.stop*
[ $VERBOSE = true ] && DEBUGLOGSTOP
exit 1
fi
fi
[ "${1}" == "start" ] && rm -f mm.vpn.fail*
OUT=$(/sbin/ip link | grep "$VPN_INTERFACE")
if [ ${#OUT} != 0 ]; then
if [ ${GOFORSTART} = true ] || [ "${1}" == "check" ]; then
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
fi
vpnuptime=$(expr `date +%s` - $(stat -c %Y ${MMFOLDER}/vpninfo))
if [ ${vpnuptime} -ge 86400 ]; then
echo "$(date) - 24hr session limit reached, restarting vpn & services" >> $logfile
echo -en "${idsCL[LightCyan]}24hr session limit reached, restarting vpn & services ."
[ -f ${MMFOLDER}/mm.noservices ] && rv="vpn" || rv=""
MMSTOP >/dev/null 2>&1
echo -en "."
read -et 10
echo -en "."
read -et 10
echo -en "."
read -et 10
echo -en "."
MMSTART start ${rv} >/dev/null 2>&1
echo -e " ${idsCL[Green]}Done${idsCL[Default]}"
fi
zerogw=$(/sbin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3)
defaultgw=$(/sbin/ip route show | grep default | cut -d' ' -f 3)
# if [ "${zerogw}" != "${defaultgw}" ]; then
# if [ "${defaultgw}" != "${VPN_INTERFACE}" ]; then
if [ "${zerogw}" != "${defaultgw}" ] || [ "${defaultgw}" != "${VPN_INTERFACE}" ]; then
echo "$(date) - Adding VPN default routes" >> $logfile
msg="Adding VPN default routes"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
/bin/ip route del default
/bin/ip route add default dev ${VPN_INTERFACE}
/bin/ip route add 0.0.0.0/1 dev ${VPN_INTERFACE}
/bin/ip route add 128.0.0.0/1 dev ${VPN_INTERFACE}
/bin/ip route add ${vpn_server_address}/32 via 10.10.0.1 dev eth0
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
zerogw=$(/sbin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3)
defaultgw=$(/sbin/ip route show | grep default | cut -d' ' -f 3)
fi
msg="Verifying default network routes"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
checked=false
cc=0
until [ "${checked}" = "" ]; do
if [ "${zerogw}" = "${defaultgw}" ]; then
checked=""
fi
if [ "${checked}" == "false" ] && [ ${cc} -eq 10 ]; then
echo -e "${idsCL[Red]}Could not confirm default routes, restarting system!${idsCL[Default]}"
echo "$(date) - Could not confirm default routes, restarting system" >> $logfile
SENDNOTICE "Network Failure" "Default route failure, will try and restart the VPN" 1
touch ${MMFOLDER}/mm.vpn.fail
MMSTOP
sleep 10s
# "${VPN_START[@]}" >/dev/null 2>&1
# sleep 10s
rm -f ${MMFOLDER}/mm.stop*
[ $VERBOSE = true ] && DEBUGLOGSTOP
exit 1
fi
((cc=${cc}+1))
sleep 1s
zerogw=$(/sbin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3)
defaultgw=$(/sbin/ip route show | grep default | cut -d' ' -f 3)
done
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
msg="Verifying traffic routing through VPN"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
mmip=""
cc=0
until [ "${REALWANIP}" != "" ] && [ "${mmip}" != "" ] && [[ "${REALWANIP}" != *"connection timed out"* ]] && [[ "${mmip}" != *"connection timed out"* ]]; do
# mmip=$(/usr/bin/dig +short myip.opendns.com @resolver1.opendns.com)
mmip=$(/usr/bin/dig +short +time=2 myip.opendns.com @208.67.222.222)
if [ ${cc} -eq 10 ]; then
echo
echo -e "${idsCL[Red]}Cannot get an WAN address, restarting system!${idsCL[Default]}"
echo "$(date) - Cannot get an WAN address, restarting system! wanip(${REALWANIP}) vpnip(${mmip})" >> $logfile
[ $VERBOSE = true ] && echo "$(date) - Default Routes: zerogw(${zerogw}) defaultgw(${defaultgw})" >> $logfile
SENDNOTICE "Network Failure" "Cannot get an WAN address, will try and restart the VPN" 1
touch ${MMFOLDER}/mm.vpn.fail
MMSTOP >/dev/null 2>&1
sleep 10s
# "${VPN_START[@]}" >/dev/null 2>&1
# sleep 10s
rm -f ${MMFOLDER}/mm.stop*
[ $VERBOSE = true ] && DEBUGLOGSTOP
exit 1
fi
((cc=${cc}+1))
sleep 1s
done
if [ "${REALWANIP}" != "${mmip}" ]; then
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
[ $VERBOSE = true ] && echo "$(date) - WAN Address: ${mmip} - GW Address: ${defaultgw}" >> $logfile
else
echo
echo -e "${idsCL[Red]}Traffic is not routing through VPN, restarting system!${idsCL[Default]}"
echo "$(date) - Traffic is not routing through VPN, restarting system! wanip(${REALWANIP}) vpnip(${mmip})" >> $logfile
[ $VERBOSE = true ] && echo "$(date) - Default Routes: zerogw(${zerogw}) defaultgw(${defaultgw})" >> $logfile
SENDNOTICE "Network Failure" "Traffic route failure, will try and restart the VPN" 1
touch ${MMFOLDER}/mm.vpn.fail
MMSTOP
sleep 10s
# "${VPN_START[@]}" >/dev/null 2>&1
# sleep 10s
rm -f ${MMFOLDER}/mm.stop*
[ $VERBOSE = true ] && DEBUGLOGSTOP
exit 1
fi
msg="Verifying Plex Media is Mounted"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
### CHECK FOR PLEX MOUNT
if [ ! -f ${PLEX_MOUNT}/mounted ]; then
echo "$(date) - Plex Media Not Mounted, will try and fix" >> $logfile
umount ${PLEX_MOUNT} >/dev/null 2>&1
sleep 2s
mount -a >/dev/null 2>&1
sleep 2s
if [ ! -f ${PLEX_MOUNT}/mounted ]; then
if [ -f ${MMFOLDER}/mm.plexmount.fail ]; then
fd=$(( `date +%s` - `stat -L --format %Y ${MMFOLDER}/mm.plexmount.fail` ))
if [ ${fd} -gt 3600 ]; then
PMFS=true
else
PMFS=false
fi
else
PMFS=true
fi
if [ ${PMFS} = true ]; then
touch ${MMFOLDER}/mm.plexmount.fail
SENDNOTICE "Plex Media Not Mounted" "Could not mount the Plex Media Folder" 1
echo "$(date) - Plex Media could not be mounted" >> $logfile
fi
echo -e "${idsCL[Red]}Not Mounted${idsCL[Default]}"
for srvc in "${SERVICES_CHECK[@]}"; do
/bin/systemctl stop ${srvc} >/dev/null 2>&1 &
done
else
echo -e "${idsCL[Green]}Mounted, fixed${idsCL[Default]}"
SENDNOTICE "Plex Media Mounted" "The Plex Media Folder was successfully re-mounted again!"
echo "$(date) - Plex Media now mounted, stopping services to restart them" >> $logfile
for srvc in "${SERVICES_CHECK[@]}"; do
/bin/systemctl stop ${srvc} >/dev/null 2>&1 &
done
rm -f ${MMFOLDER}/mm.plexmount.fail*
fi
else
echo -e "${idsCL[Green]}Mounted${idsCL[Default]}"
if [ -f ${MMFOLDER}/mm.plexmount.fail ] || [ -f ${MMFOLDER}/mm.plexmount.fail2 ]; then
echo -e "${idsCL[Green]}Mounted, fixed${idsCL[Default]}"
SENDNOTICE "Plex Media Mounted" "The Plex Media Folder was successfully re-mounted again!"
echo "$(date) - Plex Media now mounted, stopping services to restart them" >> $logfile
for srvc in "${SERVICES_CHECK[@]}"; do
/bin/systemctl stop ${srvc} >/dev/null 2>&1 &
done
rm -f ${MMFOLDER}/mm.plexmount.fail*
fi
fi
if [ -f ${MMFOLDER}/mm.plexmount.fail ] && [ "${1}" == "start" ] && [ "${2}" != "vpn" ]; then
echo -en "${idsCL[LightCyan]}Would you like to start services without Plex Media being mounted? ${idsCL[LightYellow]}(y/N)${idsCL[LightCyan]}? ${idsCL[Default]}"
read -n 1 choice
case "$choice" in
[Yy])
rm -f ${MMFOLDER}/mm.plexmount.fail*
touch ${MMFOLDER}/mm.plexmount.fail2
echo;echo
;;
* )
echo;echo
;;
esac
fi
[ "${1}" == "start" ] && [ "${2}" != "vpn" ] && rm -f ${MMFOLDER}/mm.noservices
if [ "${2}" != "vpn" ] && [ ! -f ${MMFOLDER}/mm.noservices ] && [ ! -f ${MMFOLDER}/mm.plexmount.fail ]; then
msg="${mmsv} Services"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
for srvc in "${SERVICES_CHECK[@]}"; do
[ "$(systemctl is-active ${srvc})" != "active" ] && /bin/systemctl restart ${srvc}
done
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
msg="Waiting for Services to Start"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
for srvc in "${SERVICES_CHECK[@]}"; do
if [ "${MM_SERVICE_PORTS[${srvc}]}" != "" ]; then
checkport=$(lsof -Pi :${MM_SERVICE_PORTS[${srvc}]} -sTCP:LISTEN -t)
tryupdate=false
cc=0
until [ "${checkport}" != "" ]; do
if [ ${cc} -eq 10 ]; then
if [ -f ${MMFOLDER}/mm.${srvc}.starting ]; then
msg="${srvc^} Service is not starting properly, will try and update"
echo -e "${idsCL[Red]}${msg}${idsCL[Default]}"
echo "$(date) - ${msg}" >> $logfile
SENDNOTICE "${srvc^} Service Issue" "${msg}" 1
tryupdate="true"
msg="Updating ${srvc^} Service"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
rm -f ${MMFOLDER}/mm.${srvc}.starting
else
[ "${MM_UPDATE_REPO[$srvc]}" != "" ] && touch ${MMFOLDER}/mm.${srvc}.starting
fi
checkport="go"
fi
if [ "${checkport}" != "go" ]; then
checkport=$(lsof -Pi :${MM_SERVICE_PORTS[${srvc}]} -sTCP:LISTEN -t)
((cc=${cc}+1))
sleep 1s
fi
done
if ([ "${checkport}" == "" ] || [ "${tryupdate}" == "true" ]) && [ "${MM_UPDATE_REPO[${srvc}]}" != "" ]; then
UPDATESERVICE ${srvc} >/dev/null 2>&1
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
elif [ "${tryupdate}" == "true" ]; then
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
fi
[ "${checkport}" != "" ] && rm -f ${MMFOLDER}/mm.${srvc}.starting >/dev/null 2>&1
fi
done
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
else
echo -e "${idsCL[LightYellow]}Skipping ${mmsv} Services ${idsCL[Default]}"
fi
echo
if [ "${1}" == "start" ] || [ "${1}" == "restart" ]; then
#echo -e "${idsCL[LightCyan]}It may take a couple of minutes before all services fully start${idsCL[Default]}"
MMSTATUS
fi
if [ -f ${MMFOLDER}/mm.vpn.fail* ]; then
echo "$(date) - VPN issue has been fixed" >> $logfile
SENDNOTICE "Services Restored" "VPN issue has been fixed"
rm -f ${MMFOLDER}/mm.vpn.fail*
fi
rm -f ${MMFOLDER}/mm.stop*
echo -e "${idsCL[Green]}All systems are go!${idsCL[Default]}"
if [ "${1}" = "start" ] || [ "${1}" = "" ]; then
echo "$(date) - Media Management systems started" >> $logfile
fi
fi
echo
[ $VERBOSE = true ] && DEBUGLOGSTOP
#exit 0
}
MMSTOP(){
if [ -f ${MMFOLDER}/mm.stop ]; then
echo -e "${idsCL[Yellow]}The system is already stopped, will verify anyway...${idsCL[Default]}"
else
echo -e "${idsCL[Green]}Stopping System...${idsCL[Default]}"
fi
echo
touch ${MMFOLDER}/mm.stop
# echo -en "${idsCL[LightCyan]}Stopping background MM scripts... ${idsCL[Default]}"
# getMMbash=$(pgrep -f '/mm')
# for i in $getMMbash; do
# /bin/kill $i
# done
# echo -e "${idsCL[Green]}Done${idsCL[Default]}"
msg="Stopping Services"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
for srvc in "${!MM_SERVICES[@]}"; do
if [[ ${srvc} != *"qbittorrent"* ]] && [[ ${srvc} != *"vpn"* ]] && [[ ${srvc} != *"wireguard"* ]]; then
/bin/systemctl stop ${srvc} >/dev/null 2>&1 &
fi
done
sleep 2s
/bin/systemctl stop qbittorrent
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
msg="Waiting for Services to stop"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
for srvc in "${!MM_SERVICES[@]}"; do
if [ "MM_SERVICE_PORTS[${srvc}]" != "" ] && [[ ${srvc} != *"vpn"* ]] && [[ ${srvc} != *"wireguard"* ]]; then
checkport=$(lsof -Pi :${MM_SERVICE_PORTS[${srvc}]} -sTCP:LISTEN -t)
# echo "$srvc - ${MM_SERVICE_PORTS[${srvc}]} - $checkport"
until [ "${checkport}" == "" ]; do
sleep 1s
checkport=$(lsof -Pi :${MM_SERVICE_PORTS[${srvc}]} -sTCP:LISTEN -t)
done
fi
done
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
msg="Shutting Down VPN Tunnel"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
"${VPN_STOP[@]}" >/dev/null 2>&1
checked=false
until [ "${checked}" = "" ]; do
OUT=$(/sbin/ip link | grep "$VPN_INTERFACE")
#OUT=`ip a show $VPN_INTERFACE up` >/dev/null 2>&1
if [ ${#OUT} = 0 ]; then
checked=""
fi
done
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
msg="Re-Adding Default Network Routes"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}"
[ "$(/sbin/ip route show | grep default | cut -d' ' -f 3)" != "10.10.0.1" ] && /bin/ip route add default via 10.10.0.1 && ip route del ${vpn_server_address}
[ $VERBOSE = true ] && echo "$(date) - Added default route back in" >> $logfile
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
rm -f ${MMFOLDER}/vpninfo*
rm -f ${MMFOLDER}/realwanip
rm -f ${MMFOLDER}/mm.noservices*
rm -f ${MMFOLDER}/mm.plexmount.fail*
rm -f ${MMFOLDER}/mm.stop.checking
echo "$(date) - Media Management systems stopped" >> $logfile
echo
echo -e "${idsCL[Red]}All services have been stopped${idsCL[Default]}"
echo
touch ${MMFOLDER}/mm.stop
}
MMSTATUS(){
statusArray=(wireguard)
statusArray+=(${SERVICES_CHECK[@]})
echo
for srvc in "${statusArray[@]}"; do
c=0; spc=''
spc1=$((${cw_spc}-${#MM_SERVICES[${srvc}]}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
dispname="${MM_SERVICES[${srvc}]/(/${idsCL[LightCyan]}(}"
echo -en " ${idsCL[White]}${dispname}${spc}${idsCL[White]}: ${idsCL[Default]}"
if [ "$(systemctl is-active ${srvc})" != "active" ] && [ "${srvc}" != "wireguard" ]; then
if [ -f ${MMFOLDER}/mm.noservices ]; then
echo -e "${idsCL[LightCyan]}Skipping, started wtih VPN only${idsCL[Default]}"
elif [ -f ${MMFOLDER}/mm.plexmount.fail ]; then
echo -e "${idsCL[LightCyan]}Skipping, Plex Media not mounted${idsCL[Default]}"
else
echo -e "${idsCL[Red]}Not Running${idsCL[Default]}"
fi
elif [ "${srvc}" == "wireguard" ] && [ "$(/sbin/ip link | grep $VPN_INTERFACE)" == "" ]; then
echo -e "${idsCL[Red]}Not Running${idsCL[Default]}"
else
[ "${MM_SERVICE_PORTS[${srvc}]}" != "" ] && checkport=$(lsof -Pi :${MM_SERVICE_PORTS[${srvc}]} -sTCP:LISTEN -t)
if ([ "${MM_SERVICE_PORTS[${srvc}]}" != "" ] && [ "${checkport}" != "" ]) || [ "${MM_SERVICE_PORTS[${srvc}]}" == "" ]; then
echo -e "${idsCL[Green]}Running${idsCL[Default]}"
elif [ "${checkport}" == "" ]; then
echo -e "${idsCL[LightMagenta]}Service Running, Web Port Not Accesible${idsCL[Default]}"
fi
if [[ ${srvc} == *"vpn"* ]] || [[ ${srvc} == *"wireguard"* ]]; then
zerogw=$(/sbin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3)
defaultgw=$(/sbin/ip route show | grep default | cut -d' ' -f 3)
vpnuptime=$(expr `date +%s` - $(stat -c %Y ${MMFOLDER}/vpninfo))
msg=" |- ${idsCL[Default]}VPN/Services Uptime"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6)); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -e "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Green]}$(SHOW_TIME ${vpnuptime})${idsCL[Default]}"
msg=" |- ${idsCL[Default]}0.0.0.0 Gateway"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Default]}"
echo -e "${idsCL[Green]}$zerogw${idsCL[Default]}"
msg=" |- ${idsCL[Default]}DEFAULT Gateway"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Default]}"
if [ "${zerogw}" == "${defaultgw}" ] && [ "${zerogw}" == "${VPN_INTERFACE}" ]; then
echo -e "${idsCL[Green]}$defaultgw${idsCL[Default]}"
else
echo -e "${idsCL[Red]}$defaultgw - Incorrect Gateways${idsCL[Default]}"
if ([ ! -f ${MMFOLDER}/mm.stop* ] && [ "${1}" == "fix" ]) || ([ ! -f ${MMFOLDER}/mm.stop ] && [ -f ${MMFOLDER}/mm.stop.checking ] && [ "${1}" == "fix" ]); then
echo
echo -e "${idsCL[LightCyan]}Restarting system...${idsCL[Default]}"
MMSTOP
echo -en "${idsCL[LightCyan]}Pausing for 10secs... ${idsCL[Default]}"
sleep 10s
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
echo
echo -e "${idsCL[LightCyan]}Starting system back up...${idsCL[Default]}"
echo
MMSTART
msg=" ${MM_SERVICES[wireguard]}"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[White]}${msg}${spc}${idsCL[White]}: ${idsCL[Default]}"
echo -e "${idsCL[Green]}Running${idsCL[Default]}"
vpnuptime=$(expr `date +%s` - $(stat -c %Y ${MMFOLDER}/vpninfo))
msg=" |- ${idsCL[Default]}VPN/Services Uptime"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6)); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -e "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Green]}$(SHOW_TIME ${vpnuptime})${idsCL[Default]}"
zerogw=$(/sbin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3)
defaultgw=$(/sbin/ip route show | grep default | cut -d' ' -f 3)
msg=" |- ${idsCL[Default]}0.0.0.0 Gateway"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Default]}"
echo -e "${idsCL[Green]}$zerogw${idsCL[Default]}"
msg=" |- ${idsCL[Default]}DEFAULT Gateway"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Default]}"
if [ "${zerogw}" == "${defaultgw}" ] && [ "${zerogw}" == "${VPN_INTERFACE}" ]; then
echo -e "${idsCL[Green]}$defaultgw${idsCL[Default]}"
else
echo -e "${idsCL[Red]}$defaultgw - Incorrect Gateways${idsCL[Default]}"
echo
if [ ! -f ${MMFOLDER}/mm.stop.checking ]; then
echo -e "${idsCL[LightCyan]}Shutting down system... fix yo shit${idsCL[Default]}"
MMSTOP
fi
fi
fi
fi
msg=" |- ${idsCL[Default]}Verify routing through VPN"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Default]}"
# mmip=$(/usr/bin/dig +short myip.opendns.com @resolver1.opendns.com)
mmip=$(/usr/bin/dig +short +time=2 myip.opendns.com @208.67.222.222)
if [ "${REALWANIP}" != "${mmip}" ]; then
echo -e "${idsCL[Green]}Good${idsCL[Default]}"
else
echo -e "${idsCL[Red]}Not Routing${idsCL[Default]}"
if ([ ! -f ${MMFOLDER}/mm.stop* ] && [ "${1}" == "fix" ]) || ([ ! -f ${MMFOLDER}/mm.stop ] && [ -f ${MMFOLDER}/mm.stop.checking ] && [ "${1}" == "fix" ]); then
echo
echo -e "${idsCL[LightCyan]}Restarting system...${idsCL[Default]}"
MMSTOP
echo -en "${idsCL[LightCyan]}Pausing for 10secs... ${idsCL[Default]}"
sleep 10s
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
echo
echo -e "${idsCL[LightCyan]}Starting system back up...${idsCL[Default]}"
echo
MMSTART
fi
fi
msg=" |- ${idsCL[Default]}Real WAN Address"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -e "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Green]}${REALWANIP}${idsCL[Default]}"
msg=" |- ${idsCL[Default]}VPN WAN Address"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -e "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Green]}${mmip}${idsCL[Default]}"
### CHECK FOR PLEX MOUNT
echo
msg=" Verifying Plex Media Mount"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Default]}"
if [ ! -f ${PLEX_MOUNT}/mounted ] && [ ! -f ${MMFOLDER}/mm.plexmount.fail2 ]; then
echo -e "${idsCL[Red]}Not Mounted${idsCL[Default]}"
elif [ -f ${MMFOLDER}/mm.plexmount.fail2 ]; then
echo -e "${idsCL[Red]}Not Mounted, running services as chosen previously${idsCL[Default]}"
else
echo -e "${idsCL[Green]}Mounted${idsCL[Default]}"
rm -f ${MMFOLDER}/mm.plexmount.fail*
fi
echo
fi
fi
done
echo
echo
}
GETVPNINFO(){
echo -e "${idsCL[Green]}VPN Connection Info${idsCL[Default]}"
echo
msg="Server Network"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -n "${idsCL[LightCyan]}${msg}${spc}${idsCL[White]}: ${idsCL[Green]}${VPN_SERVER}${idsCL[Default]}"
}
VPNCONFIG(){
privkey=$(/usr/bin/wg genkey)
reqpublickey=$(echo "${privkey}" | /usr/bin/wg pubkey)
reqpublickey="$(php -r 'echo rawurlencode($argv[1]);' -- "$reqpublickey")"
rm -f ${MMFOLDER}/vpninfo
JSON=$(curl https://${VPN_SERVER}:1443/api/v1/setup?public-key=${reqpublickey} --user "${VPN_USER}:${VPN_PASS}" -sk)
echo -e "vpn_publickey=\"$(echo ${JSON} | jq -r '.server_public_key')\"
vpn_server_ipv4=\"$(echo ${JSON} | jq -r '.server_ipv4')\"
vpn_client_ipv4=\"$(echo ${JSON} | jq -r '.client_ipv4')\"
vpn_routes=\"$(echo ${JSON} | jq -r '.routes')\"
vpn_server_address=\"$(echo ${JSON} | jq -r '.vpn_server_address')\"
vpn_server_port=\"$(echo ${JSON} | jq -r '.vpn_server_port')\"
" > ${MMFOLDER}/vpninfo
source ${MMFOLDER}/vpninfo
rm -f /etc/wireguard/${VPN_INTERFACE}.conf
echo -e "[Interface]
PrivateKey = ${privkey}
ListenPort = 55009
MTU = 1390
DNS = 1.1.1.1
Address = ${vpn_client_ipv4}
[Peer]
PublicKey = ${vpn_publickey}
AllowedIPs = 0.0.0.0/0
Endpoint = ${vpn_server_address}:${vpn_server_port}
PersistentKeepalive = 25" >> /etc/wireguard/${VPN_INTERFACE}.conf
}
VPNSPEEDTEST(){
echo -e "${idsCL[Green]}VPN Speedtest Comparison${idsCL[Default]}"
DIVIDER . lightGreen
echo
if [ ! -f ${MMFOLDER}/mm.stop ]; then
echo -en "${idsCL[LightCyan]}This requires all media management services to be stopped, ok (Y/n)? ${idsCL[Default]}"
read -n 1 choice
case "$choice" in
[Nn]) exit 0;; esac
echo
echo -en "${idsCL[LightCyan]}Stopping media management services ... ${idsCL[Default]}"
else
echo -en "${idsCL[LightCyan]}Verifying media management services are stopped ... ${idsCL[Default]}"
fi
MMSTOP >/dev/null 2>&1
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
echo
echo -e "${idsCL[Green]}Running test without VPN${idsCL[Default]}"
DIVIDER . green
# speedtest -s 17462
speedtest
echo
echo -en "${idsCL[LightCyan]}Starting vpn ... ${idsCL[Default]}"
# VPNCONFIG
# "${VPN_START[@]}" >/dev/null 2>&1
mm start vpn >/dev/null 2>&1
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
echo
sleep 5s
echo -e "${idsCL[Green]}Running test with VPN${idsCL[Default]}"
DIVIDER . green
# speedtest -s 17462
speedtest
echo
echo -en "${idsCL[LightCyan]}Stopping vpn ... ${idsCL[Default]}"
# "${VPN_STOP[@]}" >/dev/null 2>&1
MMSTOP >/dev/null 2>&1
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
echo
echo -en "${idsCL[LightCyan]}Would you like to start media management services (y/N)? ${idsCL[Default]}"
read -n 1 choice
echo
case "$choice" in
[Yy]) MMSTART start;;
* ) echo -e "${idsCL[Cyan]}Not starting. To start, run the command '${idsCL[Green]}mm start${idsCL[LightCyan]}'${idsCL[Default]}";echo;echo;;
esac
}
UPDATESERVICE(){
if [ "${1}" != "" ]; then
usrvc=${1}
echo "${usrvc} Update" > ${MMFOLDER}/mm.update.stop
echo "$(date) - Updating Service (${usrvc}) : Starting" >> $logfile
echo
echo -e "${idsCL[Green]}Updating ${usrvc^} Service${idsCL[Default]}"
DIVIDER . lightGreen
if [[ " ${UPDATE_SERVICES[*]} " =~ " ${usrvc} " ]]; then
msg="Stopping service and preparing"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}"
rm -f ${MMFOLDER}/logs/update.${usrvc}
touch ${MMFOLDER}/logs/update.${usrvc}
/bin/systemctl stop ${usrvc} >> ${MMFOLDER}/logs/update.${usrvc}
[ "${usrvc}" == "radarr" ] && [[ " ${SERVICES_CHECK[*]} " =~ " radarr3d " ]] && /bin/systemctl stop radarr3d >> ${MMFOLDER}/logs/update.${usrvc}
tmpfile=/tmp/${usrvc}-update.tar.gz
bak=/opt/Backups/${usrvc}.opt.tgz
bakcfg=/opt/Backups/${usrvc}.cfg.tgz
[[ -f ${bak} ]] && rm -f "${bak}" &>/dev/null
[[ -f ${tmpfile} ]] && rm -f "${tmpfile}" &>/dev/null
[ ! -d /opt/Backups ] && mkdir /opt/Backups
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
if [ "${MM_UPDATE_REPO[${usrvc}]}" != "" ] && [ "${MM_UPDATE_REPO[${usrvc}]}" != "systemd" ]; then
msg="Finding latest update url"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}"
if [[ $BETA_SERVICES == *${usrvc}* ]]; then
latest_release_url=`curl -s https://api.github.com/repos/${MM_UPDATE_REPO[${usrvc}]}/${usrvc}/releases | jq -r '.[0].assets[].browser_download_url' | grep "${MM_UPDATE_REPO_GREP[${usrvc}]}" | cut -d '"' -f 4 | grep -i 'https://'`
echo "$(date) - Updating Service (${usrvc}) : BETA UPDATEURL = ${latest_release_url}" >> $logfile
beta="BETA "
else
latest_release_url=`curl -s https://api.github.com/repos/${MM_UPDATE_REPO[${usrvc}]}/${usrvc}/releases/latest | jq -r '.assets[].browser_download_url' | grep "${MM_UPDATE_REPO_GREP[${usrvc}]}" | cut -d '"' -f 4 | grep -i 'https://'`
echo "$(date) - Updating Service (${usrvc}) : UPDATEURL = ${latest_release_url}" >> $logfile
beta=""
fi
installed_ver=`echo ${latest_release_url} | cut -d/ -f8`
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
msg="Installing version"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -e "${idsCL[White]}${msg}${spc} : ${idsCL[Green]}${installed_ver}${idsCL[Default]}"
msg="Downloading and updating"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}"
if curl --output /dev/null --silent --head --fail $latest_release_url; then
wget -qO ${tmpfile} $latest_release_url >> ${MMFOLDER}/logs/update.${usrvc}
if [[ ( $? -eq 0 ) && ( -f ${tmpfile} ) ]]; then
if [ "${usrvc}" != "bazarr" ]; then
rm -Rf /opt/${usrvc^} >> ${MMFOLDER}/logs/update.${usrvc}
mkdir /opt/${usrvc^} >> ${MMFOLDER}/logs/update.${usrvc}
fi
/bin/tar -cpPf $bak /opt/${usrvc^} >> ${MMFOLDER}/logs/update.${usrvc}
[ "${MM_SERVICE_CFG[${usrvc}]}" != "" ] && [ -d ${MM_SERVICE_CFG[${usrvc}]} ] && /bin/tar -cpPf $bakcfg ${MM_SERVICE_CFG[${usrvc}]} >> ${MMFOLDER}/logs/update.${usrvc}
if [ "${usrvc}" == "ombi" ]; then
/bin/tar xfP ${tmpfile} -C /opt/${usrvc^} >> ${MMFOLDER}/logs/update.${usrvc}
elif [ "${usrvc}" == "bazarr" ]; then
/usr/bin/unzip -oqq ${tmpfile} -d /opt/${usrvc^}
else
/bin/tar xfP ${tmpfile} -C /opt >> ${MMFOLDER}/logs/update.${usrvc}
fi
chown -R user:users /opt/${usrvc^} >> ${MMFOLDER}/logs/update.${usrvc}
fi
fi
rm -f ${tmpfile} &>/dev/null
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
elif [ "${MM_UPDATE_REPO[${usrvc}]}" == "systemd" ]; then
msg="Installing version"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -e "${idsCL[White]}${msg}${spc} : ${idsCL[Green]}${installed_ver}${idsCL[Default]}"
msg="Updating service"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}"
installed_ver=`dpkg -s ${usrvc} | grep Version`
/usr/bin/apt install ${usrvc} >/dev/null 2>&1
/bin/systemctl disable ${usrvc} >/dev/null 2>&1
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
fi
[ ! -f ${MMFOLDER}/vers.info ] && touch ${MMFOLDER}/vers.info
/bin/sed -i /${usrvc}/d ${MMFOLDER}/vers.info
echo "${usrvc}_VER=\"${installed_ver}\"" >> ${MMFOLDER}/vers.info
msg="Starting service back up"
c=0; spc=''; spc1=$((${cw_spc}-${#msg}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}"
OUT=$(ip link | grep "$VPN_INTERFACE") >/dev/null 2>&1
if [ ${#OUT} -ne 0 ] && [ ! -f ${MMFOLDER}/mm.stop ] && [ ! -f ${MMFOLDER}/mm.noservices ] && [[ " ${SERVICES_CHECK[*]} " =~ " ${usrvc} " ]]; then
/bin/systemctl disable ${usrvc} >> ${MMFOLDER}/logs/update.${usrvc}
/bin/systemctl start ${usrvc} >> ${MMFOLDER}/logs/update.${usrvc}
[ "${usrvc}" == "radarr" ] && [[ " ${SERVICES_CHECK[*]} " =~ " radarr3d " ]] && /bin/systemctl start radarr3d >> ${MMFOLDER}/logs/update.${usrvc}
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
else
echo -e "${idsCL[LightCyan]}Skipping${idsCL[Default]}"
fi
rm -f ${MMFOLDER}/mm.update.stop
echo "$(date) - Updating Service (${usrvc}) : Completed" >> $logfile
echo -e "${idsCL[Green]}${usrvc^} Service has been updated${idsCL[Default]}"
else
echo -e "${idsCL[Red]}Not currently available to update${idsCL[Default]}"
fi
else
echo -e "${idsCL[Red]}No service selected for update${idsCL[Default]}"
fi
}
SHOWSRVCUPDATES(){
echo -e "${idsCL[LightCyan]}Latest Update URLs Available${idsCL[Default]}"
DIVIDER . lightGreen
for usrvc in "${UPDATE_SERVICES[@]}"; do
if [ "${MM_UPDATE_REPO[${usrvc}]}" == "systemd" ]; then
repo_ver=`dpkg -s ${usrvc} | grep Version`
echo -e "${idsCL[LightCyan]}${MM_SERVICES[${usrvc}]}:${idsCL[Default]} Apt Repo Controller"
echo -e "${idsCL[LightGreen]}Repo Release : ${repo_ver}${idsCL[Default]}"
else
beta_url=`curl -s https://api.github.com/repos/${MM_UPDATE_REPO[${usrvc}]}/${usrvc}/releases | jq -r '.[0].assets[].browser_download_url' | grep "${MM_UPDATE_REPO_GREP[${usrvc}]}" | cut -d '"' -f 4 | grep -i 'https://'`
# beta_url=`curl -s https://api.github.com/repos/${MM_UPDATE_REPO[${usrvc}]}/${usrvc}/releases | jq -r '.[0].assets[].browser_download_url' | grep "${MM_UPDATE_REPO_GREP[${usrvc}]}" | cut -d '"' -f 4 | grep -i 'https://' | cut -d/ -f8`
beta_ver=`echo ${beta_url} | cut -d/ -f8`
latest_url=`curl -s https://api.github.com/repos/${MM_UPDATE_REPO[${usrvc}]}/${usrvc}/releases/latest | jq -r '.assets[].browser_download_url' | grep "${MM_UPDATE_REPO_GREP[${usrvc}]}" | cut -d '"' -f 4 | grep -i 'https://'`
latest_ver=`echo ${latest_url} | cut -d/ -f8`
[[ $BETA_SERVICES == *${usrvc}* ]] && cur='Beta' || cur='Stable'
echo -e "${idsCL[LightCyan]}${MM_SERVICES[${usrvc}]}:${idsCL[Default]} ${cur} Selected"
echo -e "${idsCL[LightGreen]}Stable Release : ${latest_ver}${idsCL[Default]}"
echo -e "${idsCL[LightGreen]}Beta Release : ${beta_ver}${idsCL[Default]}"
fi
cver=${usrvc}_VER
echo -e "${idsCL[LightCyan]}Current vers. : ${!cver}${idsCL[Default]}"
# echo
# echo ${latest_url}
# echo ${beta_url}
echo
echo
done
}
MMSETUP(){
echo -e "${idsCL[Green]}Media Manager Setup Scripts${idsCL[Default]}"
echo
MMSTOP
echo -en "${idsCL[LightCyan]}Would you like to configure the hostname and network settings (y/N)? ${idsCL[Default]}"
read -n 1 choice
case "$choice" in
[Yy])
echo
echo -en "${idsCL[LightYellow]}Hostname [Default='${idsCL[White]}Media-Manager${idsCL[LightYellow]}']: ${idsCL[Default]}"
read -e HOSTNAME
[ "${HOSTNAME}" == "" ] && HOSTNAME="Media-Manager"
echo
REPIP=`hostname -I`
REPIP=${REPIP/ /}
echo -en "${idsCL[LightYellow]}IP Address [Current='${idsCL[White]}${REPIP}${idsCL[LightYellow]}']: ${idsCL[Default]}"
read -e IPADDRESS
[ "${IPADDRESS}" == "" ] && IPADDRESS=${REPIP}
echo
REPSUB=`/sbin/ifconfig eth0 | awk '/netmask/{ print $4;} '`
echo -en "${idsCL[LightYellow]}Subnet Mask [Current='${idsCL[White]}${REPSUB}${idsCL[LightYellow]}']: ${idsCL[Default]}"
read -e SUBNET
[ "${SUBNET}" == "" ] && SUBNET=${REPSUB}
echo
REPGW=`route -n | grep 'UG[ \t]' | awk '{print $2}'`
echo -en "${idsCL[LightYellow]}Gateway IP [Current='${idsCL[White]}${REPGW}${idsCL[LightYellow]}']: ${idsCL[Default]}"
read -e GATEWAY
[ "${GATEWAY}" == "" ] && GATEWAY=${REPGW}
echo
REPPRIDNS=`resolvectl -i eth0 dns | awk '/eth0/{ print $4;} '`
echo -en "${idsCL[LightYellow]}Primary DNS [Current='${idsCL[White]}${REPPRIDNS}${idsCL[LightYellow]}']: ${idsCL[Default]}"
read -e PRIDNS
[ "${PRIDNS}" == "" ] && PRIDNS=${REPPRIDNS}
echo
REPSECDNS=`resolvectl -i eth0 dns | awk '/eth0/{ print $5;} '`
echo -en "${idsCL[LightYellow]}Secondary DNS (optional) [Current='${idsCL[White]}${REPSECDNS}${idsCL[LightYellow]}']: ${idsCL[Default]}"
read -e SECDNS
[ "${SECDNS}" == "" ] && SECDNS=${REPSECDNS}
echo
echo
echo "Confirm settings:"
echo -e "${idsCL[LightCyan]}Hostname : ${idsCL[Green]}${HOSTNAME}${idsCL[Default]}"
echo -e "${idsCL[LightCyan]}IP Address : ${idsCL[Green]}${IPADDRESS}${idsCL[Default]}"
echo -e "${idsCL[LightCyan]}Subnet Mask : ${idsCL[Green]}${SUBNET}${idsCL[Default]}"
echo -e "${idsCL[LightCyan]}Gateway IP : ${idsCL[Green]}${GATEWAY}${idsCL[Default]}"
echo -e "${idsCL[LightCyan]}Primary DNS : ${idsCL[Green]}${PRIDNS}${idsCL[Default]}"
echo -e "${idsCL[LightCyan]}Secondary DNS : ${idsCL[Green]}${SECDNS}${idsCL[Default]}"
echo
echo -en "${idsCL[LightCyan]}Is this correct (Y/n)? ${idsCL[Default]}"
read -n 1 choice
case "$choice" in
[Nn]) MMSETUP; exit 0;;
esac
echo -en "${idsCL[LightCyan]}Making configuration changes ... ${idsCL[Default]}"
sudo hostnamectl set-hostname ${HOSTNAME} >/dev/null 2>&1
sudo sed -i '/127.0.1.1/d' /etc/hosts >/dev/null 2>&1
echo "127.0.1.1 ${HOSTNAME} mm" >> /etc/hosts
NEWSUB=`IPprefix_by_netmask ${SUBNET}`
REPSUB=`IPprefix_by_netmask ${REPSUB}`
sed -i "s/${REPIP}\/${REPSUB}/${IPADDRESS}\/${NEWSUB}/g" /etc/netplan/eth0.yaml >/dev/null 2>&1
sed -i "s/${REPIP}\/${REPSUB}/${IPADDRESS}\/${NEWSUB}/g" /etc/netplan/eth0.yaml >/dev/null 2>&1
[ "${SECDNS}" != "" ] && DNS="${PRIDNS},${SECDNS}" || DNS="${PRIDNS}"
[ "${REPSECDNS}" != "" ] && REPDNS="${REPPRIDNS},${REPSECDNS}" || REPDNS="${REPPRIDNS}"
sed -i "s/${REPDNS}/${DNS}/g" /etc/netplan/eth0.yaml >/dev/null 2>&1
sed -i "s/${REPGW}/${GATEWAY}/g" /etc/netplan/eth0.yaml >/dev/null 2>&1
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
echo
if [ "$(hostname)" != "${HOSTNAME}" ] || [ "$REPIP" != "$IPADDRESS" ] || [ "$REPSUB" != "$NEWSUB" ] || [ "$REPGW" != "$GATEWAY" ] || [ "$REPDNS" != "$DNS" ]; then
NETCHNG=true
else
NETCHNG=false
fi
;;
esac
echo
echo -en "${idsCL[LightCyan]}This will reset the MM app config, do you wish to continue (y/N)? ${idsCL[Default]}"
read -n 1 choice
case "$choice" in
[Yy])
echo
echo -en "${idsCL[LightCyan]}Resetting config ... ${idsCL[Default]}"
sed -i '/SERVICESCHECK/d' ${MMFOLDER}/settings.conf
sed -i '/PUSHOVER_/d' ${MMFOLDER}/settings.conf
sed -i '/EMAIL_NOTICE/d' ${MMFOLDER}/settings.conf
sed -i '/OPTIONAL/d' ${MMFOLDER}/settings.conf
sed -i '/^$/d' ${MMFOLDER}/settings.conf
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
if [ "${SERVICESCHECK}" != "" ]; then
echo
srvcs2=${SERVICESCHECK/snap.tautulli.tautulli/tautulli}
echo -e "${idsCL[LightCyan]}Run previous media management services${idsCL[Default]}"
echo -en "${idsCL[LightYellow]}['${idsCL[White]}${srvcs2}${idsCL[LightYellow]}']${idsCL[LightCyan]} (Y/n)? ${idsCL[Default]}"
read -n 1 choice
case "$choice" in
[Nn]) SERVICESCHECK="";;
* ) srvcs=${srvcs2};;esac
fi
if [ "${SERVICESCHECK}" == "" ] && [ "${srvcs}" == "" ]; then
export NEWT_COLORS=''
srvcs=$(whiptail --title "Media Manager Service Selector" --checklist --separate-output \
"\nPlease select which services to enable for this system:" 0 70 16 \
"bazarr" "Bazarr (Subtitle Manager)" ON \
"jackett" "Jackett (Torrent Indexing Manager) " ON \
"lidarr" "Lidarr (Music Manager)" OFF \
"ombi" "Ombi (Download Requester)" ON \
"qbittorrent" "qBittorrent (Torrent Downloader)" ON \
"radarr" "Radarr (Movie Manager)" ON \
"radarr3d" "Radarr3D (3D Movie Manager)" OFF \
"sonarr" "Sonarr (TV Show Manager)" ON \
"tautulli" "Tautulli (Plex Stats)" ON 3>&1 1>&2 2>&3)
exitstatus=$?; srvcs="$(join_by , $srvcs)";
fi
if [ "${srvcs}" != "" ]; then
echo
echo
echo -e "${idsCL[LightCyan]}Selected services: ${idsCL[Green]}${srvcs}${idsCL[Default]}"
srvcs=${srvcs/tautulli/snap.tautulli.tautulli}
echo -e "\nSERVICESCHECK=${srvcs}\n\n#[OPTIONAL]" >> ${MMFOLDER}/settings.conf
echo
else
echo -e "\n# SERVICESCHECK=\n\n#[OPTIONAL]" >> ${MMFOLDER}/settings.conf
fi
echo
echo -e "${idsCL[LightCyan]}You may choose to setup notifications by either SMTP and/or Pushover:${idsCL[Default]}"
if [ "${EMAIL_NOTICE}" != "" ]; then
echo -en "${idsCL[LightYellow]}Use previous email address [${idsCL[White]}${EMAIL_NOTICE}${idsCL[LightYellow]}] (Y/n)? ${idsCL[Default]}"
read -n 1 choice
case "$choice" in
[Nn]) EMAIL_NOTICE="";;esac
fi
if [ "${EMAIL_NOTICE}" == "" ]; then
echo; echo -e -n "${idsCL[LightYellow]}(optional) Email notification address: ${idsCL[Default]}"
read -e EMAIL_NOTICE
fi
[ "${EMAIL_NOTICE}" != "" ] && echo "EMAIL_NOTICE=${EMAIL_NOTICE}" >> ${MMFOLDER}/settings.conf || echo "# EMAIL_NOTICE=email@address.com" >> ${MMFOLDER}/settings.conf
echo
if [ "${PUSHOVER_APP_TOKEN}" != "" ]; then
echo -en "${idsCL[LightYellow]}Use previous Pushover App Token [${idsCL[White]}${PUSHOVER_APP_TOKEN}${idsCL[LightYellow]}] (Y/n)? ${idsCL[Default]}"
read -n 1 choice
case "$choice" in
[Nn]) PUSHOVER_APP_TOKEN="";;esac
fi
if [ "${PUSHOVER_APP_TOKEN}" == "" ]; then
echo; echo -e -n "${idsCL[LightYellow]}(optional) Pushover App Token: ${idsCL[Default]}"
read -e PUSHOVER_APP_TOKEN
fi
[ "${PUSHOVER_APP_TOKEN}" != "" ] && echo "PUSHOVER_APP_TOKEN=\"${PUSHOVER_APP_TOKEN}\"" >> ${MMFOLDER}/settings.conf || echo "# PUSHOVER_APP_TOKEN=\"\"" >> ${MMFOLDER}/settings.conf
echo
if [ "${PUSHOVER_APP_TOKEN}" != "" ]; then
if [ "${PUSHOVER_USER_TOKEN}" != "" ]; then
echo -en "${idsCL[LightYellow]}Use previous Pushover User Token [${idsCL[White]}${PUSHOVER_USER_TOKEN}${idsCL[LightYellow]}] (Y/n)? ${idsCL[Default]}"
read -n 1 choice
case "$choice" in
[Nn]) PUSHOVER_USER_TOKEN="";;esac
fi
if [ "${PUSHOVER_USER_TOKEN}" == "" ]; then
echo; echo -e -n "${idsCL[LightYellow]}(optional) Pushover User Token: ${idsCL[Default]}"
read -e PUSHOVER_USER_TOKEN
fi
[ "${PUSHOVER_USER_TOKEN}" != "" ] && echo "PUSHOVER_USER_TOKEN=\"${PUSHOVER_USER_TOKEN}\"" >> ${MMFOLDER}/settings.conf || echo "# PUSHOVER_USER_TOKEN=\"\"" >> ${MMFOLDER}/settings.conf
echo
else
echo "# PUSHOVER_USER_TOKEN=\"\"" >> ${MMFOLDER}/settings.conf
fi
source /opt/idssys/mediamanager/settings.conf
IFS=,; SERVICES_CHECK=(${SERVICESCHECK}); unset IFS
echo
echo -en "${idsCL[LightCyan]}Would you like to update media management services (y/N)? ${idsCL[Default]}"
read -n 1 choice
case "$choice" in
[Yy]) for usrvc in "${UPDATE_SERVICES[@]}"; do
[ "${MM_UPDATE_REPO[${usrvc}]}" != "" ] && UPDATESERVICE ${usrvc}
done;;
* ) tmp='';;
esac
CNFG=true
;;
*) tmp='';;
esac
echo
echo -en "${idsCL[LightCyan]}Would you like to setup an SSH mount for Plex Media, requires linux Plex server (y/N)? ${idsCL[Default]}"
read -n 1 choice
case "$choice" in
[Yy])
if [ "$(dpkg-query -W --showformat='${Status}\n' grepcidr | grep "install ok installed")" == "" ]; then
echo
echo -en "${idsCL[LightCyan]}Installing requirements ... ${idsCL[Default]}"
sudo apt -y install sshfs >/dev/null 2>&1
apt install sshfs -y
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
fi
echo
echo -en "${idsCL[LightYellow]}Plex Server IP/Hostname: ${idsCL[Default]}"
read -e PLEXSERVER
echo
echo -en "${idsCL[LightYellow]}File Access Username: ${idsCL[Default]}"
read -e PLEXUSER
echo
echo -en "${idsCL[LightYellow]}File Access Password: ${idsCL[Default]}"
read -e PLEXPASSWORD
echo
echo -en "${idsCL[LightYellow]}Plex Media Folder location [ ex. /srv/media-data ] : ${idsCL[Default]}"
read -e PLEXFOLDER
echo
echo -en "${idsCL[LightCyan]}Verifying local SSH key, otherwise generating one ... ${idsCL[Default]}"
if [ ! -f /root/.ssh/id_rsa.pub ]; then
echo
echo -e "${idsCL[LightYellow]}Generating new key, enter no password, and keep defaults: ${idsCL[Default]}"
echo
sudo ssh-keygen -t rsa
fi
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
sudo ssh-copy-id ${PLEXUSER}@${PLEXSERVER}
echo
echo -e "${idsCL[LightCyan]}Configuring and mounting ... ${idsCL[Default]}"
if [ "${PLEXSERVER}" != "" ] && [ "${PLEXUSER}" != "" ] && [ "${PLEXPASSWORD}" != "" ] && [ "${PLEXFOLDER}" != "" ]; then
sudo sed -i '/\/mnt\/plex-media/d' /etc/fstab
sudo echo "sshfs#${PLEXUSER}@${PLEXSERVER}:${PLEXFOLDER} /mnt/plex-media fuse ,_netdev,reconnect,allow_other,umask=002,gid=100,uid=1000,IdentityFile=/root/.ssh/id_rsa 0 0" >> /etc/fstab
umount /mnt/plex-media >/dev/null 2>&1
mount -a >/dev/null 2>&1
sleep 2s
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
echo
echo -e "${idsCL[LightCyan]}Verify the following folder structure looks correct:${idsCL[Default]}"
ls -l /mnt/plex-media
echo -en "${idsCL[LightCyan]}Is the above folder your plex media (Y/n)? ${idsCL[Default]}"
read -n 1 choice
case "$choice" in
[Nn]) echo -e "${idsCL[Magenta]}Missing info, configure mounting to '${idsCL[White]}/mnt/plex-media${idsCL[Magenta]}' manually${idsCL[Default]}";;
* )
echo
echo -en "${idsCL[LightCyan]}Adding the file 'mounted', to the plex media folder, this file must exist for the system to know its mounted properly ... ${idsCL[Default]}"
touch /mnt/plex-media/mounted
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
;;
esac
echo -e "${idsCL[Green]}Your Plex Media is now mounted, and will automatically remount at startup.${idsCL[Default]}"
else
echo
echo -e "${idsCL[Magenta]}Missing info, configure mounting to '${idsCL[White]}/mnt/plex-media${idsCL[Magenta]}' manually${idsCL[Default]}"
fi
;;
* )
echo -e "${idsCL[Magenta]}Make sure to manually configure mounting to '${idsCL[White]}/mnt/plex-media${idsCL[Magenta]}' ${idsCL[Default]}"
;;
esac
if [ "${NETCHNG}" == "true" ] || [ "${CNFG}" == "true" ]; then
if [ "${NETCHNG}" == "true" ]; then
echo
echo -en "${idsCL[LightCyan]}A reboot is recommended, would you like to reboot now (Y/n)? ${idsCL[Default]}"
read -n 1 choice
case "$choice" in
[Nn]) echo;;
* ) reboot;;
esac
else
echo
echo -en "${idsCL[LightCyan]}Would you like to start media management services (y/N)? ${idsCL[Default]}"
read -n 1 choice
echo
case "$choice" in
[Yy]) MMSTART start;;
* ) echo -e "${idsCL[Cyan]}Not starting. To start, run the command '${idsCL[Green]}mm start${idsCL[LightCyan]}'${idsCL[Default]}";echo;echo;;
esac
fi
fi
echo
echo -e "${idsCL[LightCyan]}The following are Media Manager (mm) usage commands:${idsCL[Default]}"
DIVIDER . lightGreen
USAGE
# cat ${MMFOLDER}/settings.conf
}
USAGE(){
echo -e " Usage: ${idsCL[LightCyan]}mm {option}"
echo
echo -e " ${idsCL[Yellow]}start ${idsCL[Default]}--> Start services"
echo -e " ${idsCL[Yellow]}stop ${idsCL[Default]}--> Stop services"
echo -e " ${idsCL[Yellow]}restart ${idsCL[Default]}--> Restart services"
echo -e " ${idsCL[Yellow]}update-allservices ${idsCL[Default]}--> Update all services"
echo -e " ${idsCL[Yellow]}update-service <service> ${idsCL[Default]}--> Update specified service"
echo -e " ${idsCL[Yellow]}update-list ${idsCL[Default]}--> List available releases"
echo
echo -e " ${idsCL[Yellow]}status ${idsCL[Default]}--> Check Status on VPN and Media Services"
echo -e " ${idsCL[Yellow]}vpninfo ${idsCL[Default]}--> Show VPN Info"
echo -e " ${idsCL[Yellow]}speedtest ${idsCL[Default]}--> Run Speedtest Comparison with VPN"
echo
echo -e " ${idsCL[Yellow]}check ${idsCL[Default]}--> Crontab job for maintaining system (run */1 min)"
echo -e " ${idsCL[Yellow]}clear ${idsCL[Default]}--> Clear all tmp files"
echo -e " ${idsCL[Yellow]}update ${idsCL[Default]}--> Check for and install new updates"
echo -e " ${idsCL[Yellow]}setup ${idsCL[Default]}--> Setup/Reconfigure Settings (Network/MM Config/SSHFS Mount)"
echo ""
echo ""
}
if [ ! -f ${MMFOLDER}/mm.update.stop ]; then
[ "${2}" == "q" ] && [ "${action}" == "check" ] && echo -e "\n\n$(date) - Starting log #############################\n" >> ${outputlogfile}
if [ -f ${MMFOLDER}/mm.stop ]; then
stoppedsince=`date +%s`-$(stat -c %Y ${MMFOLDER}/mm.stop)
stopmsg="${idsCL[Yellow]}The system has currently been stopped for '$(SHOW_TIME ${stoppedsince})'.\n${idsCL[LightCyan]}(You'll need to run \"mm start\" to start services again)${idsCL[Default]}"
fi
echo "HERE: ${action} - ${1} - ${2}"
case $action in
start | check)
if [ -f ${MMFOLDER}/mm.stop ] && [ "${action}" == "check" ]; then
echo -e "${stopmsg}"
# [ $VERBOSE = true ] && echo "$(date) - Not checking, system has been stopped" >> $logfile
echo
exit 1
elif [ "${VPN_USER}" == "" ] || [ "${VPN_PASS}" == "" ] || [ "${VPN_SERVER}" == "" ]; then
echo "$(date) - VPN User Info Missing" >> $logfile
echo -e "${idsCL[Red]}VPN User Info Missing${idsCL[Default]}"
echo
exit 1
elif [ ! -f ${MMFOLDER}/mm.stop ] && [ -f ${MMFOLDER}/mm.stop.checking ]; then
checkstopped=`expr $(date +%s) - $(stat -c %Y ${MMFOLDER}/mm.stop.checking)`
if [ $checkstopped -gt 900 ]; then
rm -f ${MMFOLDER}/mm.stop*
echo "$(date) - Bash script stuck in stop mode after doing a check, will restart" >> $logfile
SENDNOTICE "MM Script Error" "Bash script stuck in check mode, restarting system"
MMSTOP
rm -f ${MMFOLDER}/mm.stop*
getStuckMM=$(pgrep -f '/mm')
for i in $getStuckMM; do
/bin/kill $i
done
else
exit 1
fi
fi
[ "${action}" != "status" ] && touch ${MMFOLDER}/mm.stop.checking
[ "${2}" == "vpn" ] && touch ${MMFOLDER}/mm.noservices
[ ! -f ${MMFOLDER}/mm.stop ] && MMSTART ${action} ${2}
[ "${action}" != "status" ] && rm -f ${MMFOLDER}/mm.stop.checking
;;
stop)
MMSTOP
;;
restart)
if [ -f ${MMFOLDER}/mm.stop ]; then
echo -e "${stopmsg}"
echo
exit 1
fi
[ -f ${MMFOLDER}/mm.noservices ] && rv="vpn" || rv=""
echo "HERE: ${rv}"
MMSTOP
echo -en "${idsCL[LightCyan]}Pausing for 30s to get new VPN WAN IP, or press any key to continue with possibly getting a new address ... ${idsCL[Default]}"
read -et 30 -n 1
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
MMSTART ${action} ${rv}
;;
status)
if [ -f ${MMFOLDER}/mm.stop ]; then
echo -e "${stopmsg}"
echo
fi
MMSTATUS ${2}
;;
setup)
MMSETUP
;;
vpninfo)
GETVPNINFO
;;
speedtest)
VPNSPEEDTEST
;;
update);;
update-service)
UPDATESERVICE ${2}
;;
update-allservices)
# for usrvc in "${SERVICES_CHECK[@]}"; do
for usrvc in "${UPDATE_SERVICES[@]}"; do
[ "${MM_UPDATE_REPO[${usrvc}]}" != "" ] && UPDATESERVICE ${usrvc}
done
;;
update-list)
SHOWSRVCUPDATES
;;
clear) echo -e "${idsCL[LightCyan]}Clearing all mm.* files ${idsCL[Default]}"
echo
;;
*) USAGE;;
esac
else
echo -e "${idsCL[Yellow]}Part of the system is currently updating, please try again shortly.\nUpdates usually only take 1-3mins.${idsCL[Default]}"
echo
fi
exit 0