From aeaac4c9fca444989eca320f38b78cf53ab1f900 Mon Sep 17 00:00:00 2001 From: Emran Batmanghelich Date: Sun, 3 Dec 2023 20:57:46 +0330 Subject: [PATCH 1/3] feat: accept filters and keep_storage in prune_builds Signed-off-by: Emran Batmanghelich --- docker/api/build.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/docker/api/build.py b/docker/api/build.py index 439f4dc35..1380f9cc7 100644 --- a/docker/api/build.py +++ b/docker/api/build.py @@ -276,10 +276,20 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None, return self._stream_helper(response, decode=decode) @utils.minimum_version('1.31') - def prune_builds(self): + def prune_builds(self, filters=None, keep_storage=None): """ Delete the builder cache + Args: + filters (dict): Filters to process on the prune list. + Available filters: + - dangling (bool): When set to true (or 1), prune only + unused and untagged images. + - until (str): Can be Unix timestamps, date formatted + timestamps, or Go duration strings (e.g. 10m, 1h30m) computed + relative to the daemon's local time. + keep_storage (int): Amount of disk space in bytes to keep for cache. + Returns: (dict): A dictionary containing information about the operation's result. The ``SpaceReclaimed`` key indicates the amount of @@ -290,7 +300,12 @@ def prune_builds(self): If the server returns an error. """ url = self._url("/build/prune") - return self._result(self._post(url), True) + params = {} + if filters is not None: + params['filters'] = utils.convert_filters(filters) + if keep_storage is not None: + params['keep-storage'] = keep_storage + return self._result(self._post(url, params=params), True) def _set_auth_headers(self, headers): log.debug('Looking for auth config') From a5b7ea3908c9b24c2ab72b5450bdfea8a3d2e65e Mon Sep 17 00:00:00 2001 From: Emran Batmanghelich Date: Mon, 4 Dec 2023 18:43:22 +0330 Subject: [PATCH 2/3] add `all` arg + check API version Signed-off-by: Emran Batmanghelich --- docker/api/build.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/docker/api/build.py b/docker/api/build.py index 1380f9cc7..6ba7751fd 100644 --- a/docker/api/build.py +++ b/docker/api/build.py @@ -276,12 +276,13 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None, return self._stream_helper(response, decode=decode) @utils.minimum_version('1.31') - def prune_builds(self, filters=None, keep_storage=None): + def prune_builds(self, filters=None, keep_storage=None, all=None): """ Delete the builder cache Args: filters (dict): Filters to process on the prune list. + Needs Docker API v1.39+ Available filters: - dangling (bool): When set to true (or 1), prune only unused and untagged images. @@ -289,6 +290,9 @@ def prune_builds(self, filters=None, keep_storage=None): timestamps, or Go duration strings (e.g. 10m, 1h30m) computed relative to the daemon's local time. keep_storage (int): Amount of disk space in bytes to keep for cache. + Needs Docker API v1.39+ + all (bool): Remove all types of build cache. + Needs Docker API v1.39+ Returns: (dict): A dictionary containing information about the operation's @@ -300,11 +304,19 @@ def prune_builds(self, filters=None, keep_storage=None): If the server returns an error. """ url = self._url("/build/prune") + if (filters, keep_storage, all) != (None, None, None) \ + and utils.version_lt(self._version, '1.39'): + raise errors.InvalidVersion( + '`filters`, `keep_storage`, and `all` args are only available ' + 'for API version > 1.38' + ) params = {} if filters is not None: params['filters'] = utils.convert_filters(filters) if keep_storage is not None: params['keep-storage'] = keep_storage + if keep_storage is not None: + params['keep-storage'] = keep_storage return self._result(self._post(url, params=params), True) def _set_auth_headers(self, headers): From 3bb9f34ef02515f6dc0e1202acc2ea5f25c07e39 Mon Sep 17 00:00:00 2001 From: Milas Bowman Date: Tue, 5 Dec 2023 00:05:58 -0500 Subject: [PATCH 3/3] Update docker/api/build.py Signed-off-by: Milas Bowman --- docker/api/build.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/api/build.py b/docker/api/build.py index 6ba7751fd..145382e4c 100644 --- a/docker/api/build.py +++ b/docker/api/build.py @@ -315,8 +315,8 @@ def prune_builds(self, filters=None, keep_storage=None, all=None): params['filters'] = utils.convert_filters(filters) if keep_storage is not None: params['keep-storage'] = keep_storage - if keep_storage is not None: - params['keep-storage'] = keep_storage + if all is not None: + params['all'] = all return self._result(self._post(url, params=params), True) def _set_auth_headers(self, headers):