Merge branch 'fix/client-jobs-dir-symlink' into unify/skills-20261112-night
This commit is contained in:
commit
93f002e681
7 files changed
+482
-62
No files matched your search
@@ -196,9 +196,9 @@ class Cleanup:
|
||||
reason = outcome if isinstance(outcome, str) and outcome else stderr.strip()[-500:] or f"pkgmgr reported no result for {item['branch']}"
|
||||
return {**item, "status": "failed", "reason": reason[:500], "code": code, "applied": applied}
|
||||
|
||||
def job(self, path: Path, now: float) -> dict:
|
||||
def job(self, folder: int, path: Path, now: float) -> dict:
|
||||
try:
|
||||
job = json.loads(path.read_text(encoding="utf-8"))
|
||||
job = json.loads(verifier.read_regular(path, folder))
|
||||
except (OSError, ValueError) as error:
|
||||
return {"id": path.stem, "finished": now, "items": [], "error": f"unreadable job: {error}"}
|
||||
items = job.get("items") if isinstance(job, dict) else None
|
||||
@@ -214,15 +214,10 @@ class Cleanup:
|
||||
|
||||
|
||||
def cycle(jobs: Path, results: Path, cleanup: Cleanup) -> int:
|
||||
handled = 0
|
||||
for path in sorted(jobs.iterdir()):
|
||||
if not JOB.fullmatch(path.name) or not path.is_file():
|
||||
path.unlink(missing_ok=True)
|
||||
continue
|
||||
verifier.write_private(results / path.name, cleanup.job(path, time.time()))
|
||||
path.unlink(missing_ok=True)
|
||||
handled += 1
|
||||
return handled
|
||||
def run(folder: int, name: str) -> dict | None:
|
||||
return cleanup.job(folder, Path(name), time.time()) if JOB.fullmatch(name) else None
|
||||
|
||||
return verifier.drain(jobs, results, run)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
@@ -232,7 +227,6 @@ def main() -> int:
|
||||
parser.add_argument(name, type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
trust = verifier.Trust(approvers=args.approvers, replay=args.replay, origin=signing.origin_of(args.port))
|
||||
args.results.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
cycle(args.jobs, args.results, Cleanup(trust, args.tree, args.pkgmgr))
|
||||
return 0
|
||||
|
||||
|
||||
@@ -29,7 +29,6 @@ Returns:
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import errno
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
@@ -47,39 +46,18 @@ JOB = re.compile(r"[0-9a-f]{16}\.json")
|
||||
SAFE_PATH = "/usr/local/bin:/usr/bin:/bin"
|
||||
CLAIM = "claim.json"
|
||||
CLAIM_SHAPE = (("session", str), ("runtime", str), ("beat", (int, float)))
|
||||
DIRECTORY = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW | os.O_CLOEXEC
|
||||
FILE = os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK | os.O_CLOEXEC
|
||||
READ_LIMIT = 1 << 20
|
||||
DIRECTORY = verifier.FOLDER
|
||||
OUTPUT_LIMIT = 4000
|
||||
TIMEOUT = 120
|
||||
|
||||
|
||||
def read_regular(name: str | Path, dir_fd: int | None) -> bytes:
|
||||
"""Args:
|
||||
name: the file, relative to dir_fd or a path; a link, FIFO or other
|
||||
non-regular file raises OSError, so does one over READ_LIMIT bytes.
|
||||
dir_fd: the open directory name is relative to, or None.
|
||||
|
||||
Returns:
|
||||
The file's bytes.
|
||||
"""
|
||||
fd = os.open(name, FILE, dir_fd=dir_fd)
|
||||
with open(fd, "rb") as handle:
|
||||
if not stat.S_ISREG(os.fstat(fd).st_mode):
|
||||
raise OSError(errno.EINVAL, "not a regular file", str(name))
|
||||
data = handle.read(READ_LIMIT + 1)
|
||||
if len(data) > READ_LIMIT:
|
||||
raise OSError(errno.EFBIG, f"larger than {READ_LIMIT} bytes", str(name))
|
||||
return data
|
||||
|
||||
|
||||
def claim_in(office_fd: int) -> dict | None:
|
||||
"""Returns:
|
||||
The claim of the open office directory in the shape council.py
|
||||
reads, or None.
|
||||
"""
|
||||
try:
|
||||
claim = json.loads(read_regular(CLAIM, office_fd))
|
||||
claim = json.loads(verifier.read_regular(CLAIM, office_fd))
|
||||
except (OSError, ValueError, RecursionError):
|
||||
return None
|
||||
shaped = isinstance(claim, dict) and all(isinstance(claim.get(key), kind) for key, kind in CLAIM_SHAPE)
|
||||
@@ -268,9 +246,9 @@ class Offices:
|
||||
vacant = "; the office is vacant now" if item["action"] == "takeover" else ""
|
||||
return ended("failed", f"{output.strip()[-500:]}{vacant}", done.returncode, output)
|
||||
|
||||
def job(self, path: Path, now: float) -> dict:
|
||||
def job(self, folder: int, path: Path, now: float) -> dict:
|
||||
try:
|
||||
job = json.loads(read_regular(path, None))
|
||||
job = json.loads(verifier.read_regular(path, folder))
|
||||
except (OSError, ValueError, RecursionError) as error:
|
||||
return {"id": path.stem, "finished": now, "items": [], "error": f"unreadable job: {error}"}
|
||||
items = job.get("items") if isinstance(job, dict) else None
|
||||
@@ -281,16 +259,10 @@ class Offices:
|
||||
|
||||
|
||||
def cycle(jobs: Path, results: Path, offices: Offices) -> int:
|
||||
handled = 0
|
||||
for path in sorted(jobs.iterdir()):
|
||||
if JOB.fullmatch(path.name):
|
||||
verifier.write_private(results / path.name, offices.job(path, time.time()))
|
||||
handled += 1
|
||||
if path.is_dir() and not path.is_symlink():
|
||||
shutil.rmtree(path)
|
||||
else:
|
||||
path.unlink(missing_ok=True)
|
||||
return handled
|
||||
def run(folder: int, name: str) -> dict | None:
|
||||
return offices.job(folder, Path(name), time.time()) if JOB.fullmatch(name) else None
|
||||
|
||||
return verifier.drain(jobs, results, run)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
@@ -300,7 +272,6 @@ def main() -> int:
|
||||
parser.add_argument(name, type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
trust = verifier.Trust(approvers=args.approvers, replay=args.replay, origin=signing.origin_of(args.port))
|
||||
args.results.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
cycle(args.jobs, args.results, Offices(trust, args.council, args.script, Path.home()))
|
||||
return 0
|
||||
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import datetime
|
||||
import errno
|
||||
import fcntl
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import secrets
|
||||
import shutil
|
||||
import stat
|
||||
import sys
|
||||
from collections.abc import Callable, Iterator
|
||||
from pathlib import Path
|
||||
from typing import NamedTuple
|
||||
|
||||
@@ -18,6 +24,10 @@ from signing import (
|
||||
rule_allows,
|
||||
)
|
||||
|
||||
FOLDER = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW | os.O_CLOEXEC
|
||||
FILE = os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK | os.O_CLOEXEC
|
||||
READ_LIMIT = 1 << 20
|
||||
|
||||
|
||||
class Trust(NamedTuple):
|
||||
approvers: Path
|
||||
@@ -32,20 +42,195 @@ def pinned(path: Path) -> list[dict]:
|
||||
return entries
|
||||
|
||||
|
||||
def write_private(path: Path, data: dict) -> None:
|
||||
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
descriptor, name = tempfile.mkstemp(dir=path.parent, prefix=f".{path.name}-")
|
||||
def write_at(folder: int, name: str, data: dict) -> None:
|
||||
"""Args:
|
||||
folder: the open directory the file goes into.
|
||||
name: the file's name there; a link of that name is replaced, never
|
||||
followed.
|
||||
data: written as JSON with mode 0600, by rename.
|
||||
"""
|
||||
temp = f".{name}-{secrets.token_hex(8)}"
|
||||
descriptor = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW | os.O_CLOEXEC, 0o600, dir_fd=folder)
|
||||
try:
|
||||
with os.fdopen(descriptor, "w", encoding="utf-8") as handle:
|
||||
json.dump(data, handle, indent=1, sort_keys=True)
|
||||
handle.flush()
|
||||
os.fsync(handle.fileno())
|
||||
os.replace(name, path)
|
||||
os.replace(temp, name, src_dir_fd=folder, dst_dir_fd=folder)
|
||||
except OSError:
|
||||
Path(name).unlink(missing_ok=True)
|
||||
with contextlib.suppress(FileNotFoundError):
|
||||
os.unlink(temp, dir_fd=folder)
|
||||
raise
|
||||
|
||||
|
||||
def write_private(path: Path, data: dict) -> None:
|
||||
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
folder = os.open(path.parent, os.O_RDONLY | os.O_DIRECTORY | os.O_CLOEXEC)
|
||||
try:
|
||||
write_at(folder, path.name, data)
|
||||
finally:
|
||||
os.close(folder)
|
||||
|
||||
|
||||
def read_regular(name: str | Path, dir_fd: int | None) -> bytes:
|
||||
"""Args:
|
||||
name: the file, relative to dir_fd or a path; a link, FIFO or other
|
||||
non-regular file raises OSError, so does one over READ_LIMIT bytes.
|
||||
dir_fd: the open directory name is relative to, or None.
|
||||
|
||||
Returns:
|
||||
The file's bytes.
|
||||
"""
|
||||
fd = os.open(name, FILE, dir_fd=dir_fd)
|
||||
with open(fd, "rb") as handle:
|
||||
if not stat.S_ISREG(os.fstat(fd).st_mode):
|
||||
raise OSError(errno.EINVAL, "not a regular file", str(name))
|
||||
data = handle.read(READ_LIMIT + 1)
|
||||
if len(data) > READ_LIMIT:
|
||||
raise OSError(errno.EFBIG, f"larger than {READ_LIMIT} bytes", str(name))
|
||||
return data
|
||||
|
||||
|
||||
def descend(folder: int, part: str, create: bool, follow: bool) -> int:
|
||||
"""Args:
|
||||
folder: an open directory, closed once part is open.
|
||||
part: one name in it.
|
||||
create: make a missing part with mode 0700.
|
||||
follow: follow part when it is a link.
|
||||
|
||||
Returns:
|
||||
The open directory part.
|
||||
"""
|
||||
try:
|
||||
child = os.open(part, FOLDER, dir_fd=folder)
|
||||
except FileNotFoundError:
|
||||
if not create:
|
||||
raise
|
||||
with contextlib.suppress(FileExistsError):
|
||||
os.mkdir(part, 0o700, dir_fd=folder)
|
||||
child = os.open(part, FOLDER, dir_fd=folder)
|
||||
except OSError as error:
|
||||
if error.errno not in (errno.ELOOP, errno.ENOTDIR) or not follow:
|
||||
raise
|
||||
child = os.open(part, FOLDER & ~os.O_NOFOLLOW, dir_fd=folder)
|
||||
os.close(folder)
|
||||
return child
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def private_dir(path: Path, create: bool) -> Iterator[int]:
|
||||
"""Args:
|
||||
path: the directory, opened from / one name at a time by descend();
|
||||
a link is followed only by a process other than root, and only
|
||||
while every directory entered so far is root's and no one else
|
||||
can write it.
|
||||
create: make missing directories with mode 0700.
|
||||
|
||||
Returns:
|
||||
The open directory, closed on exit.
|
||||
|
||||
Raises:
|
||||
Refused unless it is a directory of this effective uid that no one
|
||||
else can write, reached without a link descend() did not follow.
|
||||
"""
|
||||
folder = os.open("/", FOLDER)
|
||||
rooted = os.geteuid() != 0
|
||||
try:
|
||||
for part in path.absolute().parts[1:]:
|
||||
held = os.fstat(folder)
|
||||
rooted = rooted and held.st_uid == 0 and not held.st_mode & 0o022
|
||||
folder = descend(folder, part, create, rooted)
|
||||
found = os.fstat(folder)
|
||||
except OSError as error:
|
||||
os.close(folder)
|
||||
raise Refused(f"{path}: {error.filename}: {error.strerror or error}") from error
|
||||
if found.st_uid != os.geteuid() or found.st_mode & 0o022:
|
||||
os.close(folder)
|
||||
raise Refused(f"{path} is no directory of uid {os.geteuid()}, or others can write it")
|
||||
try:
|
||||
yield folder
|
||||
finally:
|
||||
os.close(folder)
|
||||
|
||||
|
||||
def remove_at(folder: int, name: str) -> None:
|
||||
"""Args:
|
||||
folder: the open directory.
|
||||
name: the entry to remove: a real directory with all below it through
|
||||
shutil.rmtree, which refuses a link swapped in; anything else as
|
||||
the name itself, never what a link names.
|
||||
"""
|
||||
try:
|
||||
if stat.S_ISDIR(os.lstat(name, dir_fd=folder).st_mode):
|
||||
shutil.rmtree(name, dir_fd=folder)
|
||||
else:
|
||||
os.unlink(name, dir_fd=folder)
|
||||
except FileNotFoundError:
|
||||
return
|
||||
|
||||
|
||||
def unreported(folder: int, out: int, name: str, result: dict, error: OSError) -> None:
|
||||
"""Args:
|
||||
folder: the open jobs directory.
|
||||
out: the open results directory.
|
||||
name: the job whose result could not be written; it moves to
|
||||
.<id>.unreported in out, or is removed where that move fails, so
|
||||
it never runs again.
|
||||
result: its result, printed on stderr as one line.
|
||||
error: why the result could not be written.
|
||||
"""
|
||||
kept = f".{Path(name).stem}.unreported"
|
||||
print(f"no result for {name}: {error}; the job is kept as {kept} beside the results: {json.dumps(result, sort_keys=True)}", file=sys.stderr)
|
||||
try:
|
||||
os.rename(name, kept, src_dir_fd=folder, dst_dir_fd=out)
|
||||
except OSError:
|
||||
try:
|
||||
remove_at(folder, name)
|
||||
except OSError as stuck:
|
||||
print(f"kept {name}: {stuck}", file=sys.stderr)
|
||||
|
||||
|
||||
def drain(jobs: Path, results: Path, run: Callable[[int, str], dict | None]) -> int:
|
||||
"""Args:
|
||||
jobs: the directory the hub writes jobs into.
|
||||
results: the directory each result goes to under its job's name;
|
||||
made when missing.
|
||||
run: called with the open jobs directory and each entry's name, in
|
||||
sorted order; returns the entry's result, or None for no job.
|
||||
|
||||
Returns:
|
||||
The number of results written. Every entry is removed after its
|
||||
result, each write and removal relative to the open directories; a
|
||||
result that cannot be written goes to unreported(), a removal that
|
||||
fails prints one line on stderr, and the cycle goes on; a refused
|
||||
directory changes nothing and prints one line.
|
||||
"""
|
||||
with contextlib.ExitStack() as stack:
|
||||
try:
|
||||
folder = stack.enter_context(private_dir(jobs, False))
|
||||
out = stack.enter_context(private_dir(results, True))
|
||||
except Refused as error:
|
||||
print(f"refused, nothing done: {error}", file=sys.stderr)
|
||||
return 0
|
||||
with os.scandir(folder) as entries:
|
||||
names = sorted(entry.name for entry in entries)
|
||||
handled = 0
|
||||
for name in names:
|
||||
result = run(folder, name)
|
||||
if result is not None:
|
||||
try:
|
||||
write_at(out, name, result)
|
||||
handled += 1
|
||||
except OSError as error:
|
||||
unreported(folder, out, name, result, error)
|
||||
continue
|
||||
try:
|
||||
remove_at(folder, name)
|
||||
except OSError as error:
|
||||
print(f"kept {name}: {error}", file=sys.stderr)
|
||||
return handled
|
||||
|
||||
|
||||
def pin(path: Path, entry: dict, now: float) -> list[dict]:
|
||||
stamp = datetime.datetime.fromtimestamp(now, datetime.UTC).isoformat(timespec="seconds")
|
||||
entries = [old for old in load_pinned(path) if old["id"] != entry["id"]] + [{**entry, "pinned": stamp}]
|
||||
|
||||
Reference in new issue
Block a user