def _rewalk_tree(self, event=None): """ Clear children list and rewalk the tree starting at self.node. If the node does not exist, set a watch. When the node is created the watch will trigger the recursive walk. :type event: kazoo.protocol.states.WatchedEvent or None """ if self.zkclient.exists(self.node, watch=self._rewalk_tree): # setting a watch on grandparent node for additional children self.zkclient.get_children(self.node, watch=self._rewalk_tree) new_nodes = list() self._walk(self.node, new_nodes) self._update_children_list(new_nodes) map(lambda x: x.start(), self._children) else: # This is a placeholder for when the path the ZKHGC is given a path # that doesn't exist # met is False b/c if the path doesn't exist we don't want to succeed. self._children.append( create_dummy(comp=self._comp_name, parent=self._parent)) self._log.warning('Node {0} does not exist. Will wait until it ' 'does.'.format(self.node))
def _update_children_list(self, new_nodes): """ Remove any dummy predicates from children. Using the list of paths found in the tree walk, if we have an object that matches that path, keep it, add new objects, delete any extras. :type new_nodes: list of str """ # remove dummy predicates if they exist existing_objs = copy.copy(self._children) for child in existing_objs: if child == create_dummy(comp=self._comp_name, parent=self._parent): self._children.remove(child) # remove obsolete objects existing_nodes = [i.node for i in self._children] for n in existing_nodes: if n in set(existing_nodes) - set(new_nodes): self._log.debug('Removing obsolete node: {0}'.format(n)) temp = ZookeeperHasChildren('', '', n) # create a dummy self._children.remove(temp) # add new # This currently has a limitation that nodes created at a deeper level # are not picked up automatically. For example if the base node is /A, # static nodes at /A/B or /A/D WILL be picked up, but if /A/B/C is # added later it WILL NOT be picked up until the next restart for node in set(new_nodes) - set(existing_nodes): self._log.debug('Adding new node: {0}'.format(node)) zk_child = ZookeeperHasChildren(self._comp_name, self.zkclient, node, operational=self._operational, met_on_delete=True, parent='zk.has.gc') zk_child.add_callback({"zk_hgc": self._callback}) self._children.append(zk_child)
def create(self, xmlpart, callback=None, parent=None): """ :type xmlpart: xml.etree.ElementTree.Element :type callback: types.FunctionType or None :type parent: str or None """ if xmlpart is None: # A dummy predicate will be returned if there are no predicates # met is true b/c we don't want to block on no predicates return create_dummy(comp=self._component_name, parent=self._action, met=True) if isinstance(xmlpart, str): root = ElementTree.fromstring(xmlpart) else: root = xmlpart if parent is None: parent = self._action ptype = verify_attribute(root, 'type').lower() operational = bool(verify_attribute(root, 'operational', none_allowed=True)) if ptype == 'simple': return self._ensure_new( SimplePredicate(self._component_name, operational=operational, parent=parent), callback=callback ) elif ptype == PredicateType.ZOOKEEPERNODEEXISTS: return self._ensure_new( ZookeeperNodeExists(self._component_name, self.zkclient, verify_attribute(root, 'path'), operational=operational, parent=parent), callback=callback ) elif ptype == PredicateType.ZOOKEEPERHASCHILDREN: return self._ensure_new( ZookeeperHasChildren( self._component_name, self.zkclient, verify_attribute(root, 'path'), ephemeral_only=verify_attribute(root, 'ephemeral_only', none_allowed=True, default=True), operational=operational, parent=parent), callback=callback ) elif ptype == PredicateType.ZOOKEEPERHASGRANDCHILDREN: return self._ensure_new( ZookeeperHasGrandChildren( self._component_name, self.zkclient, verify_attribute(root, 'path'), ephemeral_only=verify_attribute(root, 'ephemeral_only', none_allowed=True, default=True), operational=operational, parent=parent), callback=callback ) elif ptype == PredicateType.ZOOKEEPERGLOB: return self._ensure_new( ZookeeperGlob( self._component_name, self.zkclient, verify_attribute(root, 'path'), ephemeral_only=verify_attribute(root, 'ephemeral_only', none_allowed=True, default=True), operational=operational, parent=parent), callback=callback ) elif ptype == PredicateType.ZOOKEEPERGOODUNTILTIME: return self._ensure_new( ZookeeperGoodUntilTime(self._component_name, self.zkclient, verify_attribute(root, 'path'), operational=operational, parent=parent), callback=callback ) elif ptype == PredicateType.PROCESS: return self._ensure_new( PredicateProcess(self._component_name, self._proc_client, verify_attribute(root, 'interval', cast=float), operational=operational, parent=parent), callback=callback ) elif ptype == PredicateType.API: return self._ensure_new( APIPredicate(self._component_name, verify_attribute(root, 'url'), verb=verify_attribute(root, 'verb', none_allowed=True, default='GET'), expected_code=verify_attribute(root, 'expected_code', none_allowed=True, cast=int, default=200), interval=verify_attribute(root, 'interval', cast=float), operational=operational, parent=parent), callback=callback ) elif ptype == PredicateType.HEALTH: return self._ensure_new( PredicateHealth(self._component_name, verify_attribute(root, 'command'), verify_attribute(root, 'interval', cast=float), self._system, operational=operational, parent=parent), callback=callback ) elif ptype == PredicateType.HOLIDAY: return self._ensure_new(PredicateHoliday(self._component_name, self.zkclient, path=self._holiday_path, operational=operational, parent=parent), callback=callback ) elif ptype == PredicateType.WEEKEND: return self._ensure_new(PredicateWeekend(self._component_name, operational=operational, parent=parent), callback=callback ) elif ptype == PredicateType.TIMEWINDOW: return self._ensure_new( TimeWindow(self._component_name, begin=verify_attribute(root, 'begin', none_allowed=True), end=verify_attribute(root, 'end', none_allowed=True), weekdays=verify_attribute(root, 'weekdays', none_allowed=True), operational=operational, parent=parent), callback=callback ) # below, use recursion to get nested predicates elif ptype == PredicateType.NOT: for element in root.findall('Predicate'): dep = self.create(element, callback=callback) return self._ensure_new( PredicateNot(self._component_name, dep, parent=self._parent_name(parent, 'not')) ) elif ptype == PredicateType.AND: deps = list() for element in root.findall('Predicate'): deps.append(self.create(element, callback=callback, parent=self._parent_name(parent, 'and'))) return self._ensure_new( PredicateAnd(self._component_name, deps, parent=parent) ) elif ptype == PredicateType.OR: deps = list() for element in root.findall('Predicate'): deps.append(self.create(element, callback=callback, parent=self._parent_name(parent, 'or'))) return self._ensure_new( PredicateOr(self._component_name, deps, parent=parent) ) else: self._log.error('Unknown predicate type "{0}". Ignoring' .format(ptype)) # create dummy if it is an unknown predicate type. # met is set to true b/c we don't want to block on error return create_dummy(comp=self._component_name, parent=self._action, met=True)
def create(self, xmlpart, callback=None, parent=None): """ :type xmlpart: xml.etree.ElementTree.Element :type callback: types.FunctionType or None :type parent: str or None """ if xmlpart is None: # A dummy predicate will be returned if there are no predicates # met is true b/c we don't want to block on no predicates return create_dummy(comp=self._component_name, parent=self._action, met=True) if isinstance(xmlpart, str): root = ElementTree.fromstring(xmlpart) else: root = xmlpart if parent is None: parent = self._action ptype = verify_attribute(root, 'type').lower() operational = bool( verify_attribute(root, 'operational', none_allowed=True)) if ptype == 'simple': return self._ensure_new(SimplePredicate(self._component_name, operational=operational, parent=parent), callback=callback) elif ptype == PredicateType.ZOOKEEPERNODEEXISTS: return self._ensure_new(ZookeeperNodeExists( self._component_name, self.zkclient, verify_attribute(root, 'path'), operational=operational, parent=parent), callback=callback) elif ptype == PredicateType.ZOOKEEPERHASCHILDREN: return self._ensure_new(ZookeeperHasChildren( self._component_name, self.zkclient, verify_attribute(root, 'path'), ephemeral_only=verify_attribute(root, 'ephemeral_only', none_allowed=True, default=True), operational=operational, parent=parent), callback=callback) elif ptype == PredicateType.ZOOKEEPERHASGRANDCHILDREN: return self._ensure_new(ZookeeperHasGrandChildren( self._component_name, self.zkclient, verify_attribute(root, 'path'), ephemeral_only=verify_attribute(root, 'ephemeral_only', none_allowed=True, default=True), operational=operational, parent=parent), callback=callback) elif ptype == PredicateType.ZOOKEEPERGLOB: return self._ensure_new(ZookeeperGlob( self._component_name, self.zkclient, verify_attribute(root, 'path'), ephemeral_only=verify_attribute(root, 'ephemeral_only', none_allowed=True, default=True), operational=operational, parent=parent), callback=callback) elif ptype == PredicateType.ZOOKEEPERGOODUNTILTIME: return self._ensure_new(ZookeeperGoodUntilTime( self._component_name, self.zkclient, verify_attribute(root, 'path'), operational=operational, parent=parent), callback=callback) elif ptype == PredicateType.PROCESS: return self._ensure_new(PredicateProcess(self._component_name, self._proc_client, verify_attribute( root, 'interval', cast=float), operational=operational, parent=parent), callback=callback) elif ptype == PredicateType.API: return self._ensure_new(APIPredicate( self._component_name, verify_attribute(root, 'url'), verb=verify_attribute(root, 'verb', none_allowed=True, default='GET'), expected_code=verify_attribute(root, 'expected_code', none_allowed=True, cast=int, default=200), interval=verify_attribute(root, 'interval', cast=float), operational=operational, parent=parent), callback=callback) elif ptype == PredicateType.HEALTH: return self._ensure_new(PredicateHealth( self._component_name, verify_attribute(root, 'command'), verify_attribute(root, 'interval', cast=float), self._system, operational=operational, parent=parent), callback=callback) elif ptype == PredicateType.HOLIDAY: return self._ensure_new(PredicateHoliday(self._component_name, self.zkclient, path=self._holiday_path, operational=operational, parent=parent), callback=callback) elif ptype == PredicateType.WEEKEND: return self._ensure_new(PredicateWeekend(self._component_name, operational=operational, parent=parent), callback=callback) elif ptype == PredicateType.TIMEWINDOW: return self._ensure_new(TimeWindow( self._component_name, begin=verify_attribute(root, 'begin', none_allowed=True), end=verify_attribute(root, 'end', none_allowed=True), weekdays=verify_attribute(root, 'weekdays', none_allowed=True), operational=operational, parent=parent), callback=callback) # below, use recursion to get nested predicates elif ptype == PredicateType.NOT: for element in root.findall('Predicate'): dep = self.create(element, callback=callback) return self._ensure_new( PredicateNot(self._component_name, dep, parent=self._parent_name(parent, 'not'))) elif ptype == PredicateType.AND: deps = list() for element in root.findall('Predicate'): deps.append( self.create(element, callback=callback, parent=self._parent_name(parent, 'and'))) return self._ensure_new( PredicateAnd(self._component_name, deps, parent=parent)) elif ptype == PredicateType.OR: deps = list() for element in root.findall('Predicate'): deps.append( self.create(element, callback=callback, parent=self._parent_name(parent, 'or'))) return self._ensure_new( PredicateOr(self._component_name, deps, parent=parent)) else: self._log.error( 'Unknown predicate type "{0}". Ignoring'.format(ptype)) # create dummy if it is an unknown predicate type. # met is set to true b/c we don't want to block on error return create_dummy(comp=self._component_name, parent=self._action, met=True)