interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / ops / onchange-glob.sh

75 lines · 3950 bytes Raw Blame History
1#!/usr/bin/env bash
2# infra/ops/onchange-glob.sh: aien-onchange over directories named by shell patterns.
3# aien-onchange takes literal directories and systemd does not expand globs, so this expands each
4# PATTERN once (bash; globstar on, so 'x/**/' is x and every directory below it), keeps the
5# directories that exist and this user can read (inotify cannot watch the others), and execs
6# aien-onchange with them. The command is wrapped in `onchange-glob.sh --check`: after each run
7# the patterns are expanded again, and when the set of directories changed (a repo added, a ref
8# dir removed) the wrapper ends this aien-onchange, so systemd (Restart=always) starts it again
9# with the new set. No poll: the re-check runs only when aien-onchange runs the command (after a
10# change event or MAX_GAP).
11# aien-onchange runs the command once at start, so a restart would bypass MIN_GAP; the wrapper keeps
12# the end time of the last run in <list>.last ($XDG_RUNTIME_DIR, survives unit restarts) and holds a
13# run back until MIN_GAP has passed since then (the forgejo-backup unit stops Forgejo on every run).
14#
15# Usage: onchange-glob.sh MIN_GAP MAX_GAP PATTERN... -- CMD [ARGS...]
16# onchange-glob.sh --dry-run MIN_GAP MAX_GAP PATTERN... -- CMD [ARGS...] print, do not exec
17# Env: AIEN_ONCHANGE (default ~/.local/bin/aien-onchange)
18# Host tools: bash globbing, aien-onchange (inotifywait). Omega replacement: aien-onchange's successor.
19set -u
20shopt -s nullglob globstar
21
22expand() { # patterns... -> sorted unique readable dirs, one per line
23 local p d
24 for p in "$@"; do
25 for d in $p; do [ -d "$d" ] && [ -r "$d" ] && [ -x "$d" ] && printf '%s\n' "${d%/}"; done
26 done | sort -u
27}
28
29if [ "${1:-}" = --check ]; then
30 # --check LISTFILE MIN_GAP PATTERN... -- CMD...: wait out MIN_GAP since the last run, run CMD,
31 # then compare the expansion with LISTFILE.
32 list=$2; min=$3; shift 3; pats=()
33 while [ $# -gt 0 ] && [ "$1" != -- ]; do pats+=("$1"); shift; done
34 shift
35 last=$(cat "$list.last" 2>/dev/null) || last=
36 if [[ $last =~ ^[0-9]+$ ]]; then
37 gap=$(( last + min - $(date +%s) ))
38 [ "$gap" -gt "$min" ] && gap=$min # clock stepped back: never wait longer than MIN_GAP
39 [ "$gap" -gt 0 ] && sleep "$gap"
40 fi
41 "$@"; rc=$?
42 date +%s >"$list.last"
43 if ! expand "${pats[@]}" | cmp -s - "$list"; then
44 echo "onchange-glob: watched directory set changed; restarting the watch" >&2
45 parent=$(ps -o args= -p "$PPID" 2>/dev/null)
46 case $parent in *aien-onchange*) kill -TERM "$PPID" ;; esac
47 fi
48 exit "$rc"
49fi
50
51dry=0; [ "${1:-}" = --dry-run ] && { dry=1; shift; }
52min=${1:?usage}; max=${2:?usage}; shift 2
53pats=()
54while [ $# -gt 0 ] && [ "$1" != -- ]; do pats+=("$1"); shift; done
55[ "${1:-}" = -- ] && shift
56[ ${#pats[@]} -gt 0 ] && [ $# -gt 0 ] || { echo "usage: onchange-glob.sh [--dry-run] MIN MAX PATTERN... -- CMD..." >&2; exit 2; }
57
58mapfile -t dirs < <(expand "${pats[@]}")
59[ ${#dirs[@]} -gt 0 ] || { echo "onchange-glob: no directory matches ${pats[*]}" >&2; exit 2; }
60state=${XDG_RUNTIME_DIR:-/tmp}/onchange-glob
61list=$state/$(printf '%s\n' "${pats[@]}" | sha256sum | cut -c1-16).dirs
62self=$(cd "$(dirname "$0")" && pwd)/$(basename "$0")
63echo "onchange-glob: watching ${#dirs[@]} directories (min ${min}s, max ${max}s)" >&2
64# --dry-run writes nothing: the state dir and the list are made only for a real run (a dry run used
65# to write the list, and a test run with the real patterns overwrote the list of the real unit)
66if [ "$dry" = 1 ]; then
67 printf ' %s\n' "${dirs[@]:0:8}"
68 [ ${#dirs[@]} -gt 8 ] && echo " ... $(( ${#dirs[@]} - 8 )) more"
69 echo "would exec: ${AIEN_ONCHANGE:-$HOME/.local/bin/aien-onchange} $min $max <${#dirs[@]} dirs> -- $self --check $list $min <patterns> -- $*"
70 exit 0
71fi
72mkdir -p -m 700 "$state"
73printf '%s\n' "${dirs[@]}" >"$list"
74exec "${AIEN_ONCHANGE:-$HOME/.local/bin/aien-onchange}" "$min" "$max" "${dirs[@]}" -- \
75 "$self" --check "$list" "$min" "${pats[@]}" -- "$@"