Example #1
0
    def __init__(self, workspace):
        """
        :param workspace:
        :type workspace: azureml.core.workspace.Workspace
        :return:
        :rtype: None
        """

        self._workspace = None
        self._mms_endpoint = None

        if workspace:
            self._workspace = workspace
            self._mms_endpoint = _get_mms_url(workspace)

        else:
            raise Exception('Workspace must be provided.')
Example #2
0
    def _initialize(self, workspace, obj_dict):
        """Initialize the Image instance.

        This is used because the constructor is used as a getter.

        :param workspace:
        :type workspace: Workspace
        :param obj_dict:
        :type obj_dict: dict
        """
        self._validate_get_payload(obj_dict)

        self.created_time = parse(obj_dict['createdTime'])
        self.creation_state = obj_dict['creationState']
        self.description = obj_dict['description']
        self.id = obj_dict['id']
        self.image_build_log_uri = obj_dict.get('imageBuildLogUri', None)
        self.generated_dockerfile_uri = obj_dict.get('generatedDockerFileUri',
                                                     None)
        self.image_flavor = obj_dict.get('imageFlavor', None)
        self.image_location = obj_dict['imageLocation']
        self.image_type = obj_dict['imageType']
        self.model_ids = obj_dict['modelIds']
        self.name = obj_dict['name']
        self.tags = obj_dict['kvTags']
        self.properties = obj_dict['properties']
        self.version = obj_dict['version']

        models = []
        if 'modelDetails' in obj_dict:
            models = [
                Model.deserialize(workspace, model_payload)
                for model_payload in obj_dict['modelDetails']
            ]
        self.models = models
        self.workspace = workspace
        self._mms_endpoint = _get_mms_url(workspace) + '/images/{}'.format(
            self.id)
        self._auth = workspace._auth
Example #3
0
    def list(workspace,
             image_name=None,
             model_name=None,
             model_id=None,
             tags=None,
             properties=None):
        """List the Images associated with the corresponding workspace. Can be filtered with specific parameters.

        :param workspace: The Workspace object to list the Images in.
        :type workspace: azureml.core.workspace.Workspace
        :param image_name: Filter list to only include Images deployed with the specific image name.
        :type image_name: str
        :param model_name: Filter list to only include Images deployed with the specific model name.
        :type model_name: str
        :param model_id: Filter list to only include Images deployed with the specific model ID.
        :type model_id: str
        :param tags: Will filter based on the provided list, by either 'key' or '[key, value]'.
            Ex. ['key', ['key2', 'key2 value']]
        :type tags: builtin.list
        :param properties: Will filter based on the provided list, by either 'key' or '[key, value]'.
            Ex. ['key', ['key2', 'key2 value']]
        :type properties: builtin.list
        :return: A filtered list of Images in the provided workspace.
        :rtype: builtin.list[Images]
        :raises: azureml.exceptions.WebserviceException
        """
        warnings.warn(
            "Image class has been deprecated and will be removed in a future release. "
            + "Please migrate to using Environments. " +
            "https://docs.microsoft.com/en-us/azure/machine-learning/how-to-use-environments",
            category=DeprecationWarning,
            stacklevel=2)

        headers = workspace._auth.get_authentication_header()
        params = {'expand': 'true'}
        base_url = _get_mms_url(workspace)
        mms_url = base_url + '/images'

        if image_name:
            params['name'] = image_name
        if model_name:
            params['modelName'] = model_name
        if model_id:
            params['modelId'] = model_id
        if tags:
            tags_query = ""
            for tag in tags:
                if type(tag) is list:
                    tags_query = tags_query + tag[0] + "=" + tag[1] + ","
                else:
                    tags_query = tags_query + tag + ","
            tags_query = tags_query[:-1]
            params['tags'] = tags_query
        if properties:
            properties_query = ""
            for prop in properties:
                if type(prop) is list:
                    properties_query = properties_query + prop[0] + "=" + prop[
                        1] + ","
                else:
                    properties_query = properties_query + prop + ","
            properties_query = properties_query[:-1]
            params['properties'] = properties_query
        try:
            resp = ClientBase._execute_func(get_requests_session().get,
                                            mms_url,
                                            headers=headers,
                                            params=params,
                                            timeout=MMS_SYNC_TIMEOUT_SECONDS)
            resp.raise_for_status()
        except requests.Timeout:
            raise WebserviceException(
                'Error, request to Model Management Service timed out to URL:\n'
                '{}'.format(mms_url),
                logger=module_logger)
        except requests.exceptions.HTTPError:
            raise WebserviceException(
                'Received bad response from Model Management Service:\n'
                'Response Code: {}\n'
                'Headers: {}\n'
                'Content: {}'.format(resp.status_code, resp.headers,
                                     resp.content),
                logger=module_logger)

        content = resp.content
        if isinstance(content, bytes):
            content = content.decode('utf-8')
        image_payload = json.loads(content)
        paginated_results = get_paginated_results(image_payload, headers)

        return [
            Image.deserialize(workspace, image_dict)
            for image_dict in paginated_results
        ]
