def deployment_dict_to_pb(deployment_dict): deployment_pb = Deployment() if deployment_dict.get('spec'): spec_dict = deployment_dict.get('spec') else: raise YataiDeploymentException( '"spec" is required field for deployment') platform = spec_dict.get('operator') if platform is not None: # converting platform parameter to DeploymentOperator name in proto # e.g. 'aws-lambda' to 'AWS_LAMBDA' deployment_pb.spec.operator = DeploymentSpec.DeploymentOperator.Value( platform.replace('-', '_').upper()) for field in ['name', 'namespace']: if deployment_dict.get(field): deployment_pb.__setattr__(field, deployment_dict.get(field)) if deployment_dict.get('labels') is not None: deployment_pb.labels.update(deployment_dict.get('labels')) if deployment_dict.get('annotations') is not None: deployment_pb.annotations.update(deployment_dict.get('annotations')) if spec_dict.get('bento_name'): deployment_pb.spec.bento_name = spec_dict.get('bento_name') if spec_dict.get('bento_version'): deployment_pb.spec.bento_version = spec_dict.get('bento_version') if deployment_pb.spec.operator == DeploymentSpec.AWS_SAGEMAKER: sagemaker_config = spec_dict.get('sagemaker_operator_config', {}) sagemaker_config_pb = deployment_pb.spec.sagemaker_operator_config for field in [ 'region', 'api_name', 'instance_type', 'num_of_gunicorn_workers_per_instance', 'instance_count', ]: if sagemaker_config.get(field): sagemaker_config_pb.__setattr__(field, sagemaker_config.get(field)) elif deployment_pb.spec.operator == DeploymentSpec.AWS_LAMBDA: lambda_conf = spec_dict.get('aws_lambda_operator_config', {}) for field in ['region', 'api_name', 'memory_size', 'timeout']: if lambda_conf.get(field): deployment_pb.spec.aws_lambda_operator_config.__setattr__( field, lambda_conf.get(field)) else: raise InvalidArgument( 'Platform "{}" is not supported in the current version of ' 'BentoML'.format(platform)) return deployment_pb
def create_azure_functions_deployment( self, name, bento_name, bento_version, location, premium_plan_sku=None, min_instances=None, max_burst=None, function_auth_level=None, namespace=None, labels=None, annotations=None, wait=None, ): deployment_pb = Deployment(name=name, namespace=namespace, labels=labels, annotations=annotations) deployment_pb.spec.bento_name = bento_name deployment_pb.spec.bento_version = bento_version deployment_pb.spec.operator = DeploymentSpec.AZURE_FUNCTIONS deployment_pb.spec.azure_functions_operator_config.location = location deployment_pb.spec.azure_functions_operator_config.premium_plan_sku = ( premium_plan_sku) deployment_pb.spec.azure_functions_operator_config.min_instances = min_instances deployment_pb.spec.azure_functions_operator_config.function_auth_level = ( function_auth_level) deployment_pb.spec.azure_functions_operator_config.max_burst = max_burst return self.create(deployment_pb, wait)
def create_ec2_deployment( self, name, namespace, bento_name, bento_version, region, min_size, desired_capacity, max_size, instance_type, ami_id, wait=None, ): deployment_pb = Deployment(name=name, namespace=namespace) deployment_pb.spec.bento_name = bento_name deployment_pb.spec.bento_version = bento_version if region: deployment_pb.spec.aws_ec2_operator_config.region = region deployment_pb.spec.operator = DeploymentSpec.AWS_EC2 deployment_pb.spec.aws_ec2_operator_config.autoscale_min_size = min_size deployment_pb.spec.aws_ec2_operator_config.autoscale_desired_capacity = ( desired_capacity ) deployment_pb.spec.aws_ec2_operator_config.autoscale_max_size = max_size deployment_pb.spec.aws_ec2_operator_config.instance_type = instance_type deployment_pb.spec.aws_ec2_operator_config.ami_id = ami_id return self.create(deployment_pb, wait)
def create_sagemaker_deployment( self, name, bento_name, bento_version, api_name, instance_type, instance_count, timeout, num_of_gunicorn_workers_per_instance=None, region=None, namespace=None, labels=None, annotations=None, wait=None, ): """Create SageMaker deployment Args: name: bento_name: bento_version: api_name: instance_type: instance_count: timeout: num_of_gunicorn_workers_per_instance: region: namespace: labels: annotations: wait: Returns: ApplyDeploymentResponse Raises: BentoMLException """ deployment_pb = Deployment(name=name, namespace=namespace, labels=labels, annotations=annotations) deployment_pb.spec.bento_name = bento_name deployment_pb.spec.bento_version = bento_version deployment_pb.spec.operator = DeploymentSpec.AWS_SAGEMAKER deployment_pb.spec.sagemaker_operator_config.api_name = api_name deployment_pb.spec.sagemaker_operator_config.instance_count = instance_count deployment_pb.spec.sagemaker_operator_config.instance_type = instance_type deployment_pb.spec.sagemaker_operator_config.timeout = timeout if region: deployment_pb.spec.sagemaker_operator_config.region = region if num_of_gunicorn_workers_per_instance: deployment_pb.spec.sagemaker_operator_config.num_of_gunicorn_workers_per_instance = ( # noqa E501 num_of_gunicorn_workers_per_instance) return self.create(deployment_pb, wait)
def _get_test_sagemaker_deployment_pb(): test_pb = Deployment(name='test_deployment_name', namespace='namespace') test_pb.spec.bento_name = 'bento_name' test_pb.spec.bento_version = 'bento_version' test_pb.spec.operator = DeploymentSpec.DeploymentOperator.Value( 'AWS_SAGEMAKER') test_pb.spec.sagemaker_operator_config.api_name = 'api_name' test_pb.spec.sagemaker_operator_config.instance_type = 'mock_instance_type' test_pb.spec.sagemaker_operator_config.instance_count = 1 return test_pb
def _get_test_lambda_deployment_pb(): test_pb = Deployment(name='test_deployment_name', namespace='namespace') test_pb.spec.bento_name = 'bento_name' test_pb.spec.bento_version = 'bento_version' test_pb.spec.operator = DeploymentSpec.DeploymentOperator.Value( 'AWS_LAMBDA') test_pb.spec.aws_lambda_operator_config.api_name = 'api_name' test_pb.spec.aws_lambda_operator_config.region = 'us-west-2' test_pb.spec.aws_lambda_operator_config.timeout = 100 test_pb.spec.aws_lambda_operator_config.memory_size = 128 return test_pb
def generate_lambda_deployment_pb(): test_deployment_pb = Deployment(name='test_aws_lambda', namespace='test-namespace') test_deployment_pb.spec.bento_name = 'bento_name' test_deployment_pb.spec.bento_version = 'v1.0.0' # DeploymentSpec.DeploymentOperator.AWS_LAMBDA test_deployment_pb.spec.operator = 3 test_deployment_pb.spec.aws_lambda_operator_config.region = 'us-west-2' test_deployment_pb.spec.aws_lambda_operator_config.api_name = 'predict' test_deployment_pb.spec.aws_lambda_operator_config.memory_size = 3008 test_deployment_pb.spec.aws_lambda_operator_config.timeout = 6 return test_deployment_pb
def create_lambda_deployment( self, name, bento_name, bento_version, memory_size, timeout, api_name=None, region=None, namespace=None, labels=None, annotations=None, wait=None, ): """Create Lambda deployment Args: name: bento_name: bento_version: memory_size: timeout: api_name: region: namespace: labels: annotations: wait: Returns: ApplyDeploymentResponse: status, deployment Raises: BentoMLException """ deployment_pb = Deployment(name=name, namespace=namespace, labels=labels, annotations=annotations) deployment_pb.spec.bento_name = bento_name deployment_pb.spec.bento_version = bento_version deployment_pb.spec.operator = DeploymentSpec.AWS_LAMBDA deployment_pb.spec.aws_lambda_operator_config.memory_size = memory_size deployment_pb.spec.aws_lambda_operator_config.timeout = timeout if api_name: deployment_pb.spec.aws_lambda_operator_config.api_name = api_name if region: deployment_pb.spec.aws_lambda_operator_config.region = region return self.create(deployment_pb, wait)
def generate_sagemaker_deployment_pb(): test_deployment_pb = Deployment(name=TEST_DEPLOYMENT_NAME, namespace=TEST_DEPLOYMENT_NAMESPACE) test_deployment_pb.spec.bento_name = TEST_DEPLOYMENT_BENTO_NAME test_deployment_pb.spec.bento_version = TEST_DEPLOYMENT_BENTO_VERSION test_deployment_pb.spec.sagemaker_operator_config.api_name = TEST_BENTO_API_NAME test_deployment_pb.spec.sagemaker_operator_config.region = TEST_AWS_REGION test_deployment_pb.spec.sagemaker_operator_config.instance_count = ( TEST_DEPLOYMENT_INSTANCE_COUNT) test_deployment_pb.spec.sagemaker_operator_config.instance_type = ( TEST_DEPLOYMENT_INSTANCE_TYPE) test_deployment_pb.spec.operator = DeploymentSpec.AWS_SAGEMAKER return test_deployment_pb
def generate_ec2_deployment_pb(): test_deployment_pb = Deployment(name='test_aws_ec2', namespace='test-namespace') test_deployment_pb.spec.bento_name = 'bento_name' test_deployment_pb.spec.bento_version = 'v1.0.0' # DeploymentSpec.DeploymentOperator.AWS_LAMBDA test_deployment_pb.spec.operator = 3 test_deployment_pb.spec.aws_ec2_operator_config.region = 'us-west-2' test_deployment_pb.spec.aws_ec2_operator_config.instance_type = "t2.micro" test_deployment_pb.spec.aws_ec2_operator_config.ami_id = "test-ami-id" test_deployment_pb.spec.aws_ec2_operator_config.autoscale_min_size = 1 test_deployment_pb.spec.aws_ec2_operator_config.autoscale_desired_capacity = 1 test_deployment_pb.spec.aws_ec2_operator_config.autoscale_max_size = 1 return test_deployment_pb
def mock_deployment_pb(name=MOCK_DEPLOYMENT_NAME): mock_deployment = Deployment(name=name, namespace='mock-namespace') mock_deployment.spec.azure_functions_operator_config.location = 'mock-location' mock_deployment.spec.operator = 4 mock_deployment.created_at.GetCurrentTime() return mock_deployment
def test_deployment_labels(): runner = CliRunner() cli = create_bentoml_cli() failed_result = runner.invoke( cli.commands['lambda'], [ 'deploy', 'failed-name', '-b', 'ExampleBentoService:version', '--labels', 'test=abc', ], ) assert failed_result.exit_code == 2 with mock.patch( 'bentoml.yatai.deployment.aws_lambda.operator.AwsLambdaDeploymentOperator.add' ) as mock_operator_add: bento_name = 'MockService' bento_version = 'MockVersion' deployment_name = f'test-label-{uuid.uuid4().hex[:8]}' deployment_namespace = 'test-namespace' mocked_deployment_pb = Deployment(name=deployment_name, namespace=deployment_namespace) mocked_deployment_pb.spec.bento_name = bento_name mocked_deployment_pb.spec.bento_version = bento_version mocked_deployment_pb.spec.operator = DeploymentSpec.AWS_LAMBDA mocked_deployment_pb.spec.aws_lambda_operator_config.memory_size = 1000 mocked_deployment_pb.spec.aws_lambda_operator_config.timeout = 60 mocked_deployment_pb.spec.aws_lambda_operator_config.region = 'us-west-2' mock_operator_add.return_value = ApplyDeploymentResponse( status=Status.OK(), deployment=mocked_deployment_pb) success_result = runner.invoke( cli.commands['lambda'], [ 'deploy', deployment_name, '-b', f'{bento_name}:{bento_version}', '--namespace', deployment_namespace, '--labels', 'created_by:admin,cicd:passed', '--region', 'us-west-2', ], ) assert success_result.exit_code == 0 list_result = runner.invoke( cli.commands['deployment'], [ 'list', '--labels', 'created_by=admin,cicd NotIn (failed, unsuccessful)', '--output', 'wide', ], ) assert list_result.exit_code == 0 assert deployment_name in list_result.output.strip() assert 'created_by:admin' in list_result.output.strip()