Route native service commands and configure fresh authoring role

Select native installs only via explicit PIPELINES_PREFIX or the matching owned prefix/bin/pipelines executable. Route enable/status/disable to the existing native supervisor; validate registration ownership, label and command, keep preview read-only, and disable login registration before stopping. Never fall back to a standalone service for an invalid explicit prefix.

Fresh native installs explicitly enable authoring, while reinstalls preserve false or missing legacy role settings. Add explicit --allow-authoring/--no-authoring overrides and pass the server flag only for manifest true. Print a copyable native enable command and update installation help/docs. No Docker configuration changes.

Checks: 54 mocked native installer/lifecycle tests; 8 entrypoint tests; offline Go installation/CLI tests and vet; gofmt clean; shell syntax and diff checks. No live install, restart, deployment, permission/config changes, or native Linux verification.
This commit is contained in:
2026-10-20 21:41:05 +01:00
parent d58b3f49fc
commit bb32383bbe
11 changed files with 508 additions and 18 deletions
+79 -3
View File
@@ -6,6 +6,8 @@ import json
import os
from pathlib import Path
import platform
import plistlib
import re
import shutil
import signal
import subprocess
@@ -15,6 +17,7 @@ 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):
@@ -105,6 +108,74 @@ 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):
@@ -235,10 +306,12 @@ def supervise(config):
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([str(root / "bin/pipelines-server"), "-addr",
server_address(config), "-db", str(root / "data/pipelines.db")],
cwd=config["workspace"], env=env, stdout=log, stderr=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)
@@ -325,6 +398,9 @@ def main():
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)
+26
View File
@@ -19,6 +19,7 @@ import os
import platform
import plistlib
import secrets
import shlex
import shutil
import subprocess
import sys
@@ -227,6 +228,20 @@ class Setup:
raw = self.prefix_argument or self.environ.get("PIPELINES_PREFIX") or default
return Path(raw).expanduser().resolve()
def authoring_role(self, prefix, requested):
"""Fresh single-user installs author; never widen an existing server's role."""
if requested is not None:
return requested
path = prefix / MARKER
if not path.exists() and not path.is_symlink():
return True
if path.is_symlink() or not path.is_file():
raise ValueError("Refusing to read authoring role from a non-regular installation manifest")
existing = json.loads(path.read_text())
if not isinstance(existing, dict) or not isinstance(existing.get("allow_authoring", False), bool):
raise ValueError("Invalid existing authoring role; no permission changes were made")
return existing.get("allow_authoring", False)
# --- prefix layout ---------------------------------------------------
def prepare_layout(self, prefix):
@@ -581,6 +596,7 @@ class Setup:
except ValueError:
raise ValueError("Invalid API/web bind address; use an IPv4 address such as 0.0.0.0 or 127.0.0.1") from None
prefix = self.resolve_prefix()
allow_authoring = self.authoring_role(prefix, args.allow_authoring)
harness = self.harness()
service_path = self.service_path()
# Reuse an already built ntfy binary and skip the release lookup, so a
@@ -625,6 +641,7 @@ class Setup:
path_value = str(prefix / "bin") + os.pathsep + self.environ.get("PATH", "")
manifest = {
"format": 1,
"allow_authoring": allow_authoring,
"prefix": str(prefix),
"python": python,
"server_port": args.port,
@@ -673,6 +690,10 @@ class Setup:
print(f"ntfy is configured for {base_url}; server, web proxy and ntfy have login autostart ({service_path}).")
print(f"Web: http://{lan_ip if args.web_host == '0.0.0.0' else args.web_host}:{args.web_port}/ "
f"(sign-in password in {web['auth_file']}; existing credentials retained).")
print("Login autostart is already enabled. To enable/start it again, run:\n "
+ shlex.join([str(prefix / "bin/pipelines"), "service", "enable"]))
print("Configured server role: " + ("single-user authoring enabled (web pipeline creation/publication allowed)."
if allow_authoring else "execution-only (pipeline publication disabled)."))
if args.bind_address == "0.0.0.0":
print(f"Phone client server URL: ws://{lan_ip}:{args.port}/connect "
"(use a workspace path that exists on this server).")
@@ -692,6 +713,11 @@ def arguments(argv=None):
help="Unauthenticated server bind address (default 127.0.0.1; expose only the web proxy)")
parser.add_argument("--ntfy-port", type=int, default=DEFAULT_NTFY_PORT, help="ntfy listen port")
parser.add_argument("--model", help="Record an explicit default model; otherwise host OpenCode configuration is reused")
role = parser.add_mutually_exclusive_group()
role.add_argument("--allow-authoring", dest="allow_authoring", action="store_true", default=None,
help="Explicitly enable pipeline publication; fresh native installs default to enabled")
role.add_argument("--no-authoring", dest="allow_authoring", action="store_false",
help="Execution-only server; disable pipeline publication (reinstalls otherwise preserve the recorded role)")
parser.add_argument("--replace-data", action="store_true",
help="Allow Docker migration to replace an existing native database/auth store")
return parser.parse_args(argv)
+124
View File
@@ -1,14 +1,20 @@
import importlib.util
import io
import os
from pathlib import Path
import signal
import subprocess
import tempfile
import unittest
from contextlib import redirect_stdout
from unittest.mock import MagicMock, patch
spec = importlib.util.spec_from_file_location("manage_native", Path(__file__).with_name("manage.py"))
manage = importlib.util.module_from_spec(spec)
spec.loader.exec_module(manage)
setup_spec = importlib.util.spec_from_file_location("setup_native_service", Path(__file__).with_name("setup-native.py"))
setup_native = importlib.util.module_from_spec(setup_spec)
setup_spec.loader.exec_module(setup_native)
class FakeProcess:
@@ -20,6 +26,124 @@ class FakeProcess:
class ManageNativeTests(unittest.TestCase):
def service_config(self, home, system):
config = self.config(home / "prefix")
config["opencode_env"] = {"XDG_CONFIG_HOME": str(home / ".config")}
path = (home / "Library/LaunchAgents/net.a-wan.pipelines.plist" if system == "Darwin"
else home / ".config/systemd/user/net.a-wan.pipelines.service")
setup_native.Setup(home, environ={}).register_service(path, config)
return config
def test_native_service_preview_is_read_only_on_both_platforms(self):
for system in ("Darwin", "Linux"):
with self.subTest(system=system), tempfile.TemporaryDirectory() as directory, \
patch.object(manage.Path, "home", return_value=Path(directory)), \
patch.object(manage.platform, "system", return_value=system):
config = self.service_config(Path(directory), system)
path = Path(config["registration"])
before = path.read_bytes()
with patch.object(manage, "run", side_effect=AssertionError("preview invoked OS manager")), \
patch.object(manage, "start", side_effect=AssertionError("preview started services")), \
redirect_stdout(io.StringIO()) as output:
manage.service_command(config, ["enable", "--print"])
self.assertEqual(path.read_bytes(), before)
self.assertEqual(output.getvalue(), before.decode())
self.assertNotIn("publisher-token", output.getvalue())
def test_native_service_enable_and_disable_use_existing_registration(self):
for system in ("Darwin", "Linux"):
with self.subTest(system=system), tempfile.TemporaryDirectory() as directory, \
patch.object(manage.Path, "home", return_value=Path(directory)), \
patch.object(manage.platform, "system", return_value=system):
config = self.service_config(Path(directory), system)
before = Path(config["registration"]).read_bytes()
with patch.object(manage, "start") as start, redirect_stdout(io.StringIO()):
manage.service_command(config, ["enable"])
start.assert_called_once_with(config)
calls = []
with patch.object(manage, "run", side_effect=lambda args, **kw: calls.append(args)), \
patch.object(manage, "stop", side_effect=lambda cfg: calls.append("stop")), \
redirect_stdout(io.StringIO()):
manage.service_command(config, ["disable"])
expected = (["launchctl", "disable", f"gui/{os.getuid()}/net.a-wan.pipelines"] if system == "Darwin"
else ["systemctl", "--user", "disable", "net.a-wan.pipelines.service"])
self.assertEqual(calls, [expected, "stop"])
self.assertEqual(Path(config["registration"]).read_bytes(), before)
def test_native_service_status_does_not_print_raw_manager_output(self):
for system in ("Darwin", "Linux"):
with self.subTest(system=system), tempfile.TemporaryDirectory() as directory, \
patch.object(manage.Path, "home", return_value=Path(directory)), \
patch.object(manage.platform, "system", return_value=system):
config = self.service_config(Path(directory), system)
def manager(args, **kwargs):
self.assertTrue(kwargs["quiet"])
return subprocess.CompletedProcess(args, 0, stdout='"net.a-wan.pipelines" => true\nprivate-output')
with patch.object(manage, "run", side_effect=manager), redirect_stdout(io.StringIO()) as output:
manage.service_command(config, ["status"])
self.assertNotIn("private-output", output.getvalue())
self.assertIn("Autostart: " + ("disabled" if system == "Darwin" else "enabled"), output.getvalue())
def test_native_service_rejects_unmanaged_symlink_and_wrong_prefix_definitions(self):
for variant in ("unmanaged", "symlink", "wrong-prefix", "wrong-label"):
with self.subTest(variant=variant), tempfile.TemporaryDirectory() as directory, \
patch.object(manage.Path, "home", return_value=Path(directory)), \
patch.object(manage.platform, "system", return_value="Linux"):
config = self.service_config(Path(directory), "Linux")
path = Path(config["registration"])
if variant == "unmanaged":
path.write_text("Managed by pipelines service\nExecStart=developer-server")
elif variant == "symlink":
target = path.with_suffix(".other")
path.rename(target)
path.symlink_to(target)
elif variant == "wrong-prefix":
config["prefix"] += "-other"
else:
config["label"] = "pipelines"
with patch.object(manage, "run", side_effect=AssertionError("unsafe OS manager invocation")), \
patch.object(manage, "start", side_effect=AssertionError("unsafe startup")):
with self.assertRaises(ValueError):
manage.service_command(config, ["enable"])
def test_native_service_rejects_standalone_database_flags(self):
with redirect_stdout(io.StringIO()), patch("sys.stderr", new_callable=io.StringIO):
with self.assertRaises(SystemExit) as raised:
manage.service_command({}, ["enable", "--db", "/other.db"])
self.assertEqual(raised.exception.code, 2)
def test_manager_dispatches_internal_service_verb_without_forwarding_to_go(self):
config = {"prefix": "/scratch/native"}
with patch("sys.argv", ["manage.py", "--prefix", "/scratch/native", "native-service", "enable", "--print"]), \
patch.object(manage, "load", return_value=config), \
patch.object(manage, "service_command") as service, \
patch.object(manage.os, "execve", side_effect=AssertionError("recursive Go dispatch")):
manage.main()
service.assert_called_once_with(config, ["enable", "--print"])
def test_supervisor_authoring_flag_requires_explicit_manifest_true(self):
for role in (True, False, None, "true"):
with self.subTest(role=role), tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / "logs").mkdir()
config = self.config(root)
if role is not None:
config["allow_authoring"] = role
ntfy, server = MagicMock(), MagicMock()
ntfy.poll.return_value = None
server.poll.return_value = 0
server.returncode = 0
with patch.object(manage, "ntfy_start", return_value=ntfy), \
patch.object(manage, "wait_health"), patch.object(manage.signal, "signal"), \
patch.object(manage.subprocess, "Popen", return_value=server) as popen:
manage.supervise(config)
command = popen.call_args.args[0]
self.assertEqual(command[:5], [str(root / "bin/pipelines-server"), "-addr", "127.0.0.1:8080",
"-db", str(root / "data/pipelines.db")])
self.assertEqual("-allow-authoring" in command, role is True)
def config(self, root, mode="native"):
return {
"format": 1, "prefix": str(root), "python": "python3", "server_port": 8080,
+41
View File
@@ -250,6 +250,46 @@ class SetupNativeTests(unittest.TestCase):
installer = setup_native.Setup(directory, runner=Recorder(), environ={"PIPELINES_PREFIX": str(custom)})
self.assertEqual(installer.resolve_prefix(), custom.resolve())
def test_authoring_role_defaults_only_fresh_install_to_enabled(self):
with tempfile.TemporaryDirectory() as directory:
installer = self.installer(directory, Recorder())
prefix = Path(directory) / "prefix"
self.assertTrue(installer.authoring_role(prefix, None))
prefix.mkdir()
for previous, expected in (({}, False), ({"allow_authoring": False}, False),
({"allow_authoring": True}, True)):
with self.subTest(previous=previous):
manifest = prefix / setup_native.MARKER
manifest.write_text(json.dumps(previous))
before = manifest.read_bytes()
self.assertIs(installer.authoring_role(prefix, None), expected)
self.assertTrue(installer.authoring_role(prefix, True))
self.assertFalse(installer.authoring_role(prefix, False))
self.assertEqual(manifest.read_bytes(), before)
manifest.write_text('{"allow_authoring":"true"}')
with self.assertRaisesRegex(ValueError, "Invalid existing authoring role"):
installer.authoring_role(prefix, None)
def test_authoring_flags_are_explicit_optional_overrides(self):
self.assertIsNone(setup_native.arguments([]).allow_authoring)
self.assertTrue(setup_native.arguments(["--allow-authoring"]).allow_authoring)
self.assertFalse(setup_native.arguments(["--no-authoring"]).allow_authoring)
def test_full_run_keeps_existing_execution_only_and_accepts_fresh_opt_out(self):
for previous, arguments in (({}, []), ({"allow_authoring": False}, []), (None, ["--no-authoring"])):
with self.subTest(previous=previous), tempfile.TemporaryDirectory() as directory:
installer = self.installer(directory, Recorder())
prefix = Path(directory) / "prefix"
if previous is not None:
prefix.mkdir()
(prefix / setup_native.MARKER).write_text(json.dumps(previous))
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
patch.object(setup_native.configure, "host_timezone", return_value="UTC"), \
redirect_stdout(io.StringIO()):
installer.run(setup_native.arguments(arguments))
self.assertFalse(manage.load(prefix)["allow_authoring"])
def test_missing_opencode_fails_actionably(self):
with tempfile.TemporaryDirectory() as directory:
installer = self.installer(directory, Recorder())
@@ -268,6 +308,7 @@ class SetupNativeTests(unittest.TestCase):
installer.run(setup_native.arguments([]))
config = manage.load(prefix)
self.assertEqual(config["format"], 1)
self.assertTrue(config["allow_authoring"])
self.assertEqual(config["prefix"], str(prefix.resolve()))
self.assertEqual(config["server_port"], 8080)
self.assertEqual(config["bind_address"], "127.0.0.1")