Example #1
0
class DBProvider(config_provider.ConfigProvider):
    '''config provider which reads config from db.'''
    NAME = 'db'
    CLUSTER_FILTER = config_filter.ConfigFilter(CLUSTER_ALLOWS, CLUSTER_DENIES)
    HOST_FILTER = config_filter.ConfigFilter(HOST_ALLOWS, HOST_DENIES)

    def __init__(self):
        pass

    def getClusterConfig(self, clusterid):
        '''get cluster config from db.'''
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if cluster:
            return cluster.config
        else:
            return {}

    def getHostConfig(self, hostid):
        '''get host config from db.'''
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if host:
            return host.config
        else:
            return {}

    def updateHostConfig(self, hostid, config):
        '''update hsot config to db.'''
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if not host:
            return
        filtered_config = self.HOST_FILTER.filter(config)
        host.config = filtered_config
Example #2
0
 def test_denies(self):
     config = {
         '1': '1',
         '2': {
             '22': '22',
             '33': {
                 '333': '333',
                 '44': '444'
             }
         },
         '3': {
             '33': '44'
         }
     }
     denies = ['/1', '2/22', '2/33/333', '5']
     expected_config = {'2': {'33': {'44': '444'}}, '3': {'33': '44'}}
     filter = config_filter.ConfigFilter(denies=denies)
     filtered_config = filter.filter(config)
     self.assertEqual(filtered_config, expected_config)
     denies = ['*']
     filter = config_filter.ConfigFilter(denies=denies)
     filtered_config = filter.filter(config)
     self.assertIsNone(filtered_config)
     denies = ['*/33']
     expected_config = {'1': '1', '2': {'22': '22'}}
     filter = config_filter.ConfigFilter(denies=denies)
     filtered_config = filter.filter(config)
     self.assertEqual(filtered_config, expected_config)
 def test_init(self):
     config_filter.ConfigFilter(allows={
         'abc': config_filter.AllowRule(),
         'def': config_filter.AllowRule()
     },
                                denies={
                                    'def': config_filter.DenyRule(),
                                    'ghi': config_filter.DenyRule()
                                })
     config_filter.ConfigFilter(allows={
         u'abc': config_filter.AllowRule(),
         u'def': config_filter.AllowRule()
     },
                                denies={
                                    u'def': config_filter.DenyRule(),
                                    u'ghi': config_filter.DenyRule()
                                })
 def test_allows_path(self):
     allows = {
         '/1': config_filter.AllowRule(),
         '2/22': config_filter.AllowRule(),
         '5': config_filter.AllowRule()
     }
     expected_config = {'1': '1', '2': {'22': '22'}}
     configfilter = config_filter.ConfigFilter(allows)
     filtered_config = configfilter.filter(self.config_)
     self.assertEqual(filtered_config, expected_config)
class DBProvider(config_provider.ConfigProvider):
    """config provider which reads config from db.

    .. note::
       All method of this class should be called inside database
       session scope.
    """
    NAME = 'db'
    CLUSTER_FILTER = config_filter.ConfigFilter(
        CLUSTER_ALLOWS, CLUSTER_DENIES)
    HOST_FILTER = config_filter.ConfigFilter(
        HOST_ALLOWS, HOST_DENIES)

    def __init__(self):
        pass

    def get_cluster_config(self, clusterid):
        """Get cluster config from db."""
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if cluster:
            return cluster.config
        else:
            return {}

    def get_host_config(self, hostid):
        """Get host config from db."""
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if host:
            return host.config
        else:
            return {}

    def update_host_config(self, hostid, config):
        """Update hsot config to db."""
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if not host:
            return
        filtered_config = self.HOST_FILTER.filter(config)
        host.config = filtered_config
 def test_allows_asterisks(self):
     """test allows rules."""
     # keys in allows will be copied to dest.
     # if '*' in allows, all keys will be copied to dest.
     allows = {
         '*': config_filter.AllowRule(),
         '3': config_filter.AllowRule(),
         '5': config_filter.AllowRule()
     }
     configfilter = config_filter.ConfigFilter(allows)
     filtered_config = configfilter.filter(self.config_)
     self.assertEqual(filtered_config, self.config_)
 def test_denies(self):
     """test denies rules."""
     # keys in denies list will be removed from filtered config.
     denies = {
         '/1': config_filter.DenyRule(),
         '2/22': config_filter.DenyRule(),
         '2/33/333': config_filter.DenyRule(),
         '5': config_filter.DenyRule()
     }
     expected_config = {'2': {'33': {'44': '444'}}, '3': {'33': '44'}}
     configfilter = config_filter.ConfigFilter(denies=denies)
     filtered_config = configfilter.filter(self.config_)
     self.assertEqual(filtered_config, expected_config)
