def _check_trigger_sync(pre_image, image): """ check if it is the case that the cascaded glance has upload or first patch location. """ return pre_image.status in ('saving', 'queued') and image.size and \ [l for l in image.locations if not utils.is_glance_location(l['url'])]
def create_new_cascaded_task(self, last_run_time=None): LOG.debug(_('new_cascaded periodic task has been created.')) glance_client = create_self_glance_client(self.ks_client.auth_token) filters = {'status': 'active'} image_list = glance_client.images.list(filters=filters) input = {} run_images = {} cascading_ep = s_utils.get_cascading_endpoint_url() input['cascading_ep'] = cascading_ep input['image_id'] = 'ffffffff-ffff-ffff-ffff-ffffffffffff' all_ep_urls = s_utils.get_endpoints() for image in image_list: glance_urls = [loc['url'] for loc in image.locations if s_utils.is_glance_location(loc['url'])] lack_ep_urls = s_utils.calculate_lack_endpoints(all_ep_urls, glance_urls) if lack_ep_urls: image_core_props = s_utils.get_core_properties(image) run_images[image.id] = {'body': image_core_props, 'locations': lack_ep_urls} if not run_images: LOG.debug(_('No images need to sync to new cascaded glances.')) input['images'] = run_images return TaskObject.get_instance('periodic_add', input, last_run_time=last_run_time)
def create_new_cascaded_task(self, last_run_time=None): LOG.debug(_('new_cascaded periodic task has been created.')) glance_client = create_self_glance_client(self.ks_client.auth_token) filters = {'status': 'active'} image_list = glance_client.images.list(filters=filters) input = {} run_images = {} cascading_ep = s_utils.get_cascading_endpoint_url() input['cascading_ep'] = cascading_ep input['image_id'] = 'ffffffff-ffff-ffff-ffff-ffffffffffff' all_ep_urls = s_utils.get_endpoints() for image in image_list: glance_urls = [ loc['url'] for loc in image.locations if s_utils.is_glance_location(loc['url']) ] lack_ep_urls = s_utils.calculate_lack_endpoints( all_ep_urls, glance_urls) if lack_ep_urls: image_core_props = s_utils.get_core_properties(image) run_images[image.id] = { 'body': image_core_props, 'locations': lack_ep_urls } if not run_images: LOG.debug(_('No images need to sync to new cascaded glances.')) input['images'] = run_images return TaskObject.get_instance('periodic_add', input, last_run_time=last_run_time)
def removing_locations(self, image_id, auth_token, locs): if CONF.sync.sync_strategy == 'None': return locs = filter(lambda loc: s_utils.is_glance_location(loc['url']), locs) if not locs: return input = {'image_id': image_id, 'locations': locs} remove_locs_task = TaskObject.get_instance('locs_remove', input) self.task_queue.put_nowait(remove_locs_task)
def get_mappings_from_locations(locations): mappings = {} for loc in locations: if s_utils.is_glance_location(loc['url']): id = loc['metadata'].get('image_id') if not id: continue ep_url = s_utils.create_ep_by_loc(loc) mappings[ep_url] = id # endpoints.append(utils.create_ep_by_loc(loc)) return mappings
def _get_candidate_path(self, auth_token, from_ep, image_id, scheme='file'): g_client = create_glance_client(auth_token, from_ep) image = g_client.images.get(image_id) locs = image.locations or [] for loc in locs: if s_utils.is_glance_location(loc['url']): continue if loc['url'].startswith(scheme): if scheme == 'file': return loc['url'][len('file://'):] return loc['url'] return None
def _stil_need_synced(self, cascaded_ep, image_id, auth_token): g_client = s_utils.create_self_glance_client(auth_token) try: image = g_client.images.get(image_id) except Exception: LOG.warn(_('The add cascaded periodic task checks that the image ' 'has deleted, no need to sync. id is %s' % image_id)) return False else: if image.status != 'active': LOG.warn(_('The add cascaded period task checks image status ' 'not active, no need to sync.' 'image id is %s.' % image_id)) return False ep_list = [loc['url'] for loc in image.locations if s_utils.is_glance_location(loc['url'])] return not s_utils.is_ep_contains(cascaded_ep, ep_list)
def _stil_need_synced(self, cascaded_ep, image_id, auth_token): g_client = s_utils.create_self_glance_client(auth_token) try: image = g_client.images.get(image_id) except Exception: LOG.warn( _('The add cascaded periodic task checks that the image ' 'has deleted, no need to sync. id is %s' % image_id)) return False else: if image.status != 'active': LOG.warn( _('The add cascaded period task checks image status ' 'not active, no need to sync.' 'image id is %s.' % image_id)) return False ep_list = [ loc['url'] for loc in image.locations if s_utils.is_glance_location(loc['url']) ] return not s_utils.is_ep_contains(cascaded_ep, ep_list)
def sync_image_data(self, image_id, auth_token, eps=None, **kwargs): if CONF.sync.sync_strategy in ['None', 'nova']: return kwargs['image_id'] = image_id cascading_ep = s_utils.get_cascading_endpoint_url() kwargs['cascading_ep'] = cascading_ep copy_url = kwargs.get('src_image_url', None) if not copy_url: LOG.warn(_('No copy url found, for image %s sync, Exit.'), image_id) return LOG.info(_('choose the copy url %s for sync image %s'), copy_url, image_id) if s_utils.is_glance_location(copy_url): kwargs['copy_ep'] = s_utils.create_ep_by_loc_url(copy_url) kwargs['copy_id'] = s_utils.get_id_from_glance_loc_url(copy_url) else: kwargs['copy_ep'] = cascading_ep kwargs['copy_id'] = image_id self.task_queue.put_nowait(TaskObject.get_instance('sync', kwargs))
def adding_locations(self, image_id, auth_token, locs, **kwargs): if CONF.sync.sync_strategy == 'None': return for loc in locs: if s_utils.is_glance_location(loc['url']): if s_utils.is_snapshot_location(loc): snapshot_ep = s_utils.create_ep_by_loc(loc) snapshot_id = s_utils.get_id_from_glance_loc(loc) snapshot_client = create_glance_client( auth_token, snapshot_ep) snapshot_image = snapshot_client.images.get(snapshot_id) _pre_check_time = timeutils.utcnow() _timout = CONF.sync.snapshot_timeout while not timeutils.is_older_than(_pre_check_time, _timout): if snapshot_image.status == 'active': break LOG.debug( _('Check snapshot not active, wait for %i' 'second.' % CONF.sync.snapshot_sleep_interval)) time.sleep(CONF.sync.snapshot_sleep_interval) snapshot_image = snapshot_client.images.get( snapshot_id) if snapshot_image.status != 'active': LOG.error(_('Snapshot status to active Timeout')) return kwargs['image_id'] = image_id kwargs['snapshot_ep'] = snapshot_ep kwargs['snapshot_id'] = snapshot_id snapshot_task = TaskObject.get_instance('snapshot', kwargs) self.task_queue.put_nowait(snapshot_task) else: LOG.debug( _('patch a normal location %s to image %s' % (loc['url'], image_id))) input = {'image_id': image_id, 'location': loc} self.task_queue.put_nowait( TaskObject.get_instance('patch', input))
def adding_locations(self, image_id, auth_token, locs, **kwargs): if CONF.sync.sync_strategy == 'None': return for loc in locs: if s_utils.is_glance_location(loc['url']): if s_utils.is_snapshot_location(loc): snapshot_ep = s_utils.create_ep_by_loc(loc) snapshot_id = s_utils.get_id_from_glance_loc(loc) snapshot_client = create_glance_client(auth_token, snapshot_ep) snapshot_image = snapshot_client.images.get(snapshot_id) _pre_check_time = timeutils.utcnow() _timout = CONF.sync.snapshot_timeout while not timeutils.is_older_than(_pre_check_time, _timout): if snapshot_image.status == 'active': break LOG.debug(_('Check snapshot not active, wait for %i' 'second.' % CONF.sync.snapshot_sleep_interval)) time.sleep(CONF.sync.snapshot_sleep_interval) snapshot_image = snapshot_client.images.get( snapshot_id) if snapshot_image.status != 'active': LOG.error(_('Snapshot status to active Timeout')) return kwargs['image_id'] = image_id kwargs['snapshot_ep'] = snapshot_ep kwargs['snapshot_id'] = snapshot_id snapshot_task = TaskObject.get_instance('snapshot', kwargs) self.task_queue.put_nowait(snapshot_task) else: LOG.debug(_('patch a normal location %s to image %s' % (loc['url'], image_id))) input = {'image_id': image_id, 'location': loc} self.task_queue.put_nowait(TaskObject.get_instance('patch', input))