import_node_configurations.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """
  2. 将 Coordinate 项目的本地 JSON 配置文件 (config_sentinel.json, config_booker.json, config_standalone.json)
  3. 批量或按节点导入到 webapp 的 node_configurations 表中。
  4. 用法示例:
  5. python3 scripts/import_node_configurations.py --node-id node01 \
  6. --sentinel /path/to/config_sentinel.json \
  7. --booker /path/to/config_booker.json \
  8. --standalone /path/to/config_standalone.json
  9. """
  10. import os
  11. import sys
  12. import json
  13. import asyncio
  14. import argparse
  15. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  16. from app.core.database import AsyncSessionLocal
  17. from app.services.node_configuration_service import NodeConfigurationService
  18. from app.schemas.node_configuration import NodeConfigurationSaveRequest
  19. async def import_configs(
  20. node_id: str,
  21. sentinel_file: str = None,
  22. booker_file: str = None,
  23. standalone_file: str = None,
  24. description: str = None,
  25. operator: str = "import_script",
  26. ):
  27. if not node_id:
  28. print("❌ Error: --node-id is required")
  29. return
  30. items = [
  31. ("sentinel", sentinel_file, "哨兵轮询配置"),
  32. ("booker", booker_file, "预约抢单配置"),
  33. ("standalone", standalone_file, "独立 Booker 配置"),
  34. ]
  35. async with AsyncSessionLocal() as db:
  36. for role, file_path, default_desc in items:
  37. if not file_path:
  38. continue
  39. if not os.path.exists(file_path):
  40. print(f"⚠️ [SKIP] File not found: {file_path}")
  41. continue
  42. try:
  43. with open(file_path, "r", encoding="utf-8") as f:
  44. content = json.load(f)
  45. except Exception as e:
  46. print(f"❌ [ERROR] Failed to parse JSON file '{file_path}': {e}")
  47. continue
  48. req = NodeConfigurationSaveRequest(
  49. role=role,
  50. node_id=node_id,
  51. config_content=content,
  52. is_enabled=1,
  53. description=description or f"{node_id} {default_desc} (imported from {os.path.basename(file_path)})",
  54. operator=operator,
  55. remark=f"Initial import from {file_path}",
  56. )
  57. try:
  58. saved = await NodeConfigurationService.save_config(db, req, operator=operator)
  59. print(f"✅ [SUCCESS] Imported {role} for node '{node_id}' | hash={saved.config_hash} (id={saved.id})")
  60. except Exception as e:
  61. print(f"❌ [ERROR] Failed to save {role} config for node '{node_id}': {e}")
  62. if __name__ == "__main__":
  63. parser = argparse.ArgumentParser(description="Import Coordinate JSON configs to node_configurations table")
  64. parser.add_argument("--node-id", type=str, required=True, help="Target node ID, e.g. node01")
  65. parser.add_argument("--sentinel", type=str, help="Path to config_sentinel.json")
  66. parser.add_argument("--booker", type=str, help="Path to config_booker.json")
  67. parser.add_argument("--standalone", type=str, help="Path to config_standalone.json")
  68. parser.add_argument("--description", type=str, help="Custom description")
  69. parser.add_argument("--operator", type=str, default="migration_script", help="Operator name for audit log")
  70. args = parser.parse_args()
  71. asyncio.run(
  72. import_configs(
  73. node_id=args.node_id,
  74. sentinel_file=args.sentinel,
  75. booker_file=args.booker,
  76. standalone_file=args.standalone,
  77. description=args.description,
  78. operator=args.operator,
  79. )
  80. )