Add script 'boot-autostart-lx-container.sh'.
This commit is contained in:
parent
15d3081866
commit
a938de97c0
485
boot-autostart-lx-container.sh
Executable file
485
boot-autostart-lx-container.sh
Executable file
@ -0,0 +1,485 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
script_name="$(basename $(realpath $0))"
|
||||
working_dir="$(dirname $(realpath $0))"
|
||||
|
||||
conf_file="${working_dir}/conf/${script_name%%.*}.conf"
|
||||
|
||||
LOCK_DIR="/tmp/$(basename $0).$$.LOCK"
|
||||
log_file="${LOCK_DIR}/${script_name%%.*}.log"
|
||||
|
||||
CUR_IFS="$IFS"
|
||||
|
||||
backup_date="$(date +%Y-%m-%d-%H%M)"
|
||||
declare -a files_backuped_arr=()
|
||||
declare -a dirs_backuped_arr=()
|
||||
|
||||
# ----------
|
||||
# Base Function(s)
|
||||
# ----------
|
||||
|
||||
usage() {
|
||||
|
||||
|
||||
[[ -n "$1" ]] && error "$1"
|
||||
|
||||
|
||||
[[ $terminal ]] && echo -e "
|
||||
\033[1mUsage:\033[m
|
||||
|
||||
$(basename $0)
|
||||
|
||||
\033[1mDescription\033[m
|
||||
|
||||
The script checks whether all LX Containers configured as autostart have been
|
||||
started. LX Containers that are not running will be started.
|
||||
|
||||
\033[1mOptions\033[m
|
||||
|
||||
No Options
|
||||
|
||||
\033[1mFiles\033[m
|
||||
|
||||
No Configuration File
|
||||
|
||||
\033[1mExample:\033[m
|
||||
|
||||
$(basename $0)
|
||||
|
||||
"
|
||||
|
||||
clean_up 1
|
||||
|
||||
}
|
||||
|
||||
clean_up() {
|
||||
|
||||
# Perform program exit housekeeping
|
||||
rm -rf "$LOCK_DIR"
|
||||
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 " $*\\c" 1>&2
|
||||
else
|
||||
echo -e -n " $*" 1>&2
|
||||
fi
|
||||
rm /tmp/shprompt$$
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
fatal(){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[31m\033[1mFatal\033[m ] $*"
|
||||
else
|
||||
echo -e " [ Fatal ] $*"
|
||||
fi
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " \033[1mScript terminated\033[m.."
|
||||
else
|
||||
echo -e " Script terminated.."
|
||||
fi
|
||||
echo ""
|
||||
rm -rf $LOCK_DIR
|
||||
exit 1
|
||||
}
|
||||
|
||||
error (){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[31m\033[1mError\033[m ] $*"
|
||||
else
|
||||
echo " [ Error ] $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
warn (){
|
||||
if $LOGGING || $terminal ; then
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[33m\033[1mWarn\033[m ] $*"
|
||||
else
|
||||
echo " [ Warn ] $*"
|
||||
fi
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
|
||||
info (){
|
||||
if $LOGGING || $terminal ; then
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[32m\033[1mInfo\033[m ] $*"
|
||||
else
|
||||
echo " [ Info ] $*"
|
||||
fi
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
|
||||
ok (){
|
||||
if $LOGGING || $terminal ; then
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[32m\033[1mOk\033[m ] $*"
|
||||
else
|
||||
echo " [ Ok ] $*"
|
||||
fi
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
|
||||
echo_done() {
|
||||
if $terminal ; then
|
||||
echo -e "\033[75G[ \033[32mdone\033[m ]"
|
||||
fi
|
||||
}
|
||||
echo_ok() {
|
||||
if $terminal ; then
|
||||
echo -e "\033[75G[ \033[32mok\033[m ]"
|
||||
fi
|
||||
}
|
||||
echo_warn() {
|
||||
if $terminal ; then
|
||||
echo -e "\033[75G[ \033[33mwarn\033[m ]"
|
||||
fi
|
||||
}
|
||||
echo_failed(){
|
||||
if $terminal ; then
|
||||
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
|
||||
fi
|
||||
}
|
||||
echo_skipped() {
|
||||
if $terminal ; then
|
||||
echo -e "\033[75G[ \033[90m\033[1mskipped\033[m ]"
|
||||
fi
|
||||
}
|
||||
echo_wait(){
|
||||
if $terminal ; then
|
||||
echo -en "\033[75G[ \033[5m\033[1m...\033[m ]"
|
||||
fi
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
containsElement () {
|
||||
local e
|
||||
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
|
||||
return 1
|
||||
}
|
||||
|
||||
# - Backup file or directory
|
||||
# -
|
||||
backup() {
|
||||
|
||||
is_directory=false
|
||||
|
||||
if [[ -z "$1" ]] ; then
|
||||
error "No file/directory for backup given!"
|
||||
return
|
||||
elif [[ -h "$1" ]] ; then
|
||||
_source="$(realpath $1)"
|
||||
warn "'$1' is a symlink to $_source."
|
||||
elif [[ ! -d "$1" ]] && [[ ! -f "$1" ]] ; then
|
||||
warn "'$1' not found. Backup skipped."
|
||||
return
|
||||
else
|
||||
_source="$(realpath $1)"
|
||||
fi
|
||||
|
||||
if [[ -d "$_source" ]]; then
|
||||
is_directory=true
|
||||
echononl "Backup directory '$_source' .."
|
||||
else
|
||||
echononl "Backup file '$_source' .."
|
||||
fi
|
||||
|
||||
cp -a "$_source" "${_source}.$backup_date" > $log_file 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_ok
|
||||
if $is_directory ; then
|
||||
dirs_backuped_arr+=("$_source")
|
||||
else
|
||||
files_backuped_arr+=("$_source")
|
||||
fi
|
||||
else
|
||||
echo_failed
|
||||
error "$(cat "$log_file")"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# - Remove script generated backups, if source wasn't changed
|
||||
# -
|
||||
rm_unchanged_backup() {
|
||||
|
||||
if [[ ${#files_backuped_arr[@]} -gt 0 ]] ; then
|
||||
for _file in "${files_backuped_arr[@]}" ; do
|
||||
if $(diff "$_file" "${_file}.$backup_date" > /dev/null 2>&1) ; then
|
||||
echononl "File '$(basename "${_file}")' wasn't changed.\n Delete the previous generated backup. .."
|
||||
rm "${_file}.$backup_date" > "$log_file" 2>&1
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo_ok
|
||||
else
|
||||
echo_failed
|
||||
error "$(cat "$log_file")"
|
||||
fi
|
||||
blank_line
|
||||
fi
|
||||
done
|
||||
fi
|
||||
if [[ ${#dirs_backuped_arr[@]} -gt 0 ]] ; then
|
||||
for _dir in "${dirs_backuped_arr[@]}" ; do
|
||||
if $(diff -Nur "$_dir" "${_dir}.$backup_date" > /dev/null 2>&1) ; then
|
||||
echononl "Directory '$(basename "${_dir}")' wasn't changed.\n Delete the previous generated backup. .."
|
||||
rm -rf "${_dir}.$backup_date" > "$log_file" 2>&1
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo_ok
|
||||
else
|
||||
echo_failed
|
||||
error "$(cat "$log_file")"
|
||||
fi
|
||||
blank_line
|
||||
fi
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
# ----------
|
||||
# - Jobhandling
|
||||
# ----------
|
||||
|
||||
# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM
|
||||
# -
|
||||
trap clean_up SIGHUP SIGINT SIGTERM
|
||||
|
||||
# - Create lock directory '$LOCK_DIR"
|
||||
#
|
||||
mkdir "$LOCK_DIR"
|
||||
|
||||
|
||||
# ----------
|
||||
# - Some checks ..
|
||||
# ----------
|
||||
|
||||
# - Running in a terminal?
|
||||
# -
|
||||
if [[ -t 1 ]] ; then
|
||||
terminal=true
|
||||
else
|
||||
terminal=false
|
||||
fi
|
||||
|
||||
# -Is systemd supported on this system?
|
||||
# -
|
||||
systemd_supported=false
|
||||
systemd=$(which systemd)
|
||||
systemctl=$(which systemctl)
|
||||
|
||||
if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then
|
||||
systemd_supported=true
|
||||
fi
|
||||
|
||||
# - Print help?
|
||||
# -
|
||||
if [[ "$(trim $*)" = "-h" ]] || [[ "$(trim $*)" = "--help" ]] ; then
|
||||
usage
|
||||
fi
|
||||
|
||||
if [[ -z "$(which basename)" ]]; then
|
||||
fatal 'It seems "basename" is not installed, but needed!'
|
||||
fi
|
||||
|
||||
if [[ -z "$(which realpath)" ]]; then
|
||||
fatal 'It seems "realpath" is not installed, but needed!'
|
||||
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"
|
||||
fi
|
||||
|
||||
|
||||
# ----------
|
||||
# Read Configurations from $conf_file
|
||||
# ----------
|
||||
|
||||
|
||||
# - Give your default values here
|
||||
# -
|
||||
DEFAULT_LOGGING=false
|
||||
DEFAUL_LXC_ROOT_DIR=/data/lxc
|
||||
|
||||
|
||||
if [[ -f "$conf_file" ]]; then
|
||||
source "$conf_file"
|
||||
else
|
||||
warn "No configuration file '$conf_file' present.\n
|
||||
Loading default values.."
|
||||
fi
|
||||
|
||||
[[ -n "$LXC_ROOT_DIR" ]] || LXC_ROOT_DIR="${DEFAUL_LXC_ROOT_DIR}"
|
||||
[[ -n "$LOGGING" ]] || LOGGING="${DEFAULT_LOGGING}"
|
||||
|
||||
|
||||
|
||||
# ----------
|
||||
# - Some pre-script tasks ..
|
||||
# ----------
|
||||
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
echo ""
|
||||
echo -e " \033[1mDoing some pre-script tasks ..\033[m"
|
||||
echo ""
|
||||
fi
|
||||
|
||||
|
||||
declare -a lx_container_all_arr=()
|
||||
declare -a lx_container_autostart_arr=()
|
||||
declare -a lx_container_not_started_arr=()
|
||||
declare -a error_msg_arr=()
|
||||
declare -a info_msg_arr=()
|
||||
|
||||
echononl "Gather all configured LX-Container .."
|
||||
for _container in $(lxc-ls) ; do
|
||||
lx_container_all_arr+=("$_container")
|
||||
done
|
||||
echo_done
|
||||
|
||||
#lx_container_all_arr+=("test5")
|
||||
#lx_container_all_arr+=("test7")
|
||||
|
||||
echononl "Gather LX-Container configured as 'autostart'.."
|
||||
_failed=false
|
||||
for _container in ${lx_container_all_arr[@]} ; do
|
||||
if [[ ! -d "${LXC_ROOT_DIR}/${_container}" ]] ; then
|
||||
_failed=true
|
||||
error_msg_arr+=("")
|
||||
IFS='§' error_msg_arr+=("No Container named \033[1m${_container}\033[m found!")
|
||||
continue
|
||||
fi
|
||||
if [[ ! -f "${LXC_ROOT_DIR}/${_container}/config" ]]; then
|
||||
_failed=true
|
||||
error_msg_arr+=("")
|
||||
IFS='§' error_msg_arr+="No Cocnfiguration for Container named \033[1m${_container}\033[m found!"
|
||||
continue
|
||||
fi
|
||||
if $(grep -E -q "^\s*lxc.start.auto\s*=\s*1" "${LXC_ROOT_DIR}/${_container}/config" 2> $log_file) ; then
|
||||
lx_container_autostart_arr+=("$_container")
|
||||
fi
|
||||
if [[ -s "$log_file" ]] ; then
|
||||
if ! containsElement "$(cat $log_file)" "${error_msg_arr[@]}" ; then
|
||||
_failed=true
|
||||
error_msg_arr+=("")
|
||||
IFS='§' error_msg_arr+=("$(cat $log_file)")
|
||||
> $log_file
|
||||
fi
|
||||
continue
|
||||
fi
|
||||
|
||||
|
||||
done
|
||||
|
||||
if $_failed ; then
|
||||
echo_failed
|
||||
for _msg in ${error_msg_arr[@]} ; do
|
||||
error $_msg
|
||||
done
|
||||
for _container in ${lx_container_autostart_arr[@]} ; do
|
||||
info "Added Contaimer \033[1m${_container}\033[m."
|
||||
done
|
||||
else
|
||||
echo_done
|
||||
fi
|
||||
|
||||
error_msg_arr=()
|
||||
_failed=false
|
||||
echononl "Gather LX-Container NOT started.."
|
||||
for _container in ${lx_container_autostart_arr[@]} ; do
|
||||
if ! $(lxc-info -n "$_container"| grep -E -q "^\s*PID:" 2> /dev/null) ; then
|
||||
lx_container_not_started_arr+=("$_container")
|
||||
fi
|
||||
if [[ -s "$log_file" ]] ; then
|
||||
_faile=true
|
||||
error_msg_arr+=("$(cat $log_file)")
|
||||
fi
|
||||
done
|
||||
|
||||
if $_failed ; then
|
||||
echo_failed
|
||||
for _msg in ${error_msg_arr[@]} ; do
|
||||
error $_msg
|
||||
done
|
||||
else
|
||||
echo_done
|
||||
fi
|
||||
|
||||
|
||||
# ----------
|
||||
# - Main part of script
|
||||
# ----------
|
||||
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
echo ""
|
||||
echo -e " \033[1mMain part of script ..\033[m"
|
||||
echo ""
|
||||
fi
|
||||
|
||||
if [[ ${#lx_container_not_started_arr[@]} -lt 1 ]]; then
|
||||
info "All autotart configured LX-Container are running"
|
||||
clean_up 0
|
||||
fi
|
||||
|
||||
_falied=false
|
||||
for _container in ${lx_container_not_started_arr[@]} ; do
|
||||
echononl "Goimg to start LX-Container \033[1m${_container}\033[m .."
|
||||
lxc-start -d -n $_container > $log_file 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_ok
|
||||
else
|
||||
echo_failed
|
||||
_failed=true
|
||||
error "$(cat $log_file)"
|
||||
fi
|
||||
done
|
||||
|
||||
if $_failed ; then
|
||||
clean_up 1
|
||||
else
|
||||
clean_up 0
|
||||
fi
|
16
conf/boot-autostart-lx-container.conf.sample
Normal file
16
conf/boot-autostart-lx-container.conf.sample
Normal file
@ -0,0 +1,16 @@
|
||||
|
||||
## ===============================================================
|
||||
## - Configuration File for "boot-autostart-lx-container.sh" Script
|
||||
## ================================================================
|
||||
|
||||
# - LOGGING
|
||||
# -
|
||||
#LOGGING=false
|
||||
|
||||
# - LXC_ROOT_DIR
|
||||
# -
|
||||
# - The directory where all the LX-Containers are stored.
|
||||
# -
|
||||
# - Defaults to: LXC_ROOT_DIR="/var/lib/lxc"
|
||||
# -
|
||||
#LXC_ROOT_DIR="/var/lib/lxc"
|
Loading…
Reference in New Issue
Block a user