def get_elasticsearch_connection(): es_conn = env.get_service(label='elasticsearch-swarm-1.7.1') if es_conn: es = ElasticSearch(es_conn.get_url(url='uri')) else: es = ElasticSearch('http://localhost:9200') return es
def get_elasticsearch_connection(): es_conn = env.get_service(name='fec-api-search') if es_conn: es = Elasticsearch([es_conn.get_url(url='uri')]) else: es = Elasticsearch(['http://localhost:9200']) return es
def get_elasticsearch_connection(): es_conn = env.get_service(label='elasticsearch-swarm-1.7.1') if es_conn: es = ElasticSearch(es_conn.get_url(url='uri')) else: es = ElasticSearch('http://localhost:9200') return es
def get_elasticsearch_connection(): es_conn = env.get_service(name='fec-api-search56') if es_conn: url = es_conn.get_url(url='uri') else: url = 'http://localhost:9200' es = Elasticsearch(url, timeout=30, max_retries=10, retry_on_timeout=True) return es
def get_elasticsearch_connection(): es_conn = env.get_service(name='fec-api-search') if es_conn: url = es_conn.get_url(url='uri') else: url = 'http://localhost:9200' es = Elasticsearch(url) return es
def redis_url(): """ Retrieve the URL needed to connect to a Redis instance, depending on environment. When running in a cloud.gov environment, retrieve the uri credential for the 'redis32' service. """ # Is the app running in a cloud.gov environment if env.space is not None: redis_env = env.get_service(label='redis32') redis_url = redis_env.credentials.get('uri') return redis_url return env.get_credential('FEC_REDIS_URL', 'redis://localhost:6379/0')
def redis_url(): """ Retrieves the URL needed to connect to a Redis instance. """ # Attempt to retrieve the space name the application is running in; this # will return the space if the app is running in a cloud.gov environment or # None if it is running locally. if env.space is not None: logger.info( 'Running in the {0} space in cloud.gov.'.format(env.space) ) # While we are not able to connect to Redis, retry as many times as # necessary. This is usually due to a brief 1 - 3 second downtime as # a service instance is rebooted in the cloud.gov environment. # TODO: Make this more robust in the case of extended outages. while True: logger.info('Attempting to connect to Redis...') redis = env.get_service(label='redis32') if redis is not None: logger.info('Successfully connected to Redis.') break else: logger.error('Could not connect to Redis, retrying...') # Construct the Redis instance URL based on the service information # returned. url = redis.get_url(host='hostname', password='******', port='port') return 'redis://{}'.format(url) else: logger.debug( 'Not running in a cloud.gov space, attempting to connect locally.' ) # Fall back to attempting to read whatever is set in the FEC_REDIS_URL # environment variable, otherwise a localhost connection. return env.get_credential('FEC_REDIS_URL', 'redis://localhost:6379/0')
'ofec_zips_districts', )) def load_table(name): try: return sa.Table(name, db.metadata, autoload_with=db.engine) except sa.exc.NoSuchTableError: return None def check_keys(keys, getter, formatter): missing = [key for key in keys if getter(key) is None] if missing: print(formatter.format(', '.join(keys))) return missing CHECKS = [ (REQUIRED_CREDS, env.get_credential, 'Missing creds {}'), (REQUIRED_SERVICES, lambda service: env.get_service(label=service), 'Missing services {}'), (REQUIRED_TABLES, load_table, 'Missing tables {}'), ] def check_config(): results = [check_keys(*check) for check in CHECKS] if any(results): raise RuntimeError('Invalid configuration: {0}'.format(results))
def redis_url(): redis = env.get_service(label='redis28') if redis: url = redis.get_url(host='hostname', password='******', port='port') return 'redis://{}'.format(url) return env.get_credential('FEC_REDIS_URL', 'redis://localhost:6379/0')
def load_table(name): try: return sa.Table(name, db.metadata, autoload_with=db.engine) except sa.exc.NoSuchTableError: return None def check_keys(keys, getter, formatter): missing = [key for key in keys if getter(key) is None] if missing: print(formatter.format(', '.join(keys))) return missing CHECKS = [ (REQUIRED_CREDS, env.get_credential, 'Missing creds {}'), ( REQUIRED_SERVICES, lambda service: env.get_service(label=service), 'Missing services {}', ), (REQUIRED_TABLES, load_table, 'Missing tables {}'), ] def check_config(): results = [check_keys(*check) for check in CHECKS] if any(results): raise RuntimeError('Invalid configuration: {0}'.format(results))
tuple(db.Model.metadata.tables.keys()) + ( 'ofec_pacronyms', 'ofec_nicknames', 'ofec_zips_districts', ) ) def load_table(name): try: return sa.Table(name, db.metadata, autoload_with=db.engine) except sa.exc.NoSuchTableError: return None def check_keys(keys, getter, formatter): missing = [key for key in keys if getter(key) is None] if missing: print(formatter.format(', '.join(keys))) return missing CHECKS = [ (REQUIRED_CREDS, env.get_credential, 'Missing creds {}'), (REQUIRED_SERVICES, lambda service: env.get_service(label=service), 'Missing services {}'), (REQUIRED_TABLES, load_table, 'Missing tables {}'), ] def check_config(): results = [check_keys(*check) for check in CHECKS] if any(results): raise RuntimeError('Invalid configuration')
import sqlalchemy as sa from flask_apispec import doc, marshal_with from webservices import args from webservices import docs from webservices import utils from webservices.utils import use_kwargs from webargs import fields from pyelasticsearch import ElasticSearch from webservices.env import env es_conn = env.get_service(label='elasticsearch-swarm-1.7.1') if es_conn: es = ElasticSearch(es_conn.get_url(url='uri')) else: es = ElasticSearch('http://localhost:9200') class Search(utils.Resource): @use_kwargs(args.query) def get(self, **kwargs): query = kwargs['q'] hits = es.search('_all: %s' % query, index='docs', size=10)['hits']['hits'] return {'results': hits}
def redis_url(): redis = env.get_service(label='redis28-swarm') if redis: url = redis.get_url(host='hostname', password='******', port='port') return 'redis://{}'.format(url) return os.getenv('FEC_REDIS_URL', 'redis://localhost:6379/0')
import sqlalchemy as sa from flask_apispec import doc, marshal_with from webservices import args from webservices import docs from webservices import utils from webservices.utils import use_kwargs from webargs import fields from pyelasticsearch import ElasticSearch from webservices.env import env es_conn = env.get_service(label='elasticsearch-swarm-1.7.1') if es_conn: es = ElasticSearch(es_conn.get_url(url='uri')) else: es = ElasticSearch('http://localhost:9200') class Search(utils.Resource): @use_kwargs(args.query) def get(self, **kwargs): query = kwargs['q'] hits = es.search('_all: %s' % query, index='docs', size=10)['hits']['hits'] return {'results': hits}
tuple(db.Model.metadata.tables.keys()) + ( 'ofec_pacronyms', 'ofec_nicknames', 'ofec_zips_districts', ) ) def load_table(name): try: return sa.Table(name, db.metadata, autoload_with=db.engine) except sa.exc.NoSuchTableError: return None def check_keys(keys, getter, formatter): missing = [key for key in keys if getter(key) is None] if missing: print(formatter.format(', '.join(keys))) return missing CHECKS = [ (REQUIRED_CREDS, env.get_credential, 'Missing creds {}'), (REQUIRED_SERVICES, lambda service: env.get_service(label=service), 'Missing services {}'), (REQUIRED_TABLES, load_table, 'Missing tables {}'), ] def check_config(): results = [check_keys(*check) for check in CHECKS] if any(results): raise RuntimeError('Invalid configuration: {0}'.format(results))
def redis_url(): redis = env.get_service(label="redis28-swarm") if redis: url = redis.get_url(host="hostname", password="******", port="port") return "redis://{}".format(url) return os.getenv("FEC_REDIS_URL", "redis://")