示例#1
0
def dnsys_data_map(data):
    try:
        ver = (0x01).to_bytes(1, byteorder='big')
        cid = (data['id'] % 65535).to_bytes(2, byteorder='big')
        dt = (0x01).to_bytes(1, byteorder='big')
        did = (0x01).to_bytes(1, byteorder='big')
        bt = (sbt_code[data['bt']]['bt']).to_bytes(1, byteorder='big')
        sbt = (sbt_code[data['bt']][data['sbt']]
               if sbt_code[data['bt']][data['sbt']] != 0 else
               diff_code[data['bt']][data['sbt']](data)).to_bytes(
                   2, byteorder='big')
        op = op_code[data['op']]
        opt = (0x00).to_bytes(7, byteorder='big')
        if data['op'] == 'clear':
            bl = (0x00).to_bytes(4, byteorder='big')
            return ver + cid + dt + did + bt + sbt + op + bl + opt
        body = dnsys_data_methods[data['bt']][data['sbt']](data)
        bl = (6 + len(body)).to_bytes(4, byteorder='big')
        ul = (4 + len(body)).to_bytes(2, byteorder='big')
        sn = (data['id']).to_bytes(4, byteorder='big')
        #print(ver,cid,dt,did,bt,sbt,op,bl,opt,ul,sn,body)
        #print(6+len(body))
        return ver + cid + dt + did + bt + sbt + op + bl + opt + ul + sn + body
    except Exception as e:
        conf_logger.error(str(e))
    return None
示例#2
0
async def conf_ybind(client, data):
    try:
        # 此处处理data 转换成ybind需要的格式 然后发给ybind ybind的url接口比较丰富,此处需要根据不同数据请求不同url
        #后续if else去掉 ybind所有配置需适配
        if data['bt'] in ybind_map and data['sbt'] in ybind_map[data['bt']]:
            method, url, ybind_data = ybind_map[data['bt']][data['sbt']](data)
            conf_logger.debug(
                'send data to ybind method={} url={} data={}'.format(
                    method, url, ybind_data))
            return {'rcode': 0, 'description': 'Success'}
            if method == 'post':
                async with client.post(url, json=ybind_data,
                                       timeout=10) as resp:
                    return await resp.json()
            if method == 'put':
                async with client.put(url, json=ybind_data,
                                      timeout=10) as resp:
                    return await resp.json()
            elif method == 'delete':
                async with client.delete(url, json=ybind_data,
                                         timeout=10) as resp:
                    return await resp.json()
        else:
            conf_logger.debug('ybind not support conf: {}'.format(data))
            return {'rcode': 0, 'description': 'Success'}
    except Exception as e:
        conf_logger.error(str(e))
        return {'rcode': 999, 'description': str(e)}
    return {'rcode': 999, 'description': 'unknow error'}
示例#3
0
def beat_connect(ip, port):
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(5)
        sock.connect((ip, port))
        sock.close()
        return True
    except Exception as e:
        conf_logger.error(str(e))
    return False
示例#4
0
def dnsys_result_check(data):
    #print(data)
    try:
        rc = data[8]
        if rc in rc_code:
            #print(rc_code[rc])
            return rc_code[rc]
    except Exception as e:
        conf_logger.error(str(e))
        return str(e)
    return 'failed'
示例#5
0
async def conf_agent(url, client, data):
    try:
        async with client.post(url, json=data, timeout=10) as resp:
            return await resp.json()
    except Exception as e:
        conf_logger.error(str(e))
    return [{
        'id': i['id'],
        'status': 'failed',
        'msg': 'send error'
    } for i in data['contents']]
