def pull_image(self, context, repo, tag, image_pull_policy): image_loaded = True image = self._search_image_on_host(repo, tag) if not utils.should_pull_image(image_pull_policy, bool(image)): if image: LOG.debug('Image %s present locally' % repo) return image, image_loaded else: message = _( 'Image %s not present with pull policy of Never') % repo raise exception.ImageNotFound(message) try: LOG.debug('Pulling image from docker %s,' ' context %s' % (repo, context)) self._pull_image(repo, tag) return {'image': repo, 'path': None}, image_loaded except exception.ImageNotFound: with excutils.save_and_reraise_exception(): LOG.error('Image %s was not found in docker repo' % repo) except exception.DockerError: with excutils.save_and_reraise_exception(): LOG.error('Docker API error occurred during downloading\ image %s' % repo) except Exception as e: msg = _('Cannot download image from docker: {0}') raise exception.ZunException(msg.format(e))
def pull_image(self, context, repo, tag, image_pull_policy, registry): image_loaded = True image = self._search_image_on_host(repo, tag) if not utils.should_pull_image(image_pull_policy, bool(image)): if image: LOG.debug('Image %s present locally', repo) return image, image_loaded else: message = _('Image %s not present with pull policy of Never' ) % repo raise exception.ImageNotFound(message) try: LOG.debug('Pulling image from docker %(repo)s,' ' context %(context)s', {'repo': repo, 'context': context}) self._pull_image(repo, tag, registry) return {'image': repo, 'path': None}, image_loaded except exception.ImageNotFound: with excutils.save_and_reraise_exception(): LOG.error('Image %s was not found in docker repo', repo) except exception.DockerError: with excutils.save_and_reraise_exception(): LOG.error('Docker API error occurred during downloading ' 'image %s', repo) except Exception as e: msg = _('Cannot download image from docker: {0}') raise exception.ZunException(msg.format(e))
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 test_should_pull_image(self): self.assertFalse(utils.should_pull_image('never', True)) self.assertFalse(utils.should_pull_image('never', False)) self.assertTrue(utils.should_pull_image('always', True)) self.assertTrue(utils.should_pull_image('always', False)) self.assertTrue(utils.should_pull_image('ifnotpresent', False)) self.assertFalse(utils.should_pull_image('ifnotpresent', True))
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
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 = self._search_image_on_host(context, repo) if not common_utils.should_pull_image(image_pull_policy, bool(image)): if image: LOG.debug('Image %s present locally' % repo) return image 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: glance = utils.create_glanceclient(context) image_meta = utils.find_image(context, repo) LOG.debug('Image %s was found in glance, downloading now...' % repo) image_chunks = glance.images.data(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 %s was downloaded to path : %s' % (repo, out_path)) return {'image': repo, 'path': out_path}