def client_read(self, path, **kwargs): """Retrieve a value from a consul key.""" recurse = kwargs.get('recursive') or kwargs.get('all', False) res = self.client.kv.get(path, recurse=recurse) if kwargs.get('watch', False): return res[0] else: if res[1] is not None: return res[1].get('Value') if not recurse else res[1] else: raise KeyNotFound("The key %s was not found in consul" % path)
def dump_directory(self, path, **kwargs): """Return a dict made of all image names and their corresponding check info""" templates = {} try: directory = self.client.read( path, recursive=True, timeout=kwargs.get('timeout', DEFAULT_TIMEOUT), ) except EtcdKeyNotFound: raise KeyNotFound("The key %s was not found in etcd" % path) except TimeoutError, e: raise e
def recursive_mtime(self, path): """Recursively walks the children from the given path to find the maximum modification time""" try: data, stat = self.client.get(path) children = self.client.get_children(path) if children is not None and len(children) > 0: for child in children: new_path = '/'.join([path.rstrip('/'), child]) return max(stat.mtime, self.recursive_mtime(new_path)) else: return stat.mtime except NoNodeError: raise KeyNotFound("The key %s was not found in Zookeeper" % path)
def client_read(self, path, **kwargs): """Retrieve a value from a consul key.""" recurse = kwargs.get('recursive') or kwargs.get('all', False) res = self.client.kv.get(path, recurse=recurse) if kwargs.get('watch', False): return res[0] elif kwargs.get('all', False): # we use it in _populate_identifier_to_checks return [(child.get('Key'), child.get('Value')) for child in res[1]] else: if res[1] is not None: return res[1].get('Value') if not recurse else res[1] else: raise KeyNotFound("The key %s was not found in consul" % path)
def client_read(self, path, **kwargs): """Retrieve a value from a Zookeeper key.""" try: if kwargs.get('watch', False): return self.recursive_mtime(path) elif kwargs.get('all', False): # we use it in _populate_identifier_to_checks results = [] self.recursive_list(path, results) return results else: res, stats = self.client.get(path) return res.decode("utf-8") except NoNodeError: raise KeyNotFound("The key %s was not found in Zookeeper" % path)
def client_read(self, path, **kwargs): """Retrieve a value from a etcd key.""" try: res = self.client.read(path, timeout=kwargs.get('timeout', DEFAULT_TIMEOUT), recursive=kwargs.get('recursive', False)) if kwargs.get('watch', False) is True: return res.etcd_index else: return res.value except EtcdKeyNotFound: raise KeyNotFound("The key %s was not found in etcd" % path) except TimeoutError, e: raise e
def recursive_list(self, path, results): """Recursively walks the children from the given path and build a list of key/value tuples""" try: data, stat = self.client.get(path) if data: node_as_string = data.decode("utf-8") if not node_as_string: results.append((path.decode("utf-8"), node_as_string)) children = self.client.get_children(path) if children is not None: for child in children: new_path = '/'.join([path.rstrip('/'), child]) self.recursive_list(new_path, results) except NoNodeError: raise KeyNotFound("The key %s was not found in Zookeeper" % path)
def client_read(self, path, **kwargs): """Retrieve a value from a etcd key.""" try: res = self.client.read(path, timeout=kwargs.get('timeout', DEFAULT_TIMEOUT), recursive=kwargs.get('recursive') or kwargs.get('all', False)) if kwargs.get('watch', False): modified_indices = (res.modifiedIndex, ) + tuple( leaf.modifiedIndex for leaf in res.leaves) return max(modified_indices) else: return res.value except EtcdKeyNotFound: raise KeyNotFound("The key %s was not found in etcd" % path) except TimeoutError as e: raise e
def dump_directory(self, path, **kwargs): """Return a dict made of all image names and their corresponding check info""" templates = {} path = path.lstrip('/') try: directory = self.client_read( path, recursive=True, ) except KeyNotFound: raise KeyNotFound("The key %s was not found in consul" % path) for leaf in directory: image = leaf.get('Key').split('/')[-2] param = leaf.get('Key').split('/')[-1] value = leaf.get('Value') if image not in templates: templates[image] = {} templates[image][param] = value return templates
def dump_directory(self, path, **kwargs): """Return a dict made of all image names and their corresponding check info""" templates = {} try: directory = self.client.read( path, recursive=True, timeout=kwargs.get('timeout', DEFAULT_TIMEOUT), ) except EtcdKeyNotFound: raise KeyNotFound("The key %s was not found in etcd" % path) except TimeoutError as e: raise e for leaf in directory.leaves: image = leaf.key.split('/')[-2] param = leaf.key.split('/')[-1] value = leaf.value if image not in templates: templates[image] = {} templates[image][param] = value return templates