From dc53b749f168c40959f9c147b5ddee9ab77b2421 Mon Sep 17 00:00:00 2001 From: levasseur Date: Wed, 29 Sep 2021 17:29:39 +0200 Subject: [PATCH] introduced distro version detection and automatic system dependent configuration loading --- README.md | 1 + conf/{includes => auto}/debian.conf.sh | 0 conf/auto/devuan.conf.sh | 23 ++++++++ conf/auto/ubuntu.conf.sh | 23 ++++++++ conf/init.conf.sh | 3 - conf/lell213893.conf.sh | 3 - init.sh | 13 ++++- lib/version.sh | 56 +++++++++++++++++++ lib/zzz_main_fct.sh | 39 +++++++++++++ modules/upgrade_dist.sh | 4 +- .../{debian_buster.list => debian_10.list} | 0 .../{debian_bulleyes.list => debian_11.list} | 0 .../{ubuntu_focal.list => ubuntu_20.04.list} | 0 13 files changed, 155 insertions(+), 10 deletions(-) rename conf/{includes => auto}/debian.conf.sh (100%) create mode 100644 conf/auto/devuan.conf.sh create mode 100644 conf/auto/ubuntu.conf.sh create mode 100644 lib/version.sh rename repo/common/{debian_buster.list => debian_10.list} (100%) rename repo/common/{debian_bulleyes.list => debian_11.list} (100%) rename repo/common/{ubuntu_focal.list => ubuntu_20.04.list} (100%) diff --git a/README.md b/README.md index 38d1c8c..5e45504 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,7 @@ The following table is giving a list of error code with explanation: | 5 | Malformed module list | | 6 | Unable to find configuration | | 7 | Misuse of script internal function | +| 8 | Can't determine OS version | | 11 | Bad function call | | 12 | Error copying files | | 13 | Bad target file system | diff --git a/conf/includes/debian.conf.sh b/conf/auto/debian.conf.sh similarity index 100% rename from conf/includes/debian.conf.sh rename to conf/auto/debian.conf.sh diff --git a/conf/auto/devuan.conf.sh b/conf/auto/devuan.conf.sh new file mode 100644 index 0000000..524b2fa --- /dev/null +++ b/conf/auto/devuan.conf.sh @@ -0,0 +1,23 @@ +# Configuration propre à Debian + +# Conf gestionnaire de paquet +export PKG_MAN="apt-get" +export COM_INSTALL="install -y" +export COM_UPDATE="update" +export COM_UPGRADE="full-upgrade -y" +export COM_REMOVE="remove --purge -y" +export COM_AUTOREM="autoremove --purge -y" + +# Special variable for apt tools to disable any interactive behaviour +export DEBIAN_FRONTEND=noninteractive + +# Conf chemin +export RC_SCRIPTS_PATH="/etc/init.d" + +# Conf init +# Init SystemV ou OpenRC: +export INIT_COM="$RC_SCRIPTS_PATH/%srv% %comm%" +# Init Systemd: +#export INIT_COM="systemctl %comm% %srv%" +# Init Upstart (plus ou moins universel) +#export INIT_COM="service %srv% %com%" diff --git a/conf/auto/ubuntu.conf.sh b/conf/auto/ubuntu.conf.sh new file mode 100644 index 0000000..e142590 --- /dev/null +++ b/conf/auto/ubuntu.conf.sh @@ -0,0 +1,23 @@ +# Configuration propre à Debian + +# Conf gestionnaire de paquet +export PKG_MAN="apt-get" +export COM_INSTALL="install -y" +export COM_UPDATE="update" +export COM_UPGRADE="full-upgrade -y" +export COM_REMOVE="remove --purge -y" +export COM_AUTOREM="autoremove --purge -y" + +# Special variable for apt tools to disable any interactive behaviour +export DEBIAN_FRONTEND=noninteractive + +# Conf chemin +export RC_SCRIPTS_PATH="/etc/init.d" + +# Conf init +# Init SystemV ou OpenRC: +#export INIT_COM="$RC_SCRIPTS_PATH/%srv% %comm%" +# Init Systemd: +#export INIT_COM="systemctl %comm% %srv%" +# Init Upstart (plus ou moins universel) +export INIT_COM="service %srv% %com%" diff --git a/conf/init.conf.sh b/conf/init.conf.sh index 887c6b0..d1da515 100644 --- a/conf/init.conf.sh +++ b/conf/init.conf.sh @@ -7,9 +7,6 @@ # Importe les paramètres spécifiques LEGOS . $MYPATH/conf/includes/legos.conf.sh -# Importe les paramètres pour Debian et Ubuntu -. $MYPATH/conf/includes/debian.conf.sh - # Importe la sélection de paquets par défaut . $MYPATH/conf/includes/pkgsel.full.conf.sh diff --git a/conf/lell213893.conf.sh b/conf/lell213893.conf.sh index 741fd89..e10df97 100644 --- a/conf/lell213893.conf.sh +++ b/conf/lell213893.conf.sh @@ -7,9 +7,6 @@ # Importe les paramètres spécifiques LEGOS . $MYPATH/conf/includes/legos-bas.conf.sh -# Importe les paramètres pour Debian et Ubuntu -. $MYPATH/conf/includes/debian.conf.sh - # Importe la sélection de paquets par défaut . $MYPATH/conf/includes/pkgsel.full.conf.sh diff --git a/init.sh b/init.sh index e6ea213..3fa34e0 100755 --- a/init.sh +++ b/init.sh @@ -44,6 +44,9 @@ 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 @@ -65,6 +68,9 @@ function_exists prnt || ( exit 3 ) +# Set system dependent vars +get_os_version $(read_os_release) + # ====================== # ==== Main Program ==== # ====================== @@ -102,9 +108,12 @@ 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_auto_conf + load_configuration process_commandline_and_vars @@ -127,7 +136,7 @@ done if [[ $RUN_SHELL == true ]]; then prnt I "Lancement d'un shell intéractif..." - bash -i --rcfile $MYPATH/bash.rc + bash --rcfile $MYPATH/bash.rc -i prnt I "Sortie du script après exécution du shell." exit 0 fi @@ -185,3 +194,5 @@ fi prnt I "That's all folks !" rm -f $STAGEFILE + +# EOF diff --git a/lib/version.sh b/lib/version.sh new file mode 100644 index 0000000..afd5c0d --- /dev/null +++ b/lib/version.sh @@ -0,0 +1,56 @@ +# ------------------------------------------------------------------------------ +# Version determination function +# This file is part of the init.sh project +# Copyright (c) 2019-2021 Geoffray Levasseur +# ------------------------------------------------------------------------------ +# This file is distributed under 3-clause BSD license. +# The complete license agreement can be obtained at: +# https://opensource.org/licenses/BSD-3-Clause +# ------------------------------------------------------------------------------ + +# ------------------------------------------------------------------------------ +# Return on stdout $distro $version $codename +read_os_release() +{ + if [[ ! -f /etc/os-release ]]; then + prnt E "Your distribution doesn't have the needed os-release file." + die 8 --force + fi + + # Create a sub-shell to avoid polluting the environnement + ( + # Iniitalise version codename in case the var don't exists + VERSION_CODENAME="NULL" + + # Import the file in the environment + source /etc/os-release + + if [[ -z $ID || -z $VERSION_ID ]]; then + prnt E "Your /etc/os-release file mises some vital information." + die --force 8 + fi + + # Return values on standard stdout + echo ${ID,,} ${VERSION_ID} ${VERSION_CODENAME,,} + #prnt I "OS is: ${ID,,} ${VERSION_ID} ${VERSION_CODENAME,,}" + ) +} +export read_os_release + + +# ------------------------------------------------------------------------------ +# Create system related variables +# ------------------------------------------------------------------------------ +get_os_version() +{ + if [[ $# -ne 3 ]]; then + prnt E "get_os_version(): incorect number of parameters ($@)." + die 7 --force + fi + + export SYS_DIST=$1 + export SYS_VER=$2 + if [[ $3 != "null" ]]; then + export SYS_CODE=$3 + fi +} diff --git a/lib/zzz_main_fct.sh b/lib/zzz_main_fct.sh index 1936d11..7cb41de 100644 --- a/lib/zzz_main_fct.sh +++ b/lib/zzz_main_fct.sh @@ -115,6 +115,45 @@ read_commandline() export -f read_commandline +# ------------------------------------------------------------------------------ +# Automatically load system specific configuration if file exist in the +# following order: +# 1) auto/arch.conf.sh +# 2) auto/distro.conf.sh +# 3) auto/distro-arch.conf.sh +# 4) auto/distro-version.conf.sh +# 5) auto/distro-codename.conf.sh (if sys_code defined) +# 6) auto/distro-version-arch.conf.sh +# 7) auto/distro-codename-arch.conf.sh (if sys_code defined) +load_autoconf() +{ + local prefix="$MYPATH/conf/auto" + + if [[ -e $prefix/$SYS_ARCH.conf.sh ]]; then + . $prefix/$SYS_ARCH.conf.sh + fi + if [[ -e $prefix/$SYS_DIST.conf.sh ]]; then + . $prefix/$SYS_DIST.conf.sh + fi + if [[ -e $prefix/$SYS_DIST-$SYS_ARCH.conf.sh ]]; then + . $prefix/$SYS_DIST-$SYS_ARCH.conf.sh + fi + if [[ -e $prefix/$SYS_DIST-$SYS_VER.conf.sh ]]; then + . $prefix/$SYS_DIST-$SYS_VER.conf.sh + fi + if [[ -n $SYS_CODE && -e $prefix/$SYS_DIST-$SYS_CODE.conf.sh ]]; then + . $prefix/$SYS_DIST-$SYS_CODE.conf.sh + fi + if [[ -e $prefix/$SYS_DIST-$SYS_VER-$SYS_ARCH.conf.sh ]]; then + . $prefix/$SYS_DIST-$SYS_VER-$SYS_ARCH.conf.sh + fi + if [[ -n $SYS_CODE && -e $prefix/$SYS_DIST-$SYS_CODE-$SYS_ARCH.conf.sh ]]; then + . $prefix/$SYS_DIST-$SYS_CODE-$SYS_ARCH.conf.sh + fi +} +export -f load_autoconf + + # ------------------------------------------------------------------------------ # Load configuration with the following priorities: # 1) Those given on command line, if any diff --git a/modules/upgrade_dist.sh b/modules/upgrade_dist.sh index d4147bb..95977b9 100644 --- a/modules/upgrade_dist.sh +++ b/modules/upgrade_dist.sh @@ -40,9 +40,7 @@ upgrade_dist() fi # Remplace source.list from dist with ours (be smarter) - if [[ -n $APT_LIST_FILE ]]; then - installfile $APT_LIST_FILE /etc/apt/sources.list - fi + installfile "${SYS_DIST}_${SYS_VER}" /etc/apt/sources.list prnt I "Mise à jour de la liste des paquets..." pkgupdt diff --git a/repo/common/debian_buster.list b/repo/common/debian_10.list similarity index 100% rename from repo/common/debian_buster.list rename to repo/common/debian_10.list diff --git a/repo/common/debian_bulleyes.list b/repo/common/debian_11.list similarity index 100% rename from repo/common/debian_bulleyes.list rename to repo/common/debian_11.list diff --git a/repo/common/ubuntu_focal.list b/repo/common/ubuntu_20.04.list similarity index 100% rename from repo/common/ubuntu_focal.list rename to repo/common/ubuntu_20.04.list