def pull_image(self, context, repo, tag, image_pull_policy): # TODO(shubhams): glance driver does not handle tags # once metadata is stored in db then handle tags image_loaded = False image = self._search_image_on_host(context, repo) if image: image_path = image['path'] image_checksum = image['checksum'] md5sum = hashlib.md5() with open(image_path, 'rb') as fd: while True: # read 10MB of data each time data = fd.read(10 * 1024 * 1024) if not data: break md5sum.update(data) md5sum = md5sum.hexdigest() if md5sum == image_checksum: image_loaded = True return image, image_loaded if not common_utils.should_pull_image(image_pull_policy, bool(image)): if image: LOG.debug('Image %s present locally', repo) image_loaded = True return image, image_loaded else: message = _( 'Image %s not present with pull policy of Never') % repo raise exception.ImageNotFound(message) LOG.debug('Pulling image from glance %s', repo) try: image_meta = utils.find_image(context, repo) LOG.debug('Image %s was found in glance, downloading now...', repo) image_chunks = utils.download_image_in_chunks( context, image_meta.id) except exception.ImageNotFound: LOG.error('Image %s was not found in glance', repo) raise except Exception as e: msg = _('Cannot download image from glance: {0}') raise exception.ZunException(msg.format(e)) try: images_directory = CONF.glance.images_directory fileutils.ensure_tree(images_directory) out_path = os.path.join(images_directory, image_meta.id + '.tar') with open(out_path, 'wb') as fd: for chunk in image_chunks: fd.write(chunk) except Exception as e: msg = _('Error occurred while writing image: {0}') raise exception.ZunException(msg.format(e)) LOG.debug('Image %(repo)s was downloaded to path : %(path)s', { 'repo': repo, 'path': out_path }) return {'image': repo, 'path': out_path}, image_loaded
def pull_image(self, context, repo, tag, image_pull_policy, registry): image_loaded = False image = self._search_image_on_host(context, repo, tag) if not common_utils.should_pull_image(image_pull_policy, bool(image)): if image: if self._verify_md5sum_for_image(image): image_loaded = True return image, image_loaded else: message = _( 'Image %s not present with pull policy of Never') % repo raise exception.ImageNotFound(message) LOG.debug('Pulling image from glance %s', repo) try: image_meta = utils.find_image(context, repo, tag) LOG.debug('Image %s was found in glance, downloading now...', repo) image_chunks = utils.download_image_in_chunks( context, image_meta.id) except exception.ImageNotFound: LOG.error('Image %s was not found in glance', repo) raise except Exception as e: msg = _('Cannot download image from glance: {0}') raise exception.ZunException(msg.format(e)) try: images_directory = CONF.glance.images_directory fileutils.ensure_tree(images_directory) out_path = os.path.join(images_directory, image_meta.id + '.tar') with open(out_path, 'wb') as fd: for chunk in image_chunks: fd.write(chunk) except Exception as e: msg = _('Error occurred while writing image: {0}') raise exception.ZunException(msg.format(e)) LOG.debug('Image %(repo)s was downloaded to path : %(path)s', { 'repo': repo, 'path': out_path }) image = { 'image': image_meta.name, 'tags': image_meta.tags, 'path': out_path } return image, image_loaded
def pull_image(self, context, repo, tag, image_pull_policy, registry): image_loaded = False image = self._search_image_on_host(context, repo, tag) if not common_utils.should_pull_image(image_pull_policy, bool(image)): if image: if self._verify_md5sum_for_image(image): image_loaded = True return image, image_loaded else: message = _('Image %s not present with pull policy of Never' ) % repo raise exception.ImageNotFound(message) LOG.debug('Pulling image from glance %s', repo) try: image_meta = utils.find_image(context, repo, tag) LOG.debug('Image %s was found in glance, downloading now...', repo) image_chunks = utils.download_image_in_chunks(context, image_meta.id) except exception.ImageNotFound: LOG.error('Image %s was not found in glance', repo) raise except Exception as e: msg = _('Cannot download image from glance: {0}') raise exception.ZunException(msg.format(e)) try: images_directory = CONF.glance.images_directory fileutils.ensure_tree(images_directory) out_path = os.path.join(images_directory, image_meta.id + '.tar') with open(out_path, 'wb') as fd: for chunk in image_chunks: fd.write(chunk) except Exception as e: msg = _('Error occurred while writing image: {0}') raise exception.ZunException(msg.format(e)) LOG.debug('Image %(repo)s was downloaded to path : %(path)s', {'repo': repo, 'path': out_path}) image = {'image': image_meta.name, 'tags': image_meta.tags, 'path': out_path} return image, image_loaded