Beispiel #1
0
    def Run(self, args):
        """This is what ts 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:
      A list of the deleted docker_name.Tag and docker_name.Digest objects
    """
        # IMAGE_NAME: The fully-qualified image name to delete (with a digest).
        # Deletes the layers. Ex. gcr.io/google-appengine/java(@DIGEST|:TAG).

        http_obj = http.Http()
        with util.WrapExpectedDockerlessErrors():
            # collect input/validate
            digests, explicit_tags = self._ProcessImageNames(args.image_names)

            # Resolve tags to digests.
            for tag in explicit_tags:
                digests.add(util.GetDigestFromName(six.text_type(tag)))

            # Find all the tags that reference digests to be deleted.
            all_tags = set()
            for digest in digests:
                all_tags.update(util.GetDockerTagsForDigest(digest, http_obj))

            # Find all the tags that weren't specified explicitly.
            implicit_tags = all_tags.difference(explicit_tags)

            if implicit_tags and not args.force_delete_tags:
                log.error('Tags:')
                for tag in explicit_tags:
                    log.error('- ' + six.text_type(tag))
                raise exceptions.Error(
                    'This operation will implicitly delete the tags listed above. '
                    'Please manually remove with the `untag` command or re-run with '
                    '--force-delete-tags to confirm.')

            # Print the digests to be deleted.
            if digests:
                log.status.Print('Digests:')
            for digest in digests:
                self._PrintDigest(digest, http_obj)

            # Print the tags to be deleted.
            if explicit_tags:
                log.status.Print('Tags:')
            for tag in explicit_tags:
                log.status.Print('- ' + six.text_type(tag))

            # Prompt the user for consent to delete all the above.
            console_io.PromptContinue(
                'This operation will delete the tags and images identified by the '
                'digests above.',
                default=True,
                cancel_on_no=True)

            # The user has given explicit consent, merge the tags.
            explicit_tags.update(implicit_tags)

            # delete and collect output
            result = []
            for tag in explicit_tags:  # tags must be deleted before digests
                self._DeleteDockerTagOrDigest(tag, http_obj)
                result.append({'name': six.text_type(tag)})
            for digest in digests:
                self._DeleteDockerTagOrDigest(digest, http_obj)
                result.append({'name': six.text_type(digest)})
            return result
Beispiel #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.
    """

        filter_kinds = []
        if args.show_build_details:
            filter_kinds.append('BUILD_DETAILS')
        if args.show_package_vulnerability:
            filter_kinds.append('PACKAGE_VULNERABILITY')
            filter_kinds.append('DISCOVERY')
        if args.show_image_basis:
            filter_kinds.append('IMAGE_BASIS')
        if args.show_deployment:
            filter_kinds.append('DEPLOYABLE')

        if args.show_all_metadata:
            filter_kinds = _DEFAULT_KINDS

        if filter_kinds or args.metadata_filter:
            if filter_kinds:
                filter_from_flags = ' OR '.join(
                    ['kind = "{kind}"'.format(kind=fk) for fk in filter_kinds])

                if not args.metadata_filter:
                    occ_filter = filter_from_flags
                else:
                    occ_filter = '({occf}) AND ({flagf})'.format(
                        occf=args.metadata_filter, flagf=filter_from_flags)
            else:
                occ_filter = args.metadata_filter

            with util.WrapExpectedDockerlessErrors(args.image_name):
                img_name = util.GetDigestFromName(args.image_name)
                data = util.TransformContainerAnalysisData(
                    img_name,
                    occ_filter,
                    deployments=(args.show_deployment
                                 or args.show_all_metadata))
                # Clear out fields that weren't asked for and have no data.
                if (not data.build_details_summary.build_details
                        and not args.show_build_details
                        and not args.show_all_metadata):
                    del data.build_details_summary
                if (not data.package_vulnerability_summary.vulnerabilities
                        and not args.show_package_vulnerability
                        and not args.show_all_metadata):
                    del data.package_vulnerability_summary
                if (not data.discovery_summary.discovery
                        and not args.show_package_vulnerability
                        and not args.show_all_metadata):
                    del data.discovery_summary
                if (not data.image_basis_summary.base_images
                        and not args.show_image_basis
                        and not args.show_all_metadata):
                    del data.image_basis_summary
                if (not data.deployment_summary.deployments
                        and not args.show_deployment
                        and not args.show_all_metadata):
                    del data.deployment_summary
                return data
        else:
            with util.WrapExpectedDockerlessErrors(args.image_name):
                img_name = util.GetDigestFromName(args.image_name)
                return container_data_util.ContainerData(
                    registry=img_name.registry,
                    repository=img_name.repository,
                    digest=img_name.digest)