From e9d4ddfaece229fbd2df1a1bfa3f4f513f33d1ac Mon Sep 17 00:00:00 2001 From: Andy Roxby <107427605+aroxby-wayscript@users.noreply.github.com> Date: Thu, 16 Feb 2023 10:27:45 -0500 Subject: [PATCH 1/4] api: add `one-shot` option to container `stats` (#3089) Signed-off-by: Andy Roxby <107427605+aroxby-wayscript@users.noreply.github.com> --- docker/api/container.py | 24 ++++++++++++++++++++---- tests/unit/api_container_test.py | 13 ++++++++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/docker/api/container.py b/docker/api/container.py index ce483710..82a89228 100644 --- a/docker/api/container.py +++ b/docker/api/container.py @@ -1126,7 +1126,7 @@ class ContainerApiMixin: self._raise_for_status(res) @utils.check_resource('container') - def stats(self, container, decode=None, stream=True): + def stats(self, container, decode=None, stream=True, one_shot=None): """ Stream statistics for a specific container. Similar to the ``docker stats`` command. @@ -1138,6 +1138,9 @@ class ContainerApiMixin: False by default. stream (bool): If set to false, only the current stats will be returned instead of a stream. True by default. + one_shot (bool): If set to true, Only get a single stat instead of + waiting for 2 cycles. Must be used with stream=false. False by + default. Raises: :py:class:`docker.errors.APIError` @@ -1145,16 +1148,29 @@ class ContainerApiMixin: """ url = self._url("/containers/{0}/stats", container) + params = { + 'stream': stream + } + if one_shot is not None: + if utils.version_lt(self._version, '1.41'): + raise errors.InvalidVersion( + 'one_shot is not supported for API version < 1.41' + ) + params['one-shot'] = one_shot if stream: - return self._stream_helper(self._get(url, stream=True), + if one_shot: + raise errors.InvalidArgument( + 'one_shot is only available in conjunction with ' + 'stream=False' + ) + return self._stream_helper(self._get(url, params=params), decode=decode) else: if decode: raise errors.InvalidArgument( "decode is only available in conjunction with stream=True" ) - return self._result(self._get(url, params={'stream': False}), - json=True) + return self._result(self._get(url, params=params), json=True) @utils.check_resource('container') def stop(self, container, timeout=None): diff --git a/tests/unit/api_container_test.py b/tests/unit/api_container_test.py index d7b356c4..c605da37 100644 --- a/tests/unit/api_container_test.py +++ b/tests/unit/api_container_test.py @@ -1529,7 +1529,18 @@ class ContainerTest(BaseAPIClientTest): 'GET', url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/stats', timeout=60, - stream=True + params={'stream': True} + ) + + def test_container_stats_with_one_shot(self): + self.client.stats( + fake_api.FAKE_CONTAINER_ID, stream=False, one_shot=True) + + fake_request.assert_called_with( + 'GET', + url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/stats', + timeout=60, + params={'stream': False, 'one-shot': True} ) def test_container_top(self): From 7cd7458f2f41ef5af58589f28307a064936d7be1 Mon Sep 17 00:00:00 2001 From: Lorin Bucher Date: Thu, 16 Feb 2023 16:38:52 +0100 Subject: [PATCH 2/4] api: add `status` parameter to services list (#3093) Signed-off-by: Lorin Bucher --- docker/api/service.py | 10 +++++++++- docker/models/services.py | 2 ++ tests/integration/api_service_test.py | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docker/api/service.py b/docker/api/service.py index 371f541e..652b7c24 100644 --- a/docker/api/service.py +++ b/docker/api/service.py @@ -262,7 +262,7 @@ class ServiceApiMixin: return True @utils.minimum_version('1.24') - def services(self, filters=None): + def services(self, filters=None, status=None): """ List services. @@ -270,6 +270,8 @@ class ServiceApiMixin: filters (dict): Filters to process on the nodes list. Valid filters: ``id``, ``name`` , ``label`` and ``mode``. Default: ``None``. + status (bool): Include the service task count of running and + desired tasks. Default: ``None``. Returns: A list of dictionaries containing data about each service. @@ -281,6 +283,12 @@ class ServiceApiMixin: params = { 'filters': utils.convert_filters(filters) if filters else None } + if status is not None: + if utils.version_lt(self._version, '1.41'): + raise errors.InvalidVersion( + 'status is not supported in API version < 1.41' + ) + params['status'] = status url = self._url('/services') return self._result(self._get(url, params=params), True) diff --git a/docker/models/services.py b/docker/models/services.py index 06438748..70037041 100644 --- a/docker/models/services.py +++ b/docker/models/services.py @@ -266,6 +266,8 @@ class ServiceCollection(Collection): filters (dict): Filters to process on the nodes list. Valid filters: ``id``, ``name`` , ``label`` and ``mode``. Default: ``None``. + status (bool): Include the service task count of running and + desired tasks. Default: ``None``. Returns: list of :py:class:`Service`: The services. diff --git a/tests/integration/api_service_test.py b/tests/integration/api_service_test.py index 8ce7c9d5..dec3fa00 100644 --- a/tests/integration/api_service_test.py +++ b/tests/integration/api_service_test.py @@ -85,6 +85,20 @@ class ServiceTest(BaseAPIIntegrationTest): assert len(test_services) == 1 assert test_services[0]['Spec']['Labels']['test_label'] == 'testing' + @requires_api_version('1.41') + def test_list_services_with_status(self): + test_services = self.client.services() + assert len(test_services) == 0 + self.create_simple_service() + test_services = self.client.services( + filters={'name': 'dockerpytest_'}, status=False + ) + assert 'ServiceStatus' not in test_services[0] + test_services = self.client.services( + filters={'name': 'dockerpytest_'}, status=True + ) + assert 'ServiceStatus' in test_services[0] + def test_inspect_service_by_id(self): svc_name, svc_id = self.create_simple_service() svc_info = self.client.inspect_service(svc_id) From f84623225e0227adde48ffd8f2e1cafb1f9aa38d Mon Sep 17 00:00:00 2001 From: Milas Bowman Date: Wed, 22 Feb 2023 12:00:47 -0500 Subject: [PATCH 3/4] socket: fix for errors on pipe close in Windows (#3099) Need to return data, not size. By returning an empty string, EOF will be detected properly since `len()` will be `0`. Fixes #3098. Signed-off-by: Milas Bowman --- docker/utils/socket.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/utils/socket.py b/docker/utils/socket.py index 5aca30b1..47cb44f6 100644 --- a/docker/utils/socket.py +++ b/docker/utils/socket.py @@ -49,7 +49,7 @@ def read(socket, n=4096): if is_pipe_ended: # npipes don't support duplex sockets, so we interpret # a PIPE_ENDED error as a close operation (0-length read). - return 0 + return '' raise From aaf68b7f98df7f886778395112267b9b0f6140bc Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Wed, 22 Feb 2023 21:05:19 +0200 Subject: [PATCH 4/4] api: note the data arg may also be a stream in `put_archive` (#2478) Signed-off-by: Aarni Koskela --- docker/api/container.py | 2 +- docker/models/containers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/api/container.py b/docker/api/container.py index 82a89228..9a25b214 100644 --- a/docker/api/container.py +++ b/docker/api/container.py @@ -966,7 +966,7 @@ class ContainerApiMixin: container (str): The container where the file(s) will be extracted path (str): Path inside the container where the file(s) will be extracted. Must exist. - data (bytes): tar data to be extracted + data (bytes or stream): tar data to be extracted Returns: (bool): True if the call succeeds. diff --git a/docker/models/containers.py b/docker/models/containers.py index c718bbea..f451cf3f 100644 --- a/docker/models/containers.py +++ b/docker/models/containers.py @@ -324,7 +324,7 @@ class Container(Model): Args: path (str): Path inside the container where the file(s) will be extracted. Must exist. - data (bytes): tar data to be extracted + data (bytes or stream): tar data to be extracted Returns: (bool): True if the call succeeds.