Updated images
68
README.md
|
|
@ -1,73 +1,5 @@
|
|||
uv run bootstrap all \
|
||||
--content-config ./config.json \
|
||||
--content-modules-root ./modules-data \
|
||||
--content-module-code M0001 \
|
||||
--media-manifest ./media.json \
|
||||
--media-media-root ./modules-data/M0001-Acupuncture Points
|
||||
|
||||
|
||||
uv run bootstrap media \
|
||||
--manifest "./modules-data/M0001-Acupuncture Points/media.json" \
|
||||
--media-root "./modules-data/M0001-Acupuncture Points" \
|
||||
--validate-only
|
||||
|
||||
uv run bootstrap media \
|
||||
--manifest "./modules-data/M0001-Acupuncture Points/media.json" \
|
||||
--media-root "./modules-data/M0001-Acupuncture Points"
|
||||
|
||||
uv run acupedia-bootstrap content \
|
||||
--config ./modules-data/config.json \
|
||||
--modules-root ./modules-data \
|
||||
--module-code M0001 \
|
||||
--publish-content
|
||||
|
||||
|
||||
uv run seed \
|
||||
--config ./modules-data/config.json \
|
||||
--modules-root ./modules-data \
|
||||
--module-code M0001 \
|
||||
--seed-search-facets-only
|
||||
|
||||
uv run seed \
|
||||
--config ./modules-data/config.json \
|
||||
--modules-root ./modules-data \
|
||||
--module-code M0001 \
|
||||
--template-code M0001_CT0003 \
|
||||
--content-code LU-1 \
|
||||
--publish-content
|
||||
|
||||
|
||||
uv run reset \
|
||||
--scope module \
|
||||
--module-code M0002 \
|
||||
--execute
|
||||
|
||||
|
||||
acupedia-bootstrap commerce --plans ./modules-data/plans.json
|
||||
|
||||
acupedia-access grant-plan \
|
||||
--email av.salunke@gmail.com \
|
||||
--plan-code POINTS_ACU
|
||||
|
||||
|
||||
python3 diff.py \
|
||||
/home/chintan/Downloads/Acupedia_Dump/src \
|
||||
/home/chintan/Work/Project/acupedia-api \
|
||||
-o diff-report.txt \
|
||||
--exclude "*.lock" \
|
||||
--exclude "generated/**" \
|
||||
--exclude "uploads/**" \
|
||||
--exclude "alembic/versions/**" \
|
||||
--no-new-file-content \
|
||||
--no-removed-file-content
|
||||
|
||||
python3 diff.py \
|
||||
/home/chintan/Downloads/Acupedia_Dump/src \
|
||||
/home/chintan/Work/Project/acupedia-mobile-ui/src \
|
||||
-o diff-report.txt \
|
||||
--no-new-file-content \
|
||||
--no-removed-file-content
|
||||
|
||||
for seeders that use ORM models, we should treat the SQLAlchemy model package as the compile-time contract and audit every seeder field against Base.metadata/mapped attributes before packaging.
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,30 +0,0 @@
|
|||
# Test access CLI
|
||||
|
||||
Grant a plan and all modules mapped through `commerce.access_plan_modules`:
|
||||
|
||||
```bash
|
||||
python -m app.cli.access grant-plan \
|
||||
--email user@example.com \
|
||||
--plan-code POINTS_ACU
|
||||
```
|
||||
|
||||
Optional finite test access:
|
||||
|
||||
```bash
|
||||
python -m app.cli.access grant-plan \
|
||||
--email user@example.com \
|
||||
--plan-code POINTS_ACU \
|
||||
--expires-in-days 30
|
||||
```
|
||||
|
||||
Revoke access sourced from the plan:
|
||||
|
||||
```bash
|
||||
python -m app.cli.access revoke-plan \
|
||||
--email user@example.com \
|
||||
--plan-code POINTS_ACU
|
||||
```
|
||||
|
||||
`--user-id <uuid>` can be used instead of `--email`.
|
||||
|
||||
The grant operation is idempotent for an already-active subscription and active module entitlements. It never invents plan/module mappings; the selected plan must already have rows in `commerce.access_plan_modules`.
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
# Acupedia CLI
|
||||
|
||||
acupedia-ops bootstrap all \
|
||||
--content-config ./config.json \
|
||||
--content-modules-root ./modules-data \
|
||||
--content-module-code M0001 \
|
||||
--media-manifest ./media.json \
|
||||
--media-media-root ./modules-data/M0001-Acupuncture Points
|
||||
|
||||
acupedia-ops bootstrap media \
|
||||
--manifest "./modules-data/M0001-Acupuncture Points/media.json" \
|
||||
--media-root "./modules-data/M0001-Acupuncture Points" \
|
||||
--validate-only
|
||||
|
||||
acupedia-ops bootstrap media \
|
||||
--manifest "./modules-data/M0001-Acupuncture Points/media.json" \
|
||||
--media-root "./modules-data/M0001-Acupuncture Points"
|
||||
|
||||
acupedia-ops bootstrap content \
|
||||
--config ./modules-data/config.json \
|
||||
--modules-root ./modules-data \
|
||||
--module-code M0001 \
|
||||
--publish-content
|
||||
|
||||
acupedia-ops bootstrap content \
|
||||
--config ./modules-data/config.json \
|
||||
--modules-root ./modules-data \
|
||||
--module-code M0001 \
|
||||
--seed-search-facets-only
|
||||
|
||||
acupedia-ops bootstrap content \
|
||||
--config ./modules-data/config.json \
|
||||
--modules-root ./modules-data \
|
||||
--module-code M0001 \
|
||||
--template-code M0001_CT0003 \
|
||||
--content-code LU-1 \
|
||||
--publish-content
|
||||
|
||||
aacupedia-ops bootstrap commerce --plans ./modules-data/plans.json
|
||||
|
||||
acupedia-ops reset \
|
||||
--scope module \
|
||||
--module-code M0002 \
|
||||
--execute
|
||||
|
||||
acupedia-access grant-plan \
|
||||
--email av.salunke@gmail.com \
|
||||
--plan-code POINTS_ACU
|
||||
|
||||
|
||||
Grant a plan and all modules mapped through `commerce.access_plan_modules`:
|
||||
|
||||
```bash
|
||||
acupedia-ops access grant-plan \
|
||||
--email user@example.com \
|
||||
--plan-code POINTS_ACU
|
||||
```
|
||||
|
||||
Optional finite test access:
|
||||
|
||||
```bash
|
||||
acupedia-ops access grant-plan \
|
||||
--email user@example.com \
|
||||
--plan-code POINTS_ACU \
|
||||
--expires-in-days 30
|
||||
```
|
||||
|
||||
Revoke access sourced from the plan:
|
||||
|
||||
```bash
|
||||
acupedia-ops access revoke-plan \
|
||||
--email user@example.com \
|
||||
--plan-code POINTS_ACU
|
||||
```
|
||||
|
||||
`--user-id <uuid>` can be used instead of `--email`.
|
||||
|
||||
The grant operation is idempotent for an already-active subscription and active module entitlements. It never invents plan/module mappings; the selected plan must already have rows in `commerce.access_plan_modules`.
|
||||
|
|
@ -0,0 +1,477 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from uuid import UUID
|
||||
|
||||
from app.integrations.storage import ObjectStorage, StorageConfig
|
||||
from app.bootstrap.seed_auth.models import BootstrapAdminSeed
|
||||
from app.bootstrap.seed_content.validators import SeederError
|
||||
|
||||
from app.bootstrap.config import BootstrapConfig
|
||||
from app.bootstrap.db import create_session_factory
|
||||
from app.bootstrap.orchestrator import BootstrapOrchestrator
|
||||
|
||||
|
||||
logger = logging.getLogger("accupedia.bootstrap")
|
||||
|
||||
|
||||
def build_parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="accupedia-bootstrap",
|
||||
description="AccuPedia bounded-context bootstrap and seeding CLI.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--database-url",
|
||||
default=os.environ.get("DATABASE_URL"),
|
||||
help="PostgreSQL async SQLAlchemy URL. Defaults to DATABASE_URL.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--log-level",
|
||||
default="INFO",
|
||||
choices=["DEBUG", "INFO", "WARNING", "ERROR"],
|
||||
)
|
||||
|
||||
sub = parser.add_subparsers(dest="command", required=True)
|
||||
|
||||
auth = sub.add_parser("auth", help="Seed auth roles/permissions/admin.")
|
||||
_add_admin_args(auth)
|
||||
|
||||
content = sub.add_parser(
|
||||
"content",
|
||||
help="Run the existing content/module seeder.",
|
||||
)
|
||||
_add_content_args(content)
|
||||
|
||||
media = sub.add_parser(
|
||||
"media",
|
||||
help="Seed AccuPedia content media from a manifest.",
|
||||
)
|
||||
_add_media_args(media)
|
||||
|
||||
commerce = sub.add_parser(
|
||||
"commerce",
|
||||
help="Idempotently seed commerce plans and module mappings from plans.json.",
|
||||
)
|
||||
commerce.add_argument(
|
||||
"--plans",
|
||||
type=Path,
|
||||
default=None,
|
||||
help="Path to plans.json. Defaults to app/bootstrap/seed_commerce/plans.json.",
|
||||
)
|
||||
|
||||
all_cmd = sub.add_parser(
|
||||
"all",
|
||||
help="Run auth -> content -> media -> commerce -> validate.",
|
||||
)
|
||||
_add_admin_args(all_cmd)
|
||||
_add_content_args(all_cmd, prefix="content-")
|
||||
_add_media_args(all_cmd, prefix="media-")
|
||||
all_cmd.add_argument(
|
||||
"--commerce-plans",
|
||||
type=Path,
|
||||
default=None,
|
||||
help="Path to commerce plans.json for the commerce step.",
|
||||
)
|
||||
|
||||
sub.add_parser(
|
||||
"validate",
|
||||
help="Validate required schema/tables and seed counts.",
|
||||
)
|
||||
|
||||
return parser
|
||||
|
||||
|
||||
def _add_admin_args(parser: argparse.ArgumentParser) -> None:
|
||||
parser.add_argument("--bootstrap-admin-email")
|
||||
parser.add_argument("--bootstrap-admin-name")
|
||||
parser.add_argument(
|
||||
"--bootstrap-admin-password-env",
|
||||
default="ACCUPEDIA_BOOTSTRAP_ADMIN_PASSWORD",
|
||||
help=(
|
||||
"Environment variable containing the bootstrap admin password. "
|
||||
"The password is never accepted directly as a CLI argument."
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--bootstrap-admin-country",
|
||||
default="India",
|
||||
)
|
||||
|
||||
|
||||
def _add_content_args(
|
||||
parser: argparse.ArgumentParser,
|
||||
*,
|
||||
prefix: str = "",
|
||||
) -> None:
|
||||
if not prefix:
|
||||
parser.add_argument("--config", type=Path, required=True)
|
||||
parser.add_argument("--modules-root", type=Path, required=True)
|
||||
parser.add_argument(
|
||||
"--module-code",
|
||||
action="append",
|
||||
dest="module_codes",
|
||||
help="Seed only the selected module code. Repeat for multiple modules.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--template-code",
|
||||
action="append",
|
||||
dest="template_codes",
|
||||
help=(
|
||||
"Fine-grained mode: seed all source content for the selected template. "
|
||||
"Repeat to seed multiple templates. Requires exactly one --module-code."
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--content-code",
|
||||
action="append",
|
||||
dest="content_codes",
|
||||
help=(
|
||||
"Fine-grained mode: seed only the authored content row identified by its "
|
||||
"'code'. Repeat for multiple rows. Requires exactly one --module-code "
|
||||
"and exactly one --template-code."
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--seed-search-facets-only",
|
||||
"--seed-search-facet-only",
|
||||
dest="seed_search_facets_only",
|
||||
action="store_true",
|
||||
help=(
|
||||
"Seed/reconcile search_facets.json and rebuild the search projection only. "
|
||||
"Does not seed module metadata, definitions, presentations, or content."
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--skip-search-facets",
|
||||
action="store_true",
|
||||
help=(
|
||||
"Skip automatic search-facet seeding/reindex after a normal/template/content run."
|
||||
),
|
||||
)
|
||||
parser.add_argument("--publish-content", action="store_true")
|
||||
parser.add_argument("--continue-on-error", action="store_true")
|
||||
parser.add_argument("--created-by", type=UUID, default=None)
|
||||
return
|
||||
|
||||
if prefix != "content-":
|
||||
raise ValueError(f"Unsupported content option prefix: {prefix}")
|
||||
|
||||
parser.add_argument("--content-config", type=Path, required=True)
|
||||
parser.add_argument("--content-modules-root", type=Path, required=True)
|
||||
parser.add_argument(
|
||||
"--content-module-code",
|
||||
action="append",
|
||||
dest="content_module_codes",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--content-template-code",
|
||||
action="append",
|
||||
dest="content_template_codes",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--content-content-code",
|
||||
action="append",
|
||||
dest="content_content_codes",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--content-seed-search-facets-only",
|
||||
"--content-seed-search-facet-only",
|
||||
dest="content_seed_search_facets_only",
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--content-skip-search-facets",
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--content-publish-content",
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--content-continue-on-error",
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--content-created-by",
|
||||
type=UUID,
|
||||
default=None,
|
||||
)
|
||||
|
||||
|
||||
def _add_media_args(
|
||||
parser: argparse.ArgumentParser,
|
||||
*,
|
||||
prefix: str = "",
|
||||
) -> None:
|
||||
if not prefix:
|
||||
parser.add_argument("--manifest", type=Path, required=True)
|
||||
parser.add_argument("--media-root", type=Path, required=True)
|
||||
parser.add_argument("--validate-only", action="store_true")
|
||||
parser.add_argument("--dry-run", action="store_true")
|
||||
parser.add_argument("--force-reupload", action="store_true")
|
||||
return
|
||||
|
||||
if prefix != "media-":
|
||||
raise ValueError(f"Unsupported media option prefix: {prefix}")
|
||||
|
||||
parser.add_argument("--media-manifest", type=Path, required=True)
|
||||
parser.add_argument("--media-media-root", type=Path, required=True)
|
||||
parser.add_argument(
|
||||
"--media-validate-only",
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--media-dry-run",
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--media-force-reupload",
|
||||
action="store_true",
|
||||
)
|
||||
def _validate_content_args(
|
||||
*,
|
||||
module_codes: set[str] | None,
|
||||
template_codes: set[str] | None,
|
||||
content_codes: set[str] | None,
|
||||
seed_search_facets_only: bool,
|
||||
skip_search_facets: bool,
|
||||
publish_content: bool,
|
||||
) -> None:
|
||||
if template_codes and (not module_codes or len(module_codes) != 1):
|
||||
raise SeederError(
|
||||
"--template-code requires exactly one --module-code so a partial "
|
||||
"seed cannot accidentally target similarly named templates in other modules"
|
||||
)
|
||||
|
||||
if content_codes:
|
||||
if not module_codes or len(module_codes) != 1:
|
||||
raise SeederError("--content-code requires exactly one --module-code")
|
||||
if not template_codes or len(template_codes) != 1:
|
||||
raise SeederError("--content-code requires exactly one --template-code")
|
||||
|
||||
if seed_search_facets_only:
|
||||
if not module_codes:
|
||||
raise SeederError(
|
||||
"--seed-search-facets-only requires at least one --module-code"
|
||||
)
|
||||
if template_codes or content_codes:
|
||||
raise SeederError("--seed-search-facets-only cannot be combined with --template-code or --content-code")
|
||||
if skip_search_facets:
|
||||
raise SeederError(
|
||||
"--seed-search-facets-only cannot be combined with --skip-search-facets"
|
||||
)
|
||||
if publish_content:
|
||||
raise SeederError(
|
||||
"--publish-content is not applicable to --seed-search-facets-only"
|
||||
)
|
||||
|
||||
|
||||
def _build_admin(args: argparse.Namespace) -> BootstrapAdminSeed | None:
|
||||
email = getattr(args, "bootstrap_admin_email", None)
|
||||
name = getattr(args, "bootstrap_admin_name", None)
|
||||
|
||||
if not email and not name:
|
||||
return None
|
||||
if not email or not name:
|
||||
raise ValueError(
|
||||
"Both --bootstrap-admin-email and --bootstrap-admin-name are required."
|
||||
)
|
||||
|
||||
env_name = args.bootstrap_admin_password_env
|
||||
password = os.getenv(env_name, "")
|
||||
if not password:
|
||||
raise ValueError(
|
||||
f"Bootstrap admin password environment variable is empty: {env_name}"
|
||||
)
|
||||
|
||||
return BootstrapAdminSeed(
|
||||
email=email,
|
||||
password=password,
|
||||
display_name=name,
|
||||
country=args.bootstrap_admin_country,
|
||||
)
|
||||
|
||||
|
||||
def _env(
|
||||
name: str,
|
||||
*,
|
||||
required: bool = True,
|
||||
default: str | None = None,
|
||||
) -> str:
|
||||
value = os.getenv(name, default)
|
||||
if required and not value:
|
||||
raise RuntimeError(f"Required environment variable {name} is not set")
|
||||
return value or ""
|
||||
|
||||
|
||||
def _build_storage() -> tuple[ObjectStorage, str]:
|
||||
endpoint = _env("MINIO_ENDPOINT")
|
||||
access_key = _env("MINIO_ACCESS_KEY")
|
||||
secret_key = _env("MINIO_SECRET_KEY")
|
||||
bucket = _env(
|
||||
"MINIO_BUCKET",
|
||||
default="accupedia-media",
|
||||
required=False,
|
||||
)
|
||||
secure = _env(
|
||||
"MINIO_SECURE",
|
||||
default="false",
|
||||
required=False,
|
||||
).lower() in {"1", "true", "yes", "on"}
|
||||
|
||||
storage = ObjectStorage(
|
||||
StorageConfig(
|
||||
endpoint=endpoint,
|
||||
access_key=access_key,
|
||||
secret_key=secret_key,
|
||||
bucket=bucket,
|
||||
secure=secure,
|
||||
)
|
||||
)
|
||||
return storage, bucket
|
||||
|
||||
|
||||
async def async_main() -> int:
|
||||
parser = build_parser()
|
||||
args = parser.parse_args()
|
||||
|
||||
database_url = args.database_url
|
||||
if not database_url:
|
||||
raise SeederError("DATABASE_URL is required")
|
||||
|
||||
logging.basicConfig(
|
||||
level=getattr(logging, args.log_level),
|
||||
format="%(asctime)s %(levelname)s %(name)s - %(message)s",
|
||||
)
|
||||
|
||||
engine, session_factory = create_session_factory(database_url)
|
||||
orchestrator = BootstrapOrchestrator(session_factory)
|
||||
|
||||
try:
|
||||
if args.command == "auth":
|
||||
result = await orchestrator.run_auth(
|
||||
bootstrap_admin=_build_admin(args),
|
||||
)
|
||||
|
||||
elif args.command == "content":
|
||||
module_codes = set(args.module_codes) if args.module_codes else None
|
||||
template_codes = set(args.template_codes) if args.template_codes else None
|
||||
content_codes = set(args.content_codes) if args.content_codes else None
|
||||
|
||||
_validate_content_args(
|
||||
module_codes=module_codes,
|
||||
template_codes=template_codes,
|
||||
content_codes=content_codes,
|
||||
seed_search_facets_only=args.seed_search_facets_only,
|
||||
skip_search_facets=args.skip_search_facets,
|
||||
publish_content=args.publish_content,
|
||||
)
|
||||
|
||||
result = await orchestrator.run_content(
|
||||
config_path=args.config,
|
||||
modules_root=args.modules_root,
|
||||
module_codes=module_codes,
|
||||
template_codes=template_codes,
|
||||
content_codes=content_codes,
|
||||
search_facets_only=args.seed_search_facets_only,
|
||||
skip_search_facets=args.skip_search_facets,
|
||||
publish_content=args.publish_content,
|
||||
continue_on_error=args.continue_on_error,
|
||||
created_by=args.created_by,
|
||||
)
|
||||
|
||||
elif args.command == "media":
|
||||
storage, bucket = _build_storage()
|
||||
result = await orchestrator.run_media(
|
||||
manifest_path=args.manifest,
|
||||
media_root=args.media_root,
|
||||
storage=storage,
|
||||
bucket=bucket,
|
||||
validate_only=args.validate_only,
|
||||
dry_run=args.dry_run,
|
||||
force_reupload=args.force_reupload,
|
||||
)
|
||||
|
||||
elif args.command == "commerce":
|
||||
result = await orchestrator.run_commerce(plans_path=args.plans)
|
||||
|
||||
elif args.command == "validate":
|
||||
result = await orchestrator.run_validate()
|
||||
|
||||
elif args.command == "all":
|
||||
module_codes = (
|
||||
set(args.content_module_codes)
|
||||
if args.content_module_codes
|
||||
else None
|
||||
)
|
||||
template_codes = (
|
||||
set(args.content_template_codes)
|
||||
if args.content_template_codes
|
||||
else None
|
||||
)
|
||||
content_codes = (
|
||||
set(args.content_content_codes)
|
||||
if args.content_content_codes
|
||||
else None
|
||||
)
|
||||
|
||||
_validate_content_args(
|
||||
module_codes=module_codes,
|
||||
template_codes=template_codes,
|
||||
content_codes=content_codes,
|
||||
seed_search_facets_only=args.content_seed_search_facets_only,
|
||||
skip_search_facets=args.content_skip_search_facets,
|
||||
publish_content=args.content_publish_content,
|
||||
)
|
||||
|
||||
storage, bucket = _build_storage()
|
||||
|
||||
result = await orchestrator.run_all(
|
||||
bootstrap_admin=_build_admin(args),
|
||||
content_config_path=args.content_config,
|
||||
modules_root=args.content_modules_root,
|
||||
media_manifest_path=args.media_manifest,
|
||||
media_root=args.media_media_root,
|
||||
storage=storage,
|
||||
bucket=bucket,
|
||||
module_codes=module_codes,
|
||||
template_codes=template_codes,
|
||||
content_codes=content_codes,
|
||||
search_facets_only=args.content_seed_search_facets_only,
|
||||
skip_search_facets=args.content_skip_search_facets,
|
||||
publish_content=args.content_publish_content,
|
||||
continue_on_error=args.content_continue_on_error,
|
||||
created_by=args.content_created_by,
|
||||
validate_only=args.media_validate_only,
|
||||
dry_run=args.media_dry_run,
|
||||
force_reupload=args.media_force_reupload,
|
||||
plans_path=args.commerce_plans,
|
||||
)
|
||||
|
||||
else:
|
||||
parser.error(f"Unsupported command: {args.command}")
|
||||
return 2
|
||||
|
||||
print(json.dumps(result.to_dict(), indent=2, default=str))
|
||||
|
||||
if args.command == "media":
|
||||
return 0 if result.status == "success" else 2
|
||||
|
||||
return 0 if result.status == "success" else 1
|
||||
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
try:
|
||||
raise SystemExit(asyncio.run(async_main()))
|
||||
except Exception as exc:
|
||||
logger.exception("Bootstrap failed: %s", exc)
|
||||
raise SystemExit(1) from exc
|
||||
|
||||
174
app/cli/seed.py
|
|
@ -1,174 +0,0 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
|
||||
from app.seed.orchestrator import SeedOrchestrator
|
||||
from app.seed.validators import SeederError
|
||||
|
||||
logger = logging.getLogger("accupedia.seed")
|
||||
|
||||
|
||||
def build_parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(description="AccuPedia production module/content seeder")
|
||||
parser.add_argument("--database-url", default=os.environ.get("DATABASE_URL"))
|
||||
parser.add_argument("--schema", default="acu_content")
|
||||
parser.add_argument("--config", type=Path, required=True)
|
||||
parser.add_argument("--modules-root", type=Path, required=True)
|
||||
parser.add_argument(
|
||||
"--module-code",
|
||||
action="append",
|
||||
dest="module_codes",
|
||||
help="Seed only the selected module code. Repeat for multiple modules.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--template-code",
|
||||
action="append",
|
||||
dest="template_codes",
|
||||
help=(
|
||||
"Fine-grained mode: seed all source content for the selected template. "
|
||||
"Repeat to seed multiple templates. Requires exactly one --module-code."
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--content-code",
|
||||
action="append",
|
||||
dest="content_codes",
|
||||
help=(
|
||||
"Fine-grained mode: seed only the authored content row identified by its "
|
||||
"'code'. Repeat for multiple rows. Requires exactly one --module-code "
|
||||
"and exactly one --template-code."
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--seed-search-facets-only",
|
||||
"--seed-search-facet-only",
|
||||
dest="seed_search_facets_only",
|
||||
action="store_true",
|
||||
help=(
|
||||
"Seed/reconcile search_facets.json and rebuild the search projection only. "
|
||||
"Does not seed module metadata, definitions, presentations, or content."
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--skip-search-facets",
|
||||
action="store_true",
|
||||
help=(
|
||||
"Skip automatic search-facet seeding/reindex after a normal/template/content run."
|
||||
),
|
||||
)
|
||||
parser.add_argument("--publish-content", action="store_true")
|
||||
parser.add_argument("--continue-on-error", action="store_true")
|
||||
parser.add_argument("--created-by", type=UUID, default=None)
|
||||
parser.add_argument("--log-level", default="INFO", choices=["DEBUG", "INFO", "WARNING", "ERROR"])
|
||||
return parser
|
||||
|
||||
|
||||
async def run(args: argparse.Namespace) -> int:
|
||||
if not args.database_url:
|
||||
raise SeederError("DATABASE_URL is required")
|
||||
|
||||
module_codes = set(args.module_codes) if args.module_codes else None
|
||||
template_codes = set(args.template_codes) if args.template_codes else None
|
||||
content_codes = set(args.content_codes) if args.content_codes else None
|
||||
|
||||
if template_codes and (not module_codes or len(module_codes) != 1):
|
||||
raise SeederError(
|
||||
"--template-code requires exactly one --module-code so a partial "
|
||||
"seed cannot accidentally target similarly named templates in other modules"
|
||||
)
|
||||
|
||||
if content_codes:
|
||||
if not module_codes or len(module_codes) != 1:
|
||||
raise SeederError("--content-code requires exactly one --module-code")
|
||||
if not template_codes or len(template_codes) != 1:
|
||||
raise SeederError("--content-code requires exactly one --template-code")
|
||||
|
||||
if args.seed_search_facets_only:
|
||||
if not module_codes:
|
||||
raise SeederError(
|
||||
"--seed-search-facets-only requires at least one --module-code"
|
||||
)
|
||||
if template_codes or content_codes:
|
||||
raise SeederError(
|
||||
"--seed-search-facets-only cannot be combined with "
|
||||
"--template-code or --content-code"
|
||||
)
|
||||
if args.skip_search_facets:
|
||||
raise SeederError(
|
||||
"--seed-search-facets-only cannot be combined with --skip-search-facets"
|
||||
)
|
||||
if args.publish_content:
|
||||
raise SeederError(
|
||||
"--publish-content is not applicable to --seed-search-facets-only"
|
||||
)
|
||||
|
||||
engine = create_async_engine(
|
||||
args.database_url,
|
||||
pool_pre_ping=True,
|
||||
connect_args={"server_settings": {"search_path": f"{args.schema},public"}},
|
||||
)
|
||||
session_factory = async_sessionmaker(
|
||||
engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False,
|
||||
autoflush=False,
|
||||
)
|
||||
|
||||
try:
|
||||
async with session_factory() as session:
|
||||
# Do not wrap the complete seed run in session.begin().
|
||||
# SeedOrchestrator owns the transaction boundaries so each template
|
||||
# can commit independently.
|
||||
summaries = await SeedOrchestrator(session, schema=args.schema).process_config(
|
||||
config_path=args.config.resolve(),
|
||||
modules_root=args.modules_root.resolve(),
|
||||
module_codes=module_codes,
|
||||
template_codes=template_codes,
|
||||
content_codes=content_codes,
|
||||
search_facets_only=args.seed_search_facets_only,
|
||||
skip_search_facets=args.skip_search_facets,
|
||||
publish_content=args.publish_content,
|
||||
continue_on_error=args.continue_on_error,
|
||||
created_by=args.created_by,
|
||||
)
|
||||
|
||||
for summary in summaries:
|
||||
logger.info(
|
||||
"module=%s definition_sets=%s content_ok=%s content_failed=%s "
|
||||
"associations=%s stale_removed=%s",
|
||||
summary.module_code,
|
||||
summary.definition_sets_processed,
|
||||
summary.content_rows_succeeded,
|
||||
summary.content_rows_failed,
|
||||
summary.associations_written,
|
||||
summary.stale_associations_removed,
|
||||
)
|
||||
for error in summary.errors:
|
||||
logger.warning("module=%s error=%s", summary.module_code, error)
|
||||
return 0
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = build_parser().parse_args()
|
||||
logging.basicConfig(
|
||||
level=getattr(logging, args.log_level),
|
||||
format="%(asctime)s %(levelname)s %(name)s - %(message)s",
|
||||
)
|
||||
try:
|
||||
raise SystemExit(asyncio.run(run(args)))
|
||||
except Exception as exc:
|
||||
logger.exception("Seed failed: %s", exc)
|
||||
raise SystemExit(1) from exc
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
@ -1,110 +0,0 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.seed_images.service import ImageSeedService
|
||||
from app.integrations.storage import ObjectStorage, StorageConfig
|
||||
from app.seed_images.validators import load_manifest
|
||||
|
||||
|
||||
def env(name: str, *, required: bool = True, default: str | None = None) -> str:
|
||||
value = os.getenv(name, default)
|
||||
if required and not value:
|
||||
raise RuntimeError(f"Required environment variable {name} is not set")
|
||||
return value or ""
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="seed-images",
|
||||
description="Seed AccuPedia content media from a manifest.",
|
||||
)
|
||||
parser.add_argument("--manifest", type=Path, required=True)
|
||||
parser.add_argument("--media-root", type=Path, required=True)
|
||||
parser.add_argument("--validate-only", action="store_true")
|
||||
parser.add_argument("--dry-run", action="store_true")
|
||||
parser.add_argument("--force-reupload", action="store_true")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
async def async_main() -> int:
|
||||
args = parse_args()
|
||||
manifest = load_manifest(args.manifest)
|
||||
|
||||
database_url = env("DATABASE_URL")
|
||||
endpoint = env("MINIO_ENDPOINT")
|
||||
access_key = env("MINIO_ACCESS_KEY")
|
||||
secret_key = env("MINIO_SECRET_KEY")
|
||||
bucket = env("MINIO_BUCKET", default="accupedia-media", required=False)
|
||||
secure = env("MINIO_SECURE", default="false", required=False).lower() in {
|
||||
"1", "true", "yes", "on"
|
||||
}
|
||||
|
||||
engine = create_async_engine(
|
||||
database_url,
|
||||
pool_pre_ping=True,
|
||||
future=True,
|
||||
)
|
||||
Session = async_sessionmaker(engine, expire_on_commit=False)
|
||||
|
||||
storage = ObjectStorage(
|
||||
StorageConfig(
|
||||
endpoint=endpoint,
|
||||
access_key=access_key,
|
||||
secret_key=secret_key,
|
||||
bucket=bucket,
|
||||
secure=secure,
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
async with Session() as session:
|
||||
service = ImageSeedService(
|
||||
session=session,
|
||||
storage=storage,
|
||||
bucket=bucket,
|
||||
)
|
||||
report = await service.seed(
|
||||
manifest=manifest,
|
||||
media_root=args.media_root,
|
||||
dry_run=args.dry_run,
|
||||
validate_only=args.validate_only,
|
||||
force_reupload=args.force_reupload,
|
||||
)
|
||||
|
||||
payload = {
|
||||
"ok": report.ok,
|
||||
"total": report.total,
|
||||
"validated": report.validated,
|
||||
"uploaded": report.uploaded,
|
||||
"reused_objects": report.reused_objects,
|
||||
"db_upserts": report.db_upserts,
|
||||
"associations_upserted": report.associations_upserted,
|
||||
"errors": [
|
||||
{
|
||||
"index": issue.index,
|
||||
"file": issue.file,
|
||||
"message": issue.message,
|
||||
}
|
||||
for issue in report.errors
|
||||
],
|
||||
}
|
||||
print(json.dumps(payload, indent=2))
|
||||
return 0 if report.ok else 2
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
raise SystemExit(asyncio.run(async_main()))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
@ -16,6 +16,7 @@ from app.features.admin import router as admin_router
|
|||
from app.features.webhooks import router as webhooks_router
|
||||
|
||||
from app.cache.health import router as cache_health_router
|
||||
|
||||
from app.core.settings import settings
|
||||
from app.db.session import get_db_session
|
||||
from app.lifespan import lifespan
|
||||
|
|
|
|||
|
Before Width: | Height: | Size: 50 KiB After Width: | Height: | Size: 77 KiB |
|
Before Width: | Height: | Size: 70 KiB After Width: | Height: | Size: 111 KiB |
|
After Width: | Height: | Size: 984 KiB |
|
Before Width: | Height: | Size: 83 KiB After Width: | Height: | Size: 140 KiB |
|
Before Width: | Height: | Size: 889 KiB After Width: | Height: | Size: 984 KiB |
|
Before Width: | Height: | Size: 83 KiB After Width: | Height: | Size: 140 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 140 KiB |
|
Before Width: | Height: | Size: 83 KiB After Width: | Height: | Size: 140 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 141 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 142 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 141 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 141 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 142 KiB |
|
Before Width: | Height: | Size: 82 KiB After Width: | Height: | Size: 137 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 142 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 140 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 141 KiB |
|
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 143 KiB |
|
Before Width: | Height: | Size: 83 KiB After Width: | Height: | Size: 141 KiB |
|
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 72 KiB |
|
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 72 KiB |
|
Before Width: | Height: | Size: 56 KiB After Width: | Height: | Size: 89 KiB |
|
Before Width: | Height: | Size: 56 KiB After Width: | Height: | Size: 90 KiB |
|
Before Width: | Height: | Size: 56 KiB After Width: | Height: | Size: 88 KiB |
|
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 143 KiB |
|
Before Width: | Height: | Size: 56 KiB After Width: | Height: | Size: 88 KiB |
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 86 KiB |
|
Before Width: | Height: | Size: 64 KiB After Width: | Height: | Size: 104 KiB |
|
Before Width: | Height: | Size: 56 KiB After Width: | Height: | Size: 88 KiB |
|
Before Width: | Height: | Size: 59 KiB After Width: | Height: | Size: 94 KiB |
|
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 80 KiB |
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 87 KiB |
|
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 136 KiB |
|
Before Width: | Height: | Size: 79 KiB After Width: | Height: | Size: 125 KiB |
|
Before Width: | Height: | Size: 68 KiB After Width: | Height: | Size: 110 KiB |
|
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 144 KiB |
|
Before Width: | Height: | Size: 71 KiB After Width: | Height: | Size: 112 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 141 KiB |
|
Before Width: | Height: | Size: 87 KiB After Width: | Height: | Size: 147 KiB |
|
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 143 KiB |
|
Before Width: | Height: | Size: 86 KiB After Width: | Height: | Size: 145 KiB |
|
Before Width: | Height: | Size: 87 KiB After Width: | Height: | Size: 146 KiB |
|
Before Width: | Height: | Size: 86 KiB After Width: | Height: | Size: 145 KiB |
|
Before Width: | Height: | Size: 86 KiB After Width: | Height: | Size: 146 KiB |
|
Before Width: | Height: | Size: 86 KiB After Width: | Height: | Size: 146 KiB |
|
Before Width: | Height: | Size: 86 KiB After Width: | Height: | Size: 145 KiB |
|
Before Width: | Height: | Size: 70 KiB After Width: | Height: | Size: 113 KiB |
|
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 143 KiB |
|
Before Width: | Height: | Size: 86 KiB After Width: | Height: | Size: 146 KiB |
|
Before Width: | Height: | Size: 87 KiB After Width: | Height: | Size: 147 KiB |
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 92 KiB |
|
Before Width: | Height: | Size: 59 KiB After Width: | Height: | Size: 93 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 137 KiB |
|
Before Width: | Height: | Size: 71 KiB After Width: | Height: | Size: 113 KiB |
|
Before Width: | Height: | Size: 68 KiB After Width: | Height: | Size: 105 KiB |
|
Before Width: | Height: | Size: 73 KiB After Width: | Height: | Size: 114 KiB |
|
Before Width: | Height: | Size: 68 KiB After Width: | Height: | Size: 106 KiB |
|
Before Width: | Height: | Size: 69 KiB After Width: | Height: | Size: 112 KiB |
|
Before Width: | Height: | Size: 66 KiB After Width: | Height: | Size: 107 KiB |
|
Before Width: | Height: | Size: 63 KiB After Width: | Height: | Size: 100 KiB |
|
Before Width: | Height: | Size: 60 KiB After Width: | Height: | Size: 94 KiB |
|
Before Width: | Height: | Size: 66 KiB After Width: | Height: | Size: 108 KiB |
|
Before Width: | Height: | Size: 68 KiB After Width: | Height: | Size: 112 KiB |
|
Before Width: | Height: | Size: 68 KiB After Width: | Height: | Size: 110 KiB |
|
Before Width: | Height: | Size: 68 KiB After Width: | Height: | Size: 110 KiB |
|
Before Width: | Height: | Size: 61 KiB After Width: | Height: | Size: 99 KiB |
|
Before Width: | Height: | Size: 72 KiB After Width: | Height: | Size: 117 KiB |
|
Before Width: | Height: | Size: 68 KiB After Width: | Height: | Size: 112 KiB |
|
Before Width: | Height: | Size: 79 KiB After Width: | Height: | Size: 129 KiB |
|
Before Width: | Height: | Size: 50 KiB After Width: | Height: | Size: 77 KiB |
|
Before Width: | Height: | Size: 55 KiB After Width: | Height: | Size: 86 KiB |
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 86 KiB |
|
Before Width: | Height: | Size: 61 KiB After Width: | Height: | Size: 98 KiB |
|
Before Width: | Height: | Size: 55 KiB After Width: | Height: | Size: 86 KiB |
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 86 KiB |
|
Before Width: | Height: | Size: 60 KiB After Width: | Height: | Size: 97 KiB |
|
Before Width: | Height: | Size: 73 KiB After Width: | Height: | Size: 119 KiB |
|
Before Width: | Height: | Size: 66 KiB After Width: | Height: | Size: 107 KiB |
|
Before Width: | Height: | Size: 66 KiB After Width: | Height: | Size: 108 KiB |
|
Before Width: | Height: | Size: 66 KiB After Width: | Height: | Size: 109 KiB |
|
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 68 KiB |
|
Before Width: | Height: | Size: 67 KiB After Width: | Height: | Size: 109 KiB |
|
Before Width: | Height: | Size: 72 KiB After Width: | Height: | Size: 118 KiB |
|
Before Width: | Height: | Size: 74 KiB After Width: | Height: | Size: 122 KiB |
|
Before Width: | Height: | Size: 75 KiB After Width: | Height: | Size: 126 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 141 KiB |
|
Before Width: | Height: | Size: 41 KiB After Width: | Height: | Size: 61 KiB |
|
Before Width: | Height: | Size: 41 KiB After Width: | Height: | Size: 60 KiB |
|
Before Width: | Height: | Size: 40 KiB After Width: | Height: | Size: 60 KiB |
|
Before Width: | Height: | Size: 41 KiB After Width: | Height: | Size: 60 KiB |
|
Before Width: | Height: | Size: 40 KiB After Width: | Height: | Size: 60 KiB |
|
Before Width: | Height: | Size: 55 KiB After Width: | Height: | Size: 87 KiB |
|
Before Width: | Height: | Size: 55 KiB After Width: | Height: | Size: 86 KiB |