#!/usr/bin/env python3 """Configure, authenticate and verify the full Docker installation.""" import argparse import importlib.util import json from pathlib import Path import subprocess import sys def module(name, file): spec = importlib.util.spec_from_file_location(name, Path(__file__).with_name(file)) result = importlib.util.module_from_spec(spec) spec.loader.exec_module(result) return result configure = module("configure_compose", "configure-compose.py") tokens = module("save_ntfy_token", "save-ntfy-token.py") class Setup: def __init__(self, directory, runner=subprocess.run, prompt=input): self.directory = Path(directory) self.runner, self.prompt = runner, prompt def compose(self, *args, capture=False, check=True, **kwargs): result = self.runner(["docker", "compose", *args], cwd=self.directory, capture_output=capture, text=True, **kwargs) if check and result.returncode: # Captured ntfy output may contain tokens. Never replay it on failure. raise ValueError(f"Docker setup step failed (exit {result.returncode}); no credentials were replaced") return result def settings(self, args): settings = self.directory / ".env" if settings.exists(): if any((args.ntfy_url, args.bind_address, args.timezone, args.port)): 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") timezone = args.timezone if not timezone: try: timezone = configure.host_timezone() except ValueError: timezone = self.prompt("Timezone (e.g. Europe/London): ").strip() configure.configure(self.directory, url, bind, timezone, args.port or 8081) def bootstrap_ntfy(self): token = self.directory / "secrets/ntfy-token.txt" if token.is_symlink(): raise ValueError("Refusing a symlink for the publishing token") if token.exists() and token.stat().st_size: print("Keeping existing ntfy publishing token.") else: print("Set a password for ntfy's pipeline-publisher account (not a model-provider account).") self.compose("exec", "ntfy", "ntfy", "user", "add", "--ignore-exists", "pipeline-publisher") self.compose("exec", "-T", "ntfy", "ntfy", "access", "pipeline-publisher", "pipelines", "write-only") result = self.compose("exec", "-T", "ntfy", "ntfy", "token", "add", "pipeline-publisher", capture=True) tokens.save_token(result.stdout + result.stderr, self.directory) # --ignore-exists preserves the phone account's password on re-install. print("Set the phone-reader password if this is a new ntfy account; existing accounts are preserved.") self.compose("exec", "ntfy", "ntfy", "user", "add", "--ignore-exists", "phone-reader") self.compose("exec", "-T", "ntfy", "ntfy", "access", "phone-reader", "pipelines", "read-only") def authenticate(self, relogin=False): def connected(): result = self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "opencode", "pipelines", "auth", "list", "--format", "json", capture=True) accounts = json.loads(result.stdout) if not isinstance(accounts, list): raise ValueError("Unexpected OpenCode account-list format; authentication not verified") return any(isinstance(account, dict) and account.get("connections") for account in accounts) if connected() and not relogin: print("Keeping existing OpenCode provider setup in its Docker volume.") return print("Choose and authenticate your provider in the container's OpenCode login below.") self.compose("run", "--rm", "--no-deps", "--entrypoint", "opencode", "pipelines", "auth", "login") if not connected(): raise ValueError("Provider setup incomplete; rerun the installer to continue (credentials/volumes retained)") def verify(self): # Read the token inside the container; never put it in argv or output. probe = '''set -eu test -s "$PIPELINES_NTFY_TOKEN_FILE" printf 'header = "Authorization: Bearer %s"\\n' "$(cat "$PIPELINES_NTFY_TOKEN_FILE")" | curl --config - --fail --silent --show-error --max-time 10 --output /dev/null \ --data 'Pipelines Docker setup: authenticated publishing verified.' "$PIPELINES_NTFY_URL" ''' self.compose("exec", "-T", "pipelines", "sh", "-c", probe, capture=True) result = self.compose("exec", "-T", "pipelines", "pipelines", "--url", "ws://127.0.0.1:8080/connect", "status", "--json", capture=True) status = json.loads(result.stdout) if (status.get("ntfy", {}).get("health") != "healthy" 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("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] if not port.isdigit(): raise ValueError("Could not identify the published Pipelines port") print(f"Host CLI/TUI connection: pipelines --url ws://127.0.0.1:{port}/connect tui") print("Install host client binaries with ./install.sh --native if needed.") def run(self, args): self.compose("version", capture=True) self.settings(args) # Validate the actual resolved Compose configuration before starting anything. self.compose("config", "--quiet", capture=True) self.compose("up", "-d", "--wait", "--wait-timeout", "60", "ntfy") self.bootstrap_ntfy() self.compose("build", "pipelines") self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "pipelines", "pipelines", "skill", "install") self.authenticate(args.relogin) # Recreate to reload file secrets and environment, without deleting volumes. self.compose("up", "-d", "--force-recreate", "--wait", "--wait-timeout", "60", "pipelines") self.verify() print("Docker setup complete. Use docker compose ps / logs to manage it; no OS user service was registered.") 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("--port", type=int) parser.add_argument("--relogin", action="store_true", help="open OpenCode provider login again without deleting accounts") return parser.parse_args(argv) if __name__ == "__main__": try: Setup(Path(__file__).resolve().parents[1]).run(arguments()) except (ValueError, OSError, EOFError, json.JSONDecodeError) as error: sys.exit(f"Docker setup incomplete: {error}")