Пример #1
0
    def get_docker_image_latest_tag(self, docker_image_name, yml_docker_image):
        """Returns the docker image latest tag of the given docker image

        Args:
            docker_image_name: The name of the docker image
            yml_docker_image: The docker image as it appears in the yml file

        Returns:
            The last updated docker image tag
        """
        if yml_docker_image:
            if not yml_docker_image.startswith('demisto/'):
                error_message, error_code = Errors.not_demisto_docker()
                if self.handle_error(error_message, error_code, file_path=self.file_path):
                    return ''
                return "no-tag-required"
        try:
            return self.get_docker_image_latest_tag_request(docker_image_name)
        except (requests.exceptions.RequestException, Exception):
            if not docker_image_name:
                docker_image_name = yml_docker_image
            error_message, error_code = Errors.docker_tag_not_fetched(docker_image_name)
            if self.handle_error(error_message, error_code, file_path=self.file_path):
                return ''

            return "no-tag-required"
Пример #2
0
    def get_docker_image_latest_tag(self, docker_image_name, yml_docker_image):
        """Returns the docker image latest tag of the given docker image

        Args:
            docker_image_name: The name of the docker image
            yml_docker_image: The docker image as it appears in the yml file

        Returns:
            The last updated docker image tag
        """
        if yml_docker_image:
            if yml_docker_image.startswith('devdemisto/'):
                error_message, error_code = Errors.not_demisto_docker()
                self.handle_error(error_message,
                                  error_code,
                                  file_path=self.file_path)

            elif not yml_docker_image.startswith('demisto/'):
                error_message, error_code = Errors.not_demisto_docker()
                if self.handle_error(error_message,
                                     error_code,
                                     file_path=self.file_path):
                    return ''
                return "no-tag-required"
        try:
            tag = ''
            auth_token = DockerImageValidator.docker_auth(
                docker_image_name, False, DEFAULT_REGISTRY)
            headers = ACCEPT_HEADER.copy()
            if auth_token:
                headers['Authorization'] = 'Bearer {}'.format(auth_token)

            # first try to get the docker image tags using normal http request
            res = requests.get(
                url='https://hub.docker.com/v2/repositories/{}/tags'.format(
                    docker_image_name),
                verify=False,
                timeout=TIMEOUT,
            )
            if res.status_code == 200:
                tags = res.json().get('results', [])
                # if http request successful find the latest tag by date in the response
                if tags:
                    tag = DockerImageValidator.find_latest_tag_by_date(tags)

            else:
                # if http request did not succeed than get tags using the API.
                # See: https://docs.docker.com/registry/spec/api/#listing-image-tags
                res = requests.get('https://{}/v2/{}/tags/list'.format(
                    DEFAULT_REGISTRY, docker_image_name),
                                   headers=headers,
                                   timeout=TIMEOUT,
                                   verify=False)
                res.raise_for_status()
                # the API returns tags in lexical order with no date info - so try an get the numeric highest tag
                tags = res.json().get('tags', [])
                if tags:
                    tag = DockerImageValidator.lexical_find_latest_tag(tags)
            return tag
        except (requests.exceptions.RequestException, Exception):
            if not docker_image_name:
                docker_image_name = yml_docker_image
            error_message, error_code = Errors.docker_tag_not_fetched(
                docker_image_name)
            if self.handle_error(error_message,
                                 error_code,
                                 file_path=self.file_path):
                return ''

            return "no-tag-required"