def _create_session(self, test_connection=False): """ Create a consulate.session object, and query for its leader to ensure that the connection is made. :param test_connection: call .leader() to ensure that the connection is valid :type test_connection: bool :return consulate.Session instance """ session = consulate.Session(host=self.host, port=self.port) if test_connection: session.status.leader() return session
def kv_get_solution(logger, scheduler_type, identifier): """ Upload data to controller storage. :param logger: Logger object. :param scheduler_type: Type of the scheduler to avoif races. :param identifier: Task identifier. :return: None """ key = 'solutions/{}/{}'.format(scheduler_type, identifier) session = consulate.Session() try: return json.loads(session.kv[key]) except (AttributeError, KeyError) as err: logger.warning( "Cannot obtain key {!r} from key-value storage: {!r}".format( key, err))
def kv_upload_solution(logger, identifier, scheduler_type, dataset): """ Upload data to controller storage. :param logger: Logger object. :param identifier: Task identifier. :param scheduler_type: Scheduler type. :param dataset: Data to save about the solution. This should be dictionary. :return: None """ key = 'solutions/{}/{}'.format(scheduler_type, identifier) session = consulate.Session() try: session.kv[key] = json.dumps(dataset) return except (AttributeError, KeyError): logger.warning("Cannot save key {!r} to key-value storage".format(key))
def get_topology(servicename, blocking=True): """ Retrieve network topology from Consul's key/value store. Parameters ---------- servicename : Name of the service blocking : Retry if session to the Consul server may not be established (Default value = True) Returns ------- list of dicts List of dicts in shape of, example: `[{"outgate": "emailsink", "queue": "encrypted", "type": "PUSH"}, {"addresses": ["tcp://0.0.0.0:4567"], "queue": "plaintext", "type": "PULL"}]` """ with err.applicationbound(): answered = False while not answered: try: session = consulate.Session() except Exception as e: log.warn(str(e)) if blocking: time.sleep(10) log.warn("Retrying...") continue else: return [] try: return session.kv['sdn-services-{name}'.format( name=servicename)]['links'] except KeyError as e: log.error(e) return {}
def kv_clear_solutions(logger, scheduler_type, identifier=None): """ Upload data to controller storage. :param logger: Logger object. :param scheduler_type: Type of the scheduler to avoif races. :param identifier: Task identifier. :return: None """ try: session = consulate.Session() if isinstance(identifier, str): session.kv.delete('solutions/{}/{}'.format(scheduler_type, identifier), recurse=True) else: session.kv.delete('solutions/{}'.format(scheduler_type), recurse=True) except (AttributeError, KeyError): logger.warning("Key-value storage is inaccessible")
def put_topology(topology, blocking=True): """ Store network topology to Consul's key/value store. Parameters ---------- topology : dict Topology definition blocking : Retry if session to the Consul server may not be established (Default value = True) Returns ------- """ with err.applicationbound(): session = consulate.Session() for service, links in topology.iteritems(): session.kv['sdn-services-{name}'.format(name=service)] = links return [key for key in session.kv.find('sdn-services')]
def __init__(self, credentials: Credentials): self.__session: Session = consulate.Session( host=credentials.host(), port=credentials.port(), datacenter=credentials.datacenter(), token=credentials.token())
return rv rv_services = {} services = get_all_services() for service in services: # skip consul if 'consul' == service: continue instances = get_available_instances(service) if instances: rv_services[service] = instances logging.info(rv_services) session = consulate.Session() def get_all_service_keys(): prefix = 'services/' _ = session.kv.find(prefix, keys=True) if _ == []: return records = json.loads(_.decode('UTF8')) logging.info(records) return records records = get_all_service_keys()
import getpass import os import sys import consulate from fabric.api import run, local, cd, env, roles, execute import requests CODE_DIR = '/home/deploy/www/iodocs' env.forward_agent = True consul = consulate.Session() def group_map(): ansible_group_list = consul.kv.find('ansible/groups') ansible_group_map = {} for path, groups in ansible_group_list.iteritems(): host = os.path.basename(path) for group in groups.split(','): if group in ansible_group_map: ansible_group_map[group].append(host) else: ansible_group_map[group] = [host] return ansible_group_map