• 服务系统 server端


    from django.shortcuts import render,HttpResponse
    import json
    # Create your views here.
    from repository import models
    from aip.plugins import Plugins
    
    def server(request):
        server_dict=json.loads(request.body.decode('utf-8'))
        if not server_dict['basic']['status']:
            return HttpResponse('iiiiiiiii')
    
        # hostname=server_dict['basic']['data']['hostname']
        # server_obj=models.Server.objects.filter(hostname=hostname).first()
    
        cls=Plugins()
        response=cls.process(server_dict)
        print(response)
        print(type(response))
        return HttpResponse(json.dumps(response))
    Views
    from django.shortcuts import render,HttpResponse
    from django.conf import settings
    from repository import models
    import importlib
    from .basic import Basic
    class Plugins(object):
        def __init__(self):
            self.settings=settings.PLUGIN_ITEMS
            self.basic_key='basic'
            self.board_key='board'
            print(self.basic_key)
    
        def process(self,server_dict):
            hostname = server_dict['basic']['data']['hostname']
            server_obj = models.Server.objects.filter(hostname=hostname).first()
    
            ret={'code':1,'msg':None}
            if not server_obj:
                ret['code']=4
                return ret
            #直接传入参数
            obj = Basic(server_obj, server_dict[self.basic_key], server_dict[self.board_key])
            obj.exec()
                # return HttpResponse('请求错误')
            # else:
                #使用内存。网卡等走的流程
            for i,v in self.settings.items():
                try:
                    class_obj,class_name=v.rsplit('.',maxsplit=1)
                    module=importlib.import_module(class_obj)
                    ret=getattr(module,class_name)
                    print(ret,'=======')
                    obj=ret(server_obj,server_dict[i])
                    print(obj,'//[[[[[[')
                    obj.exec()
                    print('////',obj.exec())
                except  Exception as e:
                    ret['code']=4
            return ret
    plugins init
    from django.shortcuts import render,HttpResponse
    from django.conf import settings
    from repository import models
    import importlib
    from .basic import Basic
    class Plugins(object):
        def __init__(self):
            self.settings=settings.PLUGIN_ITEMS
            self.basic_key='basic'
            self.board_key='board'
            print(self.basic_key)
    
    
        def process(self,server_dict):
            ret = {'code': 1, 'msg': None}
            hostname = server_dict['basic']['data']['hostname']
            server_obj = models.Server.objects.filter(hostname=hostname).first()
            print('=====================',server_obj)
    
    
            if not server_obj:
                ret['code']=4
                return ret
            #直接传入参数
    
            obj = Basic(server_obj, server_dict[self.basic_key], server_dict[self.board_key])
            obj.exec()
    
                # return HttpResponse('请求错误')
            # else:
                #使用内存。网卡等走的流程
            for i,v in self.settings.items():
    
                try:
                    class_obj,class_name=v.rsplit('.',maxsplit=1)
    
                    module=importlib.import_module(class_obj)
                    print(module)
                    rets=getattr(module,class_name)
                    print(rets)
    
    
                    obj=rets(server_obj,server_dict[i])
    
                    obj.exec()
                    print(obj.exec())
    
                except  Exception as e:
                    ret['code']=4
            return ret
    from repository import models
    class Basic(object):
        def __init__(self,server_obj,basic_key,board_key):
            self.server_obj=server_obj
            self.basic_key=basic_key
            self.boaed_key=board_key
    
        def exec(self):
    
    
            temp = {}  # 获取新的主机详细信息
            basic_obj = self.basic_key['data']
            board_obj = self.boaed_key['data']
            temp.update(basic_obj)
            temp.update(board_obj)
            # 数据库更改主机的详细信息
            server_obj = models.Server.objects.filter(hostname=self.server_obj.hostname).first()
            temps = []
            for i, new_values in temp.items():
                old_values = getattr(server_obj, i)
                if new_values != old_values:
                    conent = "[%s]的[%s]由[%s]更改为[%s]" % (self.server_obj.hostname, i, old_values, new_values)
                    temps.append(conent)
                    setattr(server_obj, i, new_values)
            server_obj.save()
            if temps:
                models.ServerRecord.objects.create(server_obj=server_obj, content=';'.join(temps),
    basic
    from repository import models
    
    class Disk(object):
        def __init__(self,server_obj,server_dict):
            self.server_obj=server_obj
            self.server_dict=server_dict
            self.temps=[]
    
    
    
    
    
        def exec(self):
            # 配置disk详细信息
            disk_obj = self.server_dict['data']
            new_disk_keys = set(disk_obj.keys())
            disk_list = models.Disk.objects.all()
            old_disk_keys = {item.slot for item in disk_list}
            # print(old_disk_keys)
            app_disk = new_disk_keys - old_disk_keys
            del_disk = old_disk_keys - new_disk_keys
            same_disk = new_disk_keys & old_disk_keys
            if app_disk:
                self.add_disk_exec(app_disk, disk_obj)
    
            if del_disk:
                self.del_disk_exec(del_disk,disk_obj)
            if same_disk:
                self.same_disk_exec(same_disk, disk_obj)
    
            if self.temps:
                models.ServerRecord.objects.create(server_obj=self.server_obj, content=';'.join(self.temps),)
            # 增加硬盘信息
    
    
        def add_disk_exec(self,app_disk,disk_obj):
            for i in app_disk:
                obj = disk_obj[i]
                obj['server_obj'] = self.server_obj
                models.Disk.objects.create(**obj)
                conent = "Disk增加信息[%s]" % obj
                self.temps.append(conent)
            # print(temps)
            # if temps:
            #     models.ServerRecord.objects.create(server_obj=self.server_obj, content=';'.join(temps),
            #                                        creator_id=request.user)
    
            # 删除硬盘信息
        def del_disk_exec(self,del_disk,disk_obj):
            for i in del_disk:
                conent = "Disk删除硬盘信息[%s]" % (disk_obj[i])
                models.Disk.objects.filter(slot__in=i).delete()
                self.temps.append(conent)
                print(conent)
            # if temps:
            #     models.ServerRecord.objects.create(server_obj=server_obj, content=';'.join(temps),
            #                                        creator_id=request.user)
    
            # 共同相同:same_disk
        def same_disk_exec(self,same_disk,disk_obj):
            for i in same_disk:
                new_values = disk_obj[i]
                disk_f = models.Disk.objects.filter(slot=i).first()
                for i, new_values_new in new_values.items():
                    old_values = getattr(disk_f, i)
                    if new_values_new != old_values:
                        setattr(disk_f, i, new_values_new)
                        conent = "[%s]的[%s]由[%s]更改为[%s]" % (i, i, old_values, new_values_new)
                        self.temps.append(conent)
                disk_f.save()
    disk
    from repository import models
    class Memory(object):
        def __init__(self,server_obj,server_dict):
            self.server_obj=server_obj
            self.server_dict=server_dict
            self.temps=[]
    
        def exec(self):
            # 更新memory信息
            memory_obj = self.server_dict['data']
            new_memory_keys = set(memory_obj.keys())
            memory_list = models.Memory.objects.all()
            old_memory_keys = {item.slot for item in memory_list}
            # print(old_disk_keys)
            app_memory = new_memory_keys - old_memory_keys
            del_memory = old_memory_keys - new_memory_keys
            same_memory = new_memory_keys & old_memory_keys
            # 增加硬盘信息
            if app_memory:
                self.add_memory_key( memory_obj, app_memory)
    
            if del_memory:
                self.del_memory_key(del_memory, memory_obj)
            if same_memory:
                self.same_memory_key(same_memory, memory_obj)
            if self.temps:
                models.ServerRecord.objects.create(server_obj=self.server_obj, content=';'.join(self.temps))
    
        def add_memory_key(self,memory_obj,app_memory):
            for i in app_memory:
                obj = memory_obj[i]
                obj['server_obj'] = self.server_obj
                models.Memory.objects.create(**obj)
                conent = "memory增加信息[%s]" % obj
                self.temps.append(conent)
            # print(temps)
            # if temps:
            #     models.ServerRecord.objects.create(server_obj=server_obj, content=';'.join(temps),
            #                                        )
    
            # 删除硬盘信息
        def del_memory_key(self,del_memory,memory_obj):
            for i in del_memory:
                conent = "Disk删除硬盘信息[%s]" % (memory_obj[i])
                models.Memory.objects.filter(slot__in=i).delete()
                self.temps.append(conent)
                # print(conent)
            # if temps:
            #     models.ServerRecord.objects.create(server_obj=server_obj, content=';'.join(temps),
            #                                        )
    
            # 共同相同:same_disk
        def same_memory_key(self,same_memory,memory_obj):
            for i in same_memory:
                new_values = memory_obj[i]
                memory_f = models.Memory.objects.filter(slot=i).first()
                for i, new_values_new in new_values.items():
                    old_values = getattr(memory_f, i)
                    if new_values_new != old_values:
                        setattr(memory_f, i, new_values_new)
                        conent = "[%s]的[%s]由[%s]更改为[%s]" % (i, i, old_values, new_values_new)
                        self.temps.append(conent)
                memory_f.save()
            # if temp:
    memory
    from repository import models
    class NIC(object):
        def __init__(self,server_obj,server_dict):
            self.server_obj=server_obj
            self.nic_dict=server_dict
            self.temps=[]
    
        def exec(self):
            # 配置nic信息
            nic_obj =self.nic_dict['data']
            nic_list = self.server_obj.nic.values('name')
            new_nic_obj = set(self.nic_dict['data'].keys())
            old_nic_obj = {nic['name'] for nic in nic_list}
            # print(new_nic_obj)
            # print(old_nic_obj)
            app_nic = new_nic_obj - old_nic_obj
            del_nic = old_nic_obj - new_nic_obj
            same_nic = new_nic_obj & old_nic_obj
            if app_nic:
                self.add_nic(app_nic,nic_obj)
    
            if del_nic:
                self.del_nic(del_nic)
    
            if same_nic:
                self.same_nic_key(same_nic,nic_obj)
    
            if self.temps:
                models.ServerRecord.objects.create(server_obj=self.server_obj, content=';'.join(self.temps))
            # 增加nic信息
        def add_nic(self,app_nic,nic_obj):
            for item in app_nic:
                values = nic_obj[item]
                values['server_obj'] = self.server_obj
                models.NIC.objects.create(**values)
                conent = "nic增加信息[%s]" % item
                self.temps.append(conent)
                # print(temps)
            # if temps:
            #     models.ServerRecord.objects.create(server_obj=server_obj, content=';'.join(temps))
    
            # 删除硬盘信息
        def del_nic(self,del_nic):
            for item in del_nic:
                conent = "nic删除信息[%s]" % item
                models.NIC.objects.filter(server_obj=self.server_obj, name__in=item).delete()
                self.temps.append(conent)
            # if temps:
            #     models.ServerRecord.objects.create(server_obj=server_obj, content=';'.join(temps))
        def same_nic_key(self,same_nic,nic_obj):
            for name in same_nic:
                value = nic_obj[name]
                obj = models.NIC.objects.filter(server_obj=self.server_obj, name=name).first()
                for k, new_val in value.items():
                    old_val = getattr(obj, k)
                    if old_val != new_val:
                        conent = "名称%s的网卡的%s由%s变更为%s" % (name, k, old_val, new_val)
                        self.temps.append(conent)
                        setattr(obj, k, new_val)
                obj.save()
            # if temps:
    nic

  • 相关阅读:
    文件输出debug
    sweetalert
    js认清this的第一步
    Creating default object from empty value in PHP?
    matplotlib画图
    python解析库
    zabbix监控ssl证书过期时间
    aws 预留实例到期监控
    aws ec2挂载 s3
    aliyun挂载oss
  • 原文地址:https://www.cnblogs.com/1204guo/p/7617671.html
Copyright © 2020-2023  润新知