Beispiel #1
0
def test_deployment_dict_to_pb():
    failed_dict_no_operator = {'name': 'fake name'}
    with pytest.raises(YataiDeploymentException) as error:
        deployment_dict_to_pb(failed_dict_no_operator)
    assert str(
        error.value).startswith('"spec" is required field for deployment')

    failed_dict_custom_operator = {
        'name': 'fake',
        'spec': {
            'operator': 'custom'
        }
    }
    with pytest.raises(BentoMLException) as error:
        deployment_dict_to_pb(failed_dict_custom_operator)
    assert str(error.value).startswith('Platform "custom" is not supported')

    deployment_dict = {
        'name': 'fake',
        'spec': {
            'operator': 'aws-lambda',
            'aws_lambda_operator_config': {
                'region': 'us-west-2'
            },
        },
    }
    result_pb = deployment_dict_to_pb(deployment_dict)
    assert isinstance(result_pb, Deployment)
    assert result_pb.name == 'fake'
Beispiel #2
0
    def apply(self, deployment_info, wait):
        if isinstance(deployment_info, dict):
            deployment_pb = deployment_dict_to_pb(deployment_info)
        elif isinstance(deployment_info, str):
            deployment_pb = deployment_yaml_string_to_pb(deployment_info)
        elif isinstance(deployment_info, Deployment):
            deployment_pb = deployment_info
        else:
            raise YataiDeploymentException(
                'Unexpected argument type, expect deployment info to be str in yaml '
                'format or a dict or a deployment protobuf obj, instead got: {}'
                .format(str(type(deployment_info))))

        validation_errors = validate_deployment_pb_schema(deployment_pb)
        if validation_errors:
            raise YataiDeploymentException(
                f'Failed to validate deployment {deployment_pb.name}: '
                f'{validation_errors}')

        apply_result = self.yatai_service.ApplyDeployment(
            ApplyDeploymentRequest(deployment=deployment_pb))
        if apply_result.status.status_code != status_pb2.Status.OK:
            error_code, error_message = status_pb_to_error_code_and_message(
                apply_result.status)
            raise YataiDeploymentException(f'{error_code}:{error_message}')
        if wait:
            self._wait_deployment_action_complete(deployment_pb.name,
                                                  deployment_pb.namespace)
        return self.get(namespace=deployment_pb.namespace,
                        name=deployment_pb.name)
Beispiel #3
0
def apply_deployment(deployment_info, yatai_service=None):
    if yatai_service is None:
        from bentoml.yatai import get_yatai_service

        yatai_service = get_yatai_service()

    try:
        if isinstance(deployment_info, dict):
            deployment_pb = deployment_dict_to_pb(deployment_info)
        elif isinstance(deployment_info, str):
            deployment_pb = deployment_yaml_string_to_pb(deployment_info)
        else:
            raise YataiDeploymentException(
                'Unexpected argument type, expect deployment info to be str in yaml '
                'format or a dict, instead got: {}'.format(
                    str(type(deployment_info))))

        validation_errors = validate_deployment_pb_schema(deployment_pb)
        if validation_errors:
            return ApplyDeploymentResponse(status=Status.INVALID_ARGUMENT(
                'Failed to validate deployment: {errors}'.format(
                    errors=validation_errors)))

        return yatai_service.ApplyDeployment(
            ApplyDeploymentRequest(deployment=deployment_pb))
    except BentoMLException as error:
        return ApplyDeploymentResponse(status=Status.INTERNAL(str(error)))
Beispiel #4
0
def test_deployment_dict_to_pb_for_lambda():
    deployment_dict = {
        'name': 'mock',
        'spec': {
            'operator': 'aws-lambda',
            'aws_lambda_operator_config': {
                'region': 'us-west-2',
                'memory_size': 1024,
                'timeout': 60,
            },
        },
    }
    result_pb = deployment_dict_to_pb(deployment_dict)
    assert result_pb.spec.aws_lambda_operator_config.memory_size == 1024
    assert result_pb.spec.aws_lambda_operator_config.region == 'us-west-2'
