Files
init.sh/init.sh
2021-07-13 12:58:47 +02:00

108 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
# Init: initialise a computer and conform it
# Copyright (c) 2021 Geoffray Levasseur <geoffray.levasseur@obs-mip.fr>
# 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.95.2"
# 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
export LOGFILE=${LOGFILE:-"$MYPATH/log/init-$(uname -n)-$(stdtime).log"}
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."
# -- Cannot be a function ends here
load_configuration
process_commandline_and_vars
# 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
fi
# We launch modules one after one
for mod in $MODULE_LIST; do
: # do everything
done
prnt I "That's all folks !"