From 6fa6dcc24de066860d0a1d4be1fec3d47a364ee9 Mon Sep 17 00:00:00 2001 From: levasseur Date: Mon, 25 Oct 2021 17:33:23 +0200 Subject: [PATCH] implemented patchfile and pre post action of package manager, renamed pkgrem -> pkgrm --- conf/auto/debian.conf.sh | 8 +- conf/auto/devuan.conf.sh | 6 ++ conf/auto/ubuntu.conf.sh | 6 ++ lib/filefct.sh | 32 +++++++- lib/pkgman.sh | 156 ++++++++++++++++++++++++++++++++++++++- modules/install_pkg.sh | 8 +- modules/upgrade_dist.sh | 4 +- 7 files changed, 208 insertions(+), 12 deletions(-) diff --git a/conf/auto/debian.conf.sh b/conf/auto/debian.conf.sh index bbf4680..bf53ab2 100644 --- a/conf/auto/debian.conf.sh +++ b/conf/auto/debian.conf.sh @@ -15,7 +15,13 @@ 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 GET_INSTALLLIST="apt-get -s install @pkg@ -V | grep 'Inst' | awk '{print$2}'" +export GET_UPGRADELIST="apt-get -s full-upgrade -V | grep 'Inst' | awk '{print$2}'" +export GET_REMOVELIST="apt-get -s remove @pkg@ -V | grep 'Remv' | awk '{print$2}'" +export GET_AUTOREMLIST="apt-get -s autoremove -V | grep 'Remv' | awk '{print$2}'" + +# Special variable for apt like tools to disable any interactive behaviour +# This is not used by init.sh export DEBIAN_FRONTEND=noninteractive # Conf chemin diff --git a/conf/auto/devuan.conf.sh b/conf/auto/devuan.conf.sh index ab226d7..9069ccd 100644 --- a/conf/auto/devuan.conf.sh +++ b/conf/auto/devuan.conf.sh @@ -15,7 +15,13 @@ export COM_UPGRADE="full-upgrade -y" export COM_REMOVE="remove --purge -y" export COM_AUTOREM="autoremove --purge -y" +export GET_INSTALLLIST="apt-get -s install @pkg@ -V | grep 'Inst' | awk '{print$2}'" +export GET_UPGRADELIST="apt-get -s full-upgrade -V | grep 'Inst' | awk '{print$2}'" +export GET_REMOVELIST="apt-get -s remove @pkg@ -V | grep 'Remv' | awk '{print$2}'" +export GET_AUTOREMLIST="apt-get -s autoremove -V | grep 'Remv' | awk '{print$2}'" + # Special variable for apt tools to disable any interactive behaviour +# This is not used by init.sh export DEBIAN_FRONTEND=noninteractive # Conf chemin diff --git a/conf/auto/ubuntu.conf.sh b/conf/auto/ubuntu.conf.sh index cb31e2f..1b56648 100644 --- a/conf/auto/ubuntu.conf.sh +++ b/conf/auto/ubuntu.conf.sh @@ -15,7 +15,13 @@ export COM_UPGRADE="full-upgrade -y" export COM_REMOVE="remove --purge -y" export COM_AUTOREM="autoremove --purge -y" +export GET_INSTALLLIST="apt-get -s install @pkg@ -V | grep 'Inst' | awk '{print$2}'" +export GET_UPGRADELIST="apt-get -s full-upgrade -V | grep 'Inst' | awk '{print$2}'" +export GET_REMOVELIST="apt-get -s remove @pkg@ -V | grep 'Remv' | awk '{print$2}'" +export GET_AUTOREMLIST="apt-get -s autoremove -V | grep 'Remv' | awk '{print$2}'" + # Special variable for apt tools to disable any interactive behaviour +# This is not used by init.sh export DEBIAN_FRONTEND=noninteractive # Conf chemin diff --git a/lib/filefct.sh b/lib/filefct.sh index 8c0e905..e883892 100644 --- a/lib/filefct.sh +++ b/lib/filefct.sh @@ -177,9 +177,39 @@ isdirempty() # ------------------------------------------------------------------------------ # patch a file replacing all @var@ by the corresponding value in environment +# or the variable list given in parameter patchfile() { - : # todo + local dstfile=$1 && shift + local workfile=${dstfile}.work + + # Create a sub-process, to avoid bash environment pollution + ( + local varlist= pattern= + if [[ $# -eq 0 ]] ; then + pattern="-e s/<\(.*\)>/\$\1\$\1/g" + else + local var= + for var in $* ; do + if ! declare -p $var >/dev/null 2>&1 ; then + local $var=$(eval echo \$$var) + fi + export $var + pattern="$pattern -e s/@$var@/\$$var/g" + varlist=$varlist\$$var + done + fi + + # sed replace with \$$VAR and envsubst do the replace by value + sed $pattern $dstfile | envsubst ${varlist:+"$varlist"} > "$workfile" + ) + + local -a rights=( $(stat --printf="%a %u %g" "$dstfile") ) + mv "$workfile" "$dstfile" + chmod ${rights[0]} "$dstfile" + chown ${rights[1]}:${rights[2]} "$dstfile" + + unset dstfile } # EOF diff --git a/lib/pkgman.sh b/lib/pkgman.sh index b62584e..21b721e 100644 --- a/lib/pkgman.sh +++ b/lib/pkgman.sh @@ -29,11 +29,15 @@ pkginst() exit 11 fi if [[ ! $INSTALL_MODE == dev ]]; then + exec_preinst $@ $PKG_MAN $COM_INSTALL $@ + exec_postinst $@ else local pkg= for pkg in $@; do + exec_preinst $pkg $PKG_MAN $COM_INSTALL $pkg + exec_postinst $pkg done unset pkg fi @@ -46,14 +50,16 @@ export -f pkginst pkgupgd() { prnt I "Application de la mise à jours du système..." + exec_preupgd $PKG_MAN $COM_UPGRADE + exec_postupgd } export -f pkgupgd # ------------------------------------------------------------------------------ # Uninstallation -pkgrem() +pkgrm() { prnt I "Désinstallation de paquets..." if [[ $# -lt 1 ]]; then @@ -61,25 +67,167 @@ pkgrem() exit 11 fi if [[ ! $INSTALL_MODE == dev ]]; then + exec_prerm $@ $PKG_MAN $COM_REMOVE $@ + exec_postrm else local pkg= for pkg in $@; do + exec_prerm $pkg $PKG_MAN $COM_REMOVE $pkg + exec_postrm done uset pkg fi } -export -f pkgrem +export -f pkgrm # ------------------------------------------------------------------------------ # Cleanup -pkgautorem() +pkgautorm() { prnt I "Désinstallation de paquets superflus..." + exec_preautorm $PKG_MAN $COM_AUTOREM + exec_postautorm } -export -f pkgautorem +export -f pkgautorm + + +# ------------------------------------------------------------------------------ +# Execute preinstallation code +exec_preinst() +{ + local cmd=$(echo $GET_INSTALLLIST | sed "s/@pkg@/$@/") + local pkglist=$($cmd) + unset $cmd + for pkg in $pkglist; do + if [[ $(function_exists preinst_$pkg) ]]; then + prnt I "Exécution de la préinstallation de $pkg ..." + preinst_$pkg + fi + done + export POSTINSTLIST=$pkglist + unset pkglist +} +export -f exec_preinst + + +# ------------------------------------------------------------------------------ +# Execute postinstallation code +exec_postinst() +{ + if [[ -z $POSTINSTLIST ]]; then + return 0 + fi + for pkg in $POSTINSTLIST; do + if [[ $(function_exists postinst_$pkg) ]]; then + prnt I "Exécution de la postinstallation de $pkg ..." + postinst_$pkg + fi + done + unset POSTINSTLIST +} +export -f exec_postinst + + +# ------------------------------------------------------------------------------ +# Execute preremove code +exec_prerm() +{ + local cmd=$(echo $GET_REMOVELIST | sed "s/@pkg@/$@/") + local pkglist=$($cmd) + unset $cmd + for pkg in $pkglist; do + if [[ $(function_exists prerm_$pkg) ]]; then + prnt I "Exécution du préretrait de $pkg ..." + prerm_$pkg + fi + done + export POSTRMLIST=$pkglist + unset pkglist +} +export -f exec_prerm + + +# ------------------------------------------------------------------------------ +# Execute postremove code +exec_postrm() +{ + if [[ -z $POSTRMLIST ]]; then + return 0 + fi + for pkg in $POSTRMLIST; do + if [[ $(function_exists postrm_$pkg) ]]; then + prnt I "Exécution de la postretrait de $pkg ..." + postrm_$pkg + fi + done + unset POSTRMLIST +} +export -f exec_postrm + + +# ------------------------------------------------------------------------------ +# Execute preupgrade code +exec_preupgd() +{ + local pkglist=$($GET_UPGRADELIST) + for pkg in $pkglist; do + if [[ $(function_exists preupgd_$pkg) ]]; then + prnt I "Exécution de la pré mise à jour de $pkg ..." + preupgd_$pkg + fi + done + export POSTUPGDLIST=$pkglist + unset pkglist +} +export -f exec_preupgd + + +# ------------------------------------------------------------------------------ +# Execute postupgrade code +exec_postupgd() +{ + if [[ -z $POSTUPGDLIST ]]; then + return 0 + fi + for pkg in $POSTUPGDLIST; do + if [[ $(function_exists postupgd_$pkg) ]]; then + prnt I "Exécution de la post mise à jour de $pkg ..." + postupgd_$pkg + fi + done + unset POSTUPGDLIST +} +export -f exec_postupgd + + +# ------------------------------------------------------------------------------ +# Execute prerm code in autoremove context +exec_preautorm() +{ + local pkglist=$($GET_AUTOREMLIST) + for pkg in $pkglist; do + if [[ $(function_exists prerm_$pkg) ]]; then + prnt I "Exécution du préretrait de $pkg ..." + prerm_$pkg + fi + done + export POSTRMLIST=$pkglist + unset pkglist +} +export -f exec_preautorm + + +# ------------------------------------------------------------------------------ +# Execute postrm code in autoremove context +exec_postautorm() +{ + exec_postrm +} +export -f exec_postautorm + # EOF diff --git a/modules/install_pkg.sh b/modules/install_pkg.sh index 3c1539a..14ab508 100644 --- a/modules/install_pkg.sh +++ b/modules/install_pkg.sh @@ -13,7 +13,7 @@ # * PKGSEL: List of package to install # ------------------------------------------------------------------------------ -export VER_install_pkg="0.1.1" +export VER_install_pkg="0.1.2" export DEP_install_pkg="upgrade_dist" install_pkg() @@ -21,7 +21,7 @@ install_pkg() # Remove unnecessary packages if [[ -n $PKGS_RMLIST ]]; then prnt I "Suppression de paquets supperflus..." - pkgrem $PKGS_RMLIST + pkgrm $PKGS_RMLIST fi # Blacklist some anoying packages (and remove them if needed) @@ -33,7 +33,7 @@ install_pkg() sed -i -e "s/@pkg@/pkg/" $dest # If blacklisted we suppose uninstall as well (if neeeded) - pkgrem $pkg + pkgrm $pkg done fi @@ -45,7 +45,7 @@ install_pkg() # Cleaning prnt I "Suppression des paquets résiduels le cas échéant..." - pkgautorem + pkgautorm } precheck_install_pkg() diff --git a/modules/upgrade_dist.sh b/modules/upgrade_dist.sh index 199634d..ee3dcaa 100644 --- a/modules/upgrade_dist.sh +++ b/modules/upgrade_dist.sh @@ -13,7 +13,7 @@ # * PROXYAPTPORT: Working port for APT proxy # ------------------------------------------------------------------------------ -export VER_upgrade_dist="0.1.2" +export VER_upgrade_dist="0.1.3" # As aptitude might fail if clock is too far from real time, we need to depend # on ntp @@ -53,7 +53,7 @@ upgrade_dist() pkgupgd prnt I "Suppression des paquets résiduels..." - pkgautorem + pkgautorm } precheck_upgrade_dist()