def run_feeds_update(cls, json_obj=None, force_flush=False): """ Creates a task and runs it, optionally with a thread if locking is enabled. :return: """ error = None feeds = None with session_scope() as session: mgr = identities.manager_factory.for_session(session) system_user = mgr.get_system_credentials() catalog_client = CatalogClient(user=system_user[0], password=system_user[1]) try: feeds = get_selected_feeds_to_sync(localconfig.get_config()) if json_obj: task = cls.from_json(json_obj) if not task: return None task.feeds = feeds else: task = FeedsUpdateTask(feeds_to_sync=feeds, flush=force_flush) # Create feed task begin event try: catalog_client.add_event(FeedSyncStart(groups=feeds if feeds else 'all')) except: log.exception('Ignoring event generation error before feed sync') result = [] if cls.locking_enabled: # system_user = get_system_user_auth() run_target_with_lease(user_auth=system_user, lease_id='feed_sync', ttl=90, target=lambda: result.append(task.execute())) # A bit of work-around for the lambda def to get result from thread execution if result: result = result[0] else: result = task.execute() return result except LeaseAcquisitionFailedError as ex: error = ex log.exception('Could not acquire lock on feed sync, likely another sync already in progress') raise Exception('Cannot execute feed sync, lock is held by another feed sync in progress') except Exception as e: error = e log.exception('Error executing feeds update') raise e finally: # log feed sync event try: if error: catalog_client.add_event(FeedSyncFail(groups=feeds if feeds else 'all', error=error)) else: catalog_client.add_event(FeedSyncComplete(groups=feeds if feeds else 'all')) except: log.exception('Ignoring event generation error after feed sync')
def test_run_target_with_lease_ok(): global SimpleQueueClient SimpleQueueClient.acquire_lease = acquire_lease_mock(fail=False) SimpleQueueClient.refresh_lease = refresh_lease_mock(fail=False) SimpleQueueClient.describe_lease = describe_lease_mock(fail=False) SimpleQueueClient.create_lease = create_lease_mock(fail=False) SimpleQueueClient.release_lease = release_lease_mock(fail=False) # Pre-load the cache to ensure no db hit needed IdentityManager._credential_cache.cache_it( 'anchore-system', HttpBasicCredential('anchore-system', 'somepass')) run_target_with_lease('user', 'test_lease', pass_target, client_id='test1')
def test_run_target_with_lease_conn_error(): global SimpleQueueClient SimpleQueueClient.acquire_lease = acquire_lease_mock(fail=True) SimpleQueueClient.refresh_lease = refresh_lease_mock(fail=True) SimpleQueueClient.describe_lease = describe_lease_mock(fail=False) SimpleQueueClient.create_lease = create_lease_mock(fail=False) SimpleQueueClient.release_lease = release_lease_mock(fail=False) with pytest.raises(Exception) as raised_ex: run_target_with_lease("user", "test_lease", pass_target, client_id="test1") logger.info("Caught: {}".format(raised_ex))
def test_run_target_with_lease_ok(): global SimpleQueueClient SimpleQueueClient.acquire_lease = acquire_lease_mock(fail=False) SimpleQueueClient.refresh_lease = refresh_lease_mock(fail=False) SimpleQueueClient.describe_lease = describe_lease_mock(fail=False) SimpleQueueClient.create_lease = create_lease_mock(fail=False) SimpleQueueClient.release_lease = release_lease_mock(fail=False) run_target_with_lease(('user', 'pass'), 'test_lease', pass_target, client_id='test1')
def run_feeds_update(cls, json_obj=None, force_flush=False) -> Optional[List[FeedSyncResult]]: """ Creates a task and runs it, optionally with a thread if locking is enabled. :return: """ try: vulnerabilities_provider = get_vulnerabilities_provider() sync_configs = compute_selected_configs_to_sync( vulnerabilities_provider.get_config_name(), get_section_for_vulnerabilities(), vulnerabilities_provider.get_default_sync_config(), ) if json_obj: task = cls.from_json(json_obj) if not task: return None task.sync_configs = sync_configs else: task = FeedsUpdateTask(sync_configs=sync_configs, flush=force_flush) result = [] if cls.locking_enabled: run_target_with_lease( account=None, lease_id="feed_sync", ttl=90, target=lambda: result.append(task.execute()), ) # A bit of work-around for the lambda def to get result from thread execution if result: result = result[0] else: result = task.execute() return result except Exception: logger.exception("Error executing feeds update") raise
def run_feeds_update(cls, json_obj=None, force_flush=False): """ Creates a task and runs it, optionally with a thread if locking is enabled. :return: """ feeds = None try: feeds = get_selected_feeds_to_sync(localconfig.get_config()) if json_obj: task = cls.from_json(json_obj) if not task: return None task.feeds = feeds else: task = FeedsUpdateTask(feeds_to_sync=feeds, flush=force_flush) result = [] if cls.locking_enabled: run_target_with_lease( account=None, lease_id="feed_sync", ttl=90, target=lambda: result.append(task.execute()), ) # A bit of work-around for the lambda def to get result from thread execution if result: result = result[0] else: result = task.execute() return result except Exception: logger.exception("Error executing feeds update") raise
def run_feeds_update(cls, json_obj=None, force_flush=False): """ Creates a task and runs it, optionally with a thread if locking is enabled. :return: """ error = None feeds = None with session_scope() as session: mgr = identities.manager_factory.for_session(session) catalog_client = internal_client_for(CatalogClient, userId=None) try: feeds = get_selected_feeds_to_sync(localconfig.get_config()) if json_obj: task = cls.from_json(json_obj) if not task: return None task.feeds = feeds else: task = FeedsUpdateTask(feeds_to_sync=feeds, flush=force_flush) # Create feed task begin event try: catalog_client.add_event( FeedSyncStart(groups=feeds if feeds else 'all')) except: log.exception( 'Ignoring event generation error before feed sync') result = [] if cls.locking_enabled: run_target_with_lease( account=None, lease_id='feed_sync', ttl=90, target=lambda: result.append(task.execute())) # A bit of work-around for the lambda def to get result from thread execution if result: result = result[0] else: result = task.execute() return result except Exception as e: error = e log.exception('Error executing feeds update') raise e finally: # log feed sync event try: if error: catalog_client.add_event( FeedSyncFail(groups=feeds if feeds else 'all', error=error)) else: catalog_client.add_event( FeedSyncComplete(groups=feeds if feeds else 'all')) except: log.exception( 'Ignoring event generation error after feed sync')