| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2022, 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_container_copy_into |
| 11 |
|
| 12 |
short_description: Copy a file into a Docker container |
| 13 |
|
| 14 |
version_added: 3.4.0 |
| 15 |
|
| 16 |
description: |
| 17 |
- Copy a file into a Docker container. |
| 18 |
- Similar to C(docker cp). |
| 19 |
- To copy files in a non-running container, you must provide the O(owner_id) and O(group_id) options. This is also necessary |
| 20 |
if the container does not contain a C(/bin/sh) shell with an C(id) tool. |
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: full |
| 24 |
diff_mode: |
| 25 |
support: full |
| 26 |
details: |
| 27 |
- Additional data will need to be transferred to compute diffs. |
| 28 |
- The module uses R(the MAX_FILE_SIZE_FOR_DIFF ansible-core configuration,MAX_FILE_SIZE_FOR_DIFF) to determine for how |
| 29 |
large files diffs should be computed. |
| 30 |
idempotent: |
| 31 |
support: partial |
| 32 |
details: |
| 33 |
- If O(force=true) the module is not idempotent. |
| 34 |
|
| 35 |
options: |
| 36 |
container: |
| 37 |
description: |
| 38 |
- The name of the container to copy files to. |
| 39 |
type: str |
| 40 |
required: true |
| 41 |
path: |
| 42 |
description: |
| 43 |
- Path to a file on the managed node. |
| 44 |
- Mutually exclusive with O(content). One of O(content) and O(path) is required. |
| 45 |
type: path |
| 46 |
content: |
| 47 |
description: |
| 48 |
- The file's content. |
| 49 |
- If you plan to provide binary data, provide it pre-encoded to base64, and set O(content_is_b64=true). |
| 50 |
- Mutually exclusive with O(path). One of O(content) and O(path) is required. |
| 51 |
type: str |
| 52 |
content_is_b64: |
| 53 |
description: |
| 54 |
- If set to V(true), the content in O(content) is assumed to be Base64 encoded and will be decoded before being used. |
| 55 |
- To use binary O(content), it is better to keep it Base64 encoded and let it be decoded by this option. Otherwise you |
| 56 |
risk the data to be interpreted as UTF-8 and corrupted. |
| 57 |
type: bool |
| 58 |
default: false |
| 59 |
container_path: |
| 60 |
description: |
| 61 |
- Path to a file inside the Docker container. |
| 62 |
- Must be an absolute path. |
| 63 |
type: str |
| 64 |
required: true |
| 65 |
follow: |
| 66 |
description: |
| 67 |
- This flag indicates that filesystem links in the Docker container, if they exist, should be followed. |
| 68 |
type: bool |
| 69 |
default: false |
| 70 |
local_follow: |
| 71 |
description: |
| 72 |
- This flag indicates that filesystem links in the source tree (where the module is executed), if they exist, should |
| 73 |
be followed. |
| 74 |
type: bool |
| 75 |
default: true |
| 76 |
owner_id: |
| 77 |
description: |
| 78 |
- The owner ID to use when writing the file to disk. |
| 79 |
- If provided, O(group_id) must also be provided. |
| 80 |
- If not provided, the module will try to determine the user and group ID for the current user in the container. This |
| 81 |
will only work if C(/bin/sh) is present in the container and the C(id) binary or shell builtin is available. Also |
| 82 |
the container must be running. |
| 83 |
type: int |
| 84 |
group_id: |
| 85 |
description: |
| 86 |
- The group ID to use when writing the file to disk. |
| 87 |
- If provided, O(owner_id) must also be provided. |
| 88 |
- If not provided, the module will try to determine the user and group ID for the current user in the container. This |
| 89 |
will only work if C(/bin/sh) is present in the container and the C(id) binary or shell builtin is available. Also |
| 90 |
the container must be running. |
| 91 |
type: int |
| 92 |
mode: |
| 93 |
description: |
| 94 |
- The file mode to use when writing the file to disk. |
| 95 |
- Will use the file's mode from the source system if this option is not provided. |
| 96 |
- This option is parsed depending on how O(mode_parse) is set. |
| 97 |
type: raw |
| 98 |
mode_parse: |
| 99 |
description: |
| 100 |
- Determines how to parse the O(mode) parameter. |
| 101 |
type: str |
| 102 |
choices: |
| 103 |
legacy: |
| 104 |
- Parses the value of O(mode) as an integer. |
| 105 |
- Note that if you provide an octal number as a string to O(mode), it will be parsed as a B(decimal) number. |
| 106 |
If you provide an octal integer directly, though, it will work as expected. |
| 107 |
- This has been the default behavior of the module since it was added to community.docker. |
| 108 |
modern: |
| 109 |
- Parses the value of O(mode) as an octal string, or takes the integer value if an integer has been provided. |
| 110 |
- This is how M(ansible.builtin.copy) treats its O(ansible.builtin.copy#module:mode) option. |
| 111 |
octal_string_only: |
| 112 |
- Rejects everything that is not a string that can be parsed as an octal number. |
| 113 |
- Use this value to ensure that no accidental conversion to integers happen. |
| 114 |
default: legacy |
| 115 |
version_added: 4.6.0 |
| 116 |
force: |
| 117 |
description: |
| 118 |
- If set to V(true), force writing the file (without performing any idempotency checks). |
| 119 |
- If set to V(false), only write the file if it does not exist on the target. If a filesystem object exists at the destination, |
| 120 |
the module will not do any change. |
| 121 |
- If this option is not specified, the module will be idempotent. To verify idempotency, it will try to get information |
| 122 |
on the filesystem object in the container, and if everything seems to match will download the file from the container |
| 123 |
to compare it to the file to upload. |
| 124 |
type: bool |
| 125 |
|
| 126 |
extends_documentation_fragment: |
| 127 |
- community.docker._docker.api_documentation |
| 128 |
- community.docker._attributes |
| 129 |
- community.docker._attributes.actiongroup_docker |
| 130 |
|
| 131 |
author: |
| 132 |
- "Felix Fontein (@felixfontein)" |
| 133 |
|
| 134 |
requirements: |
| 135 |
- "Docker API >= 1.25" |
| 136 |
""" |
| 137 |
|
| 138 |
EXAMPLES = r""" |
| 139 |
--- |
| 140 |
- name: Copy a file into the container |
| 141 |
community.docker.docker_container_copy_into: |
| 142 |
container: mydata |
| 143 |
path: /home/user/data.txt |
| 144 |
container_path: /data/input.txt |
| 145 |
|
| 146 |
- name: Copy a file into the container with owner, group, and mode set |
| 147 |
community.docker.docker_container_copy_into: |
| 148 |
container: mydata |
| 149 |
path: /home/user/bin/runme.o |
| 150 |
container_path: /bin/runme |
| 151 |
owner_id: 0 # root |
| 152 |
group_id: 0 # root |
| 153 |
mode: "0755" # readable and executable by all users, writable by root |
| 154 |
mode_parse: modern # ensure that strings passed for 'mode' are passed as octal numbers |
| 155 |
""" |
| 156 |
|
| 157 |
RETURN = r""" |
| 158 |
container_path: |
| 159 |
description: |
| 160 |
- The actual path in the container. |
| 161 |
- Can only be different from O(container_path) when O(follow=true). |
| 162 |
type: str |
| 163 |
returned: success |
| 164 |
""" |
| 165 |
|
| 166 |
import base64 |
| 167 |
import io |
| 168 |
import os |
| 169 |
import stat |
| 170 |
import traceback |
| 171 |
import typing as t |
| 172 |
|
| 173 |
from ansible.module_utils.common.text.converters import to_bytes, to_text |
| 174 |
from ansible.module_utils.common.validation import check_type_int |
| 175 |
|
| 176 |
from ansible_collections.community.docker.plugins.module_utils._api.errors import ( |
| 177 |
APIError, |
| 178 |
DockerException, |
| 179 |
NotFound, |
| 180 |
) |
| 181 |
from ansible_collections.community.docker.plugins.module_utils._common_api import ( |
| 182 |
AnsibleDockerClient, |
| 183 |
RequestException, |
| 184 |
) |
| 185 |
from ansible_collections.community.docker.plugins.module_utils._copy import ( |
| 186 |
DockerFileCopyError, |
| 187 |
DockerFileNotFound, |
| 188 |
DockerUnexpectedError, |
| 189 |
determine_user_group, |
| 190 |
fetch_file_ex, |
| 191 |
put_file, |
| 192 |
put_file_content, |
| 193 |
stat_file, |
| 194 |
) |
| 195 |
from ansible_collections.community.docker.plugins.module_utils._scramble import ( |
| 196 |
generate_insecure_key, |
| 197 |
scramble, |
| 198 |
) |
| 199 |
|
| 200 |
if t.TYPE_CHECKING: |
| 201 |
import tarfile |
| 202 |
|
| 203 |
|
| 204 |
def are_fileobjs_equal(f1: t.IO[bytes], f2: t.IO[bytes]) -> bool: |
| 205 |
"""Given two (buffered) file objects, compare their contents.""" |
| 206 |
f1on: t.IO[bytes] | None = f1 |
| 207 |
f2on: t.IO[bytes] | None = f2 |
| 208 |
blocksize = 65536 |
| 209 |
b1buf = b"" |
| 210 |
b2buf = b"" |
| 211 |
while True: |
| 212 |
if f1on and len(b1buf) < blocksize: |
| 213 |
f1b = f1on.read(blocksize) |
| 214 |
if not f1b: |
| 215 |
# f1 is EOF, so stop reading from it |
| 216 |
f1on = None |
| 217 |
b1buf += f1b |
| 218 |
if f2on and len(b2buf) < blocksize: |
| 219 |
f2b = f2on.read(blocksize) |
| 220 |
if not f2b: |
| 221 |
# f2 is EOF, so stop reading from it |
| 222 |
f2on = None |
| 223 |
b2buf += f2b |
| 224 |
if not b1buf or not b2buf: |
| 225 |
# At least one of f1 and f2 is EOF and all its data has |
| 226 |
# been processed. If both are EOF and their data has been |
| 227 |
# processed, the files are equal, otherwise not. |
| 228 |
return not b1buf and not b2buf |
| 229 |
# Compare the next chunk of data, and remove it from the buffers |
| 230 |
buflen = min(len(b1buf), len(b2buf)) |
| 231 |
if b1buf[:buflen] != b2buf[:buflen]: |
| 232 |
return False |
| 233 |
b1buf = b1buf[buflen:] |
| 234 |
b2buf = b2buf[buflen:] |
| 235 |
|
| 236 |
|
| 237 |
def are_fileobjs_equal_read_first( |
| 238 |
f1: t.IO[bytes], f2: t.IO[bytes] |
| 239 |
) -> tuple[bool, bytes]: |
| 240 |
"""Given two (buffered) file objects, compare their contents. |
| 241 |
|
| 242 |
Returns a tuple (is_equal, content_of_f1), where the first element indicates |
| 243 |
whether the two file objects have the same content, and the second element is |
| 244 |
the content of the first file object.""" |
| 245 |
f1on: t.IO[bytes] | None = f1 |
| 246 |
f2on: t.IO[bytes] | None = f2 |
| 247 |
blocksize = 65536 |
| 248 |
b1buf = b"" |
| 249 |
b2buf = b"" |
| 250 |
is_equal = True |
| 251 |
content = [] |
| 252 |
while True: |
| 253 |
if f1on and len(b1buf) < blocksize: |
| 254 |
f1b = f1on.read(blocksize) |
| 255 |
if not f1b: |
| 256 |
# f1 is EOF, so stop reading from it |
| 257 |
f1on = None |
| 258 |
b1buf += f1b |
| 259 |
if f2on and len(b2buf) < blocksize: |
| 260 |
f2b = f2on.read(blocksize) |
| 261 |
if not f2b: |
| 262 |
# f2 is EOF, so stop reading from it |
| 263 |
f2on = None |
| 264 |
b2buf += f2b |
| 265 |
if not b1buf or not b2buf: |
| 266 |
# At least one of f1 and f2 is EOF and all its data has |
| 267 |
# been processed. If both are EOF and their data has been |
| 268 |
# processed, the files are equal, otherwise not. |
| 269 |
is_equal = not b1buf and not b2buf |
| 270 |
break |
| 271 |
# Compare the next chunk of data, and remove it from the buffers |
| 272 |
buflen = min(len(b1buf), len(b2buf)) |
| 273 |
if b1buf[:buflen] != b2buf[:buflen]: |
| 274 |
is_equal = False |
| 275 |
break |
| 276 |
content.append(b1buf[:buflen]) |
| 277 |
b1buf = b1buf[buflen:] |
| 278 |
b2buf = b2buf[buflen:] |
| 279 |
|
| 280 |
content.append(b1buf) |
| 281 |
if f1on: |
| 282 |
content.append(f1on.read()) |
| 283 |
|
| 284 |
return is_equal, b"".join(content) |
| 285 |
|
| 286 |
|
| 287 |
def is_container_file_not_regular_file(container_stat: dict[str, t.Any]) -> bool: |
| 288 |
return any( |
| 289 |
container_stat["mode"] & 1 << bit != 0 |
| 290 |
for bit in ( |
| 291 |
# https://pkg.go.dev/io/fs#FileMode |
| 292 |
32 - 1, # ModeDir |
| 293 |
32 - 4, # ModeTemporary |
| 294 |
32 - 5, # ModeSymlink |
| 295 |
32 - 6, # ModeDevice |
| 296 |
32 - 7, # ModeNamedPipe |
| 297 |
32 - 8, # ModeSocket |
| 298 |
32 - 11, # ModeCharDevice |
| 299 |
32 - 13, # ModeIrregular |
| 300 |
) |
| 301 |
) |
| 302 |
|
| 303 |
|
| 304 |
def get_container_file_mode(container_stat: dict[str, t.Any]) -> int: |
| 305 |
mode = container_stat["mode"] & 0xFFF |
| 306 |
if container_stat["mode"] & (1 << (32 - 9)) != 0: # ModeSetuid |
| 307 |
mode |= stat.S_ISUID # set UID bit |
| 308 |
if container_stat["mode"] & (1 << (32 - 10)) != 0: # ModeSetgid |
| 309 |
mode |= stat.S_ISGID # set GID bit |
| 310 |
if container_stat["mode"] & (1 << (32 - 12)) != 0: # ModeSticky |
| 311 |
mode |= stat.S_ISVTX # sticky bit |
| 312 |
return mode |
| 313 |
|
| 314 |
|
| 315 |
def add_other_diff( |
| 316 |
diff: dict[str, t.Any] | None, in_path: str, member: tarfile.TarInfo |
| 317 |
) -> None: |
| 318 |
if diff is None: |
| 319 |
return |
| 320 |
diff["before_header"] = in_path |
| 321 |
if member.isdir(): |
| 322 |
diff["before"] = "(directory)" |
| 323 |
elif member.issym() or member.islnk(): |
| 324 |
diff["before"] = member.linkname |
| 325 |
elif member.ischr(): |
| 326 |
diff["before"] = "(character device)" |
| 327 |
elif member.isblk(): |
| 328 |
diff["before"] = "(block device)" |
| 329 |
elif member.isfifo(): |
| 330 |
diff["before"] = "(fifo)" |
| 331 |
elif member.isdev(): |
| 332 |
diff["before"] = "(device)" |
| 333 |
elif member.isfile(): |
| 334 |
raise DockerUnexpectedError("should not be a regular file") |
| 335 |
else: |
| 336 |
diff["before"] = "(unknown filesystem object)" |
| 337 |
|
| 338 |
|
| 339 |
def retrieve_diff( |
| 340 |
client: AnsibleDockerClient, |
| 341 |
container: str, |
| 342 |
container_path: str, |
| 343 |
follow_links: bool, |
| 344 |
diff: dict[str, t.Any] | None, |
| 345 |
max_file_size_for_diff: int, |
| 346 |
regular_stat: dict[str, t.Any] | None = None, |
| 347 |
link_target: str | None = None, |
| 348 |
) -> None: |
| 349 |
if diff is None: |
| 350 |
return |
| 351 |
if regular_stat is not None: |
| 352 |
# First handle all filesystem object types that are not regular files |
| 353 |
if regular_stat["mode"] & (1 << (32 - 1)) != 0: |
| 354 |
diff["before_header"] = container_path |
| 355 |
diff["before"] = "(directory)" |
| 356 |
return |
| 357 |
if regular_stat["mode"] & (1 << (32 - 4)) != 0: |
| 358 |
diff["before_header"] = container_path |
| 359 |
diff["before"] = "(temporary file)" |
| 360 |
return |
| 361 |
if regular_stat["mode"] & (1 << (32 - 5)) != 0: |
| 362 |
diff["before_header"] = container_path |
| 363 |
diff["before"] = link_target |
| 364 |
return |
| 365 |
if regular_stat["mode"] & (1 << (32 - 6)) != 0: |
| 366 |
diff["before_header"] = container_path |
| 367 |
diff["before"] = "(device)" |
| 368 |
return |
| 369 |
if regular_stat["mode"] & (1 << (32 - 7)) != 0: |
| 370 |
diff["before_header"] = container_path |
| 371 |
diff["before"] = "(named pipe)" |
| 372 |
return |
| 373 |
if regular_stat["mode"] & (1 << (32 - 8)) != 0: |
| 374 |
diff["before_header"] = container_path |
| 375 |
diff["before"] = "(socket)" |
| 376 |
return |
| 377 |
if regular_stat["mode"] & (1 << (32 - 11)) != 0: |
| 378 |
diff["before_header"] = container_path |
| 379 |
diff["before"] = "(character device)" |
| 380 |
return |
| 381 |
if regular_stat["mode"] & (1 << (32 - 13)) != 0: |
| 382 |
diff["before_header"] = container_path |
| 383 |
diff["before"] = "(unknown filesystem object)" |
| 384 |
return |
| 385 |
# Check whether file is too large |
| 386 |
if regular_stat["size"] > max_file_size_for_diff > 0: |
| 387 |
diff["dst_larger"] = max_file_size_for_diff |
| 388 |
return |
| 389 |
|
| 390 |
# We need to get hold of the content |
| 391 |
def process_none(in_path: str) -> None: |
| 392 |
diff["before"] = "" |
| 393 |
|
| 394 |
def process_regular( |
| 395 |
in_path: str, tar: tarfile.TarFile, member: tarfile.TarInfo |
| 396 |
) -> None: |
| 397 |
add_diff_dst_from_regular_member( |
| 398 |
diff, max_file_size_for_diff, in_path, tar, member |
| 399 |
) |
| 400 |
|
| 401 |
def process_symlink(in_path: str, member: tarfile.TarInfo) -> None: |
| 402 |
diff["before_header"] = in_path |
| 403 |
diff["before"] = member.linkname |
| 404 |
|
| 405 |
def process_other(in_path: str, member: tarfile.TarInfo) -> None: |
| 406 |
add_other_diff(diff, in_path, member) |
| 407 |
|
| 408 |
fetch_file_ex( |
| 409 |
client, |
| 410 |
container, |
| 411 |
in_path=container_path, |
| 412 |
process_none=process_none, |
| 413 |
process_regular=process_regular, |
| 414 |
process_symlink=process_symlink, |
| 415 |
process_other=process_other, |
| 416 |
follow_links=follow_links, |
| 417 |
) |
| 418 |
|
| 419 |
|
| 420 |
def is_binary(content: bytes) -> bool: |
| 421 |
if b"\x00" in content: # noqa: SIM103 |
| 422 |
return True |
| 423 |
# TODO: better detection |
| 424 |
# (ansible-core also just checks for 0x00, and even just sticks to the first 8k, so this is not too bad...) |
| 425 |
return False |
| 426 |
|
| 427 |
|
| 428 |
def are_fileobjs_equal_with_diff_of_first( |
| 429 |
f1: t.IO[bytes], |
| 430 |
f2: t.IO[bytes], |
| 431 |
size: int, |
| 432 |
diff: dict[str, t.Any] | None, |
| 433 |
max_file_size_for_diff: int, |
| 434 |
container_path: str, |
| 435 |
) -> bool: |
| 436 |
if diff is None: |
| 437 |
return are_fileobjs_equal(f1, f2) |
| 438 |
if size > max_file_size_for_diff > 0: |
| 439 |
diff["dst_larger"] = max_file_size_for_diff |
| 440 |
return are_fileobjs_equal(f1, f2) |
| 441 |
is_equal, content = are_fileobjs_equal_read_first(f1, f2) |
| 442 |
if is_binary(content): |
| 443 |
diff["dst_binary"] = 1 |
| 444 |
else: |
| 445 |
diff["before_header"] = container_path |
| 446 |
diff["before"] = to_text(content) |
| 447 |
return is_equal |
| 448 |
|
| 449 |
|
| 450 |
def add_diff_dst_from_regular_member( |
| 451 |
diff: dict[str, t.Any] | None, |
| 452 |
max_file_size_for_diff: int, |
| 453 |
container_path: str, |
| 454 |
tar: tarfile.TarFile, |
| 455 |
member: tarfile.TarInfo, |
| 456 |
) -> None: |
| 457 |
if diff is None: |
| 458 |
return |
| 459 |
if member.size > max_file_size_for_diff > 0: |
| 460 |
diff["dst_larger"] = max_file_size_for_diff |
| 461 |
return |
| 462 |
|
| 463 |
mf = tar.extractfile(member) |
| 464 |
if not mf: |
| 465 |
raise AssertionError("Member should be present for regular file") |
| 466 |
with mf as tar_f: |
| 467 |
content = tar_f.read() |
| 468 |
|
| 469 |
if is_binary(content): |
| 470 |
diff["dst_binary"] = 1 |
| 471 |
else: |
| 472 |
diff["before_header"] = container_path |
| 473 |
diff["before"] = to_text(content) |
| 474 |
|
| 475 |
|
| 476 |
def copy_dst_to_src(diff: dict[str, t.Any] | None) -> None: |
| 477 |
if diff is None: |
| 478 |
return |
| 479 |
for frm, to in [ |
| 480 |
("dst_size", "src_size"), |
| 481 |
("dst_binary", "src_binary"), |
| 482 |
("before_header", "after_header"), |
| 483 |
("before", "after"), |
| 484 |
]: |
| 485 |
if frm in diff: |
| 486 |
diff[to] = diff[frm] |
| 487 |
elif to in diff: |
| 488 |
diff.pop(to) |
| 489 |
|
| 490 |
|
| 491 |
def is_file_idempotent( |
| 492 |
client: AnsibleDockerClient, |
| 493 |
container: str, |
| 494 |
managed_path: str, |
| 495 |
container_path: str, |
| 496 |
follow_links: bool, |
| 497 |
local_follow_links: bool, |
| 498 |
owner_id: int, |
| 499 |
group_id: int, |
| 500 |
mode: int | None, |
| 501 |
force: bool | None = False, |
| 502 |
diff: dict[str, t.Any] | None = None, |
| 503 |
max_file_size_for_diff: int = 1, |
| 504 |
) -> tuple[str, int, bool]: |
| 505 |
# Retrieve information of local file |
| 506 |
try: |
| 507 |
file_stat = ( |
| 508 |
os.stat(managed_path) if local_follow_links else os.lstat(managed_path) |
| 509 |
) |
| 510 |
except OSError as exc: |
| 511 |
if exc.errno == 2: |
| 512 |
raise DockerFileNotFound(f"Cannot find local file {managed_path}") from exc |
| 513 |
raise |
| 514 |
if mode is None: |
| 515 |
mode = stat.S_IMODE(file_stat.st_mode) |
| 516 |
if not stat.S_ISLNK(file_stat.st_mode) and not stat.S_ISREG(file_stat.st_mode): |
| 517 |
raise DockerFileCopyError( |
| 518 |
"Local path {managed_path} is not a symbolic link or file" |
| 519 |
) |
| 520 |
|
| 521 |
if diff is not None: |
| 522 |
if file_stat.st_size > max_file_size_for_diff > 0: |
| 523 |
diff["src_larger"] = max_file_size_for_diff |
| 524 |
elif stat.S_ISLNK(file_stat.st_mode): |
| 525 |
diff["after_header"] = managed_path |
| 526 |
diff["after"] = os.readlink(managed_path) |
| 527 |
else: |
| 528 |
with open(managed_path, "rb") as f: |
| 529 |
content = f.read() |
| 530 |
if is_binary(content): |
| 531 |
diff["src_binary"] = 1 |
| 532 |
else: |
| 533 |
diff["after_header"] = managed_path |
| 534 |
diff["after"] = to_text(content) |
| 535 |
|
| 536 |
# When forcing and we are not following links in the container, go! |
| 537 |
if force and not follow_links: |
| 538 |
retrieve_diff( |
| 539 |
client, |
| 540 |
container, |
| 541 |
container_path, |
| 542 |
follow_links, |
| 543 |
diff, |
| 544 |
max_file_size_for_diff, |
| 545 |
) |
| 546 |
return container_path, mode, False |
| 547 |
|
| 548 |
# Resolve symlinks in the container (if requested), and get information on container's file |
| 549 |
real_container_path, regular_stat, link_target = stat_file( |
| 550 |
client, |
| 551 |
container, |
| 552 |
in_path=container_path, |
| 553 |
follow_links=follow_links, |
| 554 |
) |
| 555 |
|
| 556 |
# Follow links in the Docker container? |
| 557 |
if follow_links: |
| 558 |
container_path = real_container_path |
| 559 |
|
| 560 |
# If the file was not found, continue |
| 561 |
if regular_stat is None: |
| 562 |
if diff is not None: |
| 563 |
diff["before_header"] = container_path |
| 564 |
diff["before"] = "" |
| 565 |
return container_path, mode, False |
| 566 |
|
| 567 |
# When forcing, go! |
| 568 |
if force: |
| 569 |
retrieve_diff( |
| 570 |
client, |
| 571 |
container, |
| 572 |
container_path, |
| 573 |
follow_links, |
| 574 |
diff, |
| 575 |
max_file_size_for_diff, |
| 576 |
regular_stat, |
| 577 |
link_target, |
| 578 |
) |
| 579 |
return container_path, mode, False |
| 580 |
|
| 581 |
# If force is set to False, and the destination exists, assume there's nothing to do |
| 582 |
if force is False: |
| 583 |
retrieve_diff( |
| 584 |
client, |
| 585 |
container, |
| 586 |
container_path, |
| 587 |
follow_links, |
| 588 |
diff, |
| 589 |
max_file_size_for_diff, |
| 590 |
regular_stat, |
| 591 |
link_target, |
| 592 |
) |
| 593 |
copy_dst_to_src(diff) |
| 594 |
return container_path, mode, True |
| 595 |
|
| 596 |
# Basic idempotency checks |
| 597 |
if stat.S_ISLNK(file_stat.st_mode): |
| 598 |
if link_target is None: |
| 599 |
retrieve_diff( |
| 600 |
client, |
| 601 |
container, |
| 602 |
container_path, |
| 603 |
follow_links, |
| 604 |
diff, |
| 605 |
max_file_size_for_diff, |
| 606 |
regular_stat, |
| 607 |
link_target, |
| 608 |
) |
| 609 |
return container_path, mode, False |
| 610 |
local_link_target = os.readlink(managed_path) |
| 611 |
retrieve_diff( |
| 612 |
client, |
| 613 |
container, |
| 614 |
container_path, |
| 615 |
follow_links, |
| 616 |
diff, |
| 617 |
max_file_size_for_diff, |
| 618 |
regular_stat, |
| 619 |
link_target, |
| 620 |
) |
| 621 |
return container_path, mode, local_link_target == link_target |
| 622 |
if link_target is not None: |
| 623 |
retrieve_diff( |
| 624 |
client, |
| 625 |
container, |
| 626 |
container_path, |
| 627 |
follow_links, |
| 628 |
diff, |
| 629 |
max_file_size_for_diff, |
| 630 |
regular_stat, |
| 631 |
link_target, |
| 632 |
) |
| 633 |
return container_path, mode, False |
| 634 |
if is_container_file_not_regular_file(regular_stat): |
| 635 |
retrieve_diff( |
| 636 |
client, |
| 637 |
container, |
| 638 |
container_path, |
| 639 |
follow_links, |
| 640 |
diff, |
| 641 |
max_file_size_for_diff, |
| 642 |
regular_stat, |
| 643 |
link_target, |
| 644 |
) |
| 645 |
return container_path, mode, False |
| 646 |
if file_stat.st_size != regular_stat["size"]: |
| 647 |
retrieve_diff( |
| 648 |
client, |
| 649 |
container, |
| 650 |
container_path, |
| 651 |
follow_links, |
| 652 |
diff, |
| 653 |
max_file_size_for_diff, |
| 654 |
regular_stat, |
| 655 |
link_target, |
| 656 |
) |
| 657 |
return container_path, mode, False |
| 658 |
if mode != get_container_file_mode(regular_stat): |
| 659 |
retrieve_diff( |
| 660 |
client, |
| 661 |
container, |
| 662 |
container_path, |
| 663 |
follow_links, |
| 664 |
diff, |
| 665 |
max_file_size_for_diff, |
| 666 |
regular_stat, |
| 667 |
link_target, |
| 668 |
) |
| 669 |
return container_path, mode, False |
| 670 |
|
| 671 |
# Fetch file from container |
| 672 |
def process_none(in_path: str) -> tuple[str, int, bool]: |
| 673 |
return container_path, mode, False |
| 674 |
|
| 675 |
def process_regular( |
| 676 |
in_path: str, tar: tarfile.TarFile, member: tarfile.TarInfo |
| 677 |
) -> tuple[str, int, bool]: |
| 678 |
# Check things like user/group ID and mode |
| 679 |
if any( |
| 680 |
[ |
| 681 |
member.mode & 0xFFF != mode, |
| 682 |
member.uid != owner_id, |
| 683 |
member.gid != group_id, |
| 684 |
not stat.S_ISREG(file_stat.st_mode), |
| 685 |
member.size != file_stat.st_size, |
| 686 |
] |
| 687 |
): |
| 688 |
add_diff_dst_from_regular_member( |
| 689 |
diff, max_file_size_for_diff, in_path, tar, member |
| 690 |
) |
| 691 |
return container_path, mode, False |
| 692 |
|
| 693 |
mf = tar.extractfile(member) |
| 694 |
if mf is None: |
| 695 |
raise AssertionError("Member should be present for regular file") |
| 696 |
with mf as tar_f, open(managed_path, "rb") as local_f: |
| 697 |
is_equal = are_fileobjs_equal_with_diff_of_first( |
| 698 |
tar_f, local_f, member.size, diff, max_file_size_for_diff, in_path |
| 699 |
) |
| 700 |
return container_path, mode, is_equal |
| 701 |
|
| 702 |
def process_symlink(in_path: str, member: tarfile.TarInfo) -> tuple[str, int, bool]: |
| 703 |
if diff is not None: |
| 704 |
diff["before_header"] = in_path |
| 705 |
diff["before"] = member.linkname |
| 706 |
|
| 707 |
# Check things like user/group ID and mode |
| 708 |
if member.mode & 0xFFF != mode: |
| 709 |
return container_path, mode, False |
| 710 |
if member.uid != owner_id: |
| 711 |
return container_path, mode, False |
| 712 |
if member.gid != group_id: |
| 713 |
return container_path, mode, False |
| 714 |
|
| 715 |
if not stat.S_ISLNK(file_stat.st_mode): |
| 716 |
return container_path, mode, False |
| 717 |
|
| 718 |
local_link_target = os.readlink(managed_path) |
| 719 |
return container_path, mode, member.linkname == local_link_target |
| 720 |
|
| 721 |
def process_other(in_path: str, member: tarfile.TarInfo) -> tuple[str, int, bool]: |
| 722 |
add_other_diff(diff, in_path, member) |
| 723 |
return container_path, mode, False |
| 724 |
|
| 725 |
return fetch_file_ex( |
| 726 |
client, |
| 727 |
container, |
| 728 |
in_path=container_path, |
| 729 |
process_none=process_none, |
| 730 |
process_regular=process_regular, |
| 731 |
process_symlink=process_symlink, |
| 732 |
process_other=process_other, |
| 733 |
follow_links=follow_links, |
| 734 |
) |
| 735 |
|
| 736 |
|
| 737 |
def copy_file_into_container( |
| 738 |
client: AnsibleDockerClient, |
| 739 |
container: str, |
| 740 |
managed_path: str, |
| 741 |
container_path: str, |
| 742 |
follow_links: bool, |
| 743 |
local_follow_links: bool, |
| 744 |
owner_id: int, |
| 745 |
group_id: int, |
| 746 |
mode: int | None, |
| 747 |
force: bool | None = False, |
| 748 |
do_diff: bool = False, |
| 749 |
max_file_size_for_diff: int = 1, |
| 750 |
) -> t.NoReturn: |
| 751 |
diff: dict[str, t.Any] | None |
| 752 |
diff = {} if do_diff else None |
| 753 |
|
| 754 |
container_path, mode, idempotent = is_file_idempotent( |
| 755 |
client, |
| 756 |
container, |
| 757 |
managed_path, |
| 758 |
container_path, |
| 759 |
follow_links, |
| 760 |
local_follow_links, |
| 761 |
owner_id, |
| 762 |
group_id, |
| 763 |
mode, |
| 764 |
force=force, |
| 765 |
diff=diff, |
| 766 |
max_file_size_for_diff=max_file_size_for_diff, |
| 767 |
) |
| 768 |
changed = not idempotent |
| 769 |
|
| 770 |
if changed and not client.module.check_mode: |
| 771 |
put_file( |
| 772 |
client, |
| 773 |
container, |
| 774 |
in_path=managed_path, |
| 775 |
out_path=container_path, |
| 776 |
user_id=owner_id, |
| 777 |
group_id=group_id, |
| 778 |
mode=mode, |
| 779 |
follow_links=local_follow_links, |
| 780 |
) |
| 781 |
|
| 782 |
result = { |
| 783 |
"container_path": container_path, |
| 784 |
"changed": changed, |
| 785 |
} |
| 786 |
if diff: |
| 787 |
result["diff"] = diff |
| 788 |
client.module.exit_json(**result) |
| 789 |
|
| 790 |
|
| 791 |
def is_content_idempotent( |
| 792 |
client: AnsibleDockerClient, |
| 793 |
container: str, |
| 794 |
content: bytes, |
| 795 |
container_path: str, |
| 796 |
follow_links: bool, |
| 797 |
owner_id: int, |
| 798 |
group_id: int, |
| 799 |
mode: int, |
| 800 |
force: bool | None = False, |
| 801 |
diff: dict[str, t.Any] | None = None, |
| 802 |
max_file_size_for_diff: int = 1, |
| 803 |
) -> tuple[str, int, bool]: |
| 804 |
if diff is not None: |
| 805 |
if len(content) > max_file_size_for_diff > 0: |
| 806 |
diff["src_larger"] = max_file_size_for_diff |
| 807 |
elif is_binary(content): |
| 808 |
diff["src_binary"] = 1 |
| 809 |
else: |
| 810 |
diff["after_header"] = "dynamically generated" |
| 811 |
diff["after"] = to_text(content) |
| 812 |
|
| 813 |
# When forcing and we are not following links in the container, go! |
| 814 |
if force and not follow_links: |
| 815 |
retrieve_diff( |
| 816 |
client, |
| 817 |
container, |
| 818 |
container_path, |
| 819 |
follow_links, |
| 820 |
diff, |
| 821 |
max_file_size_for_diff, |
| 822 |
) |
| 823 |
return container_path, mode, False |
| 824 |
|
| 825 |
# Resolve symlinks in the container (if requested), and get information on container's file |
| 826 |
real_container_path, regular_stat, link_target = stat_file( |
| 827 |
client, |
| 828 |
container, |
| 829 |
in_path=container_path, |
| 830 |
follow_links=follow_links, |
| 831 |
) |
| 832 |
|
| 833 |
# Follow links in the Docker container? |
| 834 |
if follow_links: |
| 835 |
container_path = real_container_path |
| 836 |
|
| 837 |
# If the file was not found, continue |
| 838 |
if regular_stat is None: |
| 839 |
if diff is not None: |
| 840 |
diff["before_header"] = container_path |
| 841 |
diff["before"] = "" |
| 842 |
return container_path, mode, False |
| 843 |
|
| 844 |
# When forcing, go! |
| 845 |
if force: |
| 846 |
retrieve_diff( |
| 847 |
client, |
| 848 |
container, |
| 849 |
container_path, |
| 850 |
follow_links, |
| 851 |
diff, |
| 852 |
max_file_size_for_diff, |
| 853 |
regular_stat, |
| 854 |
link_target, |
| 855 |
) |
| 856 |
return container_path, mode, False |
| 857 |
|
| 858 |
# If force is set to False, and the destination exists, assume there's nothing to do |
| 859 |
if force is False: |
| 860 |
retrieve_diff( |
| 861 |
client, |
| 862 |
container, |
| 863 |
container_path, |
| 864 |
follow_links, |
| 865 |
diff, |
| 866 |
max_file_size_for_diff, |
| 867 |
regular_stat, |
| 868 |
link_target, |
| 869 |
) |
| 870 |
copy_dst_to_src(diff) |
| 871 |
return container_path, mode, True |
| 872 |
|
| 873 |
# Basic idempotency checks |
| 874 |
if link_target is not None: |
| 875 |
retrieve_diff( |
| 876 |
client, |
| 877 |
container, |
| 878 |
container_path, |
| 879 |
follow_links, |
| 880 |
diff, |
| 881 |
max_file_size_for_diff, |
| 882 |
regular_stat, |
| 883 |
link_target, |
| 884 |
) |
| 885 |
return container_path, mode, False |
| 886 |
if is_container_file_not_regular_file(regular_stat): |
| 887 |
retrieve_diff( |
| 888 |
client, |
| 889 |
container, |
| 890 |
container_path, |
| 891 |
follow_links, |
| 892 |
diff, |
| 893 |
max_file_size_for_diff, |
| 894 |
regular_stat, |
| 895 |
link_target, |
| 896 |
) |
| 897 |
return container_path, mode, False |
| 898 |
if len(content) != regular_stat["size"]: |
| 899 |
retrieve_diff( |
| 900 |
client, |
| 901 |
container, |
| 902 |
container_path, |
| 903 |
follow_links, |
| 904 |
diff, |
| 905 |
max_file_size_for_diff, |
| 906 |
regular_stat, |
| 907 |
link_target, |
| 908 |
) |
| 909 |
return container_path, mode, False |
| 910 |
if mode != get_container_file_mode(regular_stat): |
| 911 |
retrieve_diff( |
| 912 |
client, |
| 913 |
container, |
| 914 |
container_path, |
| 915 |
follow_links, |
| 916 |
diff, |
| 917 |
max_file_size_for_diff, |
| 918 |
regular_stat, |
| 919 |
link_target, |
| 920 |
) |
| 921 |
return container_path, mode, False |
| 922 |
|
| 923 |
# Fetch file from container |
| 924 |
def process_none(in_path: str) -> tuple[str, int, bool]: |
| 925 |
if diff is not None: |
| 926 |
diff["before"] = "" |
| 927 |
return container_path, mode, False |
| 928 |
|
| 929 |
def process_regular( |
| 930 |
in_path: str, tar: tarfile.TarFile, member: tarfile.TarInfo |
| 931 |
) -> tuple[str, int, bool]: |
| 932 |
# Check things like user/group ID and mode |
| 933 |
if any( |
| 934 |
[ |
| 935 |
member.mode & 0xFFF != mode, |
| 936 |
member.uid != owner_id, |
| 937 |
member.gid != group_id, |
| 938 |
member.size != len(content), |
| 939 |
] |
| 940 |
): |
| 941 |
add_diff_dst_from_regular_member( |
| 942 |
diff, max_file_size_for_diff, in_path, tar, member |
| 943 |
) |
| 944 |
return container_path, mode, False |
| 945 |
|
| 946 |
mf = tar.extractfile(member) |
| 947 |
if mf is None: |
| 948 |
raise AssertionError("Member should be present for regular file") |
| 949 |
with mf as tar_f: |
| 950 |
is_equal = are_fileobjs_equal_with_diff_of_first( |
| 951 |
tar_f, |
| 952 |
io.BytesIO(content), |
| 953 |
member.size, |
| 954 |
diff, |
| 955 |
max_file_size_for_diff, |
| 956 |
in_path, |
| 957 |
) |
| 958 |
return container_path, mode, is_equal |
| 959 |
|
| 960 |
def process_symlink(in_path: str, member: tarfile.TarInfo) -> tuple[str, int, bool]: |
| 961 |
if diff is not None: |
| 962 |
diff["before_header"] = in_path |
| 963 |
diff["before"] = member.linkname |
| 964 |
|
| 965 |
return container_path, mode, False |
| 966 |
|
| 967 |
def process_other(in_path: str, member: tarfile.TarInfo) -> tuple[str, int, bool]: |
| 968 |
add_other_diff(diff, in_path, member) |
| 969 |
return container_path, mode, False |
| 970 |
|
| 971 |
return fetch_file_ex( |
| 972 |
client, |
| 973 |
container, |
| 974 |
in_path=container_path, |
| 975 |
process_none=process_none, |
| 976 |
process_regular=process_regular, |
| 977 |
process_symlink=process_symlink, |
| 978 |
process_other=process_other, |
| 979 |
follow_links=follow_links, |
| 980 |
) |
| 981 |
|
| 982 |
|
| 983 |
def copy_content_into_container( |
| 984 |
client: AnsibleDockerClient, |
| 985 |
container: str, |
| 986 |
content: bytes, |
| 987 |
container_path: str, |
| 988 |
follow_links: bool, |
| 989 |
owner_id: int, |
| 990 |
group_id: int, |
| 991 |
mode: int, |
| 992 |
force: bool | None = False, |
| 993 |
do_diff: bool = False, |
| 994 |
max_file_size_for_diff: int = 1, |
| 995 |
) -> t.NoReturn: |
| 996 |
diff: dict[str, t.Any] | None = {} if do_diff else None |
| 997 |
|
| 998 |
container_path, mode, idempotent = is_content_idempotent( |
| 999 |
client, |
| 1000 |
container, |
| 1001 |
content, |
| 1002 |
container_path, |
| 1003 |
follow_links, |
| 1004 |
owner_id, |
| 1005 |
group_id, |
| 1006 |
mode, |
| 1007 |
force=force, |
| 1008 |
diff=diff, |
| 1009 |
max_file_size_for_diff=max_file_size_for_diff, |
| 1010 |
) |
| 1011 |
changed = not idempotent |
| 1012 |
|
| 1013 |
if changed and not client.module.check_mode: |
| 1014 |
put_file_content( |
| 1015 |
client, |
| 1016 |
container, |
| 1017 |
content=content, |
| 1018 |
out_path=container_path, |
| 1019 |
user_id=owner_id, |
| 1020 |
group_id=group_id, |
| 1021 |
mode=mode, |
| 1022 |
) |
| 1023 |
|
| 1024 |
result = { |
| 1025 |
"container_path": container_path, |
| 1026 |
"changed": changed, |
| 1027 |
} |
| 1028 |
if diff: |
| 1029 |
# Since the content is no_log, make sure that the before/after strings look sufficiently different |
| 1030 |
key = generate_insecure_key() |
| 1031 |
diff["scrambled_diff"] = base64.b64encode(key) |
| 1032 |
for k in ("before", "after"): |
| 1033 |
if k in diff: |
| 1034 |
diff[k] = scramble(diff[k], key) |
| 1035 |
result["diff"] = diff |
| 1036 |
client.module.exit_json(**result) |
| 1037 |
|
| 1038 |
|
| 1039 |
def parse_modern(mode: str | int) -> int: |
| 1040 |
if isinstance(mode, str): |
| 1041 |
return int(to_text(mode), 8) |
| 1042 |
if isinstance(mode, int): |
| 1043 |
return mode |
| 1044 |
raise TypeError(f"must be an octal string or an integer, got {mode!r}") |
| 1045 |
|
| 1046 |
|
| 1047 |
def parse_octal_string_only(mode: str) -> int: |
| 1048 |
if isinstance(mode, str): |
| 1049 |
return int(to_text(mode), 8) |
| 1050 |
raise TypeError(f"must be an octal string, got {mode!r}") |
| 1051 |
|
| 1052 |
|
| 1053 |
def main() -> None: |
| 1054 |
argument_spec = { |
| 1055 |
"container": {"type": "str", "required": True}, |
| 1056 |
"path": {"type": "path"}, |
| 1057 |
"container_path": {"type": "str", "required": True}, |
| 1058 |
"follow": {"type": "bool", "default": False}, |
| 1059 |
"local_follow": {"type": "bool", "default": True}, |
| 1060 |
"owner_id": {"type": "int"}, |
| 1061 |
"group_id": {"type": "int"}, |
| 1062 |
"mode": {"type": "raw"}, |
| 1063 |
"mode_parse": { |
| 1064 |
"type": "str", |
| 1065 |
"choices": ["legacy", "modern", "octal_string_only"], |
| 1066 |
"default": "legacy", |
| 1067 |
}, |
| 1068 |
"force": {"type": "bool"}, |
| 1069 |
"content": {"type": "str", "no_log": True}, |
| 1070 |
"content_is_b64": {"type": "bool", "default": False}, |
| 1071 |
# Undocumented parameters for use by the action plugin |
| 1072 |
"_max_file_size_for_diff": {"type": "int"}, |
| 1073 |
} |
| 1074 |
|
| 1075 |
client = AnsibleDockerClient( |
| 1076 |
argument_spec=argument_spec, |
| 1077 |
min_docker_api_version="1.20", |
| 1078 |
supports_check_mode=True, |
| 1079 |
mutually_exclusive=[("path", "content")], |
| 1080 |
required_together=[("owner_id", "group_id")], |
| 1081 |
required_by={ |
| 1082 |
"content": ["mode"], |
| 1083 |
}, |
| 1084 |
) |
| 1085 |
|
| 1086 |
container: str = client.module.params["container"] |
| 1087 |
managed_path: str | None = client.module.params["path"] |
| 1088 |
container_path: str = client.module.params["container_path"] |
| 1089 |
follow: bool = client.module.params["follow"] |
| 1090 |
local_follow: bool = client.module.params["local_follow"] |
| 1091 |
owner_id: int | None = client.module.params["owner_id"] |
| 1092 |
group_id: int | None = client.module.params["group_id"] |
| 1093 |
mode: t.Any = client.module.params["mode"] |
| 1094 |
force: bool | None = client.module.params["force"] |
| 1095 |
content_str: str | None = client.module.params["content"] |
| 1096 |
max_file_size_for_diff: int = client.module.params["_max_file_size_for_diff"] or 1 |
| 1097 |
|
| 1098 |
if mode is not None: |
| 1099 |
mode_parse: t.Literal["legacy", "modern", "octal_string_only"] = ( |
| 1100 |
client.module.params["mode_parse"] |
| 1101 |
) |
| 1102 |
try: |
| 1103 |
if mode_parse == "legacy": |
| 1104 |
mode = check_type_int(mode) |
| 1105 |
elif mode_parse == "modern": |
| 1106 |
mode = parse_modern(mode) |
| 1107 |
elif mode_parse == "octal_string_only": |
| 1108 |
mode = parse_octal_string_only(mode) |
| 1109 |
except (TypeError, ValueError) as e: |
| 1110 |
client.fail(f"Error while parsing 'mode': {e}") |
| 1111 |
if mode < 0: |
| 1112 |
client.fail(f"'mode' must not be negative; got {mode}") |
| 1113 |
|
| 1114 |
content: bytes | None = None |
| 1115 |
if content_str is not None: |
| 1116 |
if client.module.params["content_is_b64"]: |
| 1117 |
try: |
| 1118 |
content = base64.b64decode(content_str) |
| 1119 |
except Exception as e: # pylint: disable=broad-exception-caught |
| 1120 |
client.fail(f"Cannot Base64 decode the content option: {e}") |
| 1121 |
else: |
| 1122 |
content = to_bytes(content_str) |
| 1123 |
|
| 1124 |
if not container_path.startswith(os.path.sep): |
| 1125 |
container_path = os.path.join(os.path.sep, container_path) |
| 1126 |
container_path = os.path.normpath(container_path) |
| 1127 |
|
| 1128 |
try: |
| 1129 |
if owner_id is None or group_id is None: |
| 1130 |
owner_id, group_id = determine_user_group(client, container) |
| 1131 |
|
| 1132 |
if content is not None: |
| 1133 |
assert mode is not None # see required_by above |
| 1134 |
copy_content_into_container( |
| 1135 |
client, |
| 1136 |
container, |
| 1137 |
content, |
| 1138 |
container_path, |
| 1139 |
follow_links=follow, |
| 1140 |
owner_id=owner_id, |
| 1141 |
group_id=group_id, |
| 1142 |
mode=mode, |
| 1143 |
force=force, |
| 1144 |
do_diff=client.module._diff, |
| 1145 |
max_file_size_for_diff=max_file_size_for_diff, |
| 1146 |
) |
| 1147 |
elif managed_path is not None: |
| 1148 |
copy_file_into_container( |
| 1149 |
client, |
| 1150 |
container, |
| 1151 |
managed_path, |
| 1152 |
container_path, |
| 1153 |
follow_links=follow, |
| 1154 |
local_follow_links=local_follow, |
| 1155 |
owner_id=owner_id, |
| 1156 |
group_id=group_id, |
| 1157 |
mode=mode, |
| 1158 |
force=force, |
| 1159 |
do_diff=client.module._diff, |
| 1160 |
max_file_size_for_diff=max_file_size_for_diff, |
| 1161 |
) |
| 1162 |
else: |
| 1163 |
# Can happen if a user explicitly passes `content: null` or `path: null`... |
| 1164 |
client.fail("One of path and content must be supplied") |
| 1165 |
except NotFound as exc: |
| 1166 |
client.fail(f'Could not find container "{container}" or resource in it ({exc})') |
| 1167 |
except APIError as exc: |
| 1168 |
client.fail( |
| 1169 |
f'An unexpected Docker error occurred for container "{container}": {exc}', |
| 1170 |
exception=traceback.format_exc(), |
| 1171 |
) |
| 1172 |
except DockerException as exc: |
| 1173 |
client.fail( |
| 1174 |
f'An unexpected Docker error occurred for container "{container}": {exc}', |
| 1175 |
exception=traceback.format_exc(), |
| 1176 |
) |
| 1177 |
except RequestException as exc: |
| 1178 |
client.fail( |
| 1179 |
f'An unexpected requests error occurred for container "{container}" when trying to talk to the Docker daemon: {exc}', |
| 1180 |
exception=traceback.format_exc(), |
| 1181 |
) |
| 1182 |
except DockerUnexpectedError as exc: |
| 1183 |
client.fail(f"Unexpected error: {exc}", exception=traceback.format_exc()) |
| 1184 |
except DockerFileCopyError as exc: |
| 1185 |
client.fail(to_text(exc)) |
| 1186 |
except OSError as exc: |
| 1187 |
client.fail(f"Unexpected error: {exc}", exception=traceback.format_exc()) |
| 1188 |
|
| 1189 |
|
| 1190 |
if __name__ == "__main__": |
| 1191 |
main() |