Files
init.sh/init.sh

237 lines
7.0 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
# exeption to that will need a special function call
set -o errexit
# set +u: allow undeclared variables because configuration files don't need
# to be complete (even if it's bad practice)
set +o nounset
# We want english messages, all the time (can be redefined in configuration)
export LC_ALL=C
export LANG=C
# Version of init
export VERSION="0.99.11"
# Store script's path (realpath -s resolve symlinks if init.sh is a symlink)
export MYPATH=$(dirname $(realpath -s $0))
# Get hostname
export HOSTNAME=$(hostname)
# Load libraries
for lib in $MYPATH/lib/*.sh; do
. $lib
done
unset lib
# =============================
# ==== Basic sanity checks ====
# =============================
function_exists prnt || (
echo "*** ERREUR FATALE !"
echo "*** Il manque des fonctions vitales venant des bibliothèques."
exit 3
)
# ======================
# ==== Main Program ====
# ======================
# Set system dependent vars
set_sys_vars $(uname -m) $(get_os_version)
# 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..."
if [[ ! -d $(dirname $LOGFILE) ]]; then
mkdir -pv $(dirname $LOGFILE)
fi
# 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."
if [[ -n $SYS_CODE ]]; then
prnt I "Lancé sur $SYS_DIST version $SYS_VER ($SYS_CODE) architecture $SYS_ARCH"
else
prnt I "Lancé sur $SYS_DIST version $SYS_VER architecture $SYS_ARCH"
fi
# -- Cannot be a function ends here
# ------------------------------------------------------------------------------
if [[ -n $CHROOT_PATH && -z $CHROOT_DONE ]]; then
chroot_bootstrap $@
prnt I "Fin normale d'execution chrootée !"
exit 0
fi
load_autoconf
load_configuration
# Load pre and post actions for package manager
for prepost in $MYPATH/prepost/*.sh; do
. $prepost
done
unset $prepost
process_commandline_and_vars
set_system_proxy
# Reinit stage file if no resuming
if [[ $RESUME != true ]]; then
[[ -f $STAGE_FILE ]] && rm -f $STAGE_FILE
fi
# Loading modules
for mod in $MODULE_LIST; do
. modules/$mod.sh
done
unset mod
if [[ $RUN_SHELL == true ]]; then
prnt I "Lancement d'un shell intéractif..."
bash --rcfile $MYPATH/bash.rc -i
prnt I "Sortie du script après exécution du shell."
exit 0
fi
# Run prechecks
if [[ JUMP != true ]]; then
tmpfile=$(mktemp /tmp/init-XXXXXX)
if [[ -n $MANUAL_MODULE_LIST ]]; then
prnt W "La vérification des dépendences est désactivé avec une liste manuelle de module."
fi
if [[ $NO_DEPS == true ]]; then
prnt W "La vérification des dépendences à été désactivée manuellement."
fi
if [[ $RESUME == true ]]; then
cat $STAGE_FILE >> $tmpfile
fi
for mod in $MODULE_LIST; do
version=VER_$mod
if [[ $RESUME == true ]] && [[ $(grep $mod $STAGE_FILE) ]]; then
prnt I "Vérification précédemment exécuté pour $mod version ${!version}."
continue
fi
prnt I "Vérification initiale pour $mod version ${!version}..."
if [[ -z $MANUAL_MODULE_LIST && $NO_DEPS != true ]]; then
deps=DEP_$mod
for dep in ${!deps}; do
if [[ ! $(grep $dep $tmpfile) ]]; then
prnt E "Le module $mod a des dépendances non satisfaite ou est exécuté trop tôt."
prnt E " * $dep doit être exécuté avant $mod, vérifiez votre liste de modules."
die 9
fi
done
unset deps
fi
precheck_$mod
echo $mod >> $tmpfile
done
rm -f $tmpfile
unset mod
fi
# 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
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 -e "${BYellow}Si vous continuez après cette étape le système sera modifié !${DEFAULTCOL}"
echo
dump_key_buffer
read -n 1 -rsp $"Appuyer sur la touche <C> pour continuer ou une autre pour s'arrêter..." key
echo
if [[ $key == "C" || $key == 'c' ]]; then
# 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
version=VER_$mod
prnt I "Application des modifications pour $mod version ${!version}..."
$mod
echo $mod >> $STAGE_FILE # Mark as done for resuming function
done
unset mod
echo
else
echo -e "${Yellow}Le système n'a subit aucune modifications.${DEFAULTCOL}"
echo
fi
prnt I "That's all folks !"
rm -f $STAGEFILE
# EOF