Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/runbooks/nos-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions docs/runbooks/swm-macos-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<string>/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
<key>HOME</key>
<string>@HOME@</string>
@NTFY_ENV@
</dict>
<key>RunAtLoad</key>
<true/>
Expand Down
105 changes: 102 additions & 3 deletions scripts/nos-runner/tests/test-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,31 @@ 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
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'
#!/usr/bin/env bash
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() {
Expand All @@ -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"
Expand Down Expand Up @@ -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 -- '<key>NTFY_TOPIC</key>' "${sup}"
check_not "no NTFY_TOPIC key in the updater plist without a topic" grep -qF -- '<key>NTFY_TOPIC</key>' "${upd}"

echo "-- ExitTimeOut follows config.sh"
expected="$(bash -c 'source "$1/config.sh" && echo "${NOS_SHUTDOWN_MAX_S}"' _ "${HERE}/..")"
Expand Down Expand Up @@ -273,6 +284,8 @@ NTFY_TOPIC=topic-abc.123 run_install
check "topic from env lands in the supervisor plist" grep -qF -- '<string>topic-abc.123</string>' "${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 -- '<string>topic-abc.123</string>' "${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
Expand Down Expand Up @@ -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}"
113 changes: 89 additions & 24 deletions scripts/nos-runner/update.sh
Original file line number Diff line number Diff line change
@@ -1,41 +1,106 @@
#!/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
ts="$(date '+%Y-%m-%dT%H:%M:%S%z')" || ts=unknown-time
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
11 changes: 7 additions & 4 deletions scripts/swm-macos-runner/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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_<NAME>_S=<secs> env-file lines go into the plist; other SWM_ lines are refused.
if [[ -f "${ENV_FILE}" ]]; then
knob_keys=" "
Expand All @@ -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}}"
Expand All @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<string>/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
<key>HOME</key>
<string>@HOME@</string>
@NTFY_ENV@
</dict>
<key>RunAtLoad</key>
<true/>
Expand Down
3 changes: 3 additions & 0 deletions scripts/swm-macos-runner/tests/test-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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='<string>30</string>' "${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 '<string>drill-topic_1.x</string>' "${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
Expand Down
Loading
Loading