Files
NodeMgmt/inc/services.inc
2024-01-16 21:53:48 -06:00

459 lines
18 KiB
Bash
Executable File

#!/usr/bin/env bash
SINGLE_SERVER_SERVICE_CHECK(){
servicestocheck=${1^^}_SINGLESRVR_SERVICES[@]
dockerstocheck=${1^^}_SINGLESRVR_DOCKERS[@]
hosts=${1^^}_HOSTS[@]
[ "${2^^}" == "START" ] && rm -f ${NM_TMPFOLDER}/.singleserver
if [[ "$(declare -p ${1^^}_SINGLESRVR_SERVICES)" =~ "declare -a" ]] || [[ "$(declare -p ${1^^}_SINGLESRVR_DOCKERS)" =~ "declare -a" ]]; then
if [[ $(ip addr show $(ip route | awk '/default/ { print $5 }') | grep "inet" | awk '/inet/ {print $2}' | cut -d'/' -f1) == *${NM_SINGLESRVR_IP[${1^^}]}* ]]; then
if [ ! -f ${NM_TMPFOLDER}/.singleserver ]; then
SENDNOTICE "Node-Balancing Notice" "Starting SingleServer Services on ${NM_HOSTNAMES[${RUN_NODE_IP}]} (${RUN_NODE_IP}), stopping on other Nodes"
touch ${NM_TMPFOLDER}/.singleserver
fi
gofor=false
if [ "${1^^}" == "WEBxx" ] && [ ! -f ${NM_REPL_CHECK_LOC['web-data']}/mounted ]; then
/bin/mount -a
sleep 5s
[ -f ${NM_REPL_CHECK_LOC["${1}-data"]}/mounted ] && gofor=true
else
gofor=true
fi
if [ "${gofor}" == "true" ]; then
for stc in "${!servicestocheck}"; do
if [ "$(systemctl is-active ${stc}.service)" != "active" ]; then
echo "$(date +%Y-%m-%d-%H-%M-%S) - ${NM_HOSTNAMES[${RUN_NODE_IP}]} - Starting ${stc} on ${RUN_NODE_IP}, stopping on the other Nodes ..." >> ${NM_LOGFILE}
[ "${stc}" = "pdnsadmin" ] && stc="pdnsadmin.service pdnsadmin.socket"
for nip in "${!hosts}"; do
if [[ "${RUN_NODE_IP}" != *"${nip}"* ]] && [ "$(CHECK_HOST ${nip})" != "false" ]; then
ssh -tq -o ConnectTimeout=1 -o ConnectionAttempts=1 root@${nip} /bin/systemctl stop ${stc}
ssh -tq -o ConnectTimeout=1 -o ConnectionAttempts=1 root@${nip} rm -f ${NM_TMPFOLDER}/.singleserver
fi
done
sleep 3s
[ "${stc}" = "pdnsadmin" ] && stc="pdnsadmin.socket pdnsadmin.service"
[ "${stc}" = "headscale" ] && sleep 90s
/bin/systemctl start ${stc} &
echo "$(date +%Y-%m-%d-%H-%M-%S) - ${NM_HOSTNAMES[${RUN_NODE_IP}]} - ... Done" >> ${NM_LOGFILE}
fi
done
for docker in "${!dockerstocheck}"; do
for nip in "${!hosts}"; do
if [[ "${RUN_NODE_IP}" = *"${nip}"* ]]; then
if [ "docker ps -a | grep ${docker})" ]; then
[ "${docker}" == "authelia" ] && [ ! "$(/usr/bin/docker ps -q -f name=auth_redis)" ] && /usr/bin/docker start auth_redis && sleep 2s >/dev/null 2>&1
[ ! "$(/usr/bin/docker ps -q -f name=${docker})" ] && /usr/bin/docker start ${docker} & >/dev/null 2>&1
# elif [ "${NM_DOCKER_COMPOSE_LOC[${docker}]}" != "" ]; then
# /usr/local/bin/docker compose -f ${NM_DOCKER_COMPOSE_LOC[${docker}]}/docker-compose.yml up -d >/dev/null 2>&1
# echo "$(date +%Y-%m-%d-%H-%M-%S) - ${nip} - ${NM_HOSTNAMES[${nip}]}[${nip}] - ${NM_DOCKER_DESC[${docker}]} (docker) is not found, creating and starting now" >> ${NM_LOGFILE}
fi
else
if [ "$(CHECK_HOST ${nip})" != "false" ]; then
[ "$(ssh -q -o ConnectTimeout=1 -o ConnectionAttempts=1 root@${nip} /usr/bin/docker ps -q -f name=${docker})" ] && ssh -q -o ConnectTimeout=1 -o ConnectionAttempts=1 root@${nip} /usr/bin/docker stop ${docker} >/dev/null 2>&1
[ "${docker}" == "authelia" ] && [ "$(ssh -q -o ConnectTimeout=1 -o ConnectionAttempts=1 root@${nip} /usr/bin/docker ps -q -f name=auth_redis)" ] && ssh -q -o ConnectTimeout=1 -o ConnectionAttempts=1 root@${nip} /usr/bin/docker stop auth_redis >/dev/null 2>&1
fi
fi
done
done
fi
else
for stc in "${!servicestocheck}"; do
[ "${stc}" = "pdnsadmin" ] && stc="pdnsadmin.socket pdnsadmin.service"
/bin/systemctl stop ${stc} & >/dev/null 2>&1
done
for docker in "${!dockerstocheck}"; do
[ "$(/usr/bin/docker ps -q -f name=${docker})" ] && /usr/bin/docker stop ${docker} & >/dev/null 2>&1
done
rm -f ${NM_TMPFOLDER}/.singleserver
fi
[ "${1^^}" == "WEB" ] && [ "$(systemctl is-active nginx)" != "active" ] && ${NM_SCRIPT} service web restart ${RUN_NODE_IP}
fi
}
SERVICE_MGMT(){
declare -A SERVICE
QUIET=0
while [ $# -gt 0 ]; do
case "${1}" in
-s|-service) SERVICE['service']=${2,,};;
-n|-node) SERVICE['node']=${2^^};;
-nt|-nodetype) SERVICE['nodetype']=${2^^};;
-a|-action) SERVICE['action']=${2,,};;
-q|-quiet) QUIET=1;;
-h|-help)
echo -e "\nUsage: ${idsCL[Yellow]}[nodemgmt or nmg] ${idsCL[LightYellow]}service${idsCL[Default]} {"
width=35
printf "%-${width}s- %s\n" " -s|-service {service}" "Name of service to manage"
printf "%-${width}s- %s\n" " -n|-node {node-lan-ip}" "Node to run on"
printf "%-${width}s- %s\n" " -nt|-nodetype {nodetype}" "NodeType to run on, or all. Defaults to local nodetype"
printf "%-${width}s- %s\n" " -a|-action {action}" "Action to perform; start, stop, restart etc"
printf "%-${width}s- %s\n" " -q|-quiet" "Do not output, typically used from cron jobs"
echo -e "}\n"
exit 0
;;
*)
if [ "${NM_SERVICE_DESC[${1}]}" != "" ]; then
SERVICE['service']=${1}
elif [ "${NM_SRVCOPTS[${1}]}" != "" ]; then
SERVICE['action']=${1}
elif [ "${NM_HOSTNAMES[${1}]}" != "" ]; then
SERVICE['node']=${1}
elif [ "${1}" == "q" ]; then
QUIET=1
fi
;;
esac
shift
done
# echo "SERVICE=${SERVICE['service']}"
# echo "ACTION=${SERVICE['action']}"
# echo "NODE=${SERVICE['node']}"
# echo "NODETYPE=${SERVICE['nodetype']}"
# echo "QUIET=${QUIET}"
if [ "${NM_SERVICE_DESC[${SERVICE['service']}]}" == "" ] && [ "${NM_SERVICE_DESC[${SERVICE['service']}]}" != "ALL" ]; then
echo -e "${idsCL[Red]}"${SERVICE['service']}" is an unknown Service.${idsCL[Default]}"
exit 1
fi
# [ "${SERVICE['nodetype']}" == "" ] && SERVICE['nodetype']=
if [ "${SERVICE['nodetype']}" == "ALL" ]; then
# NT_HOSTS=${SERVICE['nodetype']}_HOSTS[@]
# NT_HOST=${SERVICE['nodetype']}_HOSTS[0]
NTS=(${NM_NODE_TYPES[*]})
elif [ "${SERVICE['nodetype']}" != "" ]; then
if [ "${NM_HOSTS[${SERVICE['nodetype']}]}" != "" ]; then
NT_HOSTS=${SERVICE['nodetype']}_HOSTS[@]
NT_HOST=${SERVICE['nodetype']}_HOSTS[0]
NTS=("${SERVICE['nodetype']}");
else
echo -e "${idsCL[Red]}"${SERVICE['nodetype']}" is an unknown NodeType.${idsCL[Default]}"
exit 1
fi
elif [ "${RUN_NODE_TYPE}" != "" ]; then
NT_HOSTS=${RUN_NODE_TYPE}_HOSTS[@]
NT_HOST=${RUN_NODE_TYPE}_HOSTS[0]
NTS=("${RUN_NODE_TYPE}");
# else
# NTS=(${NM_NODE_TYPES[*]})
fi
#var=NC_HOSTS[@]
if [ "${SERVICE['service']}" = "glusterd" ]; then
for nip in "${GL_HOSTS[@]}"; do
if [[ "${RUN_NODE_IP}" == *"${nip}"* ]]; then
nip='localhost '
NCMD=''
else
NCMD="ssh -q -o ConnectTimeout=1 -o ConnectionAttempts=1 root@${nip}"
fi
[ ${QUIET} -eq 0 ] && echo -en "${NM_SRVCOPTS[${SERVICE['action']}]}ing on '$nip'... ${idsCL[Default]}"
checkhost=$(CHECK_HOST ${nip})
if [ "${checkhost}" != "false" ]; then
#${NCMD} systemctl ${SERVICE['action']} ${SERVICE['service']} > /dev/null 2>&1
if [ ${QUIET} -eq 0 ]; then
if [[ "enable,disable" = *"${SERVICE['action']}"* ]] || [ "${SERVICE['service']}" = "daemon-reload" ]; then
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
elif [ "$(${NCMD} systemctl is-active ${SERVICE['service']})" = "active" ]; then
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
elif [ "${SERVICE['action']}" = "stop" ]; then
echo -e "${idsCL[Red]}STOPPED${idsCL[Default]}"
else
echo -e "${idsCL[Red]}ERROR${idsCL[Default]}"
fi
fi
elif [ ${QUIET} -eq 0 ]; then
echo -e "${idsCL[Red]}Node is Down${idsCL[Default]}"
fi
done
if [ "${SERVICE['action']}" = "start" ] || [ "${SERVICE['action']}" = "restart" ]; then
echo
sleep 5s
for nip in "${GL_HOSTS[@]}"; do
if [[ "${RUN_NODE_IP}" == *"${nip}"* ]]; then
nip='localhost '
NCMD=''
else
NCMD="ssh -q -o ConnectTimeout=1 -o ConnectionAttempts=1 root@${nip}"
fi
if [ ${QUIET} -eq 0 ]; then
echo -en "Mounting drive on '$nip'... ${idsCL[Default]}"
fi
checkhost=$(CHECK_HOST ${nip})
if [ "${checkhost}" != "false" ]; then
#${NCMD} mount -a > /dev/null 2>&1
if [ ${QUIET} -eq 0 ]; then
# if [[ "${NCHOSTS}" == *"${nip}"* ]]; then
if [[ " ${NC_HOSTS[*]} " =~ " ${nip} " ]]; then
mntpath="/mnt/Nextcloud-Data"
# elif [[ "${WEBHOSTS}" == *"${nip}"* ]]; then
elif [[ " ${NM_HOSTS['WEB'][*]} " =~ " ${nip} " ]]; then
mntpath="/mnt/web-data"
fi
${NCMD} "test -e ${mntpath}/mounted"
if [ $? -eq 0 ]; then
echo -e "${idsCL[Green]}MOUNTED ($mntpath)${idsCL[Default]}"
else
echo -e "${idsCL[Red]}NOT MOUNTED ($mntpath)${idsCL[Default]}"
fi
fi
if [ "$nip" = "10.10.1.43" ]; then
echo -en "Mounting drive on '$nip'... ${idsCL[Default]}"
mntpath="/mnt/Nextcloud-Data"
${NCMD} "test -e ${mntpath}/mounted"
if [ $? -eq 0 ]; then
echo -e "${idsCL[Green]}MOUNTED ($mntpath)${idsCL[Default]}"
else
echo -e "${idsCL[Red]}NOT MOUNTED ($mntpath)${idsCL[Default]}"
fi
fi
elif [ ${QUIET} -eq 0 ]; then
echo -e "${idsCL[Red]}Node is Down${idsCL[Default]}"
fi
done
fi
else
# if [ "${RUN_NODE_TYPE}" == "" ]; then
#
# fi
if [ ${QUIET} -eq 0 ]; then
if [ "${NM_SERVICE_DESC[${SERVICE['service']}]}" = "" ]; then
echo -e "${idsCL[Red]}(${SERVICE['service']}) is not an allowed service.${idsCL[Default]}"
exit 1
fi
if [ "${NM_SRVCOPTS[${SERVICE['action']}]}" = "" ]; then
echo -e "${idsCL[Red]}(${SERVICE['action']}) is not an allowed service action.${idsCL[Default]}"
exit 1
fi
if [ "${SERVICE['action']}" = "start" ] && [ "${SERVICE['service']}" = "haproxy" ]; then
TACT='restart'
else
TACT="${SERVICE['action']}"
fi
echo
echo -e "${idsCL[LightGreen]}[[ ${NM_SERVICE_DESC[${SERVICE['service']}]} ${NM_SRVCOPTS[${SERVICE['action']}]}ing ]]${idsCL[Default]}"
echo -e "${idsCL[LightGreen]}-------------------------------------------${idsCL[Default]}"
fi
if [ "${SERVICE['service']}" = "nginx" ] && ([ "${SERVICE['node']}" == "" ] || [ "${SERVICE['node']}" == "ALL" ]); then
if [ "${RUN_NODE_TYPE}" != "" ] && [ "${NM_REPL_NGINX_PATHS[${RUN_NODE_TYPE}]}" != "" ] && ([ "${SERVICE['action']}" = "restart" ] || [ "${SERVICE['action']}" = "reload" ]); then
if [ "${SERVICE['node']}" != "ns" ]; then
if [ ${QUIET} -eq 0 ]; then
echo -en "${idsCL[LightYellow]}Verifying replication across the nodes... ${idsCL[Default]}"
fi
if [[ "${RUN_NODE_IP}" == *"${!NT_HOST}"* ]]; then
PH_CMD=""
else
PH_CMD="ssh -q -o ConnectTimeout=1 -o ConnectionAttempts=1 root@${!NT_HOST}"
fi
${PH_CMD} touch ${NM_REPL_NGINX_PATHS[${RUN_NODE_TYPE}]}/test.repl
# ${PH_CMD} "echo -e \"Service ${SERVICE['service']} ${SERVICE['action']}\" >> ${NM_REPL_NGINX_PATHS[${RUN_NODE_TYPE}]}/test.repl"
for nip in "${!NT_HOSTS}"; do
checkhost=$(CHECK_HOST ${nip})
if [ "${checkhost}" != "false" ] && [ "${nip}" != "${!NT_HOST}" ]; then
checked=false
until [ "${checked}" = "true" ]; do
checked="`${PH_CMD} ssh -q -o ConnectTimeout=1 -o ConnectionAttempts=1 root@${nip} [ -f ${NM_REPL_NGINX_PATHS[${RUN_NODE_TYPE}]}/test.repl ] && [ $(expr $(date +%s) - $(stat -L --format %Y ${NM_REPL_NGINX_PATHS[${RUN_NODE_TYPE}]}/test.repl)) -le 90 ] && echo true`"
done
fi
done
${PH_CMD} rm -f ${NM_REPL_NGINX_PATHS[${RUN_NODE_TYPE}]}/test.repl
fi
fi
elif ([ "${SERVICE['service']}" = "pdns" ] || [ "${SERVICE['service']}" = "pdnsadmin" ] || [ "${SERVICE['service']}" = "headscale" ]) && ([ "${SERVICE['node']}" == "" ] || [ "${SERVICE['node']}" == "ALL" ]); then
if [ "${SERVICE['action']}" = "start" ] || [ "${SERVICE['action']}" = "restart" ] || [ "${SERVICE['action']}" = "reload" ]; then
if [ "${SERVICE['node']}" != "ns" ]; then
[ ${QUIET} -eq 0 ] && echo -en "${idsCL[LightYellow]}Verifying replication across the nodes... ${idsCL[Default]}"
if [[ "${RUN_NODE_IP}" == *"${!NT_HOST}"* ]]; then
PH_CMD=""
else
PH_CMD="ssh -q -o ConnectTimeout=1 -o ConnectionAttempts=1 root@${!NT_HOST}"
fi
${PH_CMD} touch ${NM_REPL_CHECK_LOC[${SERVICE['service']}]}/test.repl
for nip in "${!NT_HOSTS}"; do
checkhost=$(CHECK_HOST ${nip})
if [ "${checkhost}" != "false" ] && [ "${nip}" != "${!NT_HOST}" ]; then
checked=false
until [ "${checked}" = "true" ]; do
checked="`${PH_CMD} ssh -q -o ConnectTimeout=1 -o ConnectionAttempts=1 root@${nip} [ -f ${NM_REPL_CHECK_LOC[${SERVICE['service']}]}/test.repl ] && [ $(expr $(date +%s) - $(stat -L --format %Y ${NM_REPL_CHECK_LOC[${SERVICE['service']}]}/test.repl)) -le 90 ] && echo true`"
done
fi
done
${PH_CMD} rm -f ${NM_REPL_CHECK_LOC[${SERVICE['service']}]}/test.repl
fi
fi
fi
if [ ${QUIET} -eq 0 ]; then
echo -e "${idsCL[Green]}Completed${idsCL[Default]}"
echo
fi
if [[ "$(declare -p NTS)" =~ "declare -a" ]]; then
for NTYPE in "${NTS[@]}"; do
var1=${NTYPE}_SERVICES_CHECK[@]
# if [[ "${!var1}" = *"${SERVICE['service']}"* ]]; then
if [[ " ${!var1} " =~ " ${SERVICE['service']} " ]]; then
if [ "${SERVICE['node']}" != "" ]; then
var2=(${SERVICE['node']})
sethost=true;
else
var2=${NTYPE}_HOSTS[@]
IFS=' '
var2=(${!var2})
unset IFS
sethost=false
fi
for nip in "${var2[@]}"; do
# if [[ "${RUN_NODE_IP}" == *"${SERVICE['node']}"* ]]; then GO=true;
# elif [ ! -z ${SERVICE['node']+x} ] || [ "${SERVICE['node']}" == "q" ]; then GO=true;
# else GO=false;
# fi
# if [ "${GO}" == "true" ]; then
if [ $sethost == true ]; then
nodename=${NM_HOSTNAMES[${SERVICE['node']}]}
else
nodename="${NM_HOSTNAMES[${nip}]}[${nip}]"
fi
if [[ "${RUN_NODE_IP}" == *"${nip}"* ]]; then
nip='localhost '
NCMD=''
else
NCMD="ssh -q -o ConnectTimeout=1 -o ConnectionAttempts=1 root@${nip}"
fi
NOGOCHK=true;
if [ "${SERVICE['service']}" == "gitea" ]; then
if [[ $(${NCMD} ip addr show $(ip route | awk '/default/ { print $5 }') | grep "inet" | awk '/inet/ {print $2}' | cut -d'/' -f1) != *$(GET_AUTHELIA_IP)* ]]; then
NOGOCHK=false;
fi
elif [ "${SERVICE['service']}" == "headscale" ]; then
if [[ $(${NCMD} ip addr show $(ip route | awk '/default/ { print $5 }') | grep "inet" | awk '/inet/ {print $2}' | cut -d'/' -f1) != *${NM_SINGLESRVR_IP['HS']}* ]]; then
NOGOCHK=false;
fi
fi
if [ "${NOGOCHK}" == "true" ]; then
if [ ${QUIET} -eq 0 ]; then
echo -en "${NM_SRVCOPTS[${SERVICE['action']}]}ing on ${nodename} ($nip)... ${idsCL[Default]}"
fi
checkhost=$(CHECK_HOST ${nip})
if [ "${checkhost}" != "false" ]; then
if [ "${SERVICE['service']}"= = "offsite-power-check" ]; then
if [ ${QUIET} -eq 0 ] && [ "${SERVICE['action']}" != "status" ]; then
if [ "$(ssh -q -o ConnectTimeout=1 -o ConnectionAttempts=1 root@${nip} ps -U root | grep "offsite-power-check.sh start" | grep -v "grep" | awk '{print $1}')" != "" ]; then
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
else
echo -e "${idsCL[Red]}Not Running${idsCL[Default]}"
fi
fi
else
if [ "${SERVICE['service']}" = "pdnsadmin" ]; then
srva="pdnsadmin.socket pdnsadmin.service"
elif [ "${SERVICE['service']}" = "syncthing" ] && [[ "${NM_SERVICES_CHECK[${NTYPE}]}" = *"syncthing@pdns"* ]]; then
srva="syncthing syncthing@pdns"
else
srva=${SERVICE['service']}
fi
if [ "${SERVICE['action']}" != "status" ]; then
${NCMD} systemctl ${SERVICE['action']} ${srva} >/dev/null 2>&1
else
echo
${NCMD} systemctl ${SERVICE['action']} ${srva}
echo
fi
if [ ${QUIET} -eq 0 ] && [ "${SERVICE['action']}" != "status" ]; then
if [[ "enable,disable" = *"${SERVICE['action']}"* ]] || [ "${SERVICE['service']}" = "daemon-reload" ]; then
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
elif [ "$(${NCMD} systemctl is-active ${SERVICE['service']})" = "active" ]; then
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
elif [ "${SERVICE['action']}" = "stop" ]; then
echo -e "${idsCL[Red]}STOPPED${idsCL[Default]}"
else
echo -e "${idsCL[Red]}ERROR${idsCL[Default]}"
fi
fi
fi
elif [ ${QUIET} -eq 0 ]; then
echo -e "${idsCL[Red]}Node is Down${idsCL[Default]}"
fi
fi
# fi
# if [ "${SERVICE['service']}" = "gitea" ]; then
# break
# fi
done
fi
done
else #no-nodetypes, standalone setup
echo -en "${NM_SRVCOPTS[${SERVICE['action']}]}ing on localhost ... ${idsCL[Default]}"
if [ "${SERVICE['action']}" != "status" ]; then
systemctl ${SERVICE['action']} ${srva} >/dev/null 2>&1
else
echo
systemctl ${SERVICE['action']} ${srva}
echo
fi
if [ ${QUIET} -eq 0 ] && [ "${SERVICE['action']}" != "status" ]; then
if [[ "enable,disable" = *"${SERVICE['action']}"* ]] || [ "${SERVICE['service']}" = "daemon-reload" ]; then
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
elif [ "$(systemctl is-active ${SERVICE['service']})" = "active" ]; then
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
elif [ "${SERVICE['action']}" = "stop" ]; then
echo -e "${idsCL[Red]}STOPPED${idsCL[Default]}"
else
echo -e "${idsCL[Red]}ERROR${idsCL[Default]}"
fi
fi
fi
fi
if [ ${QUIET} -eq 0 ]; then
echo
echo -e "${idsCL[Green]}${NM_SERVICE_DESC[${SERVICE['service']}]} has been ${NM_SRVCOPTS[${SERVICE['action']}]}ed${idsCL[Default]}"
echo
fi
}