Skip to content

Commit

Permalink
fix: deployment to use global context
Browse files Browse the repository at this point in the history
Signed-off-by: aarnphm-ec2-dev <29749331+aarnphm@users.noreply.github.com>
  • Loading branch information
aarnphm committed Jul 20, 2023
1 parent 566f8f3 commit 5c40f84
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 39 deletions.
1 change: 0 additions & 1 deletion src/bentoml_cli/bentos.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ def build( # type: ignore (not accessed)
push: bool,
force: bool,
threads: int,
context: str | None,
containerize: bool,
_bento_store: BentoStore = Provide[BentoMLContainer.bento_store],
_cloud_client: BentoCloudClient = Provide[BentoMLContainer.bentocloud_client],
Expand Down
36 changes: 25 additions & 11 deletions src/bentoml_cli/cloud.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from __future__ import annotations

import typing as t
import json

import click
import click_option_group as cog

if t.TYPE_CHECKING:
from .utils import Cli


def add_cloud_command(cli: click.Group) -> click.Group:
from bentoml._internal.cloud.client import RestApiClient
Expand Down Expand Up @@ -40,13 +44,8 @@ def cloud():
type=click.STRING,
help="BentoCloud or Yatai user API token",
)
@click.option(
"--context",
type=click.STRING,
help="BentoCloud or Yatai context name for the endpoint and API token",
default=default_context_name,
)
def login(endpoint: str, api_token: str, context: str) -> None: # type: ignore (not accessed)
@click.pass_obj
def login(obj: Cli, endpoint: str, api_token: str) -> None: # type: ignore (not accessed)
"""Login to BentoCloud or Yatai server."""
cloud_rest_client = RestApiClient(endpoint, api_token)
user = cloud_rest_client.get_current_user()
Expand All @@ -60,7 +59,7 @@ def login(endpoint: str, api_token: str, context: str) -> None: # type: ignore
raise CLIException("current organization is not found")

