#!/usr/bin/env python3 """One per-user lifecycle for the server, authenticated web proxy and ntfy.""" import argparse import fcntl import json import os from pathlib import Path import platform import plistlib import re import shutil import signal import subprocess import sys import time import urllib.request DEFAULT_PREFIX = Path.home() / ".local/share/pipelines" MARKER = "pipelines-install.json" SERVICE_MARKER = "Managed by Pipelines native installer" def ntfy_mode(config): """Native ntfy is selected by the manifest; absent/explicit docker uses Compose.""" return (config.get("ntfy") or {}).get("mode", "docker") def ntfy_health_url(config): return f"http://127.0.0.1:{config['ntfy_port']}/v1/health" def server_address(config): """The -addr the server binds. The bind address is a manifest setting so the phone client can reach the server over the LAN; manifests written before it existed default to loopback. """ return f"{config.get('bind_address') or '127.0.0.1'}:{config['server_port']}" def load(prefix): prefix = Path(prefix).expanduser().resolve() path = prefix / MARKER if not path.is_file() or path.is_symlink() or path.stat().st_uid != os.getuid(): raise ValueError("Not a managed Pipelines installation owned by this user") config = json.loads(path.read_text()) if config.get("format") != 1 or config.get("prefix") != str(prefix): raise ValueError("Invalid installation manifest; refusing to manage paths") return config def run(args, *, env=None, quiet=False, check=True, timeout=120): result = subprocess.run(args, env=env, capture_output=True, text=True, timeout=timeout) if check and result.returncode: # No subprocess output here: ntfy token/password commands may contain secrets. raise RuntimeError(f"Command failed ({result.returncode}): {args[0]} {args[1] if len(args)>1 else ''}") if not quiet and result.stdout: print(result.stdout.rstrip()) return result def compose(config, *args, **kwargs): return run([config["docker"], "compose", "--project-name", config["project"], "--file", str(Path(config["prefix"]) / "compose.yaml"), *args], **kwargs) def healthy(url): try: with urllib.request.urlopen(url, timeout=2) as response: return response.status == 200 and json.load(response).get("healthy") is True except (OSError, ValueError): return False def wait_health(url, timeout=40): end = time.monotonic() + timeout while time.monotonic() < end: if healthy(url): return time.sleep(0.25) raise RuntimeError(f"Service did not become healthy: {url}; see pipelines logs") def process_command(pid): """Return the OS command line for a live PID, or None when it cannot be read.""" try: return subprocess.check_output(["ps", "-p", str(pid), "-o", "command="], text=True) except (OSError, subprocess.CalledProcessError): return None def managed_pid(config): path = Path(config["prefix"]) / "supervisor.pid" try: pid = int(path.read_text()) except (OSError, ValueError): return None if pid <= 1: return None command = process_command(pid) # Never signal an arbitrary/reused PID. if command is None or str(Path(config["prefix"]) / "lib/manage.py") not in command or "supervise" not in command: return None return pid def registration(config): return config.get("registration") def service_command(config, arguments): parser = argparse.ArgumentParser(prog="pipelines service", description="Manage this native installation's server, authenticated web and ntfy login service.") parser.add_argument("action", choices=("enable", "disable", "status")) parser.add_argument("--print", dest="preview", action="store_true", help="Preview enable's existing service definition without changing or starting anything") if not arguments or arguments == ["help"]: parser.print_help() return args = parser.parse_args(arguments) if args.preview and args.action != "enable": parser.error("--print is only supported with enable") if ntfy_mode(config) != "native" or config.get("label") != "net.a-wan.pipelines" or not registration(config): raise ValueError("No native installer login service is recorded; rerun ./install") path = Path(registration(config)) if platform.system() == "Darwin": expected = Path.home() / "Library/LaunchAgents/net.a-wan.pipelines.plist" elif platform.system() == "Linux": root = Path(config.get("opencode_env", {}).get("XDG_CONFIG_HOME") or Path.home() / ".config") expected = root / "systemd/user/net.a-wan.pipelines.service" else: raise ValueError("Native login services require macOS launchd or Linux systemd --user") if (path != expected or path.is_symlink() or not path.is_file() or path.stat().st_uid != os.getuid()): raise ValueError("Refusing an unexpected native service definition path or owner") content = path.read_text() if SERVICE_MARKER not in content: raise ValueError("Refusing an unmanaged or standalone developer service definition") if platform.system() == "Darwin": definition = plistlib.loads(content.encode()) command = [config["python"], str(Path(config["prefix"]) / "lib/manage.py"), "supervise", "--prefix", config["prefix"]] if definition.get("Label") != config["label"] or definition.get("ProgramArguments") != command: raise ValueError("Native service definition does not match this installation") else: def quote(value): return '"' + value.replace('\\', '\\\\').replace('"', '\\"').replace('%', '%%').replace('$', '$$') + '"' command = [config["python"], str(Path(config["prefix"]) / "lib/manage.py"), "supervise", "--prefix", config["prefix"]] if "ExecStart=" + " ".join(map(quote, command)) not in content.splitlines(): raise ValueError("Native service definition does not match this installation") if args.preview: print(content, end="") elif args.action == "enable": start(config) print("Native login autostart enabled: server, authenticated web and ntfy.") elif args.action == "disable": if platform.system() == "Darwin": run(["launchctl", "disable", f"gui/{os.getuid()}/{config['label']}"], quiet=True) else: run(["systemctl", "--user", "disable", config["label"] + ".service"], quiet=True) stop(config) print("Native login autostart disabled; supervisor stopped. Data and credentials retained.") elif platform.system() == "Darwin": domain = f"gui/{os.getuid()}" disabled = run(["launchctl", "print-disabled", domain], quiet=True) enabled = not re.search(r'"' + re.escape(config["label"]) + r'"\s*=>\s*true', disabled.stdout) active = run(["launchctl", "print", domain + "/" + config["label"]], quiet=True, check=False).returncode == 0 print("Autostart:", "enabled" if enabled else "disabled") print("Supervisor:", "loaded" if active else "not loaded") else: enabled = run(["systemctl", "--user", "is-enabled", config["label"] + ".service"], quiet=True, check=False) active = run(["systemctl", "--user", "is-active", config["label"] + ".service"], quiet=True, check=False) print("Autostart:", "enabled" if enabled.returncode == 0 else "disabled") print("Supervisor:", "active" if active.returncode == 0 else "inactive") def start(config): root = Path(config["prefix"]) if registration(config): if platform.system() == "Darwin": target = f"gui/{os.getuid()}/{config['label']}" run(["launchctl", "enable", target], quiet=True) if run(["launchctl", "print", target], quiet=True, check=False).returncode: run(["launchctl", "bootstrap", f"gui/{os.getuid()}", registration(config)], quiet=True) run(["launchctl", "kickstart", target], quiet=True) else: run(["systemctl", "--user", "daemon-reload"], quiet=True) run(["systemctl", "--user", "enable", "--now", config["label"] + ".service"], quiet=True) elif managed_pid(config) is None: log = (root / "logs/supervisor.log").open("a") subprocess.Popen([config["python"], str(root / "lib/manage.py"), "supervise", "--prefix", str(root)], stdin=subprocess.DEVNULL, stdout=log, stderr=log, start_new_session=True) log.close() wait_health(f"http://127.0.0.1:{config['server_port']}/healthz", timeout=75) if config.get("web"): web = config["web"] host = "127.0.0.1" if web["host"] == "0.0.0.0" else web["host"] end = time.monotonic() + 20 while True: try: with urllib.request.urlopen(f"http://{host}:{web['port']}/login", timeout=2) as response: if response.status == 200: break except OSError: pass if time.monotonic() >= end: raise RuntimeError("Web proxy did not become ready; see logs/web.log") time.sleep(0.25) def stop(config): if registration(config): if platform.system() == "Darwin": run(["launchctl", "bootout", f"gui/{os.getuid()}", registration(config)], quiet=True, check=False) else: run(["systemctl", "--user", "stop", config["label"] + ".service"], quiet=True) pid = managed_pid(config) if pid is not None: os.kill(pid, signal.SIGTERM) deadline = time.monotonic() + 80 while managed_pid(config) is not None and time.monotonic() < deadline: time.sleep(0.2) if managed_pid(config) is not None: raise RuntimeError("Supervisor did not stop; inspect logs before trying again") if ntfy_mode(config) == "native": ntfy_stop(config) else: compose(config, "stop", "ntfy", quiet=True) def ntfy_start(config): info = config["ntfy"] log = (Path(config["prefix"]) / "logs/ntfy.log").open("a") child = subprocess.Popen([info["bin"], "serve", "--config", info["config"]], stdin=subprocess.DEVNULL, stdout=log, stderr=log, start_new_session=True) log.close() (Path(config["prefix"]) / "ntfy.pid").write_text(str(child.pid)) return child def ntfy_stop(config): path = Path(config["prefix"]) / "ntfy.pid" try: pid = int(path.read_text()) except (OSError, ValueError): return if pid <= 1: return command = process_command(pid) # Never signal an arbitrary/reused PID: only the managed ntfy serve process. # Accept the recorded path or its symlink-resolved form so a prefix reached # through a symlinked HOME (macOS /var → /private/var) is still matched. binary = str(config["ntfy"]["bin"]) candidates = {binary, str(Path(binary).resolve())} if command is None or "serve" not in command or not any(candidate in command for candidate in candidates): path.unlink(missing_ok=True) return os.kill(pid, signal.SIGTERM) deadline = time.monotonic() + 10 while time.monotonic() < deadline: try: os.kill(pid, 0) except ProcessLookupError: break time.sleep(0.1) path.unlink(missing_ok=True) def supervise(config): root = Path(config["prefix"]) with (root / "supervisor.lock").open("a") as lock: try: fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) except BlockingIOError: return pidfile = root / "supervisor.pid" pidfile.write_text(str(os.getpid())) stopped = False child = None ntfy_child = None web_child = None def stopping(*_): nonlocal stopped stopped = True for process in (child, ntfy_child, web_child): if process is not None and process.poll() is None: process.terminate() signal.signal(signal.SIGTERM, stopping) signal.signal(signal.SIGINT, stopping) try: if ntfy_mode(config) == "native": ntfy_child = ntfy_start(config) else: compose(config, "up", "-d", "ntfy", quiet=True) wait_health(ntfy_health_url(config)) if stopped: return env = dict(os.environ) env.update(config.get("opencode_env", {})) env.update(HOME=str(Path.home()), PATH=config["path"], TZ=config["timezone"], PIPELINES_NTFY_URL=f"http://127.0.0.1:{config['ntfy_port']}/{config['topic']}", PIPELINES_NTFY_PUBLIC_URL=config["public_url"], PIPELINES_NTFY_TOKEN_FILE=str(root / "secrets/publisher-token")) env.pop("PIPELINES_NTFY_TOKEN", None) server_command = [str(root / "bin/pipelines-server"), "-addr", server_address(config), "-db", str(root / "data/pipelines.db")] if config.get("allow_authoring") is True: server_command.append("-allow-authoring") with (root / "logs/server.log").open("a") as log: child = subprocess.Popen(server_command, cwd=config["workspace"], env=env, stdout=log, stderr=log) if config.get("web"): web = config["web"] web_env = dict(env) web_env.pop("PIPELINES_WEB_AUTH", None) web_env["PIPELINES_WEB_AUTH_FILE"] = web["auth_file"] with (root / "logs/web.log").open("a") as web_log: web_child = subprocess.Popen( [web["node"], web["script"], "--port", str(web["port"]), "--host", web["host"], "--pipelines", f"127.0.0.1:{config['server_port']}"], env=web_env, stdout=web_log, stderr=web_log) while child.poll() is None and not stopped: if web_child is not None and web_child.poll() is not None: raise RuntimeError("Authenticated web proxy exited; see logs/web.log") time.sleep(0.25) if not stopped and child.returncode: raise RuntimeError("Pipeline server exited; see logs/server.log") finally: if web_child is not None and web_child.poll() is None: web_child.terminate() try: web_child.wait(timeout=10) except subprocess.TimeoutExpired: web_child.kill() web_child.wait() if child is not None and child.poll() is None: child.terminate() try: child.wait(timeout=70) except subprocess.TimeoutExpired: child.kill() child.wait() if ntfy_mode(config) == "native": if ntfy_child is not None and ntfy_child.poll() is None: ntfy_child.terminate() try: ntfy_child.wait(timeout=10) except subprocess.TimeoutExpired: ntfy_child.kill() ntfy_child.wait() (root / "ntfy.pid").unlink(missing_ok=True) else: compose(config, "stop", "ntfy", quiet=True, check=False) pidfile.unlink(missing_ok=True) def uninstall(config, purge=False, yes=False): root = Path(config["prefix"]) if not yes and input("Uninstall Pipelines? [y/N] ").strip().lower() != "y": return if purge and not yes and input("Also DELETE pipeline history, credentials, and this install's ntfy volume? Type DELETE: ") != "DELETE": raise ValueError("Purge canceled; nothing deleted") stop(config) if registration(config): if platform.system() != "Darwin": run(["systemctl", "--user", "disable", config["label"] + ".service"], quiet=True, check=False) Path(registration(config)).unlink(missing_ok=True) if ntfy_mode(config) != "native": compose(config, "down", *(["--volumes"] if purge else []), quiet=True) link = Path(config["launcher"]) if link.is_symlink() and link.resolve() == (root / "bin/pipelines").resolve(): if str(link)=="/usr/local/bin/pipelines" and not os.access(link.parent,os.W_OK): subprocess.run(["sudo","rm",str(link)],check=True) else: link.unlink() if purge: # Only the explicitly identified, owned installation directory is removed. shutil.rmtree(root) print("Removed installation and its pipeline/ntfy data. Host OpenCode was untouched.") else: for path in (root / "bin", root / "lib"): if path.exists(): shutil.rmtree(path) config["registration"] = None config["uninstalled"] = True (root / MARKER).write_text(json.dumps(config, indent=2)) print(f"Uninstalled. Data and ntfy volume retained at {root}; reinstall to reuse them.") def main(): lifecycle = ("start", "stop", "restart", "status", "doctor", "logs", "supervise", "uninstall") # Strip only the launcher's installation selector. All application arguments # belong to the shared Go CLI (including global flags before the command). selector = argparse.ArgumentParser(add_help=False) selector.add_argument("--prefix", type=Path, default=DEFAULT_PREFIX) selected, forwarded = selector.parse_known_args() config = load(selected.prefix) if forwarded and forwarded[0] in ("service", "native-service"): service_command(config, forwarded[1:]) return if not forwarded or forwarded[0] not in lifecycle: binary = str(Path(config["prefix"]) / "bin/pipelines") env = dict(os.environ) env.update(config.get("opencode_env", {})) env.update(PIPELINES_URL=f"ws://127.0.0.1:{config['server_port']}/connect", PIPELINES_OPENCODE=config["opencode"], PATH=config["path"]) os.execve(binary, [binary, *(forwarded or ["tui"])], env) return parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("action", nargs="?", default="tui") parser.add_argument("--prefix", type=Path, default=DEFAULT_PREFIX) parser.add_argument("--purge", action="store_true") parser.add_argument("--yes", action="store_true") args, rest = parser.parse_known_args() if args.action in lifecycle and rest: parser.error("unexpected arguments: " + " ".join(rest)) if args.action == "start": start(config) elif args.action == "stop": stop(config) elif args.action == "restart": stop(config); start(config) elif args.action in ("status", "doctor"): print("Pipeline server:", "healthy" if healthy(f"http://127.0.0.1:{config['server_port']}/healthz") else "stopped/unhealthy") print("ntfy:", "healthy" if healthy(ntfy_health_url(config)) else "stopped/unhealthy") print("Phone server:", config["public_url"], "· topic:", config["topic"], "· username:", config["username"]) if args.action == "doctor": run([config["opencode"], "api", "get", "/api/info"], quiet=True) print("Host OpenCode API: responding (model execution is a separate check)") elif args.action == "logs": os.execvp("tail", ["tail", "-n", "100", "-f", str(Path(config["prefix"]) / "logs/server.log")]) elif args.action == "supervise": supervise(config) elif args.action == "uninstall": uninstall(config, args.purge, args.yes) if __name__ == "__main__": try: main() except (ValueError, RuntimeError, OSError, subprocess.SubprocessError) as error: sys.exit(str(error))