Exemple #1
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Raises:
      InvalidImageNameError: If the user specified an invalid image name.
    Returns:
      Some value that we want to have printed later.
    """

        try:
            img_name = util.GetDigestFromName(args.image_name)
            return util.TransformContainerAnalysisData(img_name,
                                                       args.occurrence_filter)
        except docker_http.V2DiagnosticException as err:
            raise util.GcloudifyRecoverableV2Errors(
                err, {
                    403:
                    'Describe failed, access denied: {0}'.format(
                        args.image_name),
                    404:
                    'Describe failed, not found: {0}'.format(args.image_name)
                })
Exemple #2
0
  def Run(self, args):
    """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Raises:
      InvalidImageNameError: If the user specified an invalid image name.
    Returns:
      Some value that we want to have printed later.
    """

    repository = util.ValidateRepositoryPath(args.image)
    http_obj = http.Http()
    with docker_image.FromRegistry(
        basic_creds=util.CredentialProvider(),
        name=repository,
        transport=http_obj) as image:
      try:
        return util.TransformManifests(
            image.manifests(),
            repository,
            show_occurrences=args.show_occurrences,
            occurrence_filter=args.occurrence_filter)
      except docker_http.V2DiagnosticException as err:
        raise util.GcloudifyRecoverableV2Errors(err, {
            403: 'Access denied: {0}'.format(repository),
            404: 'Not found: {0}'.format(repository)
        })
Exemple #3
0
 def _MapDeleteErr(self, err, tag_or_digest):
     return util.GcloudifyRecoverableV2Errors(
         err, {
             403: 'Delete failed, access denied: {0}'.format(tag_or_digest),
             404:
             'Delete failed, image not found: {0}'.format(tag_or_digest)
         })
Exemple #4
0
def RecoverFromDiagnosticException(image_name):
  try:
    yield
  except docker_http.V2DiagnosticException as err:
    raise util.GcloudifyRecoverableV2Errors(err, {
        403: 'Describe failed, access denied: {0}'.format(image_name),
        404: 'Describe failed, not found: {0}'.format(image_name)
    })
Exemple #5
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Raises:
      ArgumentError: If the user provided the flag --show-occurrences-from but
        --show-occurrences=False.
      InvalidImageNameError: If the user specified an invalid image name.
    Returns:
      Some value that we want to have printed later.
    """
        # Verify that --show-occurrences-from is set iff --show-occurrences=True.
        if args.IsSpecified(
                'show_occurrences_from') and not args.show_occurrences:
            raise ArgumentError(
                '--show-occurrences-from may only be set if --show-occurrences=True'
            )

        repository = util.ValidateRepositoryPath(args.image_name)
        http_obj = http.Http()
        with docker_image.FromRegistry(basic_creds=util.CredentialProvider(),
                                       name=repository,
                                       transport=http_obj) as image:
            try:
                manifests = image.manifests()
                # Only consider the top _DEFAULT_SHOW_OCCURRENCES_FROM images
                # to reduce computation time.
                most_recent_resource_urls = None
                if args.show_occurrences_from:
                    # This block is skipped when the user provided
                    # --show-occurrences-from=unlimited on the CLI.
                    most_recent_resource_urls = [
                        'https://%s@%s' % (args.image_name, k)
                        for k in heapq.nlargest(
                            args.show_occurrences_from,
                            manifests,
                            key=lambda k: manifests[k]['timeCreatedMs'])
                    ]
                return util.TransformManifests(
                    manifests,
                    repository,
                    show_occurrences=args.show_occurrences,
                    occurrence_filter=args.occurrence_filter,
                    resource_urls=most_recent_resource_urls)
            except docker_http.V2DiagnosticException as err:
                raise util.GcloudifyRecoverableV2Errors(
                    err, {
                        403: 'Access denied: {0}'.format(repository),
                        404: 'Not found: {0}'.format(repository)
                    })
Exemple #6
0
 def Push(image, dest_name, creds, http_obj, src_name,
          session_push_type):
     try:
         with session_push_type(dest_name, creds, http_obj) as push:
             push.upload(image)
             log.CreatedResource(dest_name)
         log.UpdatedResource(src_name)
     except docker_http.V2DiagnosticException as err:
         raise util.GcloudifyRecoverableV2Errors(
             err, {
                 403:
                 'Tagging failed, access denied: {0}'.format(dest_name)
             })
    def Run(self, args):
        # pylint: disable=missing-docstring
        def Push(image, dest_name, creds, http_obj, src_name,
                 session_push_type):
            with session_push_type(dest_name, creds, http_obj) as push:
                push.upload(image)
                log.CreatedResource(dest_name)
            log.UpdatedResource(src_name)

        http_obj = http.Http()

        src_name = util.GetDockerImageFromTagOrDigest(args.src_image)
        dest_name = docker_name.Tag(args.dest_image)

        console_io.PromptContinue('This will tag {0} with {1}'.format(
            src_name, dest_name),
                                  default=True,
                                  cancel_on_no=True)
        creds = util.CredentialProvider()
        try:
            with v2_2_image.FromRegistry(src_name, creds,
                                         http_obj) as v2_2_img:
                if v2_2_img.exists():
                    Push(v2_2_img, dest_name, creds, http_obj, src_name,
                         v2_2_session.Push)
                    return

            with v2_image.FromRegistry(src_name, creds, http_obj) as v2_img:
                Push(v2_img, dest_name, creds, http_obj, src_name,
                     v2_session.Push)

        except (v2_docker_http.V2DiagnosticException,
                v2_2_docker_http.V2DiagnosticException) as err:
            raise util.GcloudifyRecoverableV2Errors(
                err, {
                    httplib.FORBIDDEN:
                    'Add-tag failed, access denied.',
                    httplib.NOT_FOUND:
                    'Add-tag failed, not found: {0}'.format(src_name)
                })