Example #8
0
 def test_allows(self):
     config = {
         '1': '1',
         '2': {
             '22': '22',
             '33': {
                 '333': '333',
                 '44': '444'
             }
         },
         '3': {
             '33': '44'
         }
     }
     allows = ['*', '3', '5']
     filter = config_filter.ConfigFilter(allows)
     filtered_config = filter.filter(config)
     self.assertEqual(filtered_config, config)
     allows = ['/1', '2/22', '5']
     expected_config = {'1': '1', '2': {'22': '22'}}
     filter = config_filter.ConfigFilter(allows)
     filtered_config = filter.filter(config)
     self.assertEqual(filtered_config, expected_config)
     allows = ['*/33']
     expected_config = {
         '2': {
             '33': {
                 '333': '333',
                 '44': '444'
             }
         },
         '3': {
             '33': '44'
         }
     }
     filter = config_filter.ConfigFilter(allows)
     filtered_config = filter.filter(config)
     self.assertEqual(filtered_config, expected_config)
 def test_allows_asterrisks_in_path(self):
     allows = {'*/33': config_filter.AllowRule()}
     expected_config = {
         '2': {
             '33': {
                 '333': '333',
                 '44': '444'
             }
         },
         '3': {
             '33': '44'
         }
     }
     configfilter = config_filter.ConfigFilter(allows)
     filtered_config = configfilter.filter(self.config_)
     self.assertEqual(filtered_config, expected_config)
