In the spirit of sharing, I want to show an implementation that is a progression of Fabien's answer (which was extremely useful). This does printing, cleanup, et cetera, and throws an informed exception when things are bad:
import json
import logging
import re
import docker
log = logging.getLogger(__name__)
class StreamLineBuildGenerator(object):
def __init__(self, json_data):
self.__dict__ = json.loads(json_data)
class Docker(object):
REGISTRY = "some_docker_registry"
def __init__(self):
self.client = docker.from_env()
self.api_client = docker.APIClient()
def build(self, path, repository):
tag = "{}/{}".format(Docker.REGISTRY, repository)
output = self.api_client.build(path=path, tag=tag)
self._process_output(output)
log.info("done building {}".format(repository))
def push(self, repository):
tag = "{}/{}".format(Docker.REGISTRY, repository)
output = self.client.images.push(tag)
self._process_output(output)
log.info("done pushing {}".format(tag))
def _process_output(self, output):
if type(output) == str:
output = output.split("\n")
for line in output:
if line:
errors = set()
try:
stream_line = StreamLineBuildGenerator(line)
if hasattr(stream_line, "status"):
log.info(stream_line.status)
elif hasattr(stream_line, "stream"):
stream = re.sub("^\n", "", stream_line.stream)
stream = re.sub("\n$", "", stream)
# found after newline to close (red) "error" blocks: 27 91 48 109
stream = re.sub("\n(\x1B\[0m)$", "\\1", stream)
if stream:
log.info(stream)
elif hasattr(stream_line, "aux"):
if hasattr(stream_line.aux, "Digest"):
log.info("digest: {}".format(stream_line.aux["Digest"]))
if hasattr(stream_line.aux, "ID"):
log.info("ID: {}".format(stream_line.aux["ID"]))
else:
log.info("not recognized (1): {}".format(line))
if hasattr(stream_line, "error"):
errors.add(stream_line.error)
if hasattr(stream_line, "errorDetail"):
errors.add(stream_line.errorDetail["message"])
if hasattr(stream_line.errorDetail, "code"):
error_code = stream_line.errorDetail["code"]
errors.add("Error code: {}".format(error_code))
except ValueError as e:
log.error("not recognized (2): {}".format(line))
if errors:
message = "problem executing Docker: {}".format(". ".join(errors))
raise SystemError(message)