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

Co-Authored-By: Felix Fontein <felix@fontein.de>
This commit is contained in:
Mads Jensen 2019-12-08 14:36:33 +01:00 committed by ansibot
parent 03a4edb477
commit 965474841f
2 changed files with 10 additions and 12 deletions

View file

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

View file

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