From e1321111d93a3a31daba0a4e5243f5490e52874d Mon Sep 17 00:00:00 2001 From: levasseur Date: Wed, 23 Jun 2021 16:59:41 +0200 Subject: [PATCH] command line parsing implemented, implemented --force option to die(), few typos --- conf/includes/pkgsel.conf.sh | 2 +- init.sh | 157 +++++++++++++++++++++++++++-------- lib/aaa_errors.sh | 12 ++- lib/display.sh | 125 +++++++++++++++------------- lib/filefct.sh | 2 +- 5 files changed, 199 insertions(+), 99 deletions(-) diff --git a/conf/includes/pkgsel.conf.sh b/conf/includes/pkgsel.conf.sh index 6ec0cb6..79554cc 100644 --- a/conf/includes/pkgsel.conf.sh +++ b/conf/includes/pkgsel.conf.sh @@ -81,4 +81,4 @@ export PKGS_TOOLS="asciidoc docbook-utils doxygen help2man python-sphinx \ # Agrégation de tout par défaut export PKGSEL="$PKGS_BASE $PKGS_BUR $PKGS_FONTS $PKGS_GRAF $PKGS_MULTI \ - $PKGS_SCI $PKGS_PRGM $PKGS_TEX $PKGS_TOOLS" + $PKGS_SCI $PKGS_PRGM $PKGS_TEX $PKGS_TOOLS" diff --git a/init.sh b/init.sh index 793d2d7..adda863 100755 --- a/init.sh +++ b/init.sh @@ -14,13 +14,20 @@ set -o errexit export VERSION="0.95.1" # Stocke le chemin du script -MYPATH=$(dirname $0) +export MYPATH=$(dirname $0) + +# récupère le nom d'hote +export HOSTNAME=$(hostname) # Charge les bibliothèques for lib in $MYPATH/lib/*.sh; do . $lib done +# ============================= +# ==== Basic sanity checks ==== +# ============================= + # Check if a function exists function_exists() { declare -f -F $1 > /dev/null @@ -28,18 +35,22 @@ function_exists() { } function_exists prnt || ( - echo "Il manque des fonctions vitales venant des bibliothèques !" + echo "*** ERREUR FATALE !" + echo "*** Il manque des fonctions vitales venant des bibliothèques." exit 2 ) # Vérifie qu'on soit root if [[ $EUID -ne 0 ]]; prnt E "Ce script doit être démarré en tant que root. Arrêt." - die 1 + die 1 --force fi -# Variables globales importantes -export HOSTNAME=$(hostname) +# =========================== +# ==== Log file creation ==== +# =========================== + + # Variables globales importantes export LOGFILE=${LOGFILE:-"$MYPATH/log/init-$(uname -n)-$(stdtime).log"} prnt I "Création du répertoire d'accueil du fichier log..." @@ -61,19 +72,23 @@ else . $MYPATH/conf/init.conf.sh else prnt E "Aucune configuration trouvée, impossible de continuer." - die 2 + die 2 --force fi fi +# ================================ +# ==== Main program functions ==== +# ================================ + # Affiche l'aide disp_help() { cat << EOF -Utilisation : init.sh [OPTION] [-m module1,...,moduleN] +Utilisation : init.sh [OPTIONS] [-m|--module ] Initialise une machine pour l'intégrer à un réseau. -Option : - -m, --module= Lance les modules indiqués même s'il ne sont pas +Options : + -m, --module Lance les modules indiqués même s'il ne sont pas dans les fichiers de configuration. -c, --check-only Lance les procédure de vérification préexecution sans rien modifier. @@ -81,6 +96,10 @@ Option : -k, --keep-going L'execution continura en cas d'erreur. -r, --resume Reprend l'execution la ou elle c'est arrêté. -h, --help Affiche ce texte d'aide. + -v, --version Show version of that script and version of all + available modules. + +Attention : les options courtes ne sont pas concaténable. Variable d'environnement : LOGFILE Stocke le nom complet du fichier de log qui sera @@ -97,44 +116,112 @@ Fichiers de configuration : Veuiller consulter les fichiers de configuration fournis en exemple pour avoir un aperçu des paramêtres disponibles. + EOF } +# Show version infos +show_version() +{ + echo -e "${White}init.sh${DEFAULTCOL} version ${Green}$VERSION${DEFAULTCOL}" + echo "" + echo "Available modules:" + for mod in $MYPATH/modules/*.sh; do + . $mod + local modname=$(get_mod_name $mod) + local version=VER_$modname + echo -e "\t$modname:\t ${!version}" + done +} + +# Get module name from module file +get_mod_name() +{ + echo $(basename $1 | cut -f 1 -d '.') +} + + # ====================== # ==== Main Program ==== # ====================== # Processing command line options +want_module=false for opt in $@; do - case $opt in - "-h"|"--help") - disp_help - exit 0 - ;; - *) - prnt E "Paramètre \'$opt\' non géré." - disp_help - exit 1 - ;; - esac + if [[ want_module == false ]]; then + case $opt in + "-h"|"--help") + disp_help + exit 0 + ;; + "-v"|"--version") + show_version + exit 0 + "-m"|"--module") + want_module=true + ;; + "-c"|"--check-only") + export CHECKONLY=true + ;; + "-j"|"--jump") + export JUMP=true + ;; + "-k"|"--keep-going") + export KEEPGOING=true + ;; + "-r"|"--resume") + if [[ -s $MYPATH/stage ]]; then + export RESUME=true + else + prnt E "Le fichier d'état n'existe pas ou est vide !" + prnt E "Sans ce fichier, la reprise n'est pas possible." + die 1 --force + fi + ;; + *) + prnt E "Paramètre \'$opt\' non géré." + disp_help + exit 1 + ;; + esac + else + if [[ ! MANUAL_MODULE_LIST ]]; then + 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." + die 1 --force + fi + fi done +if [[ $want_module == true ]]; then + prnt E "La liste des modules à exécuter est manquante !" + die 1 --force +fi + +# Check unconsistant parameters +[[ $CHECK_ONLY == true ]] && [[ $JUMP == true ]] && ( + prnt E "Les options --check-only et --jump s'excluent mutuellement !" + die 1 --force +) +[[ $KEEPGOING == true ]] && [[ $CHECKONLY == true ]] && ( + prnt E "Les options --keep-going et --check-only sont incompatible !" + die 1 --force +) +if [[ $RESUME == true ]]; then + [[ $CHECK_ONLY == true ]] && ( + prnt E "La reprise n'a pas de sens avec --check-only." + die 1 --force + ) + [[ $MANUAL_MODULE_LIST ]] && ( + prnt E "Le mode reprise ne fonctionne pas avec une liste de \ + modules passé manuellement." + die 1 --force + ) +fi - -upgrade_dist -[[ ! $PVEHST ]] && install_ceph && conf_ceph -[[ ! $PVEHST ]] && apt_blacklist -#[[ ! $PVEHST ]] && authnz -#[[ ! $PVEHST ]] && add_users -conf_locale -install_pkg -conf_ntp -conf_ssh -install_profile -patch_snmp -install_mk-agent -conf_syslog -conf_mail prnt I "That's all folks !" echo "Après vérification des logs, vous devriez redémarrer la machine..." diff --git a/lib/aaa_errors.sh b/lib/aaa_errors.sh index 6cc7a7f..1a4da4a 100644 --- a/lib/aaa_errors.sh +++ b/lib/aaa_errors.sh @@ -4,15 +4,21 @@ function die() { local errorcode=$1 - prnt E "Sortie prématuré avec erreur (code #$errorcode)." - exit $errorcode + [[ $2 == "--force" ]] && unset KEEPGOING + + if [[ $KEEPGOING ]]; then + prnt E "Sortie prématuré avec erreur (code #$errorcode)." + exit $errorcode + else + prnt W "On continue malgrés l'erreur #$errorcode." + fi } export -f die function terminate() { prnt E "$1 reçu, sortie immédiate." - die 128 + die 128 --force } trap terminate 'Ctrl + C' SIGINT trap terminate 'SIGTERM' SIGTERM diff --git a/lib/display.sh b/lib/display.sh index eff63a2..1727c88 100644 --- a/lib/display.sh +++ b/lib/display.sh @@ -1,72 +1,79 @@ # Some display functions and defines color codes # Standard 16 colors display declaration -DEFAULTFG="\e[0;39m" -DEFAULTBG="\e[0;49m" -DEFAULTCOL=${DEFAULTBG}${DEFAULTFG} +export DEFAULTFG="\e[0;39m" +export DEFAULTBG="\e[0;49m" +export DEFAULTCOL=${DEFAULTBG}${DEFAULTFG} + # Regular Colors -Black='\e[0;30m' -Red='\e[0;31m' -Green='\e[0;32m' -Yellow='\e[0;33m' -Blue='\e[0;34m' -Purple='\e[0;35m' -Cyan='\e[0;36m' -White='\e[0;37m' +export Black='\e[0;30m' +export Red='\e[0;31m' +export Green='\e[0;32m' +export Yellow='\e[0;33m' +export Blue='\e[0;34m' +export Purple='\e[0;35m' +export Cyan='\e[0;36m' +export White='\e[0;37m' + # Bold -BBlack='\e[1;30m' -BRed='\e[1;31m' -BGreen='\e[1;32m' -BYellow='\e[1;33m' -BBlue='\e[1;34m' -BPurple='\e[1;35m' -BCyan='\e[1;36m' -BWhite='\e[1;37m' +export BBlack='\e[1;30m' +export BRed='\e[1;31m' +export BGreen='\e[1;32m' +export BYellow='\e[1;33m' +export BBlue='\e[1;34m' +export BPurple='\e[1;35m' +export BCyan='\e[1;36m' +export BWhite='\e[1;37m'* + # Underline -UBlack='\e[4;30m' -URed='\e[4;31m' -UGreen='\e[4;32m' -UYellow='\e[4;33m' -UBlue='\e[4;34m' -UPurple='\e[4;35m' -UCyan='\e[4;36m' -UWhite='\e[4;37m' +export UBlack='\e[4;30m' +export URed='\e[4;31m' +export UGreen='\e[4;32m' +export UYellow='\e[4;33m' +export UBlue='\e[4;34m' +export UPurple='\e[4;35m' +export UCyan='\e[4;36m' +export UWhite='\e[4;37m' + # Background -On_Black='\e[40m' -On_Red='\e[41m' -On_Green='\e[42m' -On_Yellow='\e[43m' -On_Blue='\e[44m' -On_Purple='\e[45m' -On_Cyan='\e[46m' -On_White='\e[47m' +export On_Black='\e[40m' +export On_Red='\e[41m' +export On_Green='\e[42m' +export On_Yellow='\e[43m' +export On_Blue='\e[44m' +export On_Purple='\e[45m' +export On_Cyan='\e[46m' +export On_White='\e[47m' + # High Intensity -IBlack='\e[0;90m' -IRed='\e[0;91m' -IGreen='\e[0;92m' -IYellow='\e[0;93m' -IBlue='\e[0;94m' -IPurple='\e[0;95m' -ICyan='\e[0;96m' -IWhite='\e[0;97m' +export IBlack='\e[0;90m' +export IRed='\e[0;91m' +export IGreen='\e[0;92m' +export IYellow='\e[0;93m' +export IBlue='\e[0;94m' +export IPurple='\e[0;95m' +export ICyan='\e[0;96m' +export IWhite='\e[0;97m' + # Bold High Intensity -BIBlack='\e[1;90m' -BIRed='\e[1;91m' -BIGreen='\e[1;92m' -BIYellow='\e[1;93m' -BIBlue='\e[1;94m' -BIPurple='\e[1;95m' -BICyan='\e[1;96m' -BIWhite='\e[1;97m' +export BIBlack='\e[1;90m' +export BIRed='\e[1;91m' +export BIGreen='\e[1;92m' +export BIYellow='\e[1;93m' +export BIBlue='\e[1;94m' +export BIPurple='\e[1;95m' +export BICyan='\e[1;96m' +export BIWhite='\e[1;97m' + # High Intensity backgrounds -On_IBlack='\e[0;100m' -On_IRed='\e[0;101m' -On_IGreen='\e[0;102m' -On_IYellow='\e[0;103m' -On_IBlue='\e[0;104m' -On_IPurple='\e[0;105m' -On_ICyan='\e[0;106m' -On_IWhite='\e[0;107m' +export On_IBlack='\e[0;100m' +export On_IRed='\e[0;101m' +export On_IGreen='\e[0;102m' +export On_IYellow='\e[0;103m' +export On_IBlue='\e[0;104m' +export On_IPurple='\e[0;105m' +export On_ICyan='\e[0;106m' +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, pas d'entête si différent) diff --git a/lib/filefct.sh b/lib/filefct.sh index 4fb159f..d38fe9f 100644 --- a/lib/filefct.sh +++ b/lib/filefct.sh @@ -16,7 +16,7 @@ backupdist() for file in $@; do if [[ -e ${file} ]]; then - tmpstmp=$(stdtime) + local tmpstmp=$(stdtime) prnt I "Création d'une sauvegarde de ${file} du $tmpstmp..." cp -av $file $file.dist.$tmpstmp fi