Skip to content

Commit

Permalink
fix: add config dict to cli (#4424)
Browse files Browse the repository at this point in the history
* fix: add config dict to cli

Signed-off-by: FogDong <[email protected]>

* ci: auto fixes from pre-commit.ci

For more information, see https://pre-commit.ci

---------

Signed-off-by: FogDong <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and frostming committed Feb 1, 2024
1 parent 3ab1861 commit bc04e59
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/bentoml/_internal/cloud/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,17 @@ def apply(
context=context,
)
# directly update the deployment with the schema, do not merge with the existing schema
if deployment_config_params.get_name() != deployment_schema.name:
raise BentoMLException(
f"Deployment name cannot be changed, current name is {deployment_schema.name}"
)
if (
deployment_config_params.get_cluster(pop=False)
!= deployment_schema.cluster.host_cluster_display_name
):
raise BentoMLException(
f"Deployment cluster cannot be changed, current cluster is {deployment_schema.cluster.host_cluster_display_name}"
)
config_struct = bentoml_cattr.structure(
deployment_config_params.get_config_dict(), UpdateDeploymentSchemaV2
)
Expand Down
128 changes: 122 additions & 6 deletions src/bentoml_cli/deployment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import json
import typing as t

import click
Expand All @@ -22,8 +23,6 @@


def add_deployment_command(cli: click.Group) -> None:
import json

from rich.table import Table

from bentoml._internal.utils import rich_console as console
Expand Down Expand Up @@ -86,6 +85,12 @@ def add_deployment_command(cli: click.Group) -> None:
help="Configuration file path",
default=None,
)
@click.option(
"--config-dict",
type=click.STRING,
help="Configuration json string",
default=None,
)
@click.option(
"--wait/--no-wait",
type=click.BOOL,
Expand All @@ -112,6 +117,7 @@ def deploy(
strategy: str | None,
env: tuple[str] | None,
config_file: str | t.TextIO | None,
config_dict: str | None,
wait: bool,
timeout: int,
) -> None:
Expand All @@ -132,6 +138,7 @@ def deploy(
strategy=strategy,
env=env,
config_file=config_file,
config_dict=config_dict,
wait=wait,
timeout=timeout,
)
Expand Down Expand Up @@ -221,6 +228,12 @@ def deployment_cli():
help="Configuration file path, mututally exclusive with other config options",
default=None,
)
@click.option(
"--config-dict",
type=click.STRING,
help="Configuration json string",
default=None,
)
@click.pass_obj
def update( # type: ignore
shared_options: SharedOptions,
Expand All @@ -234,13 +247,17 @@ def update( # type: ignore
strategy: str | None,
env: tuple[str] | None,
config_file: t.TextIO | None,
config_dict: str | None,
) -> None:
"""Update a deployment on BentoCloud.
\b
A deployment can be updated using parameters, or using config yaml file.
You can also update bento by providing a project path or existing bento.
"""
cfg_dict = None
if config_dict is not None and config_dict != "":
cfg_dict = json.loads(config_dict)
config_params = DeploymentConfigParameters(
name=name,
context=shared_options.cloud_context,
Expand All @@ -258,6 +275,7 @@ def update( # type: ignore
if env is not None
else None,
config_file=config_file,
config_dict=cfg_dict,
)
try:
config_params.verify()
Expand All @@ -273,25 +291,114 @@ def update( # type: ignore
click.echo(f"Deployment '{config_params.get_name()}' updated successfully.")

@deployment_cli.command()
@click.argument(
"bento",
type=click.STRING,
required=False,
)
@click.option(
"-n",
"--name",
type=click.STRING,
help="Deployment name",
)
@click.option(
"--cluster",
type=click.STRING,
help="Name of the cluster",
)
@click.option(
"--access-authorization",
type=click.BOOL,
help="Enable access authorization",
)
@click.option(
"--scaling-min",
type=click.INT,
help="Minimum scaling value",
)
@click.option(
"--scaling-max",
type=click.INT,
help="Maximum scaling value",
)
@click.option(
"--instance-type",
type=click.STRING,
help="Type of instance",
)
@click.option(
"--strategy",
type=click.Choice(
[deployment_strategy.value for deployment_strategy in DeploymentStrategy]
),
help="Deployment strategy",
)
@click.option(
"--env",
type=click.STRING,
help="List of environment variables pass by --env key=value, --env ...",
multiple=True,
)
@click.option(
"-f",
"--config-file",
type=click.File(),
help="Configuration file path",
default=None,
)
@click.option(
"-f",
"--config-file",
help="Configuration file path, mututally exclusive with other config options",
default=None,
)
@click.option(
"--config-dict",
type=click.STRING,
help="Configuration json string",
default=None,
)
@click.pass_obj
def apply( # type: ignore
shared_options: SharedOptions,
bento: str | None,
name: str | None,
cluster: str | None,
access_authorization: bool | None,
scaling_min: int | None,
scaling_max: int | None,
instance_type: str | None,
strategy: str | None,
env: tuple[str] | None,
config_file: str | t.TextIO | None,
config_dict: str | None,
) -> None:
"""Apply a deployment on BentoCloud.
\b
A deployment can be applied using config yaml file.
"""
cfg_dict = None
if config_dict is not None and config_dict != "":
cfg_dict = json.loads(config_dict)
config_params = DeploymentConfigParameters(
name=name,
context=shared_options.cloud_context,
bento=bento,
cluster=cluster,
access_authorization=access_authorization,
scaling_max=scaling_max,
scaling_min=scaling_min,
instance_type=instance_type,
strategy=strategy,
envs=[
{"key": item.split("=")[0], "value": item.split("=")[1]} for item in env
]
if env is not None
else None,
config_file=config_file,
config_dict=cfg_dict,
)
try:
config_params.verify()
Expand Down Expand Up @@ -363,6 +470,12 @@ def apply( # type: ignore
help="Configuration file path",
default=None,
)
@click.option(
"--config-dict",
type=click.STRING,
help="Configuration json string",
default=None,
)
@click.option(
"--wait/--no-wait",
type=click.BOOL,
Expand All @@ -389,6 +502,7 @@ def create(
strategy: str | None,
env: tuple[str] | None,
config_file: str | t.TextIO | None,
config_dict: str | None,
wait: bool,
timeout: int,
) -> None:
Expand All @@ -409,6 +523,7 @@ def create(
strategy=strategy,
env=env,
config_file=config_file,
config_dict=config_dict,
wait=wait,
timeout=timeout,
)
Expand Down Expand Up @@ -524,9 +639,6 @@ def list( # type: ignore
@click.option(
"--cluster", type=click.STRING, default=None, help="Name of the cluster."
)
# @click.option(
# "--search", type=click.STRING, default=None, help="Search for list request."
# )
@click.option(
"-o",
"--output",
Expand All @@ -538,7 +650,6 @@ def list( # type: ignore
def list_instance_types( # type: ignore
shared_options: SharedOptions,
cluster: str | None,
# search: str | None,
output: t.Literal["json", "yaml", "table"],
) -> None:
"""List existing instance types in cluster on BentoCloud."""
Expand Down Expand Up @@ -584,9 +695,13 @@ def create_deployment(
strategy: str | None,
env: tuple[str] | None,
config_file: str | t.TextIO | None,
config_dict: str | None,
wait: bool,
timeout: int,
) -> None:
cfg_dict = None
if config_dict is not None and config_dict != "":
cfg_dict = json.loads(config_dict)
config_params = DeploymentConfigParameters(
name=name,
context=context,
Expand All @@ -601,6 +716,7 @@ def create_deployment(
if env is not None
else None,
config_file=config_file,
config_dict=cfg_dict,
)
try:
config_params.verify()
Expand Down

0 comments on commit bc04e59

Please sign in to comment.