示例#1
0
文件: cache.py 项目: ooda/cloudly
def get_redis_connection(hostname=None, port=None):
    """ Get a connection to a Redis server. The priority is:
        - look for an environment variable REDISTOGO_URL (Heroku), else
        - look for an environment variable REDIS_HOST, else
        - look for an EC2 hosted server offering the service 'redis', else
        - use localhost, 127.0.0.1.
    """

    host = (
        hostname or
        os.environ.get("REDIS_HOST") or
        ec2.get_hostname("redis") or
        "127.0.0.1"
    )
    port = port or os.environ.get("REDIS_PORT", 6379)
    url = os.environ.get('REDISTOGO_URL',  # Set when on Heroku.
                         'redis://{}:{}'.format(host, port))

    log.info("Connecting to Redis server at {}".format(url))
    server = redis.from_url(url)

    # Add some utility function. These functions first
    # serialize to/from JSON the given object, then call redis get/set.
    def redis_jget(key):
        value = server.get(key)
        return json.loads(value) if value else None

    server.jset = lambda key, obj: server.set(key, json.dumps(obj))
    server.jget = redis_jget
    return server
示例#2
0
文件: metric.py 项目: ooda/cloudly
def get_cube_host(hostname=None):
    """Get the hostname for the Cube server. These are tried, in order:
        - an environment variable CUBE_HOST;
        - an EC2 hosted server offering the service 'cube';
        - 127.0.0.1.
    """
    return (
        hostname or
        os.environ.get("CUBE_HOST") or
        ec2.get_hostname("cube") or
        "127.0.0.1"
    )
示例#3
0
文件: ccouchdb.py 项目: ooda/cloudly
def get_server(hostname=None, port=None, username=None, password=None,
               protocol=None):
    """Return a server instance.

    The following heuristic is used to find the server:
        - function arguments take precedence over all else,
        - environment variable COUCHDB_HOST is used otherwise,
        - use the service finder of cloudly.aws.ec2 to look up a couchdb
          server if none was found so far,
        - else use 127.0.0.1

    """
    host = (
        hostname or
        os.environ.get("COUCHDB_HOST") or
        ec2.get_hostname("couchdb") or
        "127.0.0.1"
    )
    protocol = protocol or os.environ.get("COUCHDB_PROTOCOL", "http")
    port = port or os.environ.get("COUCHDB_PORT",
                                  443 if protocol == "https" else 5984)
    username = username or os.environ.get("COUCHDB_USERNAME", None)
    password = password or os.environ.get("COUCHDB_PASSWORD", None)

    if username is not None and password is not None:
        url = "{protocol}://{username}:{password}@{host}:{port}".format(
            protocol=protocol,
            host=host,
            port=port,
            username=username,
            password=password,
        )
        log.info("{} port {}, authenticated".format(host, port))
    else:
        url = "{protocol}://{host}:{port}".format(
            protocol=protocol,
            host=host,
            port=port,
        )
        log.info("{} port {}".format(host, port))

    return couchdb.Server(url)