Example #4
0
    def create(workspace, name, models, image_config):
        """Create an image in the provided workspace.

        :param workspace: The workspace to associate with this image.
        :type workspace: workspace: azureml.core.workspace.Workspace
        :param name: The name to associate with this image.
        :type name: str
        :param models: A list of Model objects to package with this image. Can be an empty list.
        :type models: builtin.list[azureml.core.Model]
        :param image_config: The image config object to use to configure this image.
        :type image_config: azureml.core.image.image.ImageConfig
        :return: The created Image object.
        :rtype: azureml.core.Image
        :raises: azureml.exceptions.WebserviceException
        """
        warnings.warn(
            "Image class has been deprecated and will be removed in a future release. "
            + "Please migrate to using Environments. " +
            "https://docs.microsoft.com/en-us/azure/machine-learning/how-to-use-environments",
            category=DeprecationWarning,
            stacklevel=2)

        image_name_validation(name)
        model_ids = Model._resolve_to_model_ids(workspace, models, name)

        headers = {'Content-Type': 'application/json'}
        headers.update(workspace._auth.get_authentication_header())
        params = {}
        base_endpoint = _get_mms_url(workspace)
        image_url = base_endpoint + '/images'

        json_payload = image_config.build_create_payload(
            workspace, name, model_ids)

        print('Creating image')
        resp = ClientBase._execute_func(get_requests_session().post,
                                        image_url,
                                        params=params,
                                        headers=headers,
                                        json=json_payload)
        try:
            resp.raise_for_status()
        except requests.exceptions.HTTPError:
            raise WebserviceException(
                'Received bad response from Model Management Service:\n'
                'Response Code: {}\n'
                'Headers: {}\n'
                'Content: {}'.format(resp.status_code, resp.headers,
                                     resp.content),
                logger=module_logger)
        if resp.status_code >= 400:
            raise WebserviceException('Error occurred creating image:\n'
                                      'Response Code: {}\n'
                                      'Headers: {}\n'
                                      'Content: {}'.format(
                                          resp.status_code, resp.headers,
                                          resp.content),
                                      logger=module_logger)

        if 'Operation-Location' in resp.headers:
            operation_location = resp.headers['Operation-Location']
        else:
            raise WebserviceException(
                'Missing response header key: Operation-Location',
                logger=module_logger)

        create_operation_status_id = operation_location.split('/')[-1]
        operation_url = base_endpoint + '/operations/{}'.format(
            create_operation_status_id)
        operation_headers = workspace._auth.get_authentication_header()

        operation_resp = ClientBase._execute_func(
            get_requests_session().get,
            operation_url,
            params=params,
            headers=operation_headers,
            timeout=MMS_SYNC_TIMEOUT_SECONDS)
        try:
            operation_resp.raise_for_status()
        except requests.Timeout:
            raise WebserviceException(
                'Error, request to {} timed out.'.format(operation_url),
                logger=module_logger)
        except requests.exceptions.HTTPError:
            raise WebserviceException(
                'Received bad response from Model Management Service:\n'
                'Response Code: {}\n'
                'Headers: {}\n'
                'Content: {}'.format(operation_resp.status_code,
                                     operation_resp.headers,
                                     operation_resp.content),
                logger=module_logger)

        content = operation_resp.content
        if isinstance(content, bytes):
            content = content.decode('utf-8')
        operation_content = json.loads(content)
        if 'resourceLocation' in operation_content:
            image_id = operation_content['resourceLocation'].split('/')[-1]
        else:
            raise WebserviceException(
                'Invalid operation payload, missing resourceLocation:\n'
                '{}'.format(operation_content),
                logger=module_logger)

        image = Image(workspace, id=image_id)
        image._operation_endpoint = operation_url
        return image
Example #5
0
    def _get(workspace,
             name=None,
             id=None,
             tags=None,
             properties=None,
             version=None):
        """Get the image with the given filtering criteria.

        :param workspace:
        :type workspace: azureml.core.workspace.Workspace
        :param name:
        :type name: str
        :param id:
        :type id: str
        :param tags:
        :type tags: dict[str, str]
        :param properties:
        :type properties: dict[str, str]
        :param version:
        :type version: str
        :return: azureml.core.Image payload dictionary
        :rtype: dict
        :raises: azureml.exceptions.WebserviceException
        """
        if not name and not id:
            raise WebserviceException(
                'Error, one of id or name must be provided.',
                logger=module_logger)

        headers = workspace._auth.get_authentication_header()
        params = {'orderBy': 'CreatedAtDesc', 'count': 1, 'expand': 'true'}
        base_endpoint = _get_mms_url(workspace)
        mms_endpoint = base_endpoint + '/images'

        if id:
            image_url = mms_endpoint + '/{}'.format(id)
        else:
            image_url = mms_endpoint
            params['name'] = name
        if tags:
            tags_query = ""
            for tag in tags:
                if type(tag) is list:
                    tags_query = tags_query + tag[0] + "=" + tag[1] + ","
                else:
                    tags_query = tags_query + tag + ","
            tags_query = tags_query[:-1]
            params['tags'] = tags_query
        if properties:
            properties_query = ""
            for prop in properties:
                if type(prop) is list:
                    properties_query = properties_query + prop[0] + "=" + prop[
                        1] + ","
                else:
                    properties_query = properties_query + prop + ","
            properties_query = properties_query[:-1]
            params['properties'] = properties_query
        if version:
            params['version'] = version

        resp = ClientBase._execute_func(get_requests_session().get,
                                        image_url,
                                        headers=headers,
                                        params=params,
                                        timeout=MMS_SYNC_TIMEOUT_SECONDS)

        if resp.status_code == 200:
            content = resp.content
            if isinstance(content, bytes):
                content = content.decode('utf-8')
            image_payload = json.loads(content)
            if id:
                return image_payload
            else:
                paginated_results = get_paginated_results(
                    image_payload, headers)
                if paginated_results:
                    return paginated_results[0]
                else:
                    return None
        elif resp.status_code == 404:
            return None
        else:
            raise WebserviceException(
                'Received bad response from Model Management Service:\n'
                'Response Code: {}\n'
                'Headers: {}\n'
                'Content: {}'.format(resp.status_code, resp.headers,
                                     resp.content),
                logger=module_logger)