Files
init.sh/lib/loaders.sh

151 lines
5.0 KiB
Bash

#!/bin/bash
# ------------------------------------------------------------------------------
# Loaders for conf and prepost functions
# This file is part of the init.sh project
# Copyright (c) 2019-2024 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
# ------------------------------------------------------------------------------
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# https://opensource.org/licenses/BSD-3-Clause
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Automatically load system specific configuration if file exist in the
# following order:
# 1) auto/arch.conf.sh
# 2) auto/distro.conf.sh
# 3) auto/distro-arch.conf.sh
# 4) auto/distro-version.conf.sh
# 5) auto/distro-codename.conf.sh (if sys_code defined)
# 6) auto/distro-version-arch.conf.sh
# 7) auto/distro-codename-arch.conf.sh (if sys_code defined)
load_autoconf()
{
local prefix="$MYPATH/conf/auto"
if [[ -e $prefix/$SYS_ARCH.conf.sh ]]; then
. $prefix/$SYS_ARCH.conf.sh
fi
if [[ -e $prefix/$SYS_DIST.conf.sh ]]; then
. $prefix/$SYS_DIST.conf.sh
fi
if [[ -e $prefix/$SYS_DIST-$SYS_ARCH.conf.sh ]]; then
. $prefix/$SYS_DIST-$SYS_ARCH.conf.sh
fi
if [[ -e $prefix/$SYS_DIST-$SYS_VER.conf.sh ]]; then
. $prefix/$SYS_DIST-$SYS_VER.conf.sh
fi
if [[ -n $SYS_CODE && -e $prefix/$SYS_DIST-$SYS_CODE.conf.sh ]]; then
. $prefix/$SYS_DIST-$SYS_CODE.conf.sh
fi
if [[ -e $prefix/$SYS_DIST-$SYS_VER-$SYS_ARCH.conf.sh ]]; then
. $prefix/$SYS_DIST-$SYS_VER-$SYS_ARCH.conf.sh
fi
if [[ -n $SYS_CODE && -e $prefix/$SYS_DIST-$SYS_CODE-$SYS_ARCH.conf.sh ]]; then
. $prefix/$SYS_DIST-$SYS_CODE-$SYS_ARCH.conf.sh
fi
unset prefix
}
export -f load_autoconf
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Load configuration with the following priorities:
# 1) Those given on command line, if any
# 2) <workingdir>/conf/<realm>/<hostname>.conf (Hostname based and specific)
# 2) <workingdir>/conf/<hostname>.conf (Hostname based and specific)
# 3) <workingdir>/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
prnt I "Loading $f manually specified."
if [[ -s $f ]]; then
. $f
else
prnt E "The $f file doesn't exists or is empty."
die 6 --force
fi
done
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."
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."
local cnffile=$MYPATH/conf/$HOSTNAME.conf.sh
else
if [[ -e $MYPATH/conf/init.conf.sh ]]; then
prnt I "A generic configuration will be used."
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
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Load pre and post actions for package manager
load_prepost_actions()
{
local prepost=
for prepost in $MYPATH/prepost.d/*.sh; do
prnt I "Loading prepost actions in $prepost ..."
. $prepost
done
unset prepost
}
export -f load_prepost_actions
# ------------------------------------------------------------------------------
# EOF