Safer self-update: staged next to build/ (two renames, not a copy from RAM), waits for the new revision on /api/health and rolls back to build.old if it doesn't come up, a repeated request is a no-op, maptool-recover puts build/ back after a power cut mid-swap; the dashboard dark under the page's night mode; /can.html: frames in a second column on wide screens, changed bytes highlighted (fading, bits in the tooltip), the node state column a fixed width
CI / check (push) Successful in 21s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-11-30 20:21:29 +01:00
1 parent 27313b3101
commit d58f7efc41
4 files changed
+111 -22

No files matched your search

+46 -15
View File
@@ -3,12 +3,18 @@
# button (POST /api/update) writes data/update/request.json with the commit, # button (POST /api/update) writes data/update/request.json with the commit,
# maptool-update.path runs this as root (the container writes data/ as root). # maptool-update.path runs this as root (the container writes data/ as root).
# Downloads that commit's tree from Gitea (the repo is public: no token), # Downloads that commit's tree from Gitea (the repo is public: no token),
# swaps it into build/ (the old one kept as build.old) and restarts maptool; # swaps it into build/ (the old one kept as build.old), restarts maptool and
# data/update/status.json tells the app how far it got. docs/deployment.adoc. # waits for it to answer with the new revision — if it doesn't, puts the old
# one back. data/update/status.json tells the app how far it got.
# A power cut at any point leaves either the old or the new tree in build/
# (maptool-recover.service puts one back if a cut fell between the two
# renames). docs/deployment.adoc.
set -euo pipefail set -euo pipefail
ROOT=${MAPTOOL_ROOT:-/srv/podman/maptool} ROOT=${MAPTOOL_ROOT:-/srv/podman/maptool}
REPO_URL=${REPO_URL:-https://git.priesol.net/randy/maptool} REPO_URL=${REPO_URL:-https://git.priesol.net/randy/maptool}
HEALTH_URL=${HEALTH_URL:-http://127.0.0.1:4567/api/health}
START_TIMEOUT_S=${START_TIMEOUT_S:-900} # the image build on the Pi takes a while
DIR=$ROOT/data/update DIR=$ROOT/data/update
req=$DIR/request.json req=$DIR/request.json
@@ -21,22 +27,47 @@ status() {
mv "$DIR/status.json.tmp" "$DIR/status.json" mv "$DIR/status.json.tmp" "$DIR/status.json"
} }
fail() { status failed "$1"; echo "update-maptool: $1" >&2; exit 1; } fail() { status failed "$1"; echo "update-maptool: $1" >&2; exit 1; }
revision() { curl -fsS --max-time 5 "$HEALTH_URL" 2>/dev/null | sed -n 's/.*"revision" *: *"\([^"]*\)".*/\1/p'; }
[ -n "$sha" ] || fail "no commit in the request" [ -n "$sha" ] || fail "no commit in the request"
status running "downloading ${sha:0:7}" short=${sha:0:7}
tmp=$(mktemp -d) # asked twice (two screens, a double click): nothing to do the second time
trap 'rm -rf "$tmp"' EXIT [ "$(cat "$ROOT/build/REVISION" 2>/dev/null)" = "$short" ] && { status done "already on $short"; exit 0; }
curl -fsS --retry 3 --retry-delay 5 --speed-limit 1024 --speed-time 60 -o "$tmp/src.tar.gz" "$REPO_URL/archive/$sha.tar.gz" \
|| fail "download of ${sha:0:7} failed"
mkdir "$tmp/build"
tar -xzf "$tmp/src.tar.gz" -C "$tmp/build" --strip-components=1 || fail "the archive didn't unpack"
[ -f "$tmp/build/app.rb" ] || fail "not a maptool tree"
echo "${sha:0:7}" > "$tmp/build/REVISION"
chown -R --reference="$ROOT" "$tmp/build"
status running "downloading $short"
# staged next to build/, on the same filesystem: the swap below is two
# renames, not a copy (/tmp is RAM on the Pi)
new=$ROOT/build.new
rm -rf "$new"
mkdir "$new"
trap 'rm -rf "$new" "$new.tar.gz"' EXIT
curl -fsS --retry 3 --retry-delay 5 --speed-limit 1024 --speed-time 60 -o "$new.tar.gz" "$REPO_URL/archive/$sha.tar.gz" \
|| fail "download of $short failed"
tar -xzf "$new.tar.gz" -C "$new" --strip-components=1 || fail "the archive didn't unpack"
[ -f "$new/app.rb" ] && [ -f "$new/docker-compose.yaml" ] || fail "not a maptool tree"
echo "$short" > "$new/REVISION"
chown -R --reference="$ROOT" "$new"
sync
old=$(cat "$ROOT/build/REVISION" 2>/dev/null || echo "?")
rm -rf "$ROOT/build.old" rm -rf "$ROOT/build.old"
mv "$ROOT/build" "$ROOT/build.old" mv "$ROOT/build" "$ROOT/build.old"
mv "$tmp/build" "$ROOT/build" mv "$new" "$ROOT/build"
status restarting "restarting with ${sha:0:7}" sync
trap - EXIT
status restarting "restarting with $short"
systemctl restart podman-compose@maptool.service systemctl restart podman-compose@maptool.service
status done "updated to ${sha:0:7}"
# up with the new revision, or back to the old one
deadline=$(( $(date +%s) + START_TIMEOUT_S ))
while [ "$(date +%s)" -lt "$deadline" ]; do
[ "$(revision)" = "$short" ] && { status done "updated to $short"; exit 0; }
sleep 10
done
status rolling-back "$short didn't come up in $((START_TIMEOUT_S / 60)) min, back to $old"
rm -rf "$ROOT/build.bad"
mv "$ROOT/build" "$ROOT/build.bad"
mv "$ROOT/build.old" "$ROOT/build"
sync
systemctl restart podman-compose@maptool.service
fail "$short didn't come up, back on $old (the new tree is in build.bad)"
+27 -4
View File
@@ -201,9 +201,21 @@ mustn't restart it mid-drive. Instead it checks Gitea itself (every
newer commit whose CI passed, a ⟳ button shows in the topbar; a click newer commit whose CI passed, a ⟳ button shows in the topbar; a click
writes `data/update/request.json`, `maptool-update.path` starts writes `data/update/request.json`, `maptool-update.path` starts
`bin/update-maptool.sh` (as root — the container writes `data/` as root), `bin/update-maptool.sh` (as root — the container writes `data/` as root),
which downloads that commit's archive from Gitea, swaps it into `build/` which downloads that commit's archive from Gitea, stages it as
(the previous one stays as `build.old/`), restarts maptool and reports in `build.new/` (next to `build/`, the same filesystem — `/tmp` is RAM on the
`data/update/status.json`; the page reloads on the new revision. Pi, a move from there would be a copy), swaps it in with two renames (the
previous one stays as `build.old/`), restarts maptool and waits up to
15 min for `/api/health` to answer with the new revision — if it doesn't
(the image didn't build, the app crashes), it moves the new tree to
`build.bad/`, puts the old one back and restarts again. It reports in
`data/update/status.json`; the page reloads on the new revision. Asked
twice for the same commit, the second run does nothing.
What survives what: a reload or a closed page — the update runs on the
host; a power cut while downloading — `build/` untouched, the button shows
again; a cut between the two renames (milliseconds) — `maptool-recover`
puts `build.new/` (complete by then) or `build.old/` back at boot; a cut
after them — the new tree is in place, maptool builds and starts at boot.
---- ----
# /etc/systemd/system/maptool-update.path # /etc/systemd/system/maptool-update.path
@@ -220,10 +232,21 @@ Description=Update maptool to the commit the app asked for
After=network-online.target After=network-online.target
[Service] [Service]
Type=oneshot Type=oneshot
TimeoutStartSec=30min
ExecStart=/srv/podman/maptool/build/bin/update-maptool.sh ExecStart=/srv/podman/maptool/build/bin/update-maptool.sh
# /etc/systemd/system/maptool-recover.service
[Unit]
Description=Put maptool's build/ back if a power cut fell into an update's swap
Before=podman-compose@maptool.service
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'cd /srv/podman/maptool && [ -d build ] || { [ -f build.new/app.rb ] && mv build.new build || mv build.old build; }'
[Install]
WantedBy=multi-user.target
---- ----
`systemctl enable --now maptool-update.path`. Back by hand: `systemctl enable --now maptool-update.path maptool-recover.service`. Back by hand:
`mv build build.bad && mv build.old build`, restart. `mv build build.bad && mv build.old build`, restart.
=== Library sync (both ways, OsmAnd-style) === Library sync (both ways, OsmAnd-style)
+4 -1
View File
@@ -62,7 +62,10 @@
/* Night: about a third of Day's luminance, warm greys, blue kept low; the /* Night: about a third of Day's luminance, warm greys, blue kept low; the
UI never brighter than the map (S-52: added panels "should never UI never brighter than the map (S-52: added panels "should never
generate more light than the chart display itself"). */ generate more light than the chart display itself"). */
:root[data-palette="night"] { /* also while the page's night mode is on (html.night inverts the UI and
inverts the dashboard back: it has to be the dark one then) */
:root[data-palette="night"],
html.night {
--ui-bg: #0c0c0b; --ui-bg: #0c0c0b;
--ui-fg: #8f8b80; --ui-fg: #8f8b80;
--ui-muted: #5f5c55; --ui-muted: #5f5c55;
+34 -2
View File
@@ -23,6 +23,14 @@
.stale td { opacity: 0.5; } .stale td { opacity: 0.5; }
input { padding: 6px 8px; font-size: 0.9rem; width: 220px; max-width: 100%; box-sizing: border-box; } input { padding: 6px 8px; font-size: 0.9rem; width: 220px; max-width: 100%; box-sizing: border-box; }
.empty { color: var(--muted); font-size: 0.85rem; } .empty { color: var(--muted); font-size: 0.85rem; }
/* side by side when there's room, the frames under the rest on a phone */
.cols { display: grid; gap: 0 32px; }
.cols > div { min-width: 0; }
@media (min-width: 1100px) { .cols { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }
/* a byte that just changed, fading out over CHANGED_FOR */
/* a fixed width: the column doesn't jump when a node goes "silent" */
#nodes th:nth-child(2), #nodes td:nth-child(2) { width: 9em; min-width: 9em; }
.chg { background: color-mix(in srgb, #ffb300 calc(var(--fresh) * 70%), transparent); border-radius: 3px; }
</style> </style>
</head> </head>
<body> <body>
@@ -31,6 +39,7 @@
<span id="status">loading…</span> <span id="status">loading…</span>
</header> </header>
<div class="cols"><div>
<h2>Interfaces</h2> <h2>Interfaces</h2>
<div class="scroll"><table id="interfaces"><thead><tr><th>Interface</th><th>State</th><th class="num">Frames</th><th>Last frame</th><th>Error</th></tr></thead><tbody></tbody></table></div> <div class="scroll"><table id="interfaces"><thead><tr><th>Interface</th><th>State</th><th class="num">Frames</th><th>Last frame</th><th>Error</th></tr></thead><tbody></tbody></table></div>
@@ -40,12 +49,34 @@
<h2>Signals</h2> <h2>Signals</h2>
<div class="scroll"><table id="signals"><thead><tr><th>Signal</th><th class="num">Value</th><th>Unit</th><th>Updated</th></tr></thead><tbody></tbody></table></div> <div class="scroll"><table id="signals"><thead><tr><th>Signal</th><th class="num">Value</th><th>Unit</th><th>Updated</th></tr></thead><tbody></tbody></table></div>
</div><div>
<h2>Frames <input id="filter" type="search" placeholder="Filter id, role, node…"></h2> <h2>Frames <input id="filter" type="search" placeholder="Filter id, role, node…"></h2>
<div class="scroll"><table id="frames"><thead><tr><th>Interface</th><th>COB-ID</th><th>Role</th><th>Node</th><th class="num">Count</th><th class="num">Rate</th><th>Data</th><th>Last</th></tr></thead><tbody></tbody></table></div> <div class="scroll"><table id="frames"><thead><tr><th>Interface</th><th>COB-ID</th><th>Role</th><th>Node</th><th class="num">Count</th><th class="num">Rate</th><th>Data</th><th>Last</th></tr></thead><tbody></tbody></table></div>
</div></div>
<script> <script>
// Polls GET /api/can (lib/maptool/can.rb's Store) once a second. // Polls GET /api/can (lib/maptool/can.rb's Store) once a second.
const STALE_AFTER = 10; // s without a frame: greyed out const STALE_AFTER = 10; // s without a frame: greyed out
const CHANGED_FOR = 3; // s a changed byte stays highlighted
const seen = new Map(); // "iface:cob" → { bytes: [..], changedAt: [..] }
// The frame's bytes, each one that changed in the last CHANGED_FOR s
// highlighted (fading), its tooltip saying which bits and from what.
function dataHtml(f, key, now) {
const bytes = (f.data.match(/../g) || []).map((b) => parseInt(b, 16));
const before = seen.get(key);
const changedAt = bytes.map((b, i) => (before && before.bytes[i] !== undefined && before.bytes[i] !== b ? now : before?.changedAt[i] ?? 0));
const from = bytes.map((b, i) => (before && before.bytes[i] !== b ? before.bytes[i] : before?.from?.[i]));
seen.set(key, { bytes, changedAt, from });
return bytes.map((b, i) => {
const hex = b.toString(16).toUpperCase().padStart(2, "0");
const age = now - changedAt[i];
if (!changedAt[i] || age > CHANGED_FOR || from[i] === undefined) return hex;
const bits = [...Array(8).keys()].filter((bit) => ((b ^ from[i]) >> bit) & 1);
const title = `byte ${i}: ${from[i].toString(2).padStart(8, "0")} → ${b.toString(2).padStart(8, "0")} (bit ${bits.join(", ")})`;
return `<span class="chg" style="--fresh:${(1 - age / CHANGED_FOR).toFixed(2)}" title="${title}">${hex}</span>`;
}).join(" ");
}
const esc = (s) => String(s ?? "").replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]); const esc = (s) => String(s ?? "").replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);
const hex = (n, width) => "0x" + n.toString(16).toUpperCase().padStart(width, "0"); const hex = (n, width) => "0x" + n.toString(16).toUpperCase().padStart(width, "0");
const nodeName = (n) => `${n} <code>${hex(Number(n), 2)}</code>`; const nodeName = (n) => `${n} <code>${hex(Number(n), 2)}</code>`;
@@ -74,7 +105,7 @@
for (const f of data.frames) if (f.node != null) (roles[f.node] ||= new Set()).add(f.role); for (const f of data.frames) if (f.node != null) (roles[f.node] ||= new Set()).add(f.role);
fill("nodes", Object.entries(data.nodes).sort((a, b) => a[0] - b[0]).map(([id, n]) => { fill("nodes", Object.entries(data.nodes).sort((a, b) => a[0] - b[0]).map(([id, n]) => {
const emcy = n.emcy ? `<code>${hex(n.emcy.code, 4)}</code> reg <code>${hex(n.emcy.register, 2)}</code> ${n.emcy.code === 0 ? "(reset)" : ""} · ${age(n.emcy.at)}` : "—"; const emcy = n.emcy ? `<code>${hex(n.emcy.code, 4)}</code> reg <code>${hex(n.emcy.register, 2)}</code> ${n.emcy.code === 0 ? "(reset)" : ""} · ${age(n.emcy.at)}` : "—";
return `<tr class="${stale(n.seen_at) && !n.silent ? "stale" : ""}"><td>${nodeName(id)}</td><td class="${n.silent ? "bad" : STATE_CLASS[n.state] || ""}">${n.silent ? "silent (heartbeat stopped)" : esc(n.state || "no heartbeat")}</td><td>${age(n.seen_at)}</td><td class="${n.emcy?.code ? "bad" : ""}">${emcy}</td><td>${[...(roles[id] || [])].sort().map(esc).join(", ")}</td></tr>`; return `<tr class="${stale(n.seen_at) && !n.silent ? "stale" : ""}"><td>${nodeName(id)}</td><td class="${n.silent ? "bad" : STATE_CLASS[n.state] || ""}"${n.silent ? ' title="3 heartbeats missed"' : ""}>${n.silent ? "silent" : esc(n.state || "no heartbeat")}</td><td>${age(n.seen_at)}</td><td class="${n.emcy?.code ? "bad" : ""}">${emcy}</td><td>${[...(roles[id] || [])].sort().map(esc).join(", ")}</td></tr>`;
}), "No CANopen node heard yet.", 5); }), "No CANopen node heard yet.", 5);
fill("signals", Object.entries(data.signals).sort().map(([name, s]) => fill("signals", Object.entries(data.signals).sort().map(([name, s]) =>
@@ -86,8 +117,9 @@
.map((f) => ({ ...f, cob: f.extended ? hex(f.id, 8) : hex(f.id, 3) })) .map((f) => ({ ...f, cob: f.extended ? hex(f.id, 8) : hex(f.id, 3) }))
.filter((f) => !filter || [f.interface, f.cob, f.role, f.node].join(" ").toLowerCase().includes(filter)) .filter((f) => !filter || [f.interface, f.cob, f.role, f.node].join(" ").toLowerCase().includes(filter))
.sort((a, b) => a.interface.localeCompare(b.interface) || a.id - b.id); .sort((a, b) => a.interface.localeCompare(b.interface) || a.id - b.id);
const now = Date.now() / 1000;
fill("frames", frames.map((f) => fill("frames", frames.map((f) =>
`<tr class="${stale(f.at) ? "stale" : ""}"><td>${esc(f.interface)}</td><td><code>${f.cob}</code>${f.extended ? " ext" : ""}</td><td>${esc(f.role || "")}</td><td>${f.node ?? ""}</td><td class="num">${f.count}</td><td class="num">${f.interval ? (1 / f.interval).toFixed(f.interval > 1 ? 2 : 1) + " Hz" : ""}</td><td><code>${(f.data.match(/../g) || []).join(" ")}</code></td><td>${age(f.at)}</td></tr>`), `<tr class="${stale(f.at) ? "stale" : ""}"><td>${esc(f.interface)}</td><td><code>${f.cob}</code>${f.extended ? " ext" : ""}</td><td>${esc(f.role || "")}</td><td>${f.node ?? ""}</td><td class="num">${f.count}</td><td class="num">${f.interval ? (1 / f.interval).toFixed(f.interval > 1 ? 2 : 1) + " Hz" : ""}</td><td><code>${dataHtml(f, `${f.interface}:${f.cob}`, now)}</code></td><td>${age(f.at)}</td></tr>`),
filter ? "No frame matches." : "No frame yet.", 8); filter ? "No frame matches." : "No frame yet.", 8);
} }