Python PyrakoonStore Example

说明

python pyrakoonstore示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Python

命名空间/包名称: ovsextensionsstoragepersistentpyrakoonstore

示例#1
文件: watcher.py项目: dawnpower/framework

def services_running(target):
    """
    Check all services are running
    :param target: Target to check
    :return: Boolean
    """
    try:
        key = 'ovs-watcher-{0}'.format(str(uuid.uuid4()))
        value = str(time.time())

        if target == 'framework':
            # Volatile
            _log(target, 'Testing volatile store...', 0)
            max_tries = 5
            tries = 0
            while tries < max_tries:
                try:
                    try:
                        logging.disable(logging.WARNING)
                        from ovs.extensions.storage.volatilefactory import VolatileFactory
                        VolatileFactory.store = None
                        volatile = VolatileFactory.get_client()
                        volatile.set(key, value)
                        if volatile.get(key) == value:
                            volatile.delete(key)
                            break
                        volatile.delete(key)
                    finally:
                        logging.disable(logging.NOTSET)
                except Exception as message:
                    _log(target, '  Error during volatile store test: {0}'.format(message), 2)
                key = 'ovs-watcher-{0}'.format(str(uuid.uuid4()))  # Get another key
                time.sleep(1)
                tries += 1
            if tries == max_tries:
                _log(target, '  Volatile store not working correctly', 2)
                return False
            _log(target, '  Volatile store OK after {0} tries'.format(tries), 0)

            # Persistent
            _log(target, 'Testing persistent store...', 0)
            max_tries = 5
            tries = 0
            while tries < max_tries:
                try:
                    try:
                        logging.disable(logging.WARNING)
                        persistent = PersistentFactory.get_client()
                        persistent.set(key, value)
                        if persistent.get(key) == value:
                            persistent.delete(key)
                            break
                        persistent.delete(key)
                    finally:
                        logging.disable(logging.NOTSET)
                except Exception as message:
                    _log(target, '  Error during persistent store test: {0}'.format(message), 2)
                key = 'ovs-watcher-{0}'.format(str(uuid.uuid4()))  # Get another key
                time.sleep(1)
                tries += 1
            if tries == max_tries:
                _log(target, '  Persistent store not working correctly', 2)
                return False
            _log(target, '  Persistent store OK after {0} tries'.format(tries), 0)

        if target == 'volumedriver':
            # Arakoon, voldrv cluster
            _log(target, 'Testing arakoon (voldrv)...', 0)
            max_tries = 5
            tries = 0
            while tries < max_tries:
                try:
                    from ovs.extensions.storage.persistent.pyrakoonstore import PyrakoonStore
                    client = PyrakoonStore('voldrv')
                    client.set(key, value)
                    if client.get(key) == value:
                        client.delete(key)
                        break
                    client.delete(key)
                except Exception as message:
                    _log(target, '  Error during arakoon (voldrv) test: {0}'.format(message), 2)
                key = 'ovs-watcher-{0}'.format(str(uuid.uuid4()))  # Get another key
                time.sleep(1)
                tries += 1
            if tries == max_tries:
                _log(target, '  Arakoon (voldrv) not working correctly', 2)
                return False
            _log(target, '  Arakoon (voldrv) OK', 0)

        if target in ['framework', 'volumedriver']:
            # RabbitMQ
            _log(target, 'Test rabbitMQ...', 0)
            import pika
            from ovs.extensions.db.etcd.configuration import EtcdConfiguration
            rmq_servers = EtcdConfiguration.get('/ovs/framework/messagequeue|endpoints')
            good_node = False
            for server in rmq_servers:
                try:
                    connection_string = '{0}://{1}:{2}@{3}/%2F'.format(EtcdConfiguration.get('/ovs/framework/messagequeue|protocol'),
                                                                       EtcdConfiguration.get('/ovs/framework/messagequeue|user'),
                                                                       EtcdConfiguration.get('/ovs/framework/messagequeue|password'),
                                                                       server)
                    connection = pika.BlockingConnection(pika.URLParameters(connection_string))
                    channel = connection.channel()
                    channel.basic_publish('', 'ovs-watcher', str(time.time()),
                                          pika.BasicProperties(content_type='text/plain', delivery_mode=1))
                    connection.close()
                    good_node = True
                except Exception as message:
                    _log(target, '  Error during rabbitMQ test on node {0}: {1}'.format(server, message), 2)
            if good_node is False:
                _log(target, '  No working rabbitMQ node could be found', 2)
                return False
            _log(target, '  RabbitMQ test OK', 0)
            _log(target, 'All tests OK', 0)
            return True
    except Exception as ex:
        _log(target, 'Unexpected exception: {0}'.format(ex), 2)
        return False

