Commit b936c81a by PLN (Algolia)

Sync bin/devname and tests with the estate copy

Brings in what the estate copy grew since the split: the status
history and 24 h / 14 d index page, gc of stale names, wildcard and
alias names, and the LAN face behind the ssh gate. It is the macOS-only
version; the Linux support follows in the next commits, ported onto
this base.
parent cc943387
#!/opt/homebrew/bin/python3
"""devname: stable local names for dev servers, so nobody remembers ports.
One Caddy (a launchd agent, label DEVNAME_LABEL) listens on :80/:443 and proxies each name to its port. An unprivileged process can bind a low port only
One Caddy (agent pl.nech.devname) listens on :80/:443 and proxies each name to its port. An unprivileged process can bind a low port only
on the wildcard address (not 127.0.0.1), so every site first aborts any connection whose source is not loopback.
`*.localhost` resolves to loopback on this Mac with no DNS setup (RFC 6761), so the names are local-program-only.
......@@ -16,10 +16,23 @@ allocates a port (17000-17999), registers the name and reloads Caddy. The servic
devname ensure [project] [--service S] [--branch B|auto] [--port N] [--keep-host] [--format url|port|json]
devname ls [--json] devname rm <host> devname check <host>
devname gc [--idle-hours N] [--yes] list/remove stale names (down, no ok probe in N h; default dry-run)
devname render devname serve devname doctor devname agent install|status
The agent also serves the index page devname.localhost: every registered name, live up/down status,
plus /json. It reads names.json per request — no snapshot to stale.
Since 2026-11-08 the page also has a memory: a poller probes every name once a minute the way a
visitor sees it (through the proxy, real Host header) and stores port/code/latency in CONF/status.db.
The page and /json then show per name, over the last 24 h: availability %, 5xx count, p50/p95 ms.
Rows older than 14 d are pruned on each poll. The "up" dot stays a point-in-time port check;
the 24 h column is the one that catches "down for an hour at 3 am".
LAN face (2026-11-09): the agent runs a gate thread — every 60 s `ssh domovoy-admin true`; the Pi
proves itself with its pinned ssh host key. While it holds, every name also answers as
<name>.mac.local (phones on the home wifi): https, private-range guard, announced via dns-sd, links
on the gateway page mac.local rewritten to match. Handshake fails -> no announcements, LAN blocks
out of Caddy, loopback untouched. http://mac.local/rootCA.pem stays on plain http on purpose: a
phone that trusts no CA yet must be able to fetch it.
Registry: $DEVNAME_HOME (default ~/.config/devnames)/names.json. https needs the mkcert root CA (`mkcert -install`, once).
"""
......@@ -32,15 +45,20 @@ import hashlib
import html
import http.server
import json
import math
import os
import re
import shutil
import signal
import string
import socket
import sqlite3
import subprocess
import sys
import threading
import time
import urllib.error
import urllib.parse
import urllib.request
HOME = os.path.expanduser("~")
......@@ -51,7 +69,7 @@ ADMIN_SOCK = os.path.join(CONF, "admin.sock")
CERT_DIR = os.path.join(CONF, "certs")
CERT, KEY, SANS = (os.path.join(CERT_DIR, n) for n in ("devname.pem", "devname-key.pem", "sans.txt"))
STATE = os.path.join(HOME, ".local", "state", "devname")
LABEL = os.environ.get("DEVNAME_LABEL", "devname")
LABEL = "pl.nech.devname"
PLIST = os.path.join(HOME, "Library", "LaunchAgents", LABEL + ".plist")
LOGDIR = os.path.join(HOME, "Library", "Logs", "devname")
POOL = range(17000, 18000)
......@@ -60,8 +78,26 @@ MKCERT = shutil.which("mkcert") or "/opt/homebrew/bin/mkcert"
# no JDK keystore here: mkcert's optional Java step exits 1 and would block issuing the cert (found 2026-11-03)
MKCERT_ENV = dict(os.environ, TRUST_STORES="system,nss")
SUFFIX = ".localhost"
# LAN face (2026-11-09): every name also answers as <name>.mac.local for phones on the home wifi,
# but ONLY while the domovoy handshake holds. Gate = `ssh domovoy-admin true` succeeding: the Pi
# authenticates with its ssh host key (a privkey-signed answer against the pinned known_hosts entry),
# so a lookalike device on a foreign LAN cannot open the door. Gate closed = no mDNS announcements,
# no LAN site blocks in Caddy; loopback names keep working regardless.
LAN_SUFFIX = ".mac.local"
GATEWAY_HOST = "mac.local" # the index page's LAN name; its http face serves the root CA for phones
LAN_NETS = ("127.0.0.0/8 ::1 192.168.0.0/16 10.0.0.0/8 172.16.0.0/12 fe80::/10 fd00::/8")
HANDSHAKE_ARGS = ["ssh", "-o", "BatchMode=yes", "-o", "ConnectTimeout=5", "-o", "RequestTTY=no",
"domovoy-admin", "true"]
HANDSHAKE_SECS = 60
LAN_GATE = os.path.join(CONF, "lan-gate.json") # announcer state: {"open": bool, "checked": ts, "detail": str}
INDEX_PORT = 16999 # internal: the index page behind devname.localhost, served by the agent itself
INDEX_HOST = "devname.localhost"
STATUS_DB = os.path.join(CONF, "status.db") # probe history: the index page's memory (lives with the registry it observes)
POLL_SECS = 60 # status-history probe cadence
STATS_WINDOW = 86400 # availability/latency window shown on the page (24 h)
RETENTION = 14 * 86400 # probe rows older than this are pruned on each poll
_status_lock = threading.Lock()
_status_db = None
MAIN_BRANCHES = {"main", "master", "HEAD", ""}
NAME_RE = re.compile(r"^[a-z0-9]([a-z0-9-]{0,38}[a-z0-9])?$")
......@@ -133,12 +169,277 @@ def alloc_port(reg):
raise Err("port pool 17000-17999 exhausted")
# ---- status history: the index page's memory
def status_db():
"""One shared connection, WAL so page renders read while the poller writes."""
global _status_db
if _status_db is None:
os.makedirs(CONF, exist_ok=True)
_status_db = sqlite3.connect(STATUS_DB, check_same_thread=False)
_status_db.execute("PRAGMA journal_mode=WAL")
_status_db.execute(
"CREATE TABLE IF NOT EXISTS probe (ts INTEGER, host TEXT, port_up INTEGER, code INTEGER, ms INTEGER)")
_status_db.execute("CREATE INDEX IF NOT EXISTS probe_ts ON probe (ts)")
_status_db.commit()
return _status_db
def probe_entry(host, entry):
"""One probe as a visitor sees it: through the proxy with the real Host header.
Returns (port_up, code, ms); code None means even Caddy did not answer."""
port_up = port_listening(entry["port"])
t0 = time.monotonic()
code = None
try:
req = urllib.request.Request("http://127.0.0.1/", headers={"Host": host})
with urllib.request.urlopen(req, timeout=3) as r:
code = r.status
except urllib.error.HTTPError as ex:
code = ex.code
except OSError:
code = None
return port_up, code, int((time.monotonic() - t0) * 1000)
def poll_once():
"""Probe every registered name once, record, prune; returns the row count."""
entries = load()["entries"]
ts = int(time.time())
items = list(entries.items())
if items:
with cf.ThreadPoolExecutor(max_workers=min(32, len(items))) as ex:
probed = list(ex.map(lambda kv: probe_entry(*kv), items))
rows = [(ts, h, *r) for (h, _), r in zip(items, probed)]
else:
rows = []
db = status_db()
with _status_lock:
db.executemany("INSERT INTO probe VALUES (?,?,?,?,?)", rows)
db.execute("DELETE FROM probe WHERE ts < ?", (ts - RETENTION,))
db.commit()
return len(rows)
def host_stats(window=STATS_WINDOW):
"""host -> stats over recorded probes in the window: n, up, err5, avail %, p50, p95,
plus `hours`: 24 hourly availability % (oldest first, None = no probes that hour) for the sparkline."""
db = status_db()
now = int(time.time())
since = now - window
now_hour = now // 3600
with _status_lock:
rows = db.execute("SELECT host, code, ms, ts FROM probe WHERE ts >= ?", (since,)).fetchall()
by = {}
for h, code, ms, ts in rows:
d = by.setdefault(h, [0, 0, 0, [], [0] * 24, [0] * 24]) # n, up, err5, ms[], hour n, hour up
d[0] += 1
if code is None:
continue # proxy itself dead: neither up nor a 5xx of the site's own
if code < 500:
d[1] += 1
else:
d[2] += 1
if ms is not None:
d[3].append(ms)
idx = 23 - (now_hour - ts // 3600) # 0 = 23 h ago … 23 = current hour (list renders oldest first)
if 0 <= idx < 24:
d[4][idx] += 1
if code < 500:
d[5][idx] += 1
out = {}
for h, (n, up, err5, ms, hn, hup) in by.items():
ms.sort()
out[h] = {
"n": n, "up": up, "err5": err5,
"avail": round(100.0 * up / n, 1) if n else None,
"p50": ms[len(ms) // 2] if ms else None,
"p95": ms[max(0, math.ceil(0.95 * len(ms)) - 1)] if ms else None,
"hours": [round(100.0 * u / k, 1) if k else None for k, u in zip(hn, hup)],
}
return out
def last_up():
"""host -> ts of its last successful probe (None = never answered), over the whole 14 d history."""
db = status_db()
with _status_lock:
rows = db.execute(
"SELECT host, MAX(ts) FROM probe WHERE code IS NOT NULL AND code < 500 GROUP BY host").fetchall()
return dict(rows)
STALE_HOURS = 72 # no successful probe for this long (or never) + down now = stale on the page, gc's default bar
def is_stale(host, entry, up, lastups, now=None):
"""Stale = down right now and no successful probe in STALE_HOURS (or none ever)."""
if up:
return False
lu = lastups.get(host)
now = int(now or time.time())
return lu is None or now - lu > STALE_HOURS * 3600
def daystrip_svg(days):
"""14 daily availability bars (tooltips carry label + %); a gray stub means no probes that day."""
H, BW, GAP = 18, 7.0, 2.0
bars = []
for i, (label, a) in enumerate(days):
x = round(i * (BW + GAP), 2)
if a is None:
bars.append(f'<rect x="{x}" y="{H - 2}" width="{BW}" height="2" fill="#8882">'
f'<title>{label} — no probes</title></rect>')
continue
h = max(1.0, round(H * a / 100.0, 2))
color = "#2e9e44" if a >= 99.5 else ("#d0483e" if a < 50.0 else "#d9a13b")
bars.append(f'<rect x="{x}" y="{H - h}" width="{BW}" height="{h}" fill="{color}">'
f'<title>{label} — {a:.1f}%</title></rect>')
return (f'<svg class="spark" width="{round(len(days) * (BW + GAP) - GAP, 2)}" height="{H}" '
f'role="img" aria-label="availability per day, last 14 d">{"".join(bars)}</svg>')
def host_days(window=RETENTION):
"""host -> [(label, avail % or None)] for the last 14 local days, oldest first (the 14-day strip)."""
db = status_db()
now = int(time.time())
with _status_lock:
rows = db.execute("SELECT host, code, ts FROM probe WHERE ts >= ?", (now - window,)).fetchall()
today = datetime.date.fromtimestamp(now)
keys = [today - datetime.timedelta(days=13 - i) for i in range(14)]
per = {}
for h, code, ts in rows:
if code is None:
continue
n, up = per.setdefault(h, {}).setdefault(datetime.date.fromtimestamp(ts), [0, 0])
per[h][datetime.date.fromtimestamp(ts)] = [n + 1, up + (1 if code < 500 else 0)]
out = {}
for h, d in per.items():
out[h] = []
for k in keys:
n, up = d.get(k, [0, 0])
out[h].append((k.strftime("%d %b"), round(100.0 * up / n, 1) if n else None))
return out
def sparkline_svg(hours, now=None):
"""24 hourly availability bars on an honest 0-100% scale; a gray stub means no probes that hour.
Never the only carrier of the number: the % column and each bar's tooltip print it."""
now = int(now or time.time())
now_hour = now // 3600
H, BW, GAP = 18, 4.0, 1.2
bars = []
for i, a in enumerate(hours):
x = round(i * (BW + GAP), 2)
hh = time.strftime("%H:00", time.localtime((now_hour - (len(hours) - 1 - i)) * 3600))
if a is None:
bars.append(f'<rect x="{x}" y="{H - 2}" width="{BW}" height="2" fill="#8882">'
f'<title>{hh} — no probes</title></rect>')
continue
h = max(1.0, round(H * a / 100.0, 2))
color = "#2e9e44" if a >= 99.5 else ("#d0483e" if a < 50.0 else "#d9a13b")
bars.append(f'<rect x="{x}" y="{H - h}" width="{BW}" height="{h}" fill="{color}">'
f'<title>{hh} — {a:.1f}%</title></rect>')
return (f'<svg class="spark" width="{round(len(hours) * (BW + GAP) - GAP, 2)}" height="{H}" '
f'role="img" aria-label="availability per hour, last 24 h">{"".join(bars)}</svg>')
def start_status_poller():
def loop():
time.sleep(5) # Caddy is still binding :80 when we start; the first sweep would record all-None
while True:
try:
poll_once()
except Exception as ex: # the poller must survive anything; its failure goes to the log
print(f"devname: status poll failed: {ex}", file=sys.stderr, flush=True)
time.sleep(POLL_SECS)
threading.Thread(target=loop, daemon=True, name="status-poller").start()
# ---- LAN announcer: the gate thread inside `devname serve`
def handshake_ok():
"""True iff the real domovoy answered: BatchMode ssh, so the Pi's pinned ssh host key signed
the exchange. A lookalike device on a foreign LAN fails the host-key check -> gate stays shut."""
try:
return subprocess.run(HANDSHAKE_ARGS, capture_output=True, timeout=15).returncode == 0
except (OSError, subprocess.TimeoutExpired):
return False
def write_gate(open_, detail):
tmp = LAN_GATE + ".tmp"
with open(tmp, "w") as fh:
json.dump({"open": open_, "checked": int(time.time()), "detail": detail}, fh)
fh.write("\n")
os.replace(tmp, LAN_GATE)
def lan_ip():
"""The Mac's private-range IPv4 — the address LAN devices will connect to — or None."""
try:
out = subprocess.run(["ifconfig"], capture_output=True, text=True, timeout=5).stdout
except (OSError, subprocess.TimeoutExpired):
return None
m = re.search(r"^\s+inet ((?:192\.168|10\.)\S+|172\.(?:1[6-9]|2\d|3[01])\.\S+) ", out, re.M)
return m.group(1) if m else None
def start_lan_announcer():
"""Every HANDSHAKE_SECS: make domovoy prove itself, then open or close the LAN face.
Open = render the .mac.local blocks + one dns-sd -P proxy registration per name (and mac.local
itself). -P, not -R: a plain -R service registration publishes no A record, so phones (and
avahi on the Pi) time out resolving the name — measured 2026-11-09. Closed = tear both down.
Loopback names never change with the gate."""
procs = {} # lan name -> (dns-sd Popen, advertised ip)
def stop(name=None):
for n in ([name] if name else list(procs)):
p = procs.pop(n, None)
if p and p[0].poll() is None:
p[0].terminate()
def reconcile(names):
ip = lan_ip()
if not ip:
stop() # no private address: nothing LAN-side to point the records at
return
for n in sorted(names):
p = procs.get(n)
if p is None or p[0].poll() is not None or p[1] != ip: # new, died, or dhcp moved us
stop(n)
procs[n] = (subprocess.Popen(["dns-sd", "-P", n, "_http._tcp", ".", "443", n, ip],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL), ip)
def loop():
while True:
ok = handshake_ok()
prev = lan_gate_open()
write_gate(ok, "domovoy handshake ok" if ok else "domovoy handshake failed: no announce, LAN face closed")
if ok != prev:
try:
with locked():
render(load())
except Exception as ex:
print(f"devname: lan gate render failed: {ex}", file=sys.stderr, flush=True)
if ok:
try:
reg = load()
reconcile({h[:-len(SUFFIX)] + LAN_SUFFIX for h in reg["entries"]} | {GATEWAY_HOST})
except Exception as ex:
print(f"devname: lan announce failed: {ex}", file=sys.stderr, flush=True)
else:
stop()
time.sleep(HANDSHAKE_SECS)
threading.Thread(target=loop, daemon=True, name="lan-announcer").start()
# ---- caddy
def have_https():
return os.path.exists(CERT) and os.path.exists(KEY)
def render_caddyfile(reg, https):
def render_caddyfile(reg, https, lan=False):
out = [
"# generated by devname; do not edit (devname render)",
"{",
......@@ -153,24 +454,59 @@ def render_caddyfile(reg, https):
out += [f"http://{host} {{"] + body + ["}"]
if https:
out += [f"https://{host} {{", f"\ttls {CERT} {KEY}"] + body + ["}"]
if https and lan:
body = site_body(up, e.get("rewrite", True), lan=True)
lanhost = host[:-len(SUFFIX)] + LAN_SUFFIX
out += [f"https://{lanhost} {{", f"\ttls {CERT} {KEY}"] + body + ["}"]
out += [f"http://{lanhost} {{", "\tredir https://{host}{uri} 301", "}"]
body = site_body(f"127.0.0.1:{INDEX_PORT}")
out += [f"http://{INDEX_HOST} {{"] + body + ["}"]
if https:
out += [f"https://{INDEX_HOST} {{", f"\ttls {CERT} {KEY}"] + body + ["}"]
if https and lan:
# the gateway's http face exists for exactly one thing: a phone that trusts no CA yet
# must still be able to fetch rootCA.pem (or its /CA shortcut). Everything else
# redirects to https.
out += [f"http://{GATEWAY_HOST} {{",
f"\t@notlan not remote_ip {LAN_NETS}", "\tabort @notlan",
"\t@ca path /rootCA.pem /CA",
"\t@notca not path /rootCA.pem /CA",
"\tredir @notca https://" + GATEWAY_HOST + "{uri} 301",
"\trewrite @ca /rootCA.pem",
"\treverse_proxy 127.0.0.1:16999",
"}"]
body = site_body(f"127.0.0.1:{INDEX_PORT}", rewrite=False, lan=True)
out += [f"https://{GATEWAY_HOST} {{", f"\ttls {CERT} {KEY}"] + body + ["}"]
return "\n".join(out) + "\n"
def site_body(up, rewrite=True):
body = ["\t@notlocal not remote_ip 127.0.0.0/8 ::1", "\tabort @notlocal", f"\treverse_proxy {up} {{"]
def site_body(up, rewrite=True, lan=False):
# loopback face: abort anything not from this Mac. LAN face: abort anything not from a
# private address (loopback, RFC1918, link-local, ULA) — home-lan scoped by construction.
nets = LAN_NETS if lan else "127.0.0.0/8 ::1"
guard = "@notlan" if lan else "@notlocal"
body = [f"\t{guard} not remote_ip {nets}", f"\tabort {guard}", f"\treverse_proxy {up} {{"]
if rewrite:
body.append(f"\t\theader_up Host {up}")
body.append("\t}")
return body
def lan_gate_open():
try:
with open(LAN_GATE) as fh:
return bool(json.load(fh).get("open"))
except (OSError, ValueError):
return False
def ensure_cert(reg):
"""One SAN cert over every registered name (wildcards cannot span two labels). Needs the mkcert root CA."""
names = sorted(set(reg["entries"]) | {INDEX_HOST, "localhost", "127.0.0.1", "::1"})
"""One SAN cert over every registered name, loopback and LAN faces both (wildcards cannot span two
labels). Issued whether or not the LAN gate is open: the cert must be ready when the gate opens.
Needs the mkcert root CA."""
names = sorted(set(reg["entries"])
| {h[:-len(SUFFIX)] + LAN_SUFFIX for h in reg["entries"]}
| {INDEX_HOST, GATEWAY_HOST, "localhost", "127.0.0.1", "::1"})
root = subprocess.run([MKCERT, "-CAROOT"], capture_output=True, text=True).stdout.strip()
if not os.path.exists(os.path.join(root, "rootCA.pem")):
return False
......@@ -190,7 +526,7 @@ def ensure_cert(reg):
def render(reg, reload=True):
https = ensure_cert(reg)
os.makedirs(CONF, mode=0o700, exist_ok=True)
text = render_caddyfile(reg, https)
text = render_caddyfile(reg, https, lan=https and lan_gate_open())
tmp = CADDYFILE + ".tmp"
with open(tmp, "w") as fh:
fh.write(text)
......@@ -235,8 +571,11 @@ def url(host):
# ---- commands
def cmd_ensure(a):
project = resolve_project(a.project)
if a.service:
wild = a.service == "*" # *.project.localhost: one server answers every single-label subdomain
if a.service and not wild:
check_name("service", a.service)
if wild and not a.port:
raise Err("a wildcard name shares an existing server: pass --port")
branch = resolve_branch(a.branch)
host = hostname(project, a.service, branch)
with locked():
......@@ -248,12 +587,15 @@ def cmd_ensure(a):
if not e or (a.port and a.force):
port = a.port or alloc_port(reg)
clash = [h for h, x in reg["entries"].items() if x["port"] == port and h != host]
if clash:
raise Err(f"port {port} is already registered as {clash[0]}")
if clash and not (a.alias or wild):
raise Err(f"port {port} is already registered as {clash[0]}"
f" (add --alias --port {port} to share it: one server routing several sites by Host)")
e = reg["entries"][host] = {
"port": port, "project": project, "service": a.service, "branch": branch,
"rewrite": not a.keep_host, "note": a.note or "",
"created": datetime.datetime.now(datetime.timezone.utc).isoformat(timespec="seconds"),
# who to thank (or ask): $DEVNAME_AGENT if the calling agent sets it, else the cwd it ran from
"created_by": os.environ.get("DEVNAME_AGENT") or os.path.basename(os.getcwd()) or None,
}
save(reg)
created = True
......@@ -287,6 +629,14 @@ def cmd_rm(a):
with locked():
reg = load()
if host not in reg["entries"]:
# a unique prefix resolves: `rm desk-p2` finds desk-p2.www.localhost (service of project www)
base = host[:-len(SUFFIX)] if host.endswith(SUFFIX) else host
matches = [h for h in reg["entries"] if h.startswith(base + ".")]
if len(matches) == 1:
host = matches[0]
elif matches:
raise Err(f"{a.host} is ambiguous: " + ", ".join(sorted(matches)))
else:
raise Err(f"{host} is not registered")
del reg["entries"][host]
save(reg)
......@@ -294,11 +644,59 @@ def cmd_rm(a):
return 0
def stale_entries(idle_hours, hosts=None):
"""Names worth removing: not listening now AND no successful probe in idle_hours (or none ever).
Safe by construction: an owning agent that needs its name back re-ensures it, idempotently."""
lastups = last_up()
now = int(time.time())
out = []
for h, e in sorted(load()["entries"].items()):
if hosts and h not in hosts:
continue
if port_listening(e["port"]):
continue # something is listening: never a gc candidate, however old
lu = lastups.get(h)
if lu is None or now - lu > idle_hours * 3600:
age = "never answered" if lu is None else f"last ok {time.strftime('%d %b %H:%M', time.localtime(lu))}"
out.append((h, e, age))
return out
def cmd_gc(a):
hosts = {h if h.endswith(SUFFIX) else h + SUFFIX for h in a.hosts}
victims = stale_entries(a.idle_hours, hosts or None)
if not victims:
print("devname: nothing stale" + (" among the given names" if hosts else ""))
return 0
for h, e, age in victims:
print(f"{'would remove' if not a.yes else 'removing'} {h} port {e['port']} {age}"
+ (f" note: {e['note']}" if e.get("note") else ""))
if not a.yes:
print(f"{len(victims)} candidate(s); rerun with --yes to remove (a live agent re-ensures its name idempotently)")
return 0
with locked():
reg = load()
for h, _, _ in victims: # re-check under the lock: another process may have touched the registry meanwhile
if h in reg["entries"] and not port_listening(reg["entries"][h]["port"]):
del reg["entries"][h]
save(reg)
print("devname:", render(reg), file=sys.stderr)
return 0
def cmd_check(a):
host = a.host if a.host.endswith(SUFFIX) else a.host + SUFFIX
e = load()["entries"].get(host)
entries = load()["entries"]
e = entries.get(host)
if not e:
print(f"{host}: not registered")
# a wildcard entry may cover it (Caddy semantics: exactly one extra label)
cands = [h for h in entries if h.startswith("*.") and host.endswith(h[1:])
and "." not in host[: -len(h[1:])]]
if cands:
host = max(cands, key=len)
e = entries[host]
if not e:
print(f"{a.host}: not registered")
return 1
up = port_listening(e["port"])
code = "-"
......@@ -320,37 +718,150 @@ def cmd_render(a):
return 0
# ---- index page (devname.localhost, served by the agent next to caddy)
PAGE = """<!doctype html>
# ---- index page (devname.localhost, served by the agent next to caddy).
# Server-renders the first paint (works without JS), then a small script hydrates from /json:
# no-flicker 30 s refresh, sortable headers, a filter box, and the 24 h / 14 d view toggle.
# A string.Template, not .format: the JS would need every brace doubled.
PAGE = string.Template(r"""<!doctype html>
<html lang="en"><head><meta charset="utf-8">
<meta http-equiv="refresh" content="30">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>devname — local names</title>
<style>
:root {{ color-scheme: light dark; }}
body {{ font: 15px/1.5 -apple-system, "Helvetica Neue", sans-serif; margin: 2rem auto; max-width: 52rem; padding: 0 1rem; }}
h1 {{ font-size: 1.3rem; margin: 0 0 .2rem; }}
p.sum {{ color: #888; margin: 0 0 1.4rem; }}
table {{ border-collapse: collapse; width: 100%; }}
th {{ text-align: left; font-weight: 600; border-bottom: 1px solid #8884; padding: .35rem .6rem; }}
td {{ padding: .35rem .6rem; border-bottom: 1px solid #8882; }}
td.port {{ color: #888; font-variant-numeric: tabular-nums; }}
.dot {{ display: inline-block; width: .55em; height: .55em; border-radius: 50%; margin-right: .45em; }}
tr.up .dot {{ background: #2e9e44; }}
tr.down .dot {{ background: #d0483e; }}
tr.down td.name a {{ color: #888; }}
.muted {{ color: #888; }}
footer {{ margin-top: 1.4rem; color: #888; font-size: .85rem; }}
:root { color-scheme: light dark; }
body { font: 15px/1.5 -apple-system, "Helvetica Neue", sans-serif; margin: 2rem auto; max-width: 58rem; padding: 0 1rem; }
h1 { font-size: 1.3rem; margin: 0 0 .2rem; }
p.sum { color: #888; margin: 0 0 1.4rem; }
p.sum input { margin-left: .8rem; font: inherit; color: inherit; background: none; border: none; border-bottom: 1px solid #8884; outline: none; width: 9rem; }
p.sum a { color: inherit; }
p.sum b a { color: #d0483e; text-decoration: none; }
table { border-collapse: collapse; width: 100%; }
th { text-align: left; font-weight: 600; border-bottom: 1px solid #8884; padding: .35rem .6rem; }
th[data-key] { cursor: pointer; user-select: none; }
th[data-key]:hover { color: #d0483e; }
td { padding: .35rem .6rem; border-bottom: 1px solid #8882; }
td.port { color: #888; font-variant-numeric: tabular-nums; }
td.lat { white-space: nowrap; }
td.sparkcell { padding-top: .45rem; padding-bottom: .45rem; }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.bad { color: #d0483e; font-weight: 600; }
.dot { display: inline-block; width: .55em; height: .55em; border-radius: 50%; margin-right: .45em; }
tr.up .dot { background: #2e9e44; }
tr.down .dot { background: #d0483e; }
tr.down td.name a { color: #888; }
.muted { color: #888; }
.stale { margin-left: .45em; font-size: .78rem; color: #888; border: 1px solid #8884; border-radius: .6em; padding: 0 .4em; }
td.name a { text-decoration: none; }
td.name a:hover { text-decoration: underline; }
footer { margin-top: 1.4rem; color: #888; font-size: .85rem; }
</style></head><body>
<h1>devname</h1>
<p class="sum">{n_names} names · {n_up} up · live from names.json · refreshes every 30 s</p>
<table><thead><tr><th>Name</th><th>Port</th><th>Status</th><th>Note</th></tr></thead>
<tbody>
{rows}
<p class="sum">$lan_note<span id="sum">$n_names names · $n_up up · live from names.json</span> ·
view <a href="?view=24h" data-view="24h">24 h</a> / <a href="?view=14d" data-view="14d">14 d</a><input id="q" placeholder="filter…" autocomplete="off"></p>
<table><thead><tr><th data-key="host">Name</th><th data-key="port">Port</th><th data-key="up">Status</th><th class="num" data-key="avail">24 h %</th><th id="sparkhead">24 h, hourly</th><th class="num" data-key="err5">5xx</th><th class="num" data-key="p50">p50 ms</th><th class="num" data-key="p95">p95 ms</th><th>Note</th></tr></thead>
<tbody id="rows">
$rows
</tbody></table>
<footer>served by <code>devname serve</code> · <a href="/json">JSON</a></footer>
<footer>served by <code>devname serve</code> · probed every $poll_secs s through the proxy · 14 d history ·
"up" dot = port listening now; 24 h % = HTTP via proxy answered &lt;500; bars are that % per hour (or per day, 14 d view), 0–100 ·
<a href="/json">JSON</a></footer>
<script>
"use strict";
var VIEW = "$view", DATA = null, SORT = null, Q = "";
function esc(s) { var d = document.createElement("div"); d.textContent = s == null ? "" : String(s); return d.innerHTML; }
function pad(h) { return (h < 10 ? "0" : "") + h; }
function bars(vals, labels, bw, gap) {
var H = 18, out = "", i, a, x, h, c;
for (i = 0; i < vals.length; i++) {
a = vals[i]; x = (i * (bw + gap)).toFixed(1);
if (a == null) {
out += '<rect x="' + x + '" y="16" width="' + bw + '" height="2" fill="#8882"><title>' + esc(labels[i]) + " — no probes</title></rect>";
continue;
}
h = Math.max(1, H * a / 100); c = a >= 99.5 ? "#2e9e44" : (a < 50 ? "#d0483e" : "#d9a13b");
out += '<rect x="' + x + '" y="' + (H - h).toFixed(1) + '" width="' + bw + '" height="' + h.toFixed(1) + '" fill="' + c + '"><title>' + esc(labels[i]) + " — " + a.toFixed(1) + "%</title></rect>";
}
return '<svg class="spark" width="' + (vals.length * (bw + gap) - gap).toFixed(1) + '" height="' + H + '" role="img" aria-label="availability">' + out + "</svg>";
}
function sparkFor(d) {
if (VIEW === "14d") {
var days = d.days || [], vals = [], labels = [], i;
for (i = 0; i < days.length; i++) { labels.push(days[i][0]); vals.push(days[i][1]); }
return bars(vals, labels, 7, 2);
}
var hours = (d.stats || {}).hours || [], labels2 = [], j;
var C = new Date().getHours(); // local hour, matching the server-rendered labels
for (j = 0; j < hours.length; j++) labels2.push(pad(((C - 23 + j) % 24 + 24) % 24) + ":00");
return bars(hours, labels2, 4, 1.2);
}
function row(d) {
var s = d.stats || {}, has = s.n > 0;
var LAN = /(^|\.)mac\.local$$/.test(location.hostname); // LAN face: link and label become .mac.local
var lh = LAN ? d.host.replace(/\.localhost$$/, ".mac.local") : d.host;
var name = '<a href="' + (LAN ? "https://" : "http://") + esc(lh) + '/">' + esc(lh) + "</a>";
if (d.branch) name += ' <span class="muted">⎇ ' + esc(d.branch) + "</span>";
if (d.stale) name += ' <span class="stale" title="stale: ' + (d.last_ok ? "not ok for over 72 h" : "never answered") +
'; removing is safe — the agent that made it just re-ensures it if it needs it again (devname gc)">stale</span>';
var avail = has && s.avail != null ? s.avail.toFixed(1) + "%" : "–";
var errc = has && s.err5 > 0 ? ' class="num bad"' : ' class="num"';
var p50 = has && s.p50 != null ? String(s.p50) : "–";
var p95 = has && s.p95 != null ? String(s.p95) : "–";
var spark = has ? sparkFor(d) : "";
return '<tr class="' + (d.up ? "up" : "down") + '"><td class="name">' + name + '</td><td class="port">' + d.port +
'</td><td><span class="dot"></span>' + (d.up ? "up" : "down") + '</td><td class="num">' + avail +
'</td><td class="sparkcell">' + spark + "</td><td" + errc + ">" + (has ? s.err5 : "–") +
'</td><td class="num lat">' + p50 + '</td><td class="num lat">' + p95 + "</td><td>" + esc(d.note) + "</td></tr>";
}
function render() {
if (!DATA) return;
var rows = DATA.slice(), i;
if (Q) {
var q = Q.toLowerCase();
rows = rows.filter(function (d) { return (d.host + " " + (d.note || "") + " " + (d.project || "")).toLowerCase().indexOf(q) >= 0; });
}
if (SORT) {
var k = SORT[0], dir = SORT[1];
rows.sort(function (a, b) {
var va, vb;
if (["avail", "err5", "p50", "p95"].indexOf(k) >= 0) { va = (a.stats || {})[k]; vb = (b.stats || {})[k]; }
else { va = a[k]; vb = b[k]; }
va = va == null ? -1 : va; vb = vb == null ? -1 : vb;
return (va < vb ? -1 : va > vb ? 1 : 0) * dir;
});
}
document.getElementById("rows").innerHTML = rows.map(row).join("");
document.getElementById("sparkhead").textContent = VIEW === "14d" ? "14 d, daily" : "24 h, hourly";
var up = 0; for (i = 0; i < DATA.length; i++) if (DATA[i].up) up++;
document.getElementById("sum").innerHTML = DATA.length + " names · " + up + " up · " + rows.length + " shown";
}
function tick() {
fetch("/json", { cache: "no-store" }).then(function (r) { return r.json(); }).then(function (d) {
DATA = d;
var m = document.querySelector('meta[http-equiv="refresh"]'); if (m) m.remove(); // hydration won: no more full reloads
render();
}).catch(function () {}); // proxy mid-reload or laptop asleep: keep the last paint, try again
}
setInterval(tick, 30000);
tick();
(function () {
var i, els = document.querySelectorAll("th[data-key]");
for (i = 0; i < els.length; i++) els[i].addEventListener("click", function () {
var k = this.getAttribute("data-key");
SORT = (SORT && SORT[0] === k) ? [k, -SORT[1]] : [k, 1];
render();
});
els = document.querySelectorAll("a[data-view]");
for (i = 0; i < els.length; i++) els[i].addEventListener("click", function () {
VIEW = this.getAttribute("data-view");
history.replaceState(null, "", VIEW === "14d" ? "?view=14d" : location.pathname);
render();
return false;
});
document.getElementById("q").addEventListener("input", function () { Q = this.value.trim(); render(); });
})();
</script>
</body></html>
"""
""")
def index_rows():
......@@ -363,8 +874,21 @@ def index_rows():
return [(h, e, up) for (h, e), up in zip(items, ups)]
def index_page():
def index_page(view="24h", lan=False):
rows = index_rows()
stats = host_stats()
lastups = last_up()
days = host_days() if view == "14d" else {}
def face(h):
"""(label, href) for one name: on the LAN face both become the https .mac.local form."""
if h.startswith("*."):
return h, None # a wildcard is documentation, not a browsable URL
if not lan:
return h, f"//{h}/"
lh = h[:-len(SUFFIX)] + LAN_SUFFIX
return lh, f"https://{lh}/"
groups = {}
for h, e, up in rows:
groups.setdefault(e.get("project") or h.split(".")[0], []).append((h, e, up))
......@@ -372,27 +896,76 @@ def index_page():
for proj in sorted(groups):
ents = sorted(groups[proj], key=lambda t: (t[1].get("service") or "", t[1].get("branch") or ""))
for h, e, up in ents: # main site first (no service), then branch variants, then sub-services
name = f'<a href="//{h}/">{h}</a>'
label, href = face(h)
name = f'<a href="{href}">{label}</a>' if href else f'<span class="muted">{label}</span>'
if e.get("branch"):
name += f' <span class="muted">⎇ {html.escape(e["branch"])}</span>'
if is_stale(h, e, up, lastups):
when = "never answered" if h not in lastups else "last ok " + time.strftime(
"%d %b %H:%M", time.localtime(lastups[h]))
name += (f' <span class="stale" title="stale: {when}; removing is safe — '
f'the agent that made it just re-ensures it if it needs it again '
f'(`devname gc` clears these in bulk)">stale</span>')
cls = "up" if up else "down"
s = stats.get(h)
if s and s["n"]:
avail = f'{s["avail"]:.1f}%'
err5 = str(s["err5"])
err_cls = ' class="num bad"' if s["err5"] else ' class="num"'
p50 = f'{s["p50"]}' if s["p50"] is not None else "–"
p95 = f'{s["p95"]}' if s["p95"] is not None else "–"
spark = daystrip_svg(days.get(h) or []) if view == "14d" else sparkline_svg(s["hours"])
n_title = f' title="{s["n"]} probes in 24 h"'
else: # no history yet (fresh entry or fresh db)
avail, err5, p50, p95, spark, err_cls, n_title = "–", "–", "–", "–", "", ' class="num"', ""
trs.append(f'<tr class="{cls}"><td class="name">{name}</td><td class="port">{e["port"]}</td>'
f'<td><span class="dot"></span>{"up" if up else "down"}</td>'
f'<td class="num"{n_title}>{avail}</td><td class="sparkcell">{spark}</td>'
f'<td{err_cls}>{err5}</td><td class="num lat">{p50}</td><td class="num lat">{p95}</td>'
f'<td>{html.escape(e.get("note") or "")}</td></tr>')
return PAGE.format(n_names=len(rows), n_up=sum(1 for *_, up in rows if up), rows="\n".join(trs))
lan_note = ('<b style="color:#d0483e">LAN face</b> — names served as <code>.mac.local</code> · '
'phone without the CA? fetch <a href="http://mac.local/CA">http://mac.local/CA</a> · ')
return PAGE.substitute(n_names=len(rows), n_up=sum(1 for *_, up in rows if up),
rows="\n".join(trs), poll_secs=POLL_SECS, view=view,
lan_note=lan_note if lan else "")
def json_records():
"""Everything the SPA needs per refresh: identity, live up, full stats (incl. hourly), 14 d, staleness."""
stats, lastups, days = host_stats(), last_up(), host_days()
return [{"host": h, "port": e["port"], "up": up, "project": e.get("project"),
"service": e.get("service"), "branch": e.get("branch"), "note": e.get("note") or "",
"created": e.get("created"), "created_by": e.get("created_by"),
"last_ok": lastups.get(h), "stale": is_stale(h, e, up, lastups),
"stats": stats.get(h), "days": days.get(h) or []}
for h, e, up in index_rows()]
class IndexHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path.split("?")[0] == "/json":
body, ctype = json.dumps(
[{"host": h, "port": e["port"], "up": up, "project": e.get("project"),
"service": e.get("service"), "branch": e.get("branch"), "note": e.get("note") or ""}
for h, e, up in index_rows()], indent=2) + "\n", "application/json"
path, _, qs = self.path.partition("?")
data = ctype = None
code = 200
if path in ("/rootCA.pem", "/CA"): # served over plain http on the LAN face: a phone that trusts no CA yet
root = subprocess.run([MKCERT, "-CAROOT"], capture_output=True, text=True).stdout.strip()
try:
with open(os.path.join(root, "rootCA.pem"), "rb") as fh:
data = fh.read()
ctype = "application/x-x509-ca-cert"
except OSError:
data, ctype, code = b"no mkcert root CA on this machine\n", "text/plain", 404
else:
# Caddy rewrites Host to the upstream, but X-Forwarded-Host carries the real name:
# reached as mac.local (or any .mac.local) = the LAN face, links must follow it.
xfh = (self.headers.get("X-Forwarded-Host") or "").split(":")[0]
lan = xfh == GATEWAY_HOST or xfh.endswith(LAN_SUFFIX)
if path == "/json":
data, ctype = (json.dumps(json_records(), indent=2) + "\n").encode(), "application/json"
else:
body, ctype = index_page(), "text/html; charset=utf-8"
data = body.encode()
self.send_response(200)
q = urllib.parse.parse_qs(qs)
data, ctype = (index_page("14d" if q.get("view") == ["14d"] else "24h", lan=lan).encode(),
"text/html; charset=utf-8")
self.send_response(code)
self.send_header("Content-Type", ctype)
self.send_header("Content-Length", str(len(data)))
self.send_header("Cache-Control", "no-store")
......@@ -421,6 +994,8 @@ def cmd_serve(a):
env = dict(os.environ, XDG_DATA_HOME=STATE, XDG_CONFIG_HOME=STATE, HOME=HOME)
caddy = subprocess.Popen([CADDY, "run", "--config", CADDYFILE, "--adapter", "caddyfile"], env=env)
start_index_server()
start_status_poller()
start_lan_announcer()
signal.signal(signal.SIGTERM, lambda *_: (caddy.terminate(), sys.exit(0)))
return caddy.wait()
......@@ -429,50 +1004,6 @@ def launchd_loaded():
return subprocess.run(["launchctl", "print", f"gui/{os.getuid()}/{LABEL}"], capture_output=True).returncode == 0
def systemd_unit():
"""User unit: the daemon never needs root; only binding :80/:443 does
(one sysctl or a setcap on caddy — see README)."""
return f"""[Unit]
Description=devname: stable local names for dev servers
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/bin/env python3 {os.path.abspath(__file__)} serve
Restart=on-failure
RestartSec=2
[Install]
WantedBy=default.target
"""
def systemd_active():
r = subprocess.run(["systemctl", "--user", "is-active", "devname.service"], capture_output=True, text=True)
return r.returncode == 0 and r.stdout.strip() == "active"
def agent_loaded():
return systemd_active() if sys.platform.startswith("linux") else launchd_loaded()
def cmd_agent_linux(a):
if a.action == "status":
print("active" if systemd_active() else "not active")
return 0 if systemd_active() else 1
unit_dir = os.path.expanduser("~/.config/systemd/user")
os.makedirs(unit_dir, exist_ok=True)
with open(os.path.join(unit_dir, "devname.service"), "w") as fh:
fh.write(systemd_unit())
for cmd in (["systemctl", "--user", "daemon-reload"],
["systemctl", "--user", "enable", "--now", "devname.service"]):
r = subprocess.run(cmd, capture_output=True, text=True)
if r.returncode:
raise Err("systemctl --user failed: " + r.stderr.strip()[-200:])
print("devname: systemd user unit installed and started", file=sys.stderr)
return 0
def cmd_doctor(a):
ok = True
......@@ -481,9 +1012,6 @@ def cmd_doctor(a):
ok = ok and good
print(("PASS " if good else "FAIL ") + text)
line(os.path.exists(CADDY), f"caddy at {CADDY}")
if sys.platform.startswith("linux"):
line(agent_loaded(), "systemd user unit devname.service active")
else:
line(launchd_loaded(), f"launchd agent {LABEL} loaded")
line(os.path.exists(ADMIN_SOCK), "caddy admin socket present")
try:
......@@ -506,13 +1034,18 @@ def cmd_doctor(a):
root = subprocess.run([MKCERT, "-CAROOT"], capture_output=True, text=True).stdout.strip()
line(os.path.exists(os.path.join(root, "rootCA.pem")), "mkcert root CA exists (https; trust it with `mkcert -install`)")
line(have_https(), "https cert issued for the registered names")
try:
with open(LAN_GATE) as fh:
st = json.load(fh)
age = int(time.time()) - st.get("checked", 0)
print(f" lan gate {'OPEN' if st.get('open') else 'closed'}, handshake {age}s ago ({st.get('detail', '')})")
except (OSError, ValueError):
print(" lan gate closed (announcer has not run yet)")
print(f"{len(load()['entries'])} names registered")
return 0 if ok else 1
def cmd_agent(a):
if sys.platform.startswith("linux"):
return cmd_agent_linux(a)
if a.action == "status":
print("loaded" if launchd_loaded() else "not loaded")
return 0
......@@ -550,12 +1083,19 @@ def main(argv=None):
e.add_argument("--branch", help="a branch name, or 'auto' = the cwd's git branch (main/master get no suffix)")
e.add_argument("--port", type=int, help="adopt an existing server's port instead of allocating")
e.add_argument("--force", action="store_true", help="with --port: remap an existing name")
e.add_argument("--alias", action="store_true",
help="with --port: allow several names on one port (a single server routing by Host)")
e.add_argument("--keep-host", action="store_true", help="do not rewrite Host to 127.0.0.1:<port> (default: rewrite)")
e.add_argument("--note")
e.add_argument("--format", choices=["url", "port", "json"], default="url")
e.set_defaults(fn=cmd_ensure)
s = sub.add_parser("ls"); s.add_argument("--json", action="store_true"); s.set_defaults(fn=cmd_ls)
s = sub.add_parser("rm"); s.add_argument("host"); s.set_defaults(fn=cmd_rm)
s = sub.add_parser("gc", help="remove stale names: down now, no successful probe in --idle-hours")
s.add_argument("hosts", nargs="*", help="restrict to these names; default: all stale ones")
s.add_argument("--idle-hours", type=int, default=STALE_HOURS)
s.add_argument("--yes", action="store_true", help="actually remove; without it, only list candidates")
s.set_defaults(fn=cmd_gc)
s = sub.add_parser("check"); s.add_argument("host"); s.set_defaults(fn=cmd_check)
sub.add_parser("render").set_defaults(fn=cmd_render)
sub.add_parser("serve").set_defaults(fn=cmd_serve)
......
......@@ -4,7 +4,9 @@ import io
import json
import os
import pathlib
import socket
import tempfile
import time
import unittest
from contextlib import redirect_stderr, redirect_stdout
......@@ -141,6 +143,19 @@ class DevnameTests(unittest.TestCase):
self.assertEqual(self.run_cli("rm", "gone")[0], 2)
self.assertEqual(json.loads(self.run_cli("ls", "--json")[1]), [])
def test_rm_resolves_a_unique_subservice_prefix(self):
self.run_cli("ensure", "www", "--service", "desk-p2", "--port", "8894")
rc, _, err = self.run_cli("rm", "desk-p2") # labels are whole words: desk-p2 does NOT match a hypothetical desk.*
self.assertEqual(rc, 0)
self.assertIn("removed desk-p2.www.localhost", err)
self.assertNotIn("desk-p2.www.localhost", self.dn.load()["entries"])
# the same service under two projects is ambiguous: `rm desk` must refuse
self.run_cli("ensure", "www", "--service", "desk", "--port", "8893")
self.run_cli("ensure", "sre", "--service", "desk", "--port", "8892")
rc, _, err = self.run_cli("rm", "desk")
self.assertEqual(rc, 2)
self.assertIn("ambiguous", err)
def test_registry_file_is_atomic_and_private(self):
self.run_cli("ensure", "demo")
conf = pathlib.Path(self._tmp.name)
......@@ -148,15 +163,229 @@ class DevnameTests(unittest.TestCase):
self.assertFalse((conf / "Caddyfile.tmp").exists())
self.assertEqual(oct(conf.stat().st_mode & 0o777), "0o700")
def seed_probes(self, host, rows):
now = int(time.time())
self.dn.status_db().executemany(
"INSERT INTO probe VALUES (?,?,?,?,?)",
[(now - age, host, port_up, code, ms) for age, port_up, code, ms in rows])
self.dn.status_db().commit()
def test_status_history_stats_math(self):
self.seed_probes("a.localhost", [
(60, 1, 200, 10), (60, 1, 502, 20), (60, 1, 200, 15), # one 5xx inside the window
(60, 0, None, None), # proxy dead: neither up nor 5xx
(90000, 1, 200, 5), # 25 h old: outside the 24 h window
])
s = self.dn.host_stats()["a.localhost"]
self.assertEqual((s["n"], s["up"], s["err5"]), (4, 2, 1))
self.assertEqual(s["avail"], 50.0)
self.assertEqual((s["p50"], s["p95"]), (15, 20))
def test_status_history_prune_drops_old_rows(self):
self.seed_probes("a.localhost", [(15 * 86400, 1, 200, 10), (60, 1, 200, 10)])
self.assertEqual(self.dn.poll_once(), 0) # empty registry: probes nothing, still prunes
n = self.dn.status_db().execute("SELECT COUNT(*) FROM probe").fetchone()[0]
self.assertEqual(n, 1)
def test_status_history_shows_on_the_page_and_json(self):
self.run_cli("ensure", "app", "--port", "8123")
self.seed_probes("app.localhost", [(60, 1, 200, 12), (120, 1, 503, 40), (180, 1, 200, 8)])
page = self.dn.index_page()
self.assertIn("66.7%", page)
self.assertIn(">1</td>", page) # the 5xx count, bad-colored cell still shows the number
self.assertIn("p50 ms", page)
self.assertIn("p95 ms", page)
self.assertIn('class="spark"', page) # the hourly sparkline renders
data = json.loads(self.run_cli("ls", "--json")[1]) # sanity: registry still fine
self.assertEqual(data[0]["host"], "app.localhost")
def test_status_history_absent_data_renders_dashes(self):
self.run_cli("ensure", "app", "--port", "8123")
page = self.dn.index_page()
tbody = page.split("<tbody")[1].split("</tbody>")[0]
self.assertNotIn('class="num bad"', tbody) # no cell may carry the bad class (the JS/CSS may mention it)
self.assertEqual(tbody.count("–</td>"), 4) # 24 h %, 5xx, p50, p95: four dashes, no crash
self.assertNotIn("class=\"spark\"", tbody) # and no empty sparkline
def test_hourly_buckets_feed_the_sparkline(self):
self.run_cli("ensure", "app", "--port", "8123")
now = int(time.time())
now_hour = now // 3600
hour = lambda back, off=60: (now_hour - back) * 3600 + off # absolute ts: hour `back` ago, immune to wall-clock minute
db = self.dn.status_db()
db.executemany("INSERT INTO probe VALUES (?,?,?,?,?)", [
(hour(0, 60), "app.localhost", 1, 200, 10),
(hour(0, 120), "app.localhost", 1, 503, 10),
(hour(0, 180), "app.localhost", 1, 200, 10), # current hour: 2 of 3 up
(hour(5), "app.localhost", 1, 503, 10), # 5 h ago: all down
(hour(30), "app.localhost", 1, 200, 10), # 30 h ago: outside the 24 h window
])
db.commit()
s = self.dn.host_stats()["app.localhost"]
self.assertEqual(len(s["hours"]), 24)
self.assertEqual(s["hours"][23], 66.7) # current hour: 2/3
self.assertEqual(s["hours"][18], 0.0) # 5 h ago (23 - 5)
self.assertIsNone(s["hours"][0]) # 23 h ago: no probes
self.assertEqual(sum(1 for x in s["hours"] if x is not None), 2)
page = self.dn.index_page()
self.assertIn("66.7%</title>", page) # the tooltip carries the number, not just the color
def test_sparkline_scales_honestly(self):
svg = self.dn.sparkline_svg([100.0, 50.0, 0.0, None] * 6, now=1730000000)
self.assertEqual(svg.count("<rect"), 24)
full_h, half_h = 18.0, 9.0 # 0-100 scale: full bar, half bar, stub, gray no-data
self.assertIn(f'height="{full_h}"', svg)
self.assertIn(f'height="{half_h}"', svg)
self.assertIn('height="1.0"', svg) # zero availability still shows a stub, not an invisible bar
self.assertEqual(svg.count('fill="#8882"'), 6) # the None hours
self.assertNotIn("height=\"0", svg) # never an invisible zero: the honest-scale rule
# stale names: badge, gc, provenance
def test_stale_badge_on_never_answered_down_names(self):
self.run_cli("ensure", "ghost", "--note", "left behind")
page = self.dn.index_page()
self.assertIn('class="stale"', page) # down + never answered: stale
self.assertIn("never answered", page)
self.assertIn("re-ensures", page) # the tooltip says removal is safe for the owning agent
# a listening port is never stale, whatever its history says
srv = socket.socket(); srv.bind(("127.0.0.1", 0)); srv.listen()
self.addCleanup(srv.close)
self.run_cli("ensure", "alive", "--port", str(srv.getsockname()[1]))
page = self.dn.index_page()
alive_row = page.split("alive.localhost")[1][:400]
self.assertNotIn('class="stale"', alive_row)
def test_gc_lists_then_removes_only_stale(self):
srv = socket.socket(); srv.bind(("127.0.0.1", 0)); srv.listen()
self.addCleanup(srv.close)
port = srv.getsockname()[1]
self.run_cli("ensure", "ghost", "--port", "17999") # nothing listens
self.run_cli("ensure", "alive", "--port", str(port)) # listening: untouchable
self.seed_probes("ghost.localhost", [(3600, 0, None, None)]) # never a success
rc, out, _ = self.run_cli("gc")
self.assertEqual(rc, 0)
self.assertIn("would remove ghost.localhost", out)
self.assertIn("alive.localhost", self.dn.load()["entries"])
rc, out, _ = self.run_cli("gc", "--yes")
self.assertEqual(rc, 0)
self.assertNotIn("ghost.localhost", self.dn.load()["entries"])
self.assertIn("alive.localhost", self.dn.load()["entries"])
def test_gc_spares_a_recent_success(self):
self.run_cli("ensure", "seasonal", "--port", "17998") # down now but answered an hour ago
self.seed_probes("seasonal.localhost", [(3600, 1, 200, 10)])
rc, out, _ = self.run_cli("gc")
self.assertIn("nothing stale", out)
def test_gc_host_filter_skips_a_wip_name(self):
self.run_cli("ensure", "ghost", "--port", "17997")
self.run_cli("ensure", "wip", "--port", "17996")
rc, out, _ = self.run_cli("gc", "ghost")
self.assertIn("would remove ghost.localhost", out)
self.assertNotIn("wip.localhost", out) # named targets only: the wip name is untouched
self.assertIn("wip.localhost", self.dn.load()["entries"])
def test_ensure_records_provenance(self):
self.run_cli("ensure", "demo")
e = self.dn.load()["entries"]["demo.localhost"]
self.assertIn("created", e)
self.assertIn("created_by", e)
# SPA: 14-day view, json feed for hydration
def test_host_days_math(self):
self.run_cli("ensure", "app", "--port", "8123")
now = int(time.time())
today = now - now % 3600 + 60 # safely inside today (local)
yesterday = today - 86400 # same clock time one day back
db = self.dn.status_db()
db.executemany("INSERT INTO probe VALUES (?,?,?,?,?)", [
(today, "app.localhost", 1, 200, 10),
(today, "app.localhost", 1, 503, 10),
(yesterday, "app.localhost", 1, 200, 10),
])
db.commit()
days = self.dn.host_days()["app.localhost"]
self.assertEqual(len(days), 14)
self.assertEqual(days[13][1], 50.0) # today: 1 of 2 ok
self.assertEqual(days[12][1], 100.0) # yesterday
self.assertIsNone(days[0][1]) # 13 d ago: nothing
strip = self.dn.daystrip_svg(days)
self.assertIn("50.0%</title>", strip) # the tooltip carries the number
self.assertNotIn('height="0"', strip)
def test_14d_view_renders_daystrip_and_toggle(self):
self.run_cli("ensure", "app", "--port", "8123")
page = self.dn.index_page("14d")
self.assertIn("14 d, daily", page) # the JS also flips this header at runtime
self.assertIn('data-view="14d"', page)
self.assertIn('id="q"', page) # the filter box is there even before hydration
self.assertIn("var VIEW = ", page) # the hydration script shipped
def test_json_records_carries_the_spa_fields(self):
self.run_cli("ensure", "app", "--port", "8123", "--note", "demo app")
now = int(time.time())
self.seed_probes("app.localhost", [(now - now % 3600 + 60 - now, 1, 200, 12)]) # inside the current hour
recs = self.dn.json_records()
self.assertEqual(len(recs), 1)
r = recs[0]
self.assertEqual(r["host"], "app.localhost")
self.assertTrue(r["up"] is False or r["up"] is True) # port check ran, nothing listens here
self.assertFalse(r["stale"]) # answered a minute ago: not stale, however down the port is
self.assertEqual(r["stats"]["hours"][23], 100.0)
self.assertEqual(len(r["days"]), 14)
self.assertEqual(r["note"], "demo app")
# LAN face: .mac.local sites behind the domovoy-handshake gate
def test_lan_blocks_only_render_when_the_gate_is_open(self):
reg = {"entries": {"bench.sre.localhost": {"port": 8895, "rewrite": True}}}
plain = self.dn.render_caddyfile(reg, True, lan=False)
self.assertNotIn("mac.local", plain)
lan = self.dn.render_caddyfile(reg, True, lan=True)
self.assertIn("https://bench.sre.mac.local {", lan)
self.assertIn("tls ", lan)
self.assertIn("not remote_ip " + self.dn.LAN_NETS, lan) # private-range guard, not loopback-only
self.assertIn("abort @notlan", lan)
self.assertIn("http://bench.sre.mac.local {", lan) # convenience redirect to https
self.assertIn("redir https://{host}{uri} 301", lan)
self.assertIn("http://mac.local {", lan) # gateway: CA over http, rest to https
self.assertIn("path /rootCA.pem /CA", lan) # the /CA shortcut rides the same door
self.assertIn("rewrite @ca /rootCA.pem", lan)
self.assertIn("reverse_proxy 127.0.0.1:16999", lan)
self.assertIn("https://mac.local {", lan)
self.assertEqual(lan.count("abort @notlan") + lan.count("abort @notlocal"),
lan.count("reverse_proxy")) # every site block, either face, is guarded
without_tls = self.dn.render_caddyfile(reg, False, lan=True)
self.assertNotIn("mac.local", without_tls) # no cert, no LAN face: https is the whole point there
def test_lan_gate_defaults_shut_and_follows_the_state_file(self):
self.assertFalse(self.dn.lan_gate_open())
self.dn.write_gate(True, "domovoy handshake ok")
self.assertTrue(self.dn.lan_gate_open())
self.dn.write_gate(False, "domovoy handshake failed")
self.assertFalse(self.dn.lan_gate_open())
def test_handshake_is_true_only_when_domovoy_authenticates(self):
from unittest import mock
real_run = self.dn.subprocess.run
with mock.patch.object(self.dn.subprocess, "run", return_value=mock.Mock(returncode=0)):
self.assertTrue(self.dn.handshake_ok())
with mock.patch.object(self.dn.subprocess, "run", return_value=mock.Mock(returncode=255)):
self.assertFalse(self.dn.handshake_ok())
with mock.patch.object(self.dn.subprocess, "run", side_effect=OSError("no network")):
self.assertFalse(self.dn.handshake_ok())
self.assertIn("domovoy-admin", self.dn.HANDSHAKE_ARGS)
self.assertIn("BatchMode=yes", self.dn.HANDSHAKE_ARGS) # never hangs on a passphrase prompt
def test_lan_page_rewrites_links_and_shows_the_ca_banner(self):
self.run_cli("ensure", "sre", "--service", "bench", "--port", "8895")
local = self.dn.index_page()
self.assertIn('href="//bench.sre.localhost/"', local)
self.assertNotIn("rootCA.pem", local)
lan = self.dn.index_page(lan=True)
self.assertIn('href="https://bench.sre.mac.local/"', lan)
self.assertIn("bench.sre.mac.local</a>", lan)
self.assertIn("http://mac.local/CA", lan)
if __name__ == "__main__":
unittest.main()
def test_systemd_unit_is_well_formed(self):
unit = self.dn.systemd_unit()
self.assertIn("[Unit]", unit)
self.assertIn("[Service]", unit)
self.assertIn("[Install]", unit)
self.assertIn(" serve", unit) # the same entrypoint the plist runs
self.assertIn("WantedBy=default.target", unit) # user unit, not system
self.assertNotIn("launchctl", unit)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment