Exemplo n.º 1
0
def build_cluster_from_uri(uri):
    uri_wrapper = parse_mongo_uri(uri)
    database = uri_wrapper.database or "admin"
    username = uri_wrapper.username
    password = uri_wrapper.password

    nodes = uri_wrapper.node_list
    cluster_doc = {
        "_id": mask_mongo_uri(uri)
    }
    member_doc_list = []

    for node in nodes:
        host = node[0]
        port = node[1]
        member_doc = {
            "host": "%s:%s" % (host, port)
        }
        member_doc_list.append(member_doc)

    cluster_doc["members"] = member_doc_list

    cluster = new_cluster(cluster_doc)

    # set login user if specified
    if username:
        for member in cluster.get_members():
            member.get_server().set_login_user(database, username, password)

    return cluster
Exemplo n.º 2
0
def build_cluster_from_uri(uri):
    uri_wrapper = parse_mongo_uri(uri)
    database = uri_wrapper.database or "admin"
    username = uri_wrapper.username
    password = uri_wrapper.password

    nodes = uri_wrapper.node_list
    cluster_doc = {"_id": mask_mongo_uri(uri)}
    member_doc_list = []

    for node in nodes:
        host = node[0]
        port = node[1]
        member_doc = {"host": "%s:%s" % (host, port)}
        member_doc_list.append(member_doc)

    cluster_doc["members"] = member_doc_list

    cluster = new_cluster(cluster_doc)

    # set login user if specified
    if username:
        for member in cluster.get_members():
            member.get_server().set_login_user(database, username, password)

    return cluster
Exemplo n.º 3
0
def mongo_connect(uri, conn_timeout=None, **kwargs):
    conn_timeout_mills = (conn_timeout or CONN_TIMEOUT) * 1000

    kwargs = kwargs or {}
    kwargs["connectTimeoutMS"] = conn_timeout_mills
    kwargs["socketTimeoutMS"] = SOCKET_TIMEOUT * 1000
    # default connection timeout and convert to mills

    uri_wrapper = parse_mongo_uri(uri)

    try:
        dbname = uri_wrapper.database
        if not dbname:
            if uri.endswith("/"):
                uri += "admin"
            else:
                uri += "/admin"

        # add serverSelectionTimeoutMS for pymongo 3.2
        if pymongo.get_version_string().startswith("3.2"):
            kwargs["serverSelectionTimeoutMS"] = conn_timeout_mills

        kwargs["maxPoolSize"] = 1

        mongo_client = _mongo_client(uri, **kwargs)

        return mongo_client

    except Exception, e:
        if is_connection_exception(e):
            raise ConnectionError(uri_wrapper.masked_uri, cause=e)
        elif "authentication failed" in safe_stringify(e):
            raise AuthenticationFailedError(uri_wrapper.masked_uri, cause=e)
        else:
            raise
def mongo_connect(uri, conn_timeout=None, **kwargs):
    conn_timeout_mills = (conn_timeout or CONN_TIMEOUT) * 1000

    kwargs = kwargs or {}
    kwargs["connectTimeoutMS"] = conn_timeout_mills
    kwargs["socketTimeoutMS"] = SOCKET_TIMEOUT * 1000
    # default connection timeout and convert to mills

    uri_wrapper = parse_mongo_uri(uri)

    try:
        dbname = uri_wrapper.database
        if not dbname:
            if uri.endswith("/"):
                uri += "admin"
            else:
                uri += "/admin"

        # add serverSelectionTimeoutMS for pymongo 3.2
        if pymongo.get_version_string().startswith("3.2"):
            kwargs["serverSelectionTimeoutMS"] = conn_timeout_mills

        kwargs["maxPoolSize"] = 1

        mongo_client = _mongo_client(uri, **kwargs)

        return mongo_client

    except Exception, e:
        if is_connection_exception(e):
            raise ConnectionError(uri_wrapper.masked_uri, cause=e)
        elif "authentication failed" in safe_stringify(e):
            raise AuthenticationFailedError(uri_wrapper.masked_uri, cause=e)
        else:
            raise
 def __init__(self,
              uri,
              connector_id=None,
              display_name=None,
              conn_timeout=None):
     self._uri_wrapper = parse_mongo_uri(uri)
     self._connector_id = connector_id
     self._conn_timeout = conn_timeout or CONN_TIMEOUT
     self._connection_id = None
     self._display_name = display_name
Exemplo n.º 6
0
 def generate_tag_value(self, backup):
     try:
         uri_wrapper = parse_mongo_uri(backup.source.uri)
         host = uri_wrapper.host
         ips = get_host_ips(host)
         if ips:
             return ips[0][0]
     except Exception, e:
         logger.error("SourceIPTag: Error while generating tag value: %s" %
                      e)
Exemplo n.º 7
0
 def generate_tag_value(self, plan):
     try:
         uri_wrapper = parse_mongo_uri(plan.source.uri)
         host = uri_wrapper.host
         ips = get_host_ips(host)
         if ips:
             return ips[0][0]
     except Exception, e:
         logger.error("SourceIPTag: Error while generating tag value: %s" %
                      e)
Exemplo n.º 8
0
def build_server_from_uri(uri):
    uri_wrapper = parse_mongo_uri(uri)
    node = uri_wrapper.node_list[0]
    host = node[0]
    port = node[1]

    database = uri_wrapper.database or "admin"
    username = uri_wrapper.username
    password = uri_wrapper.password

    address = "%s:%s" % (host, port)
    server = build_server_from_address(address)

    # set login user if specified
    if username:
        server.set_login_user(database, username, password)

    return server
Exemplo n.º 9
0
def build_server_from_uri(uri):
    uri_wrapper = parse_mongo_uri(uri)
    node = uri_wrapper.node_list[0]
    host = node[0]
    port = node[1]

    database = uri_wrapper.database or "admin"
    username = uri_wrapper.username
    password = uri_wrapper.password

    address = "%s:%s" % (host, port)
    server = build_server_from_address(address)

    # set login user if specified
    if username:
        server.set_login_user(database, username, password)

    return server
Exemplo n.º 10
0
def mongo_connect(uri):
    uri_wrapper = parse_mongo_uri(uri)

    try:
        dbname = uri_wrapper.database
        if not dbname:
            dbname = "admin"
            if uri.endswith("/"):
                uri += "admin"
            else:
                uri += "/admin"

        conn = pymongo.Connection(uri, socketTimeoutMS=CONN_TIMEOUT,
                                       connectTimeoutMS=CONN_TIMEOUT)
        return conn[dbname]
    except Exception, e:
        if is_connection_exception(e):
            raise ConnectionError(uri_wrapper.masked_uri, cause=e)
        elif "authentication failed" in str(e):
            raise AuthenticationFailedError(uri_wrapper.masked_uri, cause=e)
        else:
            raise
Exemplo n.º 11
0
def mongo_connect(uri):
    uri_wrapper = parse_mongo_uri(uri)

    try:
        dbname = uri_wrapper.database
        if not dbname:
            dbname = "admin"
            if uri.endswith("/"):
                uri += "admin"
            else:
                uri += "/admin"

        conn = pymongo.Connection(uri,
                                  socketTimeoutMS=CONN_TIMEOUT,
                                  connectTimeoutMS=CONN_TIMEOUT)
        return conn[dbname]
    except Exception, e:
        if is_connection_exception(e):
            raise ConnectionError(uri_wrapper.masked_uri, cause=e)
        elif "authentication failed" in str(e):
            raise AuthenticationFailedError(uri_wrapper.masked_uri, cause=e)
        else:
            raise
Exemplo n.º 12
0
 def __init__(self, uri):
     self._uri_wrapper = parse_mongo_uri(uri)
Exemplo n.º 13
0
def _db_repo_connect():
    db_conf = config.get_database_repository_conf()
    uri = db_conf["databaseURI"]
    conn = pymongo.Connection(uri)
    dbname = parse_mongo_uri(uri).database
    return conn, dbname
Exemplo n.º 14
0
def _db_repo_connect():
    db_conf = config.get_database_repository_conf()
    uri = db_conf["databaseURI"]
    conn = pymongo.Connection(uri)
    dbname = parse_mongo_uri(uri).database
    return conn, dbname
Exemplo n.º 15
0
 def __init__(self, uri):
     self._uri_wrapper = parse_mongo_uri(uri)
Exemplo n.º 16
0
 def __init__(self, uri, connector_id=None,display_name=None, conn_timeout=None):
     self._uri_wrapper = parse_mongo_uri(uri)
     self._connector_id = connector_id
     self._conn_timeout = conn_timeout or CONN_TIMEOUT
     self._connection_id = None
     self._display_name = display_name