def _update_from(self, rule, component): """ Update the component with its action. Substitution of variables is done. """ # Substitute variable names by their value in the action # string first. var_map = _get_var_map(component.id, component.name, component.type, component.category, self.ruleset.name, rule.name, rule.help) action = substitute(var_map, rule.action) _LOGGER.info("%s.action(%s): %s", rule.name, component.id, action) key = rule.name force = self.force_for_rule.get(rule.name) if force is not None: key += '?force=' + force if action is not None: self.dag.add_node_attribute(component.id, (key, action)) component.actions[key] = action
def _filter_impl(self, component): var_map = _get_var_map(component.id, component.name, component.type, component.category, self.rule.ruleset, self.rule.name) var_value = substitute(var_map, self.var) match = re.match(self.pattern, var_value) return (match and self.eq == '=~') or (not match and self.eq == '!~')
def _filter_impl(self, component): var_map = _get_var_map(component.id, component.name, component.type, component.category, self.rule.ruleset, self.rule.name, self.rule.help) cmd_string = substitute(var_map, self.rule.filter) cmd = shlex.split(to_str_from_unicode(cmd_string, should_be_uni=True)) _LOGGER.debug("%s: calling filter cmd: %s", self.rule.name, cmd) try: popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=-1) # Use system default except OSError as ose: _LOGGER.error("%s: can't call filter '%s': %s", self.rule.name, cmd_string, ose) return False (msg_std, msg_err) = popen.communicate() msg_std = msg_std.strip() msg_err = msg_err.strip() _LOGGER.debug("%s: output of filter command %s on component %s: %s", self.rule.name, cmd_string, component, msg_std) if len(msg_err) != 0: _LOGGER.warning("%s: error when applying filter command " + \ "%s to component %s: %s", self.rule.name, cmd_string, component, msg_err) _LOGGER.debug("%s: filter command: %s RC: %d", self.rule.name, cmd_string, popen.returncode) return popen.returncode == os.EX_OK
def _get_deps(self, component, rule): """ Find dependencies of a given component. This implies calling the rule.depsfinder script. Substitution of variables is done. Returns None if the given rule has already been applied on the given component. """ result = dict() depsfinder = rule.depsfinder if rule.dependson is None or len(rule.dependson) == 0 or \ depsfinder is None or len(depsfinder) == 0: _LOGGER.debug("No 'DepsFinder' or 'DependsOn' specified" + \ " in rule %s for component %s. Skipping.", rule, component) return result var_map = _get_var_map(component.id, component.name, component.type, component.category, self.ruleset.name, rule.name, rule.help) cmd = substitute(var_map, depsfinder) _LOGGER.debug("Calling depsfinder for component %s: %s", component, cmd) popen_args = shlex.split(to_str_from_unicode(cmd, should_be_uni=True)) try: popen = subprocess.Popen(popen_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=-1) # Use system default except OSError as ose: _LOGGER.error("Can't call depsfinder '%s': %s", cmd, ose) return result (msg_std, msg_err) = popen.communicate() msg_std = msg_std.strip() msg_err = msg_err.strip() if len(msg_err) != 0: _LOGGER.warning("Depsfinder error when " + \ "applying rule %s to component %s: %s", rule, component, msg_err) deps = set() with StringIO(to_unicode(msg_std)) as reader: for dep in reader: dep_id = dep.strip() if len(dep_id) == 0: continue dependency = self.components_map.get(dep_id) if dependency is None: _LOGGER.debug("Creating dep for component %s with id: %r", component, dep_id) dependency = Component(dep_id) self.components_map[dep_id] = dependency deps.add(dependency) _update_graph_with_node(self.dag, dep_id) if _LOGGER.isEnabledFor(INFO): _LOGGER.info("%s.depsfinder(%s): %s", rule.name, component.id, NodeSet.fromlist([str(x.id) for x in deps])) # Find match only on rule.dependson return _find_match([self.ruleset.rules_for[x] for x in rule.dependson], deps)