425 lines
14 KiB
Python
425 lines
14 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from acupedia_ops.bootstrap.api_cli import VALID_TARGETS, run_api_bootstrap
|
|
from acupedia_ops.common.env import load_env_file
|
|
from acupedia_ops.common.paths import repo_root
|
|
from acupedia_ops.data.image.clean_image_borders import process_path
|
|
from acupedia_ops.deployment.backend import deploy_backend
|
|
from acupedia_ops.deployment.database import deploy_database
|
|
from acupedia_ops.deployment.gitea_runner import provision_runner, remove_runner
|
|
from acupedia_ops.local.bootstrap import local_bootstrap
|
|
from acupedia_ops.local.database import reset_local_database
|
|
from acupedia_ops.local.git_status import repository_status
|
|
from acupedia_ops.local.infrastructure import (
|
|
infra_down,
|
|
infra_logs,
|
|
infra_restart,
|
|
infra_status,
|
|
infra_up,
|
|
)
|
|
from acupedia_ops.local.services import run_component, setup_components
|
|
from acupedia_ops.local.validation import has_errors, validate_workspace
|
|
from acupedia_ops.local.workspace import load_local_workspace
|
|
|
|
|
|
LOCAL_COMPONENTS = ("api", "mobile", "web", "admin")
|
|
|
|
|
|
def _build_parser() -> argparse.ArgumentParser:
|
|
parser = argparse.ArgumentParser(
|
|
prog="acupedia-ops",
|
|
description="AccuPedia operational tooling",
|
|
)
|
|
parser.add_argument(
|
|
"--env-file",
|
|
type=Path,
|
|
default=None,
|
|
help="Local workspace .env file. Defaults to <acupedia-ops>/.env.",
|
|
)
|
|
|
|
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
|
|
# Local development -------------------------------------------------
|
|
local_parser = subparsers.add_parser(
|
|
"local",
|
|
help="Local development orchestration",
|
|
)
|
|
local_sub = local_parser.add_subparsers(
|
|
dest="local_command",
|
|
required=True,
|
|
)
|
|
|
|
local_sub.add_parser(
|
|
"validate",
|
|
help="Validate local repository paths and configuration",
|
|
)
|
|
|
|
setup = local_sub.add_parser(
|
|
"setup",
|
|
help="Validate workspace and optionally install component dependencies",
|
|
)
|
|
setup.add_argument(
|
|
"--component",
|
|
action="append",
|
|
choices=LOCAL_COMPONENTS,
|
|
help="Component to set up. Repeat as needed. Defaults to all.",
|
|
)
|
|
setup.add_argument(
|
|
"--install",
|
|
action="store_true",
|
|
help="Run configured setup commands after validation",
|
|
)
|
|
|
|
status = local_sub.add_parser(
|
|
"status",
|
|
help="Show local repository and infrastructure status",
|
|
)
|
|
status.add_argument("--json", action="store_true")
|
|
|
|
run = local_sub.add_parser(
|
|
"run",
|
|
help="Run one component in its native development mode",
|
|
)
|
|
run.add_argument("component", choices=LOCAL_COMPONENTS)
|
|
|
|
infra_up_parser = local_sub.add_parser(
|
|
"infra-up",
|
|
help="Start local Docker Compose infrastructure",
|
|
)
|
|
infra_up_parser.add_argument("--build", action="store_true")
|
|
infra_up_parser.add_argument("services", nargs="*")
|
|
|
|
infra_down_parser = local_sub.add_parser(
|
|
"infra-down",
|
|
help="Stop local Docker Compose infrastructure",
|
|
)
|
|
infra_down_parser.add_argument(
|
|
"--volumes",
|
|
action="store_true",
|
|
help="Also delete named volumes; destructive",
|
|
)
|
|
|
|
infra_restart_parser = local_sub.add_parser(
|
|
"infra-restart",
|
|
help="Restart local infrastructure services",
|
|
)
|
|
infra_restart_parser.add_argument("services", nargs="*")
|
|
|
|
local_sub.add_parser(
|
|
"infra-status",
|
|
help="Show Docker Compose infrastructure state",
|
|
)
|
|
|
|
logs = local_sub.add_parser(
|
|
"logs",
|
|
help="Show local infrastructure logs",
|
|
)
|
|
logs.add_argument("services", nargs="*")
|
|
logs.add_argument("--follow", "-f", action="store_true")
|
|
logs.add_argument("--tail", type=int, default=200)
|
|
|
|
bootstrap = local_sub.add_parser(
|
|
"bootstrap",
|
|
help="Invoke acupedia-api bootstrap using local content paths",
|
|
)
|
|
bootstrap.add_argument(
|
|
"target",
|
|
nargs="?",
|
|
choices=sorted(VALID_TARGETS),
|
|
help="Defaults to bootstrap.default_target in config/local.yaml",
|
|
)
|
|
bootstrap.add_argument(
|
|
"--publish-content",
|
|
action="store_true",
|
|
help="Publish content during content/all bootstrap",
|
|
)
|
|
bootstrap.add_argument(
|
|
"bootstrap_args",
|
|
nargs=argparse.REMAINDER,
|
|
help="Additional arguments forwarded to acupedia-api after '--'",
|
|
)
|
|
|
|
reset_db = local_sub.add_parser(
|
|
"reset-db",
|
|
help="Use the backend-owned data reset CLI",
|
|
)
|
|
reset_db.add_argument(
|
|
"--execute",
|
|
action="store_true",
|
|
help="Actually execute reset. Without this, backend dry-run is used.",
|
|
)
|
|
reset_db.add_argument(
|
|
"reset_args",
|
|
nargs=argparse.REMAINDER,
|
|
help="Additional backend reset arguments after '--'",
|
|
)
|
|
|
|
# Image preprocessing -----------------------------------------------
|
|
image_parser = subparsers.add_parser(
|
|
"image",
|
|
help="Image preprocessing operations",
|
|
)
|
|
image_sub = image_parser.add_subparsers(dest="image_command", required=True)
|
|
clean = image_sub.add_parser("clean-border")
|
|
clean.add_argument("input", type=Path)
|
|
clean.add_argument("output", type=Path)
|
|
clean.add_argument("--border-fraction", type=float, default=0.035)
|
|
clean.add_argument("--difference-threshold", type=float, default=18.0)
|
|
clean.add_argument("--max-component-depth", type=float, default=0.025)
|
|
clean.add_argument("--max-edge-px", type=int, default=12)
|
|
clean.add_argument("--line-coverage", type=float, default=0.35)
|
|
|
|
# Backend bootstrap adapter -----------------------------------------
|
|
backend_bootstrap = subparsers.add_parser(
|
|
"bootstrap",
|
|
help="Direct adapter to the acupedia-api bootstrap CLI",
|
|
)
|
|
backend_bootstrap.add_argument("target", choices=sorted(VALID_TARGETS))
|
|
backend_bootstrap.add_argument("--api-root", type=Path)
|
|
backend_bootstrap.add_argument(
|
|
"--env",
|
|
dest="environment",
|
|
choices=("dev", "staging", "prod"),
|
|
)
|
|
backend_bootstrap.add_argument("bootstrap_args", nargs=argparse.REMAINDER)
|
|
|
|
# Remote deployment -------------------------------------------------
|
|
deploy = subparsers.add_parser("deploy")
|
|
deploy_sub = deploy.add_subparsers(dest="deploy_command", required=True)
|
|
backend = deploy_sub.add_parser("backend")
|
|
backend.add_argument("--env", required=True, choices=("dev", "staging", "prod"))
|
|
database = deploy_sub.add_parser("database")
|
|
database.add_argument("--env", required=True, choices=("dev", "staging", "prod"))
|
|
|
|
# Gitea runner lifecycle --------------------------------------------
|
|
runner = subparsers.add_parser("runner")
|
|
runner_sub = runner.add_subparsers(dest="runner_command", required=True)
|
|
|
|
provision = runner_sub.add_parser("provision")
|
|
provision.add_argument("--inventory", type=Path, required=True)
|
|
provision.add_argument("--limit")
|
|
provision.add_argument(
|
|
"--registration-token",
|
|
default=os.environ.get("GITEA_RUNNER_REGISTRATION_TOKEN"),
|
|
)
|
|
provision.add_argument("--force-reregister", action="store_true")
|
|
provision.add_argument("--extra-var", action="append", default=[])
|
|
|
|
remove = runner_sub.add_parser("remove")
|
|
remove.add_argument("--inventory", type=Path, required=True)
|
|
remove.add_argument("--limit")
|
|
remove.add_argument("--remove-data", action="store_true")
|
|
remove.add_argument("--extra-var", action="append", default=[])
|
|
|
|
return parser
|
|
|
|
|
|
def _load_env(path: Path | None) -> None:
|
|
load_env_file(path or (repo_root() / ".env"), override=False)
|
|
|
|
|
|
def _forwarded(values: list[str]) -> list[str]:
|
|
result = list(values)
|
|
if result and result[0] == "--":
|
|
result = result[1:]
|
|
return result
|
|
|
|
|
|
def _print_validation(workspace) -> int:
|
|
issues = validate_workspace(workspace)
|
|
if not issues:
|
|
print("Local workspace validation: OK")
|
|
return 0
|
|
|
|
for issue in issues:
|
|
print(f"[{issue.level.upper()}] {issue.component}: {issue.message}")
|
|
return 1 if has_errors(issues) else 0
|
|
|
|
|
|
def main() -> None:
|
|
parser = _build_parser()
|
|
args = parser.parse_args()
|
|
_load_env(args.env_file)
|
|
|
|
if args.command == "local":
|
|
workspace = load_local_workspace(env_file=args.env_file)
|
|
|
|
if args.local_command == "validate":
|
|
raise SystemExit(_print_validation(workspace))
|
|
|
|
if args.local_command == "setup":
|
|
exit_code = _print_validation(workspace)
|
|
if exit_code:
|
|
raise SystemExit(exit_code)
|
|
components = args.component or list(LOCAL_COMPONENTS)
|
|
if args.install:
|
|
setup_components(workspace, components)
|
|
else:
|
|
print(
|
|
"Validation completed. Dependency installation was not run; "
|
|
"use --install to run configured setup commands."
|
|
)
|
|
return
|
|
|
|
if args.local_command == "status":
|
|
rows = []
|
|
for component, path in workspace.paths.items():
|
|
status = repository_status(path) if path.exists() else None
|
|
rows.append(
|
|
{
|
|
"component": component,
|
|
"path": str(path),
|
|
"exists": path.exists(),
|
|
"git": bool(status and status.is_git_repo),
|
|
"branch": status.branch if status else None,
|
|
"revision": status.revision if status else None,
|
|
"dirty": status.dirty if status else None,
|
|
}
|
|
)
|
|
|
|
if args.json:
|
|
print(json.dumps(rows, indent=2))
|
|
else:
|
|
for row in rows:
|
|
state = "missing" if not row["exists"] else "ok"
|
|
git = ""
|
|
if row["git"]:
|
|
dirty = "*" if row["dirty"] else ""
|
|
git = f" git={row['branch'] or '-'}@{row['revision'] or '-'}{dirty}"
|
|
print(
|
|
f"{row['component']:8} {state:7} {row['path']}{git}"
|
|
)
|
|
|
|
if workspace.infra.compose_file.exists():
|
|
result = infra_status(workspace)
|
|
if result.stdout:
|
|
print("\nInfrastructure:\n" + result.stdout.rstrip())
|
|
else:
|
|
print(
|
|
"\nInfrastructure: compose file not configured/found "
|
|
f"({workspace.infra.compose_file})"
|
|
)
|
|
return
|
|
|
|
if args.local_command == "run":
|
|
run_component(workspace, args.component)
|
|
return
|
|
|
|
if args.local_command == "infra-up":
|
|
infra_up(
|
|
workspace,
|
|
build=args.build,
|
|
services=args.services,
|
|
)
|
|
return
|
|
|
|
if args.local_command == "infra-down":
|
|
infra_down(workspace, volumes=args.volumes)
|
|
return
|
|
|
|
if args.local_command == "infra-restart":
|
|
infra_restart(workspace, services=args.services)
|
|
return
|
|
|
|
if args.local_command == "infra-status":
|
|
result = infra_status(workspace)
|
|
if result.stdout:
|
|
print(result.stdout, end="")
|
|
if result.stderr:
|
|
print(result.stderr, end="")
|
|
raise SystemExit(result.returncode)
|
|
|
|
if args.local_command == "logs":
|
|
infra_logs(
|
|
workspace,
|
|
services=args.services,
|
|
follow=args.follow,
|
|
tail=args.tail,
|
|
)
|
|
return
|
|
|
|
if args.local_command == "bootstrap":
|
|
local_bootstrap(
|
|
workspace,
|
|
target=args.target,
|
|
publish_content=args.publish_content,
|
|
extra_args=_forwarded(args.bootstrap_args),
|
|
)
|
|
return
|
|
|
|
if args.local_command == "reset-db":
|
|
reset_local_database(
|
|
workspace,
|
|
execute=args.execute,
|
|
extra_args=tuple(_forwarded(args.reset_args)),
|
|
)
|
|
return
|
|
|
|
if args.command == "image" and args.image_command == "clean-border":
|
|
result = process_path(
|
|
input_path=args.input,
|
|
output_path=args.output,
|
|
border_fraction=args.border_fraction,
|
|
difference_threshold=args.difference_threshold,
|
|
max_component_depth_fraction=args.max_component_depth,
|
|
max_edge_px=args.max_edge_px,
|
|
line_coverage=args.line_coverage,
|
|
)
|
|
print(f"Processed images : {result.processed}")
|
|
print(f"Skipped images : {result.skipped}")
|
|
print(f"Copied files : {result.copied}")
|
|
print(f"Output : {result.output}")
|
|
return
|
|
|
|
if args.command == "bootstrap":
|
|
run_api_bootstrap(
|
|
target=args.target,
|
|
api_root=args.api_root,
|
|
environment=args.environment,
|
|
extra_args=_forwarded(args.bootstrap_args),
|
|
)
|
|
return
|
|
|
|
if args.command == "deploy" and args.deploy_command == "backend":
|
|
deploy_backend(environment=args.env)
|
|
return
|
|
|
|
if args.command == "deploy" and args.deploy_command == "database":
|
|
deploy_database(environment=args.env)
|
|
return
|
|
|
|
if args.command == "runner" and args.runner_command == "provision":
|
|
token = args.registration_token
|
|
if not token:
|
|
parser.error(
|
|
"--registration-token or GITEA_RUNNER_REGISTRATION_TOKEN is required"
|
|
)
|
|
provision_runner(
|
|
inventory=args.inventory,
|
|
registration_token=token,
|
|
limit=args.limit,
|
|
extra_vars=args.extra_var,
|
|
force_reregister=args.force_reregister,
|
|
)
|
|
return
|
|
|
|
if args.command == "runner" and args.runner_command == "remove":
|
|
remove_runner(
|
|
inventory=args.inventory,
|
|
limit=args.limit,
|
|
remove_data=args.remove_data,
|
|
extra_vars=args.extra_var,
|
|
)
|
|
return
|
|
|
|
parser.error("Unsupported command")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|