| 1 |
# Copyright (c) 2022 Felix Fontein <[email protected]> |
| 2 |
# Copyright 2016 Red Hat | Ansible |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 7 |
# Do not use this from other collections or standalone plugins/modules! |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
import abc |
| 12 |
import os |
| 13 |
import re |
| 14 |
import shlex |
| 15 |
import typing as t |
| 16 |
from functools import partial |
| 17 |
|
| 18 |
from ansible.module_utils.common.text.converters import to_text |
| 19 |
from ansible.module_utils.common.text.formatters import human_to_bytes |
| 20 |
|
| 21 |
from ansible_collections.community.docker.plugins.module_utils._api.utils.utils import ( |
| 22 |
parse_env_file, |
| 23 |
) |
| 24 |
from ansible_collections.community.docker.plugins.module_utils._platform import ( |
| 25 |
compare_platform_strings, |
| 26 |
) |
| 27 |
from ansible_collections.community.docker.plugins.module_utils._util import ( |
| 28 |
clean_dict_booleans_for_docker_api, |
| 29 |
compare_generic, |
| 30 |
normalize_healthcheck, |
| 31 |
omit_none_from_dict, |
| 32 |
sanitize_labels, |
| 33 |
) |
| 34 |
|
| 35 |
if t.TYPE_CHECKING: |
| 36 |
from collections.abc import Callable, Sequence |
| 37 |
|
| 38 |
from ansible.module_utils.basic import AnsibleModule |
| 39 |
|
| 40 |
from ansible_collections.community.docker.plugins.module_utils._version import ( |
| 41 |
LooseVersion, |
| 42 |
) |
| 43 |
|
| 44 |
ValueType = t.Literal["set", "list", "dict", "bool", "int", "float", "str"] |
| 45 |
AnsibleType = t.Literal["list", "dict", "bool", "int", "float", "str"] |
| 46 |
ComparisonMode = t.Literal["ignore", "strict", "allow_more_present"] |
| 47 |
ComparisonType = t.Literal["set", "set(dict)", "list", "dict", "value"] |
| 48 |
|
| 49 |
Client = t.TypeVar("Client") |
| 50 |
|
| 51 |
|
| 52 |
_DEFAULT_IP_REPLACEMENT_STRING = ( |
| 53 |
"[[DEFAULT_IP:iewahhaeB4Sae6Aen8IeShairoh4zeph7xaekoh8Geingunaesaeweiy3ooleiwi]]" |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
_MOUNT_OPTION_TYPES = { |
| 58 |
"create_mountpoint": ("bind",), |
| 59 |
"labels": ("volume",), |
| 60 |
"no_copy": ("volume",), |
| 61 |
"non_recursive": ("bind",), |
| 62 |
"propagation": ("bind",), |
| 63 |
"read_only_force_recursive": ("bind",), |
| 64 |
"read_only_non_recursive": ("bind",), |
| 65 |
"subpath": ("volume", "image"), |
| 66 |
"tmpfs_size": ("tmpfs",), |
| 67 |
"tmpfs_mode": ("tmpfs",), |
| 68 |
"tmpfs_options": ("tmpfs",), |
| 69 |
"volume_driver": ("volume",), |
| 70 |
"volume_options": ("volume",), |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
def _get_ansible_type( |
| 75 |
value_type: ValueType, |
| 76 |
) -> AnsibleType: |
| 77 |
if value_type == "set": |
| 78 |
return "list" |
| 79 |
if value_type not in ("list", "dict", "bool", "int", "float", "str"): |
| 80 |
raise ValueError(f'Invalid type "{value_type}"') |
| 81 |
return value_type |
| 82 |
|
| 83 |
|
| 84 |
class Option: |
| 85 |
def __init__( |
| 86 |
self, |
| 87 |
name: str, |
| 88 |
*, |
| 89 |
value_type: ValueType, |
| 90 |
owner: OptionGroup, |
| 91 |
ansible_type: AnsibleType | None = None, |
| 92 |
elements: ValueType | None = None, |
| 93 |
ansible_elements: AnsibleType | None = None, |
| 94 |
ansible_suboptions: dict[str, t.Any] | None = None, |
| 95 |
ansible_aliases: Sequence[str] | None = None, |
| 96 |
ansible_choices: Sequence[str] | None = None, |
| 97 |
needs_no_suboptions: bool = False, |
| 98 |
default_comparison: ComparisonMode | None = None, |
| 99 |
not_a_container_option: bool = False, |
| 100 |
not_an_ansible_option: bool = False, |
| 101 |
copy_comparison_from: str | None = None, |
| 102 |
compare: Callable[[Option, t.Any, t.Any], bool] | None = None, |
| 103 |
): |
| 104 |
self.name = name |
| 105 |
self.value_type = value_type |
| 106 |
self.ansible_type = ansible_type or _get_ansible_type(value_type) |
| 107 |
needs_elements = self.value_type in ("list", "set") |
| 108 |
needs_ansible_elements = self.ansible_type in ("list",) |
| 109 |
if elements is not None and not needs_elements: |
| 110 |
raise ValueError("elements only allowed for lists/sets") |
| 111 |
if elements is None and needs_elements: |
| 112 |
raise ValueError("elements required for lists/sets") |
| 113 |
if ansible_elements is not None and not needs_ansible_elements: |
| 114 |
raise ValueError("Ansible elements only allowed for Ansible lists") |
| 115 |
if (elements is None and ansible_elements is None) and needs_ansible_elements: |
| 116 |
raise ValueError("Ansible elements required for Ansible lists") |
| 117 |
self.elements = elements if needs_elements else None |
| 118 |
self.ansible_elements: AnsibleType | None = ( |
| 119 |
(ansible_elements or _get_ansible_type(elements or "str")) |
| 120 |
if needs_ansible_elements |
| 121 |
else None |
| 122 |
) |
| 123 |
needs_suboptions = ( |
| 124 |
self.ansible_type == "list" and self.ansible_elements == "dict" |
| 125 |
) or (self.ansible_type == "dict") |
| 126 |
if ansible_suboptions is not None and not needs_suboptions: |
| 127 |
raise ValueError( |
| 128 |
"suboptions only allowed for Ansible lists with dicts, or Ansible dicts" |
| 129 |
) |
| 130 |
if ( |
| 131 |
ansible_suboptions is None |
| 132 |
and needs_suboptions |
| 133 |
and not needs_no_suboptions |
| 134 |
and not not_an_ansible_option |
| 135 |
): |
| 136 |
raise ValueError( |
| 137 |
"suboptions required for Ansible lists with dicts, or Ansible dicts" |
| 138 |
) |
| 139 |
self.ansible_suboptions = ansible_suboptions if needs_suboptions else None |
| 140 |
self.ansible_aliases = ansible_aliases or [] |
| 141 |
self.ansible_choices = ansible_choices |
| 142 |
comparison_type: ComparisonType |
| 143 |
if self.value_type == "set" and self.elements == "dict": |
| 144 |
comparison_type = "set(dict)" |
| 145 |
elif self.value_type in ("set", "list", "dict"): |
| 146 |
comparison_type = self.value_type # type: ignore |
| 147 |
else: |
| 148 |
comparison_type = "value" |
| 149 |
self.comparison_type = comparison_type |
| 150 |
if default_comparison is not None: |
| 151 |
self.comparison = default_comparison |
| 152 |
elif comparison_type in ("list", "value"): |
| 153 |
self.comparison = "strict" |
| 154 |
else: |
| 155 |
self.comparison = "allow_more_present" |
| 156 |
self.not_a_container_option = not_a_container_option |
| 157 |
self.not_an_ansible_option = not_an_ansible_option |
| 158 |
self.copy_comparison_from = copy_comparison_from |
| 159 |
self.compare = ( |
| 160 |
( |
| 161 |
lambda param_value, container_value: compare( |
| 162 |
self, param_value, container_value |
| 163 |
) |
| 164 |
) |
| 165 |
if compare |
| 166 |
else ( |
| 167 |
lambda param_value, container_value: compare_generic( |
| 168 |
param_value, container_value, self.comparison, self.comparison_type |
| 169 |
) |
| 170 |
) |
| 171 |
) |
| 172 |
|
| 173 |
|
| 174 |
class OptionGroup: |
| 175 |
def __init__( |
| 176 |
self, |
| 177 |
*, |
| 178 |
preprocess: ( |
| 179 |
Callable[[AnsibleModule, dict[str, t.Any]], dict[str, t.Any]] | None |
| 180 |
) = None, |
| 181 |
ansible_mutually_exclusive: Sequence[Sequence[str]] | None = None, |
| 182 |
ansible_required_together: Sequence[Sequence[str]] | None = None, |
| 183 |
ansible_required_one_of: Sequence[Sequence[str]] | None = None, |
| 184 |
ansible_required_if: ( |
| 185 |
Sequence[ |
| 186 |
tuple[str, t.Any, Sequence[str]] |
| 187 |
| tuple[str, t.Any, Sequence[str], bool] |
| 188 |
] |
| 189 |
| None |
| 190 |
) = None, |
| 191 |
ansible_required_by: dict[str, Sequence[str]] | None = None, |
| 192 |
) -> None: |
| 193 |
if preprocess is None: |
| 194 |
|
| 195 |
def preprocess( |
| 196 |
module: AnsibleModule, values: dict[str, t.Any] |
| 197 |
) -> dict[str, t.Any]: |
| 198 |
return values |
| 199 |
|
| 200 |
self.preprocess = preprocess |
| 201 |
self.options: list[Option] = [] |
| 202 |
self.all_options: list[Option] = [] |
| 203 |
self.engines: dict[str, Engine] = {} |
| 204 |
self.ansible_mutually_exclusive = ansible_mutually_exclusive or [] |
| 205 |
self.ansible_required_together = ansible_required_together or [] |
| 206 |
self.ansible_required_one_of = ansible_required_one_of or [] |
| 207 |
self.ansible_required_if = ansible_required_if or [] |
| 208 |
self.ansible_required_by = ansible_required_by or {} |
| 209 |
self.argument_spec: dict[str, t.Any] = {} |
| 210 |
|
| 211 |
def add_option(self, name: str, **kwargs: t.Any) -> OptionGroup: |
| 212 |
option = Option(name, owner=self, **kwargs) |
| 213 |
if not option.not_a_container_option: |
| 214 |
self.options.append(option) |
| 215 |
self.all_options.append(option) |
| 216 |
if not option.not_an_ansible_option: |
| 217 |
ansible_option: dict[str, t.Any] = { |
| 218 |
"type": option.ansible_type, |
| 219 |
} |
| 220 |
if option.ansible_elements is not None: |
| 221 |
ansible_option["elements"] = option.ansible_elements |
| 222 |
if option.ansible_suboptions is not None: |
| 223 |
ansible_option["options"] = option.ansible_suboptions |
| 224 |
if option.ansible_aliases: |
| 225 |
ansible_option["aliases"] = option.ansible_aliases |
| 226 |
if option.ansible_choices is not None: |
| 227 |
ansible_option["choices"] = option.ansible_choices |
| 228 |
self.argument_spec[option.name] = ansible_option |
| 229 |
return self |
| 230 |
|
| 231 |
def supports_engine(self, engine_name: str) -> bool: |
| 232 |
return engine_name in self.engines |
| 233 |
|
| 234 |
def get_engine(self, engine_name: str) -> Engine: |
| 235 |
return self.engines[engine_name] |
| 236 |
|
| 237 |
def add_engine(self, engine_name: str, engine: Engine) -> OptionGroup: |
| 238 |
self.engines[engine_name] = engine |
| 239 |
return self |
| 240 |
|
| 241 |
|
| 242 |
class Engine(t.Generic[Client]): |
| 243 |
min_api_version: str | None = None |
| 244 |
min_api_version_obj: LooseVersion | None = None |
| 245 |
extra_option_minimal_versions: dict[str, dict[str, t.Any]] | None = None |
| 246 |
|
| 247 |
@abc.abstractmethod |
| 248 |
def get_value( |
| 249 |
self, |
| 250 |
module: AnsibleModule, |
| 251 |
container: dict[str, t.Any], |
| 252 |
api_version: LooseVersion, |
| 253 |
options: list[Option], |
| 254 |
image: dict[str, t.Any] | None, |
| 255 |
host_info: dict[str, t.Any] | None, |
| 256 |
) -> dict[str, t.Any]: |
| 257 |
pass |
| 258 |
|
| 259 |
def compare_value( |
| 260 |
self, option: Option, param_value: t.Any, container_value: t.Any |
| 261 |
) -> bool: |
| 262 |
return option.compare(param_value, container_value) |
| 263 |
|
| 264 |
@abc.abstractmethod |
| 265 |
def set_value( |
| 266 |
self, |
| 267 |
module: AnsibleModule, |
| 268 |
data: dict[str, t.Any], |
| 269 |
api_version: LooseVersion, |
| 270 |
options: list[Option], |
| 271 |
values: dict[str, t.Any], |
| 272 |
) -> None: |
| 273 |
pass |
| 274 |
|
| 275 |
@abc.abstractmethod |
| 276 |
def get_expected_values( |
| 277 |
self, |
| 278 |
module: AnsibleModule, |
| 279 |
client: Client, |
| 280 |
api_version: LooseVersion, |
| 281 |
options: list[Option], |
| 282 |
image: dict[str, t.Any] | None, |
| 283 |
values: dict[str, t.Any], |
| 284 |
host_info: dict[str, t.Any] | None, |
| 285 |
) -> dict[str, t.Any]: |
| 286 |
pass |
| 287 |
|
| 288 |
@abc.abstractmethod |
| 289 |
def ignore_mismatching_result( |
| 290 |
self, |
| 291 |
module: AnsibleModule, |
| 292 |
client: Client, |
| 293 |
api_version: LooseVersion, |
| 294 |
option: Option, |
| 295 |
image: dict[str, t.Any] | None, |
| 296 |
container_value: t.Any, |
| 297 |
expected_value: t.Any, |
| 298 |
host_info: dict[str, t.Any] | None, |
| 299 |
) -> bool: |
| 300 |
pass |
| 301 |
|
| 302 |
@abc.abstractmethod |
| 303 |
def preprocess_value( |
| 304 |
self, |
| 305 |
module: AnsibleModule, |
| 306 |
client: Client, |
| 307 |
api_version: LooseVersion, |
| 308 |
options: list[Option], |
| 309 |
values: dict[str, t.Any], |
| 310 |
) -> dict[str, t.Any]: |
| 311 |
pass |
| 312 |
|
| 313 |
@abc.abstractmethod |
| 314 |
def update_value( |
| 315 |
self, |
| 316 |
module: AnsibleModule, |
| 317 |
data: dict[str, t.Any], |
| 318 |
api_version: LooseVersion, |
| 319 |
options: list[Option], |
| 320 |
values: dict[str, t.Any], |
| 321 |
) -> None: |
| 322 |
pass |
| 323 |
|
| 324 |
@abc.abstractmethod |
| 325 |
def can_set_value(self, api_version: LooseVersion) -> bool: |
| 326 |
pass |
| 327 |
|
| 328 |
@abc.abstractmethod |
| 329 |
def can_update_value(self, api_version: LooseVersion) -> bool: |
| 330 |
pass |
| 331 |
|
| 332 |
@abc.abstractmethod |
| 333 |
def needs_container_image(self, values: dict[str, t.Any]) -> bool: |
| 334 |
pass |
| 335 |
|
| 336 |
@abc.abstractmethod |
| 337 |
def needs_host_info(self, values: dict[str, t.Any]) -> bool: |
| 338 |
pass |
| 339 |
|
| 340 |
|
| 341 |
class EngineDriver(t.Generic[Client]): |
| 342 |
name: str |
| 343 |
|
| 344 |
@abc.abstractmethod |
| 345 |
def setup( |
| 346 |
self, |
| 347 |
argument_spec: dict[str, t.Any], |
| 348 |
mutually_exclusive: Sequence[Sequence[str]] | None = None, |
| 349 |
required_together: Sequence[Sequence[str]] | None = None, |
| 350 |
required_one_of: Sequence[Sequence[str]] | None = None, |
| 351 |
required_if: ( |
| 352 |
Sequence[ |
| 353 |
tuple[str, t.Any, Sequence[str]] |
| 354 |
| tuple[str, t.Any, Sequence[str], bool] |
| 355 |
] |
| 356 |
| None |
| 357 |
) = None, |
| 358 |
required_by: dict[str, Sequence[str]] | None = None, |
| 359 |
) -> tuple[AnsibleModule, list[OptionGroup], Client]: |
| 360 |
pass |
| 361 |
|
| 362 |
@abc.abstractmethod |
| 363 |
def get_host_info(self, client: Client) -> dict[str, t.Any]: |
| 364 |
pass |
| 365 |
|
| 366 |
@abc.abstractmethod |
| 367 |
def get_api_version(self, client: Client) -> LooseVersion: |
| 368 |
pass |
| 369 |
|
| 370 |
@abc.abstractmethod |
| 371 |
def get_container_id(self, container: dict[str, t.Any]) -> str: |
| 372 |
pass |
| 373 |
|
| 374 |
@abc.abstractmethod |
| 375 |
def get_image_from_container(self, container: dict[str, t.Any]) -> str: |
| 376 |
pass |
| 377 |
|
| 378 |
@abc.abstractmethod |
| 379 |
def get_image_name_from_container(self, container: dict[str, t.Any]) -> str | None: |
| 380 |
pass |
| 381 |
|
| 382 |
@abc.abstractmethod |
| 383 |
def is_container_removing(self, container: dict[str, t.Any]) -> bool: |
| 384 |
pass |
| 385 |
|
| 386 |
@abc.abstractmethod |
| 387 |
def is_container_running(self, container: dict[str, t.Any]) -> bool: |
| 388 |
pass |
| 389 |
|
| 390 |
@abc.abstractmethod |
| 391 |
def is_container_paused(self, container: dict[str, t.Any]) -> bool: |
| 392 |
pass |
| 393 |
|
| 394 |
@abc.abstractmethod |
| 395 |
def inspect_container_by_name( |
| 396 |
self, client: Client, container_name: str |
| 397 |
) -> dict[str, t.Any] | None: |
| 398 |
pass |
| 399 |
|
| 400 |
@abc.abstractmethod |
| 401 |
def inspect_container_by_id( |
| 402 |
self, client: Client, container_id: str |
| 403 |
) -> dict[str, t.Any] | None: |
| 404 |
pass |
| 405 |
|
| 406 |
@abc.abstractmethod |
| 407 |
def inspect_image_by_id( |
| 408 |
self, client: Client, image_id: str |
| 409 |
) -> dict[str, t.Any] | None: |
| 410 |
pass |
| 411 |
|
| 412 |
@abc.abstractmethod |
| 413 |
def inspect_image_by_name( |
| 414 |
self, client: Client, repository: str, tag: str |
| 415 |
) -> dict[str, t.Any] | None: |
| 416 |
pass |
| 417 |
|
| 418 |
@abc.abstractmethod |
| 419 |
def pull_image( |
| 420 |
self, |
| 421 |
client: Client, |
| 422 |
repository: str, |
| 423 |
tag: str, |
| 424 |
image_platform: str | None = None, |
| 425 |
) -> tuple[dict[str, t.Any] | None, bool]: |
| 426 |
pass |
| 427 |
|
| 428 |
@abc.abstractmethod |
| 429 |
def pause_container(self, client: Client, container_id: str) -> None: |
| 430 |
pass |
| 431 |
|
| 432 |
@abc.abstractmethod |
| 433 |
def unpause_container(self, client: Client, container_id: str) -> None: |
| 434 |
pass |
| 435 |
|
| 436 |
@abc.abstractmethod |
| 437 |
def disconnect_container_from_network( |
| 438 |
self, client: Client, container_id: str, network_id: str |
| 439 |
) -> None: |
| 440 |
pass |
| 441 |
|
| 442 |
@abc.abstractmethod |
| 443 |
def connect_container_to_network( |
| 444 |
self, |
| 445 |
client: Client, |
| 446 |
container_id: str, |
| 447 |
network_id: str, |
| 448 |
parameters: dict[str, t.Any] | None = None, |
| 449 |
) -> None: |
| 450 |
pass |
| 451 |
|
| 452 |
def create_container_supports_more_than_one_network(self, client: Client) -> bool: |
| 453 |
return False |
| 454 |
|
| 455 |
@abc.abstractmethod |
| 456 |
def create_container( |
| 457 |
self, |
| 458 |
client: Client, |
| 459 |
container_name: str, |
| 460 |
create_parameters: dict[str, t.Any], |
| 461 |
networks: dict[str, dict[str, t.Any]] | None = None, |
| 462 |
) -> str: |
| 463 |
pass |
| 464 |
|
| 465 |
@abc.abstractmethod |
| 466 |
def start_container(self, client: Client, container_id: str) -> None: |
| 467 |
pass |
| 468 |
|
| 469 |
@abc.abstractmethod |
| 470 |
def wait_for_container( |
| 471 |
self, client: Client, container_id: str, timeout: int | float | None = None |
| 472 |
) -> int | None: |
| 473 |
pass |
| 474 |
|
| 475 |
@abc.abstractmethod |
| 476 |
def get_container_output( |
| 477 |
self, client: Client, container_id: str |
| 478 |
) -> tuple[bytes, t.Literal[True]] | tuple[str, t.Literal[False]]: |
| 479 |
pass |
| 480 |
|
| 481 |
@abc.abstractmethod |
| 482 |
def update_container( |
| 483 |
self, client: Client, container_id: str, update_parameters: dict[str, t.Any] |
| 484 |
) -> None: |
| 485 |
pass |
| 486 |
|
| 487 |
@abc.abstractmethod |
| 488 |
def restart_container( |
| 489 |
self, client: Client, container_id: str, timeout: int | float | None = None |
| 490 |
) -> None: |
| 491 |
pass |
| 492 |
|
| 493 |
@abc.abstractmethod |
| 494 |
def kill_container( |
| 495 |
self, client: Client, container_id: str, kill_signal: str | None = None |
| 496 |
) -> None: |
| 497 |
pass |
| 498 |
|
| 499 |
@abc.abstractmethod |
| 500 |
def stop_container( |
| 501 |
self, client: Client, container_id: str, timeout: int | float | None = None |
| 502 |
) -> None: |
| 503 |
pass |
| 504 |
|
| 505 |
@abc.abstractmethod |
| 506 |
def remove_container( |
| 507 |
self, |
| 508 |
client: Client, |
| 509 |
container_id: str, |
| 510 |
remove_volumes: bool = False, |
| 511 |
link: bool = False, |
| 512 |
force: bool = False, |
| 513 |
) -> None: |
| 514 |
pass |
| 515 |
|
| 516 |
@abc.abstractmethod |
| 517 |
def run(self, runner: Callable[[], None], client: Client) -> None: |
| 518 |
pass |
| 519 |
|
| 520 |
|
| 521 |
def _is_volume_permissions(mode: str) -> bool: |
| 522 |
for part in mode.split(","): |
| 523 |
if part not in ( |
| 524 |
"rw", |
| 525 |
"ro", |
| 526 |
"z", |
| 527 |
"Z", |
| 528 |
"consistent", |
| 529 |
"delegated", |
| 530 |
"cached", |
| 531 |
"rprivate", |
| 532 |
"private", |
| 533 |
"rshared", |
| 534 |
"shared", |
| 535 |
"rslave", |
| 536 |
"slave", |
| 537 |
"nocopy", |
| 538 |
): |
| 539 |
return False |
| 540 |
return True |
| 541 |
|
| 542 |
|
| 543 |
def _parse_port_range(range_or_port: str, module: AnsibleModule) -> list[int]: |
| 544 |
""" |
| 545 |
Parses a string containing either a single port or a range of ports. |
| 546 |
|
| 547 |
Returns a list of integers for each port in the list. |
| 548 |
""" |
| 549 |
if "-" in range_or_port: |
| 550 |
try: |
| 551 |
start, end = [int(port) for port in range_or_port.split("-")] |
| 552 |
except ValueError: |
| 553 |
module.fail_json(msg=f'Invalid port range: "{range_or_port}"') |
| 554 |
if end < start: |
| 555 |
module.fail_json(msg=f'Invalid port range: "{range_or_port}"') |
| 556 |
return list(range(start, end + 1)) |
| 557 |
try: |
| 558 |
return [int(range_or_port)] |
| 559 |
except ValueError: |
| 560 |
module.fail_json(msg=f'Invalid port: "{range_or_port}"') |
| 561 |
|
| 562 |
|
| 563 |
def _split_colon_ipv6(text: str, module: AnsibleModule) -> list[str]: |
| 564 |
""" |
| 565 |
Split string by ':', while keeping IPv6 addresses in square brackets in one component. |
| 566 |
""" |
| 567 |
if "[" not in text: |
| 568 |
return text.split(":") |
| 569 |
start = 0 |
| 570 |
result = [] |
| 571 |
while start < len(text): |
| 572 |
i = text.find("[", start) |
| 573 |
if i < 0: |
| 574 |
result.extend(text[start:].split(":")) |
| 575 |
break |
| 576 |
j = text.find("]", i) |
| 577 |
if j < 0: |
| 578 |
module.fail_json( |
| 579 |
msg=f'Cannot find closing "]" in input "{text}" for opening "[" at index {i + 1}!' |
| 580 |
) |
| 581 |
result.extend(text[start:i].split(":")) |
| 582 |
k = text.find(":", j) |
| 583 |
if k < 0: |
| 584 |
result[-1] += text[i:] |
| 585 |
start = len(text) |
| 586 |
else: |
| 587 |
result[-1] += text[i:k] |
| 588 |
if k == len(text): |
| 589 |
result.append("") |
| 590 |
break |
| 591 |
start = k + 1 |
| 592 |
return result |
| 593 |
|
| 594 |
|
| 595 |
def _preprocess_command( |
| 596 |
module: AnsibleModule, values: dict[str, t.Any] |
| 597 |
) -> dict[str, t.Any]: |
| 598 |
if "command" not in values: |
| 599 |
return values |
| 600 |
value = values["command"] |
| 601 |
if module.params["command_handling"] == "correct": |
| 602 |
if value is not None: |
| 603 |
if not isinstance(value, list): |
| 604 |
# convert from str to list |
| 605 |
value = shlex.split(to_text(value, errors="surrogate_or_strict")) |
| 606 |
value = [to_text(x, errors="surrogate_or_strict") for x in value] |
| 607 |
elif value: |
| 608 |
# convert from list to str |
| 609 |
if isinstance(value, list): |
| 610 |
value = shlex.split( |
| 611 |
" ".join([to_text(x, errors="surrogate_or_strict") for x in value]) |
| 612 |
) |
| 613 |
value = [to_text(x, errors="surrogate_or_strict") for x in value] |
| 614 |
else: |
| 615 |
value = shlex.split(to_text(value, errors="surrogate_or_strict")) |
| 616 |
value = [to_text(x, errors="surrogate_or_strict") for x in value] |
| 617 |
else: |
| 618 |
return {} |
| 619 |
return { |
| 620 |
"command": value, |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
def _preprocess_entrypoint( |
| 625 |
module: AnsibleModule, values: dict[str, t.Any] |
| 626 |
) -> dict[str, t.Any]: |
| 627 |
if "entrypoint" not in values: |
| 628 |
return values |
| 629 |
value = values["entrypoint"] |
| 630 |
if module.params["command_handling"] == "correct": |
| 631 |
if value is not None: |
| 632 |
value = [to_text(x, errors="surrogate_or_strict") for x in value] |
| 633 |
elif value: |
| 634 |
# convert from list to str. |
| 635 |
value = shlex.split( |
| 636 |
" ".join([to_text(x, errors="surrogate_or_strict") for x in value]) |
| 637 |
) |
| 638 |
value = [to_text(x, errors="surrogate_or_strict") for x in value] |
| 639 |
else: |
| 640 |
return {} |
| 641 |
return { |
| 642 |
"entrypoint": value, |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
def _preprocess_env( |
| 647 |
module: AnsibleModule, values: dict[str, t.Any] |
| 648 |
) -> dict[str, t.Any]: |
| 649 |
if not values: |
| 650 |
return {} |
| 651 |
final_env = {} |
| 652 |
if "env_file" in values: |
| 653 |
parsed_env_file = parse_env_file(values["env_file"]) |
| 654 |
for name, value in parsed_env_file.items(): |
| 655 |
final_env[name] = to_text(value, errors="surrogate_or_strict") |
| 656 |
if "env" in values: |
| 657 |
for name, value in values["env"].items(): |
| 658 |
if not isinstance(value, str): |
| 659 |
module.fail_json( |
| 660 |
msg="Non-string value found for env option. Ambiguous env options must be " |
| 661 |
"wrapped in quotes to avoid them being interpreted when directly specified " |
| 662 |
"in YAML, or explicitly converted to strings when the option is templated. " |
| 663 |
f"Key: {name}" |
| 664 |
) |
| 665 |
final_env[name] = to_text(value, errors="surrogate_or_strict") |
| 666 |
formatted_env = [] |
| 667 |
for key, value in final_env.items(): |
| 668 |
formatted_env.append(f"{key}={value}") |
| 669 |
return { |
| 670 |
"env": formatted_env, |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
def _preprocess_healthcheck( |
| 675 |
module: AnsibleModule, values: dict[str, t.Any] |
| 676 |
) -> dict[str, t.Any]: |
| 677 |
if not values: |
| 678 |
return {} |
| 679 |
return { |
| 680 |
"healthcheck": normalize_healthcheck( |
| 681 |
values["healthcheck"], normalize_test=False |
| 682 |
), |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
def _preprocess_convert_to_bytes( |
| 687 |
module: AnsibleModule, |
| 688 |
values: dict[str, t.Any], |
| 689 |
name: str, |
| 690 |
unlimited_value: int | None = None, |
| 691 |
) -> dict[str, t.Any]: |
| 692 |
if name not in values: |
| 693 |
return values |
| 694 |
try: |
| 695 |
value = values[name] |
| 696 |
if unlimited_value is not None and value in ("unlimited", str(unlimited_value)): |
| 697 |
value = unlimited_value |
| 698 |
else: |
| 699 |
value = human_to_bytes(value) |
| 700 |
values[name] = value |
| 701 |
return values |
| 702 |
except ValueError as exc: |
| 703 |
module.fail_json(msg=f"Failed to convert {name} to bytes: {exc}") |
| 704 |
|
| 705 |
|
| 706 |
def _preprocess_mac_address( |
| 707 |
module: AnsibleModule, values: dict[str, t.Any] |
| 708 |
) -> dict[str, t.Any]: |
| 709 |
if "mac_address" not in values: |
| 710 |
return values |
| 711 |
return { |
| 712 |
"mac_address": values["mac_address"].replace("-", ":"), |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
def _preprocess_networks( |
| 717 |
module: AnsibleModule, values: dict[str, t.Any] |
| 718 |
) -> dict[str, t.Any]: |
| 719 |
if ( |
| 720 |
module.params["networks_cli_compatible"] is True |
| 721 |
and values.get("networks") |
| 722 |
and "network_mode" not in values |
| 723 |
): |
| 724 |
# Same behavior as Docker CLI: if networks are specified, use the name of the first network as the value for network_mode |
| 725 |
# (assuming no explicit value is specified for network_mode) |
| 726 |
values["network_mode"] = values["networks"][0]["name"] |
| 727 |
|
| 728 |
if "networks" in values: |
| 729 |
for network in values["networks"]: |
| 730 |
if network["links"]: |
| 731 |
parsed_links = [] |
| 732 |
for link in network["links"]: |
| 733 |
parsed_link = link.split(":", 1) |
| 734 |
if len(parsed_link) == 1: |
| 735 |
parsed_link = (link, link) |
| 736 |
parsed_links.append(tuple(parsed_link)) |
| 737 |
network["links"] = parsed_links |
| 738 |
if network["mac_address"]: |
| 739 |
network["mac_address"] = network["mac_address"].replace("-", ":") |
| 740 |
|
| 741 |
return values |
| 742 |
|
| 743 |
|
| 744 |
def _preprocess_sysctls( |
| 745 |
module: AnsibleModule, values: dict[str, t.Any] |
| 746 |
) -> dict[str, t.Any]: |
| 747 |
if "sysctls" in values: |
| 748 |
for key, value in values["sysctls"].items(): |
| 749 |
values["sysctls"][key] = to_text(value, errors="surrogate_or_strict") |
| 750 |
return values |
| 751 |
|
| 752 |
|
| 753 |
def _preprocess_tmpfs( |
| 754 |
module: AnsibleModule, values: dict[str, t.Any] |
| 755 |
) -> dict[str, t.Any]: |
| 756 |
if "tmpfs" not in values: |
| 757 |
return values |
| 758 |
result = {} |
| 759 |
for tmpfs_spec in values["tmpfs"]: |
| 760 |
split_spec = tmpfs_spec.split(":", 1) |
| 761 |
if len(split_spec) > 1: |
| 762 |
result[split_spec[0]] = split_spec[1] |
| 763 |
else: |
| 764 |
result[split_spec[0]] = "" |
| 765 |
return {"tmpfs": result} |
| 766 |
|
| 767 |
|
| 768 |
def _preprocess_ulimits( |
| 769 |
module: AnsibleModule, values: dict[str, t.Any] |
| 770 |
) -> dict[str, t.Any]: |
| 771 |
if "ulimits" not in values: |
| 772 |
return values |
| 773 |
result = [] |
| 774 |
for limit in values["ulimits"]: |
| 775 |
limits = {} |
| 776 |
pieces = limit.split(":") |
| 777 |
if len(pieces) >= 2: |
| 778 |
limits["Name"] = pieces[0] |
| 779 |
limits["Soft"] = int(pieces[1]) |
| 780 |
limits["Hard"] = int(pieces[1]) |
| 781 |
if len(pieces) == 3: |
| 782 |
limits["Hard"] = int(pieces[2]) |
| 783 |
result.append(limits) |
| 784 |
return { |
| 785 |
"ulimits": result, |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
def _preprocess_mounts( |
| 790 |
module: AnsibleModule, values: dict[str, t.Any] |
| 791 |
) -> dict[str, t.Any]: |
| 792 |
last: dict[str, str] = {} |
| 793 |
|
| 794 |
def check_collision(target: str, name: str) -> None: |
| 795 |
if target in last: |
| 796 |
if name == last[target]: |
| 797 |
module.fail_json( |
| 798 |
msg=f'The mount point "{target}" appears twice in the {name} option' |
| 799 |
) |
| 800 |
else: |
| 801 |
module.fail_json( |
| 802 |
msg=f'The mount point "{target}" appears both in the {name} and {last[target]} option' |
| 803 |
) |
| 804 |
last[target] = name |
| 805 |
|
| 806 |
if "mounts" in values: |
| 807 |
mounts = [] |
| 808 |
for mount in values["mounts"]: |
| 809 |
target = mount["target"] |
| 810 |
mount_type = mount["type"] |
| 811 |
|
| 812 |
check_collision(target, "mounts") |
| 813 |
|
| 814 |
mount_dict = dict(mount) |
| 815 |
|
| 816 |
# Sanity checks |
| 817 |
if mount["source"] is None and mount_type not in ( |
| 818 |
"tmpfs", |
| 819 |
"volume", |
| 820 |
"image", |
| 821 |
"cluster", |
| 822 |
): |
| 823 |
module.fail_json( |
| 824 |
msg=f'source must be specified for mount "{target}" of type "{mount_type}"' |
| 825 |
) |
| 826 |
for option, req_mount_types in _MOUNT_OPTION_TYPES.items(): |
| 827 |
if mount[option] is not None and mount_type not in req_mount_types: |
| 828 |
type_plural = "" if len(req_mount_types) == 1 else "s" |
| 829 |
type_list = '", "'.join(req_mount_types) |
| 830 |
module.fail_json( |
| 831 |
msg=f'{option} cannot be specified for mount "{target}" of type "{mount_type}" (needs type{type_plural} "{type_list}")' |
| 832 |
) |
| 833 |
|
| 834 |
# Streamline options |
| 835 |
volume_options = mount_dict.pop("volume_options") |
| 836 |
if mount_dict["volume_driver"] and volume_options: |
| 837 |
mount_dict["volume_options"] = clean_dict_booleans_for_docker_api( |
| 838 |
volume_options |
| 839 |
) |
| 840 |
if mount_dict["labels"]: |
| 841 |
mount_dict["labels"] = clean_dict_booleans_for_docker_api( |
| 842 |
mount_dict["labels"] |
| 843 |
) |
| 844 |
if mount_dict["tmpfs_size"] is not None: |
| 845 |
try: |
| 846 |
mount_dict["tmpfs_size"] = human_to_bytes(mount_dict["tmpfs_size"]) |
| 847 |
except ValueError as exc: |
| 848 |
module.fail_json( |
| 849 |
msg=f'Failed to convert tmpfs_size of mount "{target}" to bytes: {exc}' |
| 850 |
) |
| 851 |
if mount_dict["tmpfs_mode"] is not None: |
| 852 |
try: |
| 853 |
mount_dict["tmpfs_mode"] = int(mount_dict["tmpfs_mode"], 8) |
| 854 |
except ValueError: |
| 855 |
module.fail_json( |
| 856 |
msg=f'tmp_fs mode of mount "{target}" is not an octal string!' |
| 857 |
) |
| 858 |
if mount_dict["tmpfs_options"]: |
| 859 |
opts = [] |
| 860 |
for idx, opt in enumerate(mount_dict["tmpfs_options"]): |
| 861 |
if len(opt) != 1: |
| 862 |
module.fail_json( |
| 863 |
msg=f'tmpfs_options[{idx + 1}] of mount "{target}" must be a one-element dictionary!' |
| 864 |
) |
| 865 |
k, v = list(opt.items())[0] |
| 866 |
if not isinstance(k, str): |
| 867 |
module.fail_json( |
| 868 |
msg=f'key {k!r} in tmpfs_options[{idx + 1}] of mount "{target}" must be a string!' |
| 869 |
) |
| 870 |
if v is not None and not isinstance(v, str): |
| 871 |
module.fail_json( |
| 872 |
msg=f'value {v!r} in tmpfs_options[{idx + 1}] of mount "{target}" must be a string or null/none!' |
| 873 |
) |
| 874 |
opts.append([k, v] if v is not None else [k]) |
| 875 |
mount_dict["tmpfs_options"] = opts |
| 876 |
|
| 877 |
# Add result to list |
| 878 |
mounts.append(omit_none_from_dict(mount_dict)) |
| 879 |
values["mounts"] = mounts |
| 880 |
if "volumes" in values: |
| 881 |
new_vols = [] |
| 882 |
for vol in values["volumes"]: |
| 883 |
parts = vol.split(":") |
| 884 |
if ":" in vol: |
| 885 |
if len(parts) == 3: |
| 886 |
host, container, mode = parts |
| 887 |
if not _is_volume_permissions(mode): |
| 888 |
module.fail_json(msg=f"Found invalid volumes mode: {mode}") |
| 889 |
if re.match(r"[.~]", host): |
| 890 |
host = os.path.abspath(os.path.expanduser(host)) |
| 891 |
check_collision(container, "volumes") |
| 892 |
new_vols.append(f"{host}:{container}:{mode}") |
| 893 |
continue |
| 894 |
if ( |
| 895 |
len(parts) == 2 |
| 896 |
and not _is_volume_permissions(parts[1]) |
| 897 |
and re.match(r"[.~]", parts[0]) |
| 898 |
): |
| 899 |
host = os.path.abspath(os.path.expanduser(parts[0])) |
| 900 |
check_collision(parts[1], "volumes") |
| 901 |
new_vols.append(f"{host}:{parts[1]}:rw") |
| 902 |
continue |
| 903 |
check_collision(parts[min(1, len(parts) - 1)], "volumes") |
| 904 |
new_vols.append(vol) |
| 905 |
values["volumes"] = new_vols |
| 906 |
new_binds = [] |
| 907 |
for vol in new_vols: |
| 908 |
host = None |
| 909 |
if ":" in vol: |
| 910 |
parts = vol.split(":") |
| 911 |
if len(parts) == 3: |
| 912 |
host, container, mode = parts |
| 913 |
if not _is_volume_permissions(mode): |
| 914 |
module.fail_json(msg=f"Found invalid volumes mode: {mode}") |
| 915 |
elif len(parts) == 2: |
| 916 |
if not _is_volume_permissions(parts[1]): |
| 917 |
host, container, mode = parts + ["rw"] |
| 918 |
if host is not None: |
| 919 |
new_binds.append(f"{host}:{container}:{mode}") |
| 920 |
values["volume_binds"] = new_binds |
| 921 |
return values |
| 922 |
|
| 923 |
|
| 924 |
def _preprocess_labels( |
| 925 |
module: AnsibleModule, values: dict[str, t.Any] |
| 926 |
) -> dict[str, t.Any]: |
| 927 |
result = {} |
| 928 |
if "labels" in values: |
| 929 |
labels = values["labels"] |
| 930 |
if labels is not None: |
| 931 |
labels = dict(labels) |
| 932 |
sanitize_labels(labels, "labels", module=module) |
| 933 |
result["labels"] = labels |
| 934 |
return result |
| 935 |
|
| 936 |
|
| 937 |
def _preprocess_log( |
| 938 |
module: AnsibleModule, values: dict[str, t.Any] |
| 939 |
) -> dict[str, t.Any]: |
| 940 |
result: dict[str, t.Any] = {} |
| 941 |
if "log_driver" not in values: |
| 942 |
return result |
| 943 |
result["log_driver"] = values["log_driver"] |
| 944 |
if "log_options" in values: |
| 945 |
options: dict[str, str] = {} |
| 946 |
for k, v in values["log_options"].items(): |
| 947 |
if not isinstance(v, str): |
| 948 |
value = to_text(v, errors="surrogate_or_strict") |
| 949 |
module.warn( |
| 950 |
f"Non-string value found for log_options option '{k}'. The value is automatically converted to {value!r}. " |
| 951 |
"If this is not correct, or you want to avoid such warnings, please quote the value," |
| 952 |
" or explicitly convert the values to strings when templating them." |
| 953 |
) |
| 954 |
v = value |
| 955 |
options[k] = v |
| 956 |
result["log_options"] = options |
| 957 |
return result |
| 958 |
|
| 959 |
|
| 960 |
def _preprocess_ports( |
| 961 |
module: AnsibleModule, values: dict[str, t.Any] |
| 962 |
) -> dict[str, t.Any]: |
| 963 |
if "published_ports" in values: |
| 964 |
if "all" in values["published_ports"]: |
| 965 |
module.fail_json( |
| 966 |
msg='Specifying "all" in published_ports is no longer allowed. Set publish_all_ports to "true" instead ' |
| 967 |
"to randomly assign port mappings for those not specified by published_ports." |
| 968 |
) |
| 969 |
|
| 970 |
binds: dict[ |
| 971 |
str | int, |
| 972 |
tuple[str] |
| 973 |
| tuple[str, str | int] |
| 974 |
| list[tuple[str] | tuple[str, str | int]], |
| 975 |
] = {} |
| 976 |
for port in values["published_ports"]: |
| 977 |
parts = _split_colon_ipv6( |
| 978 |
to_text(port, errors="surrogate_or_strict"), module |
| 979 |
) |
| 980 |
container_port = parts[-1] |
| 981 |
protocol = "" |
| 982 |
if "/" in container_port: |
| 983 |
container_port, protocol = parts[-1].split("/") |
| 984 |
container_ports = _parse_port_range(container_port, module) |
| 985 |
|
| 986 |
p_len = len(parts) |
| 987 |
port_binds: Sequence[tuple[str] | tuple[str, str | int]] |
| 988 |
if p_len == 1: |
| 989 |
port_binds = len(container_ports) * [(_DEFAULT_IP_REPLACEMENT_STRING,)] |
| 990 |
elif p_len == 2: |
| 991 |
if len(container_ports) == 1: |
| 992 |
port_binds = [(_DEFAULT_IP_REPLACEMENT_STRING, parts[0])] |
| 993 |
else: |
| 994 |
port_binds = [ |
| 995 |
(_DEFAULT_IP_REPLACEMENT_STRING, port) |
| 996 |
for port in _parse_port_range(parts[0], module) |
| 997 |
] |
| 998 |
elif p_len == 3: |
| 999 |
# We only allow IPv4 and IPv6 addresses for the bind address |
| 1000 |
ipaddr = parts[0] |
| 1001 |
if not re.match( |
| 1002 |
r"^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$", parts[0] |
| 1003 |
) and not re.match(r"^\[[0-9a-fA-F:]+(?:|%[^\]/]+)\]$", ipaddr): |
| 1004 |
module.fail_json( |
| 1005 |
msg="Bind addresses for published ports must be IPv4 or IPv6 addresses, not hostnames. " |
| 1006 |
f"Use the dig lookup to resolve hostnames. (Found hostname: {ipaddr})" |
| 1007 |
) |
| 1008 |
if re.match(r"^\[[0-9a-fA-F:]+\]$", ipaddr): |
| 1009 |
ipaddr = ipaddr[1:-1] |
| 1010 |
if parts[1]: |
| 1011 |
if len(container_ports) == 1: |
| 1012 |
port_binds = [(ipaddr, parts[1])] |
| 1013 |
else: |
| 1014 |
port_binds = [ |
| 1015 |
(ipaddr, port) |
| 1016 |
for port in _parse_port_range(parts[1], module) |
| 1017 |
] |
| 1018 |
else: |
| 1019 |
port_binds = len(container_ports) * [(ipaddr,)] |
| 1020 |
else: |
| 1021 |
module.fail_json( |
| 1022 |
msg=f'Invalid port description "{port}" - expected 1 to 3 colon-separated parts, but got {p_len}. ' |
| 1023 |
"Maybe you forgot to use square brackets ([...]) around an IPv6 address?" |
| 1024 |
) |
| 1025 |
|
| 1026 |
for bind, container_port_val in zip(port_binds, container_ports): |
| 1027 |
idx = ( |
| 1028 |
f"{container_port_val}/{protocol}" |
| 1029 |
if protocol |
| 1030 |
else container_port_val |
| 1031 |
) |
| 1032 |
if idx in binds: |
| 1033 |
old_bind = binds[idx] |
| 1034 |
if isinstance(old_bind, list): |
| 1035 |
old_bind.append(bind) |
| 1036 |
else: |
| 1037 |
binds[idx] = [old_bind, bind] |
| 1038 |
else: |
| 1039 |
binds[idx] = bind |
| 1040 |
values["published_ports"] = binds |
| 1041 |
|
| 1042 |
exposed: set[tuple[int, str]] = set() |
| 1043 |
if "exposed_ports" in values: |
| 1044 |
for port in values["exposed_ports"]: |
| 1045 |
port = to_text(port, errors="surrogate_or_strict").strip() |
| 1046 |
protocol = "tcp" |
| 1047 |
parts = port.split("/", maxsplit=1) |
| 1048 |
if len(parts) == 2: |
| 1049 |
port, protocol = parts |
| 1050 |
parts = port.split("-", maxsplit=1) |
| 1051 |
if len(parts) < 2: |
| 1052 |
try: |
| 1053 |
exposed.add((int(port), protocol)) |
| 1054 |
except ValueError as e: |
| 1055 |
module.fail_json(msg=f"Cannot parse port {port!r}: {e}") |
| 1056 |
else: |
| 1057 |
try: |
| 1058 |
start_port = int(parts[0]) |
| 1059 |
end_port = int(parts[1]) |
| 1060 |
if start_port > end_port: |
| 1061 |
raise ValueError( |
| 1062 |
"start port must be smaller or equal to end port." |
| 1063 |
) |
| 1064 |
except ValueError as e: |
| 1065 |
module.fail_json(msg=f"Cannot parse port range {port!r}: {e}") |
| 1066 |
for port in range(start_port, end_port + 1): |
| 1067 |
exposed.add((port, protocol)) |
| 1068 |
if "published_ports" in values: |
| 1069 |
# Any published port should also be exposed |
| 1070 |
for publish_port in values["published_ports"]: |
| 1071 |
if isinstance(publish_port, str) and "/" in publish_port: |
| 1072 |
port, protocol = publish_port.split("/") |
| 1073 |
port = int(port) |
| 1074 |
else: |
| 1075 |
protocol = "tcp" |
| 1076 |
port = int(publish_port) |
| 1077 |
exposed.add((port, protocol)) |
| 1078 |
values["ports"] = sorted(exposed) |
| 1079 |
return values |
| 1080 |
|
| 1081 |
|
| 1082 |
def _compare_platform( |
| 1083 |
option: Option, param_value: t.Any, container_value: t.Any |
| 1084 |
) -> bool: |
| 1085 |
if option.comparison == "ignore": |
| 1086 |
return True |
| 1087 |
try: |
| 1088 |
return compare_platform_strings(param_value, container_value) |
| 1089 |
except ValueError: |
| 1090 |
return param_value == container_value |
| 1091 |
|
| 1092 |
|
| 1093 |
OPTION_AUTO_REMOVE = OptionGroup().add_option("auto_remove", value_type="bool") |
| 1094 |
|
| 1095 |
OPTION_BLKIO_WEIGHT = OptionGroup().add_option("blkio_weight", value_type="int") |
| 1096 |
|
| 1097 |
OPTION_CAPABILITIES = OptionGroup().add_option( |
| 1098 |
"capabilities", value_type="set", elements="str" |
| 1099 |
) |
| 1100 |
|
| 1101 |
OPTION_CAP_DROP = OptionGroup().add_option("cap_drop", value_type="set", elements="str") |
| 1102 |
|
| 1103 |
OPTION_CGROUP_NS_MODE = OptionGroup().add_option( |
| 1104 |
"cgroupns_mode", value_type="str", ansible_choices=["private", "host"] |
| 1105 |
) |
| 1106 |
|
| 1107 |
OPTION_CGROUP_PARENT = OptionGroup().add_option("cgroup_parent", value_type="str") |
| 1108 |
|
| 1109 |
OPTION_COMMAND = OptionGroup(preprocess=_preprocess_command).add_option( |
| 1110 |
"command", value_type="list", elements="str", ansible_type="raw" |
| 1111 |
) |
| 1112 |
|
| 1113 |
OPTION_CPU_PERIOD = OptionGroup().add_option("cpu_period", value_type="int") |
| 1114 |
|
| 1115 |
OPTION_CPU_QUOTA = OptionGroup().add_option("cpu_quota", value_type="int") |
| 1116 |
|
| 1117 |
OPTION_CPUSET_CPUS = OptionGroup().add_option("cpuset_cpus", value_type="str") |
| 1118 |
|
| 1119 |
OPTION_CPUSET_MEMS = OptionGroup().add_option("cpuset_mems", value_type="str") |
| 1120 |
|
| 1121 |
OPTION_CPU_SHARES = OptionGroup().add_option("cpu_shares", value_type="int") |
| 1122 |
|
| 1123 |
OPTION_ENTRYPOINT = OptionGroup(preprocess=_preprocess_entrypoint).add_option( |
| 1124 |
"entrypoint", value_type="list", elements="str" |
| 1125 |
) |
| 1126 |
|
| 1127 |
OPTION_CPUS = OptionGroup().add_option("cpus", value_type="int", ansible_type="float") |
| 1128 |
|
| 1129 |
OPTION_DETACH_INTERACTIVE = ( |
| 1130 |
OptionGroup() |
| 1131 |
.add_option("detach", value_type="bool") |
| 1132 |
.add_option("interactive", value_type="bool") |
| 1133 |
) |
| 1134 |
|
| 1135 |
OPTION_DEVICES = OptionGroup().add_option( |
| 1136 |
"devices", value_type="set", elements="dict", ansible_elements="str" |
| 1137 |
) |
| 1138 |
|
| 1139 |
OPTION_DEVICE_READ_BPS = OptionGroup().add_option( |
| 1140 |
"device_read_bps", |
| 1141 |
value_type="set", |
| 1142 |
elements="dict", |
| 1143 |
ansible_suboptions={ |
| 1144 |
"path": {"type": "str", "required": True}, |
| 1145 |
"rate": {"type": "str", "required": True}, |
| 1146 |
}, |
| 1147 |
) |
| 1148 |
|
| 1149 |
OPTION_DEVICE_WRITE_BPS = OptionGroup().add_option( |
| 1150 |
"device_write_bps", |
| 1151 |
value_type="set", |
| 1152 |
elements="dict", |
| 1153 |
ansible_suboptions={ |
| 1154 |
"path": {"type": "str", "required": True}, |
| 1155 |
"rate": {"type": "str", "required": True}, |
| 1156 |
}, |
| 1157 |
) |
| 1158 |
|
| 1159 |
OPTION_DEVICE_READ_IOPS = OptionGroup().add_option( |
| 1160 |
"device_read_iops", |
| 1161 |
value_type="set", |
| 1162 |
elements="dict", |
| 1163 |
ansible_suboptions={ |
| 1164 |
"path": {"type": "str", "required": True}, |
| 1165 |
"rate": {"type": "int", "required": True}, |
| 1166 |
}, |
| 1167 |
) |
| 1168 |
|
| 1169 |
OPTION_DEVICE_WRITE_IOPS = OptionGroup().add_option( |
| 1170 |
"device_write_iops", |
| 1171 |
value_type="set", |
| 1172 |
elements="dict", |
| 1173 |
ansible_suboptions={ |
| 1174 |
"path": {"type": "str", "required": True}, |
| 1175 |
"rate": {"type": "int", "required": True}, |
| 1176 |
}, |
| 1177 |
) |
| 1178 |
|
| 1179 |
OPTION_DEVICE_REQUESTS = OptionGroup().add_option( |
| 1180 |
"device_requests", |
| 1181 |
value_type="set", |
| 1182 |
elements="dict", |
| 1183 |
ansible_suboptions={ |
| 1184 |
"capabilities": {"type": "list", "elements": "list"}, |
| 1185 |
"count": {"type": "int"}, |
| 1186 |
"device_ids": {"type": "list", "elements": "str"}, |
| 1187 |
"driver": {"type": "str"}, |
| 1188 |
"options": {"type": "dict"}, |
| 1189 |
}, |
| 1190 |
) |
| 1191 |
|
| 1192 |
OPTION_DEVICE_CGROUP_RULES = OptionGroup().add_option( |
| 1193 |
"device_cgroup_rules", value_type="list", elements="str" |
| 1194 |
) |
| 1195 |
|
| 1196 |
OPTION_DNS_SERVERS = OptionGroup().add_option( |
| 1197 |
"dns_servers", value_type="list", elements="str" |
| 1198 |
) |
| 1199 |
|
| 1200 |
OPTION_DNS_OPTS = OptionGroup().add_option("dns_opts", value_type="set", elements="str") |
| 1201 |
|
| 1202 |
OPTION_DNS_SEARCH_DOMAINS = OptionGroup().add_option( |
| 1203 |
"dns_search_domains", value_type="list", elements="str" |
| 1204 |
) |
| 1205 |
|
| 1206 |
OPTION_DOMAINNAME = OptionGroup().add_option("domainname", value_type="str") |
| 1207 |
|
| 1208 |
OPTION_ENVIRONMENT = ( |
| 1209 |
OptionGroup(preprocess=_preprocess_env) |
| 1210 |
.add_option( |
| 1211 |
"env", |
| 1212 |
value_type="set", |
| 1213 |
ansible_type="dict", |
| 1214 |
elements="str", |
| 1215 |
needs_no_suboptions=True, |
| 1216 |
) |
| 1217 |
.add_option( |
| 1218 |
"env_file", |
| 1219 |
value_type="set", |
| 1220 |
ansible_type="path", |
| 1221 |
elements="str", |
| 1222 |
not_a_container_option=True, |
| 1223 |
) |
| 1224 |
) |
| 1225 |
|
| 1226 |
OPTION_ETC_HOSTS = OptionGroup().add_option( |
| 1227 |
"etc_hosts", |
| 1228 |
value_type="set", |
| 1229 |
ansible_type="dict", |
| 1230 |
elements="str", |
| 1231 |
needs_no_suboptions=True, |
| 1232 |
) |
| 1233 |
|
| 1234 |
OPTION_GROUPS = OptionGroup().add_option("groups", value_type="set", elements="str") |
| 1235 |
|
| 1236 |
OPTION_HEALTHCHECK = OptionGroup(preprocess=_preprocess_healthcheck).add_option( |
| 1237 |
"healthcheck", |
| 1238 |
value_type="dict", |
| 1239 |
ansible_suboptions={ |
| 1240 |
"test": {"type": "raw"}, |
| 1241 |
"test_cli_compatible": {"type": "bool", "default": False}, |
| 1242 |
"interval": {"type": "str"}, |
| 1243 |
"timeout": {"type": "str"}, |
| 1244 |
"start_period": {"type": "str"}, |
| 1245 |
"start_interval": {"type": "str"}, |
| 1246 |
"retries": {"type": "int"}, |
| 1247 |
}, |
| 1248 |
) |
| 1249 |
|
| 1250 |
OPTION_HOSTNAME = OptionGroup().add_option("hostname", value_type="str") |
| 1251 |
|
| 1252 |
OPTION_IMAGE = OptionGroup().add_option("image", value_type="str") |
| 1253 |
|
| 1254 |
OPTION_INIT = OptionGroup().add_option("init", value_type="bool") |
| 1255 |
|
| 1256 |
OPTION_IPC_MODE = OptionGroup().add_option("ipc_mode", value_type="str") |
| 1257 |
|
| 1258 |
OPTION_KERNEL_MEMORY = OptionGroup( |
| 1259 |
preprocess=partial(_preprocess_convert_to_bytes, name="kernel_memory") |
| 1260 |
).add_option("kernel_memory", value_type="int", ansible_type="str") |
| 1261 |
|
| 1262 |
OPTION_LABELS = OptionGroup(preprocess=_preprocess_labels).add_option( |
| 1263 |
"labels", value_type="dict", needs_no_suboptions=True |
| 1264 |
) |
| 1265 |
|
| 1266 |
OPTION_LINKS = OptionGroup().add_option( |
| 1267 |
"links", value_type="set", elements="list", ansible_elements="str" |
| 1268 |
) |
| 1269 |
|
| 1270 |
OPTION_LOG_DRIVER_OPTIONS = ( |
| 1271 |
OptionGroup( |
| 1272 |
preprocess=_preprocess_log, ansible_required_by={"log_options": ["log_driver"]} |
| 1273 |
) |
| 1274 |
.add_option("log_driver", value_type="str") |
| 1275 |
.add_option( |
| 1276 |
"log_options", |
| 1277 |
value_type="dict", |
| 1278 |
ansible_aliases=["log_opt"], |
| 1279 |
needs_no_suboptions=True, |
| 1280 |
) |
| 1281 |
) |
| 1282 |
|
| 1283 |
OPTION_MAC_ADDRESS = OptionGroup(preprocess=_preprocess_mac_address).add_option( |
| 1284 |
"mac_address", value_type="str" |
| 1285 |
) |
| 1286 |
|
| 1287 |
OPTION_MEMORY = OptionGroup( |
| 1288 |
preprocess=partial(_preprocess_convert_to_bytes, name="memory") |
| 1289 |
).add_option("memory", value_type="int", ansible_type="str") |
| 1290 |
|
| 1291 |
OPTION_MEMORY_RESERVATION = OptionGroup( |
| 1292 |
preprocess=partial(_preprocess_convert_to_bytes, name="memory_reservation") |
| 1293 |
).add_option("memory_reservation", value_type="int", ansible_type="str") |
| 1294 |
|
| 1295 |
OPTION_MEMORY_SWAP = OptionGroup( |
| 1296 |
preprocess=partial( |
| 1297 |
_preprocess_convert_to_bytes, name="memory_swap", unlimited_value=-1 |
| 1298 |
) |
| 1299 |
).add_option("memory_swap", value_type="int", ansible_type="str") |
| 1300 |
|
| 1301 |
OPTION_MEMORY_SWAPPINESS = OptionGroup().add_option( |
| 1302 |
"memory_swappiness", value_type="int" |
| 1303 |
) |
| 1304 |
|
| 1305 |
OPTION_STOP_TIMEOUT = OptionGroup().add_option( |
| 1306 |
"stop_timeout", value_type="int", default_comparison="ignore" |
| 1307 |
) |
| 1308 |
|
| 1309 |
OPTION_NETWORK = ( |
| 1310 |
OptionGroup(preprocess=_preprocess_networks) |
| 1311 |
.add_option("network_mode", value_type="str") |
| 1312 |
.add_option( |
| 1313 |
"networks", |
| 1314 |
value_type="set", |
| 1315 |
elements="dict", |
| 1316 |
ansible_suboptions={ |
| 1317 |
"name": {"type": "str", "required": True}, |
| 1318 |
"ipv4_address": {"type": "str"}, |
| 1319 |
"ipv6_address": {"type": "str"}, |
| 1320 |
"aliases": {"type": "list", "elements": "str"}, |
| 1321 |
"links": {"type": "list", "elements": "str"}, |
| 1322 |
"mac_address": {"type": "str"}, |
| 1323 |
"driver_opts": {"type": "dict"}, |
| 1324 |
"gw_priority": {"type": "int"}, |
| 1325 |
}, |
| 1326 |
) |
| 1327 |
) |
| 1328 |
|
| 1329 |
OPTION_OOM_KILLER = OptionGroup().add_option("oom_killer", value_type="bool") |
| 1330 |
|
| 1331 |
OPTION_OOM_SCORE_ADJ = OptionGroup().add_option("oom_score_adj", value_type="int") |
| 1332 |
|
| 1333 |
OPTION_PID_MODE = OptionGroup().add_option("pid_mode", value_type="str") |
| 1334 |
|
| 1335 |
OPTION_PIDS_LIMIT = OptionGroup().add_option("pids_limit", value_type="int") |
| 1336 |
|
| 1337 |
OPTION_PLATFORM = OptionGroup().add_option( |
| 1338 |
"platform", value_type="str", compare=_compare_platform |
| 1339 |
) |
| 1340 |
|
| 1341 |
OPTION_PRIVILEGED = OptionGroup().add_option("privileged", value_type="bool") |
| 1342 |
|
| 1343 |
OPTION_READ_ONLY = OptionGroup().add_option("read_only", value_type="bool") |
| 1344 |
|
| 1345 |
OPTION_RESTART_POLICY = ( |
| 1346 |
OptionGroup(ansible_required_by={"restart_retries": ["restart_policy"]}) |
| 1347 |
.add_option( |
| 1348 |
"restart_policy", |
| 1349 |
value_type="str", |
| 1350 |
ansible_choices=["no", "on-failure", "always", "unless-stopped"], |
| 1351 |
) |
| 1352 |
.add_option("restart_retries", value_type="int") |
| 1353 |
) |
| 1354 |
|
| 1355 |
OPTION_RUNTIME = OptionGroup().add_option("runtime", value_type="str") |
| 1356 |
|
| 1357 |
OPTION_SECURITY_OPTS = OptionGroup().add_option( |
| 1358 |
"security_opts", value_type="set", elements="str" |
| 1359 |
) |
| 1360 |
|
| 1361 |
OPTION_SHM_SIZE = OptionGroup( |
| 1362 |
preprocess=partial(_preprocess_convert_to_bytes, name="shm_size") |
| 1363 |
).add_option("shm_size", value_type="int", ansible_type="str") |
| 1364 |
|
| 1365 |
OPTION_STOP_SIGNAL = OptionGroup().add_option("stop_signal", value_type="str") |
| 1366 |
|
| 1367 |
OPTION_STORAGE_OPTS = OptionGroup().add_option( |
| 1368 |
"storage_opts", value_type="dict", needs_no_suboptions=True |
| 1369 |
) |
| 1370 |
|
| 1371 |
OPTION_SYSCTLS = OptionGroup(preprocess=_preprocess_sysctls).add_option( |
| 1372 |
"sysctls", value_type="dict", needs_no_suboptions=True |
| 1373 |
) |
| 1374 |
|
| 1375 |
OPTION_TMPFS = OptionGroup(preprocess=_preprocess_tmpfs).add_option( |
| 1376 |
"tmpfs", value_type="dict", ansible_type="list", ansible_elements="str" |
| 1377 |
) |
| 1378 |
|
| 1379 |
OPTION_TTY = OptionGroup().add_option("tty", value_type="bool") |
| 1380 |
|
| 1381 |
OPTION_ULIMITS = OptionGroup(preprocess=_preprocess_ulimits).add_option( |
| 1382 |
"ulimits", value_type="set", elements="dict", ansible_elements="str" |
| 1383 |
) |
| 1384 |
|
| 1385 |
OPTION_USER = OptionGroup().add_option("user", value_type="str") |
| 1386 |
|
| 1387 |
OPTION_USERNS_MODE = OptionGroup().add_option("userns_mode", value_type="str") |
| 1388 |
|
| 1389 |
OPTION_UTS = OptionGroup().add_option("uts", value_type="str") |
| 1390 |
|
| 1391 |
OPTION_VOLUME_DRIVER = OptionGroup().add_option("volume_driver", value_type="str") |
| 1392 |
|
| 1393 |
OPTION_VOLUMES_FROM = OptionGroup().add_option( |
| 1394 |
"volumes_from", value_type="set", elements="str" |
| 1395 |
) |
| 1396 |
|
| 1397 |
OPTION_WORKING_DIR = OptionGroup().add_option("working_dir", value_type="str") |
| 1398 |
|
| 1399 |
OPTION_MOUNTS_VOLUMES = ( |
| 1400 |
OptionGroup(preprocess=_preprocess_mounts) |
| 1401 |
.add_option( |
| 1402 |
"mounts", |
| 1403 |
value_type="set", |
| 1404 |
elements="dict", |
| 1405 |
ansible_suboptions={ |
| 1406 |
"target": {"type": "str", "required": True}, |
| 1407 |
"source": {"type": "str"}, |
| 1408 |
"type": { |
| 1409 |
"type": "str", |
| 1410 |
"choices": ["bind", "volume", "tmpfs", "npipe", "cluster", "image"], |
| 1411 |
"default": "volume", |
| 1412 |
}, |
| 1413 |
"read_only": {"type": "bool"}, |
| 1414 |
"consistency": { |
| 1415 |
"type": "str", |
| 1416 |
"choices": ["default", "consistent", "cached", "delegated"], |
| 1417 |
}, |
| 1418 |
"propagation": { |
| 1419 |
"type": "str", |
| 1420 |
"choices": [ |
| 1421 |
"private", |
| 1422 |
"rprivate", |
| 1423 |
"shared", |
| 1424 |
"rshared", |
| 1425 |
"slave", |
| 1426 |
"rslave", |
| 1427 |
], |
| 1428 |
}, |
| 1429 |
"no_copy": {"type": "bool"}, |
| 1430 |
"labels": {"type": "dict"}, |
| 1431 |
"volume_driver": {"type": "str"}, |
| 1432 |
"volume_options": {"type": "dict"}, |
| 1433 |
"tmpfs_size": {"type": "str"}, |
| 1434 |
"tmpfs_mode": {"type": "str"}, |
| 1435 |
"non_recursive": {"type": "bool"}, |
| 1436 |
"create_mountpoint": {"type": "bool"}, |
| 1437 |
"read_only_non_recursive": {"type": "bool"}, |
| 1438 |
"read_only_force_recursive": {"type": "bool"}, |
| 1439 |
"subpath": {"type": "str"}, |
| 1440 |
"tmpfs_options": {"type": "list", "elements": "dict"}, |
| 1441 |
}, |
| 1442 |
) |
| 1443 |
.add_option("volumes", value_type="set", elements="str") |
| 1444 |
.add_option( |
| 1445 |
"volume_binds", |
| 1446 |
value_type="set", |
| 1447 |
elements="str", |
| 1448 |
not_an_ansible_option=True, |
| 1449 |
copy_comparison_from="volumes", |
| 1450 |
) |
| 1451 |
) |
| 1452 |
|
| 1453 |
OPTION_PORTS = ( |
| 1454 |
OptionGroup(preprocess=_preprocess_ports) |
| 1455 |
.add_option( |
| 1456 |
"exposed_ports", |
| 1457 |
value_type="set", |
| 1458 |
elements="str", |
| 1459 |
ansible_aliases=["exposed", "expose"], |
| 1460 |
) |
| 1461 |
.add_option("publish_all_ports", value_type="bool") |
| 1462 |
.add_option( |
| 1463 |
"published_ports", |
| 1464 |
value_type="dict", |
| 1465 |
ansible_type="list", |
| 1466 |
ansible_elements="str", |
| 1467 |
ansible_aliases=["ports"], |
| 1468 |
) |
| 1469 |
.add_option( |
| 1470 |
"ports", |
| 1471 |
value_type="set", |
| 1472 |
elements="str", |
| 1473 |
not_an_ansible_option=True, |
| 1474 |
default_comparison="ignore", |
| 1475 |
) |
| 1476 |
) |
| 1477 |
|
| 1478 |
OPTIONS = [ |
| 1479 |
OPTION_AUTO_REMOVE, |
| 1480 |
OPTION_BLKIO_WEIGHT, |
| 1481 |
OPTION_CAPABILITIES, |
| 1482 |
OPTION_CAP_DROP, |
| 1483 |
OPTION_CGROUP_NS_MODE, |
| 1484 |
OPTION_CGROUP_PARENT, |
| 1485 |
OPTION_COMMAND, |
| 1486 |
OPTION_CPU_PERIOD, |
| 1487 |
OPTION_CPU_QUOTA, |
| 1488 |
OPTION_CPUSET_CPUS, |
| 1489 |
OPTION_CPUSET_MEMS, |
| 1490 |
OPTION_CPU_SHARES, |
| 1491 |
OPTION_ENTRYPOINT, |
| 1492 |
OPTION_CPUS, |
| 1493 |
OPTION_DETACH_INTERACTIVE, |
| 1494 |
OPTION_DEVICES, |
| 1495 |
OPTION_DEVICE_READ_BPS, |
| 1496 |
OPTION_DEVICE_WRITE_BPS, |
| 1497 |
OPTION_DEVICE_READ_IOPS, |
| 1498 |
OPTION_DEVICE_WRITE_IOPS, |
| 1499 |
OPTION_DEVICE_REQUESTS, |
| 1500 |
OPTION_DEVICE_CGROUP_RULES, |
| 1501 |
OPTION_DNS_SERVERS, |
| 1502 |
OPTION_DNS_OPTS, |
| 1503 |
OPTION_DNS_SEARCH_DOMAINS, |
| 1504 |
OPTION_DOMAINNAME, |
| 1505 |
OPTION_ENVIRONMENT, |
| 1506 |
OPTION_ETC_HOSTS, |
| 1507 |
OPTION_GROUPS, |
| 1508 |
OPTION_HEALTHCHECK, |
| 1509 |
OPTION_HOSTNAME, |
| 1510 |
OPTION_IMAGE, |
| 1511 |
OPTION_INIT, |
| 1512 |
OPTION_IPC_MODE, |
| 1513 |
OPTION_KERNEL_MEMORY, |
| 1514 |
OPTION_LABELS, |
| 1515 |
OPTION_LINKS, |
| 1516 |
OPTION_LOG_DRIVER_OPTIONS, |
| 1517 |
OPTION_MAC_ADDRESS, |
| 1518 |
OPTION_MEMORY, |
| 1519 |
OPTION_MEMORY_RESERVATION, |
| 1520 |
OPTION_MEMORY_SWAP, |
| 1521 |
OPTION_MEMORY_SWAPPINESS, |
| 1522 |
OPTION_STOP_TIMEOUT, |
| 1523 |
OPTION_NETWORK, |
| 1524 |
OPTION_OOM_KILLER, |
| 1525 |
OPTION_OOM_SCORE_ADJ, |
| 1526 |
OPTION_PID_MODE, |
| 1527 |
OPTION_PIDS_LIMIT, |
| 1528 |
OPTION_PLATFORM, |
| 1529 |
OPTION_PRIVILEGED, |
| 1530 |
OPTION_READ_ONLY, |
| 1531 |
OPTION_RESTART_POLICY, |
| 1532 |
OPTION_RUNTIME, |
| 1533 |
OPTION_SECURITY_OPTS, |
| 1534 |
OPTION_SHM_SIZE, |
| 1535 |
OPTION_STOP_SIGNAL, |
| 1536 |
OPTION_STORAGE_OPTS, |
| 1537 |
OPTION_SYSCTLS, |
| 1538 |
OPTION_TMPFS, |
| 1539 |
OPTION_TTY, |
| 1540 |
OPTION_ULIMITS, |
| 1541 |
OPTION_USER, |
| 1542 |
OPTION_USERNS_MODE, |
| 1543 |
OPTION_UTS, |
| 1544 |
OPTION_VOLUME_DRIVER, |
| 1545 |
OPTION_VOLUMES_FROM, |
| 1546 |
OPTION_WORKING_DIR, |
| 1547 |
OPTION_MOUNTS_VOLUMES, |
| 1548 |
OPTION_PORTS, |
| 1549 |
] |