diff --git a/README.md b/README.md index e0cb61e..2538cb1 100644 --- a/README.md +++ b/README.md @@ -326,6 +326,8 @@ The following table is giving a list of error codes with explanation: | 16 | Invalid options provided with cron mode activated | | 17 | Missing or invalid status file, can't resume | | 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 | | 126 | Command exists but is not executable | | 127 | Command not found | diff --git a/init.sh b/init.sh index c7e61ca..8db16be 100755 --- a/init.sh +++ b/init.sh @@ -36,7 +36,7 @@ export LC_ALL=C export LANG=C # 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) export MYPATH=$(dirname "$(realpath -s "$0")") @@ -44,6 +44,10 @@ export MYPATH=$(dirname "$(realpath -s "$0")") # Get hostname export HOSTNAME=$(hostname) +# Get realm or domain name +export REALM=${REALM:-$(hostname -d)} +echo $REALM + # Load libraries for lib in $MYPATH/lib/*.sh; do . "$lib" diff --git a/lib/command_line.sh b/lib/command_line.sh index 942e827..117b486 100644 --- a/lib/command_line.sh +++ b/lib/command_line.sh @@ -182,7 +182,7 @@ process_commandline_and_vars() die 18 fi done - else + elif [[ $RUN_SHELL != "true" ]]; then prnt E "No module to execute!" die 5 fi diff --git a/lib/loaders.sh b/lib/loaders.sh index 0cdfee2..24d79d2 100644 --- a/lib/loaders.sh +++ b/lib/loaders.sh @@ -59,6 +59,17 @@ export -f load_autoconf # 3) /conf/init.conf.sh (Generic default, for testing) 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 local f= for f in $CONFFILES; do @@ -73,21 +84,48 @@ load_configuration() unset f else 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 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 prnt I "A specific configuration will be used." - . $MYPATH/conf/$HOSTNAME.conf.sh + local cnffile=$MYPATH/conf/$HOSTNAME.conf.sh else if [[ -e $MYPATH/conf/init.conf.sh ]]; then prnt I "A generic configuration will be used." - . $MYPATH/conf/init.conf.sh + local cnffile=$MYPATH/conf/init.conf.sh else prnt E "No configuration found, impossible to continue." die 6 --force fi fi + prnt I "Loading $cnffile ..." + . $cnffile fi } export -f load_configuration