Skip to content
Permalink
Browse files

YataiService python client refactor (#470)

* migrating upload_bento_service to new yatai client implementation

* reorganize yataiservice API client code

* add yatai_client deployment api part

* linter fix
  • Loading branch information
parano committed Jan 10, 2020
1 parent 4817104 commit 74b16785b89e8582c4a05560246c8ca654fed389
@@ -35,11 +35,8 @@
env_decorator as env,
artifacts_decorator as artifacts,
ver_decorator as ver,
save,
)
from bentoml.yatai.python_api import upload_bento_service


save = upload_bento_service


__all__ = [
@@ -39,16 +39,7 @@
from bentoml.utils.usage_stats import track_cli
from bentoml.exceptions import BentoMLException
from bentoml.cli.utils import Spinner
from bentoml.yatai.python_api import (
apply_deployment,
create_deployment,
delete_deployment,
get_deployment,
describe_deployment,
list_deployments,
update_sagemaker_deployment,
)
from bentoml.yatai import get_yatai_service
from bentoml.yatai.client import YataiClient

# pylint: disable=unused-variable

@@ -138,13 +129,13 @@ def _print_deployments_info(deployments, output_type):


def get_state_after_await_action_complete(
yatai_service, name, namespace, message, timeout_limit=600, wait_time=5
yatai_client, name, namespace, message, timeout_limit=600, wait_time=5
):
start_time = time.time()

with Spinner(message):
while (time.time() - start_time) < timeout_limit:
result = describe_deployment(namespace, name, yatai_service)
result = yatai_client.deployment.describe(namespace, name)
if (
result.status.status_code == status_pb2.Status.OK
and result.state.state is DeploymentState.PENDING
@@ -317,9 +308,9 @@ def create(
'memory_size': memory_size,
'timeout': timeout,
}
yatai_service = get_yatai_service()
yatai_client = YataiClient()
try:
result = create_deployment(
result = yatai_client.deployment.create(
name,
namespace,
bento_name,
@@ -328,7 +319,6 @@ def create(
operator_spec,
parse_key_value_pairs(labels),
parse_key_value_pairs(annotations),
yatai_service,
)
except BentoMLException as e:
_echo(
@@ -351,7 +341,7 @@ def create(
else:
if wait:
result_state = get_state_after_await_action_complete(
yatai_service=yatai_service,
yatai_client=yatai_client,
name=name,
namespace=namespace,
message='Creating deployment ',
@@ -434,15 +424,15 @@ def update(
output,
wait,
):
yatai_service = get_yatai_service()
yatai_client = YataiClient()
track_cli('deploy-update')
if bento:
bento_name, bento_version = bento.split(':')
else:
bento_name = None
bento_version = None
try:
result = update_sagemaker_deployment(
result = yatai_client.deployment.update_sagemaker_deployment(
namespace=namespace,
deployment_name=name,
bento_name=bento_name,
@@ -451,7 +441,6 @@ def update(
instance_type=instance_type,
num_of_gunicorn_workers_per_instance=num_of_gunicorn_workers_per_instance, # noqa E501
api_name=api_name,
yatai_service=yatai_service,
)
except BentoMLException as e:
_echo(f'Failed to update deployment {name}: {str(e)}', CLI_COLOR_ERROR)
@@ -468,17 +457,18 @@ def update(
else:
if wait:
result_state = get_state_after_await_action_complete(
yatai_service=yatai_service,
yatai_client=yatai_client,
name=name,
namespace=namespace,
message='Updating deployment',
)
if result_state.status.status_code != status_pb2.Status.OK:
describe_deployment_status = result_state.status
error_code = status_pb2.Status.Code.Name(
result_state.status.status_code
)
_echo(
f'Updated deployment {name}. Failed to retrieve latest status. '
f'{status_pb2.Status.Code.Name(describe_deployment_status.status_code)}:' # noqa E501
f'{describe_deployment_status.error_message}'
f'{error_code}:{result_state.status.error_message}'
)
return
result.deployment.state.CopyFrom(result_state.state)
@@ -510,8 +500,8 @@ def update(
def apply(deployment_yaml, output, wait):
track_cli('deploy-apply', deployment_yaml.get('spec', {}).get('operator'))
try:
yatai_service = get_yatai_service()
result = apply_deployment(deployment_yaml, yatai_service)
yatai_client = YataiClient()
result = yatai_client.deployment.apply(deployment_yaml)
if result.status.status_code != status_pb2.Status.OK:
_echo(
'Failed to apply deployment {name}. '
@@ -527,7 +517,7 @@ def apply(deployment_yaml, output, wait):
else:
if wait:
result_state = get_state_after_await_action_complete(
yatai_service=yatai_service,
yatai_client=yatai_client,
name=deployment_yaml.get('name'),
namespace=deployment_yaml.get('namespace'),
message='Applying deployment',
@@ -580,8 +570,8 @@ def apply(deployment_yaml, output, wait):
'ignore errors when deleting cloud resources',
)
def delete(name, namespace, force):
yatai_service = get_yatai_service()
get_deployment_result = get_deployment(namespace, name, yatai_service)
yatai_client = YataiClient()
get_deployment_result = yatai_client.deployment.get(namespace, name)
if get_deployment_result.status.status_code != status_pb2.Status.OK:
_echo(
'Failed to get deployment {} for deletion. {}:{}'.format(
@@ -598,7 +588,7 @@ def delete(name, namespace, force):
get_deployment_result.deployment.spec.operator
)
track_cli('deploy-delete', platform)
result = delete_deployment(name, namespace, force, yatai_service)
result = yatai_client.deployment.delete(name, namespace, force)
if result.status.status_code == status_pb2.Status.OK:
extra_properties = {}
if get_deployment_result.deployment.created_at:
@@ -631,8 +621,8 @@ def delete(name, namespace, force):
def get(name, output, namespace):
track_cli('deploy-get')

yatai_service = get_yatai_service()
result = get_deployment(namespace, name, yatai_service)
yatai_client = YataiClient()
result = yatai_client.deployment.get(namespace, name)
if result.status.status_code != status_pb2.Status.OK:
_echo(
'Failed to get deployment {name}. code: {error_code}, message: '
@@ -658,9 +648,9 @@ def get(name, output, namespace):
@click.option('-o', '--output', type=click.Choice(['json', 'yaml']), default='json')
def describe(name, output, namespace):
track_cli('deploy-describe')
yatai_service = get_yatai_service()
yatai_client = YataiClient()

result = describe_deployment(namespace, name, yatai_service)
result = yatai_client.deployment.describe(namespace, name)
if result.status.status_code != status_pb2.Status.OK:
_echo(
'Failed to describe deployment {name}. {error_code}:'
@@ -672,7 +662,7 @@ def describe(name, output, namespace):
CLI_COLOR_ERROR,
)
else:
get_result = get_deployment(namespace, name)
get_result = yatai_client.deployment.get(namespace, name)
if get_result.status.status_code != status_pb2.Status.OK:
_echo(
'Failed to describe deployment {name}. {error_code}:'
@@ -717,15 +707,14 @@ def describe(name, output, namespace):
)
def list_deployments_cli(output, limit, filters, labels, namespace, all_namespaces):
track_cli('deploy-list')
yatai_service = get_yatai_service()
yatai_client = YataiClient()

result = list_deployments(
result = yatai_client.deployment.list(
limit=limit,
filters=filters,
labels=parse_key_value_pairs(labels),
namespace=namespace,
is_all_namespaces=all_namespaces,
yatai_service=yatai_service,
)
if result.status.status_code != status_pb2.Status.OK:
_echo(
@@ -344,6 +344,19 @@ def _validate_version_str(version_str):
)


def save(bento_service, base_path=None, version=None):
from bentoml.yatai.client import YataiClient
from bentoml.yatai import get_yatai_service

if base_path:
yatai_service = get_yatai_service(repo_base_url=base_path)
yatai_client = YataiClient(yatai_service)
else:
yatai_client = YataiClient()

return yatai_client.repository.upload(bento_service, version)


class BentoService(BentoServiceBase):
"""BentoService packs a list of artifacts and exposes service APIs
for BentoAPIServer and BentoCLI to execute. By subclassing BentoService,
@@ -501,9 +514,7 @@ def version(self):
return self._bento_service_version

def save(self, base_path=None, version=None):
from bentoml.yatai import python_api

return python_api.upload_bento_service(self, base_path, version)
return save(self, base_path, version)

def save_to_dir(self, path, version=None):
return save_to_dir(self, path, version)
@@ -21,7 +21,7 @@


from bentoml import config
from bentoml.yatai.python_api import upload_bento_service


logger = logging.getLogger(__name__)

@@ -74,4 +74,4 @@ def get_yatai_service(
)


__all__ = ["get_yatai_service", "upload_bento_service"]
__all__ = ["get_yatai_service"]
@@ -0,0 +1,51 @@
# Copyright 2019 Atalaya Tech, Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# List of APIs for accessing remote or local yatai service via Python


import logging

from bentoml.yatai import get_yatai_service
from bentoml.yatai.client.bento_repository_api import BentoRepositoryAPIClient
from bentoml.yatai.client.deployment_api import DeploymentAPIClient


logger = logging.getLogger(__name__)


class YataiClient:
"""Python Client for interacting with YataiService
"""

def __init__(self, yatai_service=None):
self.yatai_service = yatai_service if yatai_service else get_yatai_service()
self.bento_repository_api_client = None
self.deployment_api_client = None

@property
def repository(self):
if not self.bento_repository_api_client:
self.bento_repository_api_client = BentoRepositoryAPIClient(
self.yatai_service
)

return self.bento_repository_api_client

@property
def deployment(self):
if not self.deployment_api_client:
self.deployment_api_client = DeploymentAPIClient(self.yatai_service)

return self.deployment_api_client

0 comments on commit 74b1678

Please sign in to comment.
You can’t perform that action at this time.