forked from kenzuya/unshackle
feat(services): load service plugins from git repos
Allow directories.services entries to be git repo specs (URL/owner-repo/ @branch) alongside local paths: clone + TTL-pull as cache, list-order priority with a duplicate-ignored summary, a refresh-services command that force-syncs and reports the per-service diff, and a dirty-clone guard that protects local edits on the automated path. Adds binaries.Git and a redact_paths config toggle.
This commit is contained in:
@@ -8,18 +8,43 @@ import click
|
||||
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.service import Service
|
||||
from unshackle.core.service_repo import DirtyServiceRepo, is_repo_spec, resolve_service_repo
|
||||
from unshackle.core.utilities import import_module_by_path
|
||||
from unshackle.core.utils.redact import redact_path
|
||||
|
||||
log = logging.getLogger("services")
|
||||
|
||||
_service_dirs = config.directories.services
|
||||
if not isinstance(_service_dirs, list):
|
||||
_service_dirs = [_service_dirs]
|
||||
_raw = config.directories.services
|
||||
if not isinstance(_raw, list):
|
||||
_raw = [_raw]
|
||||
# resolve in config order — priority IS list order: the first source to define a tag is the source,
|
||||
# later sources (local or repo) with the same tag are shadowed. List local last to make it a fallback.
|
||||
_service_dirs: list[Path] = []
|
||||
DIRTY_REPOS: list[str] = []
|
||||
for _entry in _raw:
|
||||
if isinstance(_entry, str) and is_repo_spec(_entry):
|
||||
try:
|
||||
_resolved = resolve_service_repo(_entry)
|
||||
except DirtyServiceRepo as e:
|
||||
# local edits in the clone — record it and let check_load_errors() exit cleanly
|
||||
DIRTY_REPOS.append(redact_path(str(e.path)))
|
||||
_resolved = None
|
||||
if _resolved:
|
||||
_service_dirs.append(_resolved)
|
||||
else:
|
||||
_service_dirs.append(Path(_entry))
|
||||
|
||||
_SERVICES = sorted(
|
||||
(path for service_dir in _service_dirs for path in service_dir.glob("*/__init__.py")),
|
||||
key=lambda x: x.parent.stem,
|
||||
)
|
||||
# dedupe by tag honoring list order (first wins); track shadows for the load summary
|
||||
_seen: dict[str, Path] = {}
|
||||
SHADOWED: list[str] = []
|
||||
for _dir in _service_dirs:
|
||||
for _path in _dir.glob("*/__init__.py"):
|
||||
tag = _path.parent.stem
|
||||
if tag in _seen:
|
||||
SHADOWED.append(f"{tag}: using {redact_path(str(_seen[tag]))}, ignoring duplicate {redact_path(str(_path))}")
|
||||
else:
|
||||
_seen[tag] = _path
|
||||
_SERVICES = sorted(_seen.values(), key=lambda x: x.parent.stem)
|
||||
|
||||
|
||||
def load_service(path: Path) -> object:
|
||||
@@ -64,13 +89,33 @@ _MODULES, LOAD_ERRORS = load_services(_SERVICES)
|
||||
_ALIASES = {tag: getattr(module, "ALIASES", ()) for tag, module in _MODULES.items()}
|
||||
|
||||
|
||||
_SUMMARY_LOGGED = False
|
||||
|
||||
|
||||
def check_load_errors() -> None:
|
||||
"""Raise a single clean error if any Service failed to load.
|
||||
"""Log a one-line load summary (once) and raise a single clean error on failures.
|
||||
|
||||
Called when services are actually needed (listing/resolving) so the message
|
||||
is rendered once by Click, without a traceback and without cascading through
|
||||
every command that imports this module.
|
||||
every command that imports this module. Duplicate services (a tag also served by an
|
||||
earlier, higher-priority source, which is ignored) are summarised here; the full
|
||||
list — naming the path used and the duplicate ignored — is debug-only.
|
||||
"""
|
||||
if DIRTY_REPOS:
|
||||
joined = "\n".join(f" - {p}" for p in DIRTY_REPOS)
|
||||
raise click.ClickException(
|
||||
"Service repo has local changes — refusing to refresh so your edits are not lost.\n"
|
||||
"Commit and push them to the upstream repo (or revert them), then retry:\n" + joined
|
||||
)
|
||||
global _SUMMARY_LOGGED
|
||||
if not _SUMMARY_LOGGED:
|
||||
_SUMMARY_LOGGED = True
|
||||
summary = f"Loaded {len(_MODULES)} services"
|
||||
if SHADOWED:
|
||||
summary += f" ({len(SHADOWED)} duplicate(s) ignored)"
|
||||
log.info(summary)
|
||||
for shadow in SHADOWED:
|
||||
log.debug("%s", shadow)
|
||||
if LOAD_ERRORS:
|
||||
joined = "\n".join(f" - {err}" for err in LOAD_ERRORS)
|
||||
raise click.ClickException(f"Failed to load {len(LOAD_ERRORS)} service(s):\n{joined}")
|
||||
|
||||
Reference in New Issue
Block a user