Python code modernization, 5/n (#1165)

* Address raise-missing-from.

* Address simplifiable-if-expression.

* Address unnecessary-dunder-call.

* Address unnecessary-pass.

* Address use-list-literal.

* Address unused-variable.

* Address use-dict-literal.
This commit is contained in:
Felix Fontein
2025-10-12 16:02:27 +02:00
committed by GitHub
parent cad22de628
commit c75aa5dd64
66 changed files with 1549 additions and 1429 deletions
+5 -3
View File
@@ -227,18 +227,20 @@ class APIClient(_Session, DaemonApiMixin):
try:
version_result = self.version(api_version=False)
except Exception as e:
raise DockerException(f"Error while fetching server API version: {e}")
raise DockerException(
f"Error while fetching server API version: {e}"
) from e
try:
return version_result["ApiVersion"]
except KeyError:
raise DockerException(
'Invalid response from docker daemon: key "ApiVersion" is missing.'
)
) from None
except Exception as e:
raise DockerException(
f"Error while fetching server API version: {e}. Response seems to be broken."
)
) from e
def _set_request_timeout(self, kwargs):
"""Prepare the kwargs for an HTTP request by inserting the timeout
-1
View File
@@ -370,7 +370,6 @@ def _load_legacy_config(config_file):
}
except Exception as e: # pylint: disable=broad-exception-caught
log.debug(e)
pass
log.debug("All parsing attempts failed - returning empty config")
return {}
+1 -1
View File
@@ -106,7 +106,7 @@ class Context:
self.tls_cfg[name] = tls_cfg
def inspect(self):
return self.__call__()
return self()
@classmethod
def load_context(cls, name):
+1 -1
View File
@@ -71,7 +71,7 @@ class TLSConfig:
except ValueError:
raise errors.TLSParameterError(
"client_cert must be a tuple of (client certificate, key file)"
)
) from None
if not (tls_cert and tls_key) or (
not os.path.isfile(tls_cert) or not os.path.isfile(tls_key)
@@ -52,16 +52,16 @@ class NpipeHTTPConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
try:
conn = self.pool.get(block=self.block, timeout=timeout)
except AttributeError: # self.pool is None
raise urllib3.exceptions.ClosedPoolError(self, "Pool is closed.")
except AttributeError as exc: # self.pool is None
raise urllib3.exceptions.ClosedPoolError(self, "Pool is closed.") from exc
except Empty:
except Empty as exc:
if self.block:
raise urllib3.exceptions.EmptyPoolError(
self,
"Pool reached maximum size and no more connections are allowed.",
)
pass # Oh well, we'll create a new connection then
) from exc
# Oh well, we'll create a new connection then
return conn or self._new_conn()
@@ -141,7 +141,7 @@ class NpipeSocket:
@check_closed
def recv(self, bufsize, flags=0):
err, data = win32file.ReadFile(self._handle, bufsize)
dummy_err, data = win32file.ReadFile(self._handle, bufsize)
return data
@check_closed
@@ -163,7 +163,7 @@ class NpipeSocket:
try:
overlapped = pywintypes.OVERLAPPED()
overlapped.hEvent = event
err, data = win32file.ReadFile(
dummy_err, dummy_data = win32file.ReadFile(
self._handle, readbuf[:nbytes] if nbytes else readbuf, overlapped
)
wait_result = win32event.WaitForSingleObject(event, self._timeout)
@@ -159,16 +159,16 @@ class SSHConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
try:
conn = self.pool.get(block=self.block, timeout=timeout)
except AttributeError: # self.pool is None
raise urllib3.exceptions.ClosedPoolError(self, "Pool is closed.")
except AttributeError as exc: # self.pool is None
raise urllib3.exceptions.ClosedPoolError(self, "Pool is closed.") from exc
except Empty:
except Empty as exc:
if self.block:
raise urllib3.exceptions.EmptyPoolError(
self,
"Pool reached maximum size and no more connections are allowed.",
)
pass # Oh well, we'll create a new connection then
) from exc
# Oh well, we'll create a new connection then
return conn or self._new_conn()
+4 -4
View File
@@ -39,10 +39,10 @@ class CancellableStream:
def __next__(self):
try:
return next(self._stream)
except urllib3.exceptions.ProtocolError:
raise StopIteration
except socket.error:
raise StopIteration
except urllib3.exceptions.ProtocolError as exc:
raise StopIteration from exc
except socket.error as exc:
raise StopIteration from exc
next = __next__
+2 -2
View File
@@ -107,8 +107,8 @@ def create_archive(root, files=None, fileobj=None, gzip=False, extra_files=None)
try:
with open(full_path, "rb") as f:
t.addfile(i, f)
except IOError:
raise IOError(f"Can not read file in context: {full_path}")
except IOError as exc:
raise IOError(f"Can not read file in context: {full_path}") from exc
else:
# Directories, FIFOs, symlinks... do not need to be read.
t.addfile(i, None)
@@ -85,4 +85,4 @@ def split_buffer(stream, splitter=None, decoder=lambda a: a):
try:
yield decoder(buffered)
except Exception as e:
raise StreamParseError(e)
raise StreamParseError(e) from e
+2 -2
View File
@@ -420,10 +420,10 @@ def parse_bytes(s):
if suffix in units or suffix.isdigit():
try:
digits = float(digits_part)
except ValueError:
except ValueError as exc:
raise errors.DockerException(
f"Failed converting the string value for memory ({digits_part}) to an integer."
)
) from exc
# Reconvert to long for the final result
s = int(digits * units[suffix])
+35 -35
View File
@@ -144,19 +144,19 @@ def get_connect_params(auth_data, fail_function):
"tcp://", "https://"
)
result = dict(
base_url=auth_data["docker_host"],
version=auth_data["api_version"],
timeout=auth_data["timeout"],
)
result = {
"base_url": auth_data["docker_host"],
"version": auth_data["api_version"],
"timeout": auth_data["timeout"],
}
if auth_data["tls_verify"]:
# TLS with verification
tls_config = dict(
verify=True,
assert_hostname=auth_data["tls_hostname"],
fail_function=fail_function,
)
tls_config = {
"verify": True,
"assert_hostname": auth_data["tls_hostname"],
"fail_function": fail_function,
}
if auth_data["cert_path"] and auth_data["key_path"]:
tls_config["client_cert"] = (auth_data["cert_path"], auth_data["key_path"])
if auth_data["cacert_path"]:
@@ -164,10 +164,10 @@ def get_connect_params(auth_data, fail_function):
result["tls"] = _get_tls_config(**tls_config)
elif auth_data["tls"]:
# TLS without verification
tls_config = dict(
verify=False,
fail_function=fail_function,
)
tls_config = {
"verify": False,
"fail_function": fail_function,
}
if auth_data["cert_path"] and auth_data["key_path"]:
tls_config["client_cert"] = (auth_data["cert_path"], auth_data["key_path"])
result["tls"] = _get_tls_config(**tls_config)
@@ -312,78 +312,78 @@ class AnsibleDockerClientBase(Client):
client_params = self._get_params()
params = dict()
params = {}
for key in DOCKER_COMMON_ARGS:
params[key] = client_params.get(key)
result = dict(
docker_host=self._get_value(
result = {
"docker_host": self._get_value(
"docker_host",
params["docker_host"],
"DOCKER_HOST",
DEFAULT_DOCKER_HOST,
value_type="str",
),
tls_hostname=self._get_value(
"tls_hostname": self._get_value(
"tls_hostname",
params["tls_hostname"],
"DOCKER_TLS_HOSTNAME",
None,
value_type="str",
),
api_version=self._get_value(
"api_version": self._get_value(
"api_version",
params["api_version"],
"DOCKER_API_VERSION",
"auto",
value_type="str",
),
cacert_path=self._get_value(
"cacert_path": self._get_value(
"cacert_path",
params["ca_path"],
"DOCKER_CERT_PATH",
None,
value_type="str",
),
cert_path=self._get_value(
"cert_path": self._get_value(
"cert_path",
params["client_cert"],
"DOCKER_CERT_PATH",
None,
value_type="str",
),
key_path=self._get_value(
"key_path": self._get_value(
"key_path",
params["client_key"],
"DOCKER_CERT_PATH",
None,
value_type="str",
),
tls=self._get_value(
"tls": self._get_value(
"tls", params["tls"], "DOCKER_TLS", DEFAULT_TLS, value_type="bool"
),
tls_verify=self._get_value(
"tls_verify": self._get_value(
"validate_certs",
params["validate_certs"],
"DOCKER_TLS_VERIFY",
DEFAULT_TLS_VERIFY,
value_type="bool",
),
timeout=self._get_value(
"timeout": self._get_value(
"timeout",
params["timeout"],
"DOCKER_TIMEOUT",
DEFAULT_TIMEOUT_SECONDS,
value_type="int",
),
use_ssh_client=self._get_value(
"use_ssh_client": self._get_value(
"use_ssh_client",
params["use_ssh_client"],
None,
False,
value_type="bool",
),
)
}
update_tls_hostname(result)
@@ -586,11 +586,11 @@ class AnsibleDockerClientBase(Client):
"""
Pull an image
"""
kwargs = dict(
tag=tag,
stream=True,
decode=True,
)
kwargs = {
"tag": tag,
"stream": True,
"decode": True,
}
if image_platform is not None:
kwargs["platform"] = image_platform
self.log(f"Pulling image {name}:{tag}")
@@ -654,7 +654,7 @@ class AnsibleDockerClient(AnsibleDockerClientBase):
# in case client.fail() is called.
self.fail_results = fail_results or {}
merged_arg_spec = dict()
merged_arg_spec = {}
merged_arg_spec.update(DOCKER_COMMON_ARGS)
if argument_spec:
merged_arg_spec.update(argument_spec)
@@ -706,12 +706,12 @@ class AnsibleDockerClient(AnsibleDockerClientBase):
return self.module.params
def _get_minimal_versions(self, option_minimal_versions, ignore_params=None):
self.option_minimal_versions = dict()
self.option_minimal_versions = {}
for option in self.module.argument_spec:
if ignore_params is not None:
if option in ignore_params:
continue
self.option_minimal_versions[option] = dict()
self.option_minimal_versions[option] = {}
self.option_minimal_versions.update(option_minimal_versions)
for option, data in self.option_minimal_versions.items():
+31 -31
View File
@@ -78,19 +78,19 @@ def get_connect_params(auth_data, fail_function):
"tcp://", "https://"
)
result = dict(
base_url=auth_data["docker_host"],
version=auth_data["api_version"],
timeout=auth_data["timeout"],
)
result = {
"base_url": auth_data["docker_host"],
"version": auth_data["api_version"],
"timeout": auth_data["timeout"],
}
if auth_data["tls_verify"]:
# TLS with verification
tls_config = dict(
verify=True,
assert_hostname=auth_data["tls_hostname"],
fail_function=fail_function,
)
tls_config = {
"verify": True,
"assert_hostname": auth_data["tls_hostname"],
"fail_function": fail_function,
}
if auth_data["cert_path"] and auth_data["key_path"]:
tls_config["client_cert"] = (auth_data["cert_path"], auth_data["key_path"])
if auth_data["cacert_path"]:
@@ -98,10 +98,10 @@ def get_connect_params(auth_data, fail_function):
result["tls"] = _get_tls_config(**tls_config)
elif auth_data["tls"]:
# TLS without verification
tls_config = dict(
verify=False,
fail_function=fail_function,
)
tls_config = {
"verify": False,
"fail_function": fail_function,
}
if auth_data["cert_path"] and auth_data["key_path"]:
tls_config["client_cert"] = (auth_data["cert_path"], auth_data["key_path"])
result["tls"] = _get_tls_config(**tls_config)
@@ -203,78 +203,78 @@ class AnsibleDockerClientBase(Client):
client_params = self._get_params()
params = dict()
params = {}
for key in DOCKER_COMMON_ARGS:
params[key] = client_params.get(key)
result = dict(
docker_host=self._get_value(
result = {
"docker_host": self._get_value(
"docker_host",
params["docker_host"],
"DOCKER_HOST",
DEFAULT_DOCKER_HOST,
value_type="str",
),
tls_hostname=self._get_value(
"tls_hostname": self._get_value(
"tls_hostname",
params["tls_hostname"],
"DOCKER_TLS_HOSTNAME",
None,
value_type="str",
),
api_version=self._get_value(
"api_version": self._get_value(
"api_version",
params["api_version"],
"DOCKER_API_VERSION",
"auto",
value_type="str",
),
cacert_path=self._get_value(
"cacert_path": self._get_value(
"cacert_path",
params["ca_path"],
"DOCKER_CERT_PATH",
None,
value_type="str",
),
cert_path=self._get_value(
"cert_path": self._get_value(
"cert_path",
params["client_cert"],
"DOCKER_CERT_PATH",
None,
value_type="str",
),
key_path=self._get_value(
"key_path": self._get_value(
"key_path",
params["client_key"],
"DOCKER_CERT_PATH",
None,
value_type="str",
),
tls=self._get_value(
"tls": self._get_value(
"tls", params["tls"], "DOCKER_TLS", DEFAULT_TLS, value_type="bool"
),
tls_verify=self._get_value(
"tls_verify": self._get_value(
"validate_certs",
params["validate_certs"],
"DOCKER_TLS_VERIFY",
DEFAULT_TLS_VERIFY,
value_type="bool",
),
timeout=self._get_value(
"timeout": self._get_value(
"timeout",
params["timeout"],
"DOCKER_TIMEOUT",
DEFAULT_TIMEOUT_SECONDS,
value_type="int",
),
use_ssh_client=self._get_value(
"use_ssh_client": self._get_value(
"use_ssh_client",
params["use_ssh_client"],
None,
False,
value_type="bool",
),
)
}
def depr(*args, **kwargs):
self.deprecate(*args, **kwargs)
@@ -504,7 +504,7 @@ class AnsibleDockerClientBase(Client):
old_tag = self.find_image(name, tag)
try:
repository, image_tag = parse_repository_tag(name)
registry, repo_name = auth.resolve_repository_name(repository)
registry, dummy_repo_name = auth.resolve_repository_name(repository)
params = {
"tag": tag or image_tag or "latest",
"fromImage": repository,
@@ -564,7 +564,7 @@ class AnsibleDockerClient(AnsibleDockerClientBase):
# in case client.fail() is called.
self.fail_results = fail_results or {}
merged_arg_spec = dict()
merged_arg_spec = {}
merged_arg_spec.update(DOCKER_COMMON_ARGS)
if argument_spec:
merged_arg_spec.update(argument_spec)
@@ -613,12 +613,12 @@ class AnsibleDockerClient(AnsibleDockerClientBase):
return self.module.params
def _get_minimal_versions(self, option_minimal_versions, ignore_params=None):
self.option_minimal_versions = dict()
self.option_minimal_versions = {}
for option in self.module.argument_spec:
if ignore_params is not None:
if option in ignore_params:
continue
self.option_minimal_versions[option] = dict()
self.option_minimal_versions[option] = {}
self.option_minimal_versions.update(option_minimal_versions)
for option, data in self.option_minimal_versions.items():
+35 -26
View File
@@ -31,31 +31,40 @@ from ansible_collections.community.docker.plugins.module_utils._version import (
)
DOCKER_COMMON_ARGS = dict(
docker_cli=dict(type="path"),
docker_host=dict(
type="str", fallback=(env_fallback, ["DOCKER_HOST"]), aliases=["docker_url"]
),
tls_hostname=dict(type="str", fallback=(env_fallback, ["DOCKER_TLS_HOSTNAME"])),
api_version=dict(
type="str",
default="auto",
fallback=(env_fallback, ["DOCKER_API_VERSION"]),
aliases=["docker_api_version"],
),
ca_path=dict(type="path", aliases=["ca_cert", "tls_ca_cert", "cacert_path"]),
client_cert=dict(type="path", aliases=["tls_client_cert", "cert_path"]),
client_key=dict(type="path", aliases=["tls_client_key", "key_path"]),
tls=dict(type="bool", default=DEFAULT_TLS, fallback=(env_fallback, ["DOCKER_TLS"])),
validate_certs=dict(
type="bool",
default=DEFAULT_TLS_VERIFY,
fallback=(env_fallback, ["DOCKER_TLS_VERIFY"]),
aliases=["tls_verify"],
),
# debug=dict(type='bool', default=False),
cli_context=dict(type="str"),
)
DOCKER_COMMON_ARGS = {
"docker_cli": {"type": "path"},
"docker_host": {
"type": "str",
"fallback": (env_fallback, ["DOCKER_HOST"]),
"aliases": ["docker_url"],
},
"tls_hostname": {
"type": "str",
"fallback": (env_fallback, ["DOCKER_TLS_HOSTNAME"]),
},
"api_version": {
"type": "str",
"default": "auto",
"fallback": (env_fallback, ["DOCKER_API_VERSION"]),
"aliases": ["docker_api_version"],
},
"ca_path": {"type": "path", "aliases": ["ca_cert", "tls_ca_cert", "cacert_path"]},
"client_cert": {"type": "path", "aliases": ["tls_client_cert", "cert_path"]},
"client_key": {"type": "path", "aliases": ["tls_client_key", "key_path"]},
"tls": {
"type": "bool",
"default": DEFAULT_TLS,
"fallback": (env_fallback, ["DOCKER_TLS"]),
},
"validate_certs": {
"type": "bool",
"default": DEFAULT_TLS_VERIFY,
"fallback": (env_fallback, ["DOCKER_TLS_VERIFY"]),
"aliases": ["tls_verify"],
},
# "debug": {"type": "bool", "default: False},
"cli_context": {"type": "str"},
}
class DockerException(Exception):
@@ -327,7 +336,7 @@ class AnsibleModuleDockerClient(AnsibleDockerClientBase):
# in case client.fail() is called.
self.fail_results = fail_results or {}
merged_arg_spec = dict()
merged_arg_spec = {}
merged_arg_spec.update(DOCKER_COMMON_ARGS)
if argument_spec:
merged_arg_spec.update(argument_spec)
+18 -18
View File
@@ -691,28 +691,28 @@ def update_failed(result, events, args, stdout, stderr, rc, cli):
def common_compose_argspec():
return dict(
project_src=dict(type="path"),
project_name=dict(type="str"),
files=dict(type="list", elements="path"),
definition=dict(type="dict"),
env_files=dict(type="list", elements="path"),
profiles=dict(type="list", elements="str"),
check_files_existing=dict(type="bool", default=True),
)
return {
"project_src": {"type": "path"},
"project_name": {"type": "str"},
"files": {"type": "list", "elements": "path"},
"definition": {"type": "dict"},
"env_files": {"type": "list", "elements": "path"},
"profiles": {"type": "list", "elements": "str"},
"check_files_existing": {"type": "bool", "default": True},
}
def common_compose_argspec_ex():
return dict(
argspec=common_compose_argspec(),
mutually_exclusive=[("definition", "project_src"), ("definition", "files")],
required_one_of=[
return {
"argspec": common_compose_argspec(),
"mutually_exclusive": [("definition", "project_src"), ("definition", "files")],
"required_one_of": [
("definition", "project_src"),
],
required_by={
"required_by": {
"definition": ("project_name",),
},
)
}
def combine_binary_output(*outputs):
@@ -794,7 +794,7 @@ class BaseComposeManager(DockerBaseClass):
)
def get_compose_version_from_cli(self):
rc, version_info, stderr = self.client.call_cli(
rc, version_info, dummy_stderr = self.client.call_cli(
"compose", "version", "--format", "json"
)
if rc:
@@ -853,7 +853,7 @@ class BaseComposeManager(DockerBaseClass):
if self.compose_version >= LooseVersion("2.23.0"):
# https://github.com/docker/compose/pull/11038
args.append("--no-trunc")
kwargs = dict(cwd=self.project_src, check_rc=not self.use_json_events)
kwargs = {"cwd": self.project_src, "check_rc": not self.use_json_events}
if self.compose_version >= LooseVersion("2.21.0"):
# Breaking change in 2.21.0: https://github.com/docker/compose/pull/10918
rc, containers, stderr = self.client.call_cli_json_stream(*args, **kwargs)
@@ -882,7 +882,7 @@ class BaseComposeManager(DockerBaseClass):
def list_images(self):
args = self.get_base_args() + ["images", "--format", "json"]
kwargs = dict(cwd=self.project_src, check_rc=not self.use_json_events)
kwargs = {"cwd": self.project_src, "check_rc": not self.use_json_events}
rc, images, stderr = self.client.call_cli_json(*args, **kwargs)
if self.use_json_events and rc != 0:
self._handle_failed_cli_call(args, rc, images, stderr)
+4 -4
View File
@@ -285,7 +285,7 @@ def stat_file(client, container, in_path, follow_links=False, log=None):
except Exception as exc:
raise DockerUnexpectedError(
f"When retrieving information for {in_path} from {container}, obtained header {header!r} that cannot be loaded as JSON: {exc}"
)
) from exc
# https://pkg.go.dev/io/fs#FileMode: bit 32 - 5 means ModeSymlink
if stat_data["mode"] & (1 << (32 - 5)) != 0:
@@ -500,7 +500,7 @@ def _execute_command(client, container, command, log=None, check_rc=False):
def determine_user_group(client, container, log=None):
dummy, stdout, stderr = _execute_command(
dummy_rc, stdout, dummy_stderr = _execute_command(
client, container, ["/bin/sh", "-c", "id -u && id -g"], check_rc=True, log=log
)
@@ -513,7 +513,7 @@ def determine_user_group(client, container, log=None):
user_id, group_id = stdout_lines
try:
return int(user_id), int(group_id)
except ValueError:
except ValueError as exc:
raise DockerUnexpectedError(
f'Expected two-line output with numeric IDs to obtain user and group ID for container {container}, but got "{user_id}" and "{group_id}" instead'
)
) from exc
+2 -2
View File
@@ -96,14 +96,14 @@ class _Parser:
if self.line[self.index : self.index + 2] != "\\u":
raise InvalidLogFmt("Invalid unicode escape start")
v = 0
for i in range(self.index + 2, self.index + 6):
for dummy_index in range(self.index + 2, self.index + 6):
v <<= 4
try:
v += _HEX_DICT[self.line[self.index]]
except KeyError:
raise InvalidLogFmt(
f"Invalid unicode escape digit {self.line[self.index]!r}"
)
) from None
self.index += 6
return chr(v)
+96 -88
View File
@@ -37,21 +37,21 @@ _DEFAULT_IP_REPLACEMENT_STRING = (
)
_MOUNT_OPTION_TYPES = dict(
create_mountpoint=("bind",),
labels=("volume",),
no_copy=("volume",),
non_recursive=("bind",),
propagation=("bind",),
read_only_force_recursive=("bind",),
read_only_non_recursive=("bind",),
subpath=("volume", "image"),
tmpfs_size=("tmpfs",),
tmpfs_mode=("tmpfs",),
tmpfs_options=("tmpfs",),
volume_driver=("volume",),
volume_options=("volume",),
)
_MOUNT_OPTION_TYPES = {
"create_mountpoint": ("bind",),
"labels": ("volume",),
"no_copy": ("volume",),
"non_recursive": ("bind",),
"propagation": ("bind",),
"read_only_force_recursive": ("bind",),
"read_only_non_recursive": ("bind",),
"subpath": ("volume", "image"),
"tmpfs_size": ("tmpfs",),
"tmpfs_mode": ("tmpfs",),
"tmpfs_options": ("tmpfs",),
"volume_driver": ("volume",),
"volume_options": ("volume",),
}
def _get_ansible_type(value_type):
@@ -629,7 +629,7 @@ def _preprocess_ulimits(module, values):
return values
result = []
for limit in values["ulimits"]:
limits = dict()
limits = {}
pieces = limit.split(":")
if len(pieces) >= 2:
limits["Name"] = pieces[0]
@@ -644,7 +644,7 @@ def _preprocess_ulimits(module, values):
def _preprocess_mounts(module, values):
last = dict()
last = {}
def check_collision(t, name):
if t in last:
@@ -970,53 +970,53 @@ OPTION_DEVICE_READ_BPS = OptionGroup().add_option(
"device_read_bps",
value_type="set",
elements="dict",
ansible_suboptions=dict(
path=dict(required=True, type="str"),
rate=dict(required=True, type="str"),
),
ansible_suboptions={
"path": {"type": "str", "required": True},
"rate": {"type": "str", "required": True},
},
)
OPTION_DEVICE_WRITE_BPS = OptionGroup().add_option(
"device_write_bps",
value_type="set",
elements="dict",
ansible_suboptions=dict(
path=dict(required=True, type="str"),
rate=dict(required=True, type="str"),
),
ansible_suboptions={
"path": {"type": "str", "required": True},
"rate": {"type": "str", "required": True},
},
)
OPTION_DEVICE_READ_IOPS = OptionGroup().add_option(
"device_read_iops",
value_type="set",
elements="dict",
ansible_suboptions=dict(
path=dict(required=True, type="str"),
rate=dict(required=True, type="int"),
),
ansible_suboptions={
"path": {"type": "str", "required": True},
"rate": {"type": "int", "required": True},
},
)
OPTION_DEVICE_WRITE_IOPS = OptionGroup().add_option(
"device_write_iops",
value_type="set",
elements="dict",
ansible_suboptions=dict(
path=dict(required=True, type="str"),
rate=dict(required=True, type="int"),
),
ansible_suboptions={
"path": {"type": "str", "required": True},
"rate": {"type": "int", "required": True},
},
)
OPTION_DEVICE_REQUESTS = OptionGroup().add_option(
"device_requests",
value_type="set",
elements="dict",
ansible_suboptions=dict(
capabilities=dict(type="list", elements="list"),
count=dict(type="int"),
device_ids=dict(type="list", elements="str"),
driver=dict(type="str"),
options=dict(type="dict"),
),
ansible_suboptions={
"capabilities": {"type": "list", "elements": "list"},
"count": {"type": "int"},
"device_ids": {"type": "list", "elements": "str"},
"driver": {"type": "str"},
"options": {"type": "dict"},
},
)
OPTION_DEVICE_CGROUP_RULES = OptionGroup().add_option(
@@ -1066,15 +1066,15 @@ OPTION_GROUPS = OptionGroup().add_option("groups", value_type="set", elements="s
OPTION_HEALTHCHECK = OptionGroup(preprocess=_preprocess_healthcheck).add_option(
"healthcheck",
value_type="dict",
ansible_suboptions=dict(
test=dict(type="raw"),
test_cli_compatible=dict(type="bool", default=False),
interval=dict(type="str"),
timeout=dict(type="str"),
start_period=dict(type="str"),
start_interval=dict(type="str"),
retries=dict(type="int"),
),
ansible_suboptions={
"test": {"type": "raw"},
"test_cli_compatible": {"type": "bool", "default": False},
"interval": {"type": "str"},
"timeout": {"type": "str"},
"start_period": {"type": "str"},
"start_interval": {"type": "str"},
"retries": {"type": "int"},
},
)
OPTION_HOSTNAME = OptionGroup().add_option("hostname", value_type="str")
@@ -1143,16 +1143,16 @@ OPTION_NETWORK = (
"networks",
value_type="set",
elements="dict",
ansible_suboptions=dict(
name=dict(type="str", required=True),
ipv4_address=dict(type="str"),
ipv6_address=dict(type="str"),
aliases=dict(type="list", elements="str"),
links=dict(type="list", elements="str"),
mac_address=dict(type="str"),
driver_opts=dict(type="dict"),
gw_priority=dict(type="int"),
),
ansible_suboptions={
"name": {"type": "str", "required": True},
"ipv4_address": {"type": "str"},
"ipv6_address": {"type": "str"},
"aliases": {"type": "list", "elements": "str"},
"links": {"type": "list", "elements": "str"},
"mac_address": {"type": "str"},
"driver_opts": {"type": "dict"},
"gw_priority": {"type": "int"},
},
)
)
@@ -1232,35 +1232,43 @@ OPTION_MOUNTS_VOLUMES = (
"mounts",
value_type="set",
elements="dict",
ansible_suboptions=dict(
target=dict(type="str", required=True),
source=dict(type="str"),
type=dict(
type="str",
choices=["bind", "volume", "tmpfs", "npipe", "cluster", "image"],
default="volume",
),
read_only=dict(type="bool"),
consistency=dict(
type="str", choices=["default", "consistent", "cached", "delegated"]
),
propagation=dict(
type="str",
choices=["private", "rprivate", "shared", "rshared", "slave", "rslave"],
),
no_copy=dict(type="bool"),
labels=dict(type="dict"),
volume_driver=dict(type="str"),
volume_options=dict(type="dict"),
tmpfs_size=dict(type="str"),
tmpfs_mode=dict(type="str"),
non_recursive=dict(type="bool"),
create_mountpoint=dict(type="bool"),
read_only_non_recursive=dict(type="bool"),
read_only_force_recursive=dict(type="bool"),
subpath=dict(type="str"),
tmpfs_options=dict(type="list", elements="dict"),
),
ansible_suboptions={
"target": {"type": "str", "required": True},
"source": {"type": "str"},
"type": {
"type": "str",
"choices": ["bind", "volume", "tmpfs", "npipe", "cluster", "image"],
"default": "volume",
},
"read_only": {"type": "bool"},
"consistency": {
"type": "str",
"choices": ["default", "consistent", "cached", "delegated"],
},
"propagation": {
"type": "str",
"choices": [
"private",
"rprivate",
"shared",
"rshared",
"slave",
"rslave",
],
},
"no_copy": {"type": "bool"},
"labels": {"type": "dict"},
"volume_driver": {"type": "str"},
"volume_options": {"type": "dict"},
"tmpfs_size": {"type": "str"},
"tmpfs_mode": {"type": "str"},
"non_recursive": {"type": "bool"},
"create_mountpoint": {"type": "bool"},
"read_only_non_recursive": {"type": "bool"},
"read_only_force_recursive": {"type": "bool"},
"subpath": {"type": "str"},
"tmpfs_options": {"type": "list", "elements": "dict"},
},
)
.add_option("volumes", value_type="set", elements="str")
.add_option(
@@ -398,13 +398,15 @@ class DockerAPIEngineDriver(EngineDriver):
# New docker daemon versions do not allow containers to be removed
# if they are paused. Make sure we do not end up in an infinite loop.
if count == 3:
raise RuntimeError(f"{exc} [tried to unpause three times]")
raise RuntimeError(
f"{exc} [tried to unpause three times]"
) from exc
count += 1
# Unpause
try:
self.unpause_container(client, container_id)
except Exception as exc2:
raise RuntimeError(f"{exc2} [while unpausing]")
raise RuntimeError(f"{exc2} [while unpausing]") from exc2
# Now try again
continue
raise
@@ -429,13 +431,15 @@ class DockerAPIEngineDriver(EngineDriver):
# New docker daemon versions do not allow containers to be removed
# if they are paused. Make sure we do not end up in an infinite loop.
if count == 3:
raise RuntimeError(f"{exc} [tried to unpause three times]")
raise RuntimeError(
f"{exc} [tried to unpause three times]"
) from exc
count += 1
# Unpause
try:
self.unpause_container(client, container_id)
except Exception as exc2:
raise RuntimeError(f"{exc2} [while unpausing]")
raise RuntimeError(f"{exc2} [while unpausing]") from exc2
# Now try again
continue
if (
@@ -817,28 +821,28 @@ def _preprocess_devices(module, client, api_version, value):
parts = device.split(":")
if len(parts) == 1:
expected_devices.append(
dict(
CgroupPermissions="rwm",
PathInContainer=parts[0],
PathOnHost=parts[0],
)
{
"CgroupPermissions": "rwm",
"PathInContainer": parts[0],
"PathOnHost": parts[0],
}
)
elif len(parts) == 2:
parts = device.split(":")
expected_devices.append(
dict(
CgroupPermissions="rwm",
PathInContainer=parts[1],
PathOnHost=parts[0],
)
{
"CgroupPermissions": "rwm",
"PathInContainer": parts[1],
"PathOnHost": parts[0],
}
)
else:
expected_devices.append(
dict(
CgroupPermissions=parts[2],
PathInContainer=parts[1],
PathOnHost=parts[0],
)
{
"CgroupPermissions": parts[2],
"PathInContainer": parts[1],
"PathOnHost": parts[0],
}
)
return expected_devices
@@ -1186,7 +1190,7 @@ def _get_expected_values_mounts(
expected_values["mounts"] = values["mounts"]
# volumes
expected_vols = dict()
expected_vols = {}
if image and image["Config"].get("Volumes"):
expected_vols.update(image["Config"].get("Volumes"))
if "volumes" in values:
@@ -1400,9 +1404,7 @@ def _get_values_ports(module, container, api_version, options, image, host_info)
# "ExposedPorts": null returns None type & causes AttributeError - PR #5517
if config.get("ExposedPorts") is not None:
expected_exposed = [
_normalize_port(p) for p in config.get("ExposedPorts", dict()).keys()
]
expected_exposed = [_normalize_port(p) for p in config.get("ExposedPorts", {})]
else:
expected_exposed = []
+113 -104
View File
@@ -42,7 +42,7 @@ class Container(DockerBaseClass):
@property
def exists(self):
return True if self.container else False
return bool(self.container)
@property
def removing(self):
@@ -232,17 +232,17 @@ class ContainerManager(DockerBaseClass):
"name"
]
if self.param_container_default_behavior == "compatibility":
old_default_values = dict(
auto_remove=False,
detach=True,
init=False,
interactive=False,
memory="0",
paused=False,
privileged=False,
read_only=False,
tty=False,
)
old_default_values = {
"auto_remove": False,
"detach": True,
"init": False,
"interactive": False,
"memory": "0",
"paused": False,
"privileged": False,
"read_only": False,
"tty": False,
}
for param, value in old_default_values.items():
if self.module.params[param] is None:
self.module.params[param] = value
@@ -487,7 +487,7 @@ class ContainerManager(DockerBaseClass):
)
container = self._get_container(container.id)
self.results["changed"] = True
self.results["actions"].append(dict(set_paused=self.param_paused))
self.results["actions"].append({"set_paused": self.param_paused})
self.facts = container.raw
@@ -577,19 +577,19 @@ class ContainerManager(DockerBaseClass):
if already_to_latest:
self.results["changed"] = False
self.results["actions"].append(
dict(pulled_image=f"{repository}:{tag}", changed=False)
{"pulled_image": f"{repository}:{tag}", "changed": False}
)
else:
self.results["changed"] = True
self.results["actions"].append(
dict(pulled_image=f"{repository}:{tag}", changed=True)
{"pulled_image": f"{repository}:{tag}", "changed": True}
)
elif not image or self.param_pull_check_mode_behavior == "always":
# If the image is not there, or pull_check_mode_behavior == 'always', claim we'll
# pull. (Implicitly: if the image is there, claim it already was latest unless
# pull_check_mode_behavior == 'always'.)
self.results["changed"] = True
action = dict(pulled_image=f"{repository}:{tag}")
action = {"pulled_image": f"{repository}:{tag}"}
if not image:
action["changed"] = True
self.results["actions"].append(action)
@@ -817,7 +817,7 @@ class ContainerManager(DockerBaseClass):
network_info = connected_networks.get(network["name"])
if network_info is None:
different = True
differences.append(dict(parameter=network, container=None))
differences.append({"parameter": network, "container": None})
else:
diff = False
network_info_ipam = network_info.get("IPAMConfig") or {}
@@ -855,17 +855,17 @@ class ContainerManager(DockerBaseClass):
if diff:
different = True
differences.append(
dict(
parameter=network,
container=dict(
name=network["name"],
ipv4_address=network_info_ipam.get("IPv4Address"),
ipv6_address=network_info_ipam.get("IPv6Address"),
aliases=network_info.get("Aliases"),
links=network_info.get("Links"),
mac_address=network_info.get("MacAddress"),
),
)
{
"parameter": network,
"container": {
"name": network["name"],
"ipv4_address": network_info_ipam.get("IPv4Address"),
"ipv6_address": network_info_ipam.get("IPv6Address"),
"aliases": network_info.get("Aliases"),
"links": network_info.get("Links"),
"mac_address": network_info.get("MacAddress"),
},
}
)
return different, differences
@@ -892,7 +892,7 @@ class ContainerManager(DockerBaseClass):
if not keep:
extra = True
extra_networks.append(
dict(name=network, id=network_config["NetworkID"])
{"name": network, "id": network_config["NetworkID"]}
)
return extra, extra_networks
@@ -905,11 +905,11 @@ class ContainerManager(DockerBaseClass):
if has_network_differences:
if self.diff.get("differences"):
self.diff["differences"].append(
dict(network_differences=network_differences)
{"network_differences": network_differences}
)
else:
self.diff["differences"] = [
dict(network_differences=network_differences)
{"network_differences": network_differences}
]
for netdiff in network_differences:
self.diff_tracker.add(
@@ -928,9 +928,9 @@ class ContainerManager(DockerBaseClass):
has_extra_networks, extra_networks = self.has_extra_networks(container)
if has_extra_networks:
if self.diff.get("differences"):
self.diff["differences"].append(dict(purge_networks=extra_networks))
self.diff["differences"].append({"purge_networks": extra_networks})
else:
self.diff["differences"] = [dict(purge_networks=extra_networks)]
self.diff["differences"] = [{"purge_networks": extra_networks}]
for extra_network in extra_networks:
self.diff_tracker.add(
f"network.{extra_network['name']}", active=extra_network
@@ -944,7 +944,7 @@ class ContainerManager(DockerBaseClass):
# remove the container from the network, if connected
if diff.get("container"):
self.results["actions"].append(
dict(removed_from_network=diff["parameter"]["name"])
{"removed_from_network": diff["parameter"]["name"]}
)
if not self.check_mode:
try:
@@ -957,10 +957,10 @@ class ContainerManager(DockerBaseClass):
)
# connect to the network
self.results["actions"].append(
dict(
added_to_network=diff["parameter"]["name"],
network_parameters=diff["parameter"],
)
{
"added_to_network": diff["parameter"]["name"],
"network_parameters": diff["parameter"],
}
)
if not self.check_mode:
params = {
@@ -984,7 +984,7 @@ class ContainerManager(DockerBaseClass):
def _purge_networks(self, container, networks):
for network in networks:
self.results["actions"].append(dict(removed_from_network=network["name"]))
self.results["actions"].append({"removed_from_network": network["name"]})
if not self.check_mode:
try:
self.engine_driver.disconnect_container_from_network(
@@ -1015,11 +1015,11 @@ class ContainerManager(DockerBaseClass):
if key not in ("name", "id")
}
self.results["actions"].append(
dict(
created="Created container",
create_parameters=create_parameters,
networks=networks,
)
{
"created": "Created container",
"create_parameters": create_parameters,
"networks": networks,
}
)
self.results["changed"] = True
new_container = None
@@ -1035,7 +1035,7 @@ class ContainerManager(DockerBaseClass):
def container_start(self, container_id):
self.log(f"start container {container_id}")
self.results["actions"].append(dict(started=container_id))
self.results["actions"].append({"started": container_id})
self.results["changed"] = True
if not self.check_mode:
try:
@@ -1069,7 +1069,7 @@ class ContainerManager(DockerBaseClass):
if insp.raw:
insp.raw["Output"] = output
else:
insp.raw = dict(Output=output)
insp.raw = {"Output": output}
if status != 0:
# Set `failed` to True and return output as msg
self.results["failed"] = True
@@ -1083,9 +1083,12 @@ class ContainerManager(DockerBaseClass):
f"remove container container:{container_id} v:{volume_state} link:{link} force{force}"
)
self.results["actions"].append(
dict(
removed=container_id, volume_state=volume_state, link=link, force=force
)
{
"removed": container_id,
"volume_state": volume_state,
"link": link,
"force": force,
}
)
self.results["changed"] = True
if not self.check_mode:
@@ -1105,7 +1108,7 @@ class ContainerManager(DockerBaseClass):
self.log(f"update container {container_id}")
self.log(update_parameters, pretty_print=True)
self.results["actions"].append(
dict(updated=container_id, update_parameters=update_parameters)
{"updated": container_id, "update_parameters": update_parameters}
)
self.results["changed"] = True
if not self.check_mode:
@@ -1119,7 +1122,7 @@ class ContainerManager(DockerBaseClass):
def container_kill(self, container_id):
self.results["actions"].append(
dict(killed=container_id, signal=self.param_kill_signal)
{"killed": container_id, "signal": self.param_kill_signal}
)
self.results["changed"] = True
if not self.check_mode:
@@ -1132,7 +1135,7 @@ class ContainerManager(DockerBaseClass):
def container_restart(self, container_id):
self.results["actions"].append(
dict(restarted=container_id, timeout=self.module.params["stop_timeout"])
{"restarted": container_id, "timeout": self.module.params["stop_timeout"]}
)
self.results["changed"] = True
if not self.check_mode:
@@ -1149,7 +1152,7 @@ class ContainerManager(DockerBaseClass):
self.container_kill(container_id)
return
self.results["actions"].append(
dict(stopped=container_id, timeout=self.module.params["stop_timeout"])
{"stopped": container_id, "timeout": self.module.params["stop_timeout"]}
)
self.results["changed"] = True
if not self.check_mode:
@@ -1163,57 +1166,63 @@ class ContainerManager(DockerBaseClass):
def run_module(engine_driver):
module, active_options, client = engine_driver.setup(
argument_spec=dict(
cleanup=dict(type="bool", default=False),
comparisons=dict(type="dict"),
container_default_behavior=dict(
type="str",
default="no_defaults",
choices=["compatibility", "no_defaults"],
),
command_handling=dict(
type="str", choices=["compatibility", "correct"], default="correct"
),
default_host_ip=dict(type="str"),
force_kill=dict(type="bool", default=False, aliases=["forcekill"]),
image=dict(type="str"),
image_comparison=dict(
type="str",
choices=["desired-image", "current-image"],
default="desired-image",
),
image_label_mismatch=dict(
type="str", choices=["ignore", "fail"], default="ignore"
),
image_name_mismatch=dict(
type="str", choices=["ignore", "recreate"], default="recreate"
),
keep_volumes=dict(type="bool", default=True),
kill_signal=dict(type="str"),
name=dict(type="str", required=True),
networks_cli_compatible=dict(type="bool", default=True),
output_logs=dict(type="bool", default=False),
paused=dict(type="bool"),
pull=dict(
type="raw",
choices=["never", "missing", "always", True, False],
default="missing",
),
pull_check_mode_behavior=dict(
type="str",
choices=["image_not_present", "always"],
default="image_not_present",
),
recreate=dict(type="bool", default=False),
removal_wait_timeout=dict(type="float"),
restart=dict(type="bool", default=False),
state=dict(
type="str",
default="started",
choices=["absent", "present", "healthy", "started", "stopped"],
),
healthy_wait_timeout=dict(type="float", default=300),
),
argument_spec={
"cleanup": {"type": "bool", "default": False},
"comparisons": {"type": "dict"},
"container_default_behavior": {
"type": "str",
"default": "no_defaults",
"choices": ["compatibility", "no_defaults"],
},
"command_handling": {
"type": "str",
"choices": ["compatibility", "correct"],
"default": "correct",
},
"default_host_ip": {"type": "str"},
"force_kill": {"type": "bool", "default": False, "aliases": ["forcekill"]},
"image": {"type": "str"},
"image_comparison": {
"type": "str",
"choices": ["desired-image", "current-image"],
"default": "desired-image",
},
"image_label_mismatch": {
"type": "str",
"choices": ["ignore", "fail"],
"default": "ignore",
},
"image_name_mismatch": {
"type": "str",
"choices": ["ignore", "recreate"],
"default": "recreate",
},
"keep_volumes": {"type": "bool", "default": True},
"kill_signal": {"type": "str"},
"name": {"type": "str", "required": True},
"networks_cli_compatible": {"type": "bool", "default": True},
"output_logs": {"type": "bool", "default": False},
"paused": {"type": "bool"},
"pull": {
"type": "raw",
"choices": ["never", "missing", "always", True, False],
"default": "missing",
},
"pull_check_mode_behavior": {
"type": "str",
"choices": ["image_not_present", "always"],
"default": "image_not_present",
},
"recreate": {"type": "bool", "default": False},
"removal_wait_timeout": {"type": "float"},
"restart": {"type": "bool", "default": False},
"state": {
"type": "str",
"default": "started",
"choices": ["absent", "present", "healthy", "started", "stopped"],
},
"healthy_wait_timeout": {"type": "float", "default": 300},
},
required_if=[
("state", "present", ["image"]),
],
+55 -48
View File
@@ -23,38 +23,45 @@ DEFAULT_TLS_VERIFY = False
DEFAULT_TLS_HOSTNAME = "localhost" # deprecated
DEFAULT_TIMEOUT_SECONDS = 60
DOCKER_COMMON_ARGS = dict(
docker_host=dict(
type="str",
default=DEFAULT_DOCKER_HOST,
fallback=(env_fallback, ["DOCKER_HOST"]),
aliases=["docker_url"],
),
tls_hostname=dict(type="str", fallback=(env_fallback, ["DOCKER_TLS_HOSTNAME"])),
api_version=dict(
type="str",
default="auto",
fallback=(env_fallback, ["DOCKER_API_VERSION"]),
aliases=["docker_api_version"],
),
timeout=dict(
type="int",
default=DEFAULT_TIMEOUT_SECONDS,
fallback=(env_fallback, ["DOCKER_TIMEOUT"]),
),
ca_path=dict(type="path", aliases=["ca_cert", "tls_ca_cert", "cacert_path"]),
client_cert=dict(type="path", aliases=["tls_client_cert", "cert_path"]),
client_key=dict(type="path", aliases=["tls_client_key", "key_path"]),
tls=dict(type="bool", default=DEFAULT_TLS, fallback=(env_fallback, ["DOCKER_TLS"])),
use_ssh_client=dict(type="bool", default=False),
validate_certs=dict(
type="bool",
default=DEFAULT_TLS_VERIFY,
fallback=(env_fallback, ["DOCKER_TLS_VERIFY"]),
aliases=["tls_verify"],
),
debug=dict(type="bool", default=False),
)
DOCKER_COMMON_ARGS = {
"docker_host": {
"type": "str",
"default": DEFAULT_DOCKER_HOST,
"fallback": (env_fallback, ["DOCKER_HOST"]),
"aliases": ["docker_url"],
},
"tls_hostname": {
"type": "str",
"fallback": (env_fallback, ["DOCKER_TLS_HOSTNAME"]),
},
"api_version": {
"type": "str",
"default": "auto",
"fallback": (env_fallback, ["DOCKER_API_VERSION"]),
"aliases": ["docker_api_version"],
},
"timeout": {
"type": "int",
"default": DEFAULT_TIMEOUT_SECONDS,
"fallback": (env_fallback, ["DOCKER_TIMEOUT"]),
},
"ca_path": {"type": "path", "aliases": ["ca_cert", "tls_ca_cert", "cacert_path"]},
"client_cert": {"type": "path", "aliases": ["tls_client_cert", "cert_path"]},
"client_key": {"type": "path", "aliases": ["tls_client_key", "key_path"]},
"tls": {
"type": "bool",
"default": DEFAULT_TLS,
"fallback": (env_fallback, ["DOCKER_TLS"]),
},
"use_ssh_client": {"type": "bool", "default": False},
"validate_certs": {
"type": "bool",
"default": DEFAULT_TLS_VERIFY,
"fallback": (env_fallback, ["DOCKER_TLS_VERIFY"]),
"aliases": ["tls_verify"],
},
"debug": {"type": "bool", "default": False},
}
DOCKER_COMMON_ARGS_VARS = {
option_name: f"ansible_docker_{option_name}"
@@ -245,11 +252,11 @@ class DifferenceTracker:
def add(self, name, parameter=None, active=None):
self._diff.append(
dict(
name=name,
parameter=parameter,
active=active,
)
{
"name": name,
"parameter": parameter,
"active": active,
}
)
def merge(self, other_tracker):
@@ -263,8 +270,8 @@ class DifferenceTracker:
"""
Return texts ``before`` and ``after``.
"""
before = dict()
after = dict()
before = {}
after = {}
for item in self._diff:
before[item["name"]] = item["active"]
after[item["name"]] = item["parameter"]
@@ -282,11 +289,11 @@ class DifferenceTracker:
"""
result = []
for entry in self._diff:
item = dict()
item[entry["name"]] = dict(
parameter=entry["parameter"],
container=entry["active"],
)
item = {}
item[entry["name"]] = {
"parameter": entry["parameter"],
"container": entry["active"],
}
result.append(item)
return result
@@ -335,7 +342,7 @@ def clean_dict_booleans_for_docker_api(data, allow_sequences=False):
return "false"
return str(value)
result = dict()
result = {}
if data is not None:
for k, v in data.items():
result[str(k)] = (
@@ -389,7 +396,7 @@ def normalize_healthcheck(healthcheck, normalize_test=False):
"""
Return dictionary of healthcheck parameters.
"""
result = dict()
result = {}
# All supported healthcheck parameters
options = (
@@ -420,10 +427,10 @@ def normalize_healthcheck(healthcheck, normalize_test=False):
if key == "retries":
try:
value = int(value)
except ValueError:
except ValueError as exc:
raise ValueError(
f'Cannot parse number of retries for healthcheck. Expected an integer, got "{value}".'
)
) from exc
if key == "test" and value and normalize_test:
value = normalize_healthcheck_test(value)
result[key] = value