#!/bin/bash # Exit codes: # 1 : # 2 : # 3 : # 4 : ######################################################################## # INIT ######################################################################## CONFIGFILE="$(dirname "$(readlink -f "$0")")/$(basename "$(readlink -f "$0")" .sh).config" ######################################################################## #/INIT ######################################################################## ######################################################################## # # CONSTANTS # ######################################################################## # colors BOLD="\e[1m" GREEN="\e[32m" LIGHTGREEN="${BOLD}${GREEN}" #RED="\033[1;31m" LIGHTRED="\033[1;31m" BLUE="\e[34m" LIGHTBLUE="${BOLD}${BLUE}" YELLOW="\e[33m" LIGHTYELLOW="${BOLD}${YELLOW}" #WHITE="\033[0;37m" RESET="\033[0;00m" NOW="$(date +%Y%m%d%H%M%S)" # set to 0 to output messages without colors NOCOLOR=0 # set to 0 for debugging DEBUG=0 #DEBUG=1 EXITCODE=0 ######################################################################## # # / CONSTANTS # ######################################################################## ######################################################################## # # VARIABLES # ######################################################################## SCRIPTLOG="$(dirname "$(readlink -f "$0")")/logs/$(basename "$0" .sh)_script_${NOW}.log" SCRIPTLOGERR="$(dirname "$(readlink -f "$0")")/logs/$(basename "$0" .sh)_script_${NOW}.err" # delete logs older than PURGE_LOG_TIME days PURGE_LOG_TIME=30 ######################################################################## # # / VARIABLES # ######################################################################## ######################################################################## # # FUNCTIONS # ######################################################################## usage() { printf "%s${LIGHTRED}USAGE:${RESET} $0 -u USERNAME -t TEMPLAGE_FILE [-h] [-D] -u USERNAME ${LIGHTGREEN}USERNAME${RESET} to be created -t TEMPLATE_FILE ${LIGHTGREEN}TEMPLATE_FILE${RESET} to be used (must exist) -p PASSWORD ${LIGHTGREEN}PASSWORD${RESET} for the user -h this help -D DEBUG mode " # VERY INITIAL CHECKS } no_colors_output() { if [[ ${NOCOLOR} -eq 0 ]] ; then # colors BOLD="" GREEN="" LIGHTGREEN="" LIGHTRED="" BLUE="" LIGHTBLUE="" YELLOW="" LIGHTYELLOW="" RESET="" fi } printmsg() { echo -e "$*" } output_log() { if [[ "${QUIETOUTPUT}" == true ]]; then printmsg "$*" >> "${OUTPUTFILE}" else printmsg "$*" | tee -a "${OUTPUTFILE}" fi } abort_message() { local ABORTCODE=1 [[ ${EXITCODE} -ne 0 ]] && ABORTCODE=${EXITCODE} printmsg "${LIGHTRED}ERROR${RESET}: $*" exit ${ABORTCODE} } # debug_me uses variable ${DEBUG} debug_me() { if [[ "${DEBUG}" && ${DEBUG} -eq 0 ]] ; then printmsg "${LIGHTBLUE}[DEBUG]${RESET}: $*" fi } warning_message() { printmsg "${LIGHTYELLOW}[WARNING]${RESET}: $*" } ok_message() { printmsg "${LIGHTGREEN}[OK]${RESET}: $*" } nice_countdown() { local WAIT_TIME=$1 local DEFAULT_WAIT_TIME=10 if [[ ! ${WAIT_TIME} ]] ; then WAIT_TIME=${DEFAULT_WAIT_TIME} fi local WARNING=$((WAIT_TIME*2/3)) local CRITICAL=$((WAIT_TIME/3)) for ((x=WAIT_TIME; x>0; x--)) ; do if [[ ${x} -gt ${WARNING} ]] ; then MSG_COLOR=${LIGHTGREEN} elif [[ ${x} -gt ${CRITICAL} ]] ; then MSG_COLOR=${LIGHTYELLOW} else MSG_COLOR=${LIGHTRED} fi printf "\r${MSG_COLOR}%s${RESET} .." "${x}" sleep 1 done echo } # ssh_it uses variable ${DEBUG} ssh_it() { local RESSULT=99 if [[ "${DEBUG}" && ${DEBUG} -eq 0 ]] ; then ${SSHIT} "$*" RESSULT=$? else ${SSHIT} "$*" 2>/dev/null RESSULT=$? fi return "${RESSULT}" } log_maintenance() { debug_me "log_maintenance" find "$(dirname "${SCRIPTLOG}")" -mindepth 1 -maxdepth 1 -name "*$(basename "$0" .sh)_script_*.log" -mtime +${PURGE_LOG_TIME} -delete find "$(dirname "${SCRIPTLOG}")" -mindepth 1 -maxdepth 1 -name "*$(basename "$0" .sh)_script_*.err" -mtime +${PURGE_LOG_TIME} -delete } ######################################################################## # # / FUNCTIONS # ######################################################################## ######################################################################## # # MAIN # ######################################################################## no_colors_output [[ ! -d $(dirname "${SCRIPTLOG}") ]] && mkdir -p "$(dirname "${SCRIPTLOG}")" [[ ! -d $(dirname "${SCRIPTLOGERR}") ]] && mkdir -p "$(dirname "${SCRIPTLOGERR}")" # DETECTING if the script is run by cron if [[ "$(tty)" = "not a tty" ]] ; then set -x exec > "${SCRIPTLOG}" exec 2> "${SCRIPTLOGERR}" fi if [[ "${DEBUG}" && ${DEBUG} -eq 0 ]] ; then echo -e "${BLUE}DEBUGMODE${RESET} is on" echo -e "\t SCRIPTLOG will be ${SCRIPTLOG}" echo -e "\t SCRIPTLOGERR will be ${SCRIPTLOGERR}" set -x #exec 2> ${SCRIPTLOGERR} exec 2> >(tee "${SCRIPTLOGERR}" >&2) exec > >(tee "${SCRIPTLOG}" >&1) fi # just in case the script need arguments if [[ ! "$*" ]] ; then usage exit 0 fi while getopts "o:hDF" arg; do case $arg in o) OPTION=${OPTARG} ;; h) usage ;; F) MAYTHEFORCEBEWITHYOU=true echo -e "${LIGHTBLUE}The force is strong in you?${RESET}" ;; D) DEBUG=0 ;; *) usage ;; esac done [[ ! -f ${CONFIGFILE} ]] && abort_message "CONFIGFILE ${CONFIGFILE} NOT FOUND" . "${CONFIGFILE}" #[[ ${DEBUG} -eq 0 ]] && DEBUGME="bash -x" log_maintenance exit ${EXITCODE} ######################################################################## # # / MAIN # ########################################################################