mirror of
https://github.com/ansible-collections/community.docker.git
synced 2025-12-17 12:28:55 +00:00
Address many redefined-outer-name.
This commit is contained in:
parent
38795830b7
commit
e07db8a832
@ -407,7 +407,7 @@ disable=raw-checker-failed,
|
|||||||
possibly-used-before-assignment,
|
possibly-used-before-assignment,
|
||||||
protected-access,
|
protected-access,
|
||||||
raise-missing-from,
|
raise-missing-from,
|
||||||
redefined-outer-name,
|
redefined-outer-name, # needed for test fixtures
|
||||||
simplifiable-if-expression,
|
simplifiable-if-expression,
|
||||||
subprocess-popen-preexec-fn,
|
subprocess-popen-preexec-fn,
|
||||||
super-init-not-called,
|
super-init-not-called,
|
||||||
|
|||||||
@ -289,14 +289,14 @@ class APIClient(_Session, DaemonApiMixin):
|
|||||||
except _HTTPError as e:
|
except _HTTPError as e:
|
||||||
create_api_error_from_http_exception(e)
|
create_api_error_from_http_exception(e)
|
||||||
|
|
||||||
def _result(self, response, json=False, binary=False):
|
def _result(self, response, get_json=False, get_binary=False):
|
||||||
if json and binary:
|
if get_json and get_binary:
|
||||||
raise AssertionError("json and binary must not be both True")
|
raise AssertionError("json and binary must not be both True")
|
||||||
self._raise_for_status(response)
|
self._raise_for_status(response)
|
||||||
|
|
||||||
if json:
|
if get_json:
|
||||||
return response.json()
|
return response.json()
|
||||||
if binary:
|
if get_binary:
|
||||||
return response.content
|
return response.content
|
||||||
return response.text
|
return response.text
|
||||||
|
|
||||||
@ -360,12 +360,12 @@ class APIClient(_Session, DaemonApiMixin):
|
|||||||
else:
|
else:
|
||||||
# Response is not chunked, meaning we probably
|
# Response is not chunked, meaning we probably
|
||||||
# encountered an error immediately
|
# encountered an error immediately
|
||||||
yield self._result(response, json=decode)
|
yield self._result(response, get_json=decode)
|
||||||
|
|
||||||
def _multiplexed_buffer_helper(self, response):
|
def _multiplexed_buffer_helper(self, response):
|
||||||
"""A generator of multiplexed data blocks read from a buffered
|
"""A generator of multiplexed data blocks read from a buffered
|
||||||
response."""
|
response."""
|
||||||
buf = self._result(response, binary=True)
|
buf = self._result(response, get_binary=True)
|
||||||
buf_length = len(buf)
|
buf_length = len(buf)
|
||||||
walker = 0
|
walker = 0
|
||||||
while True:
|
while True:
|
||||||
@ -478,7 +478,7 @@ class APIClient(_Session, DaemonApiMixin):
|
|||||||
return (
|
return (
|
||||||
self._stream_raw_result(res)
|
self._stream_raw_result(res)
|
||||||
if stream
|
if stream
|
||||||
else self._result(res, binary=True)
|
else self._result(res, get_binary=True)
|
||||||
)
|
)
|
||||||
|
|
||||||
self._raise_for_status(res)
|
self._raise_for_status(res)
|
||||||
@ -551,13 +551,13 @@ class APIClient(_Session, DaemonApiMixin):
|
|||||||
def get_binary(self, pathfmt, *args, **kwargs):
|
def get_binary(self, pathfmt, *args, **kwargs):
|
||||||
return self._result(
|
return self._result(
|
||||||
self._get(self._url(pathfmt, *args, versioned_api=True), **kwargs),
|
self._get(self._url(pathfmt, *args, versioned_api=True), **kwargs),
|
||||||
binary=True,
|
get_binary=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_json(self, pathfmt, *args, **kwargs):
|
def get_json(self, pathfmt, *args, **kwargs):
|
||||||
return self._result(
|
return self._result(
|
||||||
self._get(self._url(pathfmt, *args, versioned_api=True), **kwargs),
|
self._get(self._url(pathfmt, *args, versioned_api=True), **kwargs),
|
||||||
json=True,
|
get_json=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_text(self, pathfmt, *args, **kwargs):
|
def get_text(self, pathfmt, *args, **kwargs):
|
||||||
@ -581,7 +581,7 @@ class APIClient(_Session, DaemonApiMixin):
|
|||||||
def delete_json(self, pathfmt, *args, **kwargs):
|
def delete_json(self, pathfmt, *args, **kwargs):
|
||||||
return self._result(
|
return self._result(
|
||||||
self._delete(self._url(pathfmt, *args, versioned_api=True), **kwargs),
|
self._delete(self._url(pathfmt, *args, versioned_api=True), **kwargs),
|
||||||
json=True,
|
get_json=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
def post_call(self, pathfmt, *args, **kwargs):
|
def post_call(self, pathfmt, *args, **kwargs):
|
||||||
@ -603,7 +603,7 @@ class APIClient(_Session, DaemonApiMixin):
|
|||||||
self._post_json(
|
self._post_json(
|
||||||
self._url(pathfmt, *args, versioned_api=True), data, **kwargs
|
self._url(pathfmt, *args, versioned_api=True), data, **kwargs
|
||||||
),
|
),
|
||||||
binary=True,
|
get_binary=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
def post_json_to_json(self, pathfmt, *args, **kwargs):
|
def post_json_to_json(self, pathfmt, *args, **kwargs):
|
||||||
@ -612,7 +612,7 @@ class APIClient(_Session, DaemonApiMixin):
|
|||||||
self._post_json(
|
self._post_json(
|
||||||
self._url(pathfmt, *args, versioned_api=True), data, **kwargs
|
self._url(pathfmt, *args, versioned_api=True), data, **kwargs
|
||||||
),
|
),
|
||||||
json=True,
|
get_json=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
def post_json_to_text(self, pathfmt, *args, **kwargs):
|
def post_json_to_text(self, pathfmt, *args, **kwargs):
|
||||||
@ -670,5 +670,5 @@ class APIClient(_Session, DaemonApiMixin):
|
|||||||
def post_to_json(self, pathfmt, *args, **kwargs):
|
def post_to_json(self, pathfmt, *args, **kwargs):
|
||||||
return self._result(
|
return self._result(
|
||||||
self._post(self._url(pathfmt, *args, versioned_api=True), **kwargs),
|
self._post(self._url(pathfmt, *args, versioned_api=True), **kwargs),
|
||||||
json=True,
|
get_json=True,
|
||||||
)
|
)
|
||||||
|
|||||||
@ -33,7 +33,7 @@ class DaemonApiMixin(object):
|
|||||||
If the server returns an error.
|
If the server returns an error.
|
||||||
"""
|
"""
|
||||||
url = self._url("/system/df")
|
url = self._url("/system/df")
|
||||||
return self._result(self._get(url), True)
|
return self._result(self._get(url), get_json=True)
|
||||||
|
|
||||||
def info(self):
|
def info(self):
|
||||||
"""
|
"""
|
||||||
@ -47,7 +47,7 @@ class DaemonApiMixin(object):
|
|||||||
:py:class:`docker.errors.APIError`
|
:py:class:`docker.errors.APIError`
|
||||||
If the server returns an error.
|
If the server returns an error.
|
||||||
"""
|
"""
|
||||||
return self._result(self._get(self._url("/info")), True)
|
return self._result(self._get(self._url("/info")), get_json=True)
|
||||||
|
|
||||||
def login(
|
def login(
|
||||||
self,
|
self,
|
||||||
@ -108,7 +108,7 @@ class DaemonApiMixin(object):
|
|||||||
response = self._post_json(self._url("/auth"), data=req_data)
|
response = self._post_json(self._url("/auth"), data=req_data)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
self._auth_configs.add_auth(registry or auth.INDEX_NAME, req_data)
|
self._auth_configs.add_auth(registry or auth.INDEX_NAME, req_data)
|
||||||
return self._result(response, json=True)
|
return self._result(response, get_json=True)
|
||||||
|
|
||||||
def ping(self):
|
def ping(self):
|
||||||
"""
|
"""
|
||||||
@ -137,4 +137,4 @@ class DaemonApiMixin(object):
|
|||||||
If the server returns an error.
|
If the server returns an error.
|
||||||
"""
|
"""
|
||||||
url = self._url("/version", versioned_api=api_version)
|
url = self._url("/version", versioned_api=api_version)
|
||||||
return self._result(self._get(url), json=True)
|
return self._result(self._get(url), get_json=True)
|
||||||
|
|||||||
@ -133,43 +133,45 @@ def _get_tls_config(fail_function, **kwargs):
|
|||||||
fail_function(f"TLS config error: {exc}")
|
fail_function(f"TLS config error: {exc}")
|
||||||
|
|
||||||
|
|
||||||
def is_using_tls(auth):
|
def is_using_tls(auth_data):
|
||||||
return auth["tls_verify"] or auth["tls"]
|
return auth_data["tls_verify"] or auth_data["tls"]
|
||||||
|
|
||||||
|
|
||||||
def get_connect_params(auth, fail_function):
|
def get_connect_params(auth_data, fail_function):
|
||||||
if is_using_tls(auth):
|
if is_using_tls(auth_data):
|
||||||
auth["docker_host"] = auth["docker_host"].replace("tcp://", "https://")
|
auth_data["docker_host"] = auth_data["docker_host"].replace(
|
||||||
|
"tcp://", "https://"
|
||||||
|
)
|
||||||
|
|
||||||
result = dict(
|
result = dict(
|
||||||
base_url=auth["docker_host"],
|
base_url=auth_data["docker_host"],
|
||||||
version=auth["api_version"],
|
version=auth_data["api_version"],
|
||||||
timeout=auth["timeout"],
|
timeout=auth_data["timeout"],
|
||||||
)
|
)
|
||||||
|
|
||||||
if auth["tls_verify"]:
|
if auth_data["tls_verify"]:
|
||||||
# TLS with verification
|
# TLS with verification
|
||||||
tls_config = dict(
|
tls_config = dict(
|
||||||
verify=True,
|
verify=True,
|
||||||
assert_hostname=auth["tls_hostname"],
|
assert_hostname=auth_data["tls_hostname"],
|
||||||
fail_function=fail_function,
|
fail_function=fail_function,
|
||||||
)
|
)
|
||||||
if auth["cert_path"] and auth["key_path"]:
|
if auth_data["cert_path"] and auth_data["key_path"]:
|
||||||
tls_config["client_cert"] = (auth["cert_path"], auth["key_path"])
|
tls_config["client_cert"] = (auth_data["cert_path"], auth_data["key_path"])
|
||||||
if auth["cacert_path"]:
|
if auth_data["cacert_path"]:
|
||||||
tls_config["ca_cert"] = auth["cacert_path"]
|
tls_config["ca_cert"] = auth_data["cacert_path"]
|
||||||
result["tls"] = _get_tls_config(**tls_config)
|
result["tls"] = _get_tls_config(**tls_config)
|
||||||
elif auth["tls"]:
|
elif auth_data["tls"]:
|
||||||
# TLS without verification
|
# TLS without verification
|
||||||
tls_config = dict(
|
tls_config = dict(
|
||||||
verify=False,
|
verify=False,
|
||||||
fail_function=fail_function,
|
fail_function=fail_function,
|
||||||
)
|
)
|
||||||
if auth["cert_path"] and auth["key_path"]:
|
if auth_data["cert_path"] and auth_data["key_path"]:
|
||||||
tls_config["client_cert"] = (auth["cert_path"], auth["key_path"])
|
tls_config["client_cert"] = (auth_data["cert_path"], auth_data["key_path"])
|
||||||
result["tls"] = _get_tls_config(**tls_config)
|
result["tls"] = _get_tls_config(**tls_config)
|
||||||
|
|
||||||
if auth.get("use_ssh_client"):
|
if auth_data.get("use_ssh_client"):
|
||||||
if LooseVersion(docker_version) < LooseVersion("4.4.0"):
|
if LooseVersion(docker_version) < LooseVersion("4.4.0"):
|
||||||
fail_function(
|
fail_function(
|
||||||
"use_ssh_client=True requires Docker SDK for Python 4.4.0 or newer"
|
"use_ssh_client=True requires Docker SDK for Python 4.4.0 or newer"
|
||||||
@ -579,7 +581,7 @@ class AnsibleDockerClientBase(Client):
|
|||||||
break
|
break
|
||||||
return images
|
return images
|
||||||
|
|
||||||
def pull_image(self, name, tag="latest", platform=None):
|
def pull_image(self, name, tag="latest", image_platform=None):
|
||||||
"""
|
"""
|
||||||
Pull an image
|
Pull an image
|
||||||
"""
|
"""
|
||||||
@ -588,8 +590,8 @@ class AnsibleDockerClientBase(Client):
|
|||||||
stream=True,
|
stream=True,
|
||||||
decode=True,
|
decode=True,
|
||||||
)
|
)
|
||||||
if platform is not None:
|
if image_platform is not None:
|
||||||
kwargs["platform"] = platform
|
kwargs["platform"] = image_platform
|
||||||
self.log(f"Pulling image {name}:{tag}")
|
self.log(f"Pulling image {name}:{tag}")
|
||||||
old_tag = self.find_image(name, tag)
|
old_tag = self.find_image(name, tag)
|
||||||
try:
|
try:
|
||||||
@ -624,7 +626,7 @@ class AnsibleDockerClientBase(Client):
|
|||||||
self._url("/distribution/{0}/json", image),
|
self._url("/distribution/{0}/json", image),
|
||||||
headers={"X-Registry-Auth": header},
|
headers={"X-Registry-Auth": header},
|
||||||
),
|
),
|
||||||
json=True,
|
get_json=True,
|
||||||
)
|
)
|
||||||
return super(AnsibleDockerClientBase, self).inspect_distribution(
|
return super(AnsibleDockerClientBase, self).inspect_distribution(
|
||||||
image, **kwargs
|
image, **kwargs
|
||||||
|
|||||||
@ -495,7 +495,7 @@ class AnsibleDockerClientBase(Client):
|
|||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.fail(f"Error inspecting image ID {image_id} - {exc}")
|
self.fail(f"Error inspecting image ID {image_id} - {exc}")
|
||||||
|
|
||||||
def pull_image(self, name, tag="latest", platform=None):
|
def pull_image(self, name, tag="latest", image_platform=None):
|
||||||
"""
|
"""
|
||||||
Pull an image
|
Pull an image
|
||||||
"""
|
"""
|
||||||
@ -508,8 +508,8 @@ class AnsibleDockerClientBase(Client):
|
|||||||
"tag": tag or image_tag or "latest",
|
"tag": tag or image_tag or "latest",
|
||||||
"fromImage": repository,
|
"fromImage": repository,
|
||||||
}
|
}
|
||||||
if platform is not None:
|
if image_platform is not None:
|
||||||
params["platform"] = platform
|
params["platform"] = image_platform
|
||||||
|
|
||||||
headers = {}
|
headers = {}
|
||||||
header = auth.get_config_header(self, registry)
|
header = auth.get_config_header(self, registry)
|
||||||
|
|||||||
@ -330,7 +330,7 @@ class EngineDriver(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def pull_image(self, client, repository, tag, platform=None):
|
def pull_image(self, client, repository, tag, image_platform=None):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
|
|||||||
@ -232,8 +232,8 @@ class DockerAPIEngineDriver(EngineDriver):
|
|||||||
def inspect_image_by_name(self, client, repository, tag):
|
def inspect_image_by_name(self, client, repository, tag):
|
||||||
return client.find_image(repository, tag)
|
return client.find_image(repository, tag)
|
||||||
|
|
||||||
def pull_image(self, client, repository, tag, platform=None):
|
def pull_image(self, client, repository, tag, image_platform=None):
|
||||||
return client.pull_image(repository, tag, platform=platform)
|
return client.pull_image(repository, tag, image_platform=image_platform)
|
||||||
|
|
||||||
def pause_container(self, client, container_id):
|
def pause_container(self, client, container_id):
|
||||||
client.post_call("/containers/{0}/pause", container_id)
|
client.post_call("/containers/{0}/pause", container_id)
|
||||||
|
|||||||
@ -569,7 +569,7 @@ class ContainerManager(DockerBaseClass):
|
|||||||
self.client,
|
self.client,
|
||||||
repository,
|
repository,
|
||||||
tag,
|
tag,
|
||||||
platform=self.module.params["platform"],
|
image_platform=self.module.params["platform"],
|
||||||
)
|
)
|
||||||
if alreadyToLatest:
|
if alreadyToLatest:
|
||||||
self.results["changed"] = False
|
self.results["changed"] = False
|
||||||
|
|||||||
@ -570,7 +570,7 @@ class ImageManager(DockerBaseClass):
|
|||||||
self.results["changed"] = True
|
self.results["changed"] = True
|
||||||
if not self.check_mode:
|
if not self.check_mode:
|
||||||
self.results["image"], dummy = self.client.pull_image(
|
self.results["image"], dummy = self.client.pull_image(
|
||||||
self.name, tag=self.tag, platform=self.pull_platform
|
self.name, tag=self.tag, image_platform=self.pull_platform
|
||||||
)
|
)
|
||||||
elif self.source == "local":
|
elif self.source == "local":
|
||||||
if image is None:
|
if image is None:
|
||||||
|
|||||||
@ -181,7 +181,7 @@ class ImagePuller(DockerBaseClass):
|
|||||||
results["diff"]["after"] = image_info(dict(Id="unknown"))
|
results["diff"]["after"] = image_info(dict(Id="unknown"))
|
||||||
else:
|
else:
|
||||||
results["image"], not_changed = self.client.pull_image(
|
results["image"], not_changed = self.client.pull_image(
|
||||||
self.name, tag=self.tag, platform=self.platform
|
self.name, tag=self.tag, image_platform=self.platform
|
||||||
)
|
)
|
||||||
results["changed"] = not not_changed
|
results["changed"] = not not_changed
|
||||||
results["diff"]["after"] = image_info(results["image"])
|
results["diff"]["after"] = image_info(results["image"])
|
||||||
|
|||||||
@ -214,13 +214,13 @@ class DockerFileStore(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
b64auth = base64.b64encode(to_bytes(username) + b":" + to_bytes(password))
|
b64auth = base64.b64encode(to_bytes(username) + b":" + to_bytes(password))
|
||||||
auth = to_text(b64auth)
|
tauth = to_text(b64auth)
|
||||||
|
|
||||||
# build up the auth structure
|
# build up the auth structure
|
||||||
if "auths" not in self._config:
|
if "auths" not in self._config:
|
||||||
self._config["auths"] = dict()
|
self._config["auths"] = dict()
|
||||||
|
|
||||||
self._config["auths"][server] = dict(auth=auth)
|
self._config["auths"][server] = dict(auth=tauth)
|
||||||
|
|
||||||
self._write()
|
self._write()
|
||||||
|
|
||||||
@ -294,7 +294,7 @@ class LoginManager(DockerBaseClass):
|
|||||||
self.client._auth_configs.add_auth(
|
self.client._auth_configs.add_auth(
|
||||||
self.registry_url or auth.INDEX_NAME, req_data
|
self.registry_url or auth.INDEX_NAME, req_data
|
||||||
)
|
)
|
||||||
return self.client._result(response, json=True)
|
return self.client._result(response, get_json=True)
|
||||||
|
|
||||||
def login(self):
|
def login(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -619,29 +619,29 @@ class DisableSocketTest(unittest.TestCase):
|
|||||||
|
|
||||||
def test_disable_socket_timeout(self):
|
def test_disable_socket_timeout(self):
|
||||||
"""Test that the timeout is disabled on a generic socket object."""
|
"""Test that the timeout is disabled on a generic socket object."""
|
||||||
socket = self.DummySocket()
|
the_socket = self.DummySocket()
|
||||||
|
|
||||||
self.client._disable_socket_timeout(socket)
|
self.client._disable_socket_timeout(the_socket)
|
||||||
|
|
||||||
assert socket.timeout is None
|
assert the_socket.timeout is None
|
||||||
|
|
||||||
def test_disable_socket_timeout2(self):
|
def test_disable_socket_timeout2(self):
|
||||||
"""Test that the timeouts are disabled on a generic socket object
|
"""Test that the timeouts are disabled on a generic socket object
|
||||||
and it's _sock object if present."""
|
and it's _sock object if present."""
|
||||||
socket = self.DummySocket()
|
the_socket = self.DummySocket()
|
||||||
socket._sock = self.DummySocket()
|
the_socket._sock = self.DummySocket()
|
||||||
|
|
||||||
self.client._disable_socket_timeout(socket)
|
self.client._disable_socket_timeout(the_socket)
|
||||||
|
|
||||||
assert socket.timeout is None
|
assert the_socket.timeout is None
|
||||||
assert socket._sock.timeout is None
|
assert the_socket._sock.timeout is None
|
||||||
|
|
||||||
def test_disable_socket_timout_non_blocking(self):
|
def test_disable_socket_timout_non_blocking(self):
|
||||||
"""Test that a non-blocking socket does not get set to blocking."""
|
"""Test that a non-blocking socket does not get set to blocking."""
|
||||||
socket = self.DummySocket()
|
the_socket = self.DummySocket()
|
||||||
socket._sock = self.DummySocket(0.0)
|
the_socket._sock = self.DummySocket(0.0)
|
||||||
|
|
||||||
self.client._disable_socket_timeout(socket)
|
self.client._disable_socket_timeout(the_socket)
|
||||||
|
|
||||||
assert socket.timeout is None
|
assert the_socket.timeout is None
|
||||||
assert socket._sock.timeout == 0.0
|
assert the_socket._sock.timeout == 0.0
|
||||||
|
|||||||
@ -123,8 +123,8 @@ class APIErrorTest(unittest.TestCase):
|
|||||||
except requests.exceptions.HTTPError as e:
|
except requests.exceptions.HTTPError as e:
|
||||||
try:
|
try:
|
||||||
create_api_error_from_http_exception(e)
|
create_api_error_from_http_exception(e)
|
||||||
except APIError as e:
|
except APIError as e2:
|
||||||
err = e
|
err = e2
|
||||||
assert err.is_server_error() is True
|
assert err.is_server_error() is True
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user