def reset_env(): try: os.system( 'rm -rf temp/dict_ac_port_uuid_local_port.pkl temp/dict_local_port_uuid_port.pkl temp/off_dev_list.pkl temp/on_dev_list.pkl temp/used_port_list' ) except Exception, e: error_log('reset_env: %s' % e) return False
def query_status(): # 先读取on_dev_list 和 off_dev_list (各有3组) file_on_dev_list = open('./temp/on_dev_list.pkl', 'rb') on_dev_list = pickle.load(file_on_dev_list) file_on_dev_list.close() file_off_dev_list = open('./temp/off_dev_list.pkl', 'rb') off_dev_list = pickle.load(file_off_dev_list) file_off_dev_list.close() # 需要获取uuid和ad_port的对应关系 file_dict_uuid_ad_port = open('./temp/dict_uuid_ad_port.pkl', 'rb') dict_uuid_ad_port = pickle.load(file_dict_uuid_ad_port) file_dict_uuid_ad_port.close() # 需要获取uuid和auth_code的对应关系 file_dict_uuid_auth_code = open('./temp/dict_uuid_auth_code.pkl', 'rb') dict_uuid_auth_code = pickle.load(file_dict_uuid_auth_code) file_dict_uuid_auth_code.close() file_dev_list = open('./temp/dev_list.pkl', 'rb') dev_list = pickle.load(file_dev_list) file_dev_list.close() # 先查询本地的LOCAL STATUS res_local = get_local_status(dict_uuid_ad_port) if res_local[0] != 0: return False, 'LOCAL' on_dev_list_local = res_local[2] off_dev_list_local = res_local[3] # 查询云端STATUS SERVER res_cloud = get_cloud_status(dev_list, dict_uuid_auth_code) if res_cloud[0] != 0: return False, 'SERVER' on_dev_list_cloud = res_cloud[2] off_dev_list_cloud = res_cloud[3] # 比较三个有关状态的列表是否一致, 写个单独的程序比较合适 res_check = [] res_check = check_dev_status(on_dev_list, off_dev_list, on_dev_list_local, off_dev_list_local, on_dev_list_cloud, off_dev_list_cloud) if res_check[0]: return True, '' else: error_log('[STATUS] RPS 云状态查询结果|设备端STATUS查询结果|在线设备设备号三者不一致') return res_check
def client_send_data(host, port, dest_port, queue): for _ in range(1): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) server_address = (host, int(port)) # error_log(port) print 'Connecting to (%s : %s) ' % server_address try: sock.connect(server_address) except Exception, e: error_log('[CLIENT ECHO] Connect Socket error: %s ' % e) queue.put(False) try: message = port print 'Sending: %s to %s' % (message, dest_port) sock.sendall(message) sock.settimeout(5) data = sock.recv(1024) print 'Received:%s ' % data if (message + ':' + str(dest_port)) == data: pass else: error_log( '[CLIENT ECHO] The data received by the Client is (%s), but the message send form this client (%s)' % (data, message)) except Exception, e: error_log('[CLIENT ECHO] sendall/recv error: %s' % e) queue.put(False)
def list_bind(): # 获取AgentClient端口列表 file_ac_port_list = open('./temp/ac_port_list.pkl', 'rb') ac_port_list = pickle.load(file_ac_port_list) file_ac_port_list.close() # 加个_list表示这是listbind得到的 _dict_ac_port_uuid_local_port = {} res_list = all_ac_port_list_bind(ac_port_list) if res_list[0] != 0: return False _dict_ac_port_uuid_local_port = res_list[2] # 读取上一步生成的dict_ac_port_local_port_bind (加上bind以示区别) file_dict_ac_port_uuid_local_port = open( './temp/dict_ac_port_uuid_local_port.pkl', 'rb') dict_ac_port_uuid_local_port_bind = pickle.load( file_dict_ac_port_uuid_local_port) file_dict_ac_port_uuid_local_port.close() dict_local_port_uuid_port = {} dict_local_port_uuid_port = res_list[3] file_dict_local_port_uuid_port = open( './temp/dict_local_port_uuid_port.pkl', 'wb') pickle.dump(dict_local_port_uuid_port, file_dict_local_port_uuid_port) file_dict_local_port_uuid_port.close() file_on_dev_list = open('./temp/on_dev_list.pkl', 'rb') on_dev_list = pickle.load(file_on_dev_list) file_on_dev_list.close() res_check = check_list_bind(on_dev_list, ac_port_list, _dict_ac_port_uuid_local_port, dict_ac_port_uuid_local_port_bind) if res_check: return True else: error_log('[LIST BIND] listbind 的结果与createbind 不一致, 检查失败') return False
def cli_list_bind(host, port, header, body): try: conn = httplib.HTTPConnection(host, port) except Exception, e: error_log('[LIST BIND] Connect to agentclient failed: %s' % e) return 1, 'Connect to agentclient failed: %s' % e
import json import httplib from log_api import error_log def cli_create_bind(host, port, header, body): try: conn = httplib.HTTPConnection(host, port) except Exception, e: error_log('[CREATE BIND] Connect to agentclient failed: %s' % e) return 1, 'Connect to agentclient failed: %s' % e try: conn.request('POST', '/createbind', body, header) except Exception, e: error_log('[CREATE BIND] access clientdevice request failed') return 1, 'access clientdevice request failed' try: resp = conn.getresponse() except Exception, e: error_log('[CREATE BIND] client get response failed with error: %s' % e) return 1, 'client get response failed with error: %s' % e if resp.status == 200: resp_body = resp.read() resp_body = json.loads(resp_body) conn.close() # INFO: 这里是一定要有返回值的(返回的是json) return 0, '', resp_body['local_port']
def reg_dev(): # 获取 aa ip/prt 列表 aa_ip_list, aa_port_list = get_aa_ip_port_list() # 获取总的 dev_list file_dev_list = open('./temp/dev_list.pkl', 'rb') dev_list = pickle.load(file_dev_list) file_dev_list.close() # 获取uuid:port对应关系dict file_dict_uuid_port = open('./temp/dict_uuid_port.pkl', 'rb') dict_uuid_port = pickle.load(file_dict_uuid_port) file_dict_uuid_port.close() # 获取uuid对应的ad_port 对应关系 dict file_dict_uuid_ad_port = open('./temp/dict_uuid_ad_port.pkl', 'rb') dict_uuid_ad_port = pickle.load(file_dict_uuid_ad_port) file_dict_uuid_ad_port.close() # 设备要注册到auth服务器上 if not os.path.exists('./temp/dict_uuid_auth_code.pkl'): res_new = new_auth_code(dev_list) if res_new[0] != 0: exec_ret_dicts.update({ 'status': res_new[0], 'target': res_new[0], 'errors': res_new[1] }) error_log('[AUTH SERVER]' + exec_ret_dicts['errors']) return False, 'AUTH' file_dict_uuid_auth_code = open('./temp/dict_uuid_auth_code.pkl', 'wb') pickle.dump(res_new[2], file_dict_uuid_auth_code) file_dict_uuid_auth_code.close() # 获取每组在线设备数目 file_amount_on = open('./temp/amount_on.pkl', 'rb') amount_on = pickle.load(file_amount_on) file_amount_on.close() on_dev_list = [] # 会包含三个on list off_dev_list = [] # 会包含三个off list for dev_group_list in dev_list: on_list, off_list = get_on_off_dev_list(dev_group_list, amount_on) on_dev_list.append(on_list) off_dev_list.append(off_list) # 将on_dev_list/off_dev_list固化, 供后面使用 file_on_dev_list = open('./temp/on_dev_list.pkl', 'wb') pickle.dump(on_dev_list, file_on_dev_list) file_on_dev_list.close() file_off_dev_list = open('./temp/off_dev_list.pkl', 'wb') pickle.dump(off_dev_list, file_off_dev_list) file_off_dev_list.close() for index in range(3): res_set = on_dev_set_config(on_dev_list, dict_uuid_port, dict_uuid_ad_port, aa_ip_list, aa_port_list, index) if res_set[0] != 0: return False, 'SET' res_del = off_dev_del_config(off_dev_list, dict_uuid_ad_port, index) if res_del[0] != 0: return False, 'DEL' # setconfig 后立即去查服务器垣断是没有准备好的, 所以先sleep一下 time.sleep(5) return True, ''
def agentdevice(command): ret = os.system(command) if ret: error_log('[AGENT DEVICE] ' + ret)
def echo_server(host, port): try: # 创建 TCP socket 作为监听 socket listen_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) except socket.error, msg: error_log("[DEVICE ECHO] create socket failed")
Q_CLI_TH = Queue.Queue() def echo_server(host, port): try: # 创建 TCP socket 作为监听 socket listen_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) except socket.error, msg: error_log("[DEVICE ECHO] create socket failed") try: # 设置 SO_REUSEADDR 选项 listen_fd.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) except socket.error, msg: error_log("[DEVICE ECHO] setsocketopt SO_REUSEADDR failed") try: # 进行 bind -- 此处未指定 ip 地址,即 bind 了全部网卡 ip 上 listen_fd.bind((host, port)) except socket.error, msg: error_log("[DEVICE ECHO] bind failed") try: # 设置 listen 的 backlog 数 listen_fd.listen(10) except socket.error, msg: error_log('[DEVICE ECHO] %s' % msg) try: # 创建 epoll 句柄
def dev_status(host, port, header, body): try: conn = httplib.HTTPConnection(host, port) except Exception, e: error_log('[LOCAL STATUS] Connected agentdevice failed: %s' % e) return 1, 'Connected agentdevice failed: %s' % e
def dev_del_config(host, port, header, body): try: conn = httplib.HTTPConnection(host, port) except Exception, e: error_log('[DEL CONFIG] Connected agentdevice failed: %s' % e) return 1, 'Connected agentdevice failed: %s' % e
import json import httplib from log_api import error_log def dev_set_config(host, port, header, body): try: conn = httplib.HTTPConnection(host, port) except Exception, e: error_log('[SET CONFIG] Connected agentdevice failed: %s ' % e) return 1, 'Connected agentdevice failed: %s ' % e try: conn.request('POST', '/setconfig', body, header) except Exception, e: error_log('[SET CONFIG] access agentdevice request failed %s ' % e) return 1, 'access agentdevice request failed %s ' % e try: resp = conn.getresponse() except Exception, e: error_log('[SET CONFIG] device get response failed with error: %s ' % e) return 1, 'device get response failed with error: %s ' % e if resp.status == 200: conn.close() return 0, '' else: status = resp.status error_log('[SET CONFIG] device setconfig getresponse status: %s' %
def agentclient(command): ret = os.system(command) if ret: error_log('[AGENT CLIENT] ' + ret)