summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/docker/plugins/modules/docker_image_remove.py
blob: c8ea326b29b2f931ec52359b8b325cea6aa1d62d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/python
#
# Copyright 2016 Red Hat | Ansible
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import absolute_import, division, print_function
__metaclass__ = type


DOCUMENTATION = '''
---
module: docker_image_remove

short_description: Remove Docker images

version_added: 3.6.0

description:
  - Remove Docker images from the Docker daemon.

extends_documentation_fragment:
  - community.docker.docker.api_documentation
  - community.docker.attributes
  - community.docker.attributes.actiongroup_docker

attributes:
  check_mode:
    support: full
  diff_mode:
    support: full

options:
  name:
    description:
      - "Image name. Name format will be one of: C(name), C(repository/name), C(registry_server:port/name).
        When pushing or pulling an image the name can optionally include the tag by appending C(:tag_name)."
      - Note that image IDs (hashes) can also be used.
    type: str
    required: true
  tag:
    description:
      - Tag for the image name O(name) that is to be tagged.
      - If O(name)'s format is C(name:tag), then the tag value from O(name) will take precedence.
    type: str
    default: latest
  force:
    description:
      - Un-tag and remove all images matching the specified name.
    type: bool
    default: false
  prune:
    description:
      - Delete untagged parent images.
    type: bool
    default: true

requirements:
  - "Docker API >= 1.25"

author:
  - Felix Fontein (@felixfontein)

seealso:
  - module: community.docker.docker_image_load
  - module: community.docker.docker_image_pull
  - module: community.docker.docker_image_tag
'''

EXAMPLES = '''

- name: Remove an image
  community.docker.docker_image_remove:
    name: pacur/centos-7
'''

RETURN = '''
image:
    description:
      - Image inspection results for the affected image before removal.
      - Empty if the image was not found.
    returned: success
    type: dict
    sample: {}
deleted:
    description:
      - The digests of the images that were deleted.
    returned: success
    type: list
    elements: str
    sample: []
untagged:
    description:
      - The digests of the images that were untagged.
    returned: success
    type: list
    elements: str
    sample: []
'''

import traceback

from ansible.module_utils.common.text.converters import to_native

from ansible_collections.community.docker.plugins.module_utils.common_api import (
    AnsibleDockerClient,
    RequestException,
)

from ansible_collections.community.docker.plugins.module_utils.util import (
    DockerBaseClass,
    is_image_name_id,
    is_valid_tag,
)

from ansible_collections.community.docker.plugins.module_utils._api.errors import DockerException, NotFound
from ansible_collections.community.docker.plugins.module_utils._api.utils.utils import (
    parse_repository_tag,
)


class ImageRemover(DockerBaseClass):

    def __init__(self, client):
        super(ImageRemover, self).__init__()

        self.client = client
        self.check_mode = self.client.check_mode
        self.diff = self.client.module._diff

        parameters = self.client.module.params
        self.name = parameters['name']
        self.tag = parameters['tag']
        self.force = parameters['force']
        self.prune = parameters['prune']

        if not is_valid_tag(self.tag, allow_empty=True):
            self.fail('"{0}" is not a valid docker tag'.format(self.tag))

        # If name contains a tag, it takes precedence over tag parameter.
        if not is_image_name_id(self.name):
            repo, repo_tag = parse_repository_tag(self.name)
            if repo_tag:
                self.name = repo
                self.tag = repo_tag

    def fail(self, msg):
        self.client.fail(msg)

    def get_diff_state(self, image):
        if not image:
            return dict(exists=False)
        return dict(
            exists=True,
            id=image['Id'],
            tags=sorted(image.get('RepoTags') or []),
            digests=sorted(image.get('RepoDigests') or []),
        )

    def absent(self):
        results = dict(
            changed=False,
            actions=[],
            image={},
            deleted=[],
            untagged=[],
        )

        name = self.name
        if is_image_name_id(name):
            image = self.client.find_image_by_id(name, accept_missing_image=True)
        else:
            image = self.client.find_image(name, self.tag)
            if self.tag:
                name = "%s:%s" % (self.name, self.tag)

        if self.diff:
            results['diff'] = dict(before=self.get_diff_state(image))

        if not image:
            if self.diff:
                results['diff']['after'] = self.get_diff_state(image)
            return results

        results['changed'] = True
        results['actions'].append("Removed image %s" % (name))
        results['image'] = image

        if not self.check_mode:
            try:
                res = self.client.delete_json('/images/{0}', name, params={'force': self.force, 'noprune': not self.prune})
            except NotFound:
                # If the image vanished while we were trying to remove it, don't fail
                res = []
            except Exception as exc:
                self.fail("Error removing image %s - %s" % (name, to_native(exc)))

            for entry in res:
                if entry.get('Untagged'):
                    results['untagged'].append(entry['Untagged'])
                if entry.get('Deleted'):
                    results['deleted'].append(entry['Deleted'])

            results['untagged'] = sorted(results['untagged'])
            results['deleted'] = sorted(results['deleted'])

            if self.diff:
                image_after = self.client.find_image_by_id(image['Id'], accept_missing_image=True)
                results['diff']['after'] = self.get_diff_state(image_after)

        elif is_image_name_id(name):
            results['deleted'].append(image['Id'])
            results['untagged'] = sorted((image.get('RepoTags') or []) + (image.get('RepoDigests') or []))
            if not self.force and results['untagged']:
                self.fail('Cannot delete image by ID that is still in use - use force=true')
            if self.diff:
                results['diff']['after'] = self.get_diff_state({})

        elif is_image_name_id(self.tag):
            results['untagged'].append(name)
            if len(image.get('RepoTags') or []) < 1 and len(image.get('RepoDigests') or []) < 2:
                results['deleted'].append(image['Id'])
            if self.diff:
                results['diff']['after'] = self.get_diff_state(image)
                try:
                    results['diff']['after']['digests'].remove(name)
                except ValueError:
                    pass

        else:
            results['untagged'].append(name)
            if len(image.get('RepoTags') or []) < 2 and len(image.get('RepoDigests') or []) < 1:
                results['deleted'].append(image['Id'])
            if self.diff:
                results['diff']['after'] = self.get_diff_state(image)
                try:
                    results['diff']['after']['tags'].remove(name)
                except ValueError:
                    pass

        return results


def main():
    argument_spec = dict(
        name=dict(type='str', required=True),
        tag=dict(type='str', default='latest'),
        force=dict(type='bool', default=False),
        prune=dict(type='bool', default=True),
    )

    client = AnsibleDockerClient(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )

    try:
        results = ImageRemover(client).absent()
        client.module.exit_json(**results)
    except DockerException as e:
        client.fail('An unexpected Docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc())
    except RequestException as e:
        client.fail(
            'An unexpected requests error occurred when trying to talk to the Docker daemon: {0}'.format(to_native(e)),
            exception=traceback.format_exc())


if __name__ == '__main__':
    main()