Module composition improved

This commit is contained in:
m8in
2026-06-11 23:23:01 +02:00
parent 05294374f0
commit 2928addaa3
+45 -28
View File
@@ -9,13 +9,11 @@ function composition.printAll() {
_CURRENTHOST_FILE="current-host" _CURRENTHOST_FILE="current-host"
readonly _COMPOSITIONS _CURRENTHOST_FILE readonly _COMPOSITIONS _CURRENTHOST_FILE
local _composition ls -1 "${_COMPOSITIONS}"*/"${_CURRENTHOST_FILE}" | while read -r _CURRENTHOST_FILE_PATH; do
for _composition in "${_COMPOSITIONS}"*/; do # Like dirname: removes tailing '/${_CURRENTHOST_FILE}'
if [ -f "${_composition}${_CURRENTHOST_FILE}" ]; then local _COMPOSITION="${_CURRENTHOST_FILE_PATH%/${_CURRENTHOST_FILE}}"
_composition="${_composition%/}" # Like basename
_composition="${_composition##*/}" echo "${_COMPOSITION##*/}"
echo "${_composition}"
fi
done done
} }
@@ -25,33 +23,52 @@ function composition.printAllRunningOnThisHost() {
_CURRENTHOST_FILE="current-host" _CURRENTHOST_FILE="current-host"
readonly _COMPOSITIONS _CURRENTHOST_FILE readonly _COMPOSITIONS _CURRENTHOST_FILE
local _composition grep -l -F "${CIS[HOST]}" "${_COMPOSITIONS}"*/"${_CURRENTHOST_FILE}" | while read -r _CURRENTHOST_FILE_PATH; do
for _composition in "${_COMPOSITIONS}"*/; do # Like dirname: removes tailing '/${_CURRENTHOST_FILE}'
if grep -q -F "${CIS[HOST]}" "${_composition}${_CURRENTHOST_FILE}"; then local _COMPOSITION="${_CURRENTHOST_FILE_PATH%/${_CURRENTHOST_FILE}}"
_composition="${_composition%/}" # Like basename
_composition="${_composition##*/}" echo "${_COMPOSITION##*/}"
echo "${_composition}"
fi
done done
} }
function composition.printAllWhereThisHostSyncs() { function composition.printAllWhereThisHostSyncs() {
local _COMPOSITIONS _CURRENTHOST_FILE _SYNCHOST_FILE local _COMPOSITIONS _CURRENTHOST_FILE _SYNCHOSTS_FILE
_COMPOSITIONS="${CIS[DOMAINDEFINITIONS]:?"Missing CIS_DOMAINDEFINITIONS"}compositions/" _COMPOSITIONS="${CIS[DOMAINDEFINITIONS]:?"Missing CIS_DOMAINDEFINITIONS"}compositions/"
_CURRENTHOST_FILE="current-host" _CURRENTHOST_FILE="current-host"
_SYNCHOST_FILE="composition-sync-hosts" _SYNCHOSTS_FILE="composition-sync-hosts"
readonly _COMPOSITIONS _CURRENTHOST_FILE _SYNCHOST_FILE readonly _COMPOSITIONS _CURRENTHOST_FILE _SYNCHOSTS_FILE
local _composition grep -lF "${CIS[HOST]}" "${_COMPOSITIONS}"*/"${_SYNCHOSTS_FILE}" | while read -r _SYNCHOSTS_FILE_PATH; do
for _composition in "${_COMPOSITIONS}"*/; do
if grep -q -F "${CIS[HOST]}" "${_composition}${_CURRENTHOST_FILE}"; then # Like dirname: removes tailing '/${_SYNCHOSTS_FILE}'
# This host either runs the composition or syncs it. local _COMPOSITION="${_SYNCHOSTS_FILE_PATH%/${_SYNCHOSTS_FILE}}"
continue
fi # This host either runs the composition or syncs it.
if grep -q -F "${CIS[HOST]}" "${_composition}${_SYNCHOST_FILE}"; then # If there is no CURRENTHOST_FILE than the definition is invalid and should not be synced.
_composition="${_composition%/}" [ -f "${_COMPOSITION}/${_CURRENTHOST_FILE}" ] \
_composition="${_composition##*/}" && grep -q -v -F "${CIS[HOST]}" "${_COMPOSITION}/${_CURRENTHOST_FILE}" \
echo "${_composition}" && echo "${_COMPOSITION##*/}"
fi
done done
} }
# Check if this module was started correctly using source
if [ "${BASH_SOURCE[0]}" == "${0}" ]; then
# Script was executed directly
echo "FAILURE: you are using this module 'composition.module.sh' in a wrong way."
echo " It is intended as a utility library and should not be called directly."
echo
echo "Usage: Call this module at the beginning of your script e.g. like this:"
echo
echo ' #!/bin/bash'
echo ' source /cis/core/base.module.sh'
echo
echo ' #Loads this module'
echo ' base.loadModule composition'
echo
echo "Now you can use the functions provided by this module inside your script:"
echo "-------------------------------------------------------------------------"
declare -F | grep "composition." | cut -d" " -f3
exit 1
fi