| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2023, Felix Fontein <[email protected]> |
| 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_pull |
| 11 |
|
| 12 |
short_description: Pull Docker images from registries |
| 13 |
|
| 14 |
version_added: 3.6.0 |
| 15 |
|
| 16 |
description: |
| 17 |
- Pulls a Docker image from a registry. |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.docker._docker.api_documentation |
| 20 |
- community.docker._attributes |
| 21 |
- community.docker._attributes.actiongroup_docker |
| 22 |
|
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: partial |
| 26 |
details: |
| 27 |
- When trying to pull an image with O(pull=always), the module assumes this is always changed in check mode. |
| 28 |
- When check mode is combined with diff mode, the pulled image's ID is always shown as V(unknown) in the diff. |
| 29 |
diff_mode: |
| 30 |
support: full |
| 31 |
idempotent: |
| 32 |
support: full |
| 33 |
|
| 34 |
options: |
| 35 |
name: |
| 36 |
description: |
| 37 |
- Image name. Name format must be one of V(name), V(repository/name), or V(registry_server:port/name). |
| 38 |
- The name can optionally include the tag by appending V(:tag_name), or it can contain a digest by appending V(@hash:digest). |
| 39 |
type: str |
| 40 |
required: true |
| 41 |
tag: |
| 42 |
description: |
| 43 |
- Used to select an image when pulling. Defaults to V(latest). |
| 44 |
- If O(name) parameter format is C(name:tag) or C(image@hash:digest), then O(tag) will be ignored. |
| 45 |
type: str |
| 46 |
default: latest |
| 47 |
platform: |
| 48 |
description: |
| 49 |
- Ask for this specific platform when pulling. |
| 50 |
type: str |
| 51 |
pull: |
| 52 |
description: |
| 53 |
- Determines when to pull an image. |
| 54 |
- If V(always), will always pull the image. |
| 55 |
- If V(not_present), will only pull the image if no image of the name exists on the current Docker daemon, or if O(platform) |
| 56 |
does not match. |
| 57 |
type: str |
| 58 |
choices: |
| 59 |
- always |
| 60 |
- not_present |
| 61 |
default: always |
| 62 |
|
| 63 |
requirements: |
| 64 |
- "Docker API >= 1.25" |
| 65 |
|
| 66 |
author: |
| 67 |
- Felix Fontein (@felixfontein) |
| 68 |
|
| 69 |
seealso: |
| 70 |
- module: community.docker.docker_image_pull |
| 71 |
- module: community.docker.docker_image_remove |
| 72 |
- module: community.docker.docker_image_tag |
| 73 |
""" |
| 74 |
|
| 75 |
EXAMPLES = r""" |
| 76 |
--- |
| 77 |
- name: Pull an image |
| 78 |
community.docker.docker_image_pull: |
| 79 |
name: pacur/centos-7 |
| 80 |
# Select platform for pulling. If not specified, will pull whatever docker prefers. |
| 81 |
platform: amd64 |
| 82 |
""" |
| 83 |
|
| 84 |
RETURN = r""" |
| 85 |
image: |
| 86 |
description: Image inspection results for the affected image. |
| 87 |
returned: success |
| 88 |
type: dict |
| 89 |
sample: {} |
| 90 |
""" |
| 91 |
|
| 92 |
import traceback |
| 93 |
import typing as t |
| 94 |
|
| 95 |
from ansible_collections.community.docker.plugins.module_utils._api.errors import ( |
| 96 |
DockerException, |
| 97 |
) |
| 98 |
from ansible_collections.community.docker.plugins.module_utils._api.utils.utils import ( |
| 99 |
parse_repository_tag, |
| 100 |
) |
| 101 |
from ansible_collections.community.docker.plugins.module_utils._common_api import ( |
| 102 |
AnsibleDockerClient, |
| 103 |
RequestException, |
| 104 |
) |
| 105 |
from ansible_collections.community.docker.plugins.module_utils._platform import ( |
| 106 |
compare_platform_strings, |
| 107 |
compose_platform_string, |
| 108 |
normalize_platform_string, |
| 109 |
) |
| 110 |
from ansible_collections.community.docker.plugins.module_utils._util import ( |
| 111 |
DockerBaseClass, |
| 112 |
is_image_name_id, |
| 113 |
is_valid_tag, |
| 114 |
) |
| 115 |
|
| 116 |
|
| 117 |
def image_info(image: dict[str, t.Any] | None) -> dict[str, t.Any]: |
| 118 |
result = {} |
| 119 |
if image: |
| 120 |
result["id"] = image["Id"] |
| 121 |
else: |
| 122 |
result["exists"] = False |
| 123 |
return result |
| 124 |
|
| 125 |
|
| 126 |
class ImagePuller(DockerBaseClass): |
| 127 |
def __init__(self, client: AnsibleDockerClient) -> None: |
| 128 |
super().__init__() |
| 129 |
|
| 130 |
self.client = client |
| 131 |
self.check_mode = self.client.check_mode |
| 132 |
|
| 133 |
parameters = self.client.module.params |
| 134 |
self.name: str = parameters["name"] |
| 135 |
self.tag: str = parameters["tag"] |
| 136 |
self.platform: str | None = parameters["platform"] |
| 137 |
self.pull_mode: t.Literal["always", "not_present"] = parameters["pull"] |
| 138 |
|
| 139 |
if is_image_name_id(self.name): |
| 140 |
self.client.fail("Cannot pull an image by ID") |
| 141 |
if not is_valid_tag(self.tag, allow_empty=True): |
| 142 |
self.client.fail(f'"{self.tag}" is not a valid docker tag!') |
| 143 |
|
| 144 |
# If name contains a tag, it takes precedence over tag parameter. |
| 145 |
repo, repo_tag = parse_repository_tag(self.name) |
| 146 |
if repo_tag: |
| 147 |
self.name = repo |
| 148 |
self.tag = repo_tag |
| 149 |
|
| 150 |
def pull(self) -> dict[str, t.Any]: |
| 151 |
image = self.client.find_image(name=self.name, tag=self.tag) |
| 152 |
actions: list[str] = [] |
| 153 |
diff = {"before": image_info(image), "after": image_info(image)} |
| 154 |
results = { |
| 155 |
"changed": False, |
| 156 |
"actions": actions, |
| 157 |
"image": image or {}, |
| 158 |
"diff": diff, |
| 159 |
} |
| 160 |
|
| 161 |
if image and self.pull_mode == "not_present": |
| 162 |
if self.platform is None: |
| 163 |
return results |
| 164 |
host_info = self.client.info() |
| 165 |
wanted_platform = normalize_platform_string( |
| 166 |
self.platform, |
| 167 |
daemon_os=host_info.get("OSType"), |
| 168 |
daemon_arch=host_info.get("Architecture"), |
| 169 |
) |
| 170 |
image_platform = compose_platform_string( |
| 171 |
os=image.get("Os"), |
| 172 |
arch=image.get("Architecture"), |
| 173 |
variant=image.get("Variant"), |
| 174 |
daemon_os=host_info.get("OSType"), |
| 175 |
daemon_arch=host_info.get("Architecture"), |
| 176 |
) |
| 177 |
if compare_platform_strings(wanted_platform, image_platform): |
| 178 |
return results |
| 179 |
|
| 180 |
actions.append(f"Pulled image {self.name}:{self.tag}") |
| 181 |
if self.check_mode: |
| 182 |
results["changed"] = True |
| 183 |
diff["after"] = image_info({"Id": "unknown"}) |
| 184 |
else: |
| 185 |
image, not_changed = self.client.pull_image( |
| 186 |
self.name, tag=self.tag, image_platform=self.platform |
| 187 |
) |
| 188 |
results["image"] = image |
| 189 |
results["changed"] = not not_changed |
| 190 |
diff["after"] = image_info(image) |
| 191 |
|
| 192 |
return results |
| 193 |
|
| 194 |
|
| 195 |
def main() -> None: |
| 196 |
argument_spec = { |
| 197 |
"name": {"type": "str", "required": True}, |
| 198 |
"tag": {"type": "str", "default": "latest"}, |
| 199 |
"platform": {"type": "str"}, |
| 200 |
"pull": { |
| 201 |
"type": "str", |
| 202 |
"choices": ["always", "not_present"], |
| 203 |
"default": "always", |
| 204 |
}, |
| 205 |
} |
| 206 |
|
| 207 |
option_minimal_versions = { |
| 208 |
"platform": {"docker_api_version": "1.32"}, |
| 209 |
} |
| 210 |
|
| 211 |
client = AnsibleDockerClient( |
| 212 |
argument_spec=argument_spec, |
| 213 |
supports_check_mode=True, |
| 214 |
option_minimal_versions=option_minimal_versions, |
| 215 |
) |
| 216 |
|
| 217 |
try: |
| 218 |
results = ImagePuller(client).pull() |
| 219 |
client.module.exit_json(**results) |
| 220 |
except DockerException as e: |
| 221 |
client.fail( |
| 222 |
f"An unexpected Docker error occurred: {e}", |
| 223 |
exception=traceback.format_exc(), |
| 224 |
) |
| 225 |
except RequestException as e: |
| 226 |
client.fail( |
| 227 |
f"An unexpected requests error occurred when trying to talk to the Docker daemon: {e}", |
| 228 |
exception=traceback.format_exc(), |
| 229 |
) |
| 230 |
|
| 231 |
|
| 232 |
if __name__ == "__main__": |
| 233 |
main() |