示例#2
文件: watcher.py项目: grimpy/openvstorage

    def services_running(self, target):
        """
        Check all services are running
        :param target: Target to check
        :return: Boolean
        """
        try:
            key = 'ovs-watcher-{0}'.format(str(uuid.uuid4()))
            value = str(time.time())

            if target == 'config':
                self.log_message(target, 'Testing configuration store...', 0)
                from ovs.extensions.generic.configuration import Configuration
                try:
                    Configuration.list('/')
                except Exception as ex:
                    self.log_message(target, '  Error during configuration store test: {0}'.format(ex), 2)
                    return False
                if Configuration.get_store() == 'arakoon':
                    from ovs.extensions.db.arakoon.configuration import ArakoonConfiguration
                    from ovs.extensions.db.arakoon.ArakoonInstaller import ArakoonInstaller, ArakoonClusterConfig
                    from ovs.extensions.db.arakoon.pyrakoon.pyrakoon.compat import NoGuarantee
                    with open(ArakoonConfiguration.CACC_LOCATION) as config_file:
                        contents = config_file.read()
                    config = ArakoonClusterConfig(cluster_id='cacc', filesystem=True)
                    config.read_config(contents)
                    client = ArakoonInstaller.build_client(config)
                    contents = client.get(ArakoonInstaller.INTERNAL_CONFIG_KEY, consistency=NoGuarantee())
                    if Watcher.LOG_CONTENTS != contents:
                        try:
                            config.read_config(contents)  # Validate whether the contents are not corrupt
                        except Exception as ex:
                            self.log_message(target, '  Configuration stored in configuration store seems to be corrupt: {0}'.format(ex), 2)
                            return False
                        temp_filename = '{0}~'.format(ArakoonConfiguration.CACC_LOCATION)
                        with open(temp_filename, 'w') as config_file:
                            config_file.write(contents)
                            config_file.flush()
                            os.fsync(config_file)
                        os.rename(temp_filename, ArakoonConfiguration.CACC_LOCATION)
                        Watcher.LOG_CONTENTS = contents
                self.log_message(target, '  Configuration store OK', 0)
                return True

            if target == 'framework':
                # Volatile
                self.log_message(target, 'Testing volatile store...', 0)
                max_tries = 5
                tries = 0
                while tries < max_tries:
                    try:
                        try:
                            logging.disable(logging.WARNING)
                            from ovs.extensions.storage.volatilefactory import VolatileFactory
                            VolatileFactory.store = None
                            volatile = VolatileFactory.get_client()
                            volatile.set(key, value)
                            if volatile.get(key) == value:
                                volatile.delete(key)
                                break
                            volatile.delete(key)
                        finally:
                            logging.disable(logging.NOTSET)
                    except Exception as message:
                        self.log_message(target, '  Error during volatile store test: {0}'.format(message), 2)
                    key = 'ovs-watcher-{0}'.format(str(uuid.uuid4()))  # Get another key
                    time.sleep(1)
                    tries += 1
                if tries == max_tries:
                    self.log_message(target, '  Volatile store not working correctly', 2)
                    return False
                self.log_message(target, '  Volatile store OK after {0} tries'.format(tries), 0)

                # Persistent
                self.log_message(target, 'Testing persistent store...', 0)
                max_tries = 5
                tries = 0
                while tries < max_tries:
                    try:
                        try:
                            logging.disable(logging.WARNING)
                            persistent = PersistentFactory.get_client()
                            persistent.set(key, value)
                            if persistent.get(key) == value:
                                persistent.delete(key)
                                break
                            persistent.delete(key)
                        finally:
                            logging.disable(logging.NOTSET)
                    except Exception as message:
                        self.log_message(target, '  Error during persistent store test: {0}'.format(message), 2)
                    key = 'ovs-watcher-{0}'.format(str(uuid.uuid4()))  # Get another key
                    time.sleep(1)
                    tries += 1
                if tries == max_tries:
                    self.log_message(target, '  Persistent store not working correctly', 2)
                    return False
                self.log_message(target, '  Persistent store OK after {0} tries'.format(tries), 0)

            if target == 'volumedriver':
                # Arakoon, voldrv cluster
                self.log_message(target, 'Testing arakoon (voldrv)...', 0)
                max_tries = 5
                tries = 0
                while tries < max_tries:
                    try:
                        from ovs.extensions.generic.configuration import Configuration
                        from ovs.extensions.storage.persistent.pyrakoonstore import PyrakoonStore
                        cluster_name = str(Configuration.get('/ovs/framework/arakoon_clusters|voldrv'))
                        client = PyrakoonStore(cluster=cluster_name)
                        client.set(key, value)
                        if client.get(key) == value:
                            client.delete(key)
                            break
                        client.delete(key)
                    except Exception as message:
                        self.log_message(target, '  Error during arakoon (voldrv) test: {0}'.format(message), 2)
                    key = 'ovs-watcher-{0}'.format(str(uuid.uuid4()))  # Get another key
                    time.sleep(1)
                    tries += 1
                if tries == max_tries:
                    self.log_message(target, '  Arakoon (voldrv) not working correctly', 2)
                    return False
                self.log_message(target, '  Arakoon (voldrv) OK', 0)

            if target in ['framework', 'volumedriver']:
                # RabbitMQ
                self.log_message(target, 'Test rabbitMQ...', 0)
                import pika
                from ovs.extensions.generic.configuration import Configuration
                messagequeue = Configuration.get('/ovs/framework/messagequeue')
                rmq_servers = messagequeue['endpoints']
                good_node = False
                for server in rmq_servers:
                    try:
                        connection_string = '{0}://{1}:{2}@{3}/%2F'.format(messagequeue['protocol'],
                                                                           messagequeue['user'],
                                                                           messagequeue['password'],
                                                                           server)
                        connection = pika.BlockingConnection(pika.URLParameters(connection_string))
                        channel = connection.channel()
                        channel.basic_publish('', 'ovs-watcher', str(time.time()),
                                              pika.BasicProperties(content_type='text/plain', delivery_mode=1))
                        connection.close()
                        good_node = True
                    except Exception as message:
                        self.log_message(target, '  Error during rabbitMQ test on node {0}: {1}'.format(server, message), 2)
                if good_node is False:
                    self.log_message(target, '  No working rabbitMQ node could be found', 2)
                    return False
                self.log_message(target, '  RabbitMQ test OK', 0)
                self.log_message(target, 'All tests OK', 0)
                return True
        except Exception as ex:
            self.log_message(target, 'Unexpected exception: {0}'.format(ex), 2)
            return False

展开阅读全文