Files
NodeMgmt/nodemgmt-scripts.sh
2019-01-28 16:21:07 -06:00

197 lines
6.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# MySQL-BU - MySQL Backup Scripts
action="$1"
VERS='1.01272019'
CERT_DAEMON='/usr/bin/certbot'
NODE_SERVICES=mysql,nginx,gogs,haproxy,keepalived,maxscale
source /opt/idssys/nodemgmt/settings.conf
source /opt/idssys/defaults/colors.inc
source /opt/idssys/defaults/default.inc
if [ "$1" = "service" ] || [ "$1" = "set-permissions" ]; then
NODE_HOSTS=`${ids_mysql_conn} -s -N -e "SELECT value FROM idssys.nodemgmt WHERE var = 'NODE_HOSTS'"`
fi
IFS=,
NODE_HOSTS=(${NODE_HOSTS})
NODE_SERVICES=(${NODE_SERVICES})
unset IFS
# ========================================================= #
case "$1" in
service)
containsElement "${2}" "${NODE_SERVICES[@]}"
if [[ "start,stop,restart" == *"${3}"* ]]; 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" = "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'; 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 [[ $(${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
if [ $(${NCMD} pgrep mysql | wc -l) -gt "0" ]; then
SER_MYSQL="${idsCL[Green]}Running${idsCL[Default]}"
else SER_MYSQL="${idsCL[Red]}Not Running${idsCL[Default]}"
fi
if [ $(${NCMD} pgrep haproxy | wc -l) -gt "0" ]; then
SER_HAP="${idsCL[Green]}Running${idsCL[Default]}"
else SER_HAP="${idsCL[Red]}Not Running${idsCL[Default]}"
fi
if [ $(${NCMD} pgrep nginx | wc -l) -gt "0" ]; then
SER_NGINX="${idsCL[Green]}Running${idsCL[Default]}"
else SER_NGINX="${idsCL[Red]}Not Running${idsCL[Default]}"
fi
if [ $(${NCMD} pgrep keepalived | wc -l) -gt "0" ]; then
SER_KA="${idsCL[Green]}Running${idsCL[Default]}"
else SER_KA="${idsCL[Red]}Not Running${idsCL[Default]}"
fi
if [ $(${NCMD} pgrep gogs | wc -l) -gt "0" ]; then
SER_GOGS="${idsCL[Green]}Running${idsCL[Default]}"
else SER_GOGS="${idsCL[Red]}Not Running${idsCL[Default]}"
fi
if [ $(${NCMD} pgrep maxscale | wc -l) -gt "0" ]; then
SER_MS="${idsCL[Green]}Running${idsCL[Default]}"
else SER_MS="${idsCL[Red]}Not Running${idsCL[Default]}"
fi
echo -e "${idsCL[Yellow]}-------------------------------------------------------------${idsCL[Default]}"
echo -e "Node hostname: ${nip}"
echo -e "MySQL: ${SER_MYSQL}"
echo -e "NGINX: ${SER_NGINX}"
echo -e "Gogs: ${SER_GOGS}"
echo -e "HAProxy: ${SER_HAP}"
echo -e "Keepalived: ${SER_KA}"
echo -e "MaxScale: ${SER_MS}"
nid=`expr $nid + 1`
done
echo -e "${idsCL[Yellow]}-------------------------------------------------------------${idsCL[Default]}"
echo ""
;;
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 7775 /var/www/gogs
echo -e "${idsCL[Green]}Complete${idsCL[Default]}"
echo ""
nid=`expr $nid + 1`
done
;;
*)
echo ""
echo -e "Usage: ${idsCL[Yellow]}nodemgmt${idsCL[Default]} { ${idsCL[Yellow]}service${idsCL[Default]} [service] [action] | ${idsCL[Yellow]}newcert${idsCL[Default]} [domain(,s)] | ${idsCL[Yellow]}certrenew${idsCL[Default]} }"
echo ""
exit 1
;;
esac
exit 0