ctx = CloudClientContext(
name=context,
name=obj.context if obj.context is not None else default_context_name,
endpoint=endpoint,
api_token=api_token,
email=user.email,
Expand All @@ -74,9 +73,24 @@ def login(endpoint: str, api_token: str, context: str) -> None: # type: ignore
@cloud.command(aliases=["current-context"])
def get_current_context() -> None: # type: ignore (not accessed)
"""Get current cloud context."""
cur = CloudClientConfig.get_config().get_current_context()
if not get_quiet_mode():
click.echo(json.dumps(bentoml_cattr.unstructure(cur), indent=2))
click.echo(
json.dumps(
bentoml_cattr.unstructure(
CloudClientConfig.get_config().get_current_context()
),
indent=2,
)
)

@cloud.command()
def list_context() -> None: # type: ignore (not accessed)
"""List all available context."""
config = CloudClientConfig.get_config()
click.echo(
json.dumps(
bentoml_cattr.unstructure([i.name for i in config.contexts]), indent=2
)
)

@cloud.command()
@click.argument("context", type=click.STRING)
Expand Down
48 changes: 22 additions & 26 deletions src/bentoml_cli/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
TupleStrAny = tuple[str, ...]
from bentoml._internal.cloud.schemas import DeploymentListSchema
from bentoml._internal.cloud.schemas import DeploymentSchema
from .utils import Cli
else:
TupleStrAny = tuple

Expand Down Expand Up @@ -38,9 +39,6 @@ def shared_decorator(f: t.Callable[..., t.Any]):
type=click.STRING,
required=True,
),
click.option(
"--context", type=click.STRING, default=None, help="Yatai context name."
),
click.option(
"--cluster-name",
type=click.STRING,
Expand Down Expand Up @@ -70,13 +68,11 @@ def deployment_cli():
type=click.File(),
help="JSON file path for the deployment configuration",
)
@click.option(
"--context", type=click.STRING, default=None, help="Yatai context name."
)
@output_option
@click.pass_obj
def create( # type: ignore
obj: Cli,
file: str,
context: str,
output: t.Literal["json", "default"],
) -> DeploymentSchema:
"""Create a deployment on BentoCloud.
Expand All @@ -85,7 +81,9 @@ def create( # type: ignore
A deployment can be created using a json file with configurations.
The json file has the exact format as the one on BentoCloud Deployment UI.
"""
res = client.deployment.create_from_file(path_or_stream=file, context=context)
res = client.deployment.create_from_file(
path_or_stream=file, context=obj.context
)
if output == "default":
console.print(res)
elif output == "json":
Expand All @@ -101,15 +99,13 @@ def create( # type: ignore
)
@click.option("-n", "--name", type=click.STRING, help="Deployment name")
@click.option("--bento", type=click.STRING, help="Bento tag")
@click.option(
"--context", type=click.STRING, default=None, help="Yatai context name."
)
@output_option
@click.pass_obj
def update( # type: ignore
obj: Cli,
file: str | None,
name: str | None,
bento: str | None,
context: str,
output: t.Literal["json", "default"],
) -> DeploymentSchema:
"""Update a deployment on BentoCloud.
Expand All @@ -122,12 +118,11 @@ def update( # type: ignore
if name is not None:
click.echo("Reading from file, ignoring --name", err=True)
res = client.deployment.update_from_file(
path_or_stream=file,
context=context,
path_or_stream=file, context=obj.context
)
elif name is not None:
res = client.deployment.update(
name, bento=bento, context=context, latest_bento=True
name, bento=bento, context=obj.context, latest_bento=True
)
else:
raise click.BadArgumentUsage(
Expand All @@ -142,17 +137,18 @@ def update( # type: ignore

@deployment_cli.command()
@shared_decorator
@click.pass_obj
def get( # type: ignore
obj: Cli,
deployment_name: str,
context: str,
cluster_name: str,
kube_namespace: str,
output: t.Literal["json", "default"],
) -> DeploymentSchema:
"""Get a deployment on BentoCloud."""
res = client.deployment.get(
deployment_name=deployment_name,
context=context,
context=obj.context,
cluster_name=cluster_name,
kube_namespace=kube_namespace,
)
Expand All @@ -165,17 +161,18 @@ def get( # type: ignore

@deployment_cli.command()
@shared_decorator
@click.pass_obj
def terminate( # type: ignore
obj: Cli,
deployment_name: str,
context: str,
cluster_name: str,
kube_namespace: str,
output: t.Literal["json", "default"],
) -> DeploymentSchema:
"""Terminate a deployment on BentoCloud."""
res = client.deployment.terminate(
deployment_name=deployment_name,
context=context,
context=obj.context,
cluster_name=cluster_name,
kube_namespace=kube_namespace,
)
Expand All @@ -188,17 +185,18 @@ def terminate( # type: ignore

@deployment_cli.command()
@shared_decorator
@click.pass_obj
def delete( # type: ignore
obj: Cli,
deployment_name: str,
context: str,
cluster_name: str,
kube_namespace: str,
output: t.Literal["json", "default"],
) -> DeploymentSchema:
"""Delete a deployment on BentoCloud."""
res = client.deployment.delete(
deployment_name=deployment_name,
context=context,
context=obj.context,
cluster_name=cluster_name,
kube_namespace=kube_namespace,
)
Expand All @@ -210,9 +208,6 @@ def delete( # type: ignore
return res

@deployment_cli.command()
@click.option(
"--context", type=click.STRING, default=None, help="Yatai context name."
)
@click.option(
"--cluster-name", type=click.STRING, default=None, help="Name of the cluster."
)
Expand All @@ -235,8 +230,9 @@ def delete( # type: ignore
type=click.Choice(["json", "default", "table"]),
default="table",
)
@click.pass_obj
def list( # type: ignore
context: str,
obj: Cli,
cluster_name: str,
query: str,
search: str,
Expand All @@ -246,7 +242,7 @@ def list( # type: ignore
) -> DeploymentListSchema:
"""List existing deployments on BentoCloud."""
res = client.deployment.list(
context=context,
context=obj.context,
cluster_name=cluster_name,
query=query,
search=search,
Expand Down
5 changes: 4 additions & 1 deletion src/bentoml_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ def opt_callback(ctx: Context, param: Parameter, value: ClickParamType):

@attr.define
class Cli:
context: str | None
context: str | None = attr.field(default=None)

def with_options(self, **attrs: t.Any) -> t.Any:
return attr.evolve(self, **attrs)


class BentoMLCommandGroup(click.Group):
Expand Down

0 comments on commit 5c40f84

Please sign in to comment.