253 lines
11 KiB
Python
253 lines
11 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 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 managed_pid(config):
|
|
path = Path(config["prefix"]) / "supervisor.pid"
|
|
try:
|
|
pid = int(path.read_text())
|
|
if pid <= 1:
|
|
return None
|
|
command = subprocess.check_output(["ps", "-p", str(pid), "-o", "command="], text=True)
|
|
# Never signal an arbitrary/reused PID.
|
|
if str(Path(config["prefix"]) / "lib/manage.py") not in command or "supervise" not in command:
|
|
return None
|
|
return pid
|
|
except (OSError, ValueError, subprocess.CalledProcessError):
|
|
return None
|
|
|
|
|
|
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")
|
|
compose(config, "stop", "ntfy", quiet=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
|
|
|
|
def stopping(*_):
|
|
nonlocal stopped
|
|
stopped = True
|
|
if child is not None and child.poll() is None:
|
|
child.terminate()
|
|
|
|
signal.signal(signal.SIGTERM, stopping)
|
|
signal.signal(signal.SIGINT, stopping)
|
|
try:
|
|
compose(config, "up", "-d", "ntfy", quiet=True)
|
|
wait_health(f"http://127.0.0.1:{config['ntfy_port']}/v1/health")
|
|
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"),
|
|
*sum((["-workspace-root", path] for path in config["workspace_roots"]), [])],
|
|
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()
|
|
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)
|
|
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(f"http://127.0.0.1:{config['ntfy_port']}/v1/health") 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))
|