refactor downloading a bit

This commit is contained in:
Jim Rollenhagen 2014-01-14 16:53:46 -08:00
parent e0901614e5
commit 36be4d2232
2 changed files with 26 additions and 19 deletions

@ -68,9 +68,8 @@ class ImageDownloadError(errors.RESTError):
message = 'Error downloading image.'
def __init__(self, image_id, image_url):
details = 'Image with id {} not found at url {}.'.format(image_id,
image_url)
def __init__(self, image_id):
details = 'Could not download image with id {}.'.format(image_id)
super(ImageDownloadError, self).__init__()
self.details = details

@ -49,25 +49,33 @@ def _write_image(image_info, configdrive='configdrive', device='/dev/sda'):
return subprocess.call(command)
def _download_image(image_info):
for index, url in enumerate(image_info['urls']):
try:
url = image_info['urls'][0]
resp = requests.get(url, stream=True)
if resp.status_code != 200:
raise errors.ImageDownloadError(image_info['id'], url)
image_location = _image_location(image_info)
with open(image_location, 'wb') as f:
for chunk in resp.iter_content(1024 * 1024):
f.write(chunk)
def _request_url(image_info, url):
resp = requests.get(url, stream=True)
if resp.status_code != 200:
raise errors.ImageDownloadError(image_info['id'])
return resp
if not _verify_image(image_info, image_location):
# TODO(jimrollenhagen) retry download?
raise errors.ImageChecksumError(image_info['id'])
except Exception:
def _download_image(image_info):
resp = None
for url in image_info['urls']:
try:
resp = _request_url(image_info, url)
except errors.ImageDownloadError:
continue
else:
return index
break
if resp is None:
raise errors.ImageDownloadError(image_info['id'])
image_location = _image_location(image_info)
with open(image_location, 'wb') as f:
for chunk in resp.iter_content(1024 * 1024):
f.write(chunk)
if not _verify_image(image_info, image_location):
# TODO(jimrollenhagen) retry download?
raise errors.ImageChecksumError(image_info['id'])
def _verify_image(image_info, image_location):