Beispiel #1
0
    def _connect_to_db(self, retries=0):
        """
        Connect to the database, but do not initialize a connection.

        Depending on which public method is used, it initiates either a standard
            mongo connection or a gridfs connection
        """
        retries = retries

        try:
            # Establish a Connection
            connection = pymongo.Connection(self.host, self.port,
                                            **self.options)

            # Establish a database
            database = connection[self.db]

            # If user passed username and password args, give that to authenticate
            if self.username and self.password:
                database.authenticate(self.username, self.password)

        # Handle the following exceptions, if retries is less than what is
        # passed into this method, attempt to connect again.  Otherwise,
        # raise the proper exception.
        except AutoReconnect, error_message:
            time.sleep(2)
            retries += 1

            if retries <= self.max_retries:
                self._connect_to_db(retries=retries)
            else:
                raise ConnectionFailure('Max number of retries (%s) reached. Error: %s'\
                                         % (self.max_retries, error_message))
Beispiel #2
0
def _configured_socket(address, options):
    """Given (host, port) and PoolOptions, return a configured socket.

    Can raise socket.error, ConnectionFailure, or CertificateError.

    Sets socket's SSL and timeout options.
    """
    sock = _create_connection(address, options)
    ssl_context = options.ssl_context

    if ssl_context is not None:
        try:
            sock = ssl_context.wrap_socket(sock)
        except IOError as exc:
            sock.close()
            raise ConnectionFailure("SSL handshake failed: %s" % (str(exc), ))
        if ssl_context.verify_mode and options.ssl_match_hostname:
            try:
                match_hostname(sock.getpeercert(), hostname=address[0])
            except CertificateError:
                sock.close()
                raise

    sock.settimeout(options.socket_timeout)
    return sock
    def __receive_data_on_socket(self, length, sock):
        """Lowest level receive operation.

        Takes length to receive and repeatedly calls recv until able to
        return a buffer of that length, raising ConnectionFailure on error.
        """
        message = ""
        while len(message) < length:
            try:
                chunk = sock.recv(length - len(message))
            except:
                # Store the exception from sock.recv() in case we throw a new
                # exception in this exception-handler.
                exc_type, exc_value, exc_traceback = sys.exc_info()
                # PYTHON-294: must close socket here, because if it remains open
                # after this then the next time we read from it we may get stale
                # data from a previous operation.
                try:
                    self.__pool.discard_socket()
                except:
                    pass
                raise exc_value

            if chunk == "":
                raise ConnectionFailure("connection closed")
            message += chunk
        return message
 def test__mongoAdminCommand_connect_failed(self, mongo_client_mock):
     mongo_client_mock.return_value.admin.command.side_effect = (
         ConnectionFailure("connection attempt failed"),
         self._getFixture("initiate-ok"))
     result = self.service._executeAdminCommand(self.cluster_object,
                                                "replSetGetStatus")
     self.assertEqual(self.initiate_ok_response, result)
    def connect(self, host, port):
        """Connect to Mongo and return a new (connected) socket.
        """
        try:
            # Prefer IPv4. If there is demand for an option
            # to specify one or the other we can add it later.
            s = socket.socket(socket.AF_INET)
            s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
            s.settimeout(self.conn_timeout or 20.0)
            s.connect((host, port))
        except socket.gaierror:
            # If that fails try IPv6
            s = socket.socket(socket.AF_INET6)
            s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
            s.settimeout(self.conn_timeout or 20.0)
            s.connect((host, port))

        if self.use_ssl:
            try:
                s = ssl.wrap_socket(s)
            except ssl.SSLError:
                s.close()
                raise ConnectionFailure("SSL handshake failed. MongoDB may "
                                        "not be configured with SSL support.")

        s.settimeout(self.net_timeout)

        return s
Beispiel #6
0
    def open_connection(self):
        try:
            password = os.getenv('MONGODB_PASSWORD')
            if not password:
                raise ConnectionFailure(
                    "No MongoDB password found in .env file under the key 'MONGODB_PASSWORD'."
                )

            self.client = pymongo.MongoClient(
                f'mongodb+srv://{self.USER_NAME}:{password}@zbot-5waud.gcp.mongodb.net/test?retryWrites=true'
            )
            self.client.admin.command(
                'ismaster'
            )  # Check if connected and raises ConnectionFailure if not
            logger.info(
                f"Connected to MongoDB database '{self.DATABASE_NAME}'.")
            self.connected = True

            self.database = self.client[self.DATABASE_NAME]
            for collection_name in self.COLLECTION_NAMES:
                self.collections[collection_name] = self.database[
                    collection_name]
            logger.info(f"Loaded {len(self.collections)} collection(s).")

        except ConnectionFailure:
            logger.error(
                f"Could not connect to MongoDB database '{self.DATABASE_NAME}'.",
                exc_info=True)

        return self.connected
