Files
mediamanager/mm-scripts.sh
2022-02-12 21:54:23 -06:00

180 lines
4.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# MediaManager Scripts
action="$1"
source /opt/idssys/defaults/colors.inc
source /opt/idssys/defaults/default.inc
source /opt/idssys/settings/mediamanager.conf
source /opt/idssys/mediamanager/defaults.inc
if [ "${action}" != "check" ] && [ "${action}" != "update" ]; then
echo ""
echo -e "${idsCL[LightGreen]} MediaManager ${idsCL[Default]} ${idsCL[DarkGray]}(ver-${VERS})${idsCL[Default]}"
DIVIDER . lightGreen
fi
IFS=,
SERVICES_CHECK=(${SERVICESCHECK})
unset IFS
#OUT=`ip a show $VPN_INTERFACE up >/dev/null 2>&1`;
MMSTART(){
if [ "${action}" = "start" ]; then
mmsv='Starting'
echo -e "${idsCL[Green]}Starting System... ${idsCL[Default]}"
elif [ "${action}" = "stop" ]; then
mmsv='Stopping'
echo -e "${idsCL[Green]}Stopping System... ${idsCL[Default]}"
else
mmsv='Verifying'
fi
echo
echo -en "${idsCL[LightCyan]}${mmsv} VPN Tunnel... ${idsCL[Default]}"
`${VPN_START}`
checked=false
cc=0
until [ "${checked}" = "" ]; do
OUT=`ip a show $VPN_INTERFACE up` >/dev/null 2>&1
if [ ${#OUT} -ne 0 ]; then
checked=""
fi
if [ "${checked}" == "false" ] && [ ${cc} -eq 90 ]; then
if [ -f /opt/mm.vpn.fail ]; then
touch /opt/mm.vpn.fail2
rm -f /opt/mm.vpn.fail
sleep 5m
rm -f /opt/mm.stop
exit 1
elif [ -f /opt/mm.vpn.fail2 ]; then
exit 1
else
touch /opt/mm.vpn.fail
sleep 1m
rm -f /opt/mm.stop
exit 1
fi
fi
((cc=${cc}+1))
sleep 1s
done
echo -e "${idsCL[Green]} done${idsCL[Default]}"
echo -en "${idsCL[LightCyan]}${mmsv} Services... ${idsCL[Default]}"
for srvc in "${SERVICES_CHECK[@]}"; do
/bin/systemctl start ${srvc}
done
echo -e "${idsCL[Green]} done${idsCL[Default]}"
if [ "${action}" == "start" ]; then
echo -e "${idsCL[LightCyan]}It may take a couple of minutes before all systems fully start up${idsCL[Default]}"
echo
fi
rm -f /opt/mm.stop
rm -f /opt/mm.vpn.fail*
echo
echo -e "${idsCL[Green]}All services have been started${idsCL[Default]}"
echo
}
MMSTOP(){
if [ "${action}" = "start" ]; then
mmsv='Starting'
echo -e "${idsCL[Green]}Starting System...${idsCL[Default]}"
elif [ "${action}" = "stop" ]; then
mmsv='Stopping'
if [ -f /opt/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
fi
touch /opt/mm.stop
echo -en "${idsCL[LightCyan]}${mmsv} Services... ${idsCL[Default]}"
for srvc in "${SERVICES_CHECK[@]}"; do
/bin/systemctl stop ${srvc} &
done
checked=false
until [ "${checked}" = "" ]; do
if ! lsof -Pi :5858 -sTCP:LISTEN -t >/dev/null; then
if ! lsof -Pi :5555 -sTCP:LISTEN -t >/dev/null; then
if ! lsof -Pi :5656 -sTCP:LISTEN -t >/dev/null; then
checked=""
fi
fi
fi
done
echo -e "${idsCL[Green]} done${idsCL[Default]}"
echo
echo -en "${idsCL[LightCyan]}${mmsv} VPN Tunnel... ${idsCL[Default]}"
`${VPN_STOP}`
echo -e "${idsCL[Green]} done${idsCL[Default]}"
echo
echo -e "${idsCL[Red]}All services have been stopped${idsCL[Default]}"
echo
}
MMSTATUS(){
if [[ $(/sbin/ip -o -4 addr list ens160 | awk '{print $4}' | cut -d/ -f1) == *"10.10.1.24"* ]]; then
statusArray=("nordvpn")
else
statusArray=("openvpn")
fi
statusArray+=(${SERVICES_CHECK[@]})
for srvc in "${statusArray[@]}"; do
c=0; cw=36 spc=''
spc1=$((${cw}-${#NM_SERVICES[${srvc}]}))
until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done
echo -en " ${NM_SERVICES[${srvc}]}$spc: "
if [ "$(systemctl is-active ${srvc})" != "active" ]; then
echo -e "${idsCL[Red]}Not Running${idsCL[Default]}"
else
echo -e "${idsCL[Green]}Running${idsCL[Default]}"
fi
done
echo
echo
}
case $action in
start | check)
if [ -f /opt/mm.stop ] && [ "${action}" = "check" ]; then
echo -e "${idsCL[Yellow]}The system is currently stopped ${idsCL[LightCyan]}(You must run \"mm start\" to start services again)${idsCL[Default]}"
exit 0
fi
touch /opt/mm.stop
MMSTART;;
stop)
MMSTOP;;
status)
if [ -f /opt/mm.stop ]; then
echo -e "${idsCL[Yellow]}The system is currently stopped ${idsCL[LightCyan]}(You must run \"mm start\" to start services again)${idsCL[Default]}"
echo
fi
MMSTATUS;;
update);;
*)
echo -e "Usage: ${idsCL[LightCyan]}mm${idsCL[Default]} {"
echo -e " ${idsCL[Yellow]}start${idsCL[Default]} --> Start services"
echo -e " ${idsCL[Yellow]}stop${idsCL[Default]} --> Stop services"
echo -e " ${idsCL[Yellow]}check${idsCL[Default]} --> Check for VPN and start services if needed"
echo -e " ${idsCL[Yellow]}status${idsCL[Default]} --> Check service status"
echo -e "}"
echo ""
echo ""
exit 0;;
esac
exit 0