From 9f453d02fdb5ee3f152923ac1aab5000e5a2c31a Mon Sep 17 00:00:00 2001 From: Christoph Date: Sat, 1 Oct 2022 16:05:33 +0200 Subject: [PATCH] Add script 'poweroff-LXC-host.sh'. --- poweroff-LXC-host.sh | 311 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 311 insertions(+) create mode 100755 poweroff-LXC-host.sh diff --git a/poweroff-LXC-host.sh b/poweroff-LXC-host.sh new file mode 100755 index 0000000..33318eb --- /dev/null +++ b/poweroff-LXC-host.sh @@ -0,0 +1,311 @@ +#/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 "\033[60G - \033[1m$*\\c\033[m" 1>&2 +# else +# echo -e -n "\033[60G - \033[1m$*\033[m" 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 +} + + + +# ============= +# --- Jobhandling +# ============= + +# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM +# - +trap clean_up SIGHUP SIGINT SIGTERM + + +# ============= +# --- Some Checks +# ============= + +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi + +[[ -n "$(which lxc-ls)" ]] || fatal "Seems not to be a LX Host. Missing command \033[33mlxc-ls" + + + +# ========== +# - Begin Main Script +# ========== + +# ---------- +# - Headline +# ---------- + +if $terminal ; then + echo "" + echo -e "\033[1m----------\033[m" + echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m" + echo -e "\033[1m----------\033[m" + echo "" +fi + + + +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 $terminal ; then + echo "" + echo "" + echo -e "\033[13G\033[1mMain part of script ..\033[m" + echo "" +fi + +if [[ ${#lx_container_arr[@]} -eq 0 ]]; then + warn "No LX Container found!" +fi + +if [[ ${#lx_container_arr[@]} -gt 0 ]]; then + if [[ ${#lx_running_container_arr[@]} -eq 0 ]]; then + warn "No running LX Container found!" + fi +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 + + +if $terminal ; then + echo "" + echo "" + echo -e "\033[13G\033[1mGoing to reboot the system ..\033[m" + echo "" +fi + +/sbin/poweroff + + +clean_up 0