Exemple #1
0
class PolicyEngine(threading.Thread):
    """
    At a regular interval, this thread triggers system reconfiguration by
    sampling host and guest data, evaluating the policy and reporting the
    results to all enabled Controller plugins.
    """
    def __init__(self, config, hypervisor_iface, host_monitor, guest_manager):
        threading.Thread.__init__(self, name="PolicyEngine")
        self.setDaemon(True)
        self.config = config
        self.logger = logging.getLogger('mom.PolicyEngine')
        self.properties = {
            'hypervisor_iface': hypervisor_iface,
            'host_monitor': host_monitor,
            'guest_manager': guest_manager,
        }

        self.policy = Policy()
        self.load_policy()
        self.start()

    def load_policy(self):
        def read_policy(file_name, policy_name):
            try:
                with open(file_name, 'r') as f:
                    policyStr = f.read()
            except IOError, e:
                self.logger.warn("Unable to read policy file: %s" % e)
                return False
            return self.policy.set_policy(policy_name, policyStr)

        fname = self.config.get('main', 'policy')
        if fname:
            return read_policy(fname, None)

        policy_dir = self.config.get('main', 'policy-dir')
        if policy_dir:
            try:
                names = sorted(os.listdir(policy_dir))
            except OSError, e:
                self.logger.warn("Unable to read directory '%s': %s" % (
                                    policy_dir, e.strerror))
                return False
            for name in names:
                if name.startswith('.') or not name.endswith('.policy'):
                    continue
                fname = os.path.join(policy_dir, name)
                read_policy(fname, name.split('.policy')[0])
            return True