Esempio n. 1
0
 def __init__(self):
     self.neutron_agent_list = neutron_api.agent_list()
     self.dhcp = 0
     self.l3 = 0
    def add_neutron_host(self):
        data = neutron.agent_list()

        db_dic = {}


        for item in data['agents']:
            if item['binary'] == 'neutron-dhcp-agent' or \
                item['binary'] == 'neutron-l3-agent':
                print 'slllllllllllllllllll%s' % item['binary']
                continue

            # 获取该主机的id
            host_id = asset_models.Host.objects.get(
                hostname=item['host']).id

            if not item['host'] in db_dic:
                # 判断字典里面是否有该主机
                db_dic[item['host']] = {}
                db_dic[item['host']]['host_id'] = host_id
                db_dic[item['host']]['neutron_river_type'] = \
                    settings.NEUTRON_RIVER_TYPE

            binary_name = '%s' % '_'.join(item['binary'].split('-'))
            if item['alive']:
                status = 'up'
            else:
                status = 'down'
            db_dic[item['host']][binary_name] = status
        # print db_dic

        manager_group_obj = asset_models.Group.objects.get(name='Manager')
        manager_group_node_list = manager_group_obj.host_set.select_related()
        compute_group_obj = asset_models.Group.objects.get(name='Compute')
        compute_group_node_list = compute_group_obj.host_set.select_related()

        for host, item in db_dic.items():
            compute_obj = asset_models.Host.objects.get(hostname=host)
            # print item
            if compute_obj in manager_group_node_list:
                # 该主机就是manager主机
                nm_obj = openstack_models.NeutronManagerServiceStatus
                if not nm_obj.objects.filter(
                        host_id=asset_models.Host.objects.get(
                            hostname=host).id
                ):
                    print item
                    # import pdb
                    # pdb.set_trace()
                    nm_obj.objects.create(**item)

                    # 触发事件
                    content = 'Add Neutron Manager: %s' % host
                    event_neutron.neutron_info(host, content)

                # else:
                #   openstack_models.NeutronManagerServiceStatus.objects.filter(
                #         host_id=openstack_models.Host.objects.get(
                #             hostname=item['host']).id
                #     ).update(**item)
            elif compute_obj in compute_group_node_list:
                # 该主机就是compute主机
                nc_obj = openstack_models.NeutronComputeServiceStatus
                if not nc_obj.objects.filter(
                        host_id=asset_models.Host.objects.get(
                            hostname=host).id):
                    print item
                    nc_obj.objects.create(**item)

                    # 触发事件
                    content = 'Add Neutron Compute: %s' % host
                    event_neutron.neutron_info(host, content)