def _extract_tarball(tarball_path, staging_path): """Extract the tarball into the staging directory.""" tarball_fileobj = open(tarball_path, 'rb') try: utils.extract_tarball(tarball_fileobj, staging_path) finally: tarball_fileobj.close()
def _download_tarball_and_verify(request, staging_path): # NOTE(johngarbutt) By default, there is no timeout. # To ensure the script does not hang if we lose connection # to glance, we add this socket timeout. # This is here so there is no chance the timeout out has # been adjusted by other library calls. socket.setdefaulttimeout(SOCKET_TIMEOUT_SECONDS) try: response = urllib.request.urlopen(request) except urllib.error.HTTPError as error: # noqa raise RetryableError(error) except urllib.error.URLError as error: # noqa raise RetryableError(error) except httplib.HTTPException as error: # noqa # httplib.HTTPException and derivatives (BadStatusLine in particular) # don't have a useful __repr__ or __str__ raise RetryableError('%s: %s' % (error.__class__.__name__, error)) url = request.get_full_url() logging.info("Reading image data from %s" % url) callback_data = {'bytes_read': 0} checksum = md5.new() def update_md5(chunk): callback_data['bytes_read'] += len(chunk) checksum.update(chunk) try: try: utils.extract_tarball(response, staging_path, callback=update_md5) except Exception as error: # noqa raise RetryableError(error) finally: bytes_read = callback_data['bytes_read'] logging.info("Read %d bytes from %s", bytes_read, url) # Use ETag if available, otherwise content-md5(v2) or # X-Image-Meta-Checksum(v1) etag = response.info().getheader('etag', None) if etag is None: etag = response.info().getheader('content-md5', None) if etag is None: etag = response.info().getheader('x-image-meta-checksum', None) # Verify checksum using ETag checksum = checksum.hexdigest() if etag is None: msg = "No ETag found for comparison to checksum %(checksum)s" logging.info(msg % {'checksum': checksum}) elif checksum != etag: msg = 'ETag %(etag)s does not match computed md5sum %(checksum)s' raise RetryableError(msg % {'checksum': checksum, 'etag': etag}) else: msg = "Verified image checksum %(checksum)s" logging.info(msg % {'checksum': checksum})
# don't have a useful __repr__ or __str__ raise RetryableError("%s: %s" % (error.__class__.__name__, error)) url = request.get_full_url() logging.info("Reading image data from %s" % url) callback_data = {"bytes_read": 0} checksum = md5.new() def update_md5(chunk): callback_data["bytes_read"] += len(chunk) checksum.update(chunk) try: try: utils.extract_tarball(response, staging_path, callback=update_md5) except Exception, error: # noqa raise RetryableError(error) finally: bytes_read = callback_data["bytes_read"] logging.info("Read %d bytes from %s", bytes_read, url) # Use ETag if available, otherwise content-md5(v2) or # X-Image-Meta-Checksum(v1) etag = response.info().getheader("etag", None) if etag is None: etag = response.info().getheader("content-md5", None) if etag is None: etag = response.info().getheader("x-image-meta-checksum", None) # Verify checksum using ETag
# don't have a useful __repr__ or __str__ raise RetryableError('%s: %s' % (error.__class__.__name__, error)) url = request.get_full_url() logging.info("Reading image data from %s" % url) callback_data = {'bytes_read': 0} checksum = md5.new() def update_md5(chunk): callback_data['bytes_read'] += len(chunk) checksum.update(chunk) try: try: utils.extract_tarball(response, staging_path, callback=update_md5) except Exception, error: # noqa raise RetryableError(error) finally: bytes_read = callback_data['bytes_read'] logging.info("Read %d bytes from %s", bytes_read, url) # Use ETag if available, otherwise content-md5(v2) or # X-Image-Meta-Checksum(v1) etag = response.info().getheader('etag', None) if etag is None: etag = response.info().getheader('content-md5', None) if etag is None: etag = response.info().getheader('x-image-meta-checksum', None) # Verify checksum using ETag