From c3dd3448b89e68d2a970b9d24706e86476fa2771 Mon Sep 17 00:00:00 2001 From: Christoph Date: Thu, 27 Mar 2025 14:29:42 +0100 Subject: [PATCH] machine_poweroff.sh: support LXC Hostsystems. --- machine_poweroff.sh | 302 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 300 insertions(+), 2 deletions(-) diff --git a/machine_poweroff.sh b/machine_poweroff.sh index 1a00cef..9371081 100755 --- a/machine_poweroff.sh +++ b/machine_poweroff.sh @@ -1,5 +1,174 @@ #!/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" @@ -15,7 +184,7 @@ 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 + + echo "" + echo -e " Going to power off the system '$(hostname -f)' .." + echo "" + sleep 5 + +fi + +/sbin/poweroff exit 0