def wait_for_cluster(cluster_name): """ Waits for an Arakoon cluster to be available (by sending a nop) """ from ovs.extensions.db.arakoon.ArakoonManagement import ArakoonManagementEx from ovs.extensions.db.arakoon.arakoon.ArakoonExceptions import ArakoonSockReadNoBytes last_exception = None tries = 3 while tries > 0: try: cluster_object = ArakoonManagementEx().getCluster(str(cluster_name)) client = cluster_object.getClient() client.nop() return True except ArakoonSockReadNoBytes as exception: last_exception = exception tries -= 1 time.sleep(1) raise last_exception
def wait_for_cluster(cluster_name): """ Waits for an Arakoon cluster to be available (by sending a nop) """ logger.debug('Waiting for cluster {0}'.format(cluster_name)) from ovs.extensions.db.arakoon.arakoon.ArakoonExceptions import ArakoonSockReadNoBytes last_exception = None tries = 3 while tries > 0: try: cluster_object = ArakoonManagementEx().getCluster( str(cluster_name)) client = cluster_object.getClient() client.nop() logger.debug( 'Waiting for cluster {0}: available'.format(cluster_name)) return True except ArakoonSockReadNoBytes as exception: last_exception = exception tries -= 1 time.sleep(1) raise last_exception
class ArakoonStore(object): """ Arakoon client wrapper: * Uses json serialisation * Raises generic exception """ def __init__(self, cluster): """ Initializes the client """ self._cluster = ArakoonManagementEx().getCluster(cluster) self._client = self._cluster.getClient() self._lock = Lock() @locked() def get(self, key): """ Retrieves a certain value for a given key """ try: return json.loads(ArakoonStore._try(self._client.get, key)) except ValueError: raise KeyNotFoundException('Could not parse JSON stored for {0}'.format(key)) except ArakoonNotFound as field: raise KeyNotFoundException(field) @locked() def set(self, key, value): """ Sets the value for a key to a given value """ return ArakoonStore._try(self._client.set, key, json.dumps(value)) @locked() def prefix(self, prefix, max_elements=10000): """ Lists all keys starting with the given prefix """ return ArakoonStore._try(self._client.prefix, prefix, maxElements=max_elements) @locked() def delete(self, key): """ Deletes a given key from the store """ try: return ArakoonStore._try(self._client.delete, key) except ArakoonNotFound as field: raise KeyNotFoundException(field) @locked() def nop(self): """ Executes a nop command """ return ArakoonStore._try(self._client.nop) @locked() def exists(self, key): """ Check if key exists """ return ArakoonStore._try(self._client.exists, key) @staticmethod def _try(method, *args, **kwargs): """ Tries to call a given method, retry-ing if Arakoon is temporary unavailable """ last_exception = None tries = 5 while tries > 0: try: return method(*args, **kwargs) except ArakoonSockReadNoBytes as exception: last_exception = exception tries -= 1 time.sleep(1) raise last_exception
def services_running(target): 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: 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) 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: from ovs.extensions.storage.persistentfactory import PersistentFactory PersistentFactory.store = None persistent = PersistentFactory.get_client() persistent.set(key, value) if persistent.get(key) == value: persistent.delete(key) break persistent.delete(key) 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.db.arakoon.ArakoonManagement import ArakoonManagementEx cluster = ArakoonManagementEx().getCluster('voldrv') client = cluster.getClient() 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 ConfigParser import RawConfigParser from ovs.plugin.provider.configuration import Configuration rmq_ini = RawConfigParser() rmq_ini.read(os.path.join(Configuration.get('ovs.core.cfgdir'), 'rabbitmqclient.cfg')) rmq_nodes = [node.strip() for node in rmq_ini.get('main', 'nodes').split(',')] rmq_servers = map(lambda n: rmq_ini.get(n, 'location'), rmq_nodes) good_node = False for server in rmq_servers: try: connection_string = '{0}://{1}:{2}@{3}/%2F'.format(Configuration.get('ovs.core.broker.protocol'), Configuration.get('ovs.core.broker.login'), Configuration.get('ovs.core.broker.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 break 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', 1) return True except Exception as ex: _log(target, 'Unexpected exception: {0}'.format(ex), 2) return False
class ArakoonStore(object): """ Arakoon client wrapper: * Uses json serialisation * Raises generic exception """ def __init__(self, cluster): """ Initializes the client """ self._cluster = ArakoonManagementEx().getCluster(cluster) self._client = self._cluster.getClient() self._lock = Lock() self._batch_size = 100 @locked() def get(self, key): """ Retrieves a certain value for a given key """ try: return json.loads(ArakoonStore._try(self._client.get, key)) except ValueError: raise KeyNotFoundException('Could not parse JSON stored for {0}'.format(key)) except ArakoonNotFound as field: raise KeyNotFoundException(field) @locked() def set(self, key, value): """ Sets the value for a key to a given value """ return ArakoonStore._try(self._client.set, key, json.dumps(value)) @locked() def prefix(self, prefix): """ Lists all keys starting with the given prefix """ next_prefix = ArakoonStore._next_key(prefix) batch = None while batch is None or len(batch) > 0: batch = ArakoonStore._try(self._client.range, beginKey=prefix if batch is None else batch[-1], beginKeyIncluded=batch is None, endKey=next_prefix, endKeyIncluded=False, maxElements=self._batch_size) for item in batch: yield item @locked() def delete(self, key): """ Deletes a given key from the store """ try: return ArakoonStore._try(self._client.delete, key) except ArakoonNotFound as field: raise KeyNotFoundException(field) @locked() def nop(self): """ Executes a nop command """ return ArakoonStore._try(self._client.nop) @locked() def exists(self, key): """ Check if key exists """ return ArakoonStore._try(self._client.exists, key) @staticmethod def _try(method, *args, **kwargs): """ Tries to call a given method, retry-ing if Arakoon is temporary unavailable """ last_exception = None tries = 5 while tries > 0: try: return method(*args, **kwargs) except ArakoonSockReadNoBytes as exception: last_exception = exception tries -= 1 time.sleep(1) raise last_exception @staticmethod def _next_key(key): """ Calculates the next key (to be used in range queries) """ encoding = 'ascii' # For future python 3 compatibility array = bytearray(str(key), encoding) for index in range(len(array) - 1, -1, -1): array[index] += 1 if array[index] < 128: while array[-1] == 0: array = array[:-1] return str(array.decode(encoding)) array[index] = 0 return '\xff'
def services_running(target): 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: 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) 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: from ovs.extensions.storage.persistentfactory import PersistentFactory PersistentFactory.store = None persistent = PersistentFactory.get_client() persistent.set(key, value) if persistent.get(key) == value: persistent.delete(key) break persistent.delete(key) 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.db.arakoon.ArakoonManagement import ArakoonManagementEx cluster = ArakoonManagementEx().getCluster('voldrv') client = cluster.getClient() 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 configobj import ConfigObj from ovs.plugin.provider.configuration import Configuration rmq_ini = ConfigObj( os.path.join(Configuration.get('ovs.core.cfgdir'), 'rabbitmqclient.cfg')) rmq_nodes = rmq_ini.get('main')['nodes'] if type( rmq_ini.get('main')['nodes']) == list else [ rmq_ini.get('main')['nodes'] ] rmq_servers = map(lambda m: rmq_ini.get(m)['location'], rmq_nodes) good_node = False for server in rmq_servers: try: connection_string = '{0}://{1}:{2}@{3}/%2F'.format( Configuration.get('ovs.core.broker.protocol'), Configuration.get('ovs.core.broker.login'), Configuration.get('ovs.core.broker.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 break 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', 1) return True except Exception as ex: _log(target, 'Unexpected exception: {0}'.format(ex), 2) return False