composition.shouldBeSyncedByThisHost() extended to composition.shouldBeSyncedByGivenHost() to enable check in sync-send.sh

This commit is contained in:
m8in
2026-06-19 23:14:36 +02:00
parent ffebc8c66e
commit 86be20d2bd
3 changed files with 25 additions and 19 deletions
+17 -16
View File
@@ -35,11 +35,11 @@ function composition.printAllRunningOnThisHost() {
# 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.
# see also: composition.shouldBeSyncedByThisHost # see also: composition.shouldBeSyncedByGivenHost
function composition.printAllSyncedByThisHost() { function composition.printAllSyncedByThisHost() {
composition.printAll | while read -r _COMPOSITION; do composition.printAll | while read -r _COMPOSITION; do
composition.shouldBeSyncedByThisHost "${_COMPOSITION}" \ composition.shouldBeSyncedByGivenHost "${_COMPOSITION}" "${CIS[HOST]}" \
&& echo "${_COMPOSITION}" && echo "${_COMPOSITION}"
done done
} }
@@ -66,26 +66,27 @@ function composition.shouldRunOnThisHost() {
return 1 return 1
} }
# composition.shouldBeSyncedByThisHost COMPOSITION # composition.shouldBeSyncedByGivenHost COMPOSITION
# - COMPOSITION mandatory: "uptime-kuma-prod" # - COMPOSITION mandatory: "uptime-kuma-prod"
# - HOST mandatory: "server.your-domain.net"
# #
# This function checks if the given composition is or should be synced to this host. # This function checks if the given composition is or should be synced to the given host.
# 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.
# This host either runs the composition or syncs it. # The given host either runs the composition or syncs it.
function composition.shouldBeSyncedByThisHost() { function composition.shouldBeSyncedByGivenHost() {
local _COMPOSITION _COMPOSITION_PATH _CURRENTHOST_FILE _SYNCHOSTS_FILE local _COMPOSITION _HOST _COMPOSITION_PATH _CURRENTHOST_FILE _SYNCHOSTS_FILE
_COMPOSITION="${1:?"composition.shouldBeSyncedByThisHost(): Missing first parameter COMPOSITION"}" _COMPOSITION="${1:?"composition.shouldBeSyncedByGivenHost(): Missing first parameter COMPOSITION"}"
_COMPOSITION_PATH="${CIS[COMPOSITIONS]:?"composition.shouldBeSyncedByThisHost(): Missing CIS_COMPOSITIONS"}${_COMPOSITION}/" _HOST="${2:?"composition.shouldBeSyncedByGivenHost(): Missing second parameter HOST"}"
_CURRENTHOST_FILE="current-host" _COMPOSITION_PATH="${CIS[COMPOSITIONS]:?"composition.shouldBeSyncedByGivenHost(): Missing CIS_COMPOSITIONS"}${_COMPOSITION}/"
_SYNCHOSTS_FILE="composition-sync-hosts" _CURRENTHOST_FILE="${_COMPOSITION_PATH}current-host"
readonly _COMPOSITION _COMPOSITION_PATH _CURRENTHOST_FILE _SYNCHOSTS_FILE _SYNCHOSTS_FILE="${_COMPOSITION_PATH}composition-sync-hosts"
readonly _COMPOSITION _HOST _COMPOSITION_PATH _CURRENTHOST_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 CURRENTHOST_FILE than the definition is invalid and should not be synced.
[ -n "${CIS[HOST]}" ] \ [ -f "${_CURRENTHOST_FILE}" ] \
&& [ -f "${_COMPOSITION_PATH}${_CURRENTHOST_FILE}" ] \ && head -n 1 -- "${_CURRENTHOST_FILE}" | grep -q -v -E -- "^${_HOST}" \
&& head -n 1 -- "${_COMPOSITION_PATH}${_CURRENTHOST_FILE}" | grep -q -v -E -- "^${CIS[HOST]}" \ && grep -q -E -- "^${_HOST}" "${_SYNCHOSTS_FILE}" \
&& grep -q -E -- "^${CIS[HOST]}" "${_COMPOSITION_PATH}${_SYNCHOSTS_FILE}" \
&& return 0 && return 0
return 1 return 1
@@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
source /cis/core/base.module.sh source /cis/core/base.module.sh
base.loadModule composition
@@ -136,6 +137,10 @@ base.set COMPOSITION "${3}" "${REGEX[COMPOSITION]}"
base.set RECEIVERS_SNAPSHOT "${4}" "${REGEX[SNAPSHOT]}" optional base.set RECEIVERS_SNAPSHOT "${4}" "${REGEX[SNAPSHOT]}" optional
base.set RESUME_TOKEN "${5}" '^[a-zA-Z0-9][a-zA-Z0-9._:-]*$' optional base.set RESUME_TOKEN "${5}" '^[a-zA-Z0-9][a-zA-Z0-9._:-]*$' optional
! composition.shouldBeSyncedByGivenHost "${COMPOSITION}" "${RECEIVERHOST}" \
&& echo "FAILURE: The host '${RECEIVERHOST}' is no sync-host for composition: '${COMPOSITION}'" >&2 \
&& exit 1
# Resume mode # Resume mode
if [ "${RECEIVERS_SNAPSHOT}" == "@RESUME" ] && [ -n "${RESUME_TOKEN}" ]; then if [ "${RECEIVERS_SNAPSHOT}" == "@RESUME" ] && [ -n "${RESUME_TOKEN}" ]; then
sendResume "${RESUME_TOKEN}" sendResume "${RESUME_TOKEN}"
+3 -3
View File
@@ -161,7 +161,7 @@ function receiveLoopSingle() {
_COMPOSITION="${1:?"receiveLoopSingle(): Missing first parameter COMPOSITION"}" _COMPOSITION="${1:?"receiveLoopSingle(): Missing first parameter COMPOSITION"}"
readonly _COMPOSITION readonly _COMPOSITION
while composition.shouldBeSyncedByThisHost "${_COMPOSITION}"; do while composition.shouldBeSyncedByGivenHost "${_COMPOSITION}" "${CIS[HOST]}"; do
receive "${_COMPOSITION}" \ receive "${_COMPOSITION}" \
&& echo "Sleep for 5s" \ && echo "Sleep for 5s" \
&& sleep 5 \ && sleep 5 \
@@ -175,7 +175,7 @@ function receiveLoopSingle() {
return 1 return 1
done done
! composition.shouldBeSyncedByThisHost "${_COMPOSITION}" \ ! composition.shouldBeSyncedByGivenHost "${_COMPOSITION}" "${CIS[HOST]}" \
&& echo "This host '${CIS[HOST]}' is no sync-host (anymore) for composition: '${_COMPOSITION}'" && echo "This host '${CIS[HOST]}' is no sync-host (anymore) for composition: '${_COMPOSITION}'"
} }
@@ -194,7 +194,7 @@ function receiveOnceSingle() {
_COMPOSITION="${1:?"receiveOnceSingle(): Missing first parameter COMPOSITION"}" _COMPOSITION="${1:?"receiveOnceSingle(): Missing first parameter COMPOSITION"}"
readonly _COMPOSITION readonly _COMPOSITION
! composition.shouldBeSyncedByThisHost "${_COMPOSITION}" \ ! composition.shouldBeSyncedByGivenHost "${_COMPOSITION}" "${CIS[HOST]}" \
&& echo "This host '${CIS[HOST]}' is no sync-host for composition: '${_COMPOSITION}'" \ && echo "This host '${CIS[HOST]}' is no sync-host for composition: '${_COMPOSITION}'" \
&& return 1 && return 1