a70e3aef4b
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.
123 lines
5.6 KiB
Python
123 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Create local Compose settings and the host client default."""
|
|
import argparse
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
from urllib.parse import urlparse
|
|
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
|
|
|
|
|
def host_timezone():
|
|
candidates = [os.environ.get("TZ", "")]
|
|
link = str(Path("/etc/localtime").resolve())
|
|
if "/zoneinfo/" in link:
|
|
candidates.append(link.split("/zoneinfo/", 1)[1])
|
|
path = Path("/etc/timezone")
|
|
if path.exists():
|
|
candidates.append(path.read_text().strip())
|
|
for candidate in candidates:
|
|
if not candidate:
|
|
continue
|
|
try:
|
|
ZoneInfo(candidate)
|
|
return candidate
|
|
except (ZoneInfoNotFoundError, ValueError):
|
|
continue
|
|
raise ValueError("Cannot detect the host timezone; provide --timezone explicitly")
|
|
|
|
def host_workspaces(directory):
|
|
raw_home = os.environ.get("HOME") or os.environ.get("USERPROFILE") or str(Path.home())
|
|
home = str(Path.home().resolve())
|
|
repository = str(Path(directory).resolve())
|
|
# Reject both the raw and canonical resolved HOME so a platform's home alias
|
|
# (/home/pipelines on Linux; /System/Volumes/Data/home/pipelines on macOS)
|
|
# cannot bypass the reserved-path guard. Managed Docker volumes are separate
|
|
# and are not weakened by these cheap path comparisons.
|
|
candidates = {raw_home, home, str(Path(raw_home).resolve()), repository}
|
|
for value in candidates:
|
|
if any(char in value for char in "\r\n\x00$#'\"\\") or value in ("/", "/data", "/workspace", "/home/pipelines", "/System/Volumes/Data/home/pipelines"):
|
|
raise ValueError("Host home/checkout cannot be mounted safely at its original path; configure a repository bind mount explicitly")
|
|
if home == repository or str(Path(raw_home).resolve()) == repository:
|
|
raise ValueError("Host home/checkout cannot be mounted safely at its original path; configure a repository bind mount explicitly")
|
|
return {"PIPELINES_HOST_HOME": "'"+home+"'", "PIPELINES_HOST_REPOSITORY": "'"+repository+"'", "PIPELINES_UID": str(os.getuid()), "PIPELINES_GID": str(os.getgid())}
|
|
|
|
|
|
def configure_client(endpoint, environ=None):
|
|
"""Save the server address the host CLI/TUI dials by default.
|
|
|
|
Atomic, 0600, and it preserves unrelated keys. Shared by both installers so
|
|
the saved default always matches the installation that just ran.
|
|
"""
|
|
environ = os.environ if environ is None else environ
|
|
root = Path(environ.get("XDG_CONFIG_HOME") or Path.home() / ".config")
|
|
path = root / "pipelines" / "client.json"
|
|
if path.is_symlink():
|
|
raise ValueError("Refusing to modify symlinked client.json; use the printed --url explicitly")
|
|
existing = json.loads(path.read_text()) if path.exists() else {}
|
|
if not isinstance(existing, dict):
|
|
raise ValueError("client.json must contain an object; existing configuration was not replaced")
|
|
existing["server_url"] = endpoint
|
|
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
temporary = None
|
|
try:
|
|
with tempfile.NamedTemporaryFile(mode="w", dir=str(path.parent), delete=False) as file:
|
|
temporary = Path(file.name)
|
|
os.chmod(temporary, 0o600)
|
|
json.dump(existing, file, indent=2)
|
|
file.write("\n")
|
|
os.replace(temporary, path)
|
|
finally:
|
|
if temporary is not None:
|
|
temporary.unlink(missing_ok=True)
|
|
return path
|
|
|
|
|
|
def configure(directory, ntfy_url, bind_address, timezone=None, port=8081):
|
|
directory = Path(directory)
|
|
url = urlparse(ntfy_url)
|
|
if (url.scheme not in ("http", "https") or not url.hostname or url.username
|
|
or url.password or url.query or url.fragment or url.path not in ("", "/")):
|
|
raise ValueError("--ntfy-url must be an HTTP(S) base URL without credentials or a topic")
|
|
timezone = timezone or host_timezone()
|
|
ZoneInfo(timezone)
|
|
for value in (ntfy_url, bind_address, timezone):
|
|
if any(char in value for char in "\r\n\x00$#'\" "):
|
|
raise ValueError("Unsafe configuration value")
|
|
if not 1 <= port <= 65535:
|
|
raise ValueError("Invalid pipeline port")
|
|
settings = directory / ".env"
|
|
if settings.exists():
|
|
raise ValueError(".env exists; edit it rather than overwrite existing settings")
|
|
workspace_values = host_workspaces(directory)
|
|
secrets = directory / "secrets"
|
|
secrets.mkdir(mode=0o700, exist_ok=True)
|
|
token = secrets / "ntfy-token.txt"
|
|
# Empty until ntfy has issued a token. Never replace an existing credential.
|
|
try:
|
|
fd = os.open(token, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
os.close(fd)
|
|
except FileExistsError:
|
|
pass
|
|
with settings.open("x") as file:
|
|
file.write(f"TZ={timezone}\nNTFY_BASE_URL={ntfy_url.rstrip('/')}\n"
|
|
f"NTFY_BIND_ADDRESS={bind_address}\nPIPELINES_PORT={port}\n")
|
|
for key, value in workspace_values.items(): file.write(key+"="+value+"\n")
|
|
return timezone
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--ntfy-url", required=True)
|
|
parser.add_argument("--bind-address", default="0.0.0.0")
|
|
parser.add_argument("--timezone")
|
|
parser.add_argument("--port", type=int, default=8081)
|
|
args = parser.parse_args()
|
|
try:
|
|
zone = configure(Path(__file__).resolve().parents[1], args.ntfy_url,
|
|
args.bind_address, args.timezone, args.port)
|
|
except (ValueError, ZoneInfoNotFoundError) as error:
|
|
parser.error(str(error))
|
|
print(f"Created .env with timezone {zone}. Configure the ntfy token before starting pipelines.")
|