From 905732541294b6bfba12cf92762f47b3f3250341 Mon Sep 17 00:00:00 2001 From: levasseur Date: Mon, 6 Sep 2021 15:21:12 +0200 Subject: [PATCH] Added manual configuration through command line, finished function headers comments --- lib/aaa_errors.sh | 9 ++++--- lib/display.sh | 8 +++--- lib/filefct.sh | 2 ++ lib/pkgman.sh | 2 +- lib/services.sh | 2 ++ lib/support.sh | 23 +++++++++++++---- lib/zzz_main_fct.sh | 61 +++++++++++++++++++++++++++++++++++++-------- 7 files changed, 84 insertions(+), 23 deletions(-) diff --git a/lib/aaa_errors.sh b/lib/aaa_errors.sh index 3c0362e..fa1fa9e 100644 --- a/lib/aaa_errors.sh +++ b/lib/aaa_errors.sh @@ -18,10 +18,10 @@ function die() trap - ERR if [[ "$KEEPGOING" != "true" ]] || [[ "$2" == "--force" ]]; then - prnt E "Sortie prématuré avec erreur (code #$errorcode)." - exit $errorcode + prnt E "Sortie prématuré avec erreur (code #$errorcode)." + exit $errorcode else - prnt W "On continue malgrés l'erreur #$errorcode." + prnt W "On continue malgrés l'erreur #$errorcode." fi # Put the trigger back (only executed with --keepgoing) @@ -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 diff --git a/lib/display.sh b/lib/display.sh index 4b283f1..d205f77 100644 --- a/lib/display.sh +++ b/lib/display.sh @@ -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 diff --git a/lib/filefct.sh b/lib/filefct.sh index 502ddf6..754eee2 100644 --- a/lib/filefct.sh +++ b/lib/filefct.sh @@ -85,3 +85,5 @@ installfile() fi } export -f installfile + +# EOF diff --git a/lib/pkgman.sh b/lib/pkgman.sh index 8c53494..356b63c 100644 --- a/lib/pkgman.sh +++ b/lib/pkgman.sh @@ -71,4 +71,4 @@ pkgautorem() } export -f pkgautorem -#EOF +# EOF diff --git a/lib/services.sh b/lib/services.sh index 0c82604..513f800 100644 --- a/lib/services.sh +++ b/lib/services.sh @@ -60,3 +60,5 @@ svc_stop() done } export -f svc_stop + +# EOF diff --git a/lib/support.sh b/lib/support.sh index b55a5c4..09beacc 100644 --- a/lib/support.sh +++ b/lib/support.sh @@ -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 ] Initialise une machine pour l'intégrer à un réseau. Options : + -f, --file 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 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() { @@ -62,17 +70,22 @@ show_version() echo "" echo "Modules disponibles :" for mod in $MYPATH/modules/*.sh; do - . $mod - local modname=$(get_mod_name $mod) - local version=VER_$modname - echo -e "\t$BWhite$modname${DEFAULTCOL}: $BGreen${!version}$DEFAULTCOL" + . $mod + local modname=$(get_mod_name $mod) + local version=VER_$modname + echo -e "\t$BWhite$modname${DEFAULTCOL}: $BGreen${!version}$DEFAULTCOL" done [[ $EUID -ne 0 ]] && - 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_mod_name() { echo $(basename $1 | cut -f 1 -d '.') } + +# EOF + diff --git a/lib/zzz_main_fct.sh b/lib/zzz_main_fct.sh index 38085a2..801a324 100644 --- a/lib/zzz_main_fct.sh +++ b/lib/zzz_main_fct.sh @@ -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,24 +104,44 @@ read_commandline() } export -f read_commandline + +# ------------------------------------------------------------------------------ +# Load configuration with the following priorities: +# 1) Those given on command line, if any +# 2) /conf/.conf (Hostname based and specific) +# 3) /conf/init.conf.sh (Generic default) load_configuration() { - prnt I "Chargement de la configuration..." - if [[ -e $MYPATH/conf/$HOSTNAME.conf ]]; then - prnt I "Une configuration spécifique sera utilisé." - . $MYPATH/conf/$HOSTNAME.conf + 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 - if [[ -e $MYPATH/conf/init.conf.sh ]]; then - prnt I "Une configuration générique sera utilisé." - . $MYPATH/conf/init.conf.sh + prnt I "Chargement de la configuration..." + if [[ -e $MYPATH/conf/$HOSTNAME.conf ]]; then + prnt I "Une configuration spécifique sera utilisé." + . $MYPATH/conf/$HOSTNAME.conf else - prnt E "Aucune configuration trouvée, impossible de continuer." - die 6 --force + if [[ -e $MYPATH/conf/init.conf.sh ]]; then + prnt I "Une configuration générique sera utilisé." + . $MYPATH/conf/init.conf.sh + else + prnt E "Aucune configuration trouvée, impossible de continuer." + 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 +