diff options
author | Joffrey F <joffrey@docker.com> | 2018-01-26 15:59:46 -0800 |
---|---|---|
committer | Joffrey F <joffrey@docker.com> | 2018-01-26 15:59:46 -0800 |
commit | 631cc3c1215441edb075a999a77061c1275c5e5a (patch) | |
tree | 7d605cd7451cf94f0698b8a318721c8e7c627e91 /docker | |
parent | deb8222d6994dca12be65146189859c9b76ed9a5 (diff) | |
download | docker-py-1702-build-logs-dockerclient.tar.gz |
ImageCollection.build now also returns build logs along with the built image reference1702-build-logs-dockerclient
BuildError.build_logs has a copy of the logs generator
Signed-off-by: Joffrey F <joffrey@docker.com>
Diffstat (limited to 'docker')
-rw-r--r-- | docker/errors.py | 7 | ||||
-rw-r--r-- | docker/models/images.py | 14 |
2 files changed, 14 insertions, 7 deletions
diff --git a/docker/errors.py b/docker/errors.py index eeeac57..0253695 100644 --- a/docker/errors.py +++ b/docker/errors.py @@ -140,8 +140,11 @@ class StreamParseError(RuntimeError): self.msg = reason -class BuildError(Exception): - pass +class BuildError(DockerException): + def __init__(self, reason, build_log): + super(BuildError, self).__init__(reason) + self.msg = reason + self.build_log = build_log class ImageLoadError(DockerException): diff --git a/docker/models/images.py b/docker/models/images.py index dcdeac9..c4e727b 100644 --- a/docker/models/images.py +++ b/docker/models/images.py @@ -1,3 +1,4 @@ +import itertools import re import six @@ -160,7 +161,9 @@ class ImageCollection(Collection): platform (str): Platform in the format ``os[/arch[/variant]]``. Returns: - (:py:class:`Image`): The built image. + (tuple): The first item is the :py:class:`Image` object for the + image that was build. The second item is a generator of the + build logs as JSON-decoded objects. Raises: :py:class:`docker.errors.BuildError` @@ -175,9 +178,10 @@ class ImageCollection(Collection): return self.get(resp) last_event = None image_id = None - for chunk in json_stream(resp): + result_stream, internal_stream = itertools.tee(json_stream(resp)) + for chunk in internal_stream: if 'error' in chunk: - raise BuildError(chunk['error']) + raise BuildError(chunk['error'], result_stream) if 'stream' in chunk: match = re.search( r'(^Successfully built |sha256:)([0-9a-f]+)$', @@ -187,8 +191,8 @@ class ImageCollection(Collection): image_id = match.group(2) last_event = chunk if image_id: - return self.get(image_id) - raise BuildError(last_event or 'Unknown') + return (self.get(image_id), result_stream) + raise BuildError(last_event or 'Unknown', result_stream) def get(self, name): """ |