class Policy: def __init__(self, policy_string): self.logger = logging.getLogger('mom.Policy') self.policy_string = policy_string self.evaluator = Evaluator() self.code = get_code(self.evaluator, self.policy_string) def get_string(self): return self.policy_string def evaluate(self, host, guest_list): results = [] self.evaluator.stack.set('Host', host, alloc=True) self.evaluator.stack.set('Guests', guest_list, alloc=True) try: for expr in self.code: results.append(self.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
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
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
class Policy: def __init__(self, policy_string): self.policy_string = policy_string self.evaluator = Evaluator() self.code = get_code(self.evaluator, self.policy_string) def get_string(self): return self.policy_string def evaluate(self, host, guest_list): results = [] self.evaluator.stack.set('Host', host, alloc=True) self.evaluator.stack.set('Guests', guest_list, alloc=True) try: for expr in self.code: results.append(self.evaluator.eval(expr)) print "Results: %s" % results except Exception as e: print "Exception when processing rules: %s" % e return False return True
def __init__(self, policy_string): self.logger = logging.getLogger('mom.Policy') self.policy_string = policy_string self.evaluator = Evaluator() self.code = get_code(self.evaluator, self.policy_string)
def __init__(self, policy_string): self.policy_string = policy_string self.evaluator = Evaluator() self.code = get_code(self.evaluator, self.policy_string)