#!/usr/bin/env bash script_name="$(basename $(realpath $0))" working_dir="$(dirname $(realpath $0))" conf_file="${working_dir}/conf/${script_name%%.*}.conf" log_file="$(mktemp)" random_prefix="$(head -c 300 /dev/urandom | tr -cd 'a-zA-Z0-9' | head -c 8)" backup_date=$(date +%Y-%m-%d-%H%M) # ============= # --- Some Variables # ============= declare -a lx_container_arr=() declare -a lx_running_container_arr=() # ============= # --- Some Functions # ============= clean_up() { if [[ -f "$_backup_crontab_file" ]]; then blank_line echononl " (Re)Install Crontab from previously saved crontab file '$_backup_crontab_file'.." crontab $_backup_crontab_file >> $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed error "$(cat $log_file)" fi fi # Perform program exit housekeeping rm -f $log_file rm -rf /tmp/*.${random_prefix} blank_line exit $1 } echononl(){ if $terminal ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n "[ \033[5m\033[1m....\033[m ]\033[13G$*\\c" 1>&2 else echo -e -n "[ \033[5m\033[1m....\033[m ]\033[13G$*" 1>&2 fi rm /tmp/shprompt$$ fi } echo_print_command() { if $terminal ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n "\n\033[13G\033[1m$*\\c\033[m" 1>&2 else echo -e -n "\n\033[13G\033[1m$*\033[m" 1>&2 fi rm /tmp/shprompt$$ fi } echo_done() { if $terminal ; then echo -e "\033[1G[ \033[1;32mdone\033[m ]" fi } echo_ok() { if $terminal ; then echo -e "\033[1G[ \033[1;32mok\033[m ]" fi } echo_ignore() { if $terminal ; then echo -e "\033[1G[ \033[1;33mignore\033[m ]" fi } echo_warning() { if $terminal ; then echo -e "\033[1G[ \033[1;33m\033[1mwarn\033[m ]" fi } echo_failed(){ if $terminal ; then echo -e "\033[1G[ \033[1;31mfail\033[m ]" fi } echo_skipped() { if $terminal ; then echo -e "\033[1G[ \033[1;37mskip\033[m ]" fi } echo_wait(){ if $terminal ; then echo -en "\033[1G[ \033[5m\033[1m...\033[m ]" fi } fatal (){ blank_line if $terminal ; then echo -e "[ \033[31m\033[1mFatal\033[m ] \033[13G\033[37m\033[1m$*\033[m" echo "" echo -e " \033[13G\033[31m\033[1mScript will be interrupted..\033[m\033[m" else echo "fatal: $*" echo "Script will be interrupted.." fi clean_up 1 } error(){ blank_line if $terminal ; then echo -e "[ \033[31m\033[1mFehler\033[m ]\033[13G$*" else echo "" echo "[ Error ]: $*" echo "" fi blank_line } warn (){ if $terminal ; then echo "" echo -e "[ \033[33m\033[1mWarning\033[m ]\033[13G$*" echo "" fi } info (){ if $terminal ; then echo "" echo -e "[ \033[32m\033[1mInfo\033[m ]\033[13G$*" echo "" fi } ## - Check if a given array (parameter 2) contains a given string (parameter 1) ## - containsElement () { local e for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done return 1 } trim() { local var="$*" var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters echo -n "$var" } blank_line() { if $terminal ; then echo "" fi } usage() { if [ -n "$1" ];then echo -e "\n [ Error ]: $1" fi cat< /dev/null | grep -q "lxc" 2> /dev/null) \ || $(systemctl list-unit-files | grep -q "lxc"); then echononl "Get List of \033[1mall existing\033[m LX Contaoner.." _failed=false for _container in $(lxc-ls) ; do lx_container_arr+=("$_container") [[ $? -gt 0 ]] && _falied=true done if $_failed ; then echo_failed fatal "Getting list of \033[1mall\033[m LX-Container failed!" else echo_done fi _failed=false echononl "Get List of \033[1mrunning\033[m LX Contaoner.." for _container in $(lxc-ls --running) ; do lx_running_container_arr+=("$_container") [[ $? -gt 0 ]] && _falied=true done if $_failed ; then echo_failed fatal "Getting list of \033[1mrunning\033[m LX-Container failed!" else echo_done fi if [[ ${#lx_container_arr[@]} -gt 0 ]]; then for _lx_container in "${lx_container_arr[@]}" ; do echononl "Stopping Container ${_lx_container}.." if containsElement "$_lx_container" "${lx_running_container_arr[@]}" ; then lxc-stop -n ${_lx_container} > $log_file 2>&1 if [[ $? -gt 0 ]]; then echo_failed fatal "$(cat $log_file)" else echo_done fi else echo_skipped fi done fi else info "NO LX Container on this Host" fi if $terminal ; then echo "" echo "" echo -e "\033[13G\033[1mGoing to power off the system ..\033[m" echo "" else from_address="power-off@$(hostname -f)" to_address="root" content_type='Content-Type: text/plain;\n charset="utf-8"' subject="Power OFF System $(hostname -f).." msg=" Going to power off system '$(hostname -f)' .." echo -e "To:${to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \ | /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $to_address fi /sbin/poweroff exit 0