Example #10
0
class DBProvider(config_provider.ConfigProvider):
    """config provider which reads config from db.

    .. note::
       All method of this class should be called inside database
       session scope.
    """
    NAME = 'db'
    GET_CLUSTER_FILTER = config_filter.ConfigFilter(GET_CLUSTER_ALLOWS,
                                                    GET_CLUSTER_DENIES)
    GET_HOST_FILTER = config_filter.ConfigFilter(GET_HOST_ALLOWS,
                                                 GET_HOST_DENIES)
    UPDATE_CLUSTER_FILTER = config_filter.ConfigFilter(UPDATE_CLUSTER_ALLOWS,
                                                       UPDATE_CLUSTER_DENIES)
    UPDATE_HOST_FILTER = config_filter.ConfigFilter(UPDATE_HOST_ALLOWS,
                                                    UPDATE_HOST_DENIES)

    def __init__(self):
        pass

    def get_cluster_config(self, clusterid):
        """Get cluster config from db."""
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if not cluster:
            return {}

        return self.GET_CLUSTER_FILTER.filter(cluster.config)

    def get_host_config(self, hostid):
        """Get host config from db."""
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if not host:
            return {}

        return self.GET_HOST_FILTER.filter(host.config)

    def update_cluster_config(self, clusterid, config):
        """Update cluster config to db."""
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if not cluster:
            return

        cluster.config = self.UPDATE_CLUSTER_FILTER.filter(config)

    def update_host_config(self, hostid, config):
        """Update host config to db."""
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if not host:
            return

        host.config = self.UPDATE_HOST_FILTER.filter(config)

    def update_adapters(self, adapters, roles_per_target_system):
        """Update adapter config to db."""
        session = database.current_session()
        session.query(Adapter).delete()
        session.query(Role).delete()
        for adapter in adapters:
            session.add(Adapter(**adapter))

        for _, roles in roles_per_target_system.items():
            for role in roles:
                session.add(Role(**role))

    def update_switch_filters(self, switch_filters):
        """update switch filters."""
        session = database.current_session()
        switch_filter_tuples = set([])
        session.query(SwitchConfig).delete(synchronize_session='fetch')
        for switch_filter in switch_filters:
            switch_filter_tuple = tuple(switch_filter.values())
            if switch_filter_tuple in switch_filter_tuples:
                logging.debug('ignore adding switch filter: %s', switch_filter)
                continue
            else:
                logging.debug('add switch filter: %s', switch_filter)
                switch_filter_tuples.add(switch_filter_tuple)

            session.add(SwitchConfig(**switch_filter))

    def clean_host_config(self, hostid):
        """clean host config."""
        self.clean_host_installing_progress(hostid)
        session = database.current_session()
        session.query(ClusterHost).filter_by(id=hostid).delete(
            synchronize_session='fetch')
        session.query(HostState).filter_by(id=hostid).delete(
            synchronize_session='fetch')

    def reinstall_host(self, hostid):
        """reinstall host."""
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if not host:
            return

        log_dir = os.path.join(setting.INSTALLATION_LOGDIR, host.fullname, '')
        session.query(LogProgressingHistory).filter(
            LogProgressingHistory.pathname.startswith(log_dir)).delete(
                synchronize_session='fetch')
        if not host.state:
            host.state = HostState()

        host.mutable = False
        host.state.state = 'INSTALLING'
        host.state.progress = 0.0
        host.state.message = ''
        host.state.severity = 'INFO'

    def reinstall_cluster(self, clusterid):
        """reinstall cluster."""
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if not cluster:
            return

        if not cluster.state:
            cluster.state = ClusterState()

        cluster.state.state = 'INSTALLING'
        cluster.mutable = False
        cluster.state.progress = 0.0
        cluster.state.message = ''
        cluster.state.severity = 'INFO'

    def clean_cluster_installing_progress(self, clusterid):
        """clean cluster installing progress."""
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if not cluster:
            return

        if cluster.state and cluster.state.state != 'UNINITIALIZED':
            cluster.mutable = False
            cluster.state.state = 'INSTALLING'
            cluster.state.progress = 0.0
            cluster.state.message = ''
            cluster.state.severity = 'INFO'

    def clean_host_installing_progress(self, hostid):
        """clean host intalling progress."""
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if not host:
            return

        log_dir = os.path.join(setting.INSTALLATION_LOGDIR, host.fullname, '')
        session.query(LogProgressingHistory).filter(
            LogProgressingHistory.pathname.startswith(log_dir)).delete(
                synchronize_session='fetch')
        if host.state and host.state.state != 'UNINITIALIZED':
            host.mutable = False
            host.state.state = 'INSTALLING'
            host.state.progress = 0.0
            host.state.message = ''
            host.state.severity = 'INFO'

    def clean_cluster_config(self, clusterid):
        """clean cluster config."""
        session = database.current_session()
        session.query(Cluster).filter_by(id=clusterid).delete(
            synchronize_session='fetch')
        session.query(ClusterState).filter_by(id=clusterid).delete(
            synchronize_session='fetch')

    def get_cluster_hosts(self, clusterid):
        """get cluster hosts."""
        session = database.current_session()
        hosts = session.query(ClusterHost).filter_by(
            cluster_id=clusterid).all()
        return [host.id for host in hosts]

    def get_clusters(self):
        """get clusters."""
        session = database.current_session()
        clusters = session.query(Cluster).all()
        return [cluster.id for cluster in clusters]

    def get_switch_and_machines(self):
        """get switches and machines."""
        session = database.current_session()
        switches = session.query(Switch).all()
        switches_data = []
        switch_machines_data = {}
        for switch in switches:
            switches_data.append({
                'ip': switch.ip,
                'vendor_info': switch.vendor_info,
                'credential': switch.credential,
                'state': switch.state,
            })
            switch_machines_data[switch.ip] = []
            for machine in switch.machines:
                switch_machines_data[switch.ip].append({
                    'mac': machine.mac,
                    'port': machine.port,
                    'vlan': machine.vlan,
                })

        return switches_data, switch_machines_data

    def update_switch_and_machines(self, switches, switch_machines):
        """update switches and machines."""
        session = database.current_session()
        session.query(Switch).delete(synchronize_session='fetch')
        session.query(Machine).delete(synchronize_session='fetch')
        for switch_data in switches:
            switch = Switch(**switch_data)
            logging.info('add switch %s', switch)
            session.add(switch)
            for machine_data in switch_machines.get(switch.ip, []):
                machine = Machine(**machine_data)
                logging.info('add machine %s under %s', machine, switch)
                machine.switch = switch
                session.add(machine)
 def tet_deneis_asterisks_in_path(self):
     denies = {'*/33': config_filter.DenyRule()}
     expected_config = {'1': '1', '2': {'22': '22'}}
     configfilter = config_filter.ConfigFilter(denies=denies)
     filtered_config = configfilter.filter(self.config_)
     self.assertEqual(filtered_config, expected_config)
 def test_denies_asterisks(self):
     denies = {'*': config_filter.DenyRule()}
     configfilter = config_filter.ConfigFilter(denies=denies)
     filtered_config = configfilter.filter(self.config_)
     self.assertIsNone(filtered_config)