| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright 2016 Red Hat | Ansible |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: docker_image_info |
| 11 |
|
| 12 |
short_description: Inspect docker images |
| 13 |
|
| 14 |
description: |
| 15 |
- Provide one or more image names, and the module will inspect each, returning an array of inspection results. |
| 16 |
- If an image does not exist locally, it will not appear in the results. If you want to check whether an image exists locally, |
| 17 |
you can call the module with the image name, then check whether the result list is empty (image does not exist) or has |
| 18 |
one element (the image exists locally). |
| 19 |
- The module will not attempt to pull images from registries. Use M(community.docker.docker_image) with O(community.docker.docker_image#module:source=pull) |
| 20 |
to ensure an image is pulled. |
| 21 |
notes: |
| 22 |
- This module was called C(docker_image_facts) before Ansible 2.8. The usage did not change. |
| 23 |
extends_documentation_fragment: |
| 24 |
- community.docker._docker.api_documentation |
| 25 |
- community.docker._attributes |
| 26 |
- community.docker._attributes.actiongroup_docker |
| 27 |
- community.docker._attributes.info_module |
| 28 |
- community.docker._attributes.idempotent_not_modify_state |
| 29 |
|
| 30 |
options: |
| 31 |
name: |
| 32 |
description: |
| 33 |
- An image name or a list of image names. Name format will be C(name[:tag]) or C(repository/name[:tag]), where C(tag) |
| 34 |
is optional. If a tag is not provided, V(latest) will be used. Instead of image names, also image IDs can be used. |
| 35 |
- If no name is provided, a list of all images will be returned. |
| 36 |
type: list |
| 37 |
elements: str |
| 38 |
|
| 39 |
requirements: |
| 40 |
- "Docker API >= 1.25" |
| 41 |
|
| 42 |
author: |
| 43 |
- Chris Houseknecht (@chouseknecht) |
| 44 |
""" |
| 45 |
|
| 46 |
EXAMPLES = r""" |
| 47 |
--- |
| 48 |
- name: Inspect a single image |
| 49 |
community.docker.docker_image_info: |
| 50 |
name: pacur/centos-7 |
| 51 |
|
| 52 |
- name: Inspect multiple images |
| 53 |
community.docker.docker_image_info: |
| 54 |
name: |
| 55 |
- pacur/centos-7 |
| 56 |
- sinatra |
| 57 |
register: result |
| 58 |
|
| 59 |
- name: Make sure that both images pacur/centos-7 and sinatra exist locally |
| 60 |
ansible.builtin.assert: |
| 61 |
that: |
| 62 |
- result.images | length == 2 |
| 63 |
""" |
| 64 |
|
| 65 |
RETURN = r""" |
| 66 |
images: |
| 67 |
description: |
| 68 |
- Inspection results for the selected images. |
| 69 |
- The list only contains inspection results of images existing locally. |
| 70 |
returned: always |
| 71 |
type: list |
| 72 |
elements: dict |
| 73 |
sample: [ |
| 74 |
{ |
| 75 |
"Architecture": "amd64", |
| 76 |
"Author": "", |
| 77 |
"Comment": "", |
| 78 |
"Config": { |
| 79 |
"AttachStderr": false, |
| 80 |
"AttachStdin": false, |
| 81 |
"AttachStdout": false, |
| 82 |
"Cmd": ["/etc/docker/registry/config.yml"], |
| 83 |
"Domainname": "", |
| 84 |
"Entrypoint": ["/bin/registry"], |
| 85 |
"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"], |
| 86 |
"ExposedPorts": {"5000/tcp": {}}, |
| 87 |
"Hostname": "e5c68db50333", |
| 88 |
"Image": "c72dce2618dc8f7b794d2b2c2b1e64e0205ead5befc294f8111da23bd6a2c799", |
| 89 |
"Labels": {}, |
| 90 |
"OnBuild": [], |
| 91 |
"OpenStdin": false, |
| 92 |
"StdinOnce": false, |
| 93 |
"Tty": false, |
| 94 |
"User": "", |
| 95 |
"Volumes": {"/var/lib/registry": {}}, |
| 96 |
"WorkingDir": "", |
| 97 |
}, |
| 98 |
"Container": "e83a452b8fb89d78a25a6739457050131ca5c863629a47639530d9ad2008d610", |
| 99 |
"ContainerConfig": { |
| 100 |
"AttachStderr": false, |
| 101 |
"AttachStdin": false, |
| 102 |
"AttachStdout": false, |
| 103 |
"Cmd": ["/bin/sh", "-c", '#(nop) CMD ["/etc/docker/registry/config.yml"]'], |
| 104 |
"Domainname": "", |
| 105 |
"Entrypoint": ["/bin/registry"], |
| 106 |
"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"], |
| 107 |
"ExposedPorts": {"5000/tcp": {}}, |
| 108 |
"Hostname": "e5c68db50333", |
| 109 |
"Image": "c72dce2618dc8f7b794d2b2c2b1e64e0205ead5befc294f8111da23bd6a2c799", |
| 110 |
"Labels": {}, |
| 111 |
"OnBuild": [], |
| 112 |
"OpenStdin": false, |
| 113 |
"StdinOnce": false, |
| 114 |
"Tty": false, |
| 115 |
"User": "", |
| 116 |
"Volumes": {"/var/lib/registry": {}}, |
| 117 |
"WorkingDir": "", |
| 118 |
}, |
| 119 |
"Created": "2016-03-08T21:08:15.399680378Z", |
| 120 |
"DockerVersion": "1.9.1", |
| 121 |
"GraphDriver": { |
| 122 |
"Data": null, |
| 123 |
"Name": "aufs", |
| 124 |
}, |
| 125 |
"Id": "53773d8552f07b730f3e19979e32499519807d67b344141d965463a950a66e08", |
| 126 |
"Name": "registry:2", |
| 127 |
"Os": "linux", |
| 128 |
"Parent": "f0b1f729f784b755e7bf9c8c2e65d8a0a35a533769c2588f02895f6781ac0805", |
| 129 |
"RepoDigests": [], |
| 130 |
"RepoTags": ["registry:2"], |
| 131 |
"Size": 0, |
| 132 |
"VirtualSize": 165808884, |
| 133 |
}, |
| 134 |
] |
| 135 |
""" |
| 136 |
|
| 137 |
import traceback |
| 138 |
import typing as t |
| 139 |
|
| 140 |
from ansible_collections.community.docker.plugins.module_utils._api.errors import ( |
| 141 |
DockerException, |
| 142 |
NotFound, |
| 143 |
) |
| 144 |
from ansible_collections.community.docker.plugins.module_utils._api.utils.utils import ( |
| 145 |
parse_repository_tag, |
| 146 |
) |
| 147 |
from ansible_collections.community.docker.plugins.module_utils._common_api import ( |
| 148 |
AnsibleDockerClient, |
| 149 |
RequestException, |
| 150 |
) |
| 151 |
from ansible_collections.community.docker.plugins.module_utils._util import ( |
| 152 |
DockerBaseClass, |
| 153 |
is_image_name_id, |
| 154 |
) |
| 155 |
|
| 156 |
|
| 157 |
class ImageManager(DockerBaseClass): |
| 158 |
def __init__(self, client: AnsibleDockerClient, results: dict[str, t.Any]) -> None: |
| 159 |
super().__init__() |
| 160 |
|
| 161 |
self.client = client |
| 162 |
self.results = results |
| 163 |
self.name = self.client.module.params.get("name") |
| 164 |
self.log(f"Gathering facts for images: {self.name}") |
| 165 |
|
| 166 |
if self.name: |
| 167 |
self.results["images"] = self.get_facts() |
| 168 |
else: |
| 169 |
self.results["images"] = self.get_all_images() |
| 170 |
|
| 171 |
def fail(self, msg: str) -> t.NoReturn: |
| 172 |
self.client.fail(msg) |
| 173 |
|
| 174 |
def get_facts(self) -> list[dict[str, t.Any]]: |
| 175 |
""" |
| 176 |
Lookup and inspect each image name found in the names parameter. |
| 177 |
|
| 178 |
:returns array of image dictionaries |
| 179 |
""" |
| 180 |
|
| 181 |
results = [] |
| 182 |
|
| 183 |
names = self.name |
| 184 |
if not isinstance(names, list): |
| 185 |
names = [names] |
| 186 |
|
| 187 |
for name in names: |
| 188 |
if is_image_name_id(name): |
| 189 |
self.log(f"Fetching image {name} (ID)") |
| 190 |
image = self.client.find_image_by_id(name, accept_missing_image=True) |
| 191 |
else: |
| 192 |
repository, tag = parse_repository_tag(name) |
| 193 |
if not tag: |
| 194 |
tag = "latest" |
| 195 |
self.log(f"Fetching image {repository}:{tag}") |
| 196 |
image = self.client.find_image(name=repository, tag=tag) |
| 197 |
if image: |
| 198 |
results.append(image) |
| 199 |
return results |
| 200 |
|
| 201 |
def get_all_images(self) -> list[dict[str, t.Any]]: |
| 202 |
results = [] |
| 203 |
params = { |
| 204 |
"only_ids": 0, |
| 205 |
"all": 0, |
| 206 |
} |
| 207 |
images = self.client.get_json("/images/json", params=params) |
| 208 |
for image in images: |
| 209 |
try: |
| 210 |
inspection = self.client.get_json("/images/{0}/json", image["Id"]) |
| 211 |
except NotFound: |
| 212 |
inspection = None |
| 213 |
except Exception as exc: # pylint: disable=broad-exception-caught |
| 214 |
self.fail(f"Error inspecting image {image['Id']} - {exc}") |
| 215 |
results.append(inspection) |
| 216 |
return results |
| 217 |
|
| 218 |
|
| 219 |
def main() -> None: |
| 220 |
argument_spec = { |
| 221 |
"name": {"type": "list", "elements": "str"}, |
| 222 |
} |
| 223 |
|
| 224 |
client = AnsibleDockerClient( |
| 225 |
argument_spec=argument_spec, |
| 226 |
supports_check_mode=True, |
| 227 |
) |
| 228 |
|
| 229 |
try: |
| 230 |
results = {"changed": False, "images": []} |
| 231 |
|
| 232 |
ImageManager(client, results) |
| 233 |
client.module.exit_json(**results) |
| 234 |
except DockerException as e: |
| 235 |
client.fail( |
| 236 |
f"An unexpected Docker error occurred: {e}", |
| 237 |
exception=traceback.format_exc(), |
| 238 |
) |
| 239 |
except RequestException as e: |
| 240 |
client.fail( |
| 241 |
f"An unexpected requests error occurred when trying to talk to the Docker daemon: {e}", |
| 242 |
exception=traceback.format_exc(), |
| 243 |
) |
| 244 |
|
| 245 |
|
| 246 |
if __name__ == "__main__": |
| 247 |
main() |