Beispiel #7
0
    def __init__(
        self,
        url: str = DEFAULT_URL,
        db: str = DEFAULT_DB,
        check_connection: bool = True,
    ):
        """Constructor.

        Args:
            url: MongoDB server URL
            db: MongoDB 'database' (namespace) to use
            check_connection: If True, check immediately if we can connect to the
                server at the provided url. Otherwise defer this check until the
                first operation (at which point a stack trace may occur).

        Raises:
            pymongo.errors.ConnectionFailure: if check_connection is True,
                 and the connection fails
        """
        self._mongoclient_connect_status = {"initial": "untried", "retry": "untried"}
        self._client = self._mongoclient(url, check_connection, **self.timeout_args)
        if self._client is None:
            msg = self.connect_status_str
            _log.error(msg)
            raise ConnectionFailure(msg)
        self._db = getattr(self._client, db)
        self._db = getattr(self._client, db)
        self._database_name = db
        self._server_url = url
Beispiel #8
0
def _configured_socket(address, options):
    """Given (host, port) and PoolOptions, return a configured socket.

    Can raise socket.error, ConnectionFailure, or CertificateError.

    Sets socket's SSL and timeout options.
    """
    sock = _create_connection(address, options)
    ssl_context = options.ssl_context

    if ssl_context is not None:
        host = address[0]
        try:
            # According to RFC6066, section 3, IPv4 and IPv6 literals are
            # not permitted for SNI hostname.
            if _HAVE_SNI and not is_ip_address(host):
                sock = ssl_context.wrap_socket(sock, server_hostname=host)
            else:
                sock = ssl_context.wrap_socket(sock)
        except IOError as exc:
            sock.close()
            raise ConnectionFailure("SSL handshake failed: %s" % (str(exc),))
        if ssl_context.verify_mode and options.ssl_match_hostname:
            try:
                match_hostname(sock.getpeercert(), hostname=host)
            except CertificateError:
                sock.close()
                raise

    sock.settimeout(options.socket_timeout)
    return sock
Beispiel #9
0
    async def read_loop(self) -> None:
        while True:
            try:
                await self._read_loop_step()
            except asyncio.CancelledError:
                self._shut_down()
                return
            except Exception as e:
                self.__connected.clear()
                connection_error = ConnectionFailure(
                    'Connection was lost due to: {}'.format(str(e)))
                self.close(error=connection_error)
                for ft in self.__request_futures.values():
                    if not ft.done():
                        ft.set_exception(connection_error)
                self.__request_futures = {}

                if self.reconnect_task is None:
                    self.reconnect_task = asyncio.ensure_future(
                        self.reconnect())
                    self.reconnect_task.add_done_callback(
                        self._on_reconnect_task_done)
                else:
                    logger.warning('Reconnect already in progress')

                return
Beispiel #10
0
    def _shut_down(self) -> None:
        connection_error = ConnectionFailure('Shutting down.')
        for ft in self.__request_futures.values():
            if not ft.done():
                ft.set_exception(connection_error)

        self.__disconnected.set()
Beispiel #11
0
    def connect(self):
        """Connect to Mongo and return a new (connected) socket. Note that the
           pool does not keep a reference to the socket -- you must call
           return_socket() when you're done with it.
        """
        sock = self.create_connection()
        hostname = self.pair[0]

        if self.use_ssl:
            try:
                sock = ssl.wrap_socket(sock,
                                       certfile=self.ssl_certfile,
                                       keyfile=self.ssl_keyfile,
                                       ca_certs=self.ssl_ca_certs,
                                       cert_reqs=self.ssl_cert_reqs)
                if self.ssl_cert_reqs and self.ssl_match_hostname:
                    match_hostname(sock.getpeercert(), hostname)

            except ssl.SSLError:
                sock.close()
                raise ConnectionFailure("SSL handshake failed. MongoDB may "
                                        "not be configured with SSL support.")

        sock.settimeout(self.net_timeout)
        return SocketInfo(sock, self.pool_id, hostname)
