def set_policy(self, name, policyStr): if name is None: name = DEFAULT_POLICY_NAME with self.policy_sem: oldStr = self.policy_strings.get(name) if policyStr is None: try: del self.policy_strings[name] self.logger.info("Deleted policy '%s'", name) except KeyError: pass else: self.policy_strings[name] = policyStr try: self.code = get_code(Evaluator(), self._cat_policies()) except PolicyError, e: self.logger.warn("Unable to load policy: %s" % e) if oldStr is None: del self.policy_strings[name] else: self.policy_strings[name] = oldStr return False if policyStr: self.logger.info("Loaded policy '%s'", name) return True
def evaluate(self, host, guest_list): results = [] # each run needs separate evaluator so the stack is clean evaluator = Evaluator() evaluator.stack.set('Host', host, alloc=True) evaluator.stack.set('Guests', guest_list, alloc=True) with self.policy_sem: try: for expr in self.code: results.append(evaluator.eval(expr)) self.logger.debug("Results: %s" % results) except PolicyError as e: self.logger.error("Policy error: %s" % e) return False except Exception as e: self.logger.error( "Unexpected error when evaluating policy: %s" % e) return False return True