312 lines
17 KiB
Python
312 lines
17 KiB
Python
#!/usr/bin/env python3
|
|
"""Configure, authenticate and verify the full Docker installation."""
|
|
import argparse
|
|
import importlib.util
|
|
import ipaddress
|
|
import re
|
|
import shutil
|
|
import json
|
|
import os
|
|
import tempfile
|
|
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, prompt=input):
|
|
self.directory = Path(directory)
|
|
self.runner = runner
|
|
self.prompt = prompt
|
|
self.accounts = []
|
|
|
|
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"
|
|
bind = str(ipaddress.IPv4Address(args.bind_address or "0.0.0.0"))
|
|
if settings.is_symlink():
|
|
raise ValueError("Refusing to modify a symlinked .env")
|
|
if settings.exists():
|
|
if any((args.ntfy_url, args.timezone, args.port)):
|
|
raise ValueError(".env already exists; edit it explicitly rather than replace existing configuration")
|
|
text = settings.read_text()
|
|
for key, value in (("NTFY_BIND_ADDRESS", bind),
|
|
("PIPELINES_BIND_ADDRESS", "0.0.0.0")):
|
|
if any(line.startswith(key + "=") for line in text.splitlines()):
|
|
text = "".join(key + "=" + value + "\n" if line.startswith(key + "=") else line
|
|
for line in text.splitlines(keepends=True))
|
|
else:
|
|
text = text.rstrip("\n") + "\n" + key + "=" + value + "\n"
|
|
if text != settings.read_text():
|
|
settings.write_text(text)
|
|
print(f"Docker binds: ntfy {bind}, Pipelines 0.0.0.0; existing URLs, timezone, ports and credentials retained.")
|
|
return
|
|
address = lan.detect_lan() if not args.ntfy_url else None
|
|
url = args.ntfy_url or f"http://{address}:8090"
|
|
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")
|
|
self.accounts = accounts
|
|
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.")
|
|
# Keep the catalog warm in the same service used by OpenCode's own
|
|
# interactive connect UI, not just the later validation process.
|
|
login = '''set -eu
|
|
opencode api get /api/integration >/dev/null
|
|
exec opencode auth login
|
|
'''
|
|
self.compose("run", "--rm", "--no-deps", "--entrypoint", "sh", "pipelines", "-c", login)
|
|
if not connected():
|
|
raise ValueError("Provider setup incomplete; rerun the installer to continue (credentials/volumes retained)")
|
|
|
|
def discover_models(self):
|
|
# A one-off container starts a fresh OpenCode service. Its first model
|
|
# read can race catalog initialization and return an empty successful
|
|
# response. Prime discovery and retry in THAT SAME container/service;
|
|
# starting another one-off container just repeats the cold-start race.
|
|
discovery = '''set -eu
|
|
opencode api get /api/integration >/dev/null
|
|
for attempt in 1 2 3; do
|
|
models=$(opencode models)
|
|
if [ -n "$models" ]; then
|
|
printf '%s\\n' "$models"
|
|
exit 0
|
|
fi
|
|
if [ "$attempt" -lt 3 ]; then sleep 1; fi
|
|
done
|
|
'''
|
|
result = self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "sh", "pipelines", "-c", discovery, capture=True)
|
|
return [line.strip() for line in result.stdout.splitlines()
|
|
if re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_./:-]+", line.strip())]
|
|
|
|
def validate_provider(self, args):
|
|
models = self.discover_models()
|
|
ids = {account.get("id") for account in self.accounts if isinstance(account,dict) and account.get("connections")}
|
|
available = [model for model in models if model.split("/",1)[0] in ids]
|
|
saved = dict(line.split("=",1) for line in (self.directory/".env").read_text().splitlines() if "=" in line)
|
|
model = args.model or saved.get("PIPELINES_DEFAULT_MODEL", "")
|
|
if not model:
|
|
if not available:
|
|
connected = ", ".join(sorted(str(id) for id in ids)) or "none"
|
|
visible = ", ".join(sorted({model.split('/', 1)[0] for model in models})) or "none"
|
|
raise ValueError(f"OpenCode catalog discovery found no models for connected providers [{connected}] (visible model providers: [{visible}]). Your saved key was not replaced; this is NOT proof it is invalid. Rerun ./install.sh; if it persists, share this message and the OpenCode version, not your key. No model was validated.")
|
|
print("Choose the model to validate (one small tool-free test; provider charges may apply):")
|
|
for i, item in enumerate(available[:25],1):
|
|
print(f" {i}. {item}")
|
|
choice = self.prompt("Model number or full provider/model: ").strip()
|
|
model = available[int(choice)-1] if choice.isdigit() and 1 <= int(choice) <= len(available) else choice
|
|
if model not in models:
|
|
raise ValueError("Selected model is not available in this OpenCode configuration. Choose a listed model with --model PROVIDER/MODEL.")
|
|
print(f"Validating {model} with one tool-free generation; the session is retained for debugging.")
|
|
validation = '''set -eu
|
|
opencode api get /api/integration >/dev/null
|
|
exec pipelines provider-check --model "$1" --yes --json --timeout 90s
|
|
'''
|
|
result = self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "sh", "pipelines",
|
|
"-c", validation, "pipelines-provider-check", model, capture=True, check=False)
|
|
try:
|
|
report = json.loads(result.stdout)
|
|
except ValueError:
|
|
raise ValueError("Provider validation could not run. Rebuild the image and rerun; setup is NOT complete.")
|
|
if not isinstance(report,dict) or result.returncode or report.get("healthy") is not True or not report.get("session_id"):
|
|
print(json.dumps(report, indent=2), file=sys.stderr)
|
|
raise ValueError("Provider validation FAILED. Fix the reported authentication/model/quota/service error, then rerun --relogin or --model PROVIDER/MODEL. Setup is NOT complete.")
|
|
path = self.directory/".env"
|
|
text = path.read_text()
|
|
key = "PIPELINES_DEFAULT_MODEL="
|
|
lines = text.splitlines(keepends=True)
|
|
updated = "".join(key+model+"\n" if line.startswith(key) else line for line in lines)
|
|
if not any(line.startswith(key) for line in lines):
|
|
updated = updated.rstrip("\n")+"\n"+key+model+"\n"
|
|
path.write_text(updated)
|
|
print(f"Provider/model validated: {model} · session {report.get('session_id','unknown')}")
|
|
|
|
def update_host_client(self):
|
|
if not shutil.which("go"):
|
|
print("Host Go is unavailable: Docker binaries are updated, but an existing host CLI/TUI is NOT updated. Install/update that client before expecting new UI behavior.")
|
|
return
|
|
for command in (["go","install","-trimpath","./cmd/..."], ["go","run","./cmd/pipelines","skill","install"]):
|
|
result = self.runner(command, cwd=self.directory)
|
|
if result.returncode:
|
|
raise ValueError("Host client update failed; Docker setup is incomplete")
|
|
print("Updated host CLI/TUI using go install. Restart the running TUI; an already-running process keeps its old code.")
|
|
|
|
def publish_address(self):
|
|
address = lan.detect_lan()
|
|
path = self.directory/".env"
|
|
text = path.read_text()
|
|
key = "PIPELINES_HOST_IP="
|
|
lines = text.splitlines(keepends=True)
|
|
updated = "".join(key+address+"\n" if line.startswith(key) else line for line in lines)
|
|
if not any(line.startswith(key) for line in lines):
|
|
updated = updated.rstrip("\n")+"\n"+key+address+"\n"
|
|
path.write_text(updated)
|
|
|
|
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 reception is 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() or not 1 <= int(port) <= 65535:
|
|
raise ValueError("Could not identify the published Pipelines port")
|
|
endpoint = f"ws://127.0.0.1:{port}/connect"
|
|
self.verify_host_endpoint(endpoint)
|
|
self.configure_client(endpoint)
|
|
print(f"Host CLI/TUI connection: pipelines --url {endpoint} tui")
|
|
print("Install host client binaries with ./install.sh --native if needed.")
|
|
|
|
def verify_host_endpoint(self, endpoint):
|
|
if shutil.which("go"):
|
|
command = ["go", "run", "./cmd/pipelines", "--url", endpoint, "status", "--json"]
|
|
elif shutil.which("pipelines"):
|
|
command = [shutil.which("pipelines"), "--url", endpoint, "status", "--json"]
|
|
else:
|
|
print("Host-side server connection not tested: no host Go or pipelines CLI available.")
|
|
return
|
|
result = self.runner(command, cwd=self.directory, capture_output=True, text=True, timeout=60)
|
|
try:
|
|
status = json.loads(result.stdout)
|
|
except ValueError:
|
|
raise ValueError("Host client could not inspect the published Docker server; setup is NOT complete")
|
|
if (result.returncode or not isinstance(status, dict)
|
|
or not isinstance(status.get("ntfy"), dict)
|
|
or status["ntfy"].get("health") != "healthy"):
|
|
raise ValueError("Host client did not reach the verified ntfy-enabled Docker server; check the published port before using the TUI")
|
|
print("Verified: host client reaches the ntfy-enabled Docker server at " + endpoint)
|
|
|
|
def configure_client(self, endpoint):
|
|
root = Path(os.environ.get("XDG_CONFIG_HOME") or Path.home()/".config")
|
|
directory = root/"pipelines"
|
|
path = directory/"client.json"
|
|
if path.is_symlink():
|
|
raise ValueError("Refusing to modify symlinked client.json; use the printed --url explicitly")
|
|
existing = json.loads(path.read_text()) if path.exists() else {}
|
|
if not isinstance(existing, dict):
|
|
raise ValueError("client.json must contain an object; existing configuration was not replaced")
|
|
existing["server_url"] = endpoint
|
|
directory.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
temporary = None
|
|
try:
|
|
with tempfile.NamedTemporaryFile(mode="w", dir=directory, delete=False) as file:
|
|
temporary = Path(file.name)
|
|
os.chmod(temporary, 0o600)
|
|
json.dump(existing, file, indent=2)
|
|
file.write("\n")
|
|
os.replace(temporary, path)
|
|
finally:
|
|
if temporary is not None:
|
|
temporary.unlink(missing_ok=True)
|
|
print(f"Saved Docker client default: {endpoint} ({path}). Restart the TUI.")
|
|
override = os.environ.get("PIPELINES_URL", "")
|
|
if override and override != endpoint:
|
|
print("WARNING: PIPELINES_URL overrides the saved Docker address. Use the printed --url or update that variable; otherwise the TUI may still reach a different server.")
|
|
|
|
def run(self, args):
|
|
self.compose("version", capture=True)
|
|
self.settings(args)
|
|
self.publish_address()
|
|
self.update_host_client()
|
|
# 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)
|
|
self.validate_provider(args)
|
|
# 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 ntfy's default 0.0.0.0 bind address")
|
|
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")
|
|
parser.add_argument("--model", help="Provider/model for actual generation validation (saved after success)")
|
|
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}")
|