def get(self): """ Get relevant variables from consul. :rtype: dict """ session = consulate.Consulate() # track number of servers counter = 0 # prepare data for jinja to consume for the jinja template data = {"servers": {}} # get a list of available servers in the cluster accessible_addresses = [srv["Addr"] for srv in session.agent.members()] # session.catalog.services() returns a list with a single dictionary services = session.catalog.services() # get all names of services provided by cluster service_keys = [] if isinstance(services, list) and len(services) > 0 and isinstance( services[0], dict): service_keys = services[0].keys() elif isinstance(services, dict): service_keys = services.keys() for service in service_keys: data["servers"][service] = [] # figure out servers with that service servers = session.catalog.service(service) for server in servers: ip_address = server["Address"] # only add server if it's in the current cluster if ip_address in accessible_addresses: data["servers"][service].append((counter, ip_address)) counter += 1 return data
def getConsulateSession(): return consulate.Consulate(CONSUL_URL)
KV_ALERTING_HASHES = "alerting/hashes" WARNING_STATE = "warning" CRITICAL_STATE = "critical" PASSING_STATE = "passing" UNKNOWN_STATE = "unknown" ANY_STATE = "any" CONSUL_HOST = "0.0.0.0" CONSUL_PORT = 8500 if sys.version_info >= (2, 6, 0): consul = consulate.Consul(host=CONSUL_HOST,port=CONSUL_PORT) consul._adapter.timeout = 5 else: consul = consulate.Consulate(host=CONSUL_HOST,port=CONSUL_PORT) consul._adapter.timeout = 5 logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) if sys.version_info >= (2, 7, 0): handler = logging.StreamHandler(stream=sys.stdout) else: handler = logging.StreamHandler(strm=sys.stdout) handler.setLevel(logging.INFO) formatter = logging.Formatter("%(asctime)s Filename= %(filename)s Level= %(levelname)s " "LineNumber=%(lineno)d %(message)s")
def setUp(self): self.session = consulate.Consulate() self.used_keys = list()
def consul(self): if not self._consul: self._consul = session = consulate.Consulate(self.host, self.port) return self._consul