diff --git a/defaults.inc b/defaults.inc index 5b49100..95a5bde 100755 --- a/defaults.inc +++ b/defaults.inc @@ -2,6 +2,7 @@ VERS='2.02232023' +MMFOLDER="/opt/idssys/mediamanager" MMSRV=$(/sbin/ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1) noheader=' check noup stop ' diff --git a/mm-scripts.sh b/mm-scripts.sh index 8cde528..14c9a4a 100755 --- a/mm-scripts.sh +++ b/mm-scripts.sh @@ -6,14 +6,14 @@ action="$1" VERBOSE=true if [ -f /opt/idssys/settings/mediamanager.conf ]; then - mv /opt/idssys/settings/mediamanager.conf /opt/idssys/mediamanager/settings.conf + 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 +source ${MMFOLDER}/settings.conf +source ${MMFOLDER}/defaults.inc if [ "${action}" != "check" ] && [ "${action}" != "update" ] && [ "${2}" != "noup" ]; then echo "" @@ -21,7 +21,7 @@ if [ "${action}" != "check" ] && [ "${action}" != "update" ] && [ "${2}" != "nou DIVIDER . lightGreen fi -logfile=/opt/idssys/mediamanager/logfile +logfile=${MMFOLDER}/logfile touch $logfile @@ -35,13 +35,13 @@ MMSTART(){ if [ "${1}" = "start" ] || [ "${1}" = "restart" ] || [ "${1}" = "" ]; then GOFORSTART=true; - elif [ "${1}" = "check" ] && [ "$(ip link | grep "wg-torguard")" = "" ] && [ ! -f /opt/idssys/mediamanager/mm.vpn* ]; then + elif [ "${1}" = "check" ] && [ "$(ip link | grep "wg-torguard")" = "" ] && [ ! -f ${MMFOLDER}/mm.vpn* ]; then GOFORSTART=true; [ "${EMAIL_NOTICE}" != "" ] && echo "" | mail -s "Starting Media Management Systems" ${EMAIL_NOTICE} echo "$(date) - Starting Media Management system - (wireguard not running)" >> $logfile echo -e "${idsCL[LightCyan]}Fresh startup${idsCL[Default]}" - elif [ "${1}" = "check" ] && [ "$(ip link | grep "wg-torguard")" = "" ] && [ -f /opt/idssys/mediamanager/mm.vpn* ]; then + elif [ "${1}" = "check" ] && [ "$(ip link | grep "wg-torguard")" = "" ] && [ -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]}" @@ -81,9 +81,9 @@ AllowedIPs = 0.0.0.0/0 Endpoint = ${vpnaddress}:${vpnport} PersistentKeepalive = 25" >> /etc/wireguard/wg-torguard.conf - rm -f /opt/idssys/mediamanager/vpninfo.* - echo -e "$publickey" >> /opt/idssys/mediamanager/vpninfo.publickey - echo -e "$vpnaddress" >> /opt/idssys/mediamanager/vpninfo.address + rm -f ${MMFOLDER}/vpninfo.* + echo -e "$publickey" >> ${MMFOLDER}/vpninfo.publickey + echo -e "$vpnaddress" >> ${MMFOLDER}/vpninfo.address echo -e "${idsCL[Green]} done${idsCL[Default]}" @@ -103,21 +103,21 @@ PersistentKeepalive = 25" >> /etc/wireguard/wg-torguard.conf OUT=$(ip link | grep "$VPN_INTERFACE") if [ ${#OUT} = 0 ]; then - if [ -f /opt/idssys/mediamanager/mm.vpn.fail ]; 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 [ "${EMAIL_NOTICE}" != "" ] && echo "2nd Failure, will try and restart the VPN" | mail -s "VPN Failure" ${EMAIL_NOTICE} - touch /opt/idssys/mediamanager/mm.vpn.fail2 - rm -f /opt/idssys/mediamanager/mm.vpn.fail + touch ${MMFOLDER}/mm.vpn.fail2 + rm -f ${MMFOLDER}/mm.vpn.fail MMSTOP sleep 5m "${VPN_START[@]}" >/dev/null 2>&1 & sleep 30s - rm -f /opt/idssys/mediamanager/mm.stop* - rm -f /opt/idssys/mediamanager/mm.checking + rm -f ${MMFOLDER}/mm.stop* + rm -f ${MMFOLDER}/mm.checking [ $VERBOSE = true ] && DEBUGLOGSTOP exit 1 - elif [ -f /opt/idssys/mediamanager/mm.vpn.fail2 ]; then + elif [ -f ${MMFOLDER}/mm.vpn.fail2 ]; then echo "$(date) - VPN Failure #3 - Shutting down Media Management systems" >> $logfile [ "${EMAIL_NOTICE}" != "" ] && echo "Final Failure, shutting down system" | mail -s "VPN Failure" ${EMAIL_NOTICE} MMSTOP @@ -127,13 +127,13 @@ PersistentKeepalive = 25" >> /etc/wireguard/wg-torguard.conf echo -e "${idsCL[Red]} failed, restarting system${idsCL[Default]}" echo "$(date) - VPN Failure #1 - restarting system" >> $logfile [ "${EMAIL_NOTICE}" != "" ] && echo "1st Failure, will try and restart the VPN" | mail -s "VPN Failure" ${EMAIL_NOTICE} - touch /opt/idssys/mediamanager/mm.vpn.fail + touch ${MMFOLDER}/mm.vpn.fail MMSTOP sleep 30s "${VPN_START[@]}" >/dev/null 2>&1 & sleep 30s - rm -f /opt/idssys/mediamanager/mm.stop* - rm -f /opt/idssys/mediamanager/mm.checking + rm -f ${MMFOLDER}/mm.stop* + rm -f ${MMFOLDER}/mm.checking [ $VERBOSE = true ] && DEBUGLOGSTOP exit 1 fi @@ -154,7 +154,7 @@ PersistentKeepalive = 25" >> /etc/wireguard/wg-torguard.conf /bin/ip route add default dev wg-torguard /bin/ip route add 0.0.0.0/1 dev wg-torguard /bin/ip route add 128.0.0.0/1 dev wg-torguard - /bin/ip route add $(cat /opt/idssys/mediamanager/vpninfo.address)/32 via 10.10.0.1 dev eth0 + /bin/ip route add $(cat ${MMFOLDER}/vpninfo.address)/32 via 10.10.0.1 dev eth0 echo -e "${idsCL[Green]} done${idsCL[Default]}" zerogw=$(ip route show | grep 0.0.0.0 | cut -d' ' -f 3) @@ -173,12 +173,12 @@ PersistentKeepalive = 25" >> /etc/wireguard/wg-torguard.conf echo -e "${idsCL[Red]}Could not confirm default routes, restarting system!${idsCL[Default]}" echo "$(date) - Could not confirm default routes, restarting system" >> $logfile [ "${EMAIL_NOTICE}" != "" ] && echo "Default route failure, will try and restart the VPN" | mail -s "Network Failure" ${EMAIL_NOTICE} - touch /opt/idssys/mediamanager/mm.vpn.fail + touch ${MMFOLDER}/mm.vpn.fail MMSTOP sleep 2m "${VPN_START[@]}" >/dev/null 2>&1 & sleep 1m - rm -f /opt/idssys/mediamanager/mm.stop* + rm -f ${MMFOLDER}/mm.stop* [ $VERBOSE = true ] && DEBUGLOGSTOP exit 1 fi @@ -203,12 +203,12 @@ PersistentKeepalive = 25" >> /etc/wireguard/wg-torguard.conf echo "$(date) - Cannot get an WAN address, restarting system! scip(${scip}) mmip(${mmip})" >> $logfile [ $VERBOSE = true ] && echo "$(date) - Default Routes: zerogw(${zerogw}) defaultgw(${defaultgw})" >> $logfile [ "${EMAIL_NOTICE}" != "" ] && echo "Cannot get an WAN address, will try and restart the VPN" | mail -s "Network Failure" ${EMAIL_NOTICE} - touch /opt/idssys/mediamanager/mm.vpn.fail + touch ${MMFOLDER}/mm.vpn.fail MMSTOP sleep 2m "${VPN_START[@]}" >/dev/null 2>&1 & sleep 1m - rm -f /opt/idssys/mediamanager/mm.stop* + rm -f ${MMFOLDER}/mm.stop* [ $VERBOSE = true ] && DEBUGLOGSTOP exit 1 fi @@ -223,12 +223,12 @@ PersistentKeepalive = 25" >> /etc/wireguard/wg-torguard.conf echo "$(date) - Traffic is not routing through VPN, restarting system! scip(${scip}) mmip(${mmip})" >> $logfile [ $VERBOSE = true ] && echo "$(date) - Default Routes: zerogw(${zerogw}) defaultgw(${defaultgw})" >> $logfile [ "${EMAIL_NOTICE}" != "" ] && echo "Traffic route failure, will try and restart the VPN" | mail -s "Network Failure" ${EMAIL_NOTICE} - touch /opt/idssys/mediamanager/mm.vpn.fail + touch ${MMFOLDER}/mm.vpn.fail MMSTOP sleep 2m "${VPN_START[@]}" >/dev/null 2>&1 & sleep 1m - rm -f /opt/idssys/mediamanager/mm.stop* + rm -f ${MMFOLDER}/mm.stop* [ $VERBOSE = true ] && DEBUGLOGSTOP exit 1 fi @@ -243,11 +243,11 @@ PersistentKeepalive = 25" >> /etc/wireguard/wg-torguard.conf files=$(shopt -s nullglob dotglob; echo ${PLEX_MOUNT}/*) if (( ! ${#files} )); then - if [ -f /opt/idssys/mediamanager/mm.plexmount.fail ]; then - fd=$(( `date +%s` - `stat -L --format %Y /opt/idssys/mediamanager/mm.plexmount.fail` )) + 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 - rm -f /opt/idssys/mediamanager/mm.plexmount.fail + rm -f ${MMFOLDER}/mm.plexmount.fail else PMFS=false fi @@ -257,7 +257,7 @@ PersistentKeepalive = 25" >> /etc/wireguard/wg-torguard.conf if [ "${EMAIL_NOTICE}" != "" ] && [ ${PMFS} = true ]; then echo "Could not mount the Plex Media Folder" | mail -s "Plex Media Not Mounted" ${EMAIL_NOTICE} - touch /opt/idssys/mediamanager/mm.plexmount.fail + touch ${MMFOLDER}/mm.plexmount.fail echo "$(date) - Plex Media could not be mounted" >> $logfile fi else @@ -281,12 +281,12 @@ PersistentKeepalive = 25" >> /etc/wireguard/wg-torguard.conf #echo -e "${idsCL[LightCyan]}It may take a couple of minutes before all services fully start${idsCL[Default]}" MMSTATUS fi - if [ -f /opt/idssys/mediamanager/mm.vpn.fail* ]; then + if [ -f ${MMFOLDER}/mm.vpn.fail* ]; then echo "$(date) - VPN issue has been fixed" >> $logfile [ "${EMAIL_NOTICE}" != "" ] && echo "VPN issue has been fixed" | mail -s "Services Restored" ${EMAIL_NOTICE} - rm -f /opt/idssys/mediamanager/mm.vpn.fail* + rm -f ${MMFOLDER}/mm.vpn.fail* fi - rm -f /opt/idssys/mediamanager/mm.stop* + 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 @@ -301,14 +301,14 @@ PersistentKeepalive = 25" >> /etc/wireguard/wg-torguard.conf } MMSTOP(){ - if [ -f /opt/idssys/mediamanager/mm.stop ]; then + 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]}" echo fi echo - touch /opt/idssys/mediamanager/mm.stop + touch ${MMFOLDER}/mm.stop echo -en "${idsCL[LightCyan]}Stopping Services... ${idsCL[Default]}" #for srvc in "${SERVICES_CHECK[@]}"; do for i in "${!NM_SERVICES[@]}"; do @@ -352,8 +352,8 @@ MMSTOP(){ fi done - [ "$(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 $(cat /opt/idssys/mediamanager/vpninfo.address) - rm -f /opt/idssys/mediamanager/vpninfo.* + [ "$(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 $(cat ${MMFOLDER}/vpninfo.address) + rm -f ${MMFOLDER}/vpninfo.* [ $VERBOSE = true ] && echo "$(date) - Added default route back in" >> $logfile echo -e "${idsCL[Green]} done${idsCL[Default]}" @@ -414,7 +414,7 @@ MMSTATUS(){ echo -e "${idsCL[Green]}$defaultgw${idsCL[Default]}" else echo -e "${idsCL[Red]}Not correct${idsCL[Default]}" - if [ ! -f /opt/idssys/mediamanager/mm.stop ]; then + if [ ! -f ${MMFOLDER}/mm.stop ]; then echo echo -e "${idsCL[LightCyan]}Restarting system...${idsCL[Default]}" MMSTOP @@ -447,7 +447,7 @@ MMSTATUS(){ echo -e "${idsCL[Green]}Good${idsCL[Default]}" else echo -e "${idsCL[Red]}Not Good${idsCL[Default]}" - if [ ! -f /opt/idssys/mediamanager/mm.stop ]; then + if [ ! -f ${MMFOLDER}/mm.stop ]; then echo echo -e "${idsCL[LightCyan]}Restarting system...${idsCL[Default]}" MMSTOP @@ -468,18 +468,27 @@ MMSTATUS(){ echo } -if [ ! -f /opt/idssys/mediamanager/mm.update.stop ]; then - if [ -f /opt/idssys/mediamanager/mm.stop ]; then - stoppedsince=`date +%s`-$(stat -c %Y /opt/idssys/mediamanager/mm.stop) +MMUPDATE(){ + if curl -m 15 -s --head --request GET https://git.schroedercity.com | grep "HTTP/2 200" > /dev/null; then + cd ${MMFOLDER} + if [ "`git log --pretty=%H ...refs/heads/master^ | head -n 1`" != "`git ls-remote origin -h refs/heads/master |cut -f1`" ]; then + touch /opt + fi + fi +} + +if [ ! -f ${MMFOLDER}/mm.update.stop ]; then + 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 case $action in start | check) - if [ -f /opt/idssys/mediamanager/mm.stop ] && [ "${action}" = "check" ]; then - if [ -f /opt/idssys/mediamanager/mm.stop.checking ]; then - checkstopped=`expr $(date +%s) - $(stat -c %Y /opt/idssys/mediamanager/mm.stop.checking)` + if [ -f ${MMFOLDER}/mm.stop ] && [ "${action}" = "check" ]; then + if [ -f ${MMFOLDER}/mm.stop.checking ]; then + checkstopped=`expr $(date +%s) - $(stat -c %Y ${MMFOLDER}/mm.stop.checking)` if [ $checkstopped -gt 1800 ]; then - rm -f /opt/idssys/mediamanager/mm.stop* + rm -f ${MMFOLDER}/mm.stop* getStuckMM=$(pgrep -f '/mm') for i in $getStuckMM; do /bin/kill $i @@ -496,15 +505,15 @@ if [ ! -f /opt/idssys/mediamanager/mm.update.stop ]; then exit 1 fi fi - touch /opt/idssys/mediamanager/mm.stop - [ "${action}" = "check" ] && touch /opt/idssys/mediamanager/mm.stop.checking + touch ${MMFOLDER}/mm.stop + [ "${action}" = "check" ] && touch ${MMFOLDER}/mm.stop.checking MMSTART ${action};; stop) MMSTOP;; restart) - if [ -f /opt/idssys/mediamanager/mm.stop ]; then + if [ -f ${MMFOLDER}/mm.stop ]; then echo -e "${stopmsg}" echo exit 1 @@ -516,7 +525,7 @@ if [ ! -f /opt/idssys/mediamanager/mm.update.stop ]; then MMSTART ${action};; status) - if [ -f /opt/idssys/mediamanager/mm.stop ]; then + if [ -f ${MMFOLDER}/mm.stop ]; then echo -e "${stopmsg}" echo fi diff --git a/run.sh b/run.sh index 30fe8b1..52def11 100755 --- a/run.sh +++ b/run.sh @@ -8,7 +8,7 @@ source /opt/idssys/mediamanager/defaults.inc if [ -f /opt/idssys/settings/mediamanager.conf ]; then source /opt/idssys/settings/mediamanager.conf else - source /opt/idssys/mediamanager/settings.conf + source ${MMFOLDER}/settings.conf fi if [ "${1}" == "update" ]; then @@ -31,7 +31,7 @@ if [ "${1}" == "update" ]; then git pull >/dev/null 2>&1 git submodule update --remote >/dev/null 2>&1 fi - cd /opt/idssys/mediamanager + cd ${MMFOLDER} if [ "`git log --pretty=%H ...refs/heads/master^ | head -n 1`" != "`git ls-remote origin -h refs/heads/master |cut -f1`" ]; then if [ "${2}" != "q" ]; then echo -en "\e[1A"; @@ -44,7 +44,7 @@ if [ "${1}" == "update" ]; then git prune >/dev/null 2>&1 git pull >/dev/null 2>&1 git submodule update --remote >/dev/null 2>&1 - /bin/chmod +x /opt/idssys/mediamanager/mm-scripts.sh 2>&1 + /bin/chmod +x ${MMFOLDER}/mm-scripts.sh 2>&1 if [ "${2}" != "q" ]; then echo -en "\e[1A"; echo -e "\e[0K\r ${idsCL[Green]}Updates Completed${idsCL[Default]}" @@ -58,20 +58,20 @@ if [ "${1}" == "update" ]; then fi else - [ "${1}" == "clear" ] && rm -f /opt/idssys/mediamanager/mm.* - if [ -f /opt/idssys/mediamanager/mm.checking ]; then - fd=$(( `date +%s` - `stat -L --format %Y /opt/idssys/mediamanager/mm.checking` )) + [ "${1}" == "clear" ] && rm -f ${MMFOLDER}/mm.* + if [ -f ${MMFOLDER}/mm.checking ]; then + fd=$(( `date +%s` - `stat -L --format %Y ${MMFOLDER}/mm.checking` )) if [ ${fd} -gt 900 ]; then - rm -f /opt/idssys/mediamanager/mm.checking + rm -f ${MMFOLDER}/mm.checking else echo -e "${idsCL[LightCyan]}Try again later, 'mm' program currently in use...${idsCL[Default]}" fi fi - if [ ! -f /opt/idssys/mediamanager/mm.checking ]; then - touch /opt/idssys/mediamanager/mm.checking - /opt/idssys/mediamanager/mm-scripts.sh ${1} ${2} ${3} ${4} ${5} ${6} ${7} ${8} ${9} ${10} ${11} ${12} ${13} ${14} - rm -f /opt/idssys/mediamanager/mm.checking + if [ ! -f ${MMFOLDER}/mm.checking ]; then + touch ${MMFOLDER}/mm.checking + ${MMFOLDER}/mm-scripts.sh ${1} ${2} ${3} ${4} ${5} ${6} ${7} ${8} ${9} ${10} ${11} ${12} ${13} ${14} + rm -f ${MMFOLDER}/mm.checking fi fi