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:
+79
-3
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user