feat(pkg-cache): host package cache is opt-in (default OFF) + origin fallback health check
- /etc/pkgcache-host.conf setting (PKGCACHE_ENABLED, PKGCACHE_URL, PKGCACHE_CA); no cache host is built into the tool. sync/apply/revert/status; status shows mode. - pkgcache-host-health.sh + 5 min timer (installed only while ON): probes repomd.xml and .asc through the cache, 2 failures -> origin-only (reversible marker lines, atomic rename under flock), 3 successes -> cache-first. Both-down is not counted. - scripts/tests/pkgcache-host.test.sh Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6
This commit is contained in:
1 parent
a971412243
commit
b28e561387
5 files changed
+361
-30
No files matched your search
Executable
+137
@@ -0,0 +1,137 @@
|
||||
#!/bin/bash
|
||||
## pkgcache-host-health.sh — keep a cache-configured WHP host's dnf working when the
|
||||
## package cache is down OR broken. Run every 5 min by pkgcache-host-health.timer
|
||||
## (installed only while the package-cache setting is ON; see pkgcache-host-repos.sh).
|
||||
##
|
||||
## pkgcache-host-health.sh run # timer entry: probe, count, switch if needed
|
||||
## pkgcache-host-health.sh fallback # force origin-only now (reversible)
|
||||
## pkgcache-host-health.sh restore # force cache-first now
|
||||
## pkgcache-host-health.sh mode # prints: cache-first | origin-only | off
|
||||
##
|
||||
## Probe: GET <cache>/<repo path>/repodata/repomd.xml AND repomd.xml.asc (the .asc
|
||||
## is what broke on 2026-11-26: the cache answered an empty reply for uncached
|
||||
## objects), 5 s connect / 10 s total, HTTP 200, non-empty, .asc looks like a PGP
|
||||
## signature. If the cache fails the probe but the ORIGIN fails it too, the host
|
||||
## (or the internet) is down, not the cache: nothing is counted.
|
||||
## 2 consecutive cache failures -> origin-only. 3 consecutive successes -> cache-first.
|
||||
##
|
||||
## How "origin-only" is made reversible and dnf-safe:
|
||||
## * only lines of the exact shape tool-managed baseurl=<cache>/(almalinux|epel|remi)/... <origin>/...
|
||||
## are touched; every other repo/line is left alone.
|
||||
## * the cache-first line is kept right above as "#pkgcache-cachefirst baseurl=..."
|
||||
## and the live line becomes baseurl=<origin>/... — restore undoes exactly that.
|
||||
## * each file is rewritten to a temp file in the same directory and rename(2)d into
|
||||
## place (atomic: dnf sees the old or the new file, never half of one), under flock.
|
||||
## Transitions are logged to the journal (tag pkgcache-host-health) and the state file.
|
||||
set -uo pipefail
|
||||
|
||||
REPO_DIR=${PKGCACHE_REPO_DIR:-/etc/yum.repos.d}
|
||||
STATE_DIR=${PKGCACHE_STATE_DIR:-/var/lib/pkgcache-host}
|
||||
FAIL_NEEDED=2
|
||||
OK_NEEDED=3
|
||||
MARK='#pkgcache-cachefirst '
|
||||
MANAGED_RE='^baseurl=https://[^ /]+(:[0-9]+)?/(almalinux|epel|remi)/[^ ]+ https://[^ ]+$'
|
||||
|
||||
log() { logger -t pkgcache-host-health -p "daemon.$1" -- "${*:2}" 2>/dev/null || true; echo "$1: ${*:2}" >&2; }
|
||||
mkdir -p "$STATE_DIR"
|
||||
exec 9>"$STATE_DIR/lock"; flock -w 30 9 || { echo "lock busy" >&2; exit 0; }
|
||||
SF="$STATE_DIR/state"
|
||||
get() { grep -s "^$1=" "$SF" | tail -1 | cut -d= -f2; }
|
||||
put() { printf 'mode=%s\nfails=%s\noks=%s\n' "$1" "$2" "$3" >"$SF.new" && mv -f "$SF.new" "$SF"; }
|
||||
|
||||
have_managed() { grep -q -s -E "$MANAGED_RE" "$REPO_DIR"/*.repo 2>/dev/null; }
|
||||
have_fallback() { grep -q -s -F "$MARK" "$REPO_DIR"/*.repo 2>/dev/null; }
|
||||
|
||||
# python does the atomic per-file rewrite. $1 = to-origin | to-cache
|
||||
rewrite_all() {
|
||||
python3 - "$1" "$REPO_DIR" "$MARK" "$MANAGED_RE" <<'PY'
|
||||
import glob, os, re, sys, tempfile
|
||||
action, d, mark, rx = sys.argv[1:5]
|
||||
rx = re.compile(rx)
|
||||
for f in sorted(glob.glob(os.path.join(d, '*.repo'))):
|
||||
src = open(f).read().split('\n')
|
||||
out, i = [], 0
|
||||
while i < len(src):
|
||||
line = src[i]
|
||||
if action == 'to-origin' and rx.match(line):
|
||||
cache, origin = line[len('baseurl='):].split(' ', 1)
|
||||
out += [mark + line, 'baseurl=' + origin]
|
||||
elif action == 'to-cache' and line.startswith(mark) and i + 1 < len(src) and src[i + 1].startswith('baseurl='):
|
||||
out.append(line[len(mark):]); i += 1
|
||||
else:
|
||||
out.append(line)
|
||||
i += 1
|
||||
if out == src:
|
||||
continue
|
||||
fd, tmp = tempfile.mkstemp(dir=d, prefix='.pkgcache-', suffix='.tmp')
|
||||
st = os.stat(f)
|
||||
with os.fdopen(fd, 'w') as fh:
|
||||
fh.write('\n'.join(out)); fh.flush(); os.fsync(fh.fileno())
|
||||
os.chmod(tmp, st.st_mode & 0o7777); os.chown(tmp, st.st_uid, st.st_gid)
|
||||
os.rename(tmp, f)
|
||||
PY
|
||||
}
|
||||
|
||||
# The managed cache-first line (live, or the stashed one while in origin-only).
|
||||
first_line() { # $1 = regex prefix filter (almalinux|remi) or ''
|
||||
local l
|
||||
l=$( { grep -h -E "$MANAGED_RE" "$REPO_DIR"/*.repo; grep -h -F "$MARK" "$REPO_DIR"/*.repo | sed "s|^$MARK||"; } 2>/dev/null | grep -E "/${1:-(almalinux|epel|remi)}/" | head -1)
|
||||
echo "${l#baseurl=}"
|
||||
}
|
||||
expand() { local rel=${PKGCACHE_RELEASEVER:-$(rpm -E %rhel 2>/dev/null || echo 9)}; sed -e "s|\$releasever|$rel|g" -e "s|\$basearch|$(uname -m)|g"; }
|
||||
fetch() { # url file-suffix -> 0 ok
|
||||
local body code
|
||||
body=$(curl -sS --connect-timeout 5 --max-time 10 -w '\n%{http_code}' "$1" 2>/dev/null) || return 1
|
||||
code=${body##*$'\n'}; body=${body%$'\n'*}
|
||||
[ "$code" = 200 ] && [ -n "$body" ] || return 1
|
||||
case "$1" in *.asc) case "$body" in *'BEGIN PGP SIGNATURE'*) ;; *) return 1 ;; esac ;; esac
|
||||
return 0
|
||||
}
|
||||
probe() { # $1 = cache|origin ; returns 0 when repomd.xml (+ .asc where the repo is signed) fetch fine
|
||||
local line sig=1 base which=$1 u
|
||||
line=$(first_line '(almalinux|remi)'); [ -n "$line" ] || { line=$(first_line ''); sig=0; }
|
||||
[ -n "$line" ] || return 3
|
||||
if [ "$which" = cache ]; then base=${line%% *}; else base=${line#* }; fi
|
||||
base=$(printf '%s' "${PKGCACHE_PROBE_BASE:-$base}" | expand); base=${base%/}
|
||||
u="$base/repodata/repomd.xml"
|
||||
fetch "$u" || return 1
|
||||
[ "$sig" = 1 ] && { fetch "$u.asc" || return 1; }
|
||||
return 0
|
||||
}
|
||||
|
||||
cmd=${1:-mode}
|
||||
case "$cmd" in
|
||||
mode)
|
||||
if have_fallback; then echo origin-only; elif have_managed; then echo cache-first; else echo off; fi ;;
|
||||
fallback) have_managed || { echo "nothing to switch" >&2; exit 0; }
|
||||
rewrite_all to-origin && put origin 0 0 && log warning "FALLBACK (forced): cache-managed repos now origin-only" ;;
|
||||
restore) have_fallback || { echo "not in fallback" >&2; put cache 0 0; exit 0; }
|
||||
rewrite_all to-cache && put cache 0 0 && log notice "RESTORED (forced): cache-managed repos are cache-first again" ;;
|
||||
run)
|
||||
if ! have_managed && ! have_fallback; then echo "no cache-managed repos; nothing to do"; exit 0; fi
|
||||
mode=$(get mode); [ -n "$mode" ] || { have_fallback && mode=origin || mode=cache; }
|
||||
fails=$(get fails); oks=$(get oks); fails=${fails:-0}; oks=${oks:-0}
|
||||
if probe cache; then
|
||||
fails=0; oks=$((oks+1))
|
||||
if [ "$mode" = origin ]; then
|
||||
if [ "$oks" -ge "$OK_NEEDED" ]; then
|
||||
rewrite_all to-cache && { mode=cache; oks=0; log notice "RESTORED: cache healthy for $OK_NEEDED runs, repos cache-first again"; }
|
||||
else log notice "cache answers again ($oks/$OK_NEEDED), staying origin-only"; fi
|
||||
else oks=0; fi
|
||||
else
|
||||
rc=$?
|
||||
if [ "$rc" = 3 ]; then log warning "cannot derive a probe URL from the managed repos"; exit 0; fi
|
||||
oks=0
|
||||
if probe origin; then
|
||||
fails=$((fails+1))
|
||||
log warning "cache probe failed ($fails/$FAIL_NEEDED), origin reachable"
|
||||
if [ "$mode" = cache ] && [ "$fails" -ge "$FAIL_NEEDED" ]; then
|
||||
rewrite_all to-origin && { mode=origin; fails=0; log err "FALLBACK: cache failed $FAIL_NEEDED runs in a row, repos switched to origin-only"; }
|
||||
fi
|
||||
else
|
||||
log warning "cache probe failed but the origin is unreachable too: not counted (network/upstream problem)"
|
||||
fi
|
||||
fi
|
||||
put "$mode" "$fails" "$oks" ;;
|
||||
*) echo "usage: run | fallback | restore | mode" >&2; exit 2 ;;
|
||||
esac
|
||||
Reference in new issue
Block a user