Added manual configuration through command line, finished function headers comments

This commit is contained in:
levasseur
2021-09-06 15:21:12 +02:00
parent 66daa05175
commit 9057325412
7 changed files with 84 additions and 23 deletions

View File

@@ -31,6 +31,7 @@ export -f die
# ------------------------------------------------------------------------------
# Function trigered on Ctrl+C pressed or external kill affecting us
function terminate()
{
prnt E "$1 reçu, sortie immédiate."
@@ -89,3 +90,5 @@ check_root()
die 4 --force
fi
}
# EOF

View File

@@ -86,9 +86,9 @@ export On_IWhite='\e[0;107m'
# ------------------------------------------------------------------------------
# Affiche le status avec en-tête coloré et timestamp
# (valeur de $1 : I=info, W=warning, E=error, m=des espaces (allignement)
# pas d'entête si autre)
# Display status with color and timestamp
# ($1 accepted values: I=info, W=warning, E=error, m=des espaces (allignement)
# no header if anything else)
prnt() {
case $1 in
"I")
@@ -111,3 +111,5 @@ prnt() {
echo -e "${IWhite}$(date $DATEFORMAT)${DEFAULTFG} ${HEADS} $@"
}
export -f prnt
# EOF

View File

@@ -85,3 +85,5 @@ installfile()
fi
}
export -f installfile
# EOF

View File

@@ -60,3 +60,5 @@ svc_stop()
done
}
export -f svc_stop
# EOF

View File

@@ -7,6 +7,8 @@
# https://opensource.org/licenses/BSD-3-Clause
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Display help
disp_help()
{
@@ -16,6 +18,10 @@ Utilisation : init.sh [OPTIONS] [-m|--module <module1,...,moduleN>]
Initialise une machine pour l'intégrer à un réseau.
Options :
-f, --file <fichier> Permet de spécifier un fichier de configuration
à charger manuellement. Cette option peut être répétée
pour permettre le chargement de plusieurs fichiers de
configuration, le dernier surchargeant les précédants.
-m, --module <liste> Lance les modules indiqués même s'il ne sont pas
dans les fichiers de configuration. Les noms des
modules doivent être séparés par des virgules.
@@ -55,6 +61,8 @@ Fichiers de configuration :
EOF
}
# ------------------------------------------------------------------------------
# Show version infos
show_version()
{
@@ -71,8 +79,13 @@ show_version()
echo -e "\n${BYellow}Attention :$DEFAULTCOL Ce script requiert les droits d'administration pour fonctionner."
}
# ------------------------------------------------------------------------------
# Get module name from module file
get_mod_name()
{
echo $(basename $1 | cut -f 1 -d '.')
}
# EOF

View File

@@ -7,14 +7,20 @@
# https://opensource.org/licenses/BSD-3-Clause
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Read command line and set appropriate vars. Some basic checks can trigger
# errors with immediate exit.
read_commandline()
{
# Processing command line options
local want_module=false
local want_logfile=false
local want_conffile=false
for opt in $@; do
if [[ $want_module != true ]] && [[ $want_logfile != true ]]; then
if [[ $want_module != true ]] && [[ $want_logfile != true ]] &&
[[ $want_conffile != true ]]; then
case $opt in
"-h"|"--help")
disp_help
@@ -51,6 +57,9 @@ read_commandline()
"-l"|"--logfile")
local want_logfile=true
;;
"-f"|"--file")
local want_conffile=true
;;
*)
prnt E "Paramètre \"$opt\" non géré."
die 1 --force
@@ -59,16 +68,18 @@ read_commandline()
else
if [[ $want_module == true ]]; then
[[ $want_logfile == true ]] && synthax_error
[[ $want_conffile == true ]] && synthax_error
if [[ ! $MANUAL_MODULE_LIST ]]; then
export MANUAL_MODULE_LIST=$opt
want_module=false
else
prnt E "Une liste de module à déjà été fournie !"
prnt E "La ligne de commande ne tolère qu'un paramètre --module."
prnt E "La ligne de commande ne tolère qu'un seul paramètre --module."
die 1 --force
fi
elif [[ $want_logfile == true ]]; then
[[ $want_module == true ]] && synthax_error
[[ $want_conffile == true ]] && synthax_error
if [[ ! $NEW_LOGFILE ]]; then
export NEW_LOGFILE=$opt
want_logfile=false
@@ -76,6 +87,11 @@ read_commandline()
prnt E "Il n'est pas possible de spécifier plusieurs fichiers de log."
die 1 --force
fi
elif [[ $want_conffile == true ]]; then
[[ $want_module == true ]] && synthax_error
[[ $want_logfile == true ]] && synthax_error
export CONFFILES="$CONFFILES $opt"
want_logfile=false
fi
fi
done
@@ -88,8 +104,25 @@ read_commandline()
}
export -f read_commandline
# ------------------------------------------------------------------------------
# Load configuration with the following priorities:
# 1) Those given on command line, if any
# 2) <workingdir>/conf/<hostname>.conf (Hostname based and specific)
# 3) <workingdir>/conf/init.conf.sh (Generic default)
load_configuration()
{
if [[ -n $CONFFILES ]]; then
for f in $CONFFILES; do
prnt I "Chargement de $f spécifié manuellement."
if [[ -s $f ]]; then
. $f
else
prnt E "Le fichier $f n'existe pas ou est vide."
die 6 --force
fi
done
else
prnt I "Chargement de la configuration..."
if [[ -e $MYPATH/conf/$HOSTNAME.conf ]]; then
prnt I "Une configuration spécifique sera utilisé."
@@ -103,9 +136,12 @@ load_configuration()
die 6 --force
fi
fi
fi
}
export -f load_configuration
# ------------------------------------------------------------------------------
# Do deeper command line analysis to detect unconsistancies
process_commandline_and_vars()
{
# Check unconsistant parameters
@@ -148,3 +184,6 @@ process_commandline_and_vars()
fi
}
export -f process_commandline_and_vars
# EOF