mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 21:09:40 +01:00
`echo "$x" | grep -q PAT` under `set -o pipefail` can report "not found" for text that IS there: grep -q exits on its first match and closes the pipe, the writer takes SIGPIPE (141), and pipefail makes the pipeline status 141. An `if` then takes the else branch on a successful match. #587 found this in one file and noted it "only fires when the payload exceeds the ~4KB pipe buffer". That is not so, and the belief is why 82 copies in 22 scripts survived. Measured today on retire-site's real 1,158-byte plan output with has()'s exact pattern, 3,000 calls each: 'DRY RUN' line 3 of 20 echo|grep -q 2/3000 false misses 'apps.conf row' line 11 of 20 echo|grep -q 2/3000 false misses last phrase line 20 of 20 echo|grep -q 0/3000 'DRY RUN' here-string 0/3000 Rare per call, but verify makes hundreds of these calls, and the retire-site suite went red on those two exact phrases twice this week, passing every rerun. It is worse in a NEGATIVE check - hasnt() read a false miss as a PASS. And several copies are not tests: deploy-hetzner.sh exits 1 on a false miss, and the sshd drift watch would raise a false security alert. All 82 rewritten to `grep -q PAT <<<"$x"` (a here-string is not a pipeline, so there is no writer to signal) by a script that only touched the simple quoted-variable form and reported anything else. Checked: all 22 files pass `bash -n`, every file is a 1:1 line swap (82 lines), and the 20 line-continuation sites keep the here-string before the trailing backslash (the first draft of the rewrite got those wrong; caught in a dry-run diff before anything was written). #587's comment is corrected so it stops teaching the wrong threshold. Gate: scripts/test/no-grep-q-in-a-pipe.ts ratchets it repo-wide, including forms the rewrite skipped (e.g. "$(cmd)" | grep -q), and self-tests that its detector still fires. Proven by restoring the original deploy-hetzner.sh line. Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
92 lines
4.1 KiB
Bash
Executable File
92 lines
4.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Shared helpers for the Hetzner self-host tooling. Source, don't execute.
|
|
set -euo pipefail
|
|
|
|
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
. "$HERE/_box-env.sh" # SSOT: HETZNER_IP, BOX_ROOT, BOX_UBUNTU
|
|
BOX="$BOX_UBUNTU"
|
|
# Overridable so a caller can judge a PRISTINE register instead of the working
|
|
# tree. On the workstation ~15 agent sessions share these checkouts, so the
|
|
# tree is a scratchpad: on 2026-08-28 the daily register check read apps.conf
|
|
# mid-edit (substrata listed twice) and paged about a duplicate port that had
|
|
# never been committed. CI still judges the working tree, which is correct
|
|
# there — in CI the working tree IS the commit under review.
|
|
MANIFEST="${MANIFEST:-$HERE/apps.conf}"
|
|
|
|
# default_branch [repo_dir] — the remote's default branch name, resolved not guessed.
|
|
#
|
|
# 28 repos here use `main`, 3 use `master` (aoz-begleitung, dotfiles,
|
|
# sbb-lost-found), and 2 have no origin/HEAD set at all. Anything that hardcodes
|
|
# "origin/main" silently does the wrong thing on a fifth of the fleet — a gate
|
|
# that cannot find its base branch either blocks everything or checks nothing.
|
|
#
|
|
# Order: the remote's own answer, then whichever of main/master exists, then
|
|
# fail. Never a hardcoded guess.
|
|
default_branch() {
|
|
local repo="${1:-.}" b
|
|
b=$(git -C "$repo" symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null) && { echo "${b#origin/}"; return 0; }
|
|
for b in main master; do
|
|
git -C "$repo" rev-parse --verify --quiet "refs/remotes/origin/$b" >/dev/null 2>&1 && { echo "$b"; return 0; }
|
|
done
|
|
# origin/HEAD unset and neither name present. Fix with:
|
|
# git remote set-head origin -a
|
|
return 1
|
|
}
|
|
|
|
|
|
# app_lookup <name> — sets NAME PORT DOMAINS REPO APP_DIR DB or exits 1
|
|
app_lookup() {
|
|
local line
|
|
line=$(grep -v '^#' "$MANIFEST" | grep "^$1|" || true)
|
|
[ -z "$line" ] && { echo "ERROR: '$1' not in $MANIFEST" >&2; return 1; }
|
|
# Extra fields must be named, or bash's last variable swallows the remainder
|
|
# and DB silently becomes "db|owner|kind|...". Missing trailing fields read as
|
|
# empty, so a 6-field line still parses exactly as before.
|
|
IFS='|' read -r NAME PORT DOMAINS REPO APP_DIR DB OWNER KIND STATUS PLAN PRICE SINCE <<<"$line"
|
|
}
|
|
|
|
app_names() { grep -v '^#' "$MANIFEST" | cut -d'|' -f1; }
|
|
|
|
# next_free_port <candidate> <newline-separated taken ports> — the first port at
|
|
# or above <candidate> that nothing holds.
|
|
#
|
|
# The register is NOT the only thing listening on this box, and it never claimed
|
|
# to be: the four handcrafted services on 4001-4004 are excluded by design, and
|
|
# annushka's enquiry API sits on 4030 behind a hand-written vhost precisely so
|
|
# sync-infra will not touch it. Allocating from `max(registered)+1` alone was
|
|
# therefore walking toward 4030 — four sites away as of 2026-09-10, and three
|
|
# rows were added that day. A port collision does not fail loudly at allocation;
|
|
# it fails when the new unit starts, binds nothing, and the OTHER app is the one
|
|
# that looks broken.
|
|
#
|
|
# Pure so it can be tested without the box: the caller supplies what is taken.
|
|
next_free_port() {
|
|
local p="$1" taken="${2:-}"
|
|
while grep -qx "$p" <<<"$taken"; do
|
|
p=$((p + 1))
|
|
done
|
|
printf '%s' "$p"
|
|
}
|
|
|
|
# listening_ports — every TCP port with a listener on the box, one per line.
|
|
# Empty output means "could not ask", which the caller MUST announce rather than
|
|
# treat as "nothing is listening"; those two look identical and only one is safe.
|
|
listening_ports() {
|
|
box "ss -ltnH 2>/dev/null | awk '{print \$4}'" 2>/dev/null \
|
|
| sed 's/.*://' | grep -E '^[0-9]+$' | sort -un
|
|
}
|
|
|
|
# -n: don't consume stdin (box() is used inside while-read loops)
|
|
# -i: the deploy key when it is readable. On the box itself these scripts run
|
|
# as ubuntu from inside loki-app (register-cd), and ubuntu's only private
|
|
# key is the CI deploy key — whose public half is what authorizes CI. Without
|
|
# naming it, ssh offers nothing and the box refuses itself ("Permission denied
|
|
# (publickey)"), which is how sync-infra failed on velokiosk-sep10.
|
|
box() {
|
|
if [ -r "${DEPLOY_KEY_PATH:-}" ]; then
|
|
ssh -n -o BatchMode=yes -i "$DEPLOY_KEY_PATH" "$BOX" "$@"
|
|
else
|
|
ssh -n -o BatchMode=yes "$BOX" "$@"
|
|
fi
|
|
}
|