|
@@ -0,0 +1,67 @@
|
|
|
|
|
+"""
|
|
|
|
|
+将 coordinate 项目的本地 JSON 配置文件批量导入到 webapp 的数据库中。
|
|
|
|
|
+
|
|
|
|
|
+用法示例:
|
|
|
|
|
+ python3 scripts/import_coordinate_configs.py --sentinel /Users/hujiarui/workspace/coordinate/config/config_sentinel.json
|
|
|
|
|
+ python3 scripts/import_coordinate_configs.py --booker /Users/hujiarui/workspace/coordinate/config/config_booker.json
|
|
|
|
|
+ python3 scripts/import_coordinate_configs.py --standalone /Users/hujiarui/workspace/coordinate/config/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.configuration_service import ConfigurationService
|
|
|
|
|
+from app.schemas.configuration import ConfigurationCreate, ConfigurationUpdate
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+async def import_configs(sentinel_file=None, booker_file=None, standalone_file=None, node_id=None):
|
|
|
|
|
+ async with AsyncSessionLocal() as db:
|
|
|
|
|
+ items = [
|
|
|
|
|
+ (sentinel_file, f"sentinel_config:{node_id}" if node_id else "sentinel_config", "Sentinel 哨兵组配置"),
|
|
|
|
|
+ (booker_file, f"booker_config:{node_id}" if node_id else "booker_config", "Booker 预约组配置"),
|
|
|
|
|
+ (standalone_file, f"standalone_config:{node_id}" if node_id else "standalone_config", "Standalone Booker 配置"),
|
|
|
|
|
+ ]
|
|
|
|
|
+
|
|
|
|
|
+ for file_path, key, desc in items:
|
|
|
|
|
+ if file_path and os.path.exists(file_path):
|
|
|
|
|
+ with open(file_path, "r", encoding="utf-8") as f:
|
|
|
|
|
+ data = json.load(f)
|
|
|
|
|
+ val_str = json.dumps(data, ensure_ascii=False, indent=2)
|
|
|
|
|
+ try:
|
|
|
|
|
+ await ConfigurationService.create(
|
|
|
|
|
+ db,
|
|
|
|
|
+ ConfigurationCreate(
|
|
|
|
|
+ config_key=key,
|
|
|
|
|
+ config_value=val_str,
|
|
|
|
|
+ description=desc,
|
|
|
|
|
+ type="json"
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ await ConfigurationService.update_by_key(
|
|
|
|
|
+ db,
|
|
|
|
|
+ key,
|
|
|
|
|
+ ConfigurationUpdate(
|
|
|
|
|
+ config_value=val_str,
|
|
|
|
|
+ description=desc,
|
|
|
|
|
+ type="json"
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
|
|
+ print(f"✅ Successfully imported config for key '{key}'.")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == '__main__':
|
|
|
|
|
+ parser = argparse.ArgumentParser(description="Import coordinate JSON configs to webapp DB")
|
|
|
|
|
+ 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("--node-id", type=str, help="Target node ID (optional)")
|
|
|
|
|
+ args = parser.parse_args()
|
|
|
|
|
+
|
|
|
|
|
+ asyncio.run(import_configs(args.sentinel, args.booker, args.standalone, args.node_id))
|