@ -177,6 +177,14 @@ builder_cache_space_reclaimed:
sample : ' 0 '
'''
import traceback
try :
from docker . errors import DockerException
except ImportError :
# missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass
from distutils . version import LooseVersion
from ansible . module_utils . docker . common import AnsibleDockerClient
@ -214,36 +222,39 @@ def main():
msg = " Error: Docker SDK for Python ' s version is %s . Minimum version required for builds option is %s . Use `pip install --upgrade docker` to upgrade. "
client . fail ( msg % ( docker_version , cache_min_version ) )
result = dict ( )
if client . module . params [ ' containers ' ] :
filters = clean_dict_booleans_for_docker_api ( client . module . params . get ( ' containers_filters ' ) )
res = client . prune_containers ( filters = filters )
result [ ' containers ' ] = res . get ( ' ContainersDeleted ' ) or [ ]
result [ ' containers_space_reclaimed ' ] = res [ ' SpaceReclaimed ' ]
if client . module . params [ ' images ' ] :
filters = clean_dict_booleans_for_docker_api ( client . module . params . get ( ' images_filters ' ) )
res = client . prune_images ( filters = filters )
result [ ' images ' ] = res . get ( ' ImagesDeleted ' ) or [ ]
result [ ' images_space_reclaimed ' ] = res [ ' SpaceReclaimed ' ]
if client . module . params [ ' networks ' ] :
filters = clean_dict_booleans_for_docker_api ( client . module . params . get ( ' networks_filters ' ) )
res = client . prune_networks ( filters = filters )
result [ ' networks ' ] = res . get ( ' NetworksDeleted ' ) or [ ]
if client . module . params [ ' volumes ' ] :
filters = clean_dict_booleans_for_docker_api ( client . module . params . get ( ' volumes_filters ' ) )
res = client . prune_volumes ( filters = filters )
result [ ' volumes ' ] = res . get ( ' VolumesDeleted ' ) or [ ]
result [ ' volumes_space_reclaimed ' ] = res [ ' SpaceReclaimed ' ]
if client . module . params [ ' builder_cache ' ] :
res = client . prune_builds ( )
result [ ' builder_cache_space_reclaimed ' ] = res [ ' SpaceReclaimed ' ]
client . module . exit_json ( * * result )
try :
result = dict ( )
if client . module . params [ ' containers ' ] :
filters = clean_dict_booleans_for_docker_api ( client . module . params . get ( ' containers_filters ' ) )
res = client . prune_containers ( filters = filters )
result [ ' containers ' ] = res . get ( ' ContainersDeleted ' ) or [ ]
result [ ' containers_space_reclaimed ' ] = res [ ' SpaceReclaimed ' ]
if client . module . params [ ' images ' ] :
filters = clean_dict_booleans_for_docker_api ( client . module . params . get ( ' images_filters ' ) )
res = client . prune_images ( filters = filters )
result [ ' images ' ] = res . get ( ' ImagesDeleted ' ) or [ ]
result [ ' images_space_reclaimed ' ] = res [ ' SpaceReclaimed ' ]
if client . module . params [ ' networks ' ] :
filters = clean_dict_booleans_for_docker_api ( client . module . params . get ( ' networks_filters ' ) )
res = client . prune_networks ( filters = filters )
result [ ' networks ' ] = res . get ( ' NetworksDeleted ' ) or [ ]
if client . module . params [ ' volumes ' ] :
filters = clean_dict_booleans_for_docker_api ( client . module . params . get ( ' volumes_filters ' ) )
res = client . prune_volumes ( filters = filters )
result [ ' volumes ' ] = res . get ( ' VolumesDeleted ' ) or [ ]
result [ ' volumes_space_reclaimed ' ] = res [ ' SpaceReclaimed ' ]
if client . module . params [ ' builder_cache ' ] :
res = client . prune_builds ( )
result [ ' builder_cache_space_reclaimed ' ] = res [ ' SpaceReclaimed ' ]
client . module . exit_json ( * * result )
except DockerException as e :
client . fail ( ' An unexpected docker error occurred: {0} ' . format ( e ) , exception = traceback . format_exc ( ) )
if __name__ == ' __main__ ' :