From 114833a14c039aed713c22ad98e3daf7b38cd633 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Mon, 13 Jan 2025 17:53:06 +0800 Subject: [PATCH] fix: allow usage like bentoml deploy service:MyService Signed-off-by: Frost Ming --- src/bentoml/_internal/cloud/deployment.py | 71 ++++++++++++++--------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/src/bentoml/_internal/cloud/deployment.py b/src/bentoml/_internal/cloud/deployment.py index b7030c9debf..ed65448348e 100644 --- a/src/bentoml/_internal/cloud/deployment.py +++ b/src/bentoml/_internal/cloud/deployment.py @@ -336,12 +336,16 @@ def ensure_bento( _bento_store: BentoStore = Provide[BentoMLContainer.bento_store], _client: RestApiClient = Provide[BentoMLContainer.rest_api_client], ) -> Bento | BentoManifestSchema: + from bentoml.bentos import build_bentofile + from .bento import BentoAPI + if not project_path and not bento: + raise BentoMLException( + "Creating a deployment needs a target; project path or bento is necessary" + ) bento_api = BentoAPI(_client) if project_path: - from bentoml.bentos import build_bentofile - bento_obj = build_bentofile( build_ctx=project_path, bare=bare, _bento_store=_bento_store, reload=reload ) @@ -350,37 +354,48 @@ def ensure_bento( if push: bento_api.push(bento=bento_obj, bare=bare) return bento_obj - elif bento: - bento = Tag.from_taglike(bento) - try: - bento_obj = _bento_store.get(bento) - except NotFound: - bento_obj = None + assert bento is not None + bento_tag = Tag.from_taglike(bento) + try: + bento_obj = _bento_store.get(bento_tag) + except NotFound: + bento_obj = None + + # try to get from bentocloud + try: + bento_schema = bento_api.get(name=bento_tag.name, version=bento_tag.version) + except NotFound: + bento_schema = None + + if bento_obj is not None: + # push to bentocloud + if push: + bento_api.push(bento=bento_obj, bare=bare) + return bento_obj + if bento_schema is not None: + assert bento_schema.manifest is not None + if cli: + rich.print( + f"[bold blue]Using bento [green]{bento_tag}[/] from bentocloud to deploy" + ) + bento_schema.manifest.version = bento_tag.version + return bento_schema.manifest - # try to get from bentocloud + # bento is a service definition + if isinstance(bento, str): try: - bento_schema = bento_api.get(name=bento.name, version=bento.version) - except NotFound: - bento_schema = None - - if bento_obj is not None: - # push to bentocloud + bento_obj = build_bentofile( + service=bento, bare=bare, _bento_store=_bento_store, reload=reload + ) + except BentoMLException: + pass + else: + if cli: + rich.print(f":bento: Built bento [green]{bento_obj.info.tag}[/]") if push: bento_api.push(bento=bento_obj, bare=bare) return bento_obj - if bento_schema is not None: - assert bento_schema.manifest is not None - if cli: - rich.print( - f"[bold blue]Using bento [green]{bento.name}:{bento.version}[/] from bentocloud to deploy" - ) - bento_schema.manifest.version = bento.version - return bento_schema.manifest - raise NotFound(f"bento {bento} not found in both local and cloud") - else: - raise BentoMLException( - "Create a deployment needs a target; project path or bento is necessary" - ) + raise NotFound(f"Bento {bento} is not found in both local and bentocloud") @attr.define