Skip to content

Commit

Permalink
Refactor docker_image line extraction code. (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein authored Dec 28, 2020
1 parent a11f24c commit 2a0fb7d
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions plugins/modules/docker_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,17 @@ def tag_image(self, name, tag, repository, push=False):
if push:
self.push_image(repo, repo_tag)

@staticmethod
def _extract_output_line(line, output):
'''
Extract text line from stream output and, if found, adds it to output.
'''
if 'stream' in line or 'status' in line:
# Make sure we have a string (assuming that line['stream'] and
# line['status'] are either not defined, falsish, or a string)
text_line = line.get('stream') or line.get('status') or ''
output.append(text_line)

def build_image(self):
'''
Build an image
Expand Down Expand Up @@ -647,9 +658,7 @@ def build_image(self):
for line in self.client.build(**params):
# line = json.loads(line)
self.log(line, pretty_print=True)
if "stream" in line or "status" in line:
build_line = line.get("stream") or line.get("status") or ''
build_output.append(build_line)
self._extract_output_line(line, build_output)

if line.get('error'):
if line.get('errorDetail'):
Expand Down Expand Up @@ -681,9 +690,7 @@ def load_image(self):
self.log("Loading image from %s" % self.load_path)
for line in self.client.load_image(image_tar):
self.log(line, pretty_print=True)
if "stream" in line or "status" in line:
load_line = line.get("stream") or line.get("status") or ''
load_output.append(load_line)
self._extract_output_line(line, load_output)
except EnvironmentError as exc:
if exc.errno == errno.ENOENT:
self.client.fail("Error opening image %s - %s" % (self.load_path, str(exc)))
Expand Down

0 comments on commit 2a0fb7d

Please sign in to comment.