#!/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") build_metadata = module("build_metadata", "build_metadata.py") release_lookup = module("resolve_releases", "resolve-releases.py") # Exact executable version formats reported by the built image. Anything with a # banner, suffix or extra token is rejected instead of being partially parsed. # OpenCode's `--version` prints its executable name before the version token # ("opencode v2.0.26", verified probe output), so accept exactly that optional # lowercase prefix in addition to the bare/v-prefixed token; unrelated banners # such as "OpenCode v2.0.26" or trailing text are still rejected. The patch # component accepts arbitrary digits (valid/future versions could report e.g. # 260 or 100; these are illustrative examples, not verified actual releases); # verify_harness_versions still rejects a parsed 260 when the expected value is # 26 by exact string equality. HARNESS_VERSION_FORMATS = { "opencode": re.compile(r"(?:opencode )?v?(\d+\.\d+\.\d+)"), "pi": re.compile(r"(\d+\.\d+\.\d+)"), "node": re.compile(r"v?(\d+\.\d+\.\d+)"), } def parse_harness_version(name, value): """Accept only the exact known version format for a harness executable.""" pattern = HARNESS_VERSION_FORMATS.get(name) if pattern is None: raise ValueError(f"Unknown harness executable {name!r}; refusing to parse an unchecked version") match = pattern.fullmatch((value or "").strip()) if not match: raise ValueError(f"Unexpected {name} version output {value!r}; refusing to continue on an unverified image") return match.group(1) class Setup: def __init__(self, directory, runner=subprocess.run, prompt=input, resolver=None): self.directory = Path(directory) self.runner = runner self.prompt = prompt self.resolver = resolver or release_lookup.resolve 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 choose_model(self, models, title, purpose="", step="", current=""): # Keep terminal stdin free for the fuzzy picker; catalog/result use private # files. The picker always runs inside the configured Docker installation so # it never executes a host harness, even when host Go is available. with tempfile.TemporaryDirectory(prefix=".pipelines-picker-", dir=self.directory) as temporary: root = Path(temporary) catalog, result = root/"models.json", root/"selected.txt" catalog.write_text(json.dumps(models)) catalog.chmod(0o600) options = ["--title", title] if purpose: options += ["--purpose", purpose] if step: options += ["--step", step] if current: options += ["--current", current] self.compose("run", "--rm", "--no-deps", "--entrypoint", "pipelines", "pipelines", "choose-model", "--options-file", str(catalog), "--output", str(result), *options) selected = result.read_text().strip() if selected not in models: raise ValueError("Model selection did not produce an available model; defaults unchanged") return selected 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", "") harness_model = args.harness_model or saved.get("PIPELINES_HARNESS_MODEL", "") # Fresh setup selects the interactive model first and the new-pipeline # model second, matching `pipelines harness setup` (and reconfiguration). if (not model or not harness_model) and 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; if it persists, share this message and the OpenCode version, not your key. No model was validated.") if not harness_model: harness_model = self.choose_model( available, "Choose the interactive OpenCode harness model", purpose="Runs your interactive OpenCode authoring and debugging sessions. It does not change new-pipeline-stage models.", step="Step 1 of 2 · interactive model", current=saved.get("PIPELINES_HARNESS_MODEL", "")) if not model: model = self.choose_model( available, "Choose the default model for new pipelines", purpose="Runs model stages in new pipelines and starters. Stored separately from the interactive harness model.", step="Step 2 of 2 · new-pipeline-stage model", current=saved.get("PIPELINES_DEFAULT_MODEL", "")) if model not in models: raise ValueError("Selected model is not available in this OpenCode configuration. Choose a listed model with --model PROVIDER/MODEL.") if harness_model not in models: raise ValueError("Selected interactive harness model is unavailable; defaults unchanged") 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) lines = path.read_text().splitlines(keepends=True) key = "PIPELINES_HARNESS_MODEL=" updated = "".join(key+harness_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+harness_model+"\n" path.write_text(updated) setup_options = ["--model", harness_model, "--pipeline-model", model] versions = getattr(self, "harness_version_label", "") if versions: setup_options += ["--versions", versions] self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "pipelines", "pipelines", "harness", "setup", "opencode", *setup_options) self.selected_harness_model, self.selected_pipeline_model = harness_model, model 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/pipelines","./cmd/pipelines-tui"], ["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 configure_workspaces(self): home = str(Path.home().resolve()) path = self.directory/".env" values = configure.host_workspaces(self.directory) lines = path.read_text().splitlines(keepends=True) text = "".join(key+"="+values[key]+"\n" if (key:=line.split("=",1)[0]) in values else line for line in lines) for key,value in values.items(): if not any(line.startswith(key+"=") for line in lines): text=text.rstrip("\n")+"\n"+key+"="+value+"\n" path.write_text(text) print("Host repositories mounted at their original paths under " + home + "; server runs with host UID/GID. Stage tool/edit permissions still apply.") def prepare_volume_ownership(self): # Existing volumes may belong to the old image UID 10001. Change only # ownership on container-managed data/auth volumes, never the host home. resolved = json.loads(self.compose("config", "--format", "json", capture=True).stdout) mounts = resolved.get("services", {}).get("pipelines", {}).get("volumes", []) for target in ("/data", "/home/pipelines", "/workspace"): if not any(mount.get("target") == target and mount.get("type") == "volume" for mount in mounts): raise ValueError("Refusing ownership migration: " + target + " is not a managed Docker volume; no host directory ownership was changed") self.compose("run", "--rm", "--no-deps", "-T", "--user", "0:0", "--entrypoint", "sh", "pipelines", "-c", 'set -eu; chown -R "$PIPELINES_UID:$PIPELINES_GID" /data /home/pipelines /workspace') def verify_workspace(self): directory = str(self.directory.resolve()) self.compose("exec", "-T", "pipelines", "sh", "-c", 'set -eu; test -d "$1"; test -r "$1"; test -x "$1"; cd "$1"; test "$(pwd -P)" = "$1"', "pipelines-workspace-check", directory, capture=True) print("Verified: Docker server can enter the host installation repository at " + directory) def resolve_releases(self): fixture = os.environ.get("PIPELINES_RELEASE_FIXTURE") try: if fixture: return release_lookup.load_fixture(fixture) return self.resolver() except release_lookup.ReleaseError as error: raise ValueError( f"Could not resolve current harness releases ({error}); keeping the last verified Dockerfile pins. " "Rerun when the registry/npm is reachable; refusing to silently install stale copies.") from None def reconfigure_harness(self, name, options): # Reuse the setup resolution/verification path without rebuilding or # restarting services: a stale image needs an explicit ./install upgrade. releases = self.resolve_releases() build_identity = self.build_identity() if self.needs_image_build(releases, build_identity): raise ValueError( f"The Docker image does not match the current resolved releases " f"(OpenCode {releases['opencode']['version']}, Pi {releases['pi']['version']}); " "run ./install to rebuild it, then rerun this reconfiguration") found = self.verify_harness_versions(releases) or {} label = self.harness_versions_label({ "opencode": found.get("opencode", releases["opencode"]["version"]), "pi": found.get("pi", releases["pi"]["version"]), "node": found.get("node", ""), }) self.compose("exec", "pipelines", "pipelines", "harness", "setup", name, "--login", "--versions", label, *options) def link_host(self, name=None, config=None): # Shared by fresh setup and --opencode/--pi reconfiguration. The host # launcher is linked from the container profile; it is never executed # from a host harness runtime. if config is None: config = json.loads(self.compose( "exec", "-T", "pipelines", "pipelines", "harness", "list", "--json", capture=True).stdout) name = name or config["default"] profile = config["profiles"][name] if shutil.which("go"): executable = ["go", "run", "./cmd/pipelines"] elif shutil.which("pipelines"): executable = [shutil.which("pipelines")] else: print("Host harness not linked: rerun ./install with host Go available to build the Docker client.") return args = [*executable, "harness", "link", name, "--docker-directory", str(self.directory.resolve()), "--model", profile["model"], "--pipeline-model", profile["pipeline_model"]] if config.get("default") == name: args.append("--default") result = self.runner(args, cwd=self.directory) if result.returncode: raise ValueError("Host harness link failed; container configuration retained") def release_receipt_path(self): return self.directory / ".pipelines-releases.json" def read_release_receipt(self): path = self.release_receipt_path() if path.is_symlink(): raise ValueError("Refusing to read a symlinked release receipt") if not path.exists(): return {} try: data = json.loads(path.read_text()) except ValueError: return {} return data if isinstance(data, dict) else {} def record_release_receipt(self, releases, build_identity=None): path = self.release_receipt_path() if path.is_symlink(): raise ValueError("Refusing to replace a symlinked release receipt") data = dict(releases) if build_identity: data["build"] = dict(build_identity) encoded = json.dumps(data, indent=2, sort_keys=True) + "\n" handle, temporary = tempfile.mkstemp(dir=str(self.directory), prefix=".pipelines-releases-") try: os.chmod(temporary, 0o600) with os.fdopen(handle, "w") as file: file.write(encoded) os.replace(temporary, path) finally: if os.path.exists(temporary): os.unlink(temporary) def release_build_args(self, releases): return build_metadata.release_args(releases) def build_identity(self): args = build_metadata.docker_args(self.directory, self.runner) return dict(arg.split("=", 1) for arg in args if arg != "--build-arg") def image_identity(self, image_id): # Read the actual built image's labels rather than trusting the receipt # file, so an unrelated/stale image cannot satisfy the build decision. result = self.runner(["docker", "image", "inspect", image_id, "--format", "{{json .Config.Labels}}"], cwd=self.directory, capture_output=True, text=True) if result.returncode or not result.stdout.strip(): return {} try: labels = json.loads(result.stdout) except ValueError: return {} return labels if isinstance(labels, dict) else {} def expected_labels(self, releases, build_identity=None): labels = { "net.a-wan.pipelines.opencode-version": releases["opencode"]["version"], "net.a-wan.pipelines.opencode-digest": releases["opencode"]["digest"], "net.a-wan.pipelines.pi-version": releases["pi"]["version"], } if build_identity: labels["net.a-wan.pipelines.source-version"] = build_identity.get("PIPELINES_VERSION", "") labels["net.a-wan.pipelines.source-commit"] = build_identity.get("PIPELINES_COMMIT", "") labels["net.a-wan.pipelines.source-dirty"] = build_identity.get("PIPELINES_DIRTY", "") return labels def receipt_matches(self, receipt, releases, build_identity=None): if not isinstance(receipt, dict): return False opencode, pi = receipt.get("opencode"), receipt.get("pi") if not isinstance(opencode, dict) or not isinstance(pi, dict): # A corrupted receipt must fail closed and rebuild, not raise. return False if (opencode.get("version") != releases["opencode"]["version"] or opencode.get("digest") != releases["opencode"]["digest"] or pi.get("version") != releases["pi"]["version"]): return False return not build_identity or receipt.get("build") == build_identity def needs_image_build(self, releases, build_identity=None, force_dirty=False): # A dirty tree has no content identity: the same commit/version/dirty # strings can describe different bytes. During install, always invoke the # Docker build for a dirty source and let Docker layer caching decide. The # reconfiguration path (force_dirty=False) deliberately checks only the # verified release image so a model/login-only change is not a rebuild: # source identity is compared only for a full install. if force_dirty and build_identity and build_identity.get("PIPELINES_DIRTY") == "true": return True identity = build_identity if force_dirty else None images = self.compose("images", "-q", "pipelines", capture=True, check=False).stdout.strip().splitlines() if not images: return True if not self.receipt_matches(self.read_release_receipt(), releases, identity): return True labels = self.image_identity(images[0].strip()) return not all(labels.get(key) == value for key, value in self.expected_labels(releases, identity).items()) def verify_harness_versions(self, releases): # One bounded in-image check per harness; fail closed on stale/missing. probe = '''set -eu printf 'opencode=%s\\n' "$(opencode --version)" printf 'pi=%s\\n' "$(pi --version)" printf 'node=%s\\n' "$(node --version)" ''' result = self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "sh", "pipelines", "-c", probe, capture=True) found = {} for line in result.stdout.splitlines(): name, separator, value = line.strip().partition("=") if separator and name in HARNESS_VERSION_FORMATS: found[name] = parse_harness_version(name, value) expected_opencode = releases["opencode"]["version"] expected_opencode = expected_opencode[1:] if expected_opencode.startswith("v") else expected_opencode expected_pi = releases["pi"]["version"] expected_pi = expected_pi[1:] if expected_pi.startswith("v") else expected_pi if found.get("opencode") != expected_opencode or found.get("pi") != expected_pi: raise ValueError( f"Built image does not report the resolved harness versions (expected OpenCode {expected_opencode}, " f"Pi {expected_pi}); refusing to continue on a stale or mismatched image") engine = releases["pi"].get("engine", "") if engine: if "node" not in found: raise ValueError(f"Could not verify Node compatibility for Pi ({engine}); refusing to continue") if not release_lookup.node_satisfies(engine, found["node"]): raise ValueError(f"Built image Node {found['node']} does not satisfy Pi's required engine {engine}") print(f"Verified image harnesses: OpenCode {expected_opencode}, Pi {expected_pi}" + (f", Node {found['node']} ({engine})" if engine else "")) return found def harness_versions_label(self, found): # Rendered by the Go onboarding wizard; no extra probe or subprocess. parts = [f"OpenCode {found.get('opencode', '?')}", f"Pi {found.get('pi', '?')}"] if found.get("node"): parts.append(f"Node {found['node']}") return " · ".join(parts) def verify_build(self, expected): result = self.compose("exec", "-T", "pipelines", "pipelines", "--url", "ws://127.0.0.1:8080/connect", "ping", "--json", capture=True) try: pong = json.loads(result.stdout) build = pong["build"] if (pong["type"] != "pong" or build["commit"] != expected["PIPELINES_COMMIT"] or build["version"] != expected["PIPELINES_VERSION"] or build["dirty"] is not (expected["PIPELINES_DIRTY"] == "true")): raise ValueError("build mismatch") except (ValueError, KeyError, TypeError): raise ValueError("Running server does not match the built source; setup is NOT complete. Check docker compose logs pipelines") from None print("Verified running server build: " + build["version"] + " (" + build["commit"] + ")") 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("Run ./install with host Go available to install/update the Docker CLI/TUI clients.") 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) # Refuse an unidentifiable source archive before changing settings/services. build_args = build_metadata.docker_args(self.directory, self.runner) expected_build = dict(arg.split("=", 1) for arg in build_args if arg != "--build-arg") # Resolve current releases before touching settings/services; a failure # keeps the verified pins and stops rather than installing stale copies. releases = self.resolve_releases() self.settings(args) self.publish_address() self.configure_workspaces() 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() if self.needs_image_build(releases, expected_build, force_dirty=True): self.compose("build", *build_args, *self.release_build_args(releases), "pipelines") found = self.verify_harness_versions(releases) or {} self.record_release_receipt(releases, expected_build) else: print("Keeping the existing image: resolved harness versions, digest and source identity are unchanged.") found = {} self.harness_version_label = self.harness_versions_label({ "opencode": found.get("opencode", releases["opencode"]["version"]), "pi": found.get("pi", releases["pi"]["version"]), "node": found.get("node", ""), }) self.compose("stop", "--timeout", "60", "pipelines") self.prepare_volume_ownership() self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "pipelines", "pipelines", "skill", "install") if args.harness == "opencode": self.authenticate(args.relogin) self.validate_provider(args) else: options = ["--default", "--login", "--versions", self.harness_version_label] if args.model: options += ["--pipeline-model", args.model] if args.harness_model: options += ["--model", args.harness_model] self.compose("run", "--rm", "--no-deps", "--entrypoint", "pipelines", "pipelines", "harness", "setup", "pi", *options) # Recreate to reload file secrets and environment, without deleting volumes. self.compose("up", "-d", "--force-recreate", "--wait", "--wait-timeout", "60", "pipelines") self.verify_build(expected_build) self.verify_workspace() self.verify() if shutil.which("go") or shutil.which("pipelines"): self.link_host() 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)") parser.add_argument("--harness-model", help="Separate interactive OpenCode model default (catalog validation only)") parser.add_argument("--harness", choices=["opencode", "pi"], default="opencode", help="Harness for fresh Docker setup; Pi skips OpenCode login/inference validation") 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, subprocess.CalledProcessError, configure.ZoneInfoNotFoundError) as error: sys.exit(f"Docker setup incomplete: {error}")