#!/usr/bin/env bash # NodeMgmt - Galera/NGINX Node Management Scripts action="$1" if [ ! -f /opt/idssys/settings/nodemgmt.conf ]; then if [ ! -d /opt/idssys/settings ]; then mkdir /opt/idssys/settings fi mv /opt/idssys/nodemgmt/settings.conf /opt/idssys/settings/nodemgmt.conf fi source /opt/idssys/defaults/colors.inc source /opt/idssys/defaults/default.inc source /opt/idssys/settings/nodemgmt.conf source /opt/idssys/nodemgmt/defaults.inc # ========================================================= # STATUS() { if [ ! -z ${LOCAL_SERVICES+x} ]; then lip=$(/sbin/ip -o -4 addr list ens192 | awk '{print $4}' | cut -d/ -f1) #DIVIDER true echo -e "Node hostname: ${idsST[Bold]}${idsCL[LightCyan]}${NODE_HOSTNAME} (${lip})${idsST[Reset]}${idsCL[LightCyan]} - localhost${idsCL[Default]}" for srvc in "${LOCAL_SERVICES[@]}" do if [[ "mysql,nginx,gitea,haproxy,keepalived,maxscale" = *"${srvc}"* ]]; then spc=" "; else spc=""; fi echo -en "${NM_SERVICES[${srvc}]} $spc" if [ $(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 fi nid=1 for nip in "${NODE_HOSTS[@]}" do if [[ $(/sbin/ip -o -4 addr list ens192 | awk '{print $4}' | cut -d/ -f1) == *"${nip}"* ]]; then NCMD=''; LH='- localhost' else NCMD="ssh root@${nip}"; LH='' fi DIVIDER true echo -e "Node hostname: ${idsST[Bold]}${idsCL[LightCyan]}iDS-Node${nid} (${nip})${idsST[Reset]}${idsCL[LightCyan]} ${LH}${idsCL[Default]}" for srvc in "${NODE_SERVICES[@]}" do if [[ "mysql,nginx,gitea,haproxy,keepalived,maxscale" = *"${srvc}"* ]]; then spc=" "; else spc=""; fi echo -en "${NM_SERVICES[${srvc}]} $spc" 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 DIVIDER echo "" if [ -z $action ] || [ "${action}" = "gui" ]; then ENTER2CONTINUE fi } NEWCERT(){ echo -e "${idsCL[LightGreen]}Requesting Certificate for '${idsCL[Yellow]}${1}${idsCL[LightGreen]}'...${idsCL[Default]}" echo "" do_with_root $CERT_DAEMON certonly --webroot -w /var/www/html -d ${1} do_with_root chown -R root:letsencrypt /etc/letsencrypt do_with_root chmod -R 6775 /etc/letsencrypt echo -e "${idsCL[LightYellow]}Waiting for certifcate replication between the nodes...${idsCL[Default]}" echo "" sleep 20 SERVICE nginx reload } CERTRENEW(){ echo -e "${idsCL[LightGreen]}Renewing Certificates...${idsCL[Default]}" do_with_root $CERT_DAEMON renew --webroot -w /var/www/html 2>&1 | tee /opt/idssys/nodemgmt/cert-renewal.lastrun do_with_root chown -R root:letsencrypt /etc/letsencrypt 2>&1 | tee -a /opt/idssys/nodemgmt/cert-renewal.lastrun do_with_root chmod -R 6775 /etc/letsencrypt 2>&1 | tee -a /opt/idssys/nodemgmt/cert-renewal.lastrun echo -e "${idsCL[LightYellow]}Waiting for certifcate replication between the nodes...${idsCL[Default]}" echo "" sleep 20 SERVICE nginx reload 2>&1 | tee -a /opt/idssys/nodemgmt/cert-renewal.lastrun } NIGHTLYRENEW(){ rm -f /opt/idssys/nodemgmt/cert-renewal.lastrun do_with_root $CERT_DAEMON renew --webroot -w /var/www/html &>> /opt/idssys/nodemgmt/cert-renewal.lastrun do_with_root chown -R root:letsencrypt /etc/letsencrypt &>> /opt/idssys/nodemgmt/cert-renewal.lastrun do_with_root chmod -R 6775 /etc/letsencrypt &>> /opt/idssys/nodemgmt/cert-renewal.lastrun sleep 20 SERVICE nginx reload &>> /opt/idssys/nodemgmt/cert-renewal.lastrun } LISTCERTS(){ #DIVIDER true for certdir in /etc/letsencrypt/live/*/ ; do SUBJECT=$(openssl x509 -in ${certdir}/cert.pem -noout -subject|grep -oP '(?<=CN = )[^,]+'|sort -uV) SUBJECTNAMES=$(openssl x509 -in ${certdir}/cert.pem -noout -text|grep -oP '(?<=DNS:|IP Address:)[^,]+'|sort -uV) SUBJECTNAMES=${SUBJECTNAMES//$'\n'/, } SUBJECTNAMES=$(echo $SUBJECTNAMES | sed "s/${SUBJECT}, //g") SUBJECTNAMES=$(echo $SUBJECTNAMES | sed "s/, ${SUBJECT}//g") SUBJECTNAMES=$(echo $SUBJECTNAMES | sed "s/${SUBJECT}//g") CERTEXPIRE=$(date -d "$(: | openssl x509 -in ${certdir}/cert.pem -text | grep 'Not After' |awk '{print $4,$5,$7}')" '+%s'); DAYS=14; DUEIN=$(($(date +%s) + (86400*$DAYS))); DIVIDER true echo -e "Certificate Subject: ${idsST[Bold]}${idsBG[Blue]}${SUBJECT}${idsST[Reset]}${idsBG[Default]}" #if [ "${#SUBJECT_NAMES[*]}" -gt 1 ]; then if [ "$SUBJECTNAMES" != "" ]; then echo -e "Alternate Subject(s): ${idsCL[Cyan]}${SUBJECTNAMES}${idsCL[Default]}" fi if [ $DUEIN -gt $CERTEXPIRE ]; then echo -e "Expiration Date: ${idsST[Bold]}${idsCL[Red]}$(date -d @${CERTEXPIRE} '+%m-%d-%Y')${idsST[Reset]}${idsCL[Default]}" else echo -e "Expiration Date: ${idsCL[Green]}$(date -d @${CERTEXPIRE} '+%m-%d-%Y')${idsCL[Default]}" fi echo "" done if [ -z $action ] || [ "${action}" = "gui" ]; then ENTER2CONTINUE fi echo "" } BACKUP(){ BACKUP_FOLDER=/opt/idssys/backups/node-backup #BACKUP_FOLDER=/opt/idssys/backups/node-backups/${NODE_HOSTNAME} #/`date +%Y-%m-%d` #BACKUP_TIMEFOLDER=${BACKUP_DAYFOLDER}/`date +%H-%M` if [ "${action}" != "backup" ]; then echo -e "${idsCL[Green]}Backing up Node Settings and Files...${idsCL[Default]}" DIVIDER true fi if [ ! -d ${BACKUP_FOLDER} ]; then mkdir -p ${BACKUP_FOLDER} fi declare -A BACKUP_ITEMS BACKUP_ITEMS[nginx-settings]=/etc/nginx BACKUP_ITEMS[letsencrypt-certs]=/etc/letsencrypt BACKUP_ITEMS[gitea]=/var/lib/gitea BACKUP_ITEMS[nginx-logs]='/var/www/!NGINX-Logs' #BACKUP_ITEMS[webserver-files]=/var/www for item in "${!BACKUP_ITEMS[@]}" do if [ "${action}" != "backup" ]; then echo -en "Backing up '${item}'... " fi #tar -czPf ${BACKUP_FOLDER}/${item}.tar.gz -C ${BACKUP_ITEMS[$item]} . tar -czPf ${BACKUP_FOLDER}/${item}.tar.gz -g ${BACKUP_FOLDER}/backup.snar -C ${BACKUP_ITEMS[$item]} . --exclude='./data/repository' --exclude='*/.stfolder' --exclude='*/.stversions' --exclude='*/.git' if [ "${action}" != "backup" ]; then echo -e "${idsCL[Green]}Ok${idsCL[Default]}" fi done touch ${BACKUP_FOLDER}/backup.lastrun if [ "${action}" != "backup" ]; then echo "" DIVIDER true fi if [ -z $action ] || [ "${action}" = "gui" ]; then ENTER2CONTINUE fi } DELSITE(){ while [ $# -gt 0 ]; do case "$1" in -site) DEL_SITE=${2};; -ssl) DEL_SSL=${2};; -list) DELSITES; exit 0;; -*) echo "Invalid option: '${1}' requires an argument" 1>&2 echo "" echo -e "Usage: ${idsCL[Yellow]}nodemgmt delsite${idsCL[Default]} {" width=33 printf "%-${width}s- %s\n" " -site {FQDN address}" "(*required)" printf "%-${width}s- %s\n" " -ssl {yes or [no]}" "Delete SSL certs as well" printf "%-${width}s- %s\n" " -list" "List sites (same as running nodemgmt delsites)" echo "}" exit 1;; esac shift done if [ -z ${DEL_SSL+x} ] || [[ $DEL_SSL =~ ^[Nn]$ ]]; then DEL_SSL=no elif [[ $DEL_SSL =~ ^[Yy]$ ]]; then DEL_SSL=yes fi if [ ! -z ${DEL_SITE+x} ]; then echo -e "${idsCL[LightRed]}Deleting site '${idsCL[Red]}${DEL_SITE}${idsCL[LightRed]}'...${idsCL[Default]}" echo "" echo -e "${idsCL[LightRed]}[[Removing Files and Folders]]${idsCL[Default]}" echo -e "${idsCL[LightRed]}-------------------------------------------${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 "Removing from iDS-Node${nid} ($nip)... ${idsCL[Default]}" if [ -f /etc/nginx/sites-available/${DEL_SITE} ]; then ${NCMD} rm -f /etc/nginx/sites-available/${DEL_SITE} ${NCMD} rm -f /etc/nginx/sites-enabled/${DEL_SITE} fi if [ -d /var/www/${DEL_SITE} ]; then ${NCMD} rm -rf /var/www/${DEL_SITE} fi if [ "${DEL_SSL}" = "yes" ] && [ -d /etc/letsencrypt/archive/${DEL_SITE} ]; then ${NCMD} rm -rf /etc/letsencrypt/archive/${DEL_SITE} ${NCMD} rm -rf /etc/letsencrypt/live/${DEL_SITE} ${NCMD} rm -f /etc/letsencrypt/renewal/${DEL_SITE}.conf fi echo -e "${idsCL[Green]}OK${idsCL[Default]}" nid=`expr $nid + 1` done echo "" SERVICE nginx reload echo -e "${idsCL[LightRed]}Site has been deleted.${idsCL[Default]}" else echo "Missing arguments" echo "" echo -e "Usage: ${idsCL[Yellow]}nodemgmt delsite${idsCL[Default]} {" width=33 printf "%-${width}s- %s\n" " -site {FQDN address}" "Site to delete" printf "%-${width}s- %s\n" " -ssl {yes or [no]}" "Delete SSL certs as well" printf "%-${width}s- %s\n" " -list" "List sites (same as running nodemgmt delsites)" echo "}" exit 1 fi } DELSITES(){ echo echo -e "${idsCL[Red]}Select a site to delete...${idsCL[Default]}" DIVIDER true sid=1 for siteconf in /etc/nginx/sites-available/* ; do [ -e "$siteconf" ] || continue IFS='/'; site_conf=(${siteconf}); unset IFS SITES[${sid}]=${site_conf[4]} sid=`expr $sid + 1` done for s in "${!SITES[@]}"; do echo " [${s}] ${SITES[${s}]}" done echo "" if [ -z $action ] || [ "${action}" = "gui" ]; then echo " [B] Back" fi echo " [Q] Quit" echo "" echo -e -n "${idsCL[LightYellow]}Please select a site from above from above:${idsCL[Default]} " read selsite echo "" if [ -z ${SITES[$selsite]} ] && [ "${selsite}" != "Q" ] && [ "${selsite}" != "q" ] && [ "${selsite}" != "B" ] && [ "${selsite}" != "b" ]; then echo "Thats an invaild option," echo "please select a valid option only." sleep 1 DELSITES exit 0 elif [ "${selsite}" = "Q" ] || [ "${selsite}" = "q" ]; then exit 0 elif [ "${selsite}" = "B" ] || [ "${selsite}" = "b" ]; then GUI else while : do echo -e -n "${idsCL[LightRed]}Are you sure you want to delete '${idsST[Bold]}${idsCL[Red]}${SITES[${selsite}]}${idsST[Reset]}${idsCL[LightRed]}'? [y/N]${idsCL[Default]} " read response echo "" if [[ $response =~ ^[Yy]$ ]]; then echo -e -n "${idsCL[LightRed]}Do you also want to delete the certs for '${SITES[${selsite}]}' as well? [y/N]${idsCL[Default]} " read sslresponse DELSITE -site ${SITES[${selsite}]} -ssl ${sslresponse} echo "" DIVIDER ENTER2CONTINUE break else break fi done DELSITES exit 0 fi if [ -z $action ] || [ "${action}" = "gui" ]; then ENTER2CONTINUE fi } NEWSITE(){ while [ $# -gt 0 ]; do case "$1" in -site) NEW_SITE=${2};; -type) SITE_TYPE=${2};; -ssl) CREATE_SSL=${2};; -proxy_scheme) PROXYSCHEME=${2};; -proxy_host) PROXYHOST=${2};; -proxy_port) PROXYPORT=${2};; -h | -help | --help) echo "Missing arguments" echo "" echo -e "Usage: ${idsCL[Yellow]}nodemgmt newcert${idsCL[Default]} {" width=33 printf "%-${width}s- %s\n" " -site {FQDN address(,es)}" "(new site and aliases, comma seperated)" printf "%-${width}s- %s\n" " -ssl {yes or no}" "(defaults to yes)" printf "%-${width}s- %s\n" " -type {'local' or 'proxy'}" "(defaults to local)" printf "%-${width}s- %s\n" " -port {host port}" "(proxy backend host)" printf "%-${width}s- %s\n" " -host {IP or FQDN}" "(proxy backend port)" printf "%-${width}s- %s\n" " -scheme {http or https}" "(proxy backend scheme)" echo "}" exit 1;; esac shift done #if [ -z ${SITE_TYPE+x} ]; then SITE_TYPE=local; fi #if [ -z ${CREATE_SSL+x} ]; then CREATE_SSL=true; fi if [ -z ${NEW_SITE+x} ]; then echo -e -n "${idsCL[LightCyan]}New site domain name: ${idsCL[Default]}" read NEW_SITE showdivide=yes fi if [ -z ${CREATE_SSL+x} ]; then echo -e -n "${idsCL[LightCyan]}Create SSL for site? [Y/n] ${idsCL[Default]}" read CREATE_SSL showdivide=yes if [[ $CREATE_SSL =~ ^[Yy]$ ]]; then CREATE_SSL=yes else CREATE_SSL=no fi fi if [ -z ${SITE_TYPE+x} ]; then echo -e -n "${idsCL[LightCyan]}Site type ([local]/proxy): ${idsCL[Default]}" read SITE_TYPE showdivide=yes if [ "${SITE_TYPE}" != "proxy" ]; then SITE_TYPE=local fi fi if [ "${SITE_TYPE}" = "proxy" ]; then if [ -z ${PROXYHOST+x} ]; then echo -e -n "${idsCL[LightCyan]}What is the proxy backend address (IP or FQDN): ${idsCL[Default]}" read PROXYHOST showdivide=yes fi if [ -z ${PROXYPORT+x} ]; then echo -e -n "${idsCL[LightCyan]}What is the proxy backend port (tcp port): ${idsCL[Default]}" read PROXYPORT showdivide=yes fi if [ -z ${PROXYSCHEME+x} ]; then echo -e -n "${idsCL[LightCyan]}What is the proxy backend scheme (http/https): ${idsCL[Default]}" read PROXYSCHEME showdivide=yes fi fi if [ "${showdivide}" = "yes" ]; then DIVIDER; fi echo "" width=14 printf "%-${width}s: %s\n" "New site" "${NEW_SITE}" printf "%-${width}s: %s\n" "Create SSL" "${CREATE_SSL}" printf "%-${width}s: %s\n" "Site type" "${SITE_TYPE}" if [ "${SITE_TYPE}" = "proxy" ]; then printf "%-${width}s: %s\n" "Proxy host" "${PROXYHOST}" printf "%-${width}s: %s\n" "Proxy port" "${PROXYPORT}" printf "%-${width}s: %s\n" "Proxy scheme" "${PROXYSCHEME}" fi echo -e -n "${idsCL[LightRed]}Is this information correct? [Y/n]${idsCL[Default]} " read response if [[ $response =~ ^[Yy]$ ]]; then if [[ ${NEW_SITE} == *","* ]]; then IFS=','; NEW_SITES=(${NEW_SITE}); unset IFS MAIN_SITE=${NEW_SITES[0]} NGINX_SERVERNAME=${NEW_SITE//[,]/ } else MAIN_SITE=${NEW_SITE} NGINX_SERVERNAME=${NEW_SITE} fi if [ "${SITE_TYPE}" = "proxy" ]; then if [ ! -z ${PROXYSCHEME+x} ] && [ ! -z ${PROXYHOST+x} ] && [ ! -z ${PROXYPORT+x} ]; then GO=true; fi else GO=true fi if [ "${GO}" = "true" ]; then echo -e "${idsCL[LightGreen]}Setting up new site for '${idsCL[Yellow]}${MAIN_SITE}${idsCL[LightGreen]}' {${NGINX_SERVERNAME}}...${idsCL[Default]}" echo "" if [ "${SITE_TYPE}" = "local" ]; then echo -e "server { listen 8080;" > /etc/nginx/sites-available/${MAIN_SITE} if [ "${CREATE_SSL}" = "yes" ]; then echo -e " listen 8443 ssl http2;" >> /etc/nginx/sites-available/${MAIN_SITE} fi echo -e " server_name ${NGINX_SERVERNAME}; set \$base /var/www/${MAIN_SITE}; root \$base/public_html; access_log /var/log/nginx/${MAIN_SITE}-access.log; error_log /var/log/nginx/${MAIN_SITE}-error.log warn;" >> /etc/nginx/sites-available/${MAIN_SITE} if [ "${CREATE_SSL}" = "yes" ]; then echo -e " ssl_certificate /etc/letsencrypt/live/${MAIN_SITE}/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/${MAIN_SITE}/privkey.pem; include conf.d/include/ssl-ciphers.conf;" >> /etc/nginx/sites-available/${MAIN_SITE} fi echo -e " index index.php; location / { try_files \$uri \$uri/ /index.php?\$query_string;" >> /etc/nginx/sites-available/${MAIN_SITE} if [ "${CREATE_SSL}" = "yes" ]; then echo -e " include conf.d/include/force-ssl.conf;" >> /etc/nginx/sites-available/${MAIN_SITE} fi echo -e " } location ~ \.php\$ { fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; include conf.d/include/php_fastcgi.conf; } include conf.d/include/general.conf;" >> /etc/nginx/sites-available/${MAIN_SITE} if [ "${CREATE_SSL}" = "yes" ]; then echo -e " include conf.d/include/letsencrypt-acme-challenge.conf;" >> /etc/nginx/sites-available/${MAIN_SITE} fi echo -e "}" >> /etc/nginx/sites-available/${MAIN_SITE} for nip in "${NODE_HOSTS[@]}"; do if [[ $(/sbin/ip -o -4 addr list ens192 | awk '{print $4}' | cut -d/ -f1) == *"${nip}"* ]]; then NCMD='' else NCMD="ssh root@${nip}" fi ${NCMD} mkdir -p /var/www/${MAIN_SITE}/{public_html,nginx_logs} done SET-PERMISSIONS ${MAIN_SITE} else echo -e "server { set \$forward_scheme ${PROXYSCHEME}; set \$server \"${PROXYHOST}\"; set \$port ${PROXYPORT}; listen 8080;" > /etc/nginx/sites-available/${MAIN_SITE} if [ "${CREATE_SSL}" = "yes" ]; then echo -e " listen 8443 ssl http2;" >> /etc/nginx/sites-available/${MAIN_SITE} fi echo -e " server_name ${NGINX_SERVERNAME};" >> /etc/nginx/sites-available/${MAIN_SITE} if [ "${CREATE_SSL}" = "yes" ]; then echo -e " include conf.d/include/letsencrypt-acme-challenge.conf; include conf.d/include/ssl-ciphers.conf; ssl_certificate /etc/letsencrypt/live/${MAIN_SITE}/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/${MAIN_SITE}/privkey.pem;" >> /etc/nginx/sites-available/${MAIN_SITE} fi echo -e " access_log /var/log/nginx/proxy-${MAIN_SITE}.log proxy; location / {" >> /etc/nginx/sites-available/${MAIN_SITE} if [ "${CREATE_SSL}" = "yes" ]; then echo -e " include conf.d/include/force-ssl.conf;" >> /etc/nginx/sites-available/${MAIN_SITE} fi echo -e " include conf.d/include/proxy.conf; } } " >> /etc/nginx/sites-available/${MAIN_SITE} fi ln -s /etc/nginx/sites-available/${MAIN_SITE} /etc/nginx/sites-enabled/${MAIN_SITE} if [ "${CREATE_SSL}" = "yes" ]; then NEWCERT ${NEW_SITE} else SERVICE nginx reload fi echo "" echo -e "${idsCL[LightGreen]}The new site for '${idsCL[LightGreen]}${NEW_SITE}${idsCL[Default]}' has been created.${idsCL[Default]}" echo "" else echo "Missing proxy arguments" Exit 1 fi else ${SCRIPT} newsite exit 0 fi } STATUS-CHECK(){ if [ "${STATUS_CHECK_EMAIL}" != "" ]; then if [ ! -z ${LOCAL_SERVICES+x} ]; then lip=$(/sbin/ip -o -4 addr list ens192 | awk '{print $4}' | cut -d/ -f1) for srvc in "${LOCAL_SERVICES[@]}" do if [ $(pgrep ${srvc} | wc -l) -lt "1" ]; then echo "${NM_SERVICES[${srvc}]} is down" | mail -s "${NODE_HOSTNAME}-${nip}" ${STATUS_CHECK_EMAIL} touch ${FOLDER}/localhost-${srvc}.down elif [ -f ${FOLDER}/localhost-${srvc}.down ]; then echo "${NM_SERVICES[${srvc}]} is back UP!" | mail -s "${NODE_HOSTNAME}-${nip}" ${STATUS_CHECK_EMAIL} rm -f ${FOLDER}/localhost-${srvc}.down fi done fi if [ -z ${LOCAL_SERVICES+x} ] || [ "${2}" = "all" ]; then nid=1 for nip in "${NODE_HOSTS[@]}" do if [[ $(/sbin/ip -o -4 addr list ens192 | awk '{print $4}' | cut -d/ -f1) == *"${nip}"* ]]; then NCMD='' else NCMD="ssh root@${nip}" fi for srvc in "${NODE_SERVICES[@]}" do if [ $(${NCMD} pgrep ${srvc} | wc -l) -lt "1" ]; then echo "${NM_SERVICES[${srvc}]} is down" | mail -s "iDS-Node${nid}-${nip}" ${STATUS_CHECK_EMAIL} touch ${FOLDER}/${nip}-${srvc}.down elif [ -f ${FOLDER}/${nip}-${srvc}.down ]; then echo "${NM_SERVICES[${srvc}]} is back UP!" | mail -s "iDS-Node${nid}-${nip}" ${STATUS_CHECK_EMAIL} rm -f ${FOLDER}/${nip}-${srvc}.down fi done nid=`expr $nid + 1` done fi fi } SET-PERMISSIONS(){ if [ "${1}" != "" ]; then NEW_SITE=${1} echo -e "${idsCL[Yellow]}Setting new site folder permissions for (/var/www/${NEW_SITE})${idsCL[Default]}" fi 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 [ "${NEW_SITE}" != "" ]; then echo -en "${idsCL[Cyan]}iDS-Node${nid} (${nip}) ${idsCL[Default]}" ${NCMD} chown -R www-data:www-data /var/www/$NEW_SITE ${NCMD} chmod -R 7775 /var/www/$NEW_SITE echo -e "${idsCL[Green]}Complete${idsCL[Default]}" else echo -e "${idsCL[Yellow]}Setting folder permissions for iDS-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]}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 "" fi nid=`expr $nid + 1` done if [ "${NEW_SITE}" != "" ]; then echo ""; fi if [ -z $action ] || [ "${action}" = "gui" ]; then ENTER2CONTINUE fi } SERVICES(){ echo "" echo -e "${idsCL[Green]}Node Services${idsCL[Default]}" DIVIDER true while : do echo "" echo -e "${idsCL[Green]}Node Services${idsCL[Default]}" DIVIDER true s=1 for srvc in "${NODE_SERVICES[@]}" do echo " [${s}] ${NM_SERVICES[${srvc}]}" s=`expr $s + 1` done if [ -z $action ] || [ "${action}" = "gui" ]; then echo " [B] Back" fi echo " [Q] Quit" echo "" echo -e -n "${idsCL[LightYellow]}Please select a service from above:${idsCL[Default]} " read selsrvc echo "" if [ -z ${NM_SERVICES[${selsrvc}]} ] && [ "${selsrvc}" != "Q" ] && [ "${selsrvc}" != "q" ] && [ "${selsrvc}" != "B" ] && [ "${selsrvc}" != "b" ]; then echo "Thats an invaild option," echo "please select a valid option only." sleep 1 SERVICES exit 0 elif [ "${selsrvc}" = "Q" ] || [ "${selsrvc}" = "q" ]; then exit 0 elif [ "${selsrvc}" = "B" ] || [ "${selsrvc}" = "b" ]; then GUI else echo "" echo -e "${idsCL[Green]}Node Service Selected: '${idsST[Bold]}${NM_SERVICES[${selsrvc}]}${idsST[Reset]}${idsCL[Green]}'${idsCL[Default]}" DIVIDER true s=1 for srvcopt in "${!NM_SRVCOPTS[@]}"; do do echo " [${s}] ${srvcopts~}" s=`expr $s + 1` done if [ -z $action ] || [ "${action}" = "gui" ]; then echo " [B] Back" fi echo " [Q] Quit" echo "" echo -e -n "${idsCL[LightYellow]}Please select a service from above:${idsCL[Default]} " read selsrvcopt echo "" if [ -z ${NM_SRVCOPTS[${selsrvcopt}]} ] && [ "${selsrvc}" != "Q" ] && [ "${selsrvc}" != "q" ] && [ "${selsrvc}" != "B" ] && [ "${selsrvc}" != "b" ]; then echo "Thats an invaild option," echo "please select a valid option only." sleep 1 SERVICES exit 0 elif [ "${selsrvc}" = "Q" ] || [ "${selsrvc}" = "q" ]; then exit 0 elif [ "${selsrvc}" = "B" ] || [ "${selsrvc}" = "b" ]; then SERVICES else fi fi done } SERVICE(){ if [ "${NM_SERVICES[${1}]}" = "" ]; then echo -e "${idsCL[Red]}(${1}) is not an allowed service.${idsCL[Default]}" exit 1 fi if [[ "start,stop,restart,reload,enable,disable" != *"${2}"* ]] && [ "${1}" != "daemon-reload" ]; then echo -e "${idsCL[Red]}(${3}) is not an allowed service action.${idsCL[Default]}" exit 1 fi if [ "$2" = "start" ] && [ "$1" = "haproxy" ]; then TACT='restart'; else TACT="${2}"; fi echo -e "${idsCL[LightGreen]}[[${NM_SERVICES[${1}]} ${NM_SRVCOPTS[${2}]}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 "${NM_SRVCOPTS[${2}]}ing on iDS-Node${nid} ($nip)... ${idsCL[Default]}" $NCMD systemctl $2 $1 if [[ "enable,disable" = *"${2}"* ]] || [ "${1}" = "daemon-reload" ]; then echo -e "${idsCL[Green]}OK${idsCL[Default]}" elif [[ $(${NCMD} pgrep ${1} | wc -l) -gt "0" ]]; then echo -e "${idsCL[Green]}OK${idsCL[Default]}" elif [ "$2" = "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]}${NM_SERVICES[${1}]} has been ${NM_SRVCOPTS[${2}]}ed${idsCL[Default]}" echo "" } GUI(){ DISP_HEADER true true while : do echo " [1] Node Status" echo " [2] Node Services" echo "" echo " [3] New Site" echo " [4] Delete Site" echo "" echo " [5] New Certificate" echo " [6] Renew Certificates" echo " [7] List Certificates" echo "" echo " [8] Set folder permissions on nodes" echo " [9] Backup Node Settings and Files" echo "" echo " [Q] Quit" echo "" echo "" echo -e -n "${idsCL[LightYellow]}Please select an [ActionItem] from above:${idsCL[Default]} " read -n 1 opt echo "" case $opt in 1) STATUS; GUI;; 2) GUI;; 3) NEW_SITE; GUI;; 4) DELSITES; GUI;; 5) GUI;; 6) CERTRENEW; GUI;; 7) LISTCERTS; GUI;; 8) SET-PERMISSIONS; GUI;; 9) BACKUP; GUI;; [Qq]) EXIT1 exit 0;; *) echo "Thats an invaild option,"; echo "please select a valid option only."; sleep 1;; esac done } if [ ${action-x} ]; then if [ "$1" != "status-check" ] && [ "$1" != "nightlyrenew" ] && [ "$1" != "backup" ]; then DISP_HEADER fi case $action in newcert) NEWCERT ${2};; certrenew) CERTRENEW;; listcerts) LISTCERTS;; nightlyrenew) NIGHTLYRENEW;; delsite) DELSITE ${2} ${3} ${4} ${5};; delsites) DELSITES;; newsite) NEWSITE ${2} ${3} ${4} ${5} ${6} ${7} ${8} ${9} ${10} ${11} ${12} ${13} ${14};; update) ;; backup) BACKUP;; status) STATUS ${2};; status-check) STATUS-CHECK ${2};; set-permissions) DIVIDER; SET-PERMISSIONS ${2};; service) SERVICE ${2} ${3};; gui) GUI;; *) 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 else GUI fi exit 0