CANopen diagnostics and map data over HTTPS: /can.html (interfaces, nodes with heartbeat state and last EMCY, decoded signals, every COB-ID with role, count, rate and last data); signals can mark an invalid raw value (esphome-canopen's unknown) and NaN gives no value; the CAN reader never dies and a broken can.json only turns CAN off; the GNSS node mcu-stm32-xiao-c5-2 (0x21) and host networking for the car's server in docs; bin/pull-maps.sh pulls map data from GET /api/sync/maps(/file) with SYNC_TOKEN instead of rsync over SSH, resuming cut-off downloads
This commit is contained in:
1 parent
a3e5fefaf1
commit
6a0d6194fe
9 files changed
+365
-43
No files matched your search
+74
-23
@@ -1,37 +1,88 @@
|
||||
#!/bin/bash
|
||||
# Map data, one way: the home server (brick) builds it, the car's server
|
||||
# pulls it — only while at home (brick reachable), never over LTE. The
|
||||
# library goes both ways on its own (lib/maptool/sync.rb), not here.
|
||||
# Map data, one way: the main server (brick) builds it, the car's server
|
||||
# pulls it over HTTPS from the same public URL and SYNC_TOKEN as the
|
||||
# library sync (no SSH/VPN): GET /api/sync/maps lists the files, each one
|
||||
# comes from /api/sync/maps/file, a cut-off download resumes. The library
|
||||
# goes both ways on its own (lib/maptool/sync.rb), not here.
|
||||
#
|
||||
# Run by maptool-pull-maps.timer on the car's server (docs/deployment.adoc).
|
||||
# The key it uses is limited on brick to read-only rsync of the data dir:
|
||||
# command="python3 /usr/share/doc/packages/rsync/support/rrsync -ro /srv/podman/maptool/data",restrict ssh-ed25519 …
|
||||
# so paths below are relative to brick's data/.
|
||||
# Big files (GBs), so by default only at home: when HOME_HOST answers.
|
||||
# HOME_HOST= (empty) pulls anywhere, LTE included.
|
||||
set -euo pipefail
|
||||
|
||||
SOURCE=${MAP_SOURCE:-randy@10.17.240.2}
|
||||
ENV_FILE=${MAP_ENV:-/srv/podman/maptool/.env}
|
||||
DATA=${MAP_DATA:-/srv/podman/maptool/data}
|
||||
FILES="offline.pmtiles offline.pmtiles.meta.json terrain.pmtiles terrain.pmtiles.meta.json offline_regions.json"
|
||||
DIRS="routing places hiking_trails kiwix nps"
|
||||
HOME_HOST=${HOME_HOST-10.17.240.2}
|
||||
|
||||
host=${SOURCE#*@}
|
||||
if ! timeout 5 bash -c "</dev/tcp/$host/22" 2>/dev/null; then
|
||||
echo "pull-maps: $host not reachable (not at home), skipping"
|
||||
env_value() { grep -m1 "^$1=" "$ENV_FILE" | cut -d= -f2- | sed 's/^["'\'']//; s/["'\'']$//'; }
|
||||
server=$(env_value SYNC_SERVER_URL)
|
||||
token=$(env_value SYNC_TOKEN)
|
||||
if [ -z "$server" ] || [ -z "$token" ]; then
|
||||
echo "pull-maps: no SYNC_SERVER_URL/SYNC_TOKEN in $ENV_FILE" >&2
|
||||
exit 1
|
||||
fi
|
||||
server=${server%/}
|
||||
|
||||
if [ -n "$HOME_HOST" ] && ! timeout 5 bash -c "</dev/tcp/$HOME_HOST/22" 2>/dev/null; then
|
||||
echo "pull-maps: $HOME_HOST not reachable (not at home), skipping"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
changes=$(mktemp)
|
||||
trap 'rm -f "$changes"' EXIT
|
||||
# Into a temp file, renamed when complete (rsync's default): the app and
|
||||
# Valhalla keep reading the old copy until restarted.
|
||||
for f in $FILES; do
|
||||
rsync -a --partial-dir=.rsync-partial --out-format='%n' "$SOURCE:$f" "$DATA/$f" >> "$changes" 2>/dev/null || true
|
||||
done
|
||||
for d in $DIRS; do
|
||||
rsync -a --delete-delay --partial-dir=.rsync-partial --out-format='%n' "$SOURCE:$d/" "$DATA/$d/" >> "$changes"
|
||||
# The token in a header file, not on the command line (ps shows that).
|
||||
auth=$(mktemp)
|
||||
trap 'rm -f "$auth"' EXIT
|
||||
printf 'Authorization: Bearer %s\n' "$token" > "$auth"
|
||||
|
||||
manifest=$(curl -fsS --retry 3 -H "@$auth" "$server/api/sync/maps")
|
||||
|
||||
updated=0
|
||||
failed=0
|
||||
declare -A wanted=()
|
||||
while IFS=$'\t' read -r size mtime path; do
|
||||
[ -n "$path" ] || continue
|
||||
case "$path" in /* | *..*) echo "pull-maps: skipping odd path $path" >&2; continue ;; esac
|
||||
wanted[$path]=1
|
||||
local_file="$DATA/$path"
|
||||
[ -f "$local_file" ] && [ "$(stat -c '%s %Y' "$local_file")" = "$size $mtime" ] && continue
|
||||
|
||||
mkdir -p "$(dirname "$local_file")"
|
||||
part="$local_file.part"
|
||||
# A partial download of another version of the file starts over.
|
||||
[ "$(cat "$part.version" 2>/dev/null)" = "$size $mtime" ] || rm -f "$part"
|
||||
echo "$size $mtime" > "$part.version"
|
||||
echo "pull-maps: $path ($((size / 1048576)) MB)"
|
||||
# Into a temp file, renamed when complete: the app and Valhalla keep
|
||||
# reading the old copy until restarted.
|
||||
if [ "$(stat -c %s "$part" 2>/dev/null || echo -1)" != "$size" ]; then
|
||||
curl -fsS --retry 10 --retry-all-errors --retry-delay 5 -C - -o "$part" -H "@$auth" \
|
||||
--get --data-urlencode "path=$path" "$server/api/sync/maps/file" || true
|
||||
fi
|
||||
if [ "$(stat -c %s "$part" 2>/dev/null || echo -1)" = "$size" ]; then
|
||||
touch -d "@$mtime" "$part"
|
||||
mv "$part" "$local_file"
|
||||
rm -f "$part.version"
|
||||
updated=$((updated + 1))
|
||||
else
|
||||
echo "pull-maps: $path incomplete, continuing next run" >&2
|
||||
failed=$((failed + 1))
|
||||
fi
|
||||
done <<< "$manifest"
|
||||
|
||||
# What the server no longer has, in the directories it sends.
|
||||
for dir in $(cut -f3 <<< "$manifest" | grep / | cut -d/ -f1 | sort -u); do
|
||||
while IFS= read -r -d '' file; do
|
||||
rel=${file#"$DATA/"}
|
||||
case "$rel" in *.part | *.part.version | routing/service.sh) continue ;; esac
|
||||
if [ -z "${wanted[$rel]:-}" ]; then
|
||||
echo "pull-maps: removing $rel"
|
||||
rm -f "$file"
|
||||
updated=$((updated + 1))
|
||||
fi
|
||||
done < <(find "$DATA/$dir" -type f -not -path '*/.*' -print0)
|
||||
done
|
||||
|
||||
if grep -qv '/$' "$changes"; then
|
||||
echo "pull-maps: $(grep -cv '/$' "$changes") files updated, restarting maptool"
|
||||
if [ "$updated" -gt 0 ]; then
|
||||
echo "pull-maps: $updated files changed, restarting maptool"
|
||||
sudo systemctl restart podman-compose@maptool.service
|
||||
fi
|
||||
[ "$failed" -eq 0 ]
|
||||
Reference in new issue
Block a user