199 lines
5.6 KiB
Bash
Executable File
199 lines
5.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
|
|
# 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
|
|
export LC_ALL=C
|
|
export LANG=C
|
|
|
|
# Version of init
|
|
export VERSION="0.99.8"
|
|
|
|
# Store script's path
|
|
export MYPATH=$(dirname $0)
|
|
|
|
# Get hostname
|
|
export HOSTNAME=$(hostname)
|
|
|
|
# System architecture
|
|
export SYS_ARCH=$(uname -m)
|
|
|
|
# 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
|
|
)
|
|
|
|
# Set system dependent vars
|
|
get_os_version $(read_os_release)
|
|
|
|
# ======================
|
|
# ==== 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..."
|
|
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."
|
|
prnt I "Lancé sur $SYS_DIST version $SYS_VER ($SYS_CODE) architecture $SYS_ARCH"
|
|
|
|
# -- Cannot be a function ends here
|
|
|
|
load_autoconf
|
|
|
|
load_configuration
|
|
|
|
process_commandline_and_vars
|
|
|
|
# Declare proxy system vars if needed and if not already declared
|
|
if [[ -n $PROXYSRV && -n $PROXYSRVPORT && -z $NO_PROXY ]]; then
|
|
export http_proxy=${http_proxy:-"http://$PROXYSRV:$PROXYSRVPORT/"}
|
|
export https_proxy=${https_proxy:-"http://$PROXYSRV:$PROXYSRVPORT/"}
|
|
fi
|
|
|
|
# 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
|
|
|
|
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
|
|
[[ JUMP != true ]] && for mod in $MODULE_LIST; do
|
|
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
|
|
|
|
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
|
|
|
|
while read -r -t 0.001; do :; done # Dump the 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
|
|
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
|