|
| 1 | +# --------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# --------------------------------------------------------- |
| 4 | +# pylint: disable=protected-access,redefined-builtin |
| 5 | + |
| 6 | +from typing import Dict, List, Optional |
| 7 | +from azure.ai.ml._restclient.v2022_10_01_preview.models import ( |
| 8 | + CustomService, |
| 9 | + Docker, |
| 10 | + Endpoint as RestEndpoint, |
| 11 | + EnvironmentVariable as RestEnvironmentVariable, |
| 12 | + EnvironmentVariableType as RestEnvironmentVariableType, |
| 13 | + Image as RestImage, |
| 14 | + ImageType as RestImageType, |
| 15 | + Protocol, |
| 16 | + VolumeDefinition as RestVolumeDefinition, |
| 17 | + VolumeDefinitionType as RestVolumeDefinitionType, |
| 18 | +) |
| 19 | +from azure.ai.ml.exceptions import ErrorCategory, ErrorTarget, ValidationException |
| 20 | +from azure.ai.ml.constants._compute import ( |
| 21 | + CustomApplicationDefaults, |
| 22 | + DUPLICATE_APPLICATION_ERROR, |
| 23 | + INVALID_VALUE_ERROR, |
| 24 | +) |
| 25 | +from azure.ai.ml._utils._experimental import experimental |
| 26 | + |
| 27 | + |
| 28 | +@experimental |
| 29 | +class ImageSettings: |
| 30 | + """Specifies an image configuration for a Custom Application. |
| 31 | +
|
| 32 | + :param reference: Image reference URL. |
| 33 | + :type reference: str |
| 34 | + """ |
| 35 | + |
| 36 | + def __init__(self, *, reference: str): |
| 37 | + self.reference = reference |
| 38 | + |
| 39 | + def _to_rest_object(self) -> RestImage: |
| 40 | + return RestImage(type=RestImageType.DOCKER, reference=self.reference) |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def _from_rest_object(cls, obj: RestImage) -> "ImageSettings": |
| 44 | + return ImageSettings(reference=obj.reference) |
| 45 | + |
| 46 | + |
| 47 | +@experimental |
| 48 | +class EndpointsSettings: |
| 49 | + """Specifies an endpoint configuration for a Custom Application. |
| 50 | +
|
| 51 | + :param target: Application port inside the container. |
| 52 | + :type target: int |
| 53 | + :param published: Port over which the application is exposed from container. |
| 54 | + :type published: int |
| 55 | + """ |
| 56 | + |
| 57 | + def __init__(self, *, target: int, published: int): |
| 58 | + EndpointsSettings._validate_endpoint_settings( |
| 59 | + target=target, published=published |
| 60 | + ) |
| 61 | + self.target = target |
| 62 | + self.published = published |
| 63 | + |
| 64 | + def _to_rest_object(self) -> RestEndpoint: |
| 65 | + return RestEndpoint( |
| 66 | + name=CustomApplicationDefaults.ENDPOINT_NAME, |
| 67 | + target=self.target, |
| 68 | + published=self.published, |
| 69 | + protocol=Protocol.HTTP, |
| 70 | + ) |
| 71 | + |
| 72 | + @classmethod |
| 73 | + def _from_rest_object(cls, obj: RestEndpoint) -> "EndpointsSettings": |
| 74 | + return EndpointsSettings(target=obj.target, published=obj.published) |
| 75 | + |
| 76 | + @classmethod |
| 77 | + def _validate_endpoint_settings(cls, target: int, published: int): |
| 78 | + ports = { |
| 79 | + CustomApplicationDefaults.TARGET_PORT: target, |
| 80 | + CustomApplicationDefaults.PUBLISHED_PORT: published, |
| 81 | + } |
| 82 | + min_value = CustomApplicationDefaults.PORT_MIN_VALUE |
| 83 | + max_value = CustomApplicationDefaults.PORT_MAX_VALUE |
| 84 | + |
| 85 | + for port_name, port in ports.items(): |
| 86 | + message = INVALID_VALUE_ERROR.format(port_name, min_value, max_value) |
| 87 | + if not min_value < port < max_value: |
| 88 | + raise ValidationException( |
| 89 | + message=message, |
| 90 | + target=ErrorTarget.COMPUTE, |
| 91 | + no_personal_data_message=message, |
| 92 | + error_category=ErrorCategory.USER_ERROR, |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +@experimental |
| 97 | +class VolumeSettings: |
| 98 | + """Specifies the Bind Mount settings for a Custom Application. |
| 99 | +
|
| 100 | + :param source: The host path of the mount. |
| 101 | + :type source: str |
| 102 | + :param target: The path in the container for the mount. |
| 103 | + :type target: str |
| 104 | + """ |
| 105 | + |
| 106 | + def __init__(self, *, source: str, target: str): |
| 107 | + self.source = source |
| 108 | + self.target = target |
| 109 | + |
| 110 | + def _to_rest_object(self) -> RestVolumeDefinition: |
| 111 | + return RestVolumeDefinition( |
| 112 | + type=RestVolumeDefinitionType.BIND, |
| 113 | + read_only=False, |
| 114 | + source=self.source, |
| 115 | + target=self.target, |
| 116 | + ) |
| 117 | + |
| 118 | + @classmethod |
| 119 | + def _from_rest_object(cls, obj: RestVolumeDefinition) -> "VolumeSettings": |
| 120 | + return VolumeSettings(source=obj.source, target=obj.target) |
| 121 | + |
| 122 | + |
| 123 | +@experimental |
| 124 | +class CustomApplications: |
| 125 | + """Specifies the custom service application configuration. |
| 126 | +
|
| 127 | + :param name: Name of the Custom Application. |
| 128 | + :type name: str |
| 129 | + :param image: Describes the Image Specifications. |
| 130 | + :type image: ImageSettings |
| 131 | + :param type: Type of the Custom Application. |
| 132 | + :type type: Optional[str] |
| 133 | + :param endpoints: Configuring the endpoints for the container. |
| 134 | + :type endpoints: List[EndpointsSettings] |
| 135 | + :param environment_variables: Environment Variables for the container. |
| 136 | + :type environment_variables: Optional[Dict[str, str]] |
| 137 | + :param bind_mounts: Configuration of the bind mounts for the container. |
| 138 | + :type bind_mounts: Optional[List[VolumeSettings]] |
| 139 | + """ |
| 140 | + |
| 141 | + def __init__( |
| 142 | + self, |
| 143 | + *, |
| 144 | + name: str, |
| 145 | + image: ImageSettings, |
| 146 | + type: str = CustomApplicationDefaults.DOCKER, |
| 147 | + endpoints: List[EndpointsSettings], |
| 148 | + environment_variables: Optional[Dict] = None, |
| 149 | + bind_mounts: Optional[List[VolumeSettings]] = None, |
| 150 | + **kwargs |
| 151 | + ): |
| 152 | + self.name = name |
| 153 | + self.type = type |
| 154 | + self.image = image |
| 155 | + self.endpoints = endpoints |
| 156 | + self.environment_variables = environment_variables |
| 157 | + self.bind_mounts = bind_mounts |
| 158 | + self.additional_properties = kwargs |
| 159 | + |
| 160 | + def _to_rest_object(self): |
| 161 | + endpoints = None |
| 162 | + if self.endpoints: |
| 163 | + endpoints = [endpoint._to_rest_object() for endpoint in self.endpoints] |
| 164 | + |
| 165 | + environment_variables = None |
| 166 | + if self.environment_variables: |
| 167 | + environment_variables = { |
| 168 | + name: RestEnvironmentVariable( |
| 169 | + type=RestEnvironmentVariableType.LOCAL, value=value |
| 170 | + ) |
| 171 | + for name, value in self.environment_variables.items() |
| 172 | + } |
| 173 | + |
| 174 | + volumes = None |
| 175 | + if self.bind_mounts: |
| 176 | + volumes = [volume._to_rest_object() for volume in self.bind_mounts] |
| 177 | + |
| 178 | + return CustomService( |
| 179 | + name=self.name, |
| 180 | + image=self.image._to_rest_object(), |
| 181 | + endpoints=endpoints, |
| 182 | + environment_variables=environment_variables, |
| 183 | + volumes=volumes, |
| 184 | + docker=( |
| 185 | + Docker(privileged=True) |
| 186 | + if self.type == CustomApplicationDefaults.DOCKER |
| 187 | + else None |
| 188 | + ), |
| 189 | + additional_properties=self.additional_properties, |
| 190 | + ) |
| 191 | + |
| 192 | + @classmethod |
| 193 | + def _from_rest_object(cls, obj: CustomService) -> "CustomApplications": |
| 194 | + endpoints = [] |
| 195 | + for endpoint in obj.endpoints: |
| 196 | + endpoints.append(EndpointsSettings._from_rest_object(endpoint)) |
| 197 | + |
| 198 | + environment_variables = ( |
| 199 | + {name: value.value for name, value in obj.environment_variables.items()} |
| 200 | + if obj.environment_variables |
| 201 | + else None |
| 202 | + ) |
| 203 | + |
| 204 | + bind_mounts = [] |
| 205 | + if obj.volumes: |
| 206 | + for volume in obj.volumes: |
| 207 | + bind_mounts.append(VolumeSettings._from_rest_object(volume)) |
| 208 | + |
| 209 | + return CustomApplications( |
| 210 | + name=obj.name, |
| 211 | + image=ImageSettings._from_rest_object(obj.image), |
| 212 | + endpoints=endpoints, |
| 213 | + environment_variables=environment_variables, |
| 214 | + bind_mounts=bind_mounts, |
| 215 | + additional_properties=obj.additional_properties, |
| 216 | + ) |
| 217 | + |
| 218 | + |
| 219 | +def validate_custom_applications(custom_apps: List[CustomApplications]): |
| 220 | + message = DUPLICATE_APPLICATION_ERROR |
| 221 | + |
| 222 | + names = [app.name for app in custom_apps] |
| 223 | + if len(set(names)) != len(names): |
| 224 | + raise ValidationException( |
| 225 | + message=message.format("application_name"), |
| 226 | + target=ErrorTarget.COMPUTE, |
| 227 | + no_personal_data_message=message.format("application_name"), |
| 228 | + error_category=ErrorCategory.USER_ERROR, |
| 229 | + ) |
| 230 | + |
| 231 | + published_ports = [ |
| 232 | + endpoint.published for app in custom_apps for endpoint in app.endpoints |
| 233 | + ] |
| 234 | + |
| 235 | + if len(set(published_ports)) != len(published_ports): |
| 236 | + raise ValidationException( |
| 237 | + message=message.format("published_port"), |
| 238 | + target=ErrorTarget.COMPUTE, |
| 239 | + no_personal_data_message=message.format("published_port"), |
| 240 | + error_category=ErrorCategory.USER_ERROR, |
| 241 | + ) |
0 commit comments