UnisKB/deploy_artifacts/seed_local_v2_parsers.py

131 lines
6.2 KiB
Python

"""Optionally register local MinerU 2.7.6 backends and grant two built-in roles.
Run in the MaxKB container; preview is read-only unless --apply is passed.
Existing conflicting resources/grants cause a transaction rollback, never overwrite.
"""
import argparse
import json
import os
import sys
from urllib.parse import urlsplit
from urllib.request import urlopen
import uuid
APP_ROOT = '/opt/maxkb-app'
sys.path.insert(0, APP_ROOT + '/apps')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'maxkb.settings')
import django
django.setup()
from django.db import transaction
from system_manage.ai_hub import parser_save
from system_manage.models.ai_hub import AIParserResource
from system_manage.models.platform_access import PlatformGrant, PlatformRole
from system_manage.parsers.catalog import validate_options
from system_manage.platform_access import ADMIN_ROLE_ID, DEFAULT_ROLE_ID, BUILTIN_ADMIN_USER_ID, is_admin
from users.models import User
WORKSPACE = 'default'
VERSION = '2.7.6'
RESOURCES = (
('pipeline', '传统管道', 'pipeline', '本地 V2 - Pipeline'),
('hybrid', '混合引擎', 'hybrid-auto-engine', '本地 V2 - Hybrid'),
('vlm', 'VLM引擎', 'vlm-auto-engine', '本地 V2 - VLM'),
)
ROLES = (ADMIN_ROLE_ID, DEFAULT_ROLE_ID)
def resource_id(key):
return uuid.uuid5(uuid.NAMESPACE_URL, 'unis:local-parser:v2:2.7.6:' + key)
def endpoint_value(value):
value = value.rstrip('/')
parsed = urlsplit(value)
if parsed.scheme not in ('http', 'https') or not parsed.hostname or parsed.username or parsed.password or parsed.query or parsed.fragment:
raise ValueError('解析服务地址无效')
return value
def verify_health(endpoint):
with urlopen(endpoint + '/health', timeout=10) as response:
payload = json.load(response)
if not isinstance(payload, dict) or str(payload.get('version')) != VERSION:
raise ValueError('解析服务健康检查失败或版本不是 2.7.6')
def seed(endpoint, apply):
endpoint = endpoint_value(endpoint)
verify_health(endpoint)
result = {'mode': 'apply' if apply else 'preview', 'version': VERSION, 'resources': [], 'grants_created': 0, 'grants_to_create': 0}
with transaction.atomic():
# Serialize concurrent runs and fail before any write if built-in roles are unavailable.
roles = list(PlatformRole.objects.select_for_update().filter(id__in=ROLES).order_by('id'))
if {str(role.id) for role in roles} != set(ROLES) or any(not role.enabled for role in roles):
raise ValueError('管理员或普通用户角色不存在或已停用')
default = next(role for role in roles if str(role.id) == DEFAULT_ROLE_ID)
if not default.is_default:
raise ValueError('普通用户角色不是默认角色')
admin = User.objects.filter(id=BUILTIN_ADMIN_USER_ID, is_active=True).first()
if not admin or not is_admin(admin):
raise ValueError('内置管理员账号不存在、停用或尚未授予统一管理员角色')
for key, name, backend, previous_name in RESOURCES:
pk = resource_id(key)
options = validate_options({'protocol': 'legacy', 'engine_version': VERSION,
'backend': backend, 'language': 'ch'}, endpoint)
obj = AIParserResource.objects.filter(pk=pk).first()
existed = obj is not None
if obj:
if (obj.workspace_id != WORKSPACE or obj.name not in (name, previous_name) or obj.endpoint != endpoint
or obj.options != options or not obj.enabled or obj.credential):
raise ValueError('已有初始化资源与预期不一致:' + name)
if AIParserResource.objects.filter(workspace_id=WORKSPACE, name=name).exclude(pk=pk).exists():
raise ValueError('已存在同名解析资源,请先人工核对:' + name)
rename = bool(obj and obj.name == previous_name)
grants = []
for role_id in ROLES:
grant = PlatformGrant.objects.filter(workspace_id=WORKSPACE, subject_type='ROLE',
subject_id=role_id, resource_type='PARSER', target=str(pk)).first()
if grant and ('USE' not in (grant.actions or []) or grant.expires_at is not None):
raise ValueError('已有角色授权与预期不一致,请先人工核对:' + name)
grants.append((role_id, grant))
if grant is None:
result['grants_to_create'] += 1
if apply and obj is None:
obj = AIParserResource(id=pk, workspace_id=WORKSPACE, owner_id=admin.id,
name=name, endpoint=endpoint)
parser_save(WORKSPACE, admin, {'name': name, 'endpoint': endpoint, 'options': options}, obj)
elif apply and rename:
parser_save(WORKSPACE, admin, {'name': name}, obj)
for role_id, grant in grants:
if apply and grant is None:
PlatformGrant.objects.create(workspace_id=WORKSPACE, subject_type='ROLE',
subject_id=role_id, resource_type='PARSER', target=str(pk),
actions=['VIEW', 'USE'], granted_by_id=admin.id)
result['grants_created'] += 1
result['resources'].append({'name': name, 'backend': backend,
'action': ('renamed' if apply else 'rename') if rename else
('existing' if existed else ('created' if apply else 'create'))})
return result
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--endpoint', default='http://mineru-api:8000', help='MinerU 2.7.6 API base URL')
parser.add_argument('--apply', action='store_true', help='Create the resources and role grants')
args = parser.parse_args()
print(json.dumps(seed(args.endpoint, args.apply), ensure_ascii=False))
if __name__ == '__main__':
try:
main()
except Exception as exc:
# Avoid dumping connection details or private configuration.
if isinstance(exc, ValueError):
print('初始化失败:' + str(exc), file=sys.stderr)
else:
print('初始化失败:' + type(exc).__name__, file=sys.stderr)
sys.exit(1)