Files
NodeMgmt/nodemgmt-scripts.sh

213 lines
7.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# NodeMgmt - Galera/NGINX Node Management Scripts
action="$1"
source /opt/idssys/defaults/colors.inc
source /opt/idssys/defaults/default.inc
source /opt/idssys/nodemgmt/settings.conf
source /opt/idssys/nodemgmt/defaults.inc
# ========================================================= #
case "$1" in
service)
containsElement "${2}" "${NODE_SERVICES[@]}"
if [[ "start,stop,restart,enable,disable" != *"${3}"* ]] && [ "${2}" != "daemon-reload" ]; then
echo -e "${idsCL[Red]}(${3}) is not an allowed service action.${idsCL[Default]}"
exit 1
fi
if [ "$2" = "mysql" ]; then SER='MySQL'
elif [ "$2" = "nginx" ]; then SER='NGINX'
elif [ "$2" = "gogs" ]; then SER='Gogs'
elif [ "$2" = "gitea" ]; then SER='Gitea'
elif [ "$2" = "haproxy" ]; then SER='HAProxy'
elif [ "$2" = "keepalived" ]; then SER='Keepalived'
elif [ "$2" = "maxscale" ]; then SER='MaxScale'
else SER="${2}"
fi
if [ "$3" = "stop" ]; then ADISP='Stopp';
elif [ "$3" = "enable" ]; then ADISP='Enabl';
elif [ "$3" = "disable" ]; then ADISP='Disabl';
else ADISP="$(tr '[:lower:]' '[:upper:]' <<< ${3:0:1})${3:1}"; fi
if [ "$3" = "start" ] && [ "$2" = "haproxy" ]; then TACT='restart'; else TACT="${3}"; fi
echo -e "${idsCL[LightGreen]}[[${SER} ${ADISP}ing]]${idsCL[Default]}"
echo -e "${idsCL[LightGreen]}-------------------------------------------${idsCL[Default]}"
nid=1
for nip in "${NODE_HOSTS[@]}"
do
if [[ $(/sbin/ip -o -4 addr list ens192 | awk '{print $4}' | cut -d/ -f1) == *"${nip}"* ]]; then
nip='localhost'
NCMD=''
else
NCMD="ssh root@${nip}"
fi
echo -en "${ADISP}ing on Node ${nid} ($nip)...${idsCL[Default]}"
$NCMD systemctl $3 $2
if [[ "enable,disable" = *"${3}"* ]] || [ "${2}" = "daemon-reload" ]; then
echo -e "${idsCL[Green]}OK${idsCL[Default]}"
elif [[ $(${NCMD} pgrep ${2} | wc -l) -gt "0" ]]; then
echo -e "${idsCL[Green]}OK${idsCL[Default]}"
elif [ "$3" = "stop" ]; then echo -e "${idsCL[Red]}STOPPED${idsCL[Default]}"
else echo -e "${idsCL[Red]}ERROR${idsCL[Default]}"
fi
nid=`expr $nid + 1`
done
echo ""
echo -e "${idsCL[Green]}${SER} has been ${ADISP}ed${idsCL[Default]}"
echo ""
exit 0
;;
newcert)
echo -e "${idsCL[LightGreen]}Requesting Certificate for '${idsCL[Yellow]}${2}${idsCL[LightGreen]}'...${idsCL[Default]}"
do_with_root $CERT_DAEMON certonly --webroot -w /var/www/html -d $2
do_with_root chown -R root:letsencrypt /etc/letsencrypt
do_with_root chmod -R 6775 /etc/letsencrypt
echo -e "${idsCL[LightGreen]}Waiting for Certificate Replication...${idsCL[Default]}"
sleep 20
"$0" service nginx reload
exit 0
;;
certrenew)
echo -e "${idsCL[LightGreen]}Renewing Certificates...${idsCL[Default]}"
do_with_root $DAEMON renew --webroot -w /var/www/html
do_with_root chown -R root:letsencrypt /etc/letsencrypt
do_with_root chmod -R 6775 /etc/letsencrypt
echo -e "${idsCL[LightGreen]}Waiting for Certificate Replication...${idsCL[Default]}"
sleep 20
"$0" service nginx reload
exit 0
;;
nightlyrenew)
rm -f /opt/idssys/nodemgmt/cert-renewal-run-$(hostname)
do_with_root $DAEMON renew --webroot -w /var/www/html >> /opt/idssys/nodemgmt/cert-renewal-run-$(hostname)
do_with_root chown -R root:letsencrypt /etc/letsencrypt >> /opt/idssys/nodemgmt/cert-renewal-run-$(hostname)
do_with_root chmod -R 6775 /etc/letsencrypt >> /opt/idssys/nodemgmt/cert-renewal-run-$(hostname)
sleep 20
"$0" service nginx reload >> /opt/idssys/nodemgmt/cert-renewal-run-$(hostname)
exit 0
;;
update)
exit 0
;;
status)
echo ""
echo -e "${idsCL[Yellow]} NodeMgmt - Galera/NGINX Node Management${idsCL[Default]} ${idsCL[DarkGray]}(ver-${VERS})${idsCL[Default]}"
nid=1
for nip in "${NODE_HOSTS[@]}"
do
if [[ $(/sbin/ip -o -4 addr list ens192 | awk '{print $4}' | cut -d/ -f1) == *"${nip}"* ]]; then
nip='localhost'
NCMD=''
else
NCMD="ssh root@${nip}"
fi
echo -e "${idsCL[Yellow]}-------------------------------------------------------------${idsCL[Default]}"
echo -e "Node: ${nip}"
for srvc in "${NODE_SERVICES[@]}"
do
if [ "${srvc}" = "mysql" ]; then SER='MySQL: '
elif [ "${srvc}" = "nginx" ]; then SER='NGINX: '
elif [ "${srvc}" = "gogs" ]; then SER='Gogs: '
elif [ "${srvc}" = "gitea" ]; then SER='Gitea: '
elif [ "${srvc}" = "haproxy" ]; then SER='HAProxy:'
elif [ "${srvc}" = "keepalived" ]; then SER='Keepalived:'
elif [ "${srvc}" = "maxscale" ]; then SER='MaxScale:'
else SER="${2}"
fi
echo -en "${SER} "
if [ $(${NCMD} pgrep ${srvc} | wc -l) -gt "0" ]; then
echo -e "${idsCL[Green]}Running${idsCL[Default]}"
else echo -e "${idsCL[Red]}Not Running${idsCL[Default]}"
fi
done
nid=`expr $nid + 1`
done
echo -e "${idsCL[Yellow]}-------------------------------------------------------------${idsCL[Default]}"
echo ""
;;
status-check)
nid=1
for nip in "${NODE_HOSTS[@]}"
do
if [[ $(/sbin/ip -o -4 addr list ens192 | awk '{print $4}' | cut -d/ -f1) == *"${nip}"* ]]; then
nip='localhost'
NCMD=''
else
NCMD="ssh root@${nip}"
fi
for srvc in "${NODE_SERVICES[@]}"
do
if [ "${srvc}" = "mysql" ]; then SER='MySQL: '
elif [ "${srvc}" = "nginx" ]; then SER='NGINX: '
elif [ "${srvc}" = "gogs" ]; then SER='Gogs: '
elif [ "${srvc}" = "gitea" ]; then SER='Gitea: '
elif [ "${srvc}" = "haproxy" ]; then SER='HAProxy:'
elif [ "${srvc}" = "keepalived" ]; then SER='Keepalived:'
elif [ "${srvc}" = "maxscale" ]; then SER='MaxScale:'
else SER="${2}"
fi
if [ $(${NCMD} pgrep ${srvc} | wc -l) -lt "1" ]; then
echo "${SER} is down" | mail -s "iDS-Node${nid}-${nip}" alerts2external@schroedercity.com
fi
done
nid=`expr $nid + 1`
done
;;
set-permissions)
nid=1
for nip in "${NODE_HOSTS[@]}"
do
if [[ $(/sbin/ip -o -4 addr list ens192 | awk '{print $4}' | cut -d/ -f1) == *"${nip}"* ]]; then
nip='localhost'
NCMD=''
else
NCMD="ssh root@${nip}"
fi
echo -e "${idsCL[Yellow]}Setting folder permissions for Node ${nid} (${nip})${idsCL[Default]}"
echo -en "${idsCL[Cyan]}LetsEncrypt Certs ${idsCL[Default]}"
${NCMD} chown -R root:letsencrypt /etc/letsencrypt
${NCMD} chmod -R 6775 /etc/letsencrypt
echo -e "${idsCL[Green]}Complete${idsCL[Default]}"
echo -en "${idsCL[Cyan]}Webserver folders ${idsCL[Default]}"
${NCMD} chown -R www-data:www-data /var/www
${NCMD} chmod -R 7775 /var/www
echo -e "${idsCL[Green]}Complete${idsCL[Default]}"
echo -en "${idsCL[Cyan]}Gogs Folder ${idsCL[Default]}"
${NCMD} chown -R gogs:gogs /var/www/gogs
${NCMD} chmod -R 750 /var/www/gogs
echo -e "${idsCL[Green]}Complete${idsCL[Default]}"
echo -en "${idsCL[Cyan]}Gitea Folder ${idsCL[Default]}"
${NCMD} chown -R git:git /var/lib/gitea
${NCMD} chmod -R 750 /var/lib/gitea
echo -e "${idsCL[Green]}Complete${idsCL[Default]}"
echo ""
nid=`expr $nid + 1`
done
;;
*)
echo -e "Usage: ${idsCL[Yellow]}nodemgmt${idsCL[Default]} { ${idsCL[Yellow]}service${idsCL[Default]} [service] [action] | ${idsCL[Yellow]}status${idsCL[Default]} | ${idsCL[Yellow]}newcert${idsCL[Default]} [domain(,s)] | ${idsCL[Yellow]}certrenew${idsCL[Default]} }"
echo ""
echo ""
exit 0
;;
esac
exit 0