Beispiel #5
0
    def create(self, deployment_info, wait):
        from bentoml.yatai.validator import validate_deployment_pb

        if isinstance(deployment_info, dict):
            deployment_pb = deployment_dict_to_pb(deployment_info)
        elif isinstance(deployment_info, str):
            deployment_pb = deployment_yaml_string_to_pb(deployment_info)
        elif isinstance(deployment_info, Deployment):
            deployment_pb = deployment_info
        else:
            raise YataiDeploymentException(
                'Unexpected argument type, expect deployment info to be str in yaml '
                'format or a dict or a deployment protobuf obj, instead got: {}'.format(
                    str(type(deployment_info))
                )
            )

        validation_errors = validate_deployment_pb(deployment_pb)
        if validation_errors:
            raise YataiDeploymentException(
                f'Failed to validate deployment {deployment_pb.name}: '
                f'{validation_errors}'
            )
        # Make sure there is no active deployment with the same deployment name
        get_deployment_pb = self.yatai_service.GetDeployment(
            GetDeploymentRequest(
                deployment_name=deployment_pb.name, namespace=deployment_pb.namespace
            )
        )
        if get_deployment_pb.status.status_code != status_pb2.Status.NOT_FOUND:
            raise BentoMLException(
                f'Deployment "{deployment_pb.name}" already existed, use Update or '
                f'Apply for updating existing deployment, delete the deployment, '
                f'or use a different deployment name'
            )
        apply_result = self.yatai_service.ApplyDeployment(
            ApplyDeploymentRequest(deployment=deployment_pb)
        )
        if apply_result.status.status_code != status_pb2.Status.OK:
            error_code, error_message = status_pb_to_error_code_and_message(
                apply_result.status
            )
            raise YataiDeploymentException(f'{error_code}:{error_message}')
        if wait:
            self._wait_deployment_action_complete(
                deployment_pb.name, deployment_pb.namespace
            )
        return self.get(namespace=deployment_pb.namespace, name=deployment_pb.name)
Beispiel #6
0
def test_deployment_dict_to_pb_for_sagemaker():
    deployment_dict = {
        'name': 'mock',
        'spec': {
            'operator': 'aws-sagemaker',
            'sagemaker_operator_config': {
                'region': 'us-west-2',
                'api_name': 'predict',
                'instance_type': 'mock_type',
                'num_of_gunicorn_workers_per_instance': 4,
            },
        },
    }
    result_pb = deployment_dict_to_pb(deployment_dict)
    assert result_pb.spec.sagemaker_operator_config.region == 'us-west-2'
    assert result_pb.spec.sagemaker_operator_config.api_name == 'predict'
    assert result_pb.spec.sagemaker_operator_config.instance_type == 'mock_type'
Beispiel #7
0
    def apply_deployment(self, deployment_info):
        if isinstance(deployment_info, dict):
            deployment_pb = deployment_dict_to_pb(deployment_info)
        elif isinstance(deployment_info, str):
            deployment_pb = deployment_yaml_string_to_pb(deployment_info)
        elif isinstance(deployment_info, Deployment):
            deployment_pb = deployment_info
        else:
            raise YataiDeploymentException(
                'Unexpected argument type, expect deployment info to be str in yaml '
                'format or a dict or a deployment protobuf obj, instead got: {}'
                .format(str(type(deployment_info))))

        validation_errors = validate_deployment_pb_schema(deployment_pb)
        if validation_errors:
            return ApplyDeploymentResponse(status=Status.INVALID_ARGUMENT(
                'Failed to validate deployment: {errors}'.format(
                    errors=validation_errors)))

        return self.yatai_service.ApplyDeployment(
            ApplyDeploymentRequest(deployment=deployment_pb))