From 86be20d2bda7a3d2c0d0d53b5a739e0dce5d09a1 Mon Sep 17 00:00:00 2001 From: m8in Date: Fri, 19 Jun 2026 23:14:36 +0200 Subject: [PATCH] composition.shouldBeSyncedByThisHost() extended to composition.shouldBeSyncedByGivenHost() to enable check in sync-send.sh --- module/composition.module.sh | 33 ++++++++++--------- script/host/zfs/composition-sync/sync-send.sh | 5 +++ script/host/zfs/composition-sync/sync.sh | 6 ++-- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/module/composition.module.sh b/module/composition.module.sh index 3479427..a7828f0 100755 --- a/module/composition.module.sh +++ b/module/composition.module.sh @@ -35,11 +35,11 @@ function composition.printAllRunningOnThisHost() { # composition.printAllSyncedByThisHost # # 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() { composition.printAll | while read -r _COMPOSITION; do - composition.shouldBeSyncedByThisHost "${_COMPOSITION}" \ + composition.shouldBeSyncedByGivenHost "${_COMPOSITION}" "${CIS[HOST]}" \ && echo "${_COMPOSITION}" done } @@ -66,26 +66,27 @@ function composition.shouldRunOnThisHost() { return 1 } -# composition.shouldBeSyncedByThisHost COMPOSITION +# composition.shouldBeSyncedByGivenHost COMPOSITION # - 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. -# This host either runs the composition or syncs it. -function composition.shouldBeSyncedByThisHost() { - local _COMPOSITION _COMPOSITION_PATH _CURRENTHOST_FILE _SYNCHOSTS_FILE - _COMPOSITION="${1:?"composition.shouldBeSyncedByThisHost(): Missing first parameter COMPOSITION"}" - _COMPOSITION_PATH="${CIS[COMPOSITIONS]:?"composition.shouldBeSyncedByThisHost(): Missing CIS_COMPOSITIONS"}${_COMPOSITION}/" - _CURRENTHOST_FILE="current-host" - _SYNCHOSTS_FILE="composition-sync-hosts" - readonly _COMPOSITION _COMPOSITION_PATH _CURRENTHOST_FILE _SYNCHOSTS_FILE +# The given host either runs the composition or syncs it. +function composition.shouldBeSyncedByGivenHost() { + local _COMPOSITION _HOST _COMPOSITION_PATH _CURRENTHOST_FILE _SYNCHOSTS_FILE + _COMPOSITION="${1:?"composition.shouldBeSyncedByGivenHost(): Missing first parameter COMPOSITION"}" + _HOST="${2:?"composition.shouldBeSyncedByGivenHost(): Missing second parameter HOST"}" + _COMPOSITION_PATH="${CIS[COMPOSITIONS]:?"composition.shouldBeSyncedByGivenHost(): Missing CIS_COMPOSITIONS"}${_COMPOSITION}/" + _CURRENTHOST_FILE="${_COMPOSITION_PATH}current-host" + _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. # If there is no CURRENTHOST_FILE than the definition is invalid and should not be synced. - [ -n "${CIS[HOST]}" ] \ - && [ -f "${_COMPOSITION_PATH}${_CURRENTHOST_FILE}" ] \ - && head -n 1 -- "${_COMPOSITION_PATH}${_CURRENTHOST_FILE}" | grep -q -v -E -- "^${CIS[HOST]}" \ - && grep -q -E -- "^${CIS[HOST]}" "${_COMPOSITION_PATH}${_SYNCHOSTS_FILE}" \ + [ -f "${_CURRENTHOST_FILE}" ] \ + && head -n 1 -- "${_CURRENTHOST_FILE}" | grep -q -v -E -- "^${_HOST}" \ + && grep -q -E -- "^${_HOST}" "${_SYNCHOSTS_FILE}" \ && return 0 return 1 diff --git a/script/host/zfs/composition-sync/sync-send.sh b/script/host/zfs/composition-sync/sync-send.sh index 1e0e0ef..f2cad89 100755 --- a/script/host/zfs/composition-sync/sync-send.sh +++ b/script/host/zfs/composition-sync/sync-send.sh @@ -1,5 +1,6 @@ #!/bin/bash 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 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 if [ "${RECEIVERS_SNAPSHOT}" == "@RESUME" ] && [ -n "${RESUME_TOKEN}" ]; then sendResume "${RESUME_TOKEN}" diff --git a/script/host/zfs/composition-sync/sync.sh b/script/host/zfs/composition-sync/sync.sh index 46ff7b9..12d292b 100755 --- a/script/host/zfs/composition-sync/sync.sh +++ b/script/host/zfs/composition-sync/sync.sh @@ -161,7 +161,7 @@ function receiveLoopSingle() { _COMPOSITION="${1:?"receiveLoopSingle(): Missing first parameter COMPOSITION"}" readonly _COMPOSITION - while composition.shouldBeSyncedByThisHost "${_COMPOSITION}"; do + while composition.shouldBeSyncedByGivenHost "${_COMPOSITION}" "${CIS[HOST]}"; do receive "${_COMPOSITION}" \ && echo "Sleep for 5s" \ && sleep 5 \ @@ -175,7 +175,7 @@ function receiveLoopSingle() { return 1 done - ! composition.shouldBeSyncedByThisHost "${_COMPOSITION}" \ + ! composition.shouldBeSyncedByGivenHost "${_COMPOSITION}" "${CIS[HOST]}" \ && 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"}" readonly _COMPOSITION - ! composition.shouldBeSyncedByThisHost "${_COMPOSITION}" \ + ! composition.shouldBeSyncedByGivenHost "${_COMPOSITION}" "${CIS[HOST]}" \ && echo "This host '${CIS[HOST]}' is no sync-host for composition: '${_COMPOSITION}'" \ && return 1