Files
init.sh/init.sh

159 lines
4.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# Init.sh: initialise a computer and conform it
# Copyright (c) 2019-2021 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
# ------------------------------------------------------------------------------
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# https://opensource.org/licenses/BSD-3-Clause
# ------------------------------------------------------------------------------
# Global variables:
# * INSTALL_MODE: if dev is declared here, packages will be installed one by
# one instead of sending the whole package list to the package manage
# * LOGFILE: Define manually output log file name. Can be superseeded through
# command line parameter. ATTENTION: That variable cannot be set in
# configuration file as it is treated before loading those, so it must be
# defined before calling that script.
# ------------------------------------------------------------------------------
# trace ERR through pipes
set -o pipefail
# trace ERR through 'time command' and other functions
set -o errtrace
# set -e : exit the script if any statement returns a non-true return value
set -o errexit
# set +u : allow undeclared variables (configuration files don't need to be complete)
set +o nounset
# We want english messages
export LC_ALL=C
export LANG=C
# Version of init
export VERSION="0.99.3"
# Store script's path
export MYPATH=$(dirname $0)
# Get hostname
export HOSTNAME=$(hostname)
# Load libraries
for lib in $MYPATH/lib/*.sh; do
. $lib
done
# =============================
# ==== Basic sanity checks ====
# =============================
# Check if a function exists, return 0 if so
function_exists() {
declare -f -F $1 > /dev/null
return $?
}
function_exists prnt || (
echo "*** ERREUR FATALE !"
echo "*** Il manque des fonctions vitales venant des bibliothèques."
exit 3
)
# ======================
# ==== Main Program ====
# ======================
# Initializing global variables
export CHECK_ONLY=false
export JUMP=false
export KEEPGOING=false
export RESUME=false
export STAGE_FILE="$MYPATH/stage"
read_commandline $@
# After this we need to be root
# (--help and --version are allowed as unprivileged user)
check_root
# Logfile variable treatment -- cannot be a function
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..."
[[ ! -d $(dirname $LOGFILE) ]] && mkdir -pv $(dirname $LOGFILE)
# Log all outputs to the logfile
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec > >(tee -a $LOGFILE)
exec 2> >(tee -a $LOGFILE >&2)
prnt I "Démarrage d'init version $VERSION."
prnt I "Le fichier de log est $LOGFILE."
# -- Cannot be a function ends here
load_configuration
process_commandline_and_vars
# Reinit stage file if no resuming
if [[ $RESUME != true ]]; then
[[ -f $STAGE_FILE ]] && rm -f $STAGE_FILE
fi
# Run prechecks
[[ JUMP != true ]] && for mod in $MODULE_LIST; do
. modules/$mod.sh
version=VER_$mod
prnt I "Vérification initiale pour $mod version ${!version}..."
precheck_$mod
done
# If we only checks, we stop here
if [[ $CHECK_ONLY == true ]]; then
prnt I "Mode de vérification seulement, on s'arrête là."
exit 0
else
if [[ $JUMP == true ]]; then
prnt W "Les vérification sont évités, attention !"
else
prnt I "Toutes les vérification ont été faites."
fi
echo
if [[ $KEEPGOING == true ]]; then
echo -e "${BRed}ATTENTION : Vous avez demandé la poursuite du script en cas d'erreur.${DEFAULTCOL}"
echo -e "${BRed}ATTENTION : Cette option peut produires des résultats chaotiques.${DEFAULTCOL}"
echo -e "${BRed}ATTENTION : Cette option ne devrait être utilisé que sur système de test.${DEFAULTCOL}"
echo
fi
echo "Appuyez sur CTLR + C maintenant si vous souhaitez interrompre."
echo -e "${BYellow}Si vous continuez après cette étape le système sera modifié !${DEFAULTCOL}"
echo
read -n 1 -s -r -p "Appuyer sur n'importe quelle touche pour continuer..."
fi
# We launch modules one after one
for mod in $MODULE_LIST; do
if [[ $RESUME == true ]] && [[ $(grep $mod $STAGE_FILE) ]]; then
continue
fi
# We need this only if JUMP is set but doesn't matter if it's done again
. modules/$mod.sh
echo $mod >> $STAGE_FILE
version=VER_$mod
prnt I "Application des modifications pour $mod version ${!version}..."
$mod
done
prnt I "That's all folks !"
rm -f $STAGEFILE