plugin_service.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from collections.abc import Sequence
  2. from mimetypes import guess_type
  3. from core.helper.download import download_with_size_limit
  4. from core.helper.marketplace import download_plugin_pkg
  5. from core.plugin.entities.plugin import PluginDeclaration, PluginEntity, PluginInstallationSource
  6. from core.plugin.entities.plugin_daemon import PluginInstallTask
  7. from core.plugin.manager.asset import PluginAssetManager
  8. from core.plugin.manager.debugging import PluginDebuggingManager
  9. from core.plugin.manager.plugin import PluginInstallationManager
  10. class PluginService:
  11. @staticmethod
  12. def get_debugging_key(tenant_id: str) -> str:
  13. """
  14. get the debugging key of the tenant
  15. """
  16. manager = PluginDebuggingManager()
  17. return manager.get_debugging_key(tenant_id)
  18. @staticmethod
  19. def list(tenant_id: str) -> list[PluginEntity]:
  20. """
  21. list all plugins of the tenant
  22. """
  23. manager = PluginInstallationManager()
  24. return manager.list_plugins(tenant_id)
  25. @staticmethod
  26. def get_asset(tenant_id: str, asset_file: str) -> tuple[bytes, str]:
  27. """
  28. get the asset file of the plugin
  29. """
  30. manager = PluginAssetManager()
  31. # guess mime type
  32. mime_type, _ = guess_type(asset_file)
  33. return manager.fetch_asset(tenant_id, asset_file), mime_type or "application/octet-stream"
  34. @staticmethod
  35. def check_plugin_unique_identifier(tenant_id: str, plugin_unique_identifier: str) -> bool:
  36. """
  37. check if the plugin unique identifier is already installed by other tenant
  38. """
  39. manager = PluginInstallationManager()
  40. return manager.fetch_plugin_by_identifier(tenant_id, plugin_unique_identifier)
  41. @staticmethod
  42. def fetch_plugin_manifest(tenant_id: str, plugin_unique_identifier: str) -> PluginDeclaration:
  43. manager = PluginInstallationManager()
  44. return manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
  45. @staticmethod
  46. def fetch_install_tasks(tenant_id: str, page: int, page_size: int) -> Sequence[PluginInstallTask]:
  47. manager = PluginInstallationManager()
  48. return manager.fetch_plugin_installation_tasks(tenant_id, page, page_size)
  49. @staticmethod
  50. def fetch_install_task(tenant_id: str, task_id: str) -> PluginInstallTask:
  51. manager = PluginInstallationManager()
  52. return manager.fetch_plugin_installation_task(tenant_id, task_id)
  53. @staticmethod
  54. def delete_install_task(tenant_id: str, task_id: str) -> bool:
  55. manager = PluginInstallationManager()
  56. return manager.delete_plugin_installation_task(tenant_id, task_id)
  57. @staticmethod
  58. def upload_pkg(tenant_id: str, pkg: bytes, verify_signature: bool = False) -> str:
  59. """
  60. Upload plugin package files
  61. returns: plugin_unique_identifier
  62. """
  63. manager = PluginInstallationManager()
  64. return manager.upload_pkg(tenant_id, pkg, verify_signature)
  65. @staticmethod
  66. def upload_pkg_from_github(
  67. tenant_id: str, repo: str, version: str, package: str, verify_signature: bool = False
  68. ) -> str:
  69. """
  70. Install plugin from github release package files,
  71. returns plugin_unique_identifier
  72. """
  73. pkg = download_with_size_limit(
  74. f"https://github.com/{repo}/releases/download/{version}/{package}", 15 * 1024 * 1024
  75. )
  76. manager = PluginInstallationManager()
  77. return manager.upload_pkg(
  78. tenant_id,
  79. pkg,
  80. verify_signature,
  81. )
  82. @staticmethod
  83. def install_from_local_pkg(tenant_id: str, plugin_unique_identifiers: Sequence[str]):
  84. manager = PluginInstallationManager()
  85. return manager.install_from_identifiers(
  86. tenant_id,
  87. plugin_unique_identifiers,
  88. PluginInstallationSource.Package,
  89. {},
  90. )
  91. @staticmethod
  92. def install_from_github(tenant_id: str, plugin_unique_identifier: str, repo: str, version: str, package: str):
  93. """
  94. Install plugin from github release package files,
  95. returns plugin_unique_identifier
  96. """
  97. manager = PluginInstallationManager()
  98. return manager.install_from_identifiers(
  99. tenant_id,
  100. [plugin_unique_identifier],
  101. PluginInstallationSource.Github,
  102. {
  103. "repo": repo,
  104. "version": version,
  105. "package": package,
  106. },
  107. )
  108. @staticmethod
  109. def install_from_marketplace_pkg(
  110. tenant_id: str, plugin_unique_identifiers: Sequence[str], verify_signature: bool = False
  111. ):
  112. """
  113. Install plugin from marketplace package files,
  114. returns installation task id
  115. """
  116. manager = PluginInstallationManager()
  117. # check if already downloaded
  118. for plugin_unique_identifier in plugin_unique_identifiers:
  119. try:
  120. manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
  121. # already downloaded, skip
  122. except Exception:
  123. # plugin not installed, download and upload pkg
  124. pkg = download_plugin_pkg(plugin_unique_identifier)
  125. manager.upload_pkg(tenant_id, pkg, verify_signature)
  126. return manager.install_from_identifiers(
  127. tenant_id,
  128. plugin_unique_identifiers,
  129. PluginInstallationSource.Marketplace,
  130. {
  131. "plugin_unique_identifier": plugin_unique_identifier,
  132. },
  133. )
  134. @staticmethod
  135. def uninstall(tenant_id: str, plugin_installation_id: str) -> bool:
  136. manager = PluginInstallationManager()
  137. return manager.uninstall(tenant_id, plugin_installation_id)