Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixes LP Bug#845788
glance.client.image_update needed to calculate size so that Glance's Swift
driver can do chunking properly for large objects.

(cherry picked from commit 6cfff16)

Change-Id: Iafe8034a710cff53a0caa3ae5e9ee3a3adda19f8
  • Loading branch information
jaypipes authored and markmc committed Nov 18, 2011
1 parent a0ecd5c commit 536435f
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions glance/client.py
Expand Up @@ -96,6 +96,33 @@ def get_image_meta(self, image_id):
image = utils.get_image_meta_from_headers(res)
return image

def _get_image_size(self, image_data):
"""
Analyzes the incoming image file and attempts to determine
its size.
:param image_data: The input to the client, typically a file
redirected from stdin.
:retval The image file's size or None if it cannot be determined.
"""
# For large images, we need to supply the size of the
# image file. See LP Bugs #827660 and #845788.
if hasattr(image_data, 'seek') and hasattr(image_data, 'tell'):
try:
image_data.seek(0, os.SEEK_END)
image_size = image_data.tell()
image_data.seek(0)
return image_size
except IOError, e:
if e.errno == errno.ESPIPE:
# Illegal seek. This means the user is trying
# to pipe image data to the client, e.g.
# echo testdata | bin/glance add blah..., or
# that stdin is empty
return None
else:
raise

def add_image(self, image_meta=None, image_data=None):
"""
Tells Glance about an image's metadata as well
Expand All @@ -114,24 +141,10 @@ def add_image(self, image_meta=None, image_data=None):
if image_data:
body = image_data
headers['content-type'] = 'application/octet-stream'
# For large images, we need to supply the size of the
# image file. See LP Bug #827660.
if hasattr(image_data, 'seek') and hasattr(image_data, 'tell'):
try:
image_data.seek(0, os.SEEK_END)
image_size = image_data.tell()
image_data.seek(0)
headers['x-image-meta-size'] = image_size
headers['content-length'] = image_size
except IOError, e:
if e.errno == errno.ESPIPE:
# Illegal seek. This means the user is trying
# to pipe image data to the client, e.g.
# echo testdata | bin/glance add blah..., or
# that stdin is empty
pass
else:
raise
image_size = self._get_image_size(image_data)
if image_size:
headers['x-image-meta-size'] = image_size
headers['content-length'] = image_size
else:
body = None

Expand All @@ -151,6 +164,10 @@ def update_image(self, image_id, image_meta=None, image_data=None):
if image_data:
body = image_data
headers['content-type'] = 'application/octet-stream'
image_size = self._get_image_size(image_data)
if image_size:
headers['x-image-meta-size'] = image_size
headers['content-length'] = image_size
else:
body = None

Expand Down

0 comments on commit 536435f

Please sign in to comment.