Automatically configure LAN networking without installer prompts

This commit is contained in:
2026-10-09 22:04:17 +01:00
parent a3e4d7817a
commit de3a3c2363
8 changed files with 219 additions and 17 deletions
+18 -12
View File
@@ -17,12 +17,13 @@ def module(name, file):
configure = module("configure_compose", "configure-compose.py")
tokens = module("save_ntfy_token", "save-ntfy-token.py")
lan = module("lan_address", "lan-address.py")
class Setup:
def __init__(self, directory, runner=subprocess.run, prompt=input):
def __init__(self, directory, runner=subprocess.run):
self.directory = Path(directory)
self.runner, self.prompt = runner, prompt
self.runner = runner
def compose(self, *args, capture=False, check=True, **kwargs):
result = self.runner(["docker", "compose", *args], cwd=self.directory,
@@ -39,17 +40,19 @@ class Setup:
raise ValueError(".env already exists; edit it explicitly rather than replace existing configuration")
print("Keeping existing .env and credentials.")
return
url = args.ntfy_url or self.prompt("Phone-facing ntfy base URL (HTTPS or trusted LAN/VPN, no topic): ").strip()
bind = args.bind_address or self.prompt("ntfy bind IP (LAN/VPN IP for direct phone access; 127.0.0.1 for reverse proxy): ").strip()
if not bind:
raise ValueError("Choose an explicit ntfy bind address; setup will not expose it by guessing")
address = lan.detect_lan() if not args.ntfy_url else None
url = args.ntfy_url or f"http://{address}:8090"
bind = args.bind_address or address or "127.0.0.1"
timezone = args.timezone
if not timezone:
try:
timezone = configure.host_timezone()
except ValueError:
timezone = self.prompt("Timezone (e.g. Europe/London): ").strip()
timezone = "UTC"
print("Host timezone unavailable; using UTC (override with --timezone if needed).")
configure.configure(self.directory, url, bind, timezone, args.port or 8081)
print(f"LAN notifications configured: {url} (bind {bind}; authentication required).")
print("Use this address on your phone on the same trusted LAN; do not forward this HTTP port to the Internet.")
def bootstrap_ntfy(self):
token = self.directory / "secrets/ntfy-token.txt"
@@ -101,7 +104,7 @@ printf 'header = "Authorization: Bearer %s"\\n' "$(cat "$PIPELINES_NTFY_TOKEN_FI
or not status.get("ntfy", {}).get("url") or status.get("topic") != "pipelines"):
raise ValueError("Pipelines did not report healthy ntfy; check docker compose logs pipelines ntfy")
print("Verified: Pipelines reports healthy ntfy and authenticated publishing succeeded.")
print("On your phone: add the ntfy server from .env, log in as phone-reader, subscribe to pipelines.")
print(f"On your phone: add {status['ntfy']['url']}, log in as phone-reader, subscribe to pipelines.")
print("Phone delivery and model inference are not verified by this setup check.")
address = self.compose("port", "pipelines", "8080", capture=True).stdout.strip()
port = address.rsplit(":", 1)[-1]
@@ -128,9 +131,9 @@ printf 'header = "Authorization: Bearer %s"\\n' "$(cat "$PIPELINES_NTFY_TOKEN_FI
def arguments(argv=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--ntfy-url")
parser.add_argument("--bind-address")
parser.add_argument("--timezone")
parser.add_argument("--ntfy-url", help="Override the auto-detected LAN ntfy address")
parser.add_argument("--bind-address", help="Override the detected LAN bind IP")
parser.add_argument("--timezone", help="Override the detected timezone (fallback: UTC)")
parser.add_argument("--port", type=int)
parser.add_argument("--relogin", action="store_true", help="open OpenCode provider login again without deleting accounts")
return parser.parse_args(argv)
@@ -139,5 +142,8 @@ def arguments(argv=None):
if __name__ == "__main__":
try:
Setup(Path(__file__).resolve().parents[1]).run(arguments())
except (ValueError, OSError, EOFError, json.JSONDecodeError) as error:
except KeyboardInterrupt:
print("\nSetup canceled; existing configuration, credentials and Docker volumes were retained.", file=sys.stderr)
sys.exit(130)
except (ValueError, OSError, EOFError, json.JSONDecodeError, configure.ZoneInfoNotFoundError) as error:
sys.exit(f"Docker setup incomplete: {error}")