Пример #1
0
def DeleteDockerImage(args):
    """Deletes a Docker digest or image.

  If input is an image, delete the image along with its resources.

  If input is an image identified by digest, delete the digest.
  If input is an image identified by tag, delete the digest and the tag.
  If --delete-tags is specified, delete all tags associated with the image
  digest.

  Args:
    args: user input arguments.

  Returns:
    The long-running operation from DeletePackage API call.
  """
    image, version_or_tag = _ParseDockerImage(args.IMAGE, _INVALID_IMAGE_ERROR)
    _ValidateDockerRepo(image.docker_repo.GetRepositoryName())
    client = ar_requests.GetClient()
    messages = ar_requests.GetMessages()
    if not version_or_tag:
        console_io.PromptContinue(
            message="\nThis operation will delete all tags and images for " +
            image.GetDockerString() + ".",
            cancel_on_no=True)
        return ar_requests.DeletePackage(client, messages,
                                         image.GetPackageName())

    else:
        tags_to_delete = []
        docker_version = version_or_tag
        if isinstance(version_or_tag, DockerTag):
            docker_version = DockerVersion(
                version_or_tag.image,
                ar_requests.GetVersionFromTag(client, messages,
                                              version_or_tag.GetTagName()))
            tags_to_delete.append(version_or_tag)
        existing_tags = _GetDockerVersionTags(client, messages, docker_version)
        if args.delete_tags:
            tags_to_delete.extend(existing_tags)

        if len(existing_tags) != len(tags_to_delete):
            raise ar_exceptions.ArtifactRegistryError(
                "Cannot delete image {} because it is tagged. "
                "Existing tags are:\n- {}".format(
                    args.IMAGE, "\n- ".join(tag.GetDockerString()
                                            for tag in existing_tags)))

        _LogResourcesToDelete(docker_version, tags_to_delete)
        console_io.PromptContinue(
            message="\nThis operation will delete the above resources.",
            cancel_on_no=True)

        for tag in tags_to_delete:
            ar_requests.DeleteTag(client, messages, tag.GetTagName())
        return ar_requests.DeleteVersion(client, messages,
                                         docker_version.GetVersionName())
Пример #2
0
def DeleteDockerImage(args):
    """Deletes a Docker digest or image.

  If input is an image, delete the image along with its resources.

  If input is an image identified by digest, delete the digest.
  If input is an image identified by tag, delete the digest and the tag.
  If --delete-tags is specified, delete all tags associated with the image
  digest.

  Args:
    args: user input arguments.

  Returns:
    The long-running operation from DeletePackage API call.
  """
    image, version_or_tag = _ParseDockerImage(args.IMAGE, _INVALID_IMAGE_ERROR)
    client = ar_requests.GetClient()
    messages = ar_requests.GetMessages()
    if not version_or_tag:
        console_io.PromptContinue(
            message="\nThis operation will delete all tags and images for " +
            image.GetDockerString() + ".",
            cancel_on_no=True)
        return ar_requests.DeletePackage(client, messages,
                                         image.GetPackageName())

    else:
        tags_to_delete = []
        docker_version = version_or_tag
        if isinstance(version_or_tag, DockerTag):
            docker_version = DockerVersion(
                version_or_tag.image,
                ar_requests.GetVersionFromTag(client, messages,
                                              version_or_tag.GetTagName()))
            tags_to_delete.append(version_or_tag)
        if args.delete_tags:
            tags_to_delete.extend(
                _GetDockerVersionTags(client, messages, docker_version))

        _LogResourcesToDelete(docker_version, tags_to_delete)
        console_io.PromptContinue(
            message="\nThis operation will delete the above resources.",
            cancel_on_no=True)
        for tag in tags_to_delete:
            ar_requests.DeleteTag(client, messages, tag.GetTagName())
        return ar_requests.DeleteVersion(client, messages,
                                         docker_version.GetVersionName())