def get_next_available_server(client_class, username, failed_host=None, component=None, create_client_fn=create_client): ''' Given a failed host, attempts to find the next available host and returns a Sentry server if found, as well as a list of all Sentry hosts attempted. ''' current_host = failed_host has_next = True attempted_hosts = [] while has_next: LOG.warn('Could not connect to Sentry server %s, attempting to fetch next available client.' % current_host) next_server = get_sentry_server(current_host=current_host) time.sleep(1) try: client = create_client_fn(client_class, username, next_server, component) client.list_sentry_roles_by_group(groupName='*') # If above operation succeeds, return client LOG.info('Successfully connected to Sentry server %s, after attempting [%s], returning client.' % (client.host, ', '.join(attempted_hosts))) return next_server, attempted_hosts except StructuredThriftTransportException, e: # If we have come back around to the original failed client, exit if client.host == failed_host: has_next = False else: current_host = client.host attempted_hosts.append(current_host) except Exception, e: raise PopupException(_('Encountered unexpected error while trying to find available Sentry client: %s' % e))
def test_get_random_sentry_server(self): # Test that with no current_host, a server for a random host is returned xml = self._sentry_site_xml(rpc_addresses='%s,host-1,host-2' % self.rpc_addresses, rpc_port=self.rpc_port) file(os.path.join(self.tmpdir, 'sentry-site.xml'), 'w').write(xml) sentry_site.reset() server = get_sentry_server() assert_true(server is not None) assert_true(server['hostname'] in '%s,host-1,host-2' % self.rpc_addresses)
def test_get_single_sentry_server(self): # Test that with a current host and single server, the single server is returned xml = self._sentry_site_xml(rpc_addresses='host-1', rpc_port=self.rpc_port) file(os.path.join(self.tmpdir, 'sentry-site.xml'), 'w').write(xml) sentry_site.reset() server = get_sentry_server(current_host='host-1') assert_true(server is not None) assert_equal(server['hostname'], 'host-1')
def test_get_first_sentry_server(self): # Test that if the current host is the last host of multiple servers, the first server is returned xml = self._sentry_site_xml(rpc_addresses='host-1,%s,host-2' % self.rpc_addresses, rpc_port=self.rpc_port) file(os.path.join(self.tmpdir, 'sentry-site.xml'), 'w').write(xml) sentry_site.reset() server = get_sentry_server(current_host='host-2') assert_true(server is not None) assert_equal(server['hostname'], 'host-1')
def get_cached_server(current_host=None): global API_CACHE if current_host and API_CACHE is not None: clear_api_cache() if API_CACHE is None: server = get_sentry_server(current_host) if server is not None: set_api_cache(server) else: raise PopupException(_('Failed to find an available Sentry server.')) else: LOG.debug("Returning cached Sentry server: %s:%s" % (API_CACHE['hostname'], API_CACHE['port'])) return API_CACHE