Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bd685f72fb | ||
|
|
8107b29e14 |
No files matched your search
@@ -250,6 +250,14 @@ report: setup key / route pending).
|
||||
|
||||
## Operating the cache (CT130)
|
||||
|
||||
- **Self-check (`pkg-cache/`).** `pkgcache-selfcheck.timer` runs `/usr/local/sbin/pkgcache-selfcheck` every 60 s. Background: on 2026-11-26 the CT's ext4 images went `emergency_ro` and nginx answered an empty reply for anything uncached, so clients' dnf failed instead of falling back.
|
||||
- Checks: `/` and `/srv/cache` mounted rw; write+fsync+delete on both; a refresh fetch through nginx of a tiny metadata file (10 s; one retry).
|
||||
- On failure: stops nginx and `docker-registry`, so clients get connection refused and fall back within a second. It logs at `daemon.err` (`journalctl -t pkgcache-selfcheck`). If the origin is unreachable too it only warns.
|
||||
- Alert: Uptime Kuma monitors 53 and 54 (https://100.123.207.73/ and :5000/v2/) go DOWN because the services are stopped. No webhook is configured on CT130.
|
||||
- Recovery: only for services it stopped. 3 consecutive fully healthy runs and a good `nginx -t`, then it starts them and re-checks the fetch.
|
||||
- It never runs fsck or remounts. After `emergency_ro` an operator still has to fix the disks (see the 2026-11-26 incident) and the self-check restarts the services on its own.
|
||||
- Install: copy the script to `/usr/local/sbin/pkgcache-selfcheck` (0755) and the two units to `/etc/systemd/system/`, then `systemctl enable --now pkgcache-selfcheck.timer`. State is in `/run/pkgcache-selfcheck/`.
|
||||
|
||||
- **nginx config.**
|
||||
- `/etc/nginx/conf.d/pkgcache-http.conf` holds the cache zone, maps and log format.
|
||||
- `/etc/nginx/pkgcache/{server-common,meta,pkg}.conf` hold the shared settings.
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
[Unit]
|
||||
Description=pkg-cache self-check (stop nginx+registry when the cache is unhealthy)
|
||||
Documentation=file:///usr/local/sbin/pkgcache-selfcheck
|
||||
After=nginx.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/local/sbin/pkgcache-selfcheck
|
||||
TimeoutStartSec=120
|
||||
Nice=5
|
||||
Executable
+139
@@ -0,0 +1,139 @@
|
||||
#!/bin/bash
|
||||
## pkgcache-selfcheck — run ON CT130 every 60 s by pkgcache-selfcheck.timer.
|
||||
##
|
||||
## Why: on 2026-11-26 both ext4 images (rootfs + /srv/cache, raw files on a CIFS
|
||||
## 'soft' share) went emergency_ro. nginx kept answering cached files but sent an
|
||||
## EMPTY reply (curl 52) for anything uncached, so clients' dnf failed instead of
|
||||
## falling back to the origin. A refused connection falls back in <1 s; a half-dead
|
||||
## cache does not. So when the cache is not healthy this script STOPS nginx and the
|
||||
## registry (connection refused = fast fallback) and starts them again once healthy.
|
||||
##
|
||||
## Checks (all must pass):
|
||||
## 1. every path in CHECK_PATHS is on a filesystem mounted rw (not ro/emergency_ro)
|
||||
## 2. write + fsync + delete of a small file on each path succeeds (15 s limit)
|
||||
## 3. nginx can fetch a fresh object through itself: a localhost refresh request
|
||||
## (X-Pkgcache-Refresh: 1 bypasses the cached copy and re-stores it) of a tiny
|
||||
## metadata file must return 200 with the expected body within 10 s. If it
|
||||
## fails, the origin is fetched directly: origin also unreachable = an internet
|
||||
## outage, not a cache fault, so nothing is stopped (WARN only).
|
||||
## On failure: stop SERVICES, drop a marker in STATE_DIR, log at daemon.err.
|
||||
## Alert path: Uptime Kuma monitors 53 (https://100.123.207.73/) and 54 (:5000/v2/)
|
||||
## go DOWN because the services are stopped. No webhook/secret is configured on
|
||||
## CT130, none is invented here.
|
||||
## Recovery: only if WE stopped them (marker). After OK_NEEDED consecutive fully
|
||||
## passing runs and a good `nginx -t`, start the services, re-run the fetch check,
|
||||
## and stop them again if it fails. NEVER runs fsck or remounts anything.
|
||||
##
|
||||
## Test knobs (env): PKGCACHE_STATE_DIR, PKGCACHE_CHECK_PATHS, PKGCACHE_SERVICES,
|
||||
## PKGCACHE_PROBE_URL, PKGCACHE_ORIGIN_URL, PKGCACHE_SYSTEMCTL, PKGCACHE_NGINX_TEST.
|
||||
set -uo pipefail
|
||||
|
||||
STATE_DIR=${PKGCACHE_STATE_DIR:-/run/pkgcache-selfcheck}
|
||||
CHECK_PATHS=${PKGCACHE_CHECK_PATHS:-"/ /srv/cache"}
|
||||
SERVICES=${PKGCACHE_SERVICES-"nginx docker-registry"} # empty = manage nothing (tests)
|
||||
PROBE_URL=${PKGCACHE_PROBE_URL:-http://127.0.0.1/almalinux/RPM-GPG-KEY-AlmaLinux-9}
|
||||
ORIGIN_URL=${PKGCACHE_ORIGIN_URL:-https://repo.almalinux.org/almalinux/RPM-GPG-KEY-AlmaLinux-9}
|
||||
PROBE_MAGIC='BEGIN PGP PUBLIC KEY BLOCK'
|
||||
SYSTEMCTL=${PKGCACHE_SYSTEMCTL:-systemctl}
|
||||
NGINX_TEST=${PKGCACHE_NGINX_TEST:-nginx -t}
|
||||
OK_NEEDED=3
|
||||
|
||||
log() { local p=$1; shift; logger -t pkgcache-selfcheck -p "daemon.$p" -- "$*" 2>/dev/null || true; echo "$p: $*" >&2; }
|
||||
|
||||
mkdir -p "$STATE_DIR" 2>/dev/null || { log err "cannot create $STATE_DIR"; exit 1; }
|
||||
exec 9>"$STATE_DIR/lock"
|
||||
flock -n 9 || { echo "another run in progress" >&2; exit 0; }
|
||||
|
||||
MARKER="$STATE_DIR/stopped"
|
||||
OKFILE="$STATE_DIR/okcount"
|
||||
status() { printf '%s %s\n' "$(date -u +%FT%TZ)" "$*" >"$STATE_DIR/status" 2>/dev/null || true; }
|
||||
|
||||
# ---- check 1 + 2: mounts and write+fsync+delete ------------------------------
|
||||
check_disks() {
|
||||
local p opts f
|
||||
for p in $CHECK_PATHS; do
|
||||
opts=$(findmnt -n -o OPTIONS --target "$p" 2>/dev/null) || { echo "$p: findmnt failed"; return 1; }
|
||||
case ",$opts," in *,rw,*) ;; *) echo "$p: not mounted rw (options: $opts)"; return 1 ;; esac
|
||||
case ",$opts," in *,ro,*) echo "$p: mounted ro (options: $opts)"; return 1 ;; esac
|
||||
f="$p/.pkgcache-selfcheck.$$"
|
||||
if ! timeout -k 2 15 dd if=/dev/zero of="$f" bs=4096 count=1 conv=fsync status=none 2>/dev/null; then
|
||||
rm -f -- "$f" 2>/dev/null; echo "$p: write+fsync failed"; return 1
|
||||
fi
|
||||
timeout -k 2 15 rm -f -- "$f" || { echo "$p: delete failed"; return 1; }
|
||||
[ -e "$f" ] && { echo "$p: test file still present after delete"; return 1; }
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
# ---- check 3: nginx fetches a fresh object through itself ---------------------
|
||||
probe_once() {
|
||||
local body
|
||||
body=$(curl -sS --max-time 10 -H 'X-Pkgcache-Refresh: 1' -w '\n%{http_code}' "$PROBE_URL" 2>&1) || { echo "curl: ${body##*$'\n'}"; return 1; }
|
||||
local code=${body##*$'\n'}; body=${body%$'\n'*}
|
||||
[ "$code" = 200 ] || { echo "HTTP $code"; return 1; }
|
||||
case "$body" in *"$PROBE_MAGIC"*) return 0 ;; *) echo "200 but unexpected body (${#body} bytes)"; return 1 ;; esac
|
||||
}
|
||||
# prints a reason on failure; return 2 = cache fault is NOT proven (origin unreachable too)
|
||||
check_fetch() {
|
||||
local why
|
||||
why=$(probe_once) && return 0
|
||||
sleep 3 # one retry: don't act on a single blip
|
||||
why=$(probe_once) && return 0
|
||||
if curl -sS -o /dev/null --max-time 10 -f "$ORIGIN_URL" 2>/dev/null; then
|
||||
echo "fetch through nginx failed ($why) but the origin is reachable"; return 1
|
||||
fi
|
||||
echo "fetch through nginx failed ($why); origin unreachable too (upstream outage, not a cache fault)"; return 2
|
||||
}
|
||||
|
||||
stop_services() {
|
||||
local s
|
||||
for s in $SERVICES; do $SYSTEMCTL stop "$s" 2>&1 | sed 's/^/stop: /' >&2; done
|
||||
printf '%s %s\n' "$(date -u +%FT%TZ)" "$1" >"$MARKER" 2>/dev/null || true
|
||||
echo 0 >"$OKFILE" 2>/dev/null || true
|
||||
}
|
||||
|
||||
# ---- main ---------------------------------------------------------------------
|
||||
# Something else (an admin, a package script) started nginx while our stop marker exists:
|
||||
# do not "recover" on our own schedule or leave it running unchecked. Drop the marker and
|
||||
# judge it like normal operation below, which stops it again if the cache is unhealthy.
|
||||
if [ -e "$MARKER" ] && $SYSTEMCTL is-active --quiet nginx; then
|
||||
log warning "nginx was started by something else while stopped by this script; re-checking it now"
|
||||
rm -f "$MARKER" "$OKFILE"
|
||||
fi
|
||||
if [ -e "$MARKER" ]; then
|
||||
# we stopped the services earlier: look for recovery
|
||||
if why=$(check_disks); then
|
||||
n=$(( $(cat "$OKFILE" 2>/dev/null || echo 0) + 1 )); echo "$n" >"$OKFILE"
|
||||
if [ "$n" -lt "$OK_NEEDED" ]; then
|
||||
log notice "recovering: disks healthy ($n/$OK_NEEDED), services stay stopped"; status "stopped; recovering $n/$OK_NEEDED"; exit 0
|
||||
fi
|
||||
if ! tout=$($NGINX_TEST 2>&1); then
|
||||
log err "disks healthy but nginx config test failed, staying stopped: ${tout//$'\n'/ | }"; echo 0 >"$OKFILE"; status "stopped; nginx -t failed"; exit 1
|
||||
fi
|
||||
for s in $SERVICES; do $SYSTEMCTL start "$s" 2>&1 | sed 's/^/start: /' >&2; done
|
||||
sleep 2
|
||||
why=$(check_fetch); rc=$?
|
||||
if [ $rc = 1 ]; then
|
||||
log err "restarted but the fetch check fails ($why); stopping again"; stop_services "restart check failed: $why"; status "stopped; restart check failed"; exit 1
|
||||
fi
|
||||
rm -f "$MARKER" "$OKFILE"
|
||||
log notice "RECOVERED: $OK_NEEDED consecutive healthy runs, nginx -t ok, services started"; status "ok (recovered)"; exit 0
|
||||
else
|
||||
echo 0 >"$OKFILE"; log err "still unhealthy, services stay stopped: $why"; status "stopped; $why"; exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# normal operation
|
||||
if ! why=$(check_disks); then
|
||||
log err "UNHEALTHY: $why -> stopping ${SERVICES} so clients fall back to the origin (connection refused)"
|
||||
stop_services "$why"; status "stopped; $why"; exit 1
|
||||
fi
|
||||
if ! $SYSTEMCTL is-active --quiet nginx; then
|
||||
log warning "nginx is not active (not stopped by this script); disks fine; fetch check skipped"; status "nginx inactive (manual?)"; exit 0
|
||||
fi
|
||||
why=$(check_fetch); rc=$?
|
||||
case $rc in
|
||||
0) status ok; exit 0 ;;
|
||||
2) log warning "$why"; status "warn: $why"; exit 0 ;;
|
||||
*) log err "UNHEALTHY: $why -> stopping ${SERVICES}"; stop_services "$why"; status "stopped; $why"; exit 1 ;;
|
||||
esac
|
||||
@@ -0,0 +1,10 @@
|
||||
[Unit]
|
||||
Description=Run the pkg-cache self-check every 60 s
|
||||
|
||||
[Timer]
|
||||
OnBootSec=45s
|
||||
OnUnitActiveSec=60s
|
||||
AccuracySec=1s
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
Executable
+86
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env bash
|
||||
## pkgcache-selfcheck.test.sh — fixture test for pkg-cache/pkgcache-selfcheck.sh.
|
||||
## Fakes findmnt/curl/systemctl/logger via PATH; one extra case uses a REAL
|
||||
## read-only bind mount when `sudo -n` works (skipped otherwise).
|
||||
set -euo pipefail
|
||||
HERE=$(cd "$(dirname "$0")/../.." && pwd)
|
||||
S="$HERE/pkg-cache/pkgcache-selfcheck.sh"
|
||||
T=$(mktemp -d); trap 'rm -rf "$T"' EXIT
|
||||
fail=0
|
||||
ok() { echo "ok - $1"; }
|
||||
bad() { echo "FAIL - $1"; fail=1; }
|
||||
mkdir -p "$T/bin" "$T/disk1" "$T/disk2"
|
||||
cat >"$T/bin/findmnt" <<'EOS'
|
||||
#!/bin/sh
|
||||
# last arg = path; $FAKE_RO = path that reports ro
|
||||
for a; do p=$a; done
|
||||
case "$p" in "$FAKE_RO") echo "ro,relatime" ;; *) echo "rw,relatime" ;; esac
|
||||
EOS
|
||||
cat >"$T/bin/curl" <<'EOS'
|
||||
#!/bin/sh
|
||||
# FAKE_PROBE=ok|http500|empty|refused ; FAKE_ORIGIN=ok|down
|
||||
for a; do last=$a; done
|
||||
case "$last" in
|
||||
*probe*) case "$FAKE_PROBE" in
|
||||
ok) printf -- '-----BEGIN PGP PUBLIC KEY BLOCK-----\nx\n200'; exit 0;;
|
||||
http500) printf 'err\n500'; exit 0;;
|
||||
empty) echo "curl: (52) Empty reply from server" >&2; exit 52;;
|
||||
refused) echo "curl: (7) refused" >&2; exit 7;; esac;;
|
||||
*origin*) [ "$FAKE_ORIGIN" = ok ] && exit 0 || exit 7;;
|
||||
esac
|
||||
EOS
|
||||
cat >"$T/bin/systemctl" <<'EOS'
|
||||
#!/bin/sh
|
||||
echo "$*" >>"$CALLS"
|
||||
case "$1" in is-active) [ -e "$ACTIVE" ]; exit $?;; start) touch "$ACTIVE";; stop) rm -f "$ACTIVE";; esac
|
||||
exit 0
|
||||
EOS
|
||||
printf '#!/bin/sh\nexit 0\n' >"$T/bin/logger"; chmod +x "$T/bin/"*
|
||||
export PATH="$T/bin:$PATH" CALLS="$T/calls" ACTIVE="$T/active"
|
||||
export PKGCACHE_STATE_DIR="$T/state" PKGCACHE_CHECK_PATHS="$T/disk1 $T/disk2"
|
||||
export PKGCACHE_PROBE_URL=http://probe PKGCACHE_ORIGIN_URL=http://origin
|
||||
export PKGCACHE_SERVICES="nginx docker-registry" # explicit: never rely on the script default in a test
|
||||
export PKGCACHE_NGINX_TEST=true FAKE_RO=none FAKE_PROBE=ok FAKE_ORIGIN=ok
|
||||
run() { "$S" >"$T/out" 2>&1; }
|
||||
reset() { rm -rf "$T/state" "$T/calls"; touch "$T/calls" "$ACTIVE"; }
|
||||
stopped() { [ ! -e "$ACTIVE" ] && grep -q '^stop nginx' "$CALLS"; }
|
||||
|
||||
reset; run && ! stopped && ok "healthy: nothing stopped" || bad "healthy"
|
||||
[ -z "$(ls -A "$T/disk1")" ] && ok "test file removed" || bad "test file left behind"
|
||||
|
||||
reset; FAKE_RO="$T/disk2" run && bad "ro disk exit" || { stopped && grep -q '^stop docker-registry' "$CALLS" && ok "ro mount: nginx+registry stopped" || bad "ro mount not stopped"; }
|
||||
reset; chmod 555 "$T/disk1"; if [ "$(id -u)" != 0 ]; then run && bad "unwritable exit" || { stopped && ok "write failure: stopped" || bad "write failure not stopped"; }; fi; chmod 755 "$T/disk1"
|
||||
reset; FAKE_PROBE=empty run && bad "empty reply exit" || { stopped && ok "empty reply (curl 52), origin up: stopped" || bad "empty reply not stopped"; }
|
||||
reset; FAKE_PROBE=http500 run && bad "500 exit" || { stopped && ok "HTTP 500, origin up: stopped" || bad "500 not stopped"; }
|
||||
reset; FAKE_PROBE=empty FAKE_ORIGIN=down run && ! stopped && ok "origin down too: warn only, not stopped" || bad "upstream outage must not stop"
|
||||
|
||||
# recovery: needs 3 consecutive good runs, only if WE stopped it
|
||||
reset; FAKE_PROBE=empty run || true
|
||||
export FAKE_PROBE=ok
|
||||
run; stopped && ok "recovery 1/3: still stopped" || bad "recovery 1"
|
||||
FAKE_RO="$T/disk1" run || true; run; stopped && ok "failure resets the streak (1/3 again)" || bad "streak reset"
|
||||
run; stopped && ok "recovery 2/3: still stopped" || bad "recovery 2"
|
||||
run && [ -e "$ACTIVE" ] && [ ! -e "$T/state/stopped" ] && grep -q '^start nginx' "$CALLS" && ok "3rd healthy run: services started, marker cleared" || bad "recovery 3"
|
||||
# nginx -t failure blocks the restart
|
||||
reset; FAKE_PROBE=empty run || true; export FAKE_PROBE=ok
|
||||
run; run; PKGCACHE_NGINX_TEST=false run && bad "nginx -t fail exit" || { [ ! -e "$ACTIVE" ] && ok "bad nginx config: stays stopped" || bad "started despite bad config"; }
|
||||
# restart whose fetch check fails is stopped again
|
||||
reset; FAKE_PROBE=empty run || true; run; run; FAKE_PROBE=empty run && bad "restart-fail exit" || { [ ! -e "$ACTIVE" ] && [ -e "$T/state/stopped" ] && ok "restart with failing fetch: stopped again" || bad "restart-fail"; }
|
||||
# never start something an admin stopped
|
||||
reset; rm -f "$ACTIVE"; run && ! grep -q '^start' "$CALLS" && ok "admin-stopped nginx is left alone" || bad "admin stop"
|
||||
# empty services list must mean "manage nothing", never the real defaults
|
||||
reset; PKGCACHE_SERVICES="" FAKE_PROBE=empty run || true
|
||||
! grep -q '^stop' "$CALLS" && ok "empty PKGCACHE_SERVICES: nothing stopped" || bad "empty services list stopped something"
|
||||
# something else restarts nginx while our marker exists: re-checked, stopped again if still bad
|
||||
reset; FAKE_PROBE=empty run || true; touch "$ACTIVE"
|
||||
FAKE_RO="$T/disk1" run || true
|
||||
[ ! -e "$ACTIVE" ] && ok "nginx restarted by someone else while unhealthy: stopped again" || bad "fight: left running"
|
||||
reset; FAKE_PROBE=empty run || true; touch "$ACTIVE"
|
||||
run && [ -e "$ACTIVE" ] && [ ! -e "$T/state/stopped" ] && ok "nginx restarted by someone else and healthy: left running, marker cleared" || bad "fight: healthy case"
|
||||
# REAL read-only bind mount
|
||||
mkdir -p "$T/real" "$T/robind"
|
||||
if sudo -n mount --bind "$T/real" "$T/robind" 2>/dev/null && sudo -n mount -o remount,bind,ro "$T/robind"; then
|
||||
rm "$T/bin/findmnt"; reset; PKGCACHE_CHECK_PATHS="$T/robind" run && bad "real ro exit" || { stopped && ok "REAL read-only bind mount: stopped" || bad "real ro"; }
|
||||
sudo -n umount "$T/robind"
|
||||
else echo "skip - cannot bind-mount here, skipping the real ro bind mount case"; fi
|
||||
exit $fail
|
||||
Reference in new issue
Block a user