feat: multi-platform alerting + shared system.env config with dynamic help values
- lib/notify.sh: route notify_send to every platform in NOTIFY_PLATFORM (notify.env, default telegram; comma-separated = send to all). New platforms need only a bin/pos-communication-<p> sender implementing 'send <value> [--markdown]' (Matrix/Synapse ready) - pos-communication-telegram: add --markdown as alias for --parse-mode markdown to match the sender contract - lib/common.sh: load_system_env() — shared ~/.config/linux_post_install/ system.env for pos-system-* tools (env exported > file > default) - pos-system-health/backup: load system.env and show effective dynamic values (NOTIFY_PLATFORM, HEALTH_BACKUP_MAX_AGE_DAYS, BACKUP_SERVICE_ROOTS) in --help - config/system.env + config/notify.env templates copied by postinstall - systemd/pos-health.service: EnvironmentFile for both configs so the daily digest honors them
This commit is contained in:
@@ -117,5 +117,24 @@ confirm() {
|
||||
fi
|
||||
}
|
||||
|
||||
# ── system.env loader ──────────────────────────────────────────
|
||||
# Shared "system" tool config (~/.config/linux_post_install/system.env).
|
||||
# Fills only variables that are not already exported — an explicitly-set
|
||||
# environment variable always wins (flags > environment > file).
|
||||
load_system_env() {
|
||||
local f="$HOME/.config/linux_post_install/system.env" k v
|
||||
[ -f "$f" ] || return 0
|
||||
while IFS='=' read -r k v; do
|
||||
[ -n "$k" ] || continue
|
||||
case "$k" in
|
||||
\#*) continue ;;
|
||||
esac
|
||||
v="${v%\"}"; v="${v#\"}"; v="${v%\'}"; v="${v#\'}"
|
||||
if [ -z "${!k:-}" ]; then
|
||||
export "$k"="$v"
|
||||
fi
|
||||
done < <(grep -E '^[A-Z_]+=' "$f" || true)
|
||||
}
|
||||
|
||||
# ── Source guard ───────────────────────────────────────────────
|
||||
return 0 2>/dev/null || true
|
||||
|
||||
+53
-23
@@ -1,16 +1,39 @@
|
||||
#!/usr/bin/env bash
|
||||
# lib/notify.sh — optional alerting helper. Self-contained by design:
|
||||
# defines ONLY notify_send() so it can be sourced by tools that define
|
||||
# their own log/warn/err (e.g. pos-system-firewall) without clobbering.
|
||||
# lib/notify.sh — optional MULTI-PLATFORM alerting helper. Self-contained by
|
||||
# design: defines ONLY notify_send() + notify_platforms() (plus internal
|
||||
# helpers) so it can be sourced by tools that define their own log/warn/err
|
||||
# (e.g. pos-system-firewall) without clobbering.
|
||||
#
|
||||
# Usage (opt-in — source it, do NOT auto-load from common.sh):
|
||||
# source "$(dirname "$0")/../lib/notify.sh" 2>/dev/null || source "$(dirname "$0")/notify.sh"
|
||||
# notify_send "Backup completed: $ARCHIVE"
|
||||
# notify_send "⚠ disk full" --markdown
|
||||
# notify_send "**disk full**" --markdown
|
||||
#
|
||||
# Delegates to `pos communication telegram send`; silent-fail if the
|
||||
# telegram sender is missing or not configured (warns, never breaks the
|
||||
# caller and never changes its exit code).
|
||||
# Platform routing — ~/.config/linux_post_install/notify.env:
|
||||
# NOTIFY_PLATFORM=telegram,matrix
|
||||
# Comma-separated = send to every listed platform (default: telegram).
|
||||
#
|
||||
# Sender contract — each platform is a bin/pos-communication-<platform> tool
|
||||
# that MUST implement:
|
||||
# pos-communication-<platform> send <value> [--markdown]
|
||||
# (exit 0 on delivery; non-zero on failure)
|
||||
# To add a platform (e.g. Matrix/Synapse), add `bin/pos-communication-matrix`
|
||||
# implementing that interface and put `matrix` in NOTIFY_PLATFORM.
|
||||
#
|
||||
# Silent-fails per platform: a missing sender or a failed send only warns and
|
||||
# never changes the caller's exit code.
|
||||
|
||||
CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/linux_post_install"
|
||||
|
||||
# Effective platform list (env > notify.env > "telegram").
|
||||
notify_platforms() {
|
||||
local p="${NOTIFY_PLATFORM:-}"
|
||||
if [ -z "$p" ] && [ -f "$CONFIG_DIR/notify.env" ]; then
|
||||
p="$(grep -E '^NOTIFY_PLATFORM=' "$CONFIG_DIR/notify.env" | tail -1 | cut -d= -f2-)"
|
||||
p="${p%\"}"; p="${p#\"}"; p="${p%\'}"; p="${p#\'}"
|
||||
fi
|
||||
printf '%s' "${p:-telegram}"
|
||||
}
|
||||
|
||||
notify_send() {
|
||||
local msg="" markdown=0
|
||||
@@ -26,22 +49,29 @@ notify_send() {
|
||||
return 0
|
||||
fi
|
||||
|
||||
local tg
|
||||
tg="$(command -v pos-communication-telegram 2>/dev/null)" || \
|
||||
tg="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/../bin/pos-communication-telegram"
|
||||
local -a plist
|
||||
IFS=',' read -r -a plist <<< "$(notify_platforms)"
|
||||
|
||||
if [ ! -x "$tg" ]; then
|
||||
warn "notify_send: pos-communication-telegram not found, notification skipped" 2>/dev/null || true
|
||||
return 0
|
||||
fi
|
||||
local p sender
|
||||
for p in "${plist[@]}"; do
|
||||
p="${p// /}"
|
||||
[ -n "$p" ] || continue
|
||||
sender="$(command -v "pos-communication-${p}" 2>/dev/null)" || \
|
||||
sender="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/../bin/pos-communication-${p}"
|
||||
|
||||
if [ "$markdown" -eq 1 ]; then
|
||||
"$tg" send "$msg" --parse-mode markdown >/dev/null 2>&1 || {
|
||||
warn "notify_send: telegram send failed, notification skipped" 2>/dev/null || true
|
||||
}
|
||||
else
|
||||
"$tg" send "$msg" >/dev/null 2>&1 || {
|
||||
warn "notify_send: telegram send failed, notification skipped" 2>/dev/null || true
|
||||
}
|
||||
fi
|
||||
if [ ! -x "$sender" ]; then
|
||||
warn "notify_send: pos-communication-${p} not found, notification skipped" 2>/dev/null || true
|
||||
continue
|
||||
fi
|
||||
|
||||
if [ "$markdown" -eq 1 ]; then
|
||||
"$sender" send "$msg" --markdown >/dev/null 2>&1 || {
|
||||
warn "notify_send: ${p} send failed, notification skipped" 2>/dev/null || true
|
||||
}
|
||||
else
|
||||
"$sender" send "$msg" >/dev/null 2>&1 || {
|
||||
warn "notify_send: ${p} send failed, notification skipped" 2>/dev/null || true
|
||||
}
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user