91 lines
3.5 KiB
Bash
Executable File
91 lines
3.5 KiB
Bash
Executable File
#!/bin/bash
|
|
# 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).
|
|
# Big files (GBs), so by default only at home: when HOME_HOST answers.
|
|
# HOME_HOST= (empty) pulls anywhere, LTE included.
|
|
set -euo pipefail
|
|
|
|
ENV_FILE=${MAP_ENV:-/srv/podman/maptool/.env}
|
|
DATA=${MAP_DATA:-/srv/podman/maptool/data}
|
|
HOME_HOST=${HOME_HOST-10.17.240.2}
|
|
|
|
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
|
|
|
|
# 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
|
|
# --speed-*: a connection that went dead (Wi-Fi switched, proxy gone)
|
|
# is dropped after a minute without data and the retry resumes.
|
|
curl -fsS --retry 10 --retry-all-errors --retry-delay 5 --speed-limit 1024 --speed-time 60 -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 [ "$updated" -gt 0 ]; then
|
|
echo "pull-maps: $updated files changed, restarting maptool"
|
|
sudo systemctl restart podman-compose@maptool.service
|
|
fi
|
|
[ "$failed" -eq 0 ]
|