#!/bin/bash # The sync server from scratch, the way docs/server.md says (bin/setup-server.sh with Apache), in a clean # Debian container — certbot left out (no public name here). Checks: no token → 401, the printed token # works, data/ isn't reachable from the web. Leaves it running on http://127.0.0.1:18080 with --keep # (then: docker rm -f dash-sync-test), e.g. to connect a test dashboard to it. set -euo pipefail REPO=$(cd "$(dirname "$0")/.." && pwd) KEEP=0; [ "${1:-}" = --keep ] && KEEP=1 docker rm -f dash-sync-test > /dev/null 2>&1 || true docker run -d --name dash-sync-test -p 18080:80 -v "$REPO":/repo:ro debian:bookworm sleep infinity > /dev/null trap '[ $KEEP = 1 ] || docker rm -f dash-sync-test > /dev/null' EXIT docker exec dash-sync-test bash -c ' set -e apt-get update -qq > /dev/null && apt-get install -y -qq apache2 libapache2-mod-php php-sqlite3 php-cli sudo curl rsync > /dev/null 2>&1 printf "#!/bin/sh\napache2ctl -k graceful\n" > /usr/local/bin/systemctl && chmod +x /usr/local/bin/systemctl mkdir -p /etc/letsencrypt/live/sync.test # stands in for certbot apache2ctl start rsync -a /repo/server/ /srv/dash-sync/ bash /srv/dash-sync/bin/setup-server.sh sync.test me > /tmp/setup.log 2>&1 a2dissite -q 000-default && apache2ctl -k graceful' > /dev/null TOKEN=$(docker exec dash-sync-test grep -oE '^ +[A-Za-z0-9_-]{40,}$' /tmp/setup.log | head -1 | tr -d ' ') sleep 1 fail=0 code=$(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:18080/v1/hello) [ "$code" = 401 ] || { echo "FAIL no token → $code (want 401)"; fail=1; } hello=$(curl -s -H "Authorization: Bearer $TOKEN" http://127.0.0.1:18080/v1/hello) echo "$hello" | grep -q '"device":"dashboard"' || { echo "FAIL the token: $hello"; fail=1; } code=$(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:18080/../data/sync.db) [ "$code" != 200 ] || { echo "FAIL data/sync.db is reachable"; fail=1; } # many devices at once: 60 batches, 8 new devices and 20 reads in parallel must all succeed (no "database is locked") # — on a throw-away account, so the 'dashboard' token above stays clean for a test dashboard docker exec dash-sync-test bash -c 'sudo -u www-data php /srv/dash-sync/bin/admin.php add-account load > /dev/null sudo -u www-data php /srv/dash-sync/bin/admin.php add-device load loader' > /tmp/dash-load-token.$$ 2>&1 LOAD=$(grep -oE '[A-Za-z0-9_-]{40,}' /tmp/dash-load-token.$$ | head -1); rm -f /tmp/dash-load-token.$$ python3 - "$LOAD" <<'PY' || fail=1 import os, sys, urllib.request, concurrent.futures as cf T, B = sys.argv[1], "http://127.0.0.1:18080" def call(kind): h = {"Authorization": "Bearer " + T} if kind == "batch": req = urllib.request.Request(B + "/v1/batches", data=b"DS1\0" + os.urandom(4000), headers=h) elif kind == "dev": req = urllib.request.Request(B + "/v1/devices", data=b'{"name":"d%d"}' % int.from_bytes(os.urandom(3), "big"), headers=dict(h, **{"Content-Type": "application/json"})) else: req = urllib.request.Request(B + "/v1/batches?after=0&limit=5", headers=h) try: return urllib.request.urlopen(req, timeout=60).status except urllib.error.HTTPError as e: return e.code key = urllib.request.Request(B + "/v1/key", data=b'{"v":1,"salt":"x","nonce":"x","box":"x"}', # a dummy key: the headers={"Authorization": "Bearer " + T}, method="PUT") # loader manages devices urllib.request.urlopen(key, timeout=30) with cf.ThreadPoolExecutor(30) as ex: codes = list(ex.map(call, ["batch"] * 60 + ["dev"] * 8 + ["get"] * 20)) bad = [c for c in codes if c != 200] get = lambda path: __import__("json").loads(urllib.request.urlopen(urllib.request.Request(B + path, headers={"Authorization": "Bearer " + T}), timeout=30).read()) latest, devices = get("/v1/hello")["latest"], len(get("/v1/devices")["devices"]) if bad: print(f"FAIL {len(bad)} of {len(codes)} parallel requests failed: {sorted(set(bad))}") elif latest < 60 or devices < 9: # answered 200, but was it kept? (a rolled-back write says ok too) print(f"FAIL not kept: {latest} batches (want 60), {devices} devices (want 9)") bad = [0] else: print(f"{len(codes)} parallel requests ok, all kept") sys.exit(1 if bad else 0) PY [ $fail = 0 ] && echo "ok — token for the device 'dashboard': $TOKEN" [ $KEEP = 1 ] && echo "running on http://127.0.0.1:18080 (docker rm -f dash-sync-test)" exit $fail