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() function terminate()
{ {
prnt E "$1 reçu, sortie immédiate." prnt E "$1 reçu, sortie immédiate."
@@ -89,3 +90,5 @@ check_root()
die 4 --force die 4 --force
fi fi
} }
# EOF

View File

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

View File

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

View File

@@ -71,4 +71,4 @@ pkgautorem()
} }
export -f pkgautorem export -f pkgautorem
#EOF # EOF

View File

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

View File

@@ -7,6 +7,8 @@
# https://opensource.org/licenses/BSD-3-Clause # https://opensource.org/licenses/BSD-3-Clause
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Display help # Display help
disp_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. Initialise une machine pour l'intégrer à un réseau.
Options : 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 -m, --module <liste> Lance les modules indiqués même s'il ne sont pas
dans les fichiers de configuration. Les noms des dans les fichiers de configuration. Les noms des
modules doivent être séparés par des virgules. modules doivent être séparés par des virgules.
@@ -55,6 +61,8 @@ Fichiers de configuration :
EOF EOF
} }
# ------------------------------------------------------------------------------
# Show version infos # Show version infos
show_version() show_version()
{ {
@@ -71,8 +79,13 @@ show_version()
echo -e "\n${BYellow}Attention :$DEFAULTCOL Ce script requiert les droits d'administration pour fonctionner." echo -e "\n${BYellow}Attention :$DEFAULTCOL Ce script requiert les droits d'administration pour fonctionner."
} }
# ------------------------------------------------------------------------------
# Get module name from module file # Get module name from module file
get_mod_name() get_mod_name()
{ {
echo $(basename $1 | cut -f 1 -d '.') echo $(basename $1 | cut -f 1 -d '.')
} }
# EOF

View File

@@ -7,14 +7,20 @@
# https://opensource.org/licenses/BSD-3-Clause # 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() read_commandline()
{ {
# Processing command line options # Processing command line options
local want_module=false local want_module=false
local want_logfile=false local want_logfile=false
local want_conffile=false
for opt in $@; do 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 case $opt in
"-h"|"--help") "-h"|"--help")
disp_help disp_help
@@ -51,6 +57,9 @@ read_commandline()
"-l"|"--logfile") "-l"|"--logfile")
local want_logfile=true local want_logfile=true
;; ;;
"-f"|"--file")
local want_conffile=true
;;
*) *)
prnt E "Paramètre \"$opt\" non géré." prnt E "Paramètre \"$opt\" non géré."
die 1 --force die 1 --force
@@ -59,16 +68,18 @@ read_commandline()
else else
if [[ $want_module == true ]]; then if [[ $want_module == true ]]; then
[[ $want_logfile == true ]] && synthax_error [[ $want_logfile == true ]] && synthax_error
[[ $want_conffile == true ]] && synthax_error
if [[ ! $MANUAL_MODULE_LIST ]]; then if [[ ! $MANUAL_MODULE_LIST ]]; then
export MANUAL_MODULE_LIST=$opt export MANUAL_MODULE_LIST=$opt
want_module=false want_module=false
else else
prnt E "Une liste de module à déjà été fournie !" 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 die 1 --force
fi fi
elif [[ $want_logfile == true ]]; then elif [[ $want_logfile == true ]]; then
[[ $want_module == true ]] && synthax_error [[ $want_module == true ]] && synthax_error
[[ $want_conffile == true ]] && synthax_error
if [[ ! $NEW_LOGFILE ]]; then if [[ ! $NEW_LOGFILE ]]; then
export NEW_LOGFILE=$opt export NEW_LOGFILE=$opt
want_logfile=false want_logfile=false
@@ -76,6 +87,11 @@ read_commandline()
prnt E "Il n'est pas possible de spécifier plusieurs fichiers de log." prnt E "Il n'est pas possible de spécifier plusieurs fichiers de log."
die 1 --force die 1 --force
fi 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
fi fi
done done
@@ -88,8 +104,25 @@ read_commandline()
} }
export -f 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() 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..." prnt I "Chargement de la configuration..."
if [[ -e $MYPATH/conf/$HOSTNAME.conf ]]; then if [[ -e $MYPATH/conf/$HOSTNAME.conf ]]; then
prnt I "Une configuration spécifique sera utilisé." prnt I "Une configuration spécifique sera utilisé."
@@ -103,9 +136,12 @@ load_configuration()
die 6 --force die 6 --force
fi fi
fi fi
fi
} }
export -f load_configuration export -f load_configuration
# ------------------------------------------------------------------------------
# Do deeper command line analysis to detect unconsistancies
process_commandline_and_vars() process_commandline_and_vars()
{ {
# Check unconsistant parameters # Check unconsistant parameters
@@ -148,3 +184,6 @@ process_commandline_and_vars()
fi fi
} }
export -f process_commandline_and_vars export -f process_commandline_and_vars
# EOF