Use a context manager in docker.ImageManager. (#65609)

Co-Authored-By: Felix Fontein <felix@fontein.de>
pull/65637/head
Mads Jensen 5 years ago committed by ansibot
parent 03a4edb477
commit 965474841f

@ -0,0 +1,2 @@
bugfixes:
- docker_image - improve file handling when loading images from disk.

@ -426,6 +426,7 @@ image:
sample: {} sample: {}
''' '''
import errno
import os import os
import re import re
import traceback import traceback
@ -787,20 +788,15 @@ class ImageManager(DockerBaseClass):
''' '''
try: try:
self.log("Opening image %s" % self.load_path) self.log("Opening image %s" % self.load_path)
image_tar = open(self.load_path, 'rb') with open(self.load_path, 'rb') as image_tar:
except Exception as exc: self.log("Loading image from %s" % self.load_path)
self.fail("Error opening image %s - %s" % (self.load_path, str(exc))) self.client.load_image(image_tar)
except EnvironmentError as exc:
try: if exc.errno == errno.ENOENT:
self.log("Loading image from %s" % self.load_path) self.fail("Error opening image %s - %s" % (self.load_path, str(exc)))
self.client.load_image(image_tar)
except Exception as exc:
self.fail("Error loading image %s - %s" % (self.name, str(exc))) self.fail("Error loading image %s - %s" % (self.name, str(exc)))
try:
image_tar.close()
except Exception as exc: except Exception as exc:
self.fail("Error closing image %s - %s" % (self.name, str(exc))) self.fail("Error loading image %s - %s" % (self.name, str(exc)))
return self.client.find_image(self.name, self.tag) return self.client.find_image(self.name, self.tag)

Loading…
Cancel
Save