98 lines
3.0 KiB
Bash
98 lines
3.0 KiB
Bash
#!/usr/bin/env bash
|
|
# g9-diun-notify.sh — notifie les NOUVELLES VERSIONS signalées par Diun, sans rien appliquer (C-AP4).
|
|
# Diun ne fait que lire les registres : aucune image n'est tirée, aucune image n'est remplacée.
|
|
# Usage : g9-diun-notify.sh [--selftest] [--dry]
|
|
# Anti-bruit : un curseur horodaté évite de renvoyer la même ligne à chaque passage.
|
|
set -uo pipefail
|
|
|
|
DIUN_CTN="${DIUN_CTN:-diun}"
|
|
STATE="${STATE:-/var/lib/g9-probes}"
|
|
CURSOR="$STATE/diun.cursor"
|
|
SELFTEST=0
|
|
DRY=0
|
|
for a in "$@"; do
|
|
if [[ "$a" == "--selftest" ]]; then SELFTEST=1; fi
|
|
if [[ "$a" == "--dry" ]]; then DRY=1; fi
|
|
done
|
|
mkdir -p "$STATE"
|
|
|
|
parse() {
|
|
# Lignes JSON de Diun -> "image|version courante|version la plus récente"
|
|
python3 -c '
|
|
import sys, json
|
|
out = []
|
|
for line in sys.stdin:
|
|
line = line.strip()
|
|
if not line.startswith("{"):
|
|
continue
|
|
try:
|
|
d = json.loads(line)
|
|
except Exception:
|
|
continue
|
|
if d.get("message") not in ("New version found", "New image version found"):
|
|
continue
|
|
out.append("%s -> %s" % (d.get("image", "?"), d.get("latest", d.get("version", "?"))))
|
|
print("\n".join(sorted(set(out))))
|
|
'
|
|
}
|
|
|
|
payload_from() {
|
|
local listed="$1" ts
|
|
ts="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
python3 -c 'import json,sys; print(json.dumps({"ts":sys.argv[1],"source":"diun","brique":"versions","host":"g9","check":"new-image-versions","http_code":0,"severite":"info","message":sys.argv[2],"runbook":"RUNBOOKS/digest.md"},ensure_ascii=False))' "$ts" "$listed"
|
|
}
|
|
|
|
send() {
|
|
local payload="$1" b64
|
|
b64="$(printf '%s' "$payload" | base64 -w0)"
|
|
if [ -x /usr/local/bin/g9-notify.py ]; then
|
|
python3 /usr/local/bin/g9-notify.py "$b64"
|
|
else
|
|
source "$(dirname "$0")/lib-notify.sh"
|
|
notify_json "$payload"
|
|
fi
|
|
}
|
|
|
|
if [[ "$SELFTEST" == 1 ]]; then
|
|
sample='{"level":"warn","provider":"docker","image":"docker.io/library/redis:7","latest":"docker.io/library/redis:7.2.4","time":"2026-10-12T06:20:00+02:00","message":"New version found"}'
|
|
listed="$(printf '%s\n' "$sample" | parse)"
|
|
if [[ -z "$listed" ]]; then
|
|
echo "DIUN_KO parse"
|
|
exit 1
|
|
fi
|
|
payload="$(payload_from "selftest — $listed")"
|
|
if [[ "$DRY" == 1 ]]; then
|
|
echo "$payload"
|
|
exit 0
|
|
fi
|
|
send "$payload"
|
|
exit 0
|
|
fi
|
|
|
|
if ! docker ps --format '{{.Names}}' | grep -qx "$DIUN_CTN"; then
|
|
echo "DIUN_KO conteneur $DIUN_CTN absent"
|
|
exit 1
|
|
fi
|
|
|
|
since="$(cat "$CURSOR" 2>/dev/null)"
|
|
if [[ -z "$since" ]]; then
|
|
since="$(date -u -d '24 hours ago' +%Y-%m-%dT%H:%M:%SZ)"
|
|
fi
|
|
now="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
|
|
listed="$(docker logs --since "$since" "$DIUN_CTN" 2>&1 | parse)"
|
|
if [[ -z "$listed" ]]; then
|
|
echo "DIUN_OK aucune nouvelle version depuis $since"
|
|
printf '%s' "$now" > "$CURSOR"
|
|
exit 0
|
|
fi
|
|
|
|
nb="$(printf '%s\n' "$listed" | grep -c .)"
|
|
payload="$(payload_from "$nb version(s) plus récente(s) signalée(s) : $(printf '%s; ' $listed) — Diun n'applique rien, dry-run Watchtower = scanned/2")"
|
|
if [[ "$DRY" == 1 ]]; then
|
|
echo "$payload"
|
|
exit 0
|
|
fi
|
|
send "$payload"
|
|
printf '%s' "$now" > "$CURSOR"
|