From 23d36cc8af1b315de3212ac5031cc9c573aac0cb Mon Sep 17 00:00:00 2001 From: fatalerrors Date: Fri, 5 May 2023 19:49:13 +0200 Subject: [PATCH] added group and domain support for conf file and repository --- lib/filefct.sh | 69 +++++++++++++++++++++++++++++++++++--------------- lib/loaders.sh | 10 +++++--- 2 files changed, 56 insertions(+), 23 deletions(-) diff --git a/lib/filefct.sh b/lib/filefct.sh index 271ad41..f54fa5c 100644 --- a/lib/filefct.sh +++ b/lib/filefct.sh @@ -10,9 +10,12 @@ # Some useful variables: -export HOST_REPO_PATH=${HOST_REPO_PATH:-"$MYPATH/repo/hosts/$HOSTNAME"} -export GROUP_REPO_PATH=${GROUP_REPO_PATH:-"$MYPATH/repo/groups"} -export COMM_REPO_PATH=${COMM_REPO_PATH:-"$MYPATH/repo/common"} +export HOST_REPO_PATH=${HOST_REPO_PATH:-"$MYPATH/repo/hosts/$HOSTNAME"} +export REALM_REPO_PATH=${REALM_REPO_PATH:-"$MYPATH/repo/realms/$REALM"} +export GROUP_REPO_PATH=${GROUP_REPO_PATH:-"$MYPATH/repo/groups/$GROUPNAME"} +export RLMGRP_REPO_PATH=${RLMGRP_REPO_PATH:-"$MYPATH/repo/realms/$REALM/groups/$GROUPNAME"} +export RLMHST_REPO_PATH=${RLMHST_REPO_PATH:-"$MYPATH/repo/realms/$REALM/hosts/$HOSTNAME"} +export COMM_REPO_PATH=${COMM_REPO_PATH:-"$MYPATH/repo/common"} # ------------------------------------------------------------------------------ @@ -40,10 +43,10 @@ backup_dist() die 12 fi elif [[ -d ${file} ]]; then - prnt I "Creation a backup of the directory ${file} on $tmstmp..." + prnt I "Creating a backup of the directory ${file} on $tmstmp..." cp -av $file ${file}.dist.${tmstmp} if [[ $? -ne 0 ]]; then - prnt E "backup_dist(): Failed copyind directory recursively." + prnt E "backup_dist(): Failed copying directory recursively." die 12 fi else @@ -58,11 +61,26 @@ export -f backup_dist # ------------------------------------------------------------------------------ # Select source file according to our priority mechanism +# 1) repo//hosts// => specific hostname in specific realm +# 2) repo//groups// => specific group in specific realm +# 3) repo/hosts// => specific hostname in any realm +# 4) repo/groups// => specific group in any realm +# 5) repo// => any hostname in specific realm +# 6) repo/common => any hostname any realm +# Note: if a specific hostname is provided it makes unecessary to find a group. select_file() { local infile=$1 - if [[ -f $HOST_REPO_PATH/$infile ]]; then + if [[ -f $RLMHST_REPO_PATH/$infile ]]; then + local source="$RLMHST_REPO_PATH/$infile" + elif [[ -f $RLMGRP_REPO_PATH/$infile ]]; then + local source="$RLMGRP_REPO_PATH/$infile" + elif [[ -f $HOST_REPO_PATH/$infile ]]; then local source="$HOST_REPO_PATH/$infile" + elif [[ -f $GROUP_REPO_PATH/$infile ]]; then + local source="$GROUP_REPO_PATH/$infile" + elif [[ -f $REALM_REPO_PATH/$infile ]]; then + local source="$REALM_REPO_PATH/$infile" elif [[ -f $COMM_REPO_PATH/$infile ]]; then local source="$COMM_REPO_PATH/$infile" else @@ -77,12 +95,20 @@ export -f select_file # ------------------------------------------------------------------------------ -# Select source directory according to our priority mechanism +# Select source directory according to our priority mechanism (same as above) select_directory() { local indir=$1 - if [[ -d $HOST_REPO_PATH/$indir ]]; then + if [[ -d $RLMHST_REPO_PATH/$indir ]]; then + local source="$RLMHST_REPO_PATH/$indir" + elif [[ -d $RLMGRP_REPO_PATH/$indir ]]; then + local source="$RLMGRP_REPO_PATH/$indir" + elif [[ -d $HOST_REPO_PATH/$indir ]]; then local source="$HOST_REPO_PATH/$indir" + elif [[ -d $GROUP_REPO_PATH/$indir ]]; then + local source="$GROUP_REPO_PATH/$indir" + elif [[ -d $REALM_REPO_PATH/$indir ]]; then + local source="$REALM_REPO_PATH/$indir" elif [[ -d $COMM_REPO_PATH/$indir ]]; then local source="$COMM_REPO_PATH/$indir" else @@ -116,19 +142,16 @@ install_file() local arg= for arg in $@; do filelist="$filelist $(select_file $arg)" + # We always replace until the last argument being the target + target="$arg" done unset arg - # Empty to just obtain the target which is the last element of the list - local file= - for file in $filelist; do - : - done - if [[ ! $file == /* ]]; then + if [[ ! $target == /* ]]; then prnt E "install_file(): Target must be on the root filesystem and full path must be provided." die 13 fi - unset file + unset target if [[ -d $(dirname $i) ]]; then prnt I "Creating required target directory $(dirname $i)..." @@ -152,14 +175,19 @@ export -f install_file # Add the content of a file at the end of an other append_file() { + if [[ $# -ne 2 ]]; then + prnt E "append_file(): Two arguments are required, source and destination." + die 11 + fi + local srcfile=$(select_file $1) local dstfile=$2 - if [[ -e $dstfile ]]; then + if [[ ! $dstfile == /* ]]; then prnt E "append_file(): Target must be on the root filesystem and full path must be provided." die 13 fi - if [[ ! $dstfile == /* ]]; then - prnt E "append_file(): Target file must exist." + if [[ -e $dstfile ]]; then + prnt E "append_file(): Target file must exist (use touch first to create it if required)." die 13 fi @@ -303,6 +331,7 @@ directory_exists() } export -f directory_exists + # ------------------------------------------------------------------------------ # check if file exists and return error if not directory_must_exists() @@ -315,7 +344,7 @@ directory_must_exists() fi unset md } - - export -f directory_must_exists + + # EOF diff --git a/lib/loaders.sh b/lib/loaders.sh index 5832e40..7aee7ed 100644 --- a/lib/loaders.sh +++ b/lib/loaders.sh @@ -53,8 +53,9 @@ export -f load_autoconf # ------------------------------------------------------------------------------ # Load configuration with the following priorities: # 1) Those given on command line, if any -# 2) /conf/.conf (Hostname based and specific) -# 3) /conf/init.conf.sh (Generic default) +# 2) /conf//.conf (Hostname based and specific) +# 2) /conf/.conf (Hostname based and specific) +# 3) /conf/init.conf.sh (Generic default, for testing) load_configuration() { if [[ -n $CONFFILES ]]; then @@ -71,7 +72,10 @@ load_configuration() unset f else prnt I "Loading configuration..." - if [[ -e $MYPATH/conf/$HOSTNAME.conf.sh ]]; then + if [[ -e $MYPATH/conf/$REALM/$HOSTNAME.conf.sh ]]; then + prnt I "A specific configuration will be used." + . $MYPATH/conf/$HOSTNAME.conf.sh + elif [[ -e $MYPATH/conf/$HOSTNAME.conf.sh ]]; then prnt I "A specific configuration will be used." . $MYPATH/conf/$HOSTNAME.conf.sh else