
    Vh/                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZ d Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: docker_prune

short_description: Allows to prune various docker objects

description:
  - Allows to run C(docker container prune), C(docker image prune), C(docker network prune) and C(docker volume prune) through
    the Docker API.
extends_documentation_fragment:
  - community.docker.docker.api_documentation
  - community.docker.attributes
  - community.docker.attributes.actiongroup_docker

attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
  idempotent:
    support: full

options:
  containers:
    description:
      - Whether to prune containers.
    type: bool
    default: false
  containers_filters:
    description:
      - A dictionary of filter values used for selecting containers to delete.
      - 'For example, C(until: 24h).'
      - See L(the docker documentation,https://docs.docker.com/engine/reference/commandline/container_prune/#filtering) for
        more information on possible filters.
    type: dict
  images:
    description:
      - Whether to prune images.
    type: bool
    default: false
  images_filters:
    description:
      - A dictionary of filter values used for selecting images to delete.
      - 'For example, C(dangling: true).'
      - See L(the docker documentation,https://docs.docker.com/engine/reference/commandline/image_prune/#filtering) for more
        information on possible filters.
    type: dict
  networks:
    description:
      - Whether to prune networks.
    type: bool
    default: false
  networks_filters:
    description:
      - A dictionary of filter values used for selecting networks to delete.
      - See L(the docker documentation,https://docs.docker.com/engine/reference/commandline/network_prune/#filtering) for
        more information on possible filters.
    type: dict
  volumes:
    description:
      - Whether to prune volumes.
    type: bool
    default: false
  volumes_filters:
    description:
      - A dictionary of filter values used for selecting volumes to delete.
      - See L(the docker documentation,https://docs.docker.com/engine/reference/commandline/volume_prune/#filtering) for more
        information on possible filters.
    type: dict
  builder_cache:
    description:
      - Whether to prune the builder cache.
    type: bool
    default: false
  builder_cache_all:
    description:
      - Whether to remove all types of build cache.
    type: bool
    default: false
    version_added: 3.10.0
  builder_cache_filters:
    description:
      - A dictionary of filter values used for selecting images to delete.
      - 'For example, C(until: 10m).'
      - See L(the API documentation,https://docs.docker.com/engine/api/v1.44/#tag/Image/operation/BuildPrune) for more information
        on possible filters.
    type: dict
    version_added: 3.10.0
  builder_cache_keep_storage:
    description:
      - Amount of disk space to keep for cache in format C(<number>[<unit>]).".
      - Number is a positive integer. Unit can be one of V(B) (byte), V(K) (kibibyte, 1024B), V(M) (mebibyte), V(G) (gibibyte),
        V(T) (tebibyte), or V(P) (pebibyte).
      - Omitting the unit defaults to bytes.
    type: str
    version_added: 3.10.0

author:
  - "Felix Fontein (@felixfontein)"

notes:
  - The module always returned C(changed=false) before community.docker 3.5.1.
requirements:
  - "Docker API >= 1.25"
a  
---
- name: Prune containers older than 24h
  community.docker.docker_prune:
    containers: true
    containers_filters:
      # only consider containers created more than 24 hours ago
      until: 24h

- name: Prune containers with labels
  community.docker.docker_prune:
    containers: true
    containers_filters:
      # Prune containers whose "foo" label has value "bar", and
      # whose "bam" label has value "baz". If you only want to
      # compare one label, you can provide it as a string instead
      # of a list with one element.
      label:
        - foo=bar
        - bam=baz
      # Prune containers whose label "bar" does *not* have value
      # "baz". If you want to avoid more than one label, you can
      # provide a list of multiple label-value pairs.
      "label!": bar=baz

- name: Prune everything
  community.docker.docker_prune:
    containers: true
    images: true
    networks: true
    volumes: true
    builder_cache: true

- name: Prune everything (including non-dangling images)
  community.docker.docker_prune:
    containers: true
    images: true
    images_filters:
      dangling: false
    networks: true
    volumes: true
    builder_cache: true
a  
# containers
containers:
  description:
    - List of IDs of deleted containers.
  returned: O(containers=true)
  type: list
  elements: str
  sample: []
containers_space_reclaimed:
  description:
    - Amount of reclaimed disk space from container pruning in bytes.
  returned: O(containers=true)
  type: int
  sample: 0

# images
images:
  description:
    - List of IDs of deleted images.
  returned: O(images=true)
  type: list
  elements: str
  sample: []
images_space_reclaimed:
  description:
    - Amount of reclaimed disk space from image pruning in bytes.
  returned: O(images=true)
  type: int
  sample: 0

# networks
networks:
  description:
    - List of IDs of deleted networks.
  returned: O(networks=true)
  type: list
  elements: str
  sample: []

# volumes
volumes:
  description:
    - List of IDs of deleted volumes.
  returned: O(volumes=true)
  type: list
  elements: str
  sample: []
volumes_space_reclaimed:
  description:
    - Amount of reclaimed disk space from volumes pruning in bytes.
  returned: O(volumes=true)
  type: int
  sample: 0

# builder_cache
builder_cache_space_reclaimed:
  description:
    - Amount of reclaimed disk space from builder cache pruning in bytes.
  returned: O(builder_cache=true)
  type: int
  sample: 0
builder_cache_caches_deleted:
  description:
    - The build caches that were deleted.
  returned: O(builder_cache=true) and API version is 1.39 or later
  type: list
  elements: str
  sample: []
  version_added: 3.10.0
N)	to_native)human_to_bytes)AnsibleDockerClientRequestException)"clean_dict_booleans_for_docker_api)DockerException)convert_filtersc                  
   t        t        dd      t        d      t        dd      t        d      t        dd      t        d      t        dd      t        d      t        dd      t        dd      t        d      t        d            } t        | t        t        d	      t        d
	      t        d	      t        d
	                  }d }|j                  j                  j	                  d      /	 t        |j                  j                  j	                  d            }	 t               }d}|j                  j                  d   r|t        |j                  j                  j	                  d      d      }dt        |      i}|j                  d|      }|j	                  d      xs g |d<   |d   |d<   |d   s|d   rd}|j                  j                  d   r|t        |j                  j                  j	                  d      d      }dt        |      i}|j                  d|      }|j	                  d      xs g |d<   |d   |d<   |d   s|d   rd}|j                  j                  d   rot        |j                  j                  j	                  d       d      }dt        |      i}|j                  d!|      }|j	                  d"      xs g |d<   |d   rd}|j                  j                  d#   r|t        |j                  j                  j	                  d$      d      }dt        |      i}|j                  d%|      }|j	                  d&      xs g |d#<   |d   |d'<   |d#   s|d'   rd}|j                  j                  d(   rt        |j                  j                  j	                  d)      d      }dt        |      i}|j                  j                  j	                  d*      rd+|d,<   |||d-<   |j                  d.|      }|d   |d/<   |d/   rd}d0|v r|d0   |d1<   |d1   rd}||d2<    |j                  j                  d6i | y # t        $ r6}|j                  j                  dj                  |             Y d }~ld }~ww xY w# t        $ rG}	|j                  d3j                  t        |	            t!        j"                         4       Y d }	~	y d }	~	wt$        $ rG}	|j                  d5j                  t        |	            t!        j"                         4       Y d }	~	y d }	~	ww xY w)7NboolF)typedefaultdict)r   str)
