133 lines
3.2 KiB
Bash
Executable File
133 lines
3.2 KiB
Bash
Executable File
#!/bin/bash
|
|
# Init: initialise a computer and conform it
|
|
# Copyright (c) 2019-2021 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
|
|
|
# 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
|
|
set +o nounset
|
|
|
|
# We want english messages
|
|
export LC_ALL=C
|
|
export LANG=C
|
|
|
|
# Version of init
|
|
export VERSION="0.99.1"
|
|
|
|
# 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
|
|
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
|
|
[[ $JUMP == true ]] && print I "Toutes les vérification ont été faites."
|
|
echo
|
|
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
|
|
version=VER_$mod
|
|
prnt I "Application des modifications pour $mod version ${!version}..."
|
|
echo $mod >> $STAGE_FILE
|
|
$mod
|
|
done
|
|
|
|
prnt I "That's all folks !"
|