示例#6
0
async def conf_dnsys(client, data):
    try:
        send_data = dnsys_data_map(data)
        print(send_data, len(send_data))
        if send_data == None:
            return [{
                'id': data['id'],
                'status': 'failed',
                'msg': 'unsupported conf'
            }]
        tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        tcp_socket.settimeout(10)
        tcp_socket.connect(dnsys_addr)
        tcp_socket.send(send_data)
        recv_data = tcp_socket.recv(1024)
        res = dnsys_result_check(recv_data)
        tcp_socket.close()
        if res == 'success':
            return [{'id': data['id'], 'status': 'success', 'msg': ''}]
        return [{'id': data['id'], 'status': 'failed', 'msg': res}]
    except Exception as e:
        conf_logger.error(str(e))
        return [{'id': data['id'], 'status': 'failed', 'msg': str(e)}]
    return [{'id': data['id'], 'status': 'failed', 'msg': 'unknow error'}]
示例#7
0
async def pub_conf(data):
    async with aiohttp.ClientSession() as client:
        if data['bt'] in fpga_bt:
            fpga_res = await conf_dnsys(client, data)
            conf_logger.debug('recv data from dnsys: {}'.format(fpga_res))
            try:
                if fpga_res[0]['status'] == 'success':
                    #可能还需调用ybind commit接口
                    content.add_oplog(data, 'success', '')
                    handle_real_data(data)
                    conf_logger.info('conf success: {}'.format(data))
                    return 'success'
                else:
                    content.add_oplog(data, 'fail', fpga_res[0]['msg'])
                    conf_logger.info('conf failed: {}'.format(data))
                    return fpga_res[0]['msg']
            except Exception as e:
                conf_logger.error(str(e))
                content.add_oplog(data, 'fail', 'fpga api return result error')
                conf_logger.info('conf failed: {}'.format(data))
                return 'fpga api return result error'
        elif data['bt'] in ybind_bt:
            ybind_res = await conf_ybind(client, data)
            conf_logger.debug('recv ybind:{}'.format(ybind_res))
            try:
                if ybind_res['description'] == 'Success':
                    content.add_oplog(data, 'success', '')
                    handle_real_data(data)
                    conf_logger.info('conf success: {}'.format(data))
                    return 'success'
                else:
                    content.add_oplog(data, 'fail', ybind_res['description'])
                    conf_logger.info('conf failed: {}'.format(data))
                    return ybind_res['description']
            except Exception as e:
                conf_logger.error(str(e))
                content.add_oplog(data, 'fail',
                                  'ybind api return result error')
                conf_logger.info('conf failed: {}'.format(data))
                return 'ybind api return result error'
        else:
            fpga_res = await conf_dnsys(client, data)
            ybind_res = await conf_ybind(client, data)
            conf_logger.debug('recv data from ybind:{} dnsys:{}'.format(
                ybind_res, fpga_res))
            try:
                if ybind_res['description'] == 'Success' and fpga_res[0][
                        'status'] == 'success':
                    #可能还需调用ybind commit接口
                    content.add_oplog(data, 'success', '')
                    handle_real_data(data)
                    conf_logger.info('conf success {}'.format(data))
                    return 'success'
                elif ybind_res['description'] == 'Success' and fpga_res[0][
                        'status'] != 'success':
                    # ybind回退操作
                    #ybind_res = await rollback_ybind(client,data)
                    content.add_oplog(
                        data, 'fail',
                        'ybind conf success and fpga conf failed')
                    conf_logger.info('conf failed: {}'.format(data))
                    return fpga_res[0]['msg']
                elif ybind_res['description'] != 'Success' and fpga_res[0][
                        'status'] == 'success':
                    # fpga回滚操作
                    #fpga_res = await rollback_fpga(client,data)
                    content.add_oplog(
                        data, 'fail',
                        'dnsys conf success and ybind conf failed')
                    conf_logger.info('conf failed: {}'.format(data))
                    return ybind_res['description']
                else:
                    content.add_oplog(data, 'fail',
                                      'dnsys and ybind conf failed')
                    conf_logger.info('conf failed: {}'.format(data))
                    return 'dnsys and ybind conf failed'
            except Exception as e:
                conf_logger.error(str(e))
                content.add_oplog(data, 'fail', 'api return result error')
                conf_logger.info('conf failed: {}'.format(data))
                return 'api return result error'