implemented realm detection and improved configuration files loading

This commit is contained in:
2023-10-23 00:00:27 +02:00
parent 7464ad6555
commit 3fb06c257c
4 changed files with 49 additions and 5 deletions

View File

@@ -326,6 +326,8 @@ The following table is giving a list of error codes with explanation:
| 16 | Invalid options provided with cron mode activated | | 16 | Invalid options provided with cron mode activated |
| 17 | Missing or invalid status file, can't resume | | 17 | Missing or invalid status file, can't resume |
| 18 | Module file don't exists or is empty | | 18 | Module file don't exists or is empty |
| 20 | Ambigous realm with autodetection |
| 21 | Unconsistant directory structure with configured realm |
| 50..100 | Error in module execution | | 50..100 | Error in module execution |
| 126 | Command exists but is not executable | | 126 | Command exists but is not executable |
| 127 | Command not found | | 127 | Command not found |

View File

@@ -36,7 +36,7 @@ export LC_ALL=C
export LANG=C export LANG=C
# Version of init # Version of init
export VERSION="0.99.20" export VERSION="0.99.21"
# Store script's path (realpath -s resolve symlinks if init.sh is a symlink) # Store script's path (realpath -s resolve symlinks if init.sh is a symlink)
export MYPATH=$(dirname "$(realpath -s "$0")") export MYPATH=$(dirname "$(realpath -s "$0")")
@@ -44,6 +44,10 @@ export MYPATH=$(dirname "$(realpath -s "$0")")
# Get hostname # Get hostname
export HOSTNAME=$(hostname) export HOSTNAME=$(hostname)
# Get realm or domain name
export REALM=${REALM:-$(hostname -d)}
echo $REALM
# Load libraries # Load libraries
for lib in $MYPATH/lib/*.sh; do for lib in $MYPATH/lib/*.sh; do
. "$lib" . "$lib"

View File

@@ -182,7 +182,7 @@ process_commandline_and_vars()
die 18 die 18
fi fi
done done
else elif [[ $RUN_SHELL != "true" ]]; then
prnt E "No module to execute!" prnt E "No module to execute!"
die 5 die 5
fi fi

View File

@@ -59,6 +59,17 @@ export -f load_autoconf
# 3) <workingdir>/conf/init.conf.sh (Generic default, for testing) # 3) <workingdir>/conf/init.conf.sh (Generic default, for testing)
load_configuration() load_configuration()
{ {
# --------------------------------------------------------------------------
# Get list of possible files to load when REALM is not declared
get_files()
{
for d in $MYPATH/conf/*; do
if [[ -d $d ]]; then
find $d -maxdepth 1 -name "$HOSTNAME.conf.sh"
fi
done
}
if [[ -n $CONFFILES ]]; then if [[ -n $CONFFILES ]]; then
local f= local f=
for f in $CONFFILES; do for f in $CONFFILES; do
@@ -73,21 +84,48 @@ load_configuration()
unset f unset f
else else
prnt I "Loading configuration..." prnt I "Loading configuration..."
if [[ -z $REALM ]]; then
prnt W "REALM is undeclared, trying to scan configuration subdirectories for this host..."
local found_realms=$(get_files)
case "$(echo $found_realms | wc -w)" in
"0")
: # We do nothing as we'll check for other scenario
;;
"1")
export REALM="$(basename $(dirname $found_realms))"
local auto_realm="$REALM"
;;
*)
prnt E "More than one file correspond to that host. This is ambigous and need to be fixed."
prnt m "You can fix that situation with one of those actions:"
prnt m "\t * Declare a REALM variable with the actual domain name of the host."
prnt m "\t * Give manually the configuration file using the --file option."
prnt m "\t * Configure the domain name of the host."
die 20 --force
;;
esac
fi
if [[ -e $MYPATH/conf/$REALM/$HOSTNAME.conf.sh ]]; then if [[ -e $MYPATH/conf/$REALM/$HOSTNAME.conf.sh ]]; then
prnt I "A specific configuration will be used." prnt I "A specific configuration will be used."
. $MYPATH/conf/$HOSTNAME.conf.sh local cnffile=$MYPATH/conf/$REALM/$HOSTNAME.conf.sh
if [[ -n $auto_realm && $REALM != $auto_realm ]]; then
prnt E "The domain name in the confinguration file don't correspond to the detected domain through directory structure."
die 21 --force
fi
elif [[ -e $MYPATH/conf/$HOSTNAME.conf.sh ]]; then elif [[ -e $MYPATH/conf/$HOSTNAME.conf.sh ]]; then
prnt I "A specific configuration will be used." prnt I "A specific configuration will be used."
. $MYPATH/conf/$HOSTNAME.conf.sh local cnffile=$MYPATH/conf/$HOSTNAME.conf.sh
else else
if [[ -e $MYPATH/conf/init.conf.sh ]]; then if [[ -e $MYPATH/conf/init.conf.sh ]]; then
prnt I "A generic configuration will be used." prnt I "A generic configuration will be used."
. $MYPATH/conf/init.conf.sh local cnffile=$MYPATH/conf/init.conf.sh
else else
prnt E "No configuration found, impossible to continue." prnt E "No configuration found, impossible to continue."
die 6 --force die 6 --force
fi fi
fi fi
prnt I "Loading $cnffile ..."
. $cnffile
fi fi
} }
export -f load_configuration export -f load_configuration