Added composition.printRunningHost() and used it to encapsulate definition files. Changed current-host to running-host.

This commit is contained in:
m8in
2026-06-24 19:59:29 +02:00
parent 8044d776d8
commit 423d1c38e9
4 changed files with 39 additions and 20 deletions
+29 -13
View File
@@ -33,6 +33,22 @@ function composition.printAllRunningOnThisHost() {
done done
} }
# composition.printRunningHost
#
# This function prints the name of the host the given composition is running on.
function composition.printRunningHost() {
local _COMPOSITION _RUNNINGHOST_FILE
_COMPOSITION="${1:?"composition.printRunningHost(): Missing first parameter COMPOSITION"}"
_RUNNINGHOST_FILE="${CIS[COMPOSITIONS]:?"composition.printRunningHost(): Missing CIS_COMPOSITIONS"}${_COMPOSITION}/running-host"
readonly _COMPOSITION _RUNNINGHOST_FILE
[ -f "${_RUNNINGHOST_FILE}" ] \
&& head -n 1 -- "${_RUNNINGHOST_FILE}" \
&& return 0
return 1
}
# composition.printAllSyncedByThisHost # composition.printAllSyncedByThisHost
# #
# This function prints a list of all compositions which should be synced to this host, one per line. # This function prints a list of all compositions which should be synced to this host, one per line.
@@ -75,19 +91,19 @@ function composition.printZFS() {
# - COMPOSITION mandatory: "uptime-kuma-prod" # - COMPOSITION mandatory: "uptime-kuma-prod"
# #
# This function checks if the given composition runs or should run on this host. # This function checks if the given composition runs or should run on this host.
# Therefore a file 'current-host' defines the the current host where the composition runs. # Therefore a file 'running-host' defines the the current host where the composition runs.
function composition.shouldRunOnThisHost() { function composition.shouldRunOnThisHost() {
local _COMPOSITION _CURRENTHOST_FILE local _COMPOSITION _RUNNINGHOST_FILE
_COMPOSITION="${1:?"composition.shouldRunOnThisHost(): Missing first parameter COMPOSITION"}" _COMPOSITION="${1:?"composition.shouldRunOnThisHost(): Missing first parameter COMPOSITION"}"
_CURRENTHOST_FILE="${CIS[COMPOSITIONS]:?"composition.shouldRunOnThisHost(): Missing CIS_COMPOSITIONS"}${_COMPOSITION}/current-host" _RUNNINGHOST_FILE="${CIS[COMPOSITIONS]:?"composition.shouldRunOnThisHost(): Missing CIS_COMPOSITIONS"}${_COMPOSITION}/running-host"
readonly _COMPOSITION _CURRENTHOST_FILE readonly _COMPOSITION _RUNNINGHOST_FILE
! [ -f "${_CURRENTHOST_FILE}" ] \ ! [ -f "${_RUNNINGHOST_FILE}" ] \
&& print.failure "Missing file current-host for composition: '${_COMPOSITION}'" \ && print.failure "Missing file running-host for composition: '${_COMPOSITION}'" \
&& return 1 && return 1
[ -n "${CIS[HOST]}" ] \ [ -n "${CIS[HOST]}" ] \
&& head -n 1 -- "${_CURRENTHOST_FILE}" | grep -q -E -- "^${CIS[HOST]}" \ && head -n 1 -- "${_RUNNINGHOST_FILE}" | grep -q -E -- "^${CIS[HOST]}" \
&& return 0 && return 0
return 1 return 1
@@ -101,18 +117,18 @@ function composition.shouldRunOnThisHost() {
# Therefore a file 'composition-sync-hosts' defines a list of host, one per line, where the composition should be synced to. # Therefore a file 'composition-sync-hosts' defines a list of host, one per line, where the composition should be synced to.
# The given host either runs the composition or syncs it. # The given host either runs the composition or syncs it.
function composition.shouldBeSyncedByGivenHost() { function composition.shouldBeSyncedByGivenHost() {
local _COMPOSITION _HOST _COMPOSITION_PATH _CURRENTHOST_FILE _SYNCHOSTS_FILE local _COMPOSITION _HOST _COMPOSITION_PATH _RUNNINGHOST_FILE _SYNCHOSTS_FILE
_COMPOSITION="${1:?"composition.shouldBeSyncedByGivenHost(): Missing first parameter COMPOSITION"}" _COMPOSITION="${1:?"composition.shouldBeSyncedByGivenHost(): Missing first parameter COMPOSITION"}"
_HOST="${2:?"composition.shouldBeSyncedByGivenHost(): Missing second parameter HOST"}" _HOST="${2:?"composition.shouldBeSyncedByGivenHost(): Missing second parameter HOST"}"
_COMPOSITION_PATH="${CIS[COMPOSITIONS]:?"composition.shouldBeSyncedByGivenHost(): Missing CIS_COMPOSITIONS"}${_COMPOSITION}/" _COMPOSITION_PATH="${CIS[COMPOSITIONS]:?"composition.shouldBeSyncedByGivenHost(): Missing CIS_COMPOSITIONS"}${_COMPOSITION}/"
_CURRENTHOST_FILE="${_COMPOSITION_PATH}current-host" _RUNNINGHOST_FILE="${_COMPOSITION_PATH}running-host"
_SYNCHOSTS_FILE="${_COMPOSITION_PATH}composition-sync-hosts" _SYNCHOSTS_FILE="${_COMPOSITION_PATH}composition-sync-hosts"
readonly _COMPOSITION _HOST _COMPOSITION_PATH _CURRENTHOST_FILE _SYNCHOSTS_FILE readonly _COMPOSITION _HOST _COMPOSITION_PATH _RUNNINGHOST_FILE _SYNCHOSTS_FILE
# This host either runs the composition or syncs it. # This host either runs the composition or syncs it.
# If there is no CURRENTHOST_FILE than the definition is invalid and should not be synced. # If there is no _RUNNINGHOST_FILE than the definition is invalid and should not be synced.
[ -f "${_CURRENTHOST_FILE}" ] \ [ -f "${_RUNNINGHOST_FILE}" ] \
&& head -n 1 -- "${_CURRENTHOST_FILE}" | grep -q -v -E -- "^${_HOST}" \ && head -n 1 -- "${_RUNNINGHOST_FILE}" | grep -q -v -E -- "^${_HOST}" \
&& grep -q -E -- "^${_HOST}" "${_SYNCHOSTS_FILE}" \ && grep -q -E -- "^${_HOST}" "${_SYNCHOSTS_FILE}" \
&& return 0 && return 0
@@ -5,14 +5,14 @@ if [ $(id -u) -ne 0 ]; then
fi fi
source /cis/core/base.module.sh source /cis/core/base.module.sh
base.loadModule composition
function setup() { function setup() {
local _COMPOSITION _COMPOSITIONS_FOLDER _CURRENT_HOST_FILE _SYNC_HOSTS_FILE local _COMPOSITION _COMPOSITIONS_FOLDER _SYNC_HOSTS_FILE
_COMPOSITION="${1}" _COMPOSITION="${1}"
_COMPOSITIONS_FOLDER="${CIS[DOMAINDEFINITIONS]}compositions/" _COMPOSITIONS_FOLDER="${CIS[DOMAINDEFINITIONS]}compositions/"
_CURRENT_HOST_FILE="${_COMPOSITIONS_FOLDER}${_COMPOSITION}/current-host"
_SYNC_HOSTS_FILE="${_COMPOSITIONS_FOLDER}${_COMPOSITION}/composition-sync-hosts" _SYNC_HOSTS_FILE="${_COMPOSITIONS_FOLDER}${_COMPOSITION}/composition-sync-hosts"
readonly _COMPOSITION _COMPOSITIONS_FOLDER readonly _COMPOSITION _COMPOSITIONS_FOLDER
@@ -20,7 +20,7 @@ function setup() {
echo echo
echo "No folder for your defined composition settings found: ${_COMPOSITIONS_FOLDER}" echo "No folder for your defined composition settings found: ${_COMPOSITIONS_FOLDER}"
echo "Please create it and add your custom composition settings in there, following this convention:" echo "Please create it and add your custom composition settings in there, following this convention:"
echo " 1.) './NAME_OF_THE_COMPOSITION/current-host' containing one line with the FQDN of the host running the composition." echo " 1.) './NAME_OF_THE_COMPOSITION/running-host' containing one line with the FQDN of the host running the composition."
echo " 2.) './NAME_OF_THE_COMPOSITION/composition-sync-hosts' containing a list of hosts receiving the composition, one host with its FQDN per line." echo " 2.) './NAME_OF_THE_COMPOSITION/composition-sync-hosts' containing a list of hosts receiving the composition, one host with its FQDN per line."
echo echo
@@ -29,9 +29,8 @@ function setup() {
&& echo && echo
[ -n "${_COMPOSITION}" ] \ [ -n "${_COMPOSITION}" ] \
&& [ -f "${_CURRENT_HOST_FILE}" ] \
&& echo "Current defined host to run composition '${_COMPOSITION}' is:" \ && echo "Current defined host to run composition '${_COMPOSITION}' is:" \
&& cat "${_CURRENT_HOST_FILE}" \ && composition.printRunningHost "${_COMPOSITION}" \
&& echo && echo
[ -n "${_COMPOSITION}" ] \ [ -n "${_COMPOSITION}" ] \
+1 -2
View File
@@ -92,8 +92,7 @@ function receive() {
local _RECEIVERHOST _COMPOSITION _SOURCEHOST _SSH_PORT _SSH_COMMAND _SEND_SCRIPT _ZFS_BRANCH _ZFS local _RECEIVERHOST _COMPOSITION _SOURCEHOST _SSH_PORT _SSH_COMMAND _SEND_SCRIPT _ZFS_BRANCH _ZFS
_COMPOSITION="${1:?"receive(): Missing first parameter COMPOSITION"}" _COMPOSITION="${1:?"receive(): Missing first parameter COMPOSITION"}"
_RECEIVERHOST="${CIS[HOST]:?"Missing CIS_HOST"}" _RECEIVERHOST="${CIS[HOST]:?"Missing CIS_HOST"}"
_SOURCEHOST=$(head -n 1 "${CIS[DOMAINDEFINITIONS]:?"Missing CIS_DOMAINDEFINITIONS"}compositions/${_COMPOSITION}/current-host" 2> /dev/null) base.set _SOURCEHOST "$(composition.printRunningHost "${_COMPOSITION}")" '^[a-zA-Z0-9][a-zA-Z0-9.-]*$'
base.set _SOURCEHOST "${_SOURCEHOST}" '^[a-zA-Z0-9][a-zA-Z0-9.-]*$'
_SSH_PORT=$(head -n 1 "${CIS[DOMAINDEFINITIONS]:?"Missing CIS_DOMAINDEFINITIONS"}compositions/${_COMPOSITION}/ssh-port" 2> /dev/null) _SSH_PORT=$(head -n 1 "${CIS[DOMAINDEFINITIONS]:?"Missing CIS_DOMAINDEFINITIONS"}compositions/${_COMPOSITION}/ssh-port" 2> /dev/null)
base.set _SSH_PORT "${_SSH_PORT:-22}" '^[1-9][0-9]{1,4}$' base.set _SSH_PORT "${_SSH_PORT:-22}" '^[1-9][0-9]{1,4}$'
_SSH_COMMAND="ssh -p ${_SSH_PORT} -o ConnectTimeout=20 -o ServerAliveInterval=15 -C composition-sync@${_SOURCEHOST}" _SSH_COMMAND="ssh -p ${_SSH_PORT} -o ConnectTimeout=20 -o ServerAliveInterval=15 -C composition-sync@${_SOURCEHOST}"
+5
View File
@@ -91,6 +91,11 @@ function snapshot() {
if [ -n "${_ZFS}" ]; then if [ -n "${_ZFS}" ]; then
( (
flock -n 9 || return 1 flock -n 9 || return 1
! composition.shouldRunOnThisHost "${_COMPOSITION}" \
&& printf -- "ZFS will be skipped, because this host '%b' is not running the composition:\n" "${CIS[HOST]}" >&2 \
&& printf -- " - Composition : %b\n" "${_COMPOSITION}" >&2 \
&& printf -- " - Running host: %b\n" "$(composition.printRunningHost "${_COMPOSITION}")" >&2 \
&& continue
case "${_MODE:?"Missing MODE"}" in case "${_MODE:?"Missing MODE"}" in
MINUTELY) zfs snapshot "${_ZFS}@SNAPMINUTELY_${_MINUTE}" 2> /dev/null \ MINUTELY) zfs snapshot "${_ZFS}@SNAPMINUTELY_${_MINUTE}" 2> /dev/null \