def test_is_rhic_lookup_task_expired(self): # Create a valid, in_progress task task_a = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a11111111111", completed=False) task_a.save() self.assertFalse(identity.is_rhic_lookup_task_expired(task_a)) # Create a completed task task_b = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a22222222222", completed=True, initiated=datetime.now(tzutc()), modified=datetime.now(tzutc())) task_b.save() self.assertFalse(identity.is_rhic_lookup_task_expired(task_b)) # Create a timedout incomplete task cfg = config.get_rhic_serve_config_info() timeout_in_minutes = cfg["single_rhic_lookup_timeout_in_minutes"] expired_time = datetime.now(tzutc()) - timedelta(minutes=timeout_in_minutes+1) task_c = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a333333333333", completed=False, initiated=expired_time) task_c.save() self.assertTrue(identity.is_rhic_lookup_task_expired(task_c)) # Create a completed expired task expired_hours = cfg["single_rhic_lookup_cache_unknown_in_hours"] expired_time = datetime.now(tzutc()) - timedelta(hours=expired_hours+1) task_d = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a444444444444", completed=True, modified=expired_time) task_d.save() self.assertTrue(identity.is_rhic_lookup_task_expired(task_d))
def test_purge_expired_rhic_lookups(self): cfg = config.get_rhic_serve_config_info() # Create a valid, in_progress task task_a = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a11111111111", completed=False) task_a.save() # Create a completed task task_b = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a22222222222", completed=True, initiated=datetime.now(tzutc()), modified=datetime.now(tzutc())) task_b.save() # Create a timedout incomplete task timeout_in_minutes = cfg["single_rhic_lookup_timeout_in_minutes"] expired_time = datetime.now(tzutc()) - timedelta(minutes=timeout_in_minutes+1) task_c = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a333333333333", completed=False, initiated=expired_time) task_c.save() # Create a completed expired task expired_hours = cfg["single_rhic_lookup_cache_unknown_in_hours"] expired_time = datetime.now(tzutc()) - timedelta(hours=expired_hours+1) task_d = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a444444444444", completed=True, modified=expired_time) task_d.save() identity.purge_expired_rhic_lookups() found = RHICLookupTask.objects() self.assertEquals(len(found), 2) for f in found: self.assertTrue(f.uuid in [task_a.uuid, task_b.uuid]) self.assertTrue(f.uuid not in [task_c.uuid, task_d.uuid])
def sync_from_rhic_serve_blocking(): _LOG.info("Attempting to synchronize RHIC data from configured rhic_serve") current_time = datetime.now(tzutc()) cfg = config.get_rhic_serve_config_info() # Lookup last time we synced from this host # Sync records from that point in time. # If we haven't synced before we get back None and proceed with a full sync server_hostname = cfg["host"] last_sync = get_last_sync_timestamp(server_hostname) current_offset=0 current_limit=cfg["sync_all_rhics_pagination_limit_per_call"] sync_loop = True while sync_loop: data, meta = rhic_serve_client.get_all_rhics(host=server_hostname, port=cfg["port"], url=cfg["rhics_url"], last_sync=last_sync, offset=current_offset, limit=current_limit) if not data: _LOG.info("Received no data from %s:%s%s" % (cfg["host"], cfg["port"], cfg["rhics_url"])) return True _LOG.info("Fetched %s RHICs from %s:%s%s with last_sync=%s, offset=%s, limit=%s" % (len(data), cfg["host"], cfg["port"], cfg["rhics_url"], last_sync, current_offset, current_limit)) syncd_uuids = process_data(data) current_offset = current_offset + len(syncd_uuids) if current_offset >= meta["total_count"]: break if not save_last_sync(server_hostname, current_time): _LOG.info("Unable to update last sync for: %s at %s" % (server_hostname, current_time)) return False return True
def is_rhic_lookup_task_expired(current_task): cfg = config.get_rhic_serve_config_info() if not current_task.completed: # Task is in progress, ensure that it's initiated time is within timeout range timeout_in_minutes = cfg["single_rhic_lookup_timeout_in_minutes"] threshold = current_task.initiated + timedelta(minutes=timeout_in_minutes) if not threshold.tzinfo: threshold = pytz.UTC.localize(threshold) if threshold < datetime.now(tzutc()): _LOG.info("Task has timed out, threshold was: %s. Task = <%s>" % (threshold, current_task)) # Current time is greater than the threshold this task had to stay alive # It is expired return True else: # Task has completed, check if it's within cached time boundaries valid_hours = cfg["single_rhic_lookup_cache_unknown_in_hours"] modified = current_task.modified if not modified.tzinfo: modified = pytz.UTC.localize(modified) threshold = datetime.now(tzutc()) - timedelta(hours=valid_hours) if modified < threshold: _LOG.info("Cached task has expired, threshold was: %s. Task = <%s>" % (threshold, current_task)) # Task was modified more than # hours ago # It is expired return True return False
def get_single_rhic(host, port, url, uuid): cfg = config.get_rhic_serve_config_info() url = url + uuid + "/" try: conn = get_connection(host, port, cfg["client_cert"], cfg["client_key"]) return conn.GET(url) except Exception, e: _LOG.exception("Caught exception from 'get_single_rhic' with config info: %s" % (cfg)) raise
def check_certs(): status = _check_path(config.get_splice_server_identity_cert_path(), "[security].splice_server_identity_cert") status &= _check_path(config.get_splice_server_identity_ca_path(), "[security].splice_server_identity_ca") status &= _check_path(config.get_splice_server_identity_key_path(), "[security].splice_server_identity_key") status &= _check_path(config.get_rhic_ca_path(), "[security].rhic_ca_path") rhic_serve_cfg = config.get_rhic_serve_config_info() status &= _check_path(rhic_serve_cfg["client_key"], "[rhic_serve].client_key") status &= _check_path(rhic_serve_cfg["client_cert"], "[rhic_serve].client_cert") return status
def test_get_current_rhic_lookup_tasks(self): cfg = config.get_rhic_serve_config_info() # Create a valid, in_progress task task_a = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a11111111111", completed=False) task_a.save() # Create a completed task task_b = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a22222222222", completed=True, initiated=datetime.now(tzutc()), modified=datetime.now(tzutc())) task_b.save() # Create a timedout incomplete task timeout_in_minutes = cfg["single_rhic_lookup_timeout_in_minutes"] expired_time = datetime.now(tzutc()) - timedelta(minutes=timeout_in_minutes+1) task_c = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a333333333333", completed=False, initiated=expired_time) task_c.save() # Create a completed expired task expired_hours = cfg["single_rhic_lookup_cache_unknown_in_hours"] expired_time = datetime.now(tzutc()) - timedelta(hours=expired_hours+1) task_d = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a444444444444", completed=True, modified=expired_time) task_d.save() # Ensure all tasks where created and have been saved in mongo current_tasks = [x.uuid for x in RHICLookupTask.objects()] for t in [task_a, task_b, task_c, task_d]: self.assertTrue(t.uuid in current_tasks) # In-progress, valid task task = identity.get_current_rhic_lookup_tasks(task_a.uuid) self.assertIsNotNone(task) self.assertEquals(task.uuid, task_a.uuid) # Completed, valid task task = identity.get_current_rhic_lookup_tasks(task_b.uuid) self.assertIsNotNone(task) self.assertEquals(task.uuid, task_b.uuid) # In-progress, timed out task task = identity.get_current_rhic_lookup_tasks(task_c.uuid) self.assertIsNone(task) found = [x.uuid for x in RHICLookupTask.objects()] self.assertTrue(task_c.uuid not in found) # Completed, cache time expired task task = identity.get_current_rhic_lookup_tasks(task_d.uuid) self.assertIsNone(task) found = [x.uuid for x in RHICLookupTask.objects()] self.assertTrue(task_d.uuid not in found) # Be sure of the 4 tasks we created, the expired and timedout were removed # while the 2 good tasks remained self.assertEquals(len(found), 2) self.assertTrue(task_a.uuid in found) self.assertTrue(task_b.uuid in found)
def get_all_rhics(host, port, url, last_sync=None, offset=None, limit=None, accept_gzip=True): cfg = config.get_rhic_serve_config_info() try: conn = get_connection(host, port, cfg["client_cert"], cfg["client_key"], accept_gzip=accept_gzip) url_with_params = _form_url(url, last_sync, offset, limit) status, data = conn.GET(url_with_params) if status == 200: return data["objects"], data["meta"] raise RequestException(status, data) except Exception, e: _LOG.exception("Caught exception from 'get_all_rhics' with config info: %s" % (cfg)) raise
def sync_single_rhic_blocking(uuid): cfg = config.get_rhic_serve_config_info() _LOG.info("Attempting to synchronize a single RHIC '%s' with config info: '%s'" % (uuid, cfg)) host = cfg["host"] port = cfg["port"] # URL is based on URL for 'all_rhics', we add the RHIC uuid to it to form a single URL url = cfg["rhics_url"] status_code, data = rhic_serve_client.get_single_rhic(host=host, port=port, url=url, uuid=uuid) _LOG.info("Received '%s' from %s:%s:%s for RHIC '%s'. Response = \n%s" % (status_code, host, port, url, uuid, data)) if status_code == 202: # This task is in progress, nothing further to do return status_code if status_code == 200: create_or_update_consumer_identity(data) return status_code
def test_get_in_progress_rhic_lookups(self): # Create a valid, in_progress task task_a = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a11111111111", completed=False) task_a.save() # Create a completed task task_b = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a22222222222", completed=True, initiated=datetime.now(tzutc()), modified=datetime.now(tzutc())) task_b.save() # Create a timedout incomplete task cfg = config.get_rhic_serve_config_info() timeout_in_minutes = cfg["single_rhic_lookup_timeout_in_minutes"] expired_time = datetime.now(tzutc()) - timedelta(minutes=timeout_in_minutes+1) task_c = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a333333333333", completed=False, initiated=expired_time) task_c.save() current_tasks = identity.get_in_progress_rhic_lookups() self.assertEquals(len(current_tasks), 1) self.assertEquals(current_tasks[0].uuid, task_a.uuid)
try: conn = get_connection(host, port, cfg["client_cert"], cfg["client_key"], accept_gzip=accept_gzip) url_with_params = _form_url(url, last_sync, offset, limit) status, data = conn.GET(url_with_params) if status == 200: return data["objects"], data["meta"] raise RequestException(status, data) except Exception, e: _LOG.exception("Caught exception from 'get_all_rhics' with config info: %s" % (cfg)) raise if __name__ == "__main__": from datetime import timedelta from datetime import datetime from dateutil.tz import tzutc last_sync = datetime.now(tzutc()) - timedelta(days=30) config.init(settings.SPLICE_CONFIG_FILE) cfg = config.get_rhic_serve_config_info() data, meta = get_all_rhics(host=cfg["host"], port=cfg["port"], url=cfg["rhics_url"], offset=0, limit=1000, last_sync=last_sync, accept_gzip=True) print "--- Test Sync all RHICs ---" print data if len(data) > 0: uuid = data[0]["uuid"] print "\n---Test A Single RHIC ---\n" print get_single_rhic(host=cfg["host"], port=cfg["port"], url=cfg["rhics_url"], uuid=uuid) print "\n -- Test an unknown RHIC ---\n" uuid = "1a1aa1aa-f6f4-45be-9d86-deb97a79d181" print get_single_rhic(host=cfg["host"], port=cfg["port"], url=cfg["rhics_url"], uuid=uuid)