This repository was archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
feat: Migrate filebrowsers to storage proxy #524
Open
leksikov
wants to merge
19
commits into
main
Choose a base branch
from
feature/filebrowser-in-storage-proxy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c5c3811
feat: draft for manager filebrowser requests management
leksikov bee80c9
fix: add vfid for vfolder name in requests
leksikov 6ffd6fc
feat: add support for File Browser on manager side
leksikov 8b5132f
fix: add 510.feature news fragment
leksikov 0310e4e
Merge remote-tracking branch 'origin/main' into feature/filebrowser-i…
leksikov 7bea3f1
fix: update news fragment
leksikov 7bd094c
fix: accidental file edits
leksikov 2301ffd
fix: add browser destroy command
leksikov 280e953
Merge remote-tracking branch 'origin/main' into feature/filebrowser-i…
leksikov c2914a6
fix: filebrowser launch and add destory function
leksikov 7c58cdc
fix: add filebrowser destroy feature
leksikov d22d954
fix: mypy error
leksikov c2f61d5
fix: mypy error with return statement
leksikov b152e7b
fix: mypy return statement
leksikov d2a325b
fix: to resolve the git feedback
leksikov 57fc4a7
fix: fix style
leksikov 81291a4
Merge branch 'main' into feature/filebrowser-in-storage-proxy
leksikov 11c91fc
fix: add support for argument option host volume
leksikov 5608e6b
Merge remote-tracking branch 'origin/main' into feature/filebrowser-i…
leksikov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Implementation of Manager Facing API for File Browser in Storage Proxy. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import logging | ||
| from typing import Any, Iterable, Mapping, Tuple | ||
|
|
||
| import aiohttp | ||
| import aiohttp_cors | ||
| import sqlalchemy as sa | ||
| import trafaret as t | ||
| from ai.backend.common.logging import BraceStyleAdapter | ||
| from aiohttp import web | ||
|
|
||
| from ..exceptions import InvalidArgument | ||
| from ..models import vfolders | ||
| from .auth import auth_required | ||
| from .context import RootContext | ||
| from .manager import READ_ALLOWED, server_status_required | ||
| from .types import CORSOptions, WebMiddleware | ||
| from .utils import check_api_params | ||
|
|
||
| log = BraceStyleAdapter(logging.getLogger(__name__)) | ||
|
|
||
| VFolderRow = Mapping[str, Any] | ||
|
|
||
|
|
||
| async def get_vfid(root_ctx: RootContext, name: str) -> str: | ||
| async with root_ctx.db.begin() as conn: | ||
| query = ( | ||
| sa.select([vfolders.c.id]) | ||
| .select_from(vfolders) | ||
| .where(vfolders.c.name == name) | ||
| ) | ||
| folder_id = await conn.scalar(query) | ||
|
|
||
| query = sa.delete(vfolders).where(vfolders.c.id == folder_id) | ||
|
|
||
| return folder_id.hex | ||
|
|
||
|
|
||
| async def get_volume(root_ctx: RootContext, vfid: str) -> str: | ||
| async with root_ctx.db.begin() as conn: | ||
|
||
| query = ( | ||
| sa.select([vfolders.c.host]) | ||
| .select_from(vfolders) | ||
| .where(vfolders.c.id == vfid) | ||
| ) | ||
| host = await conn.scalar(query) | ||
| return host | ||
|
|
||
|
|
||
| @auth_required | ||
| @server_status_required(READ_ALLOWED) | ||
| @check_api_params( | ||
| t.Dict( | ||
| { | ||
| t.Key("vfolders"): t.List(t.String), | ||
| }, | ||
| ), | ||
| ) | ||
| async def create_or_update_filebrowser( | ||
| request: web.Request, | ||
| params: Any, | ||
| ) -> web.Response: | ||
|
|
||
| root_ctx: RootContext = request.app["_root.context"] | ||
|
|
||
| vfolders = [] | ||
|
|
||
| # Search for vfid based on vfolder name. And then get relevant host address and volume. | ||
| for vfolder_name in params["vfolders"]: | ||
| vfolders.append( | ||
| {"name": vfolder_name, "vfid": await get_vfid(root_ctx, vfolder_name)}, | ||
| ) | ||
|
|
||
| host = await get_volume(root_ctx, await get_vfid(root_ctx, vfolder_name)) | ||
| proxy_name, _ = root_ctx.storage_manager.split_host(host) | ||
|
|
||
| try: | ||
| proxy_info = root_ctx.storage_manager._proxies[proxy_name] | ||
| except KeyError: | ||
| raise InvalidArgument("There is no such storage proxy", proxy_name) | ||
|
|
||
| headers = {} | ||
| headers["X-BackendAI-Storage-Auth-Token"] = proxy_info.secret | ||
|
|
||
| try: | ||
| async with proxy_info.session.request( | ||
| "POST", | ||
| proxy_info.manager_api_url / "browser/create", | ||
| headers=headers, | ||
| json={"vfolders": vfolders}, | ||
| ) as client_resp: | ||
| return web.json_response(await client_resp.json()) | ||
| except aiohttp.ClientResponseError: | ||
| raise | ||
|
|
||
|
|
||
| @auth_required | ||
| @server_status_required(READ_ALLOWED) | ||
| @check_api_params( | ||
| t.Dict( | ||
| { | ||
| t.Key("container_id"): t.String, | ||
| }, | ||
| ), | ||
| ) | ||
| async def destroy_filebrowser( | ||
| request: web.Request, | ||
| params: Any, | ||
| ) -> web.Response: | ||
| root_ctx: RootContext = request.app["_root.context"] | ||
| container_id = params["container_id"] | ||
|
|
||
| volumes = await root_ctx.storage_manager.get_all_volumes() | ||
|
|
||
| # search for volume among available volumes which has file browser container id in order to destroy | ||
| for volume in volumes: | ||
| proxy_name = volume[0] | ||
| try: | ||
| proxy_info = root_ctx.storage_manager._proxies[proxy_name] | ||
| except KeyError: | ||
| raise InvalidArgument("There is no such storage proxy", proxy_name) | ||
|
|
||
| headers = {} | ||
| headers["X-BackendAI-Storage-Auth-Token"] = proxy_info.secret | ||
| auth_token = proxy_info.secret | ||
| try: | ||
| async with proxy_info.session.request( | ||
| "DELETE", | ||
| proxy_info.manager_api_url / "browser/destroy", | ||
| headers=headers, | ||
| json={"container_id": container_id, "auth_token": auth_token}, | ||
| ) as client_resp: | ||
| return web.json_response(await client_resp.json()) | ||
| except aiohttp.ClientResponseError: | ||
| raise | ||
| return web.json_response({"status": "fail"}) | ||
|
|
||
|
|
||
| async def init(app: web.Application) -> None: | ||
| pass | ||
|
|
||
|
|
||
| async def shutdown(app: web.Application) -> None: | ||
| pass | ||
|
|
||
|
|
||
| def create_app( | ||
| default_cors_options: CORSOptions, | ||
| ) -> Tuple[web.Application, Iterable[WebMiddleware]]: | ||
| app = web.Application() | ||
| app["prefix"] = "browser" | ||
|
||
| app["api_versions"] = ( | ||
| 2, | ||
| 3, | ||
| 4, | ||
| ) | ||
| app.on_startup.append(init) | ||
| app.on_shutdown.append(shutdown) | ||
| cors = aiohttp_cors.setup(app, defaults=default_cors_options) | ||
| cors.add(app.router.add_route("POST", r"/create", create_or_update_filebrowser)) | ||
| cors.add(app.router.add_route("DELETE", r"/destroy", destroy_filebrowser)) | ||
|
|
||
| return app, [] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This query is not executed. If missed, please add another
await conn.execute().Also, if you use different types of queries in a single scope (e.g., select & delete), please name the variables differently (e.g.,
select_query,delete_query) because in the future SQLAlchemy v2 with mypy extensions will check the different typing of query objects.