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
This commit is contained in:
1 parent
27313b3101
commit
d58f7efc41
4 files changed
+111
-22
No files matched your search
+46
-15
@@ -3,12 +3,18 @@
|
||||
# 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).
|
||||
# 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;
|
||||
# data/update/status.json tells the app how far it got. docs/deployment.adoc.
|
||||
# swaps it into build/ (the old one kept as build.old), restarts maptool and
|
||||
# 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
|
||||
|
||||
ROOT=${MAPTOOL_ROOT:-/srv/podman/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
|
||||
req=$DIR/request.json
|
||||
|
||||
@@ -21,22 +27,47 @@ status() {
|
||||
mv "$DIR/status.json.tmp" "$DIR/status.json"
|
||||
}
|
||||
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"
|
||||
status running "downloading ${sha:0:7}"
|
||||
tmp=$(mktemp -d)
|
||||
trap 'rm -rf "$tmp"' EXIT
|
||||
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"
|
||||
short=${sha:0:7}
|
||||
# asked twice (two screens, a double click): nothing to do the second time
|
||||
[ "$(cat "$ROOT/build/REVISION" 2>/dev/null)" = "$short" ] && { status done "already on $short"; exit 0; }
|
||||
|
||||
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"
|
||||
mv "$ROOT/build" "$ROOT/build.old"
|
||||
mv "$tmp/build" "$ROOT/build"
|
||||
status restarting "restarting with ${sha:0:7}"
|
||||
mv "$new" "$ROOT/build"
|
||||
sync
|
||||
trap - EXIT
|
||||
status restarting "restarting with $short"
|
||||
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)"
|
||||
Reference in new issue
Block a user