| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- """
- 将 Coordinate 项目的本地 JSON 配置文件 (config_sentinel.json, config_booker.json, config_standalone.json)
- 批量或按节点导入到 webapp 的 node_configurations 表中。
- 用法示例:
- python3 scripts/import_node_configurations.py --node-id node01 \
- --sentinel /path/to/config_sentinel.json \
- --booker /path/to/config_booker.json \
- --standalone /path/to/config_standalone.json
- """
- import os
- import sys
- import json
- import asyncio
- import argparse
- sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
- from app.core.database import AsyncSessionLocal
- from app.services.node_configuration_service import NodeConfigurationService
- from app.schemas.node_configuration import NodeConfigurationSaveRequest
- async def import_configs(
- node_id: str,
- sentinel_file: str = None,
- booker_file: str = None,
- standalone_file: str = None,
- description: str = None,
- operator: str = "import_script",
- ):
- if not node_id:
- print("❌ Error: --node-id is required")
- return
- items = [
- ("sentinel", sentinel_file, "哨兵轮询配置"),
- ("booker", booker_file, "预约抢单配置"),
- ("standalone", standalone_file, "独立 Booker 配置"),
- ]
- async with AsyncSessionLocal() as db:
- for role, file_path, default_desc in items:
- if not file_path:
- continue
- if not os.path.exists(file_path):
- print(f"⚠️ [SKIP] File not found: {file_path}")
- continue
- try:
- with open(file_path, "r", encoding="utf-8") as f:
- content = json.load(f)
- except Exception as e:
- print(f"❌ [ERROR] Failed to parse JSON file '{file_path}': {e}")
- continue
- req = NodeConfigurationSaveRequest(
- role=role,
- node_id=node_id,
- config_content=content,
- is_enabled=1,
- description=description or f"{node_id} {default_desc} (imported from {os.path.basename(file_path)})",
- operator=operator,
- remark=f"Initial import from {file_path}",
- )
- try:
- saved = await NodeConfigurationService.save_config(db, req, operator=operator)
- print(f"✅ [SUCCESS] Imported {role} for node '{node_id}' | hash={saved.config_hash} (id={saved.id})")
- except Exception as e:
- print(f"❌ [ERROR] Failed to save {role} config for node '{node_id}': {e}")
- if __name__ == "__main__":
- parser = argparse.ArgumentParser(description="Import Coordinate JSON configs to node_configurations table")
- parser.add_argument("--node-id", type=str, required=True, help="Target node ID, e.g. node01")
- parser.add_argument("--sentinel", type=str, help="Path to config_sentinel.json")
- parser.add_argument("--booker", type=str, help="Path to config_booker.json")
- parser.add_argument("--standalone", type=str, help="Path to config_standalone.json")
- parser.add_argument("--description", type=str, help="Custom description")
- parser.add_argument("--operator", type=str, default="migration_script", help="Operator name for audit log")
- args = parser.parse_args()
- asyncio.run(
- import_configs(
- node_id=args.node_id,
- sentinel_file=args.sentinel,
- booker_file=args.booker,
- standalone_file=args.standalone,
- description=args.description,
- operator=args.operator,
- )
- )
|