Beispiel #12
0
    def __init__(self):
        self.client = None
        self.connected = False
        self.database = None
        self.collections = {}
        self.database_host = os.getenv('MONGODB_DATABASE_HOST')
        self.database_name = os.getenv('MONGODB_DATABASE_NAME')

        if not self.database_host:
            raise ConnectionFailure(
                "No MongoDB host found in .env file under the key 'MONGODB_HOST'."
            )
        if not self.database_name:
            raise ConnectionFailure(
                "No MongoDB database name found in .env file under the key 'MONGODB_DATABASE_NAME'."
            )
Beispiel #13
0
def unset_config(email, key):
    try:
        result = barry_db.users.update_one({'email': email},
                                           {'$unset': {
                                               'config.' + key: ''
                                           }})
    except:
        raise ConnectionFailure("Failed to connect to the database")
    def test__mongoAdminCommand_NoPrimary(self, mongo_client_mock):
        mongo_client_mock.return_value.admin.command.side_effect = (
            ConnectionFailure(
                "No replica set members match selector \"Primary()\""),
            self._getFixture("initiate-ok"), self._getFixture("initiate-ok"))

        self.service._executeAdminCommand(self.cluster_object,
                                          "replSetGetStatus")
Beispiel #15
0
    def test_standalone_error(self):
        responses = (('a', 27017), ConnectionFailure("SPECIAL MESSAGE"))
        uri = "mongodb://a:27017"
        expected_results = [
            'ServerHeartbeatStartedEvent', 'ServerHeartbeatFailedEvent'
        ]

        self.create_mock_monitor(responses, uri, expected_results)
Beispiel #16
0
 def setUp(self):
     try:
         self.db = connect_to_db('localhost', 37017)
     except AutoReconnect as e:
         raise ConnectionFailure('Could not connect to MongoDB client. Make'
                                 ' sure a tunnel is set up (or some other '
                                 'method is used) before running the '
                                 'tests.')
 def __init__(self, client, port, database, collection):
     self.conn = MongoClient(client, port)
     self.database = self.conn[database]
     self.collection = self.database[collection]
     try:
         self.conn.admin.command('ismaster')
     except ConnectionFailure:
         raise ConnectionFailure("error")
    def __recv_data(self, length, sock):
        """Lowest level receive operation.

        Takes length to receive and repeatedly calls recv until able to
        return a buffer of that length, raising ConnectionFailure on error.
        """
        chunks = []
        while length:
            try:
                chunk = sock.recv(length)
            except:
                raise ConnectionFailure("connection error")
            if chunk == "":
                raise ConnectionFailure("connection closed")
            length -= len(chunk)
            chunks.append(chunk)
        return "".join(chunks)
Beispiel #19
0
    def test_heartbeat_raises_exception_when_connection_alive_is_false(self, *calls):
        # pylint: disable=W0613
        with patch('mongodb_proxy.MongoProxy') as mock_proxy:
            mock_proxy.return_value.admin.command.side_effect = ConnectionFailure('Test')
            useless_conn = MongoConnection('useless', 'useless', 'useless')

            with pytest.raises(HeartbeatFailure):
                useless_conn.heartbeat()
Beispiel #20
0
def get_config(email, key):
    try:
        user = barry_db.users.find_one({'email': email})
    except:
        raise ConnectionFailure("Failed to connect to the database")

    if 'config' in user and key in user['config']:
        return user['config'][key]
    raise Exception("No such key in user configs")
Beispiel #21
0
 def get_database(self, database=None):
     """ Retrieves a database from an existing connection. """
     if not self.connection:
         raise ConnectionFailure('No connection')
     if not database:
         if not self._database:
             raise Exception('No database submitted')
         database = self._database
     return self.connection[database]
Beispiel #22
0
 def _raise_wait_queue_timeout(self):
     listeners = self.opts.event_listeners
     if self.enabled_for_cmap:
         listeners.publish_connection_check_out_failed(
             self.address, ConnectionCheckOutFailedReason.TIMEOUT)
     raise ConnectionFailure(
         'Timed out while checking out a connection from connection pool '
         'with max_size %r and wait_queue_timeout %r' %
         (self.opts.max_pool_size, self.opts.wait_queue_timeout))
