refactor: plugin installation

This commit is contained in:
Yeuoly
2024-10-14 17:52:29 +08:00
parent 8e1cf3233c
commit 276701e1b7
5 changed files with 277 additions and 128 deletions

View File

@ -1,7 +1,6 @@
import io
import json
from flask import Response, request, send_file
from flask import request, send_file
from flask_login import current_user
from flask_restful import Resource, reqparse
from werkzeug.exceptions import Forbidden
@ -11,7 +10,6 @@ from controllers.console import api
from controllers.console.setup import setup_required
from controllers.console.wraps import account_initialization_required
from core.model_runtime.utils.encoders import jsonable_encoder
from core.plugin.entities.plugin_daemon import InstallPluginMessage
from libs.login import login_required
from services.plugin.plugin_service import PluginService
@ -59,37 +57,63 @@ class PluginIconApi(Resource):
return send_file(io.BytesIO(icon_bytes), mimetype=mimetype, max_age=icon_cache_max_age)
class PluginInstallCheckUniqueIdentifierApi(Resource):
@setup_required
@login_required
@account_initialization_required
def get(self):
req = reqparse.RequestParser()
req.add_argument("plugin_unique_identifier", type=str, required=True, location="args")
args = req.parse_args()
user = current_user
tenant_id = user.current_tenant_id
return {"installed": PluginService.check_plugin_unique_identifier(tenant_id, args["plugin_unique_identifier"])}
class PluginInstallFromUniqueIdentifierApi(Resource):
class PluginUploadPkgApi(Resource):
@setup_required
@login_required
@account_initialization_required
def post(self):
req = reqparse.RequestParser()
req.add_argument("plugin_unique_identifier", type=str, required=True, location="json")
args = req.parse_args()
user = current_user
if not user.is_admin_or_owner:
raise Forbidden()
tenant_id = user.current_tenant_id
file = request.files["pkg"]
content = file.read()
return {"plugin_unique_identifier": PluginService.upload_pkg(tenant_id, content)}
class PluginUploadFromPkgApi(Resource):
@setup_required
@login_required
@account_initialization_required
def post(self):
user = current_user
if not user.is_admin_or_owner:
raise Forbidden()
tenant_id = user.current_tenant_id
return {"success": PluginService.install_from_unique_identifier(tenant_id, args["plugin_unique_identifier"])}
file = request.files["pkg"]
content = file.read()
response = PluginService.upload_pkg(tenant_id, content)
return {
"plugin_unique_identifier": response,
}
class PluginUploadFromGithubApi(Resource):
@setup_required
@login_required
@account_initialization_required
def post(self):
user = current_user
if not user.is_admin_or_owner:
raise Forbidden()
tenant_id = user.current_tenant_id
parser = reqparse.RequestParser()
parser.add_argument("repo", type=str, required=True, location="json")
parser.add_argument("version", type=str, required=True, location="json")
parser.add_argument("package", type=str, required=True, location="json")
args = parser.parse_args()
response = PluginService.upload_pkg_from_github(tenant_id, args["repo"], args["version"], args["package"])
return {
"plugin_unique_identifier": response,
}
class PluginInstallFromPkgApi(Resource):
@ -103,19 +127,15 @@ class PluginInstallFromPkgApi(Resource):
tenant_id = user.current_tenant_id
file = request.files["pkg"]
content = file.read()
parser = reqparse.RequestParser()
parser.add_argument("plugin_unique_identifier", type=str, required=True, location="json")
args = parser.parse_args()
def generator():
try:
response = PluginService.install_from_local_pkg(tenant_id, content)
for message in response:
yield f"data: {json.dumps(jsonable_encoder(message))}\n\n"
except ValueError as e:
error_message = InstallPluginMessage(event=InstallPluginMessage.Event.Error, data=str(e))
yield f"data: {json.dumps(jsonable_encoder(error_message))}\n\n"
response = PluginService.install_from_local_pkg(tenant_id, args["plugin_unique_identifier"])
return Response(generator(), mimetype="text/event-stream")
return {
"task_id": response,
}
class PluginInstallFromGithubApi(Resource):
@ -133,20 +153,16 @@ class PluginInstallFromGithubApi(Resource):
parser.add_argument("repo", type=str, required=True, location="json")
parser.add_argument("version", type=str, required=True, location="json")
parser.add_argument("package", type=str, required=True, location="json")
parser.add_argument("plugin_unique_identifier", type=str, required=True, location="json")
args = parser.parse_args()
def generator():
try:
response = PluginService.install_from_github_pkg(
tenant_id, args["repo"], args["version"], args["package"]
)
for message in response:
yield f"data: {json.dumps(jsonable_encoder(message))}\n\n"
except ValueError as e:
error_message = InstallPluginMessage(event=InstallPluginMessage.Event.Error, data=str(e))
yield f"data: {json.dumps(jsonable_encoder(error_message))}\n\n"
response = PluginService.install_from_github(
tenant_id, args["repo"], args["version"], args["package"], args["plugin_unique_identifier"]
)
return Response(generator(), mimetype="text/event-stream")
return {
"task_id": response,
}
class PluginInstallFromMarketplaceApi(Resource):
@ -164,16 +180,55 @@ class PluginInstallFromMarketplaceApi(Resource):
parser.add_argument("plugin_unique_identifier", type=str, required=True, location="json")
args = parser.parse_args()
def generator():
try:
response = PluginService.install_from_marketplace_pkg(tenant_id, args["plugin_unique_identifier"])
for message in response:
yield f"data: {json.dumps(jsonable_encoder(message))}\n\n"
except ValueError as e:
error_message = InstallPluginMessage(event=InstallPluginMessage.Event.Error, data=str(e))
yield f"data: {json.dumps(jsonable_encoder(error_message))}\n\n"
response = PluginService.install_from_marketplace_pkg(tenant_id, args["plugin_unique_identifier"])
return Response(generator(), mimetype="text/event-stream")
return {
"task_id": response,
}
class PluginFetchManifestApi(Resource):
@setup_required
@login_required
@account_initialization_required
def get(self):
user = current_user
parser = reqparse.RequestParser()
parser.add_argument("plugin_unique_identifier", type=str, required=True, location="args")
args = parser.parse_args()
tenant_id = user.current_tenant_id
return {"manifest": PluginService.fetch_plugin_manifest(tenant_id, args["plugin_unique_identifier"])}
class PluginFetchInstallTasksApi(Resource):
@setup_required
@login_required
@account_initialization_required
def get(self):
user = current_user
if not user.is_admin_or_owner:
raise Forbidden()
tenant_id = user.current_tenant_id
return {"tasks": PluginService.fetch_install_tasks(tenant_id)}
class PluginFetchInstallTaskApi(Resource):
@setup_required
@login_required
@account_initialization_required
def get(self, task_id: str):
user = current_user
if not user.is_admin_or_owner:
raise Forbidden()
tenant_id = user.current_tenant_id
return {"task": PluginService.fetch_install_task(tenant_id, task_id)}
class PluginUninstallApi(Resource):
@ -197,9 +252,12 @@ class PluginUninstallApi(Resource):
api.add_resource(PluginDebuggingKeyApi, "/workspaces/current/plugin/debugging-key")
api.add_resource(PluginListApi, "/workspaces/current/plugin/list")
api.add_resource(PluginIconApi, "/workspaces/current/plugin/icon")
api.add_resource(PluginInstallCheckUniqueIdentifierApi, "/workspaces/current/plugin/install/check_unique_identifier")
api.add_resource(PluginInstallFromUniqueIdentifierApi, "/workspaces/current/plugin/install/from_unique_identifier")
api.add_resource(PluginInstallFromPkgApi, "/workspaces/current/plugin/install/from_pkg")
api.add_resource(PluginInstallFromGithubApi, "/workspaces/current/plugin/install/from_github")
api.add_resource(PluginInstallFromMarketplaceApi, "/workspaces/current/plugin/install/from_marketplace")
api.add_resource(PluginUploadFromPkgApi, "/workspaces/current/plugin/upload/pkg")
api.add_resource(PluginUploadFromGithubApi, "/workspaces/current/plugin/upload/github")
api.add_resource(PluginInstallFromPkgApi, "/workspaces/current/plugin/install/pkg")
api.add_resource(PluginInstallFromGithubApi, "/workspaces/current/plugin/install/github")
api.add_resource(PluginInstallFromMarketplaceApi, "/workspaces/current/plugin/install/marketplace")
api.add_resource(PluginFetchManifestApi, "/workspaces/current/plugin/fetch-manifest")
api.add_resource(PluginFetchInstallTasksApi, "/workspaces/current/plugin/tasks")
api.add_resource(PluginFetchInstallTaskApi, "/workspaces/current/plugin/tasks/<task_id>")
api.add_resource(PluginUninstallApi, "/workspaces/current/plugin/uninstall")