reworked error management, added --logfile option, reworked command line analysing
This commit is contained in:
13
init.sh
13
init.sh
@@ -9,7 +9,7 @@ set -o pipefail
|
|||||||
set -o errtrace
|
set -o errtrace
|
||||||
|
|
||||||
# set -e : exit the script if any statement returns a non-true return value
|
# set -e : exit the script if any statement returns a non-true return value
|
||||||
set -o errexit
|
#set -o errexit
|
||||||
|
|
||||||
# set +u : allow undeclared variables
|
# set +u : allow undeclared variables
|
||||||
set +o nounset
|
set +o nounset
|
||||||
@@ -19,7 +19,7 @@ export LC_ALL=C
|
|||||||
export LANG=C
|
export LANG=C
|
||||||
|
|
||||||
# Version of init
|
# Version of init
|
||||||
export VERSION="0.95.2"
|
export VERSION="0.95.4"
|
||||||
|
|
||||||
# Store script's path
|
# Store script's path
|
||||||
export MYPATH=$(dirname $0)
|
export MYPATH=$(dirname $0)
|
||||||
@@ -59,7 +59,7 @@ export KEEPGOING=false
|
|||||||
export RESUME=false
|
export RESUME=false
|
||||||
export STAGE_FILE="$MYPATH/stage"
|
export STAGE_FILE="$MYPATH/stage"
|
||||||
|
|
||||||
read_commandline
|
read_commandline $@
|
||||||
|
|
||||||
# After this we need to be root
|
# After this we need to be root
|
||||||
# (--help and --version are allowed as unprivileged user)
|
# (--help and --version are allowed as unprivileged user)
|
||||||
@@ -67,7 +67,11 @@ check_root
|
|||||||
|
|
||||||
# Logfile variable treatment -- cannot be a function
|
# Logfile variable treatment -- cannot be a function
|
||||||
|
|
||||||
export LOGFILE=${LOGFILE:-"$MYPATH/log/init-$(uname -n)-$(stdtime).log"}
|
if [[ -n $NEW_LOGFILE ]]; then
|
||||||
|
export LOGFILE=$NEW_LOGFILE
|
||||||
|
else
|
||||||
|
export LOGFILE=${LOGFILE:-"$MYPATH/log/init-$(uname -n)-$(stdtime).log"}
|
||||||
|
fi
|
||||||
|
|
||||||
prnt I "Création du répertoire d'accueil du fichier log..."
|
prnt I "Création du répertoire d'accueil du fichier log..."
|
||||||
[[ ! -d $(dirname $LOGFILE) ]] && mkdir -pv $(dirname $LOGFILE)
|
[[ ! -d $(dirname $LOGFILE) ]] && mkdir -pv $(dirname $LOGFILE)
|
||||||
@@ -78,6 +82,7 @@ trap 'exec 2>&4 1>&3' 0 1 2 3
|
|||||||
exec > >(tee -a $LOGFILE)
|
exec > >(tee -a $LOGFILE)
|
||||||
exec 2> >(tee -a $LOGFILE >&2)
|
exec 2> >(tee -a $LOGFILE >&2)
|
||||||
prnt I "Démarrage d'init version $VERSION."
|
prnt I "Démarrage d'init version $VERSION."
|
||||||
|
prnt I "Le fichier de log est $LOGFILE."
|
||||||
|
|
||||||
# -- Cannot be a function ends here
|
# -- Cannot be a function ends here
|
||||||
|
|
||||||
|
|||||||
@@ -30,55 +30,37 @@ trap "terminate 'SIGTERM'" SIGTERM
|
|||||||
|
|
||||||
function error()
|
function error()
|
||||||
{
|
{
|
||||||
# Check the call is actually the result of an error
|
local parent_lineno="$1"
|
||||||
local error_code="$?"
|
local message="$2"
|
||||||
[[ $error_code == 0 ]] && return;
|
local code="${3:-1}"
|
||||||
|
|
||||||
# Local variables
|
|
||||||
local i=0
|
|
||||||
local regex=''
|
|
||||||
local mem=''
|
|
||||||
|
|
||||||
local error_file=''
|
|
||||||
local error_lineno=''
|
|
||||||
local lineno=$1 && shift
|
|
||||||
|
|
||||||
# Print error header
|
# Print error header
|
||||||
prnt E "${BIWhite}*** ${BIRed}Une erreur fatale est intervenue, \
|
prnt E "${BIWhite}*** ${BIRed}Une erreur fatale est intervenue, le script va s'arrêter immédiatement !$DEFAULTCOL"
|
||||||
le script va s'arrêter immédiatement !$DEFAULTCOL"
|
|
||||||
|
if [[ -n "$message" ]]; then
|
||||||
# Print backtrace
|
prnt E "Erreur ligne $parent_lineno, code d'erreur $code avec le message :"
|
||||||
backtrace 2
|
echo -e "\t\t$message"
|
||||||
|
else
|
||||||
# Exit the script
|
prnt E "Erreur ligne $(caller), avec le code d'erreur $code."
|
||||||
die $error_code
|
fi
|
||||||
|
#awk 'NR>L-4 && NR<L+4 { printf "%-5d%3s%s\n",NR,(NR==L?">>>":""),$0 }' L=$1 $0
|
||||||
}
|
}
|
||||||
|
|
||||||
# Trigger error function on error
|
# Trigger error function on error
|
||||||
trap "error ${LINENO}" ERR
|
trap "error ${LINENO}; backtrace; exit 255" ERR
|
||||||
|
|
||||||
# Print a backtrace
|
# Print a backtrace
|
||||||
function backtrace
|
function backtrace
|
||||||
{
|
{
|
||||||
local _start_from_=0
|
echo "======== Pile d'appel ========"
|
||||||
|
typeset -i i=0
|
||||||
local params=( "$@" )
|
for func in "${FUNCNAME[@]}"
|
||||||
if (( "${#params[@]}" >= "1" )); then
|
do
|
||||||
_start_from_="$1"
|
printf '%15s() %s:%d\n' \
|
||||||
fi
|
"$func" "${BASH_SOURCE[$i]}" "${BASH_LINENO[$i]}"
|
||||||
|
let i++ || true
|
||||||
local i=0
|
|
||||||
local first=false
|
|
||||||
local last=false
|
|
||||||
while caller $i > /dev/null; do
|
|
||||||
if [[ -n "$_start_from_" ]] && (( "$i" + 1 >= "$_start_from_" )); then
|
|
||||||
if [[ "$first" == false ]]; then
|
|
||||||
echo "==== Pile d'appel ===="
|
|
||||||
first=true
|
|
||||||
fi
|
|
||||||
caller $i
|
|
||||||
fi
|
|
||||||
(( i=i++ ))
|
|
||||||
done
|
done
|
||||||
|
echo "=============================="
|
||||||
}
|
}
|
||||||
|
|
||||||
check_root()
|
check_root()
|
||||||
|
|||||||
@@ -4,35 +4,42 @@ disp_help()
|
|||||||
{
|
{
|
||||||
cat << EOF
|
cat << EOF
|
||||||
Utilisation : init.sh [OPTIONS] [-m|--module <module1,...,moduleN>]
|
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 :
|
||||||
-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.
|
dans les fichiers de configuration. Les noms des
|
||||||
-c, --check-only Lance les procédure de vérification préexecution
|
modules doivent être séparés par des virgules.
|
||||||
sans rien modifier.
|
-c, --check-only Lance les procédures de vérification sans rien
|
||||||
-j, --jump Saute les procédures de vérification.
|
modifier.
|
||||||
-k, --keep-going L'execution continura en cas d'erreur.
|
-j, --jump Saute les procédures de vérification des
|
||||||
-r, --resume Reprend l'execution la ou elle c'est arrêté.
|
modules.
|
||||||
|
-k, --keep-going Continue l'execution en cas d'erreur.
|
||||||
|
-r, --resume Reprend l'execution là ou elle s'est arrêté.
|
||||||
-R, --no-root-check Ne pas vérifier les droits root (ou UID 0)
|
-R, --no-root-check Ne pas vérifier les droits root (ou UID 0)
|
||||||
-h, --help Affiche ce texte d'aide.
|
-h, --help Affiche ce texte d'aide.
|
||||||
-v, --version Show version of that script and version of all
|
-l, --logfile <nom> Nom du fichier de log. Peut aussi être changé
|
||||||
available modules.
|
via la variable d'environnement LOGFILE.
|
||||||
|
-v, --version Affiche la version de ce script et celles de
|
||||||
|
tous les modules disponibles.
|
||||||
|
|
||||||
Attention : les options courtes ne sont pas concaténable.
|
Attention : les options courtes ne sont pas concaténable.
|
||||||
|
|
||||||
Variable d'environnement :
|
Variable d'environnement :
|
||||||
LOGFILE Stocke le nom complet du fichier de log qui sera
|
LOGFILE Stocke le nom complet du fichier de log qui sera
|
||||||
utilisé.
|
produit.
|
||||||
|
|
||||||
Fichiers de configuration :
|
Fichiers de configuration :
|
||||||
Le fichier de configuration principal fournira les détails nécessaire
|
Le fichier de configuration principal fournira les détails nécessaires
|
||||||
au déploiement de la nouvelle machine. Il doit se situer dans le
|
au déploiement de la nouvelle machine. Il doit se situer dans le
|
||||||
répertoire conf/ à coté du script init.sh. Si un fichier appelé
|
répertoire conf/ à coté du script init.sh. Si un fichier appelé
|
||||||
"hostname.sh" (ou hostname est le nom d'hote) existe il sera le fichier
|
"hostname.sh" existe (ou hostname est le nom d'hote), alors il sera le
|
||||||
de configuration principal. Dans le cas contraire, le nom générique
|
fichier de configuration principal. Dans le cas contraire, le nom
|
||||||
"init.conf.sh" sera recherché. En l'absence de fichier de configuration
|
générique "init.conf.sh" sera recherché.
|
||||||
ce script ne pourra pas fonctionner.
|
|
||||||
|
En l'absence de fichier de configuration ce script ne pourra pas
|
||||||
|
fonctionner.
|
||||||
|
|
||||||
Veuiller consulter les fichiers de configuration fournis en exemple
|
Veuiller consulter les fichiers de configuration fournis en exemple
|
||||||
pour avoir un aperçu des paramêtres disponibles.
|
pour avoir un aperçu des paramêtres disponibles.
|
||||||
|
|||||||
@@ -2,30 +2,32 @@
|
|||||||
read_commandline()
|
read_commandline()
|
||||||
{
|
{
|
||||||
# Processing command line options
|
# Processing command line options
|
||||||
want_module=false
|
local want_module=false
|
||||||
|
local want_logfile=false
|
||||||
|
|
||||||
for opt in $@; do
|
for opt in $@; do
|
||||||
if [[ $want_module == false ]]; then
|
if [[ $want_module != true ]] && [[ $want_logfile != true ]]; then
|
||||||
case $opt in
|
case $opt in
|
||||||
"-h"|"--help")
|
"-h"|"--help")
|
||||||
disp_help
|
disp_help
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
"-v"|"--version")
|
"-v"|"--version")
|
||||||
show_version
|
show_version
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
"-m"|"--module")
|
"-m"|"--module")
|
||||||
want_module=true
|
local want_module=true
|
||||||
;;
|
;;
|
||||||
"-c"|"--check-only")
|
"-c"|"--check-only")
|
||||||
export CHECK_ONLY=true
|
export CHECK_ONLY=true
|
||||||
;;
|
;;
|
||||||
"-j"|"--jump")
|
"-j"|"--jump")
|
||||||
export JUMP=true
|
export JUMP=true
|
||||||
;;
|
;;
|
||||||
"-k"|"--keep-going")
|
"-k"|"--keep-going")
|
||||||
export KEEPGOING=true
|
export KEEPGOING=true
|
||||||
;;
|
;;
|
||||||
"-r"|"--resume")
|
"-r"|"--resume")
|
||||||
if [[ -s $STAGE_FILE ]]; then
|
if [[ -s $STAGE_FILE ]]; then
|
||||||
export RESUME=true
|
export RESUME=true
|
||||||
@@ -34,28 +36,49 @@ read_commandline()
|
|||||||
prnt E "Sans ce fichier, la reprise n'est pas possible."
|
prnt E "Sans ce fichier, la reprise n'est pas possible."
|
||||||
die 1 --force
|
die 1 --force
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
"-R"|"--no-root-check")
|
"-R"|"--no-root-check")
|
||||||
export NO_ROOT_CHECK=true
|
export NO_ROOT_CHECK=true
|
||||||
;;
|
;;
|
||||||
|
"-l"|"--logfile")
|
||||||
|
local want_logfile=true
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
prnt E "Paramètre \'$opt\' non géré."
|
prnt E "Paramètre \"$opt\" non géré."
|
||||||
disp_help
|
die 1 --force
|
||||||
exit 1
|
;;
|
||||||
;;
|
|
||||||
esac
|
esac
|
||||||
else
|
else
|
||||||
if [[ ! $MANUAL_MODULE_LIST ]]; then
|
if [[ $want_module == true ]]; then
|
||||||
MANUAL_MODULE_LIST=$opt
|
[[ $want_logfile == true ]] && synthax_error
|
||||||
want_module=false
|
if [[ ! $MANUAL_MODULE_LIST ]]; then
|
||||||
else
|
export MANUAL_MODULE_LIST=$opt
|
||||||
prnt E "Une liste de module à déjà été fournie !"
|
want_module=false
|
||||||
prnt E "La ligne de commande ne tolère qu'un paramètre --module."
|
else
|
||||||
die 1 --force
|
prnt E "Une liste de module à déjà été fournie !"
|
||||||
|
prnt E "La ligne de commande ne tolère qu'un paramètre --module."
|
||||||
|
die 1 --force
|
||||||
|
fi
|
||||||
|
elif [[ $want_logfile == true ]]; then
|
||||||
|
[[ $want_module == true ]] && synthax_error
|
||||||
|
if [[ ! $NEW_LOGFILE ]]; then
|
||||||
|
export NEW_LOGFILE=$opt
|
||||||
|
want_logfile=false
|
||||||
|
else
|
||||||
|
prnt E "Il n'est pas possible de spécifier plusieurs fichiers de log."
|
||||||
|
die 1 --force
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# If those var are true at that point, something is wrong
|
||||||
|
if [[ $want_logfile == true ]] || [[ $want_module == true ]]; then
|
||||||
|
prnt E "Erreur de syntaxe dans la ligne de commande."
|
||||||
|
die 1
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
export -f read_commandline
|
||||||
|
|
||||||
load_configuration()
|
load_configuration()
|
||||||
{
|
{
|
||||||
@@ -73,14 +96,10 @@ load_configuration()
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
export -f load_configuration
|
||||||
|
|
||||||
process_commandline_and_vars()
|
process_commandline_and_vars()
|
||||||
{
|
{
|
||||||
if [[ $want_module == true ]]; then
|
|
||||||
prnt E "La liste des modules à exécuter est manquante !"
|
|
||||||
die 1 --force
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check unconsistant parameters
|
# Check unconsistant parameters
|
||||||
if [[ $CHECK_ONLY == true ]]; then
|
if [[ $CHECK_ONLY == true ]]; then
|
||||||
[[ $JUMP == true ]] && (
|
[[ $JUMP == true ]] && (
|
||||||
@@ -120,3 +139,4 @@ process_commandline_and_vars()
|
|||||||
die 5
|
die 5
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
export -f process_commandline_and_vars
|
||||||
|
|||||||
Reference in New Issue
Block a user