Beispiel #23
0
def list_config(email):
    try:
        user = barry_db.users.find_one({'email': email})
    except:
        raise ConnectionFailure("Failed to connect to the database")

    if 'config' in user:
        return user['config']
    return {}
Beispiel #24
0
def get_db():
    try:
        # Check the connection was successful
        client.admin.command('ismaster')
        return client[DB_NAME]
    except ConnectionFailure as e:
        print(f"Server not available: {e}")
        raise ConnectionFailure(
            f"Was unable to connect to the database: {MONGO_DB}")
Beispiel #25
0
 def open_connection(self):
     """
     Establish MongoDB client connection.
     """
     self.client = pymongo.MongoClient(self._mongo_uri)
     if self.client is None:
         raise ConnectionFailure('No client connection: {}').format(
             self._mongo_uri)
     self.db = self.client[self._mongo_db]
     self.collection = self.db[self._mongo_collection]
Beispiel #26
0
    def create_connection(self):
        """Connect and return a socket object.

        This is a modified version of create_connection from
        CPython >=2.6.
        """
        host, port = self.pair

        # Check if dealing with a unix domain socket
        if host.endswith('.sock'):
            if not hasattr(socket, "AF_UNIX"):
                raise ConnectionFailure("UNIX-sockets are not supported "
                                        "on this system")
            sock = socket.socket(socket.AF_UNIX)
            try:
                sock.connect(host)
                return sock
            except socket.error as e:
                if sock is not None:
                    sock.close()
                raise e

        # Don't try IPv6 if we don't support it. Also skip it if host
        # is 'localhost' (::1 is fine). Avoids slow connect issues
        # like PYTHON-356.
        family = socket.AF_INET
        if socket.has_ipv6 and host != 'localhost':
            family = socket.AF_UNSPEC

        err = None
        for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
            af, socktype, proto, dummy, sa = res
            sock = None
            try:
                sock = socket.socket(af, socktype, proto)
                sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
                sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE,
                                self.socket_keepalive)
                sock.settimeout(self.conn_timeout)
                sock.connect(sa)
                return sock
            except socket.error as e:
                err = e
                if sock is not None:
                    sock.close()

        if err is not None:
            raise err
        else:
            # This likely means we tried to connect to an IPv6 only
            # host with an OS/kernel or Python interpreter that doesn't
            # support IPv6. The test case is Jython2.5.1 which doesn't
            # support IPv6 at all.
            raise socket.error('getaddrinfo failed')
Beispiel #27
0
 def g(arg1, counter=[0]):
     if counter[0] == 0:
         counter[0] += 1
         print "NetworkTimeout"
         raise NetworkTimeout("FOO")
     elif counter[0] == 1:
         counter[0] += 1
         print "Connection Failure"
         raise ConnectionFailure("BAR")
     else:
         return True
Beispiel #28
0
    def __init__(self):
        handlers = urls

        settings = dict(autoescape=None, )
        tornado.web.Application.__init__(self, handlers, **settings)

        try:
            self.con = MongoClient(config_test.DB_CONN_STR)
            self.database = self.con[config_test.DB_NAME]
        except Exception as e:
            raise ConnectionFailure("Failed to connect to db", e)
Beispiel #29
0
 def __next__(self):
     if len(self.dataBuffer) < 16:
         return None
     message_length, = struct.unpack("<i", self.dataBuffer[:4])
     if len(self.dataBuffer) < message_length:
         return None
     if message_length < 16:
         raise ConnectionFailure()
     message_data = self.dataBuffer[:message_length]
     self.dataBuffer = self.dataBuffer[message_length:]
     return self.decode(message_data)
Beispiel #30
0
    def _connect(self):
        try:
            self.client = pymongo.MongoClient(self.host,self.port, serverSelectionTimeoutMS=database_connect_time_out,connectTimeoutMS=database_connect_time_out)
            self.client.server_info()
            msg = 'host: {}  port:  {}  database_name : {}   MongoDB数据库连接成功'.format(host, port, self.database)
            logger.info(msg)

            self.db = self.client[self.database]
        except ServerSelectionTimeoutError as e:
            msg = 'host: {}  port:  {}  database_name : {}   MongoDB数据库连接失败 原因: 可能配置文件出错或者连接超时  超时时间为:  {} 毫秒'.format(host, port, self.database, database_connect_time_out)
            raise ConnectionFailure(msg)