150 lines
8.1 KiB
Python
150 lines
8.1 KiB
Python
#!/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")
|
|
lan = module("lan_address", "lan-address.py")
|
|
|
|
|
|
class Setup:
|
|
def __init__(self, directory, runner=subprocess.run):
|
|
self.directory = Path(directory)
|
|
self.runner = runner
|
|
|
|
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
|
|
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 = "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"
|
|
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(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]
|
|
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", 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)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
Setup(Path(__file__).resolve().parents[1]).run(arguments())
|
|
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}")
|