diff --git a/docs/runbooks/nos-runner.md b/docs/runbooks/nos-runner.md
index 8f00d4e..7249c85 100644
--- a/docs/runbooks/nos-runner.md
+++ b/docs/runbooks/nos-runner.md
@@ -106,6 +106,14 @@ The updater LaunchAgent runs every 600 s. It pulls the clone and runs `install.s
To end it sooner, set `NOS_RUNNER` back to `mimolette` by hand.
- **Updater plist change.** A job cannot reload itself. The updater logs
"updater plist changed; run install.sh by hand" and sends one alert. Run `install.sh` by hand (section 4, step 3).
+- **Failed updates alert.** A pass fails when the git fetch or fast-forward fails, the revision cannot be read, or `install.sh` fails.
+ The updater counts consecutive failed passes in `~/.local/state/nos-runner/update-fails`.
+ At 6 (one hour) it sends one `NOS runner: the updater has failed 6 passes in a row. Last failure: ...` alert and writes `update-alerted`; later failures stay quiet.
+ The first good pass after that sends `NOS runner: the updater works again.` and clears both files.
+ `NOS_UPDATE_ALERT_AFTER` overrides the threshold, but `install.sh` does not carry it into the plist: a hand edit of the plist lasts only until the next `install.sh`. A failed ntfy delivery is retried on the next failed or good pass.
+ The updater needs `NTFY_TOPIC` in its plist, which `install.sh` now writes from the env file.
+ The updater cannot reload its own plist, so one hand run of `install.sh` (section 4, step 3) is needed to turn the alert on.
+ Until then the updater logs `NTFY_TOPIC is unset` and sends nothing.
- A pf rule change needs `install-pf.sh` again with sudo. The updater never touches root files.
## 6. Hold, release and full stop
diff --git a/docs/runbooks/swm-macos-runner.md b/docs/runbooks/swm-macos-runner.md
index 1d87dde..47d8a1b 100644
--- a/docs/runbooks/swm-macos-runner.md
+++ b/docs/runbooks/swm-macos-runner.md
@@ -89,6 +89,15 @@ The updater job runs `update.sh` at load and every 600 s. It always exits 0.
So an update installs within one cycle: at most `SWM_JOB_MAX_S` + 600 s after the merge, and sooner when a job ends.
- If `install.sh` fails, the version stays unmarked and the next run tries again.
- The updater never reloads itself. When a change touches its own plist, run `install.sh` by hand.
+- **Failed updates alert.** A pass fails when the git fetch or fast-forward fails, the revision cannot be read, or `install.sh` fails.
+ A pass that only waits for a running job is not a failure and does not change the count.
+ The updater counts consecutive failed passes in `~/.local/state/swm-macos-runner/update-fails`.
+ At 6 (one hour) it sends one `macOS runner: the updater has failed 6 passes in a row. Last failure: ...` alert and writes `update-alerted`; later failures stay quiet.
+ The first good pass after that sends `macOS runner: the updater works again.` and clears both files.
+ `SWM_UPDATE_ALERT_AFTER` overrides the threshold, but `install.sh` does not carry it into the plist: a hand edit of the plist lasts only until the next `install.sh`. A failed ntfy delivery is retried on the next failed or good pass.
+ The updater needs `NTFY_TOPIC` in its plist, which `install.sh` now writes from the env file (the topic only, not the supervisor knobs).
+ The updater cannot reload its own plist, so one hand run of `install.sh` is needed to turn the alert on.
+ Until then the updater logs `NTFY_TOPIC is unset` and sends nothing.
- The log is `~/Library/Logs/swm-macos-runner-update.log`.
Stop the updater (the supervisor keeps running):
diff --git a/scripts/nos-runner/launchd/com.nightowlstudio.nos-runner-update.plist b/scripts/nos-runner/launchd/com.nightowlstudio.nos-runner-update.plist
index 51aa477..19d670d 100644
--- a/scripts/nos-runner/launchd/com.nightowlstudio.nos-runner-update.plist
+++ b/scripts/nos-runner/launchd/com.nightowlstudio.nos-runner-update.plist
@@ -15,6 +15,7 @@
/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
HOME
@HOME@
+@NTFY_ENV@
RunAtLoad
diff --git a/scripts/nos-runner/tests/test-install.sh b/scripts/nos-runner/tests/test-install.sh
index 310ca20..50678a0 100755
--- a/scripts/nos-runner/tests/test-install.sh
+++ b/scripts/nos-runner/tests/test-install.sh
@@ -77,6 +77,7 @@ cat >"${WORK}/bin/curl" <<'STUB'
#!/usr/bin/env bash
echo "$*" >>"${STUB_LOG}/curl"
cat >>"${STUB_LOG}/curl.stdin"
+if [[ -n "${STUB_CURL_FAIL:-}" ]]; then exit 22; fi
STUB
cat >"${WORK}/bin/git" <<'STUB'
#!/usr/bin/env bash
@@ -84,7 +85,7 @@ echo "$*" >>"${STUB_LOG}/git"
case " $* " in
*" fetch "*) exit "${STUB_PULL_RC:-0}" ;;
*" merge "*) exit "${STUB_MERGE_RC:-0}" ;;
-*" log "*) echo "${STUB_REV:-rev1}" ;;
+*" log "*) echo "${STUB_REV-rev1}" ;;
esac
STUB
cat >"${WORK}/bin/bash-install" <<'STUB'
@@ -92,6 +93,15 @@ cat >"${WORK}/bin/bash-install" <<'STUB'
echo "$*" >>"${STUB_LOG}/install-run"
exit "${STUB_INSTALL_RC:-0}"
STUB
+# rm stub for update.sh: removes only files under this test's WORK dir.
+cat >"${WORK}/bin/rm-stub" <<'STUB'
+#!/usr/bin/env bash
+echo "$*" >>"${STUB_LOG}/rm"
+for a in "$@"; do
+ case "${a}" in -*) ;; "${STUB_WORK}"/*) ;; *) echo "stub rm: outside WORK: ${a}" >&2; exit 9 ;; esac
+done
+exec /bin/rm "$@"
+STUB
chmod +x "${WORK}"/bin/*
new_home() {
@@ -104,8 +114,8 @@ run_install() {
SLEEP_BIN="${WORK}/bin/sleep" bash "${INSTALL}" >"${WORK}/out" 2>"${WORK}/err"
}
run_update() {
- HOME="${WORK}/home" STUB_LOG="${WORK}/log" GIT_BIN="${WORK}/bin/git" BASH_BIN="${WORK}/bin/bash-install" \
- NOS_CLONE="${WORK}/clone" bash "${UPDATE}" >"${WORK}/out" 2>"${WORK}/err"
+ HOME="${WORK}/home" STUB_LOG="${WORK}/log" STUB_WORK="${WORK}" GIT_BIN="${WORK}/bin/git" BASH_BIN="${WORK}/bin/bash-install" \
+ RM_BIN="${WORK}/bin/rm-stub" CURL_BIN="${WORK}/bin/curl" NOS_CLONE="${WORK}/clone" bash "${UPDATE}" >"${WORK}/out" 2>"${WORK}/err"
}
lib="${WORK}/home/.local/libexec/nos-runner"
agents="${WORK}/home/Library/LaunchAgents"
@@ -147,6 +157,7 @@ check "bootstrap updater" grep -qF -- "bootstrap gui/501 ${upd}" "${LC}"
check_not "first install does not HUP" grep -qF -- "kill SIGHUP" "${LC}"
check "warns when NTFY_TOPIC is unset" grep -qF -- 'no NTFY_TOPIC' "${WORK}/err"
check_not "no NTFY_TOPIC key without a topic" grep -qF -- 'NTFY_TOPIC' "${sup}"
+check_not "no NTFY_TOPIC key in the updater plist without a topic" grep -qF -- 'NTFY_TOPIC' "${upd}"
echo "-- ExitTimeOut follows config.sh"
expected="$(bash -c 'source "$1/config.sh" && echo "${NOS_SHUTDOWN_MAX_S}"' _ "${HERE}/..")"
@@ -273,6 +284,8 @@ NTFY_TOPIC=topic-abc.123 run_install
check "topic from env lands in the supervisor plist" grep -qF -- 'topic-abc.123' "${sup}"
check "topic from env is saved to the env file" grep -qF -- 'NTFY_TOPIC=topic-abc.123' "${WORK}/home/.config/nos-runner/env"
check "topic plist lints" "${LINT[@]}" "${sup}"
+check "topic from env lands in the updater plist" grep -qF -- 'topic-abc.123' "${upd}"
+check "updater topic plist lints" "${LINT[@]}" "${upd}"
check_not "topic never printed" grep -qF -- 'topic-abc.123' "${WORK}/out"
rm -f "${WORK}/log/launchctl"
run_install
@@ -450,4 +463,90 @@ STUB_INSTALL_RC=1 STUB_REV=rev4 run_update || rc=$?
check "update_install_failure_exits_0" test "${rc}" -eq 0
check "update_install_failure_keeps_old_rev" grep -qF -- rev2 "${WORK}/home/.local/state/nos-runner/installed-rev"
+echo "-- update.sh failure alerts"
+ns="${WORK}/home/.local/state/nos-runner"
+alerts() { if [[ -e "${WORK}/log/curl" ]]; then grep -c . "${WORK}/log/curl"; else echo 0; fi; }
+new_home
+mkdir -p "${ns}"
+echo rev1 >"${ns}/installed-rev"
+export NTFY_TOPIC=topic-abc
+for _ in 1 2 3 4 5; do STUB_PULL_RC=1 run_update; done
+check "5 failures: count is 5" test "$(cat "${ns}/update-fails")" = 5
+check "5 failures: no alert" test "$(alerts)" -eq 0
+STUB_PULL_RC=1 run_update
+check "6th failure: one alert" test "$(alerts)" -eq 1
+check "alert names the runner" grep -qF -- 'NOS runner: the updater has failed 6 passes' "${WORK}/log/curl"
+check "alert gives the last reason" grep -qF -- 'Last failure: git update failed' "${WORK}/log/curl"
+check "alert marker written" test -e "${ns}/update-alerted"
+check "alert topic via stdin" grep -q 'topic-abc' "${WORK}/log/curl.stdin"
+check_not "alert topic not in argv" grep -q 'topic-abc' "${WORK}/log/curl"
+STUB_PULL_RC=1 run_update
+check "7th failure: still one alert" test "$(alerts)" -eq 1
+STUB_REV=rev1 run_update
+check "success after alert: recovery alert (two in all)" test "$(alerts)" -eq 2
+check "recovery text" grep -qF -- 'NOS runner: the updater works again' "${WORK}/log/curl"
+check_not "count cleared" test -e "${ns}/update-fails"
+check_not "marker cleared" test -e "${ns}/update-alerted"
+STUB_REV=rev1 run_update
+check "next success is quiet" test "$(alerts)" -eq 2
+for _ in 1 2 3 4 5 6; do STUB_PULL_RC=1 run_update; done
+check "a new run of failures alerts again" test "$(alerts)" -eq 3
+
+new_home
+mkdir -p "${ns}"
+echo rev1 >"${ns}/installed-rev"
+for _ in 1 2 3; do STUB_PULL_RC=1 run_update; done
+STUB_REV=rev1 run_update
+check "success without an alert: no alert" test "$(alerts)" -eq 0
+check_not "success without an alert: count cleared" test -e "${ns}/update-fails"
+
+new_home
+mkdir -p "${ns}"
+echo rev1 >"${ns}/installed-rev"
+for _ in 1 2 3 4 5 6; do STUB_INSTALL_RC=1 STUB_REV=rev5 run_update; done
+check "install failures count: one alert" test "$(alerts)" -eq 1
+check "install failure reason in the alert" grep -qF -- 'Last failure: install.sh failed' "${WORK}/log/curl"
+
+new_home
+mkdir -p "${ns}"
+echo rev1 >"${ns}/installed-rev"
+for _ in 1 2 3 4 5 6; do STUB_REV='' run_update; done
+check "unreadable revision counts: one alert" test "$(alerts)" -eq 1
+check "revision reason in the alert" grep -qF -- 'cannot read the scripts/nos-runner revision' "${WORK}/log/curl"
+
+new_home
+mkdir -p "${ns}"
+echo rev1 >"${ns}/installed-rev"
+for _ in 1 2; do STUB_PULL_RC=1 NOS_UPDATE_ALERT_AFTER=3 run_update; done
+check "threshold 3: no alert after 2" test "$(alerts)" -eq 0
+STUB_PULL_RC=1 NOS_UPDATE_ALERT_AFTER=3 run_update
+check "threshold 3: alert on the 3rd" test "$(alerts)" -eq 1
+
+new_home
+mkdir -p "${ns}"
+echo rev1 >"${ns}/installed-rev"
+for _ in 1 2 3 4 5 6; do STUB_PULL_RC=1 NOS_UPDATE_ALERT_AFTER=bogus run_update; done
+check "bad threshold falls back to 6" test "$(alerts)" -eq 1
+check "bad threshold is logged" grep -qF -- 'not a positive number' "${WORK}/err"
+
+new_home
+mkdir -p "${ns}"
+echo rev1 >"${ns}/installed-rev"
+for _ in 1 2 3; do STUB_PULL_RC=1 NOS_UPDATE_ALERT_AFTER=2 STUB_CURL_FAIL=1 run_update; done
+check "failed delivery is logged" grep -qF -- 'ntfy delivery failed' "${WORK}/err"
+check_not "no marker after a failed delivery" test -e "${ns}/update-alerted"
+STUB_PULL_RC=1 NOS_UPDATE_ALERT_AFTER=2 run_update
+check "the next failure sends it" test -e "${ns}/update-alerted"
+
+unset NTFY_TOPIC
+new_home
+mkdir -p "${ns}"
+echo rev1 >"${ns}/installed-rev"
+rc=0
+for _ in 1 2 3 4 5 6; do STUB_PULL_RC=1 run_update || rc=$?; done
+check "no topic: exit 0" test "${rc}" -eq 0
+check "no topic: no curl" test "$(alerts)" -eq 0
+check "no topic: warning logged" grep -qF -- 'NTFY_TOPIC is unset' "${WORK}/err"
+check_not "no topic: no marker" test -e "${ns}/update-alerted"
+
exit "${fail}"
diff --git a/scripts/nos-runner/update.sh b/scripts/nos-runner/update.sh
index 16e39d1..8f2b540 100755
--- a/scripts/nos-runner/update.sh
+++ b/scripts/nos-runner/update.sh
@@ -1,13 +1,21 @@
#!/bin/bash
# Updater LaunchAgent job (every 600 s): update the clone, run install.sh when scripts/nos-runner/ changed. Always exits 0.
+# After NOS_UPDATE_ALERT_AFTER (default 6) failed passes in a row it sends one ntfy alert, and one more when a pass succeeds again.
set -uo pipefail
unset CDPATH
GIT_BIN="${GIT_BIN:-git}"
BASH_BIN="${BASH_BIN:-/bin/bash}"
+RM_BIN="${RM_BIN:-rm}"
+CURL_BIN="${CURL_BIN:-curl}"
+NTFY_BASE="${NTFY_BASE:-https://ntfy.sh}"
CLONE="${NOS_CLONE:-${HOME}/.local/share/dev-env}"
STATE_DIR="${NOS_STATE_DIR:-${HOME}/.local/state/nos-runner}"
MARK="${STATE_DIR}/installed-rev"
+# Consecutive failed passes, and a marker that the failure alert went out (so later failures stay quiet).
+FAILS="${STATE_DIR}/update-fails"
+ALERTED="${STATE_DIR}/update-alerted"
+ALERT_AFTER="${NOS_UPDATE_ALERT_AFTER:-6}"
log() {
local ts
@@ -15,27 +23,84 @@ log() {
printf '%s update: %s\n' "${ts}" "$*" >&2
}
-# Not `git pull`: the swm updater runs in the same second, and two pulls
-# collide on FETCH_HEAD. Any failure retries.
-if ! "${GIT_BIN}" -C "${CLONE}" fetch --quiet --no-write-fetch-head origin >/dev/null 2>&1 ||
- ! "${GIT_BIN}" -C "${CLONE}" merge --quiet --ff-only origin/main >/dev/null 2>&1; then
- log "git update failed; will retry"
- exit 0
-fi
-
-# The last commit that touched scripts/nos-runner/ is the version to install; the mark file records what is installed.
-rev="$("${GIT_BIN}" -C "${CLONE}" log -1 --format=%H -- scripts/nos-runner 2>/dev/null)" || rev=""
-if [[ -z "${rev}" ]]; then
- log "cannot read the scripts/nos-runner revision"
- exit 0
-fi
-have="$(cat "${MARK}" 2>/dev/null)" || have=""
-if [[ "${rev}" == "${have}" ]]; then exit 0; fi
-
-log "scripts/nos-runner changed (${have:-none} -> ${rev}); running install.sh"
-if NOS_FROM_UPDATER=1 "${BASH_BIN}" "${CLONE}/scripts/nos-runner/install.sh"; then
- (umask 077 && mkdir -p "${STATE_DIR}" && printf '%s\n' "${rev}" >"${MARK}") || log "cannot write ${MARK}"
-else
- log "install.sh failed; will retry"
-fi
-exit 0
+# notify MSG: always returns 0; NOTIFY_OK=1 only when ntfy accepted MSG. Same contract as notify in supervisor.sh.
+notify() {
+ NOTIFY_OK=0
+ if [[ -z "${NTFY_TOPIC:-}" ]]; then
+ log "warning: NTFY_TOPIC is unset; alert not sent: $1"
+ return 0
+ fi
+ if [[ ! "${NTFY_TOPIC}" =~ ^[A-Za-z0-9_.-]+$ ]]; then
+ log "warning: NTFY_TOPIC has characters outside A-Za-z0-9_.-; alert not sent: $1"
+ return 0
+ fi
+ # The URL goes to curl on stdin so the topic never shows in ps.
+ if printf 'url = "%s/%s"\n' "${NTFY_BASE}" "${NTFY_TOPIC}" | "${CURL_BIN}" -fsS --max-time 20 -K - -d "$1" >/dev/null 2>&1; then
+ NOTIFY_OK=1
+ else
+ log "ntfy delivery failed"
+ fi
+ return 0
+}
+
+# pass_failed REASON: count the failed pass; alert once when the count reaches the threshold.
+pass_failed() {
+ local n alert_after="${ALERT_AFTER}"
+ log "$1; will retry"
+ n="$(cat "${FAILS}" 2>/dev/null)" || n=0
+ [[ "${n}" =~ ^[0-9]+$ ]] || n=0
+ n=$((n + 1))
+ (umask 077 && mkdir -p "${STATE_DIR}" && printf '%s\n' "${n}" >"${FAILS}") || log "cannot write ${FAILS}"
+ if [[ ! "${alert_after}" =~ ^[0-9]+$ ]] || [[ "${alert_after}" -lt 1 ]]; then
+ log "NOS_UPDATE_ALERT_AFTER is not a positive number; using 6"
+ alert_after=6
+ fi
+ if [[ "${n}" -ge "${alert_after}" && ! -e "${ALERTED}" ]]; then
+ notify "NOS runner: the updater has failed ${n} passes in a row. Last failure: $1"
+ if [[ "${NOTIFY_OK}" -eq 1 ]]; then (umask 077 && : >"${ALERTED}") || log "cannot write ${ALERTED}"; fi
+ fi
+}
+
+# pass_ok: a pass that ended up to date or installed. Clears the count; sends one recovery alert if a failure alert went out.
+pass_ok() {
+ "${RM_BIN}" -f "${FAILS}"
+ if [[ -e "${ALERTED}" ]]; then
+ notify "NOS runner: the updater works again."
+ if [[ "${NOTIFY_OK}" -eq 1 ]]; then "${RM_BIN}" -f "${ALERTED}"; fi
+ fi
+}
+
+main() {
+ # Not `git pull`: the swm updater runs in the same second, and two pulls
+ # collide on FETCH_HEAD. Any failure retries.
+ if ! "${GIT_BIN}" -C "${CLONE}" fetch --quiet --no-write-fetch-head origin >/dev/null 2>&1 ||
+ ! "${GIT_BIN}" -C "${CLONE}" merge --quiet --ff-only origin/main >/dev/null 2>&1; then
+ pass_failed "git update failed"
+ return 0
+ fi
+
+ # The last commit that touched scripts/nos-runner/ is the version to install; the mark file records what is installed.
+ local rev have
+ rev="$("${GIT_BIN}" -C "${CLONE}" log -1 --format=%H -- scripts/nos-runner 2>/dev/null)" || rev=""
+ if [[ -z "${rev}" ]]; then
+ pass_failed "cannot read the scripts/nos-runner revision"
+ return 0
+ fi
+ have="$(cat "${MARK}" 2>/dev/null)" || have=""
+ if [[ "${rev}" == "${have}" ]]; then
+ pass_ok
+ return 0
+ fi
+
+ log "scripts/nos-runner changed (${have:-none} -> ${rev}); running install.sh"
+ if NOS_FROM_UPDATER=1 "${BASH_BIN}" "${CLONE}/scripts/nos-runner/install.sh"; then
+ (umask 077 && mkdir -p "${STATE_DIR}" && printf '%s\n' "${rev}" >"${MARK}") || log "cannot write ${MARK}"
+ pass_ok
+ else
+ pass_failed "install.sh failed"
+ fi
+ return 0
+}
+
+# One line, so bash has parsed it all before install.sh can replace this file.
+main; exit 0
diff --git a/scripts/swm-macos-runner/install.sh b/scripts/swm-macos-runner/install.sh
index 02a4d5f..549b59a 100755
--- a/scripts/swm-macos-runner/install.sh
+++ b/scripts/swm-macos-runner/install.sh
@@ -55,6 +55,9 @@ else
echo "install: warning: no NTFY_TOPIC in ${ENV_FILE}; alerts will not be sent" >&2
fi
+# The updater plist gets the topic only (its alerts); the supervisor knobs below are not for it.
+upd_env="${ntfy_env}"
+
# SWM__S= env-file lines go into the plist; other SWM_ lines are refused.
if [[ -f "${ENV_FILE}" ]]; then
knob_keys=" "
@@ -73,13 +76,13 @@ x_libexec="$(xml_esc "${LIBEXEC}")"
x_home="$(xml_esc "${HOME}")"
x_tart="$(xml_esc "${tart_home}")"
-# render TEMPLATE OUT: fill the @TOKEN@ placeholders.
+# render TEMPLATE OUT [ENV]: fill the @TOKEN@ placeholders; ENV (default: the topic and knobs) replaces @NTFY_ENV@.
render() {
- local line
+ local line env="${3-${ntfy_env}}"
: >"$2"
while IFS= read -r line || [[ -n "${line}" ]]; do
if [[ "${line}" == "@NTFY_ENV@" ]]; then
- if [[ -n "${ntfy_env}" ]]; then printf '%s\n' "${ntfy_env}" >>"$2"; fi
+ if [[ -n "${env}" ]]; then printf '%s\n' "${env}" >>"$2"; fi
continue
fi
line="${line//@LIBEXEC@/${x_libexec}}"
@@ -94,7 +97,7 @@ trap '"${RM_BIN}" -rf "${rendered:?}"' EXIT
render "${HERE}/launchd/${LABEL}.plist" "${rendered}/${LABEL}.plist" || die "cannot render ${LABEL}.plist"
if grep -q '@[A-Z_]*@' "${rendered}/${LABEL}.plist"; then die "an unfilled placeholder is left in ${LABEL}.plist"; fi
-render "${HERE}/launchd/${UPD_LABEL}.plist" "${rendered}/${UPD_LABEL}.plist" || die "cannot render ${UPD_LABEL}.plist"
+render "${HERE}/launchd/${UPD_LABEL}.plist" "${rendered}/${UPD_LABEL}.plist" "${upd_env}" || die "cannot render ${UPD_LABEL}.plist"
if grep -q '@[A-Z_]*@' "${rendered}/${UPD_LABEL}.plist"; then die "an unfilled placeholder is left in ${UPD_LABEL}.plist"; fi
domain="gui/${uid}"
diff --git a/scripts/swm-macos-runner/launchd/com.smartwatermelon.swm-macos-runner-update.plist b/scripts/swm-macos-runner/launchd/com.smartwatermelon.swm-macos-runner-update.plist
index 7bf3ebb..37a63c0 100644
--- a/scripts/swm-macos-runner/launchd/com.smartwatermelon.swm-macos-runner-update.plist
+++ b/scripts/swm-macos-runner/launchd/com.smartwatermelon.swm-macos-runner-update.plist
@@ -15,6 +15,7 @@
/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
HOME
@HOME@
+@NTFY_ENV@
RunAtLoad
diff --git a/scripts/swm-macos-runner/tests/test-install.sh b/scripts/swm-macos-runner/tests/test-install.sh
index f9f93ba..27a2005 100755
--- a/scripts/swm-macos-runner/tests/test-install.sh
+++ b/scripts/swm-macos-runner/tests/test-install.sh
@@ -192,6 +192,8 @@ echo "== no topic"
ENV_BODY="" run_install
check "no topic still installs" test "${RC}" -eq 0
check "warns about no topic" grep -q 'no NTFY_TOPIC' <<<"${OUT}"
+check_not "no topic: no key in the updater plist" grep -q 'NTFY_TOPIC' "${H}/${U}"
+check_not "no topic: no stray placeholder in the updater plist" grep -q '@NTFY_ENV@' "${H}/${U}"
check_not "plist has no stray placeholder" grep -q '@NTFY_ENV@' "${H}/${P}"
echo "== bad topic"
@@ -208,6 +210,7 @@ check "knobs: SWM_CLONE_LIMIT_S in the plist" awk -v k=SWM_CLONE_LIMIT_S -v v='<
check "knobs: SWM_JOB_MAX_S in the plist" awk -v k=SWM_JOB_MAX_S -v v='30' "${PLIST_AWK}" "${H}/${P}"
check_not "knobs: no literal newline escape in the plist" grep -qF '\n' "${H}/${P}"
check "knobs: topic still in the plist" grep -q 'drill-topic_1.x' "${H}/${P}"
+check "updater plist gets the topic" grep -q 'drill-topic_1.x' "${H}/${U}"
check_not "knobs: not in the updater plist" grep -q 'SWM_CLONE_LIMIT_S' "${H}/${U}"
ENV_BODY=$'SWM_CLONE_LIMIT_S=1' run_install
check "knobs: knob alone (no topic) still installs" test "${RC}" -eq 0
diff --git a/scripts/swm-macos-runner/tests/test-update.sh b/scripts/swm-macos-runner/tests/test-update.sh
index 52440ca..1d3f012 100755
--- a/scripts/swm-macos-runner/tests/test-update.sh
+++ b/scripts/swm-macos-runner/tests/test-update.sh
@@ -62,6 +62,14 @@ for a in "$@"; do
done
exec /bin/rm "$@"
STUB
+# curl stub: logs the arguments and stdin (the ntfy URL); fails on STUB_CURL_FAIL.
+cat >"${WORK}/bin/curl" <<'STUB'
+#!/usr/bin/env bash
+echo "$*" >>"${STUB_LOG}/curl"
+cat >>"${STUB_LOG}/curl.stdin"
+if [[ -n "${STUB_CURL_FAIL:-}" ]]; then exit 22; fi
+exit 0
+STUB
chmod +x "${WORK}"/bin/*
# run_update [NAME=value ...]: fresh dirs unless KEEP=1; sets S (state), L (log), OUT, RC.
@@ -77,7 +85,7 @@ run_update() {
printf 'name=\nid=\nfails=0\nalert=\njob=0\n' >"${S}/slot"
fi
OUT="$(env HOME="${WORK}/home" SWM_STATE_DIR="${S}" SWM_CLONE="${WORK}/clone" STUB_LOG="${L}" STUB_REVS="${L}/revs" \
- GIT_BIN="${WORK}/bin/git" RM_BIN="${WORK}/bin/rm" STUB_WORK="${WORK}" BASH_BIN="${WORK}/bin/bashstub" "$@" bash "${UPDATE}" 2>&1)"
+ GIT_BIN="${WORK}/bin/git" RM_BIN="${WORK}/bin/rm" STUB_WORK="${WORK}" BASH_BIN="${WORK}/bin/bashstub" CURL_BIN="${WORK}/bin/curl" "$@" bash "${UPDATE}" 2>&1)"
RC=$?
}
# again [NAME=value ...]: run again in the same dirs.
@@ -204,4 +212,104 @@ check "exit 0" test "${RC}" -eq 0
check_not "no install" test -e "${L}/install"
check "mark unchanged" test "$(cat "${S}/installed")" = REV1
+# fails N [NAME=value ...]: N more failed passes (git fetch fails) in the same dirs.
+fails() {
+ local i count="$1"
+ shift
+ for ((i = 0; i < count; i += 1)); do again STUB_PULL_FAIL=1 "$@"; done
+}
+alerts() { if [[ -e "${L}/curl" ]]; then grep -c . "${L}/curl"; else echo 0; fi; }
+
+echo "== alert after repeated failures"
+run_update
+fails 5 NTFY_TOPIC=topic-abc
+check "5 failures: count is 5" test "$(cat "${S}/update-fails")" = 5
+check "5 failures: no alert" test "$(alerts)" -eq 0
+fails 1 NTFY_TOPIC=topic-abc
+check "6th failure: one alert" test "$(alerts)" -eq 1
+check "alert names the runner" grep -q 'macOS runner: the updater has failed 6 passes' "${L}/curl"
+check "alert gives the last reason" grep -q 'Last failure: git update failed' "${L}/curl"
+check "alert marker written" test -e "${S}/update-alerted"
+check "topic via stdin" grep -q 'topic-abc' "${L}/curl.stdin"
+check_not "topic not in argv" grep -q 'topic-abc' "${L}/curl"
+check "exit 0" test "${RC}" -eq 0
+fails 1 NTFY_TOPIC=topic-abc
+check "7th failure: still one alert" test "$(alerts)" -eq 1
+check "count keeps rising" test "$(cat "${S}/update-fails")" = 7
+again NTFY_TOPIC=topic-abc
+check "success after alert: recovery alert (two in all)" test "$(alerts)" -eq 2
+check "recovery text" grep -q 'macOS runner: the updater works again' "${L}/curl"
+check_not "count cleared" test -e "${S}/update-fails"
+check_not "marker cleared" test -e "${S}/update-alerted"
+again NTFY_TOPIC=topic-abc
+check "next success is quiet" test "$(alerts)" -eq 2
+fails 6 NTFY_TOPIC=topic-abc
+check "a new run of failures alerts again" test "$(alerts)" -eq 3
+
+echo "== success without an alert"
+run_update
+fails 3 NTFY_TOPIC=topic-abc
+again NTFY_TOPIC=topic-abc
+check "no alert and no recovery" test "$(alerts)" -eq 0
+check_not "count cleared" test -e "${S}/update-fails"
+
+echo "== a job-waiting pass is not a failure"
+run_update
+fails 2 NTFY_TOPIC=topic-abc
+newrev
+printf 'name=mimolette-macos-1\nid=7\nfails=0\nalert=\njob=1\n' >"${S}/slot"
+again NTFY_TOPIC=topic-abc
+check "waiting leaves the count" test "$(cat "${S}/update-fails")" = 2
+again NTFY_TOPIC=topic-abc
+check "waiting twice still leaves the count" test "$(cat "${S}/update-fails")" = 2
+check "waiting sends nothing" test "$(alerts)" -eq 0
+run_update
+newrev
+printf 'name=mimolette-macos-1\nid=7\nfails=0\nalert=\njob=1\n' >"${S}/slot"
+fails 5 NTFY_TOPIC=topic-abc
+newrev
+again NTFY_TOPIC=topic-abc STUB_PULL_FAIL=
+check "waiting between failures does not reach the threshold" test "$(alerts)" -eq 0
+
+echo "== install failure counts"
+run_update
+newrev
+for _ in 1 2 3 4 5 6; do again NTFY_TOPIC=topic-abc STUB_INSTALL_FAIL=1; done
+check "6 install failures: one alert" test "$(alerts)" -eq 1
+check "alert gives the install reason" grep -q 'Last failure: install.sh failed' "${L}/curl"
+
+echo "== unreadable revision counts"
+run_update
+: >"${L}/revs"
+for _ in 1 2 3 4 5 6; do again NTFY_TOPIC=topic-abc; done
+check "6 unreadable revisions: one alert" test "$(alerts)" -eq 1
+check "alert gives the revision reason" grep -q 'cannot read the scripts/swm-macos-runner revision' "${L}/curl"
+
+echo "== threshold from the environment"
+run_update
+fails 2 NTFY_TOPIC=topic-abc SWM_UPDATE_ALERT_AFTER=3
+check "below a threshold of 3: no alert" test "$(alerts)" -eq 0
+fails 1 NTFY_TOPIC=topic-abc SWM_UPDATE_ALERT_AFTER=3
+check "threshold of 3: alert on the 3rd" test "$(alerts)" -eq 1
+run_update
+fails 6 NTFY_TOPIC=topic-abc SWM_UPDATE_ALERT_AFTER=bogus
+check "a bad threshold falls back to 6" test "$(alerts)" -eq 1
+check "bad threshold is logged" grep -q 'not a positive number' <<<"${OUT}"
+
+echo "== no topic"
+run_update
+fails 6
+check "no curl without NTFY_TOPIC" test "$(alerts)" -eq 0
+check "warning logged" grep -q 'NTFY_TOPIC is unset' <<<"${OUT}"
+check "exit 0" test "${RC}" -eq 0
+check_not "no marker without a delivered alert" test -e "${S}/update-alerted"
+
+echo "== failed delivery is retried"
+run_update
+fails 6 NTFY_TOPIC=topic-abc STUB_CURL_FAIL=1
+check "delivery failure logged" grep -q 'ntfy delivery failed' <<<"${OUT}"
+check_not "no marker after a failed delivery" test -e "${S}/update-alerted"
+fails 1 NTFY_TOPIC=topic-abc
+check "the next failure sends it" test -e "${S}/update-alerted"
+
exit "${fail}"
diff --git a/scripts/swm-macos-runner/update.sh b/scripts/swm-macos-runner/update.sh
index a7f79dd..17448ed 100755
--- a/scripts/swm-macos-runner/update.sh
+++ b/scripts/swm-macos-runner/update.sh
@@ -1,17 +1,24 @@
#!/bin/bash
# Updater LaunchAgent job (every 600 s): update the clone and run install.sh when the runner code changed and no job runs. Always exits 0.
+# After SWM_UPDATE_ALERT_AFTER (default 6) failed passes in a row it sends one ntfy alert, and one more when a pass succeeds again.
set -uo pipefail
unset CDPATH
GIT_BIN="${GIT_BIN:-git}"
BASH_BIN="${BASH_BIN:-/bin/bash}"
RM_BIN="${RM_BIN:-rm}"
+CURL_BIN="${CURL_BIN:-curl}"
+NTFY_BASE="${NTFY_BASE:-https://ntfy.sh}"
CLONE="${SWM_CLONE:-${HOME}/.local/share/dev-env}"
STATE_DIR="${SWM_STATE_DIR:-${HOME}/.local/state/swm-macos-runner}"
MARK="${STATE_DIR}/installed"
SLOT_FILE="${STATE_DIR}/slot"
# Tells an idle supervisor to hold off starting a VM until this install is done.
PENDING="${STATE_DIR}/update-pending"
+# Consecutive failed passes, and a marker that the failure alert went out (so later failures stay quiet).
+FAILS="${STATE_DIR}/update-fails"
+ALERTED="${STATE_DIR}/update-alerted"
+ALERT_AFTER="${SWM_UPDATE_ALERT_AFTER:-6}"
log() {
local ts
@@ -19,12 +26,59 @@ log() {
printf '%s update: %s\n' "${ts}" "$*" >&2
}
+# notify MSG: always returns 0; NOTIFY_OK=1 only when ntfy accepted MSG. Same contract as notify in lib.sh, which this job does not source.
+notify() {
+ NOTIFY_OK=0
+ if [[ -z "${NTFY_TOPIC:-}" ]]; then
+ log "warning: NTFY_TOPIC is unset; alert not sent: $1"
+ return 0
+ fi
+ if [[ ! "${NTFY_TOPIC}" =~ ^[A-Za-z0-9_.-]+$ ]]; then
+ log "warning: NTFY_TOPIC has characters outside A-Za-z0-9_.-; alert not sent: $1"
+ return 0
+ fi
+ # The URL goes to curl on stdin so the topic never shows in ps.
+ if printf 'url = "%s/%s"\n' "${NTFY_BASE}" "${NTFY_TOPIC}" | "${CURL_BIN}" -fsS --max-time 20 -K - -d "$1" >/dev/null 2>&1; then
+ NOTIFY_OK=1
+ else
+ log "ntfy delivery failed"
+ fi
+ return 0
+}
+
+# pass_failed REASON: count the failed pass; alert once when the count reaches the threshold.
+pass_failed() {
+ local n alert_after="${ALERT_AFTER}"
+ log "$1; will retry"
+ n="$(cat "${FAILS}" 2>/dev/null)" || n=0
+ [[ "${n}" =~ ^[0-9]+$ ]] || n=0
+ n=$((n + 1))
+ (umask 077 && mkdir -p "${STATE_DIR}" && printf '%s\n' "${n}" >"${FAILS}") || log "cannot write ${FAILS}"
+ if [[ ! "${alert_after}" =~ ^[0-9]+$ ]] || [[ "${alert_after}" -lt 1 ]]; then
+ log "SWM_UPDATE_ALERT_AFTER is not a positive number; using 6"
+ alert_after=6
+ fi
+ if [[ "${n}" -ge "${alert_after}" && ! -e "${ALERTED}" ]]; then
+ notify "macOS runner: the updater has failed ${n} passes in a row. Last failure: $1"
+ if [[ "${NOTIFY_OK}" -eq 1 ]]; then (umask 077 && : >"${ALERTED}") || log "cannot write ${ALERTED}"; fi
+ fi
+}
+
+# pass_ok: a pass that ended up to date or installed. Clears the count; sends one recovery alert if a failure alert went out.
+pass_ok() {
+ "${RM_BIN}" -f "${FAILS}"
+ if [[ -e "${ALERTED}" ]]; then
+ notify "macOS runner: the updater works again."
+ if [[ "${NOTIFY_OK}" -eq 1 ]]; then "${RM_BIN}" -f "${ALERTED}"; fi
+ fi
+}
+
main() {
# Not `git pull`: the NOS updater runs in the same second, and two pulls
# collide on FETCH_HEAD. Any failure retries.
if ! "${GIT_BIN}" -C "${CLONE}" fetch --quiet --no-write-fetch-head origin >/dev/null 2>&1 ||
! "${GIT_BIN}" -C "${CLONE}" merge --quiet --ff-only origin/main >/dev/null 2>&1; then
- log "git update failed; will retry"
+ pass_failed "git update failed"
return 0
fi
@@ -32,12 +86,13 @@ main() {
local rev have job k v
rev="$("${GIT_BIN}" -C "${CLONE}" log -1 --format=%H -- scripts/swm-macos-runner scripts/nos-runner/github-app.sh 2>/dev/null)" || rev=""
if [[ -z "${rev}" ]]; then
- log "cannot read the scripts/swm-macos-runner revision"
+ pass_failed "cannot read the scripts/swm-macos-runner revision"
return 0
fi
have="$(cat "${MARK}" 2>/dev/null)" || have=""
if [[ "${rev}" == "${have}" ]]; then
"${RM_BIN}" -f "${PENDING}"
+ pass_ok
return 0
fi
@@ -49,6 +104,7 @@ main() {
done <"${SLOT_FILE}"
fi
if [[ "${job}" != 0 ]]; then
+ # Waiting is neither a failure nor a success: the failure count stays as it is.
log "new version ${rev} but a job is running; waiting"
(umask 077 && mkdir -p "${STATE_DIR}" && : >"${PENDING}") || log "cannot write ${PENDING}"
return 0
@@ -59,8 +115,9 @@ main() {
if SWM_FROM_UPDATER=1 "${BASH_BIN}" "${CLONE}/scripts/swm-macos-runner/install.sh"; then
(umask 077 && mkdir -p "${STATE_DIR}" && printf '%s\n' "${rev}" >"${MARK}") || log "cannot write ${MARK}"
"${RM_BIN}" -f "${PENDING}"
+ pass_ok
else
- log "install.sh failed; will retry"
+ pass_failed "install.sh failed"
fi
return 0
}