admin-stuff/machine_poweroff.sh

366 lines
7.3 KiB
Bash
Executable File

#!/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<<EOF
Usage: $(basename $0) YYYY-MM-DD
$(basename $0) - Scripts shuts down this machine if the given date is the actual date.
The most common practice is, to use this script as cronjob.
EOF
clean_up 1
}
isValidDate() {
DATE="${1}"
# Autorized separator char ['space', '/', '.', '_', '-']
SEPAR="([ \/._-])?"
# Date format day[01..31], month[01,03,05,07,08,10,12], year[1900..2099]
DATE_1="((([123][0]|[012][1-9])|3[1])${SEPAR}(0[13578]|1[02])${SEPAR}(19|20)[0-9][0-9])"
# Date format day[01..30], month[04,06,09,11], year[1900..2099]
DATE_2="(([123][0]|[012][1-9])${SEPAR}(0[469]|11)${SEPAR}(19|20)[0-9][0-9])"
# Date format day[01..28], month[02], year[1900..2099]
DATE_3="(([12][0]|[01][1-9]|2[1-8])${SEPAR}02${SEPAR}(19|20)[0-9][0-9])"
# Date format day[29], month[02], year[1904..2096]
DATE_4="(29${SEPAR}02${SEPAR}(19|20(0[48]|[2468][048]|[13579][26])))"
# Date 29.02.2000
DATE_5="(29${SEPAR}02${SEPAR}2000)"
# Match the date in the Regex
if [[ "${DATE}" =~ ^(${DATE_1}|${DATE_2}|${DATE_3}|${DATE_4}|${DATE_5})$ ]] ; then
return 0
else
return 1
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
[ $# -ne "1" ] && usage "Wrong number of arguments"
_date=$1
IFS='-' read -a _val_arr <<< "$_date"
__year=${_val_arr[0]}
__month=${_val_arr[1]}
__day=${_val_arr[2]}
if ! isValidDate "${__day}-${__month}-${__year}" ; then
usage "Invalid date: ${_date}"
fi
# ==========
# - 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
if [[ "$(/bin/date +%Y-%m-%d)" != "$_date" ]] ; then
info "Shutdown Date \\033[1m${_date} \033[mis NOT today. So nothing to do.."
clean_up 1
fi
if $(dpkg -l 2> /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