containerscontainers_filtersimagesimages_filtersnetworksnetworks_filtersvolumesvolumes_filtersbuilder_cachebuilder_cache_allbuilder_cache_filtersbuilder_cache_keep_storagez1.31)docker_py_versionz1.39)r   r   r   r   )argument_specoption_minimal_versionsr   z<Error while parsing value of builder_cache_keep_storage: {0})msgr   r   T)allow_sequencesfiltersz/containers/prune)paramsContainersDeletedSpaceReclaimedcontainers_space_reclaimedr   r   z/images/pruneImagesDeletedimages_space_reclaimedr   r   z/networks/pruneNetworksDeletedr   r   z/volumes/pruneVolumesDeletedvolumes_space_reclaimedr   r   r   trueallzkeep-storagez/build/prunebuilder_cache_space_reclaimedCachesDeletedbuilder_cache_caches_deletedchangedz(An unexpected Docker error occurred: {0})	exceptionzSAn unexpected requests error occurred when trying to talk to the Docker daemon: {0} )r   r   moduler%   getr   
ValueError	fail_jsonformatr
   r   post_to_json	exit_jsonr   failr   	traceback
format_excr	   )
r    clientr   excresultr3   r$   r%   reses
             q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/docker/plugins/modules/docker_prune.pymainrF      s   VU3V,/(6516*&%0&)6FE:"/#'U#3M !# $8"V<"&"@'+f'E	!
	F "&}} <=I	t)78L8L8P8PQm8n)o&?.==-89M9M9Q9QRf9gy}~G!9:F%%&9&%IC#&77+>#?#E2F< 367G3HF/0l#v.J'K==)89M9M9Q9QRb9cuyzG!9:F%%of%EC"ww7=2F8/23C/DF+,h6*B#C==
+89M9M9Q9QRd9ew{|G!9:F%%&7%GC!$):!;!ArF:j!==	*89M9M9Q9QRc9dvz{G!9:F%%&6v%FC #(8 9 ?RF9034D0EF,-i F+D$E==089M9M9Q9QRi9j  }A  BG!9:F}}##''(;< &u)5)C~&%%nV%DC69:J6KF2356#%9<_9M5689"G#y)&)y  	tMM##(f(m(mnq(r#ss	tz  w>EEiPQlS_h_s_s_uvv .ahhirstiuv**, 	 	. 	..s=    .Q  /L0R"  	R)+RR"	U +=S--U 9=T;;U __main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNr>   +ansible.module_utils.common.text.convertersr   +ansible.module_utils.common.text.formattersr   Dansible_collections.community.docker.plugins.module_utils.common_apir   r	   >ansible_collections.community.docker.plugins.module_utils.utilr
   Eansible_collections.community.docker.plugins.module_utils._api.errorsr   Jansible_collections.community.docker.plugins.module_utils._api.utils.utilsr   rF   __name__r5       rE   <module>rU      sd    A @gR*XF
P  A F
 n a fa.H zF rT   