plugin_service.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. import json
  2. import hashlib
  3. from typing import Optional, List, Dict, Any, Tuple
  4. from fastapi import UploadFile
  5. from sqlalchemy import select, func, delete
  6. from sqlalchemy.ext.asyncio import AsyncSession
  7. from app.core.biz_exception import BizLogicError
  8. from app.core.redis import get_redis_client
  9. from app.core.logger import logger
  10. from app.models.plugin import Plugin, PluginVersion
  11. from app.schemas.plugin import PluginUpdate
  12. from app.services.seaweedfs_service import SeaweedFSService
  13. class PluginService:
  14. @classmethod
  15. async def publish_plugin(
  16. cls,
  17. db: AsyncSession,
  18. name: str,
  19. version: str,
  20. display_name: Optional[str] = "",
  21. filename: Optional[str] = "",
  22. class_name: Optional[str] = "",
  23. protocol: Optional[str] = "IPlugin",
  24. description: Optional[str] = "",
  25. file: Optional[UploadFile] = None,
  26. download_url: Optional[str] = None,
  27. sha256: Optional[str] = "",
  28. changelog: Optional[str] = None,
  29. set_as_latest: bool = True,
  30. developer_id: Optional[str] = None,
  31. ) -> Dict[str, Any]:
  32. """
  33. 发布插件新版本:
  34. 1. 支持直接上传 .py 文件(通过 SeaweedFSService 存储并计算 sha256)或直接传入 download_url
  35. 2. 自动更新/创建 plugins 基础信息(记录开发管理者 developer_id、脚本文件名 filename、入口类名 class_name、协议 protocol)
  36. 3. 记录 plugin_versions 发布历史
  37. """
  38. if not name or not version:
  39. raise BizLogicError("Plugin name and version are required")
  40. final_download_url = download_url
  41. final_sha256 = (sha256 or "").strip()
  42. # 计算并推导文件名
  43. resolved_filename = (filename or "").strip()
  44. if file is not None and file.filename:
  45. if not resolved_filename:
  46. resolved_filename = file.filename
  47. file_bytes = await file.read()
  48. if not file_bytes:
  49. raise BizLogicError("Uploaded file is empty")
  50. final_sha256 = hashlib.sha256(file_bytes).hexdigest()
  51. await file.seek(0)
  52. # 上传至 SeaweedFS
  53. upload_result = await SeaweedFSService.upload(file)
  54. final_download_url = upload_result.get("url")
  55. if not final_download_url:
  56. raise BizLogicError("Failed to retrieve file download URL from storage")
  57. if not final_download_url:
  58. raise BizLogicError("Either file upload or download_url must be provided")
  59. if not resolved_filename:
  60. resolved_filename = f"{name}.py"
  61. resolved_class_name = (class_name or "").strip()
  62. resolved_protocol = (protocol or "").strip() or "IPlugin"
  63. # 1. 查找或创建 Plugin 基础记录
  64. stmt = select(Plugin).where(Plugin.name == name)
  65. res = await db.execute(stmt)
  66. plugin = res.scalar_one_or_none()
  67. dev_id = developer_id.strip() if (developer_id and developer_id.strip()) else None
  68. if plugin is None:
  69. # 若初次创建且类名未指定,智能推导驼峰类名,如 vfs_plugin -> VFSPlugin
  70. if not resolved_class_name:
  71. parts = [p.capitalize() for p in name.split("_") if p]
  72. resolved_class_name = "".join(parts) or name
  73. plugin = Plugin(
  74. name=name,
  75. display_name=display_name or name,
  76. filename=resolved_filename,
  77. class_name=resolved_class_name,
  78. protocol=resolved_protocol,
  79. latest_version=version,
  80. description=description or "",
  81. developer_id=dev_id,
  82. status=1,
  83. )
  84. db.add(plugin)
  85. await db.flush()
  86. else:
  87. if display_name:
  88. plugin.display_name = display_name
  89. if description is not None and description != "":
  90. plugin.description = description
  91. if developer_id is not None:
  92. plugin.developer_id = dev_id
  93. if resolved_filename:
  94. plugin.filename = resolved_filename
  95. if resolved_class_name:
  96. plugin.class_name = resolved_class_name
  97. if resolved_protocol:
  98. plugin.protocol = resolved_protocol
  99. if set_as_latest:
  100. plugin.latest_version = version
  101. # 2. 查找或创建 PluginVersion 历史版本记录
  102. v_stmt = select(PluginVersion).where(
  103. PluginVersion.plugin_id == plugin.id,
  104. PluginVersion.version == version,
  105. )
  106. v_res = await db.execute(v_stmt)
  107. pv = v_res.scalar_one_or_none()
  108. if pv is None:
  109. pv = PluginVersion(
  110. plugin_id=plugin.id,
  111. plugin_name=name,
  112. version=version,
  113. download_url=final_download_url,
  114. sha256=final_sha256,
  115. changelog=changelog,
  116. )
  117. db.add(pv)
  118. else:
  119. pv.download_url = final_download_url
  120. pv.sha256 = final_sha256
  121. if changelog is not None:
  122. pv.changelog = changelog
  123. if set_as_latest:
  124. plugin.latest_version = version
  125. await db.commit()
  126. await db.refresh(plugin)
  127. await db.refresh(pv)
  128. return {
  129. "plugin_id": plugin.id,
  130. "name": plugin.name,
  131. "version": pv.version,
  132. "download_url": pv.download_url,
  133. "sha256": pv.sha256,
  134. "class_name": plugin.class_name,
  135. "protocol": plugin.protocol,
  136. }
  137. @classmethod
  138. async def get_plugin_list(
  139. cls,
  140. db: AsyncSession,
  141. page: int = 1,
  142. page_size: int = 20,
  143. status: Optional[int] = None,
  144. ) -> Dict[str, Any]:
  145. """分页获取插件列表"""
  146. query = select(Plugin)
  147. count_query = select(func.count(Plugin.id))
  148. if status is not None:
  149. query = query.where(Plugin.status == status)
  150. count_query = count_query.where(Plugin.status == status)
  151. total_res = await db.execute(count_query)
  152. total = total_res.scalar() or 0
  153. query = query.order_by(Plugin.id.desc()).offset((page - 1) * page_size).limit(page_size)
  154. res = await db.execute(query)
  155. plugins = res.scalars().all()
  156. return {
  157. "list": plugins,
  158. "total": total,
  159. }
  160. @classmethod
  161. async def get_plugin_detail(cls, db: AsyncSession, plugin_id: int) -> Dict[str, Any]:
  162. """获取单个插件详情(包含所有历史发布版本)"""
  163. stmt = select(Plugin).where(Plugin.id == plugin_id)
  164. res = await db.execute(stmt)
  165. plugin = res.scalar_one_or_none()
  166. if not plugin:
  167. raise BizLogicError(f"Plugin with id {plugin_id} not found")
  168. v_stmt = select(PluginVersion).where(
  169. PluginVersion.plugin_id == plugin_id
  170. ).order_by(PluginVersion.id.desc())
  171. v_res = await db.execute(v_stmt)
  172. versions = v_res.scalars().all()
  173. return {
  174. "id": plugin.id,
  175. "name": plugin.name,
  176. "display_name": plugin.display_name,
  177. "filename": plugin.filename,
  178. "latest_version": plugin.latest_version,
  179. "description": plugin.description,
  180. "status": plugin.status,
  181. "developer_id": plugin.developer_id,
  182. "versions": versions,
  183. }
  184. @classmethod
  185. async def update_plugin(
  186. cls,
  187. db: AsyncSession,
  188. plugin_id: int,
  189. plugin_in: PluginUpdate,
  190. ) -> Plugin:
  191. """更新插件基础信息"""
  192. stmt = select(Plugin).where(Plugin.id == plugin_id)
  193. res = await db.execute(stmt)
  194. plugin = res.scalar_one_or_none()
  195. if not plugin:
  196. raise BizLogicError(f"Plugin with id {plugin_id} not found")
  197. if plugin_in.display_name is not None:
  198. plugin.display_name = plugin_in.display_name
  199. if plugin_in.filename is not None:
  200. plugin.filename = plugin_in.filename.strip()
  201. if plugin_in.class_name is not None:
  202. plugin.class_name = plugin_in.class_name.strip()
  203. if plugin_in.protocol is not None:
  204. plugin.protocol = plugin_in.protocol.strip()
  205. if plugin_in.description is not None:
  206. plugin.description = plugin_in.description
  207. if plugin_in.status is not None:
  208. plugin.status = plugin_in.status
  209. if plugin_in.developer_id is not None:
  210. dev_id = plugin_in.developer_id.strip()
  211. plugin.developer_id = dev_id if dev_id else None
  212. await db.commit()
  213. await db.refresh(plugin)
  214. return plugin
  215. @classmethod
  216. async def switch_version(
  217. cls,
  218. db: AsyncSession,
  219. plugin_id: int,
  220. version: str,
  221. ) -> Plugin:
  222. """切换插件的生效版本(一键回滚/切换推荐版本)"""
  223. stmt = select(Plugin).where(Plugin.id == plugin_id)
  224. res = await db.execute(stmt)
  225. plugin = res.scalar_one_or_none()
  226. if not plugin:
  227. raise BizLogicError(f"Plugin with id {plugin_id} not found")
  228. # 校验该版本是否存在
  229. v_stmt = select(PluginVersion).where(
  230. PluginVersion.plugin_id == plugin_id,
  231. PluginVersion.version == version,
  232. )
  233. v_res = await db.execute(v_stmt)
  234. pv = v_res.scalar_one_or_none()
  235. if not pv:
  236. raise BizLogicError(f"Version '{version}' does not exist for plugin '{plugin.name}'")
  237. plugin.latest_version = version
  238. await db.commit()
  239. await db.refresh(plugin)
  240. return plugin
  241. @classmethod
  242. async def delete_plugin(cls, db: AsyncSession, plugin_id: int) -> bool:
  243. """删除整套插件及其所有版本记录"""
  244. stmt = select(Plugin).where(Plugin.id == plugin_id)
  245. res = await db.execute(stmt)
  246. plugin = res.scalar_one_or_none()
  247. if not plugin:
  248. raise BizLogicError(f"Plugin with id {plugin_id} not found")
  249. # 删除所有版本
  250. await db.execute(
  251. delete(PluginVersion).where(PluginVersion.plugin_id == plugin_id)
  252. )
  253. # 删除插件主体
  254. await db.delete(plugin)
  255. await db.commit()
  256. return True
  257. @classmethod
  258. async def delete_version(cls, db: AsyncSession, plugin_id: int, version: str) -> bool:
  259. """删除单个历史版本记录(禁止直接删除当前生效的 latest_version)"""
  260. stmt = select(Plugin).where(Plugin.id == plugin_id)
  261. res = await db.execute(stmt)
  262. plugin = res.scalar_one_or_none()
  263. if not plugin:
  264. raise BizLogicError(f"Plugin with id {plugin_id} not found")
  265. if plugin.latest_version == version:
  266. raise BizLogicError(
  267. f"Cannot delete active latest version '{version}'. Please switch to another version first."
  268. )
  269. v_stmt = select(PluginVersion).where(
  270. PluginVersion.plugin_id == plugin_id,
  271. PluginVersion.version == version,
  272. )
  273. v_res = await db.execute(v_stmt)
  274. pv = v_res.scalar_one_or_none()
  275. if not pv:
  276. raise BizLogicError(f"Version '{version}' not found for plugin '{plugin.name}'")
  277. await db.delete(pv)
  278. await db.commit()
  279. return True
  280. @classmethod
  281. async def dispatch_upgrade(
  282. cls,
  283. db: AsyncSession,
  284. target_node: str = "all",
  285. plugins: Optional[List[str]] = None,
  286. ) -> List[Dict[str, Any]]:
  287. """
  288. 下发插件升级命令:
  289. 1. 组装指定插件(或所有已启用插件)的最新版本配置
  290. 2. 通过 Redis 发布到指定频道广播通知节点 Upgrade-Listener
  291. """
  292. query = select(Plugin).where(Plugin.status == 1)
  293. if plugins and len(plugins) > 0:
  294. query = query.where(Plugin.name.in_(plugins))
  295. res = await db.execute(query)
  296. target_plugins = res.scalars().all()
  297. payload = []
  298. for p in target_plugins:
  299. # 获取对应的 latest_version 记录
  300. v_stmt = select(PluginVersion).where(
  301. PluginVersion.plugin_id == p.id,
  302. PluginVersion.version == p.latest_version,
  303. )
  304. v_res = await db.execute(v_stmt)
  305. pv = v_res.scalar_one_or_none()
  306. if pv:
  307. payload.append({
  308. "name": p.name,
  309. "filename": p.filename or f"{p.name}.py",
  310. "version": p.latest_version,
  311. "download_url": pv.download_url,
  312. "sha256": pv.sha256,
  313. })
  314. # 确定广播频道
  315. if not target_node or target_node.strip() == "" or target_node == "all":
  316. channel = "vs:channel:upgrade"
  317. else:
  318. channel = f"vs:channel:upgrade:{target_node.strip()}"
  319. try:
  320. redis_client = await get_redis_client()
  321. await redis_client.publish(channel, json.dumps(payload))
  322. logger.info(f"🚀 [Plugin Upgrade] Dispatched {len(payload)} plugins to Redis channel '{channel}'")
  323. except Exception as e:
  324. logger.error(f"❌ [Plugin Upgrade] Failed to publish upgrade to Redis: {e}", exc_info=True)
  325. raise BizLogicError(f"Failed to publish upgrade notification to Redis: {str(e)}")
  326. return payload
  327. @classmethod
  328. async def get_latest_plugins(cls, db: AsyncSession) -> List[Dict[str, Any]]:
  329. """获取当前所有已启用的最新可用插件清单(供节点主动拉取)"""
  330. stmt = select(Plugin).where(Plugin.status == 1).order_by(Plugin.id.asc())
  331. res = await db.execute(stmt)
  332. plugins = res.scalars().all()
  333. result = []
  334. for p in plugins:
  335. v_stmt = select(PluginVersion).where(
  336. PluginVersion.plugin_id == p.id,
  337. PluginVersion.version == p.latest_version,
  338. )
  339. v_res = await db.execute(v_stmt)
  340. pv = v_res.scalar_one_or_none()
  341. result.append({
  342. "name": p.name,
  343. "display_name": p.display_name,
  344. "filename": p.filename or f"{p.name}.py",
  345. "class_name": p.class_name or "",
  346. "protocol": p.protocol or "IPlugin",
  347. "version": p.latest_version,
  348. "description": p.description,
  349. "download_url": pv.download_url if pv else "",
  350. "sha256": pv.sha256 if pv else "",
  351. "changelog": pv.changelog if pv else "",
  352. })
  353. return result