Files
pipelines/scripts/manage.py
T
alan a70e3aef4b Install Pipelines natively by default on macOS and Linux
Add scripts/setup-native.py and make ./install (and --native) build the server, the CLI
and ntfy from source into ~/.local/share/pipelines, detect an existing host OpenCode
and Pi instead of installing them, install the packaged authoring skill, migrate an
existing Docker database and ntfy auth store, save the host client default and start
both services. ./install --docker keeps the previous Docker server + ntfy
installation unchanged.

ntfy is compiled from its pinned Go module on every host because upstream publishes
no macOS server binary (its darwin asset is built with -tags noserver); its cgo
SQLite driver is why a C compiler is now required natively. The ntfy credential
bootstrap and the client default writer are shared by both installers,
installation.Load resolves symlinked prefixes, the Docker migration copies the
SQLite write-ahead log so the newest definitions and runs are not lost, and the
account subcommands read the generated configuration through NTFY_CONFIG_FILE.

Verified on macOS: 115 Python tests, go build/vet/test, and scratch installs that
migrate 9 pipelines and 31 runs, build ntfy v2.30.0 and start both services healthy
on spare ports before stopping them.
2026-10-13 17:20:14 +01:00

326 lines
13 KiB
Python

#!/usr/bin/env python3
"""One per-user lifecycle for the native server and its private Docker ntfy."""
import argparse
import fcntl
import json
import os
from pathlib import Path
import platform
import shutil
import signal
import subprocess
import sys
import time
import urllib.request
DEFAULT_PREFIX = Path.home() / ".local/share/pipelines"
MARKER = "pipelines-install.json"
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 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 start(config):
root = Path(config["prefix"])
if registration(config):
if platform.system() == "Darwin":
target = f"gui/{os.getuid()}/{config['label']}"
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)
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
def stopping(*_):
nonlocal stopped
stopped = True
for process in (child, ntfy_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)
with (root / "logs/server.log").open("a") as log:
child = subprocess.Popen([str(root / "bin/pipelines-server"), "-addr",
f"127.0.0.1:{config['server_port']}", "-db", str(root / "data/pipelines.db")],
cwd=config["workspace"], env=env, stdout=log, stderr=log)
while child.poll() is None and not stopped:
time.sleep(0.25)
if not stopped and child.returncode:
raise RuntimeError("Pipeline server exited; see logs/server.